player: osd: fix zoom
[vlc.git] / modules / codec / cc.c
blob1a0c8e568ad05137d4bcdbfd162759ab35ff4484
1 /*****************************************************************************
2 * cc.c : CC 608/708 subtitles decoder
3 *****************************************************************************
4 * Copyright © 2007-2011 Laurent Aimar, VLC authors and VideoLAN
5 * 2011-2016 VLC authors and VideoLAN
6 * 2016-2017 VideoLabs, VLC authors and VideoLAN
8 * Authors: Laurent Aimar < fenrir # via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 /* The EIA 608 decoder part has been initialy based on ccextractor (GPL)
29 * and rewritten */
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
35 #include <assert.h>
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_codec.h>
40 #include <vlc_charset.h>
42 #include "substext.h"
43 #include "cea708.h"
45 #if 0
46 #define Debug(code) code
47 #else
48 #define Debug(code)
49 #endif
51 /*****************************************************************************
52 * Module descriptor.
53 *****************************************************************************/
54 static int Open ( vlc_object_t * );
55 static void Close( vlc_object_t * );
57 #define OPAQUE_TEXT N_("Opacity")
58 #define OPAQUE_LONGTEXT N_("Setting to true " \
59 "makes the text to be boxed and maybe easier to read." )
61 vlc_module_begin ()
62 set_shortname( N_("CC 608/708"))
63 set_description( N_("Closed Captions decoder") )
64 set_capability( "spu decoder", 50 )
65 set_category( CAT_INPUT )
66 set_subcategory( SUBCAT_INPUT_SCODEC )
67 set_callbacks( Open, Close )
69 add_bool( "cc-opaque", true,
70 OPAQUE_TEXT, OPAQUE_LONGTEXT, false )
71 vlc_module_end ()
73 /*****************************************************************************
74 * Local prototypes
75 *****************************************************************************/
76 typedef enum
78 EIA608_MODE_POPUP = 0,
79 EIA608_MODE_ROLLUP_2 = 1,
80 EIA608_MODE_ROLLUP_3 = 2,
81 EIA608_MODE_ROLLUP_4 = 3,
82 EIA608_MODE_PAINTON = 4,
83 EIA608_MODE_TEXT = 5
84 } eia608_mode_t;
86 typedef enum
88 EIA608_COLOR_WHITE = 0,
89 EIA608_COLOR_GREEN = 1,
90 EIA608_COLOR_BLUE = 2,
91 EIA608_COLOR_CYAN = 3,
92 EIA608_COLOR_RED = 4,
93 EIA608_COLOR_YELLOW = 5,
94 EIA608_COLOR_MAGENTA = 6,
95 EIA608_COLOR_USERDEFINED = 7
96 } eia608_color_t;
98 typedef enum
100 EIA608_FONT_REGULAR = 0x00,
101 EIA608_FONT_ITALICS = 0x01,
102 EIA608_FONT_UNDERLINE = 0x02,
103 EIA608_FONT_UNDERLINE_ITALICS = EIA608_FONT_UNDERLINE | EIA608_FONT_ITALICS
104 } eia608_font_t;
106 #define EIA608_SCREEN_ROWS 15
107 #define EIA608_SCREEN_COLUMNS 32
109 #define EIA608_MARGIN 0.10
110 #define EIA608_VISIBLE (1.0 - EIA608_MARGIN * 2)
111 #define FONT_TO_LINE_HEIGHT_RATIO 1.06
113 struct eia608_screen // A CC buffer
115 uint8_t characters[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1];
116 eia608_color_t colors[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1];
117 eia608_font_t fonts[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1]; // Extra char at the end for a 0
118 int row_used[EIA608_SCREEN_ROWS]; // Any data in row?
120 typedef struct eia608_screen eia608_screen;
122 typedef enum
124 EIA608_STATUS_DEFAULT = 0x00,
125 EIA608_STATUS_CHANGED = 0x01, /* current screen has been altered */
126 EIA608_STATUS_CAPTION_ENDED = 0x02, /* screen flip */
127 EIA608_STATUS_CAPTION_CLEARED = 0x04, /* active screen erased */
128 EIA608_STATUS_DISPLAY = EIA608_STATUS_CAPTION_CLEARED | EIA608_STATUS_CAPTION_ENDED,
129 } eia608_status_t;
131 static const struct {
132 eia608_color_t i_color;
133 eia608_font_t i_font;
134 int i_column;
135 } pac2_attribs[]= {
136 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 0 },
137 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 0 },
138 { EIA608_COLOR_GREEN, EIA608_FONT_REGULAR, 0 },
139 { EIA608_COLOR_GREEN, EIA608_FONT_UNDERLINE, 0 },
140 { EIA608_COLOR_BLUE, EIA608_FONT_REGULAR, 0 },
141 { EIA608_COLOR_BLUE, EIA608_FONT_UNDERLINE, 0 },
142 { EIA608_COLOR_CYAN, EIA608_FONT_REGULAR, 0 },
143 { EIA608_COLOR_CYAN, EIA608_FONT_UNDERLINE, 0 },
144 { EIA608_COLOR_RED, EIA608_FONT_REGULAR, 0 },
145 { EIA608_COLOR_RED, EIA608_FONT_UNDERLINE, 0 },
146 { EIA608_COLOR_YELLOW, EIA608_FONT_REGULAR, 0 },
147 { EIA608_COLOR_YELLOW, EIA608_FONT_UNDERLINE, 0 },
148 { EIA608_COLOR_MAGENTA, EIA608_FONT_REGULAR, 0 },
149 { EIA608_COLOR_MAGENTA, EIA608_FONT_UNDERLINE, 0 },
150 { EIA608_COLOR_WHITE, EIA608_FONT_ITALICS, 0 },
151 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE_ITALICS, 0 },
153 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 0 },
154 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 0 },
155 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 4 },
156 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 4 },
157 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 8 },
158 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 8 },
159 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 12 },
160 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 12 },
161 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 16 },
162 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 16 },
163 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 20 },
164 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 20 },
165 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 24 },
166 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 24 },
167 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 28 },
168 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 28 } ,
171 #define EIA608_COLOR_DEFAULT EIA608_COLOR_WHITE
173 static const int rgi_eia608_colors[] = {
174 0xffffff, // white
175 0x00ff00, // green
176 0x0000ff, // blue
177 0x00ffff, // cyan
178 0xff0000, // red
179 0xffff00, // yellow
180 0xff00ff, // magenta
181 0xffffff, // user defined XXX we use white
184 typedef struct
186 /* Current channel (used to reject packet without channel information) */
187 int i_channel;
189 /* */
190 int i_screen; /* Displayed screen */
191 eia608_screen screen[2];
193 struct
195 int i_row;
196 int i_column;
197 } cursor;
199 /* */
200 eia608_mode_t mode;
201 eia608_color_t color;
202 eia608_font_t font;
203 int i_row_rollup;
205 /* Last command pair (used to reject duplicated command) */
206 struct
208 uint8_t d1;
209 uint8_t d2;
210 } last;
211 } eia608_t;
213 static void Eia608Init( eia608_t * );
214 static eia608_status_t Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] );
215 static void Eia608FillUpdaterRegions( subtext_updater_sys_t *p_updater, eia608_t *h );
217 /* It will be enough up to 63 B frames, which is far too high for
218 * broadcast environment */
219 #define CC_MAX_REORDER_SIZE (64)
220 typedef struct
222 int i_queue;
223 block_t *p_queue;
225 int i_field;
226 int i_channel;
228 int i_reorder_depth;
230 cea708_demux_t *p_dtvcc;
232 cea708_t *p_cea708;
233 eia608_t *p_eia608;
234 bool b_opaque;
235 } decoder_sys_t;
237 static int Decode( decoder_t *, block_t * );
238 static void Flush( decoder_t * );
240 static void DTVCC_ServiceData_Handler( void *priv, uint8_t i_sid, vlc_tick_t i_time,
241 const uint8_t *p_data, size_t i_data )
243 decoder_t *p_dec = priv;
244 decoder_sys_t *p_sys = p_dec->p_sys;
245 //msg_Err( p_dec, "DTVCC_ServiceData_Handler sid %d bytes %ld", i_sid, i_data );
246 if( i_sid == 1 )
247 CEA708_Decoder_Push( p_sys->p_cea708, i_time, p_data, i_data );
250 /*****************************************************************************
251 * Open: probe the decoder and return score
252 *****************************************************************************
253 * Tries to launch a decoder and return score so that the interface is able
254 * to chose.
255 *****************************************************************************/
256 static int Open( vlc_object_t *p_this )
258 decoder_t *p_dec = (decoder_t*)p_this;
259 decoder_sys_t *p_sys;
261 if( ( p_dec->fmt_in.i_codec != VLC_CODEC_CEA608 ||
262 p_dec->fmt_in.subs.cc.i_channel > 3 ) &&
263 ( p_dec->fmt_in.i_codec != VLC_CODEC_CEA708 ||
264 p_dec->fmt_in.subs.cc.i_channel > 63 ) )
265 return VLC_EGENERIC;
267 p_dec->pf_decode = Decode;
268 p_dec->pf_flush = Flush;
270 /* Allocate the memory needed to store the decoder's structure */
271 p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
272 if( p_sys == NULL )
273 return VLC_ENOMEM;
275 if( p_dec->fmt_in.i_codec == VLC_CODEC_CEA608 )
277 /* 0 -> i_field = 0; i_channel = 1;
278 1 -> i_field = 0; i_channel = 2;
279 2 -> i_field = 1; i_channel = 1;
280 3 -> i_field = 1; i_channel = 2; */
281 p_sys->i_field = p_dec->fmt_in.subs.cc.i_channel >> 1;
282 p_sys->i_channel = 1 + (p_dec->fmt_in.subs.cc.i_channel & 1);
284 p_sys->p_eia608 = malloc(sizeof(*p_sys->p_eia608));
285 if( !p_sys->p_eia608 )
287 free( p_sys );
288 return VLC_ENOMEM;
290 Eia608Init( p_sys->p_eia608 );
292 else
294 p_sys->p_dtvcc = CEA708_DTVCC_Demuxer_New( p_dec, DTVCC_ServiceData_Handler );
295 if( !p_sys->p_dtvcc )
297 free( p_sys );
298 return VLC_ENOMEM;
301 p_sys->p_cea708 = CEA708_Decoder_New( p_dec );
302 if( !p_sys->p_cea708 )
304 CEA708_DTVCC_Demuxer_Release( p_sys->p_dtvcc );
305 free( p_sys );
306 return VLC_ENOMEM;
309 p_sys->i_channel = p_dec->fmt_in.subs.cc.i_channel;
312 p_sys->b_opaque = var_InheritBool( p_dec, "cc-opaque" );
313 p_sys->i_reorder_depth = p_dec->fmt_in.subs.cc.i_reorder_depth;
315 p_dec->fmt_out.i_codec = VLC_CODEC_TEXT;
317 return VLC_SUCCESS;
320 /*****************************************************************************
321 * Flush:
322 *****************************************************************************/
323 static void Flush( decoder_t *p_dec )
325 decoder_sys_t *p_sys = p_dec->p_sys;
327 if( p_sys->p_eia608 )
329 Eia608Init( p_sys->p_eia608 );
331 else
333 CEA708_DTVCC_Demuxer_Flush( p_sys->p_dtvcc );
334 CEA708_Decoder_Flush( p_sys->p_cea708 );
337 block_ChainRelease( p_sys->p_queue );
338 p_sys->p_queue = NULL;
339 p_sys->i_queue = 0;
342 /****************************************************************************
343 * Decode: the whole thing
344 ****************************************************************************
346 ****************************************************************************/
347 static void Push( decoder_t *, block_t * );
348 static block_t *Pop( decoder_t *, bool );
349 static void Convert( decoder_t *, vlc_tick_t, const uint8_t *, size_t );
351 static bool DoDecode( decoder_t *p_dec, bool b_drain )
353 block_t *p_block = Pop( p_dec, b_drain );
354 if( !p_block )
355 return false;
357 Convert( p_dec, p_block->i_pts, p_block->p_buffer, p_block->i_buffer );
358 block_Release( p_block );
360 return true;
363 static int Decode( decoder_t *p_dec, block_t *p_block )
365 decoder_sys_t *p_sys = p_dec->p_sys;
367 if( p_block )
369 /* Reset decoder if needed */
370 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
372 /* Drain */
373 for( ; DoDecode( p_dec, true ) ; );
374 if( p_sys->p_eia608 )
376 Eia608Init( p_sys->p_eia608 );
378 else
380 CEA708_DTVCC_Demuxer_Flush( p_sys->p_dtvcc );
381 CEA708_Decoder_Flush( p_sys->p_cea708 );
384 if( (p_block->i_flags & BLOCK_FLAG_CORRUPTED) || p_block->i_buffer < 1 )
386 block_Release( p_block );
387 return VLCDEC_SUCCESS;
391 /* XXX Cc captions data are OUT OF ORDER (because we receive them in the bitstream
392 * order (ie ordered by video picture dts) instead of the display order.
393 * We will simulate a simple IPB buffer scheme
394 * and reorder with pts.
395 * XXX it won't work with H264 which use non out of order B picture or MMCO */
396 if( p_sys->i_reorder_depth == 0 )
398 /* Wait for a P and output all *previous* picture by pts order (for
399 * hierarchical B frames) */
400 if( (p_block->i_flags & BLOCK_FLAG_TYPE_B) == 0 )
401 for( ; DoDecode( p_dec, true ); );
404 Push( p_dec, p_block );
407 const bool b_no_reorder = (p_dec->fmt_in.subs.cc.i_reorder_depth < 0);
408 for( ; DoDecode( p_dec, (p_block == NULL) || b_no_reorder ); );
410 return VLCDEC_SUCCESS;
413 /*****************************************************************************
414 * CloseDecoder: clean up the decoder
415 *****************************************************************************/
416 static void Close( vlc_object_t *p_this )
418 decoder_t *p_dec = (decoder_t *)p_this;
419 decoder_sys_t *p_sys = p_dec->p_sys;
421 free( p_sys->p_eia608 );
422 if( p_sys->p_cea708 )
424 CEA708_Decoder_Release( p_sys->p_cea708 );
425 CEA708_DTVCC_Demuxer_Release( p_sys->p_dtvcc );
428 block_ChainRelease( p_sys->p_queue );
429 free( p_sys );
432 /*****************************************************************************
434 *****************************************************************************/
435 static void Push( decoder_t *p_dec, block_t *p_block )
437 decoder_sys_t *p_sys = p_dec->p_sys;
439 if( p_sys->i_queue >= CC_MAX_REORDER_SIZE )
441 block_Release( Pop( p_dec, true ) );
442 msg_Warn( p_dec, "Trashing a CC entry" );
445 block_t **pp_block;
446 /* find insertion point */
447 for( pp_block = &p_sys->p_queue; *pp_block ; pp_block = &((*pp_block)->p_next) )
449 if( p_block->i_pts == VLC_TICK_INVALID || (*pp_block)->i_pts == VLC_TICK_INVALID )
450 continue;
451 if( p_block->i_pts < (*pp_block)->i_pts )
453 if( p_sys->i_reorder_depth > 0 &&
454 p_sys->i_queue < p_sys->i_reorder_depth &&
455 pp_block == &p_sys->p_queue )
457 msg_Info( p_dec, "Increasing reorder depth to %d", ++p_sys->i_reorder_depth );
459 break;
462 /* Insert, keeping a pts and/or fifo ordered list */
463 p_block->p_next = *pp_block ? *pp_block : NULL;
464 *pp_block = p_block;
465 p_sys->i_queue++;
468 static block_t *Pop( decoder_t *p_dec, bool b_forced )
470 decoder_sys_t *p_sys = p_dec->p_sys;
471 block_t *p_block;
473 if( p_sys->i_queue == 0 )
474 return NULL;
476 if( !b_forced && p_sys->i_queue < CC_MAX_REORDER_SIZE )
478 if( p_sys->i_queue < p_sys->i_reorder_depth || p_sys->i_reorder_depth == 0 )
479 return NULL;
482 /* dequeue head */
483 p_block = p_sys->p_queue;
484 p_sys->p_queue = p_block->p_next;
485 p_block->p_next = NULL;
486 p_sys->i_queue--;
488 return p_block;
491 static subpicture_t *Subtitle( decoder_t *p_dec, eia608_t *h, vlc_tick_t i_pts )
493 //decoder_sys_t *p_sys = p_dec->p_sys;
494 subpicture_t *p_spu = NULL;
496 /* We cannot display a subpicture with no date */
497 if( i_pts == VLC_TICK_INVALID )
498 return NULL;
500 /* Create the subpicture unit */
501 p_spu = decoder_NewSubpictureText( p_dec );
502 if( !p_spu )
503 return NULL;
505 p_spu->i_start = i_pts;
506 p_spu->i_stop = i_pts + VLC_TICK_FROM_SEC(10); /* 10s max */
507 p_spu->b_ephemer = true;
508 p_spu->b_absolute = false;
510 subtext_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
511 decoder_sys_t *p_dec_sys = p_dec->p_sys;
513 /* Set first region defaults */
514 /* The "leavetext" alignment is a special mode where the subpicture
515 region itself gets aligned, but the text inside it does not */
516 p_spu_sys->region.align = SUBPICTURE_ALIGN_TOP|SUBPICTURE_ALIGN_LEFT;
517 p_spu_sys->region.inner_align = SUBPICTURE_ALIGN_BOTTOM|SUBPICTURE_ALIGN_LEFT;
518 p_spu_sys->region.flags = UPDT_REGION_IGNORE_BACKGROUND | UPDT_REGION_USES_GRID_COORDINATES;
520 /* Set style defaults (will be added to segments if none set) */
521 p_spu_sys->p_default_style->i_style_flags |= STYLE_MONOSPACED;
522 if( p_dec_sys->b_opaque )
524 p_spu_sys->p_default_style->i_background_alpha = STYLE_ALPHA_OPAQUE;
525 p_spu_sys->p_default_style->i_features |= STYLE_HAS_BACKGROUND_ALPHA;
526 p_spu_sys->p_default_style->i_style_flags |= STYLE_BACKGROUND;
528 p_spu_sys->margin_ratio = EIA608_MARGIN;
529 p_spu_sys->p_default_style->i_font_color = rgi_eia608_colors[EIA608_COLOR_DEFAULT];
530 /* FCC defined "safe area" for EIA-608 captions is 80% of the height of the display */
531 p_spu_sys->p_default_style->f_font_relsize = EIA608_VISIBLE * 100 / EIA608_SCREEN_ROWS /
532 FONT_TO_LINE_HEIGHT_RATIO;
533 p_spu_sys->p_default_style->i_features |= (STYLE_HAS_FONT_COLOR | STYLE_HAS_FLAGS);
535 Eia608FillUpdaterRegions( p_spu_sys, h );
537 return p_spu;
540 static void Convert( decoder_t *p_dec, vlc_tick_t i_pts,
541 const uint8_t *p_buffer, size_t i_buffer )
543 decoder_sys_t *p_sys = p_dec->p_sys;
545 unsigned i_ticks = 0;
546 while( i_buffer >= 3 )
548 if( (p_buffer[0] & 0x04) /* Valid bit */ )
550 const vlc_tick_t i_spupts = i_pts + vlc_tick_from_samples(i_ticks, 1200/3);
551 /* Mask off the specific i_field bit, else some sequences can be lost. */
552 if ( p_sys->p_eia608 &&
553 (p_buffer[0] & 0x03) == p_sys->i_field )
555 eia608_status_t i_status = Eia608Parse( p_sys->p_eia608,
556 p_sys->i_channel, &p_buffer[1] );
558 /* a caption is ready or removed, process its screen */
560 * In case of rollup/painton with 1 packet/frame, we need
561 * to update on Changed status.
562 * Batch decoding might be incorrect if those in
563 * large number of commands (mp4, ...) then.
564 * see CEAv1.2zero.trp tests */
565 if( i_status & (EIA608_STATUS_DISPLAY | EIA608_STATUS_CHANGED) )
567 Debug(printf("\n"));
568 subpicture_t *p_spu = Subtitle( p_dec, p_sys->p_eia608, i_spupts );
569 if( p_spu )
570 decoder_QueueSub( p_dec, p_spu );
573 else if( p_sys->p_cea708 && (p_buffer[0] & 0x03) >= 2 )
575 CEA708_DTVCC_Demuxer_Push( p_sys->p_dtvcc, i_spupts, p_buffer );
579 i_ticks++;
581 i_buffer -= 3;
582 p_buffer += 3;
587 /*****************************************************************************
589 *****************************************************************************/
590 static void Eia608Cursor( eia608_t *h, int dx )
592 h->cursor.i_column += dx;
593 if( h->cursor.i_column < 0 )
594 h->cursor.i_column = 0;
595 else if( h->cursor.i_column > EIA608_SCREEN_COLUMNS-1 )
596 h->cursor.i_column = EIA608_SCREEN_COLUMNS-1;
598 static void Eia608ClearScreenRowX( eia608_t *h, int i_screen, int i_row, int x )
600 eia608_screen *screen = &h->screen[i_screen];
602 if( x == 0 )
604 screen->row_used[i_row] = false;
606 else
608 screen->row_used[i_row] = false;
609 for( int i = 0; i < x; i++ )
611 if( screen->characters[i_row][i] != ' ' ||
612 screen->colors[i_row][i] != EIA608_COLOR_DEFAULT ||
613 screen->fonts[i_row][i] != EIA608_FONT_REGULAR )
615 screen->row_used[i_row] = true;
616 break;
621 for( ; x < EIA608_SCREEN_COLUMNS+1; x++ )
623 screen->characters[i_row][x] = x < EIA608_SCREEN_COLUMNS ? ' ' : '\0';
624 screen->colors[i_row][x] = EIA608_COLOR_DEFAULT;
625 screen->fonts[i_row][x] = EIA608_FONT_REGULAR;
629 static void Eia608ClearScreenRow( eia608_t *h, int i_screen, int i_row )
631 Eia608ClearScreenRowX( h, i_screen, i_row, 0 );
634 static void Eia608ClearScreen( eia608_t *h, int i_screen )
636 for( int i = 0; i < EIA608_SCREEN_ROWS; i++ )
637 Eia608ClearScreenRow( h, i_screen, i );
640 static int Eia608GetWritingScreenIndex( eia608_t *h )
642 switch( h->mode )
644 case EIA608_MODE_POPUP: // Non displayed screen
645 return 1 - h->i_screen;
647 case EIA608_MODE_ROLLUP_2: // Displayed screen
648 case EIA608_MODE_ROLLUP_3:
649 case EIA608_MODE_ROLLUP_4:
650 case EIA608_MODE_PAINTON:
651 return h->i_screen;
652 default:
653 /* It cannot happen, else it is a bug */
654 vlc_assert_unreachable();
655 return 0;
659 static void Eia608EraseScreen( eia608_t *h, bool b_displayed )
661 Eia608ClearScreen( h, b_displayed ? h->i_screen : (1-h->i_screen) );
664 static void Eia608Write( eia608_t *h, const uint8_t c )
666 const int i_row = h->cursor.i_row;
667 const int i_column = h->cursor.i_column;
668 eia608_screen *screen;
670 if( h->mode == EIA608_MODE_TEXT )
671 return;
673 screen = &h->screen[Eia608GetWritingScreenIndex( h )];
675 screen->characters[i_row][i_column] = c;
676 screen->colors[i_row][i_column] = h->color;
677 screen->fonts[i_row][i_column] = h->font;
678 screen->row_used[i_row] = true;
679 Eia608Cursor( h, 1 );
681 static void Eia608Erase( eia608_t *h )
683 const int i_row = h->cursor.i_row;
684 const int i_column = h->cursor.i_column - 1;
685 eia608_screen *screen;
687 if( h->mode == EIA608_MODE_TEXT )
688 return;
689 if( i_column < 0 )
690 return;
692 screen = &h->screen[Eia608GetWritingScreenIndex( h )];
694 /* FIXME do we need to reset row_used/colors/font ? */
695 screen->characters[i_row][i_column] = ' ';
696 Eia608Cursor( h, -1 );
698 static void Eia608EraseToEndOfRow( eia608_t *h )
700 if( h->mode == EIA608_MODE_TEXT )
701 return;
703 Eia608ClearScreenRowX( h, Eia608GetWritingScreenIndex( h ), h->cursor.i_row, h->cursor.i_column );
706 static void Eia608RollUp( eia608_t *h )
708 if( h->mode == EIA608_MODE_TEXT )
709 return;
711 const int i_screen = Eia608GetWritingScreenIndex( h );
712 eia608_screen *screen = &h->screen[i_screen];
714 int keep_lines;
716 /* Window size */
717 if( h->mode == EIA608_MODE_ROLLUP_2 )
718 keep_lines = 2;
719 else if( h->mode == EIA608_MODE_ROLLUP_3 )
720 keep_lines = 3;
721 else if( h->mode == EIA608_MODE_ROLLUP_4 )
722 keep_lines = 4;
723 else
724 return;
726 /* Reset the cursor */
727 h->cursor.i_column = 0;
729 /* Erase lines above our window */
730 for( int i = 0; i < h->cursor.i_row - keep_lines; i++ )
731 Eia608ClearScreenRow( h, i_screen, i );
733 /* Move up */
734 for( int i = 0; i < keep_lines-1; i++ )
736 const int i_row = h->cursor.i_row - keep_lines + i + 1;
737 if( i_row < 0 )
738 continue;
739 assert( i_row+1 < EIA608_SCREEN_ROWS );
740 memcpy( screen->characters[i_row], screen->characters[i_row+1], sizeof(*screen->characters) );
741 memcpy( screen->colors[i_row], screen->colors[i_row+1], sizeof(*screen->colors) );
742 memcpy( screen->fonts[i_row], screen->fonts[i_row+1], sizeof(*screen->fonts) );
743 screen->row_used[i_row] = screen->row_used[i_row+1];
745 /* Reset current row */
746 Eia608ClearScreenRow( h, i_screen, h->cursor.i_row );
748 static void Eia608ParseChannel( eia608_t *h, const uint8_t d[2] )
750 /* Check odd parity */
751 static const int p4[16] = {
752 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
754 if( p4[d[0] & 0xf] == p4[d[0] >> 4] ||
755 p4[d[1] & 0xf] == p4[ d[1] >> 4] )
757 h->i_channel = -1;
758 return;
761 /* */
762 const int d1 = d[0] & 0x7f;
763 if( d1 >= 0x10 && d1 <= 0x1f )
764 h->i_channel = 1 + ((d1 & 0x08) != 0);
765 else if( d1 < 0x10 )
766 h->i_channel = 3;
768 static eia608_status_t Eia608ParseTextAttribute( eia608_t *h, uint8_t d2 )
770 const int i_index = d2 - 0x20;
771 assert( d2 >= 0x20 && d2 <= 0x2f );
773 Debug(printf("[TA %d]", i_index));
774 h->color = pac2_attribs[i_index].i_color;
775 h->font = pac2_attribs[i_index].i_font;
776 Eia608Cursor( h, 1 );
778 return EIA608_STATUS_DEFAULT;
780 static eia608_status_t Eia608ParseSingle( eia608_t *h, const uint8_t dx )
782 assert( dx >= 0x20 );
783 Eia608Write( h, dx );
784 return EIA608_STATUS_CHANGED;
786 static eia608_status_t Eia608ParseDouble( eia608_t *h, uint8_t d2 )
788 assert( d2 >= 0x30 && d2 <= 0x3f );
789 Debug(printf("\033[0;33m%s\033[0m", d2 + 0x50));
790 Eia608Write( h, d2 + 0x50 ); /* We use charaters 0x80...0x8f */
791 return EIA608_STATUS_CHANGED;
793 static eia608_status_t Eia608ParseExtended( eia608_t *h, uint8_t d1, uint8_t d2 )
795 assert( d2 >= 0x20 && d2 <= 0x3f );
796 assert( d1 == 0x12 || d1 == 0x13 );
797 if( d1 == 0x12 )
798 d2 += 0x70; /* We use charaters 0x90-0xaf */
799 else
800 d2 += 0x90; /* We use charaters 0xb0-0xcf */
802 Debug(printf("[EXT %x->'%c']", d2, d2));
803 /* The extended characters replace the previous one with a more
804 * advanced one */
805 Eia608Cursor( h, -1 );
806 Eia608Write( h, d2 );
807 return EIA608_STATUS_CHANGED;
809 static eia608_status_t Eia608ParseCommand0x14( eia608_t *h, uint8_t d2 )
811 eia608_status_t i_status = EIA608_STATUS_DEFAULT;
812 eia608_mode_t proposed_mode;
814 switch( d2 )
816 case 0x20: /* Resume caption loading */
817 Debug(printf("[RCL]"));
818 h->mode = EIA608_MODE_POPUP;
819 break;
820 case 0x21: /* Backspace */
821 Debug(printf("[BS]"));
822 Eia608Erase( h );
823 i_status = EIA608_STATUS_CHANGED;
824 break;
825 case 0x22: /* Reserved */
826 case 0x23:
827 Debug(printf("[ALARM %d]", d2 - 0x22));
828 break;
829 case 0x24: /* Delete to end of row */
830 Debug(printf("[DER]"));
831 Eia608EraseToEndOfRow( h );
832 break;
833 case 0x25: /* Rollup 2 */
834 case 0x26: /* Rollup 3 */
835 case 0x27: /* Rollup 4 */
836 Debug(printf("[RU%d]", d2 - 0x23));
837 if( h->mode == EIA608_MODE_POPUP || h->mode == EIA608_MODE_PAINTON )
839 Eia608EraseScreen( h, true );
840 Eia608EraseScreen( h, false );
841 i_status = EIA608_STATUS_CHANGED | EIA608_STATUS_CAPTION_CLEARED;
844 if( d2 == 0x25 )
845 proposed_mode = EIA608_MODE_ROLLUP_2;
846 else if( d2 == 0x26 )
847 proposed_mode = EIA608_MODE_ROLLUP_3;
848 else
849 proposed_mode = EIA608_MODE_ROLLUP_4;
851 if ( proposed_mode != h->mode )
853 h->mode = proposed_mode;
854 h->cursor.i_column = 0;
855 h->cursor.i_row = h->i_row_rollup;
857 break;
858 case 0x28: /* Flash on */
859 Debug(printf("[FON]"));
860 /* TODO */
861 break;
862 case 0x29: /* Resume direct captionning */
863 Debug(printf("[RDC]"));
864 h->mode = EIA608_MODE_PAINTON;
865 break;
866 case 0x2a: /* Text restart */
867 Debug(printf("[TR]"));
868 /* TODO */
869 break;
871 case 0x2b: /* Resume text display */
872 Debug(printf("[RTD]"));
873 h->mode = EIA608_MODE_TEXT;
874 break;
876 case 0x2c: /* Erase displayed memory */
877 Debug(printf("[EDM]"));
878 Eia608EraseScreen( h, true );
879 i_status = EIA608_STATUS_CHANGED | EIA608_STATUS_CAPTION_CLEARED;
880 break;
881 case 0x2d: /* Carriage return */
882 Debug(printf("[CR]"));
883 Eia608RollUp(h);
884 i_status = EIA608_STATUS_CHANGED;
885 break;
886 case 0x2e: /* Erase non displayed memory */
887 Debug(printf("[ENM]"));
888 Eia608EraseScreen( h, false );
889 break;
890 case 0x2f: /* End of caption (flip screen if not paint on) */
891 Debug(printf("[EOC]"));
892 if( h->mode != EIA608_MODE_PAINTON )
893 h->i_screen = 1 - h->i_screen;
894 h->mode = EIA608_MODE_POPUP;
895 h->cursor.i_column = 0;
896 h->cursor.i_row = 0;
897 h->color = EIA608_COLOR_DEFAULT;
898 h->font = EIA608_FONT_REGULAR;
899 i_status = EIA608_STATUS_CHANGED | EIA608_STATUS_CAPTION_ENDED;
900 break;
902 return i_status;
904 static bool Eia608ParseCommand0x17( eia608_t *h, uint8_t d2 )
906 switch( d2 )
908 case 0x21: /* Tab offset 1 */
909 case 0x22: /* Tab offset 2 */
910 case 0x23: /* Tab offset 3 */
911 Debug(printf("[TO%d]", d2 - 0x20));
912 Eia608Cursor( h, d2 - 0x20 );
913 break;
915 return false;
917 static bool Eia608ParsePac( eia608_t *h, uint8_t d1, uint8_t d2 )
919 static const int pi_row[] = {
920 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
922 const int i_row_index = ( (d1<<1) & 0x0e) | ( (d2>>5) & 0x01 );
924 Debug(printf("[PAC,%d]", i_row_index));
925 assert( d2 >= 0x40 && d2 <= 0x7f );
927 if( pi_row[i_row_index] <= 0 )
928 return false;
930 /* Row */
931 if( h->mode != EIA608_MODE_TEXT )
932 h->cursor.i_row = pi_row[i_row_index] - 1;
933 h->i_row_rollup = pi_row[i_row_index] - 1;
934 /* Column */
935 if( d2 >= 0x60 )
936 d2 -= 0x60;
937 else if( d2 >= 0x40 )
938 d2 -= 0x40;
939 h->cursor.i_column = pac2_attribs[d2].i_column;
940 h->color = pac2_attribs[d2].i_color;
941 h->font = pac2_attribs[d2].i_font;
943 return false;
946 static eia608_status_t Eia608ParseData( eia608_t *h, uint8_t d1, uint8_t d2 )
948 eia608_status_t i_status = EIA608_STATUS_DEFAULT;
950 if( d1 >= 0x18 && d1 <= 0x1f )
951 d1 -= 8;
953 #define ON( d2min, d2max, cmd ) do { if( d2 >= d2min && d2 <= d2max ) i_status = cmd; } while(0)
954 switch( d1 )
956 case 0x11:
957 ON( 0x20, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
958 ON( 0x30, 0x3f, Eia608ParseDouble( h, d2 ) );
959 break;
960 case 0x12: case 0x13:
961 ON( 0x20, 0x3f, Eia608ParseExtended( h, d1, d2 ) );
962 break;
963 case 0x14: case 0x15:
964 ON( 0x20, 0x2f, Eia608ParseCommand0x14( h, d2 ) );
965 break;
966 case 0x17:
967 ON( 0x21, 0x23, Eia608ParseCommand0x17( h, d2 ) );
968 ON( 0x2e, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
969 break;
971 if( d1 == 0x10 )
972 ON( 0x40, 0x5f, Eia608ParsePac( h, d1, d2 ) );
973 else if( d1 >= 0x11 && d1 <= 0x17 )
974 ON( 0x40, 0x7f, Eia608ParsePac( h, d1, d2 ) );
975 #undef ON
976 if( d1 >= 0x20 )
978 Debug(printf("\033[0;33m%c", d1));
979 i_status = Eia608ParseSingle( h, d1 );
980 if( d2 >= 0x20 )
982 Debug(printf("%c", d2));
983 i_status |= Eia608ParseSingle( h, d2 );
985 Debug(printf("\033[0m"));
988 /* Ignore changes occuring to doublebuffer */
989 if( h->mode == EIA608_MODE_POPUP && i_status == EIA608_STATUS_CHANGED )
990 i_status = EIA608_STATUS_DEFAULT;
992 return i_status;
995 static void Eia608TextUtf8( char *psz_utf8, uint8_t c ) // Returns number of bytes used
997 #define E1(c,u) { c, { u, '\0' } }
998 #define E2(c,u1,u2) { c, { u1, u2, '\0' } }
999 #define E3(c,u1,u2,u3) { c, { u1, u2, u3, '\0' } }
1000 static const struct {
1001 uint8_t c;
1002 char utf8[3+1];
1003 } c2utf8[] = {
1004 // Regular line-21 character set, mostly ASCII except these exceptions
1005 E2( 0x2a, 0xc3,0xa1), // lowercase a, acute accent
1006 E2( 0x5c, 0xc3,0xa9), // lowercase e, acute accent
1007 E2( 0x5e, 0xc3,0xad), // lowercase i, acute accent
1008 E2( 0x5f, 0xc3,0xb3), // lowercase o, acute accent
1009 E2( 0x60, 0xc3,0xba), // lowercase u, acute accent
1010 E2( 0x7b, 0xc3,0xa7), // lowercase c with cedilla
1011 E2( 0x7c, 0xc3,0xb7), // division symbol
1012 E2( 0x7d, 0xc3,0x91), // uppercase N tilde
1013 E2( 0x7e, 0xc3,0xb1), // lowercase n tilde
1014 // THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
1015 // THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F
1016 E2( 0x80, 0xc2,0xae), // Registered symbol (R)
1017 E2( 0x81, 0xc2,0xb0), // degree sign
1018 E2( 0x82, 0xc2,0xbd), // 1/2 symbol
1019 E2( 0x83, 0xc2,0xbf), // Inverted (open) question mark
1020 E3( 0x84, 0xe2,0x84,0xa2), // Trademark symbol (TM)
1021 E2( 0x85, 0xc2,0xa2), // Cents symbol
1022 E2( 0x86, 0xc2,0xa3), // Pounds sterling
1023 E3( 0x87, 0xe2,0x99,0xaa), // Music note
1024 E2( 0x88, 0xc3,0xa0), // lowercase a, grave accent
1025 E2( 0x89, 0xc2,0xa0), // transparent space
1026 E2( 0x8a, 0xc3,0xa8), // lowercase e, grave accent
1027 E2( 0x8b, 0xc3,0xa2), // lowercase a, circumflex accent
1028 E2( 0x8c, 0xc3,0xaa), // lowercase e, circumflex accent
1029 E2( 0x8d, 0xc3,0xae), // lowercase i, circumflex accent
1030 E2( 0x8e, 0xc3,0xb4), // lowercase o, circumflex accent
1031 E2( 0x8f, 0xc3,0xbb), // lowercase u, circumflex accent
1032 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
1033 // THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F
1034 E2( 0x90, 0xc3,0x81), // capital letter A with acute
1035 E2( 0x91, 0xc3,0x89), // capital letter E with acute
1036 E2( 0x92, 0xc3,0x93), // capital letter O with acute
1037 E2( 0x93, 0xc3,0x9a), // capital letter U with acute
1038 E2( 0x94, 0xc3,0x9c), // capital letter U with diaresis
1039 E2( 0x95, 0xc3,0xbc), // lowercase letter U with diaeresis
1040 E1( 0x96, 0x27), // apostrophe
1041 E2( 0x97, 0xc2,0xa1), // inverted exclamation mark
1042 E1( 0x98, 0x2a), // asterisk
1043 E1( 0x99, 0x27), // apostrophe (yes, duped). See CCADI source code.
1044 E1( 0x9a, 0x2d), // hyphen-minus
1045 E2( 0x9b, 0xc2,0xa9), // copyright sign
1046 E3( 0x9c, 0xe2,0x84,0xa0), // Service mark
1047 E1( 0x9d, 0x2e), // Full stop (.)
1048 E3( 0x9e, 0xe2,0x80,0x9c), // Quotation mark
1049 E3( 0x9f, 0xe2,0x80,0x9d), // Quotation mark
1050 E2( 0xa0, 0xc3,0x80), // uppercase A, grave accent
1051 E2( 0xa1, 0xc3,0x82), // uppercase A, circumflex
1052 E2( 0xa2, 0xc3,0x87), // uppercase C with cedilla
1053 E2( 0xa3, 0xc3,0x88), // uppercase E, grave accent
1054 E2( 0xa4, 0xc3,0x8a), // uppercase E, circumflex
1055 E2( 0xa5, 0xc3,0x8b), // capital letter E with diaresis
1056 E2( 0xa6, 0xc3,0xab), // lowercase letter e with diaresis
1057 E2( 0xa7, 0xc3,0x8e), // uppercase I, circumflex
1058 E2( 0xa8, 0xc3,0x8f), // uppercase I, with diaresis
1059 E2( 0xa9, 0xc3,0xaf), // lowercase i, with diaresis
1060 E2( 0xaa, 0xc3,0x94), // uppercase O, circumflex
1061 E2( 0xab, 0xc3,0x99), // uppercase U, grave accent
1062 E2( 0xac, 0xc3,0xb9), // lowercase u, grave accent
1063 E2( 0xad, 0xc3,0x9b), // uppercase U, circumflex
1064 E2( 0xae, 0xc2,0xab), // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
1065 E2( 0xaf, 0xc2,0xbb), // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
1066 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
1067 // THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F
1068 E2( 0xb0, 0xc3,0x83), // Uppercase A, tilde
1069 E2( 0xb1, 0xc3,0xa3), // Lowercase a, tilde
1070 E2( 0xb2, 0xc3,0x8d), // Uppercase I, acute accent
1071 E2( 0xb3, 0xc3,0x8c), // Uppercase I, grave accent
1072 E2( 0xb4, 0xc3,0xac), // Lowercase i, grave accent
1073 E2( 0xb5, 0xc3,0x92), // Uppercase O, grave accent
1074 E2( 0xb6, 0xc3,0xb2), // Lowercase o, grave accent
1075 E2( 0xb7, 0xc3,0x95), // Uppercase O, tilde
1076 E2( 0xb8, 0xc3,0xb5), // Lowercase o, tilde
1077 E1( 0xb9, 0x7b), // Open curly brace
1078 E1( 0xba, 0x7d), // Closing curly brace
1079 E1( 0xbb, 0x5c), // Backslash
1080 E1( 0xbc, 0x5e), // Caret
1081 E1( 0xbd, 0x5f), // Underscore
1082 E2( 0xbe, 0xc2,0xa6), // Pipe (broken bar)
1083 E1( 0xbf, 0x7e), // Tilde (utf8 code unsure)
1084 E2( 0xc0, 0xc3,0x84), // Uppercase A, umlaut
1085 E2( 0xc1, 0xc3,0xa4), // Lowercase A, umlaut
1086 E2( 0xc2, 0xc3,0x96), // Uppercase O, umlaut
1087 E2( 0xc3, 0xc3,0xb6), // Lowercase o, umlaut
1088 E2( 0xc4, 0xc3,0x9f), // Esszett (sharp S)
1089 E2( 0xc5, 0xc2,0xa5), // Yen symbol
1090 E2( 0xc6, 0xc2,0xa4), // Currency symbol
1091 E1( 0xc7, 0x7c), // Vertical bar
1092 E2( 0xc8, 0xc3,0x85), // Uppercase A, ring
1093 E2( 0xc9, 0xc3,0xa5), // Lowercase A, ring
1094 E2( 0xca, 0xc3,0x98), // Uppercase O, slash
1095 E2( 0xcb, 0xc3,0xb8), // Lowercase o, slash
1096 E3( 0xcc, 0xe2,0x8c,0x9c), // Upper left corner
1097 E3( 0xcd, 0xe2,0x8c,0x9d), // Upper right corner
1098 E3( 0xce, 0xe2,0x8c,0x9e), // Lower left corner
1099 E3( 0xcf, 0xe2,0x8c,0x9f), // Lower right corner
1101 E1(0,0)
1103 #undef E3
1104 #undef E2
1105 #undef E1
1107 for( size_t i = 0; i < ARRAY_SIZE(c2utf8) ; i++ )
1108 if( c2utf8[i].c == c ) {
1109 strcpy( psz_utf8, c2utf8[i].utf8 );
1110 return;
1113 psz_utf8[0] = c < 0x80 ? c : '?'; /* Normal : Unsupported */
1114 psz_utf8[1] = '\0';
1117 static void Eia608Strlcat( char *d, const char *s, int i_max )
1119 if( i_max > 1 )
1120 strncat( d, s, i_max-1 - strnlen(d, i_max-1));
1121 if( i_max > 0 )
1122 d[i_max-1] = '\0';
1125 #define CAT(t) Eia608Strlcat( psz_text, t, i_text_max )
1127 static text_segment_t * Eia608TextLine( struct eia608_screen *screen, int i_row )
1129 const uint8_t *p_char = screen->characters[i_row];
1130 const eia608_color_t *p_color = screen->colors[i_row];
1131 const eia608_font_t *p_font = screen->fonts[i_row];
1132 int i_start;
1133 int i_end;
1134 int x;
1135 eia608_color_t prev_color = EIA608_COLOR_DEFAULT;
1136 eia608_font_t prev_font = EIA608_FONT_REGULAR;
1138 char utf8[4];
1139 const unsigned i_text_max = 4 * EIA608_SCREEN_COLUMNS + 1;
1140 char psz_text[i_text_max + 1];
1141 psz_text[0] = '\0';
1143 /* Search the start */
1144 i_start = 0;
1146 /* Convert leading spaces to non-breaking so that they don't get
1147 stripped by the RenderHtml routine as regular whitespace */
1148 while( i_start < EIA608_SCREEN_COLUMNS && p_char[i_start] == ' ' ) {
1149 Eia608TextUtf8( utf8, 0x89 );
1150 CAT( utf8 );
1151 i_start++;
1154 /* Search the end */
1155 i_end = EIA608_SCREEN_COLUMNS-1;
1156 while( i_end > i_start && p_char[i_end] == ' ' )
1157 i_end--;
1159 /* */
1160 if( i_start > i_end ) /* Nothing to render */
1161 return NULL;
1163 text_segment_t *p_segment, *p_segments_head = p_segment = text_segment_New( NULL );
1164 if(!p_segment)
1165 return NULL;
1167 p_segment->style = text_style_Create( STYLE_NO_DEFAULTS );
1168 if(!p_segment->style)
1170 text_segment_Delete(p_segment);
1171 return NULL;
1173 /* Ensure we get a monospaced font (required for accurate positioning */
1174 p_segment->style->i_style_flags |= STYLE_MONOSPACED;
1176 for( x = i_start; x <= i_end; x++ )
1178 eia608_color_t color = p_color[x];
1179 eia608_font_t font = p_font[x];
1181 if(font != prev_font || color != prev_color)
1183 EnsureUTF8(psz_text);
1184 p_segment->psz_text = strdup(psz_text);
1185 psz_text[0] = '\0';
1186 p_segment->p_next = text_segment_New( NULL );
1187 p_segment = p_segment->p_next;
1188 if(!p_segment)
1189 return p_segments_head;
1191 p_segment->style = text_style_Create( STYLE_NO_DEFAULTS );
1192 if(!p_segment->style)
1194 text_segment_Delete(p_segment);
1195 return p_segments_head;
1197 p_segment->style->i_style_flags |= STYLE_MONOSPACED;
1199 /* start segment with new style */
1200 if(font & EIA608_FONT_ITALICS)
1202 p_segment->style->i_style_flags |= STYLE_ITALIC;
1203 p_segment->style->i_features |= STYLE_HAS_FLAGS;
1205 if(font & EIA608_FONT_UNDERLINE)
1207 p_segment->style->i_style_flags |= STYLE_UNDERLINE;
1208 p_segment->style->i_features |= STYLE_HAS_FLAGS;
1211 if(color != EIA608_COLOR_DEFAULT)
1213 p_segment->style->i_font_color = rgi_eia608_colors[color];
1214 p_segment->style->i_features |= STYLE_HAS_FONT_COLOR;
1218 Eia608TextUtf8( utf8, p_char[x] );
1219 CAT( utf8 );
1221 /* */
1222 prev_font = font;
1223 prev_color = color;
1226 #undef CAT
1228 if( p_segment )
1230 assert(!p_segment->psz_text); // shouldn't happen
1231 EnsureUTF8(psz_text);
1232 p_segment->psz_text = strdup(psz_text);
1235 return p_segments_head;
1238 static void Eia608FillUpdaterRegions( subtext_updater_sys_t *p_updater, eia608_t *h )
1240 struct eia608_screen *screen = &h->screen[h->i_screen];
1241 substext_updater_region_t *p_region = &p_updater->region;
1242 text_segment_t **pp_last = &p_region->p_segments;
1243 bool b_newregion = false;
1245 for( int i = 0; i < EIA608_SCREEN_ROWS; i++ )
1247 if( !screen->row_used[i] )
1248 continue;
1250 text_segment_t *p_segments = Eia608TextLine( screen, i );
1251 if( p_segments )
1253 if( b_newregion )
1255 substext_updater_region_t *p_newregion;
1256 p_newregion = SubpictureUpdaterSysRegionNew();
1257 if( !p_newregion )
1259 text_segment_ChainDelete( p_segments );
1260 return;
1262 /* Copy defaults */
1263 p_newregion->align = p_region->align;
1264 p_newregion->inner_align = p_region->inner_align;
1265 p_newregion->flags = p_region->flags;
1266 SubpictureUpdaterSysRegionAdd( p_region, p_newregion );
1267 p_region = p_newregion;
1268 pp_last = &p_region->p_segments;
1269 b_newregion = false;
1272 if( p_region->p_segments == NULL ) /* First segment in the [new] region */
1274 p_region->origin.y = (float) i /* start line number */
1275 / (EIA608_SCREEN_ROWS * FONT_TO_LINE_HEIGHT_RATIO);
1276 p_region->flags |= UPDT_REGION_ORIGIN_Y_IS_RATIO;
1278 else /* Insert line break between region lines */
1280 *pp_last = text_segment_New( "\n" );
1281 if( *pp_last )
1282 pp_last = &((*pp_last)->p_next);
1285 *pp_last = p_segments;
1286 do { pp_last = &((*pp_last)->p_next); } while ( *pp_last != NULL );
1288 else
1290 b_newregion = !!p_region->p_segments;
1295 /* */
1296 static void Eia608Init( eia608_t *h )
1298 memset( h, 0, sizeof(*h) );
1300 /* */
1301 h->i_channel = -1;
1303 h->i_screen = 0;
1304 Eia608ClearScreen( h, 0 );
1305 Eia608ClearScreen( h, 1 );
1307 /* Cursor for writing text */
1308 h->cursor.i_column = 0;
1309 h->cursor.i_row = 0;
1311 h->last.d1 = 0x00;
1312 h->last.d2 = 0x00;
1313 h->mode = EIA608_MODE_POPUP;
1314 h->color = EIA608_COLOR_DEFAULT;
1315 h->font = EIA608_FONT_REGULAR;
1316 h->i_row_rollup = EIA608_SCREEN_ROWS-1;
1318 static eia608_status_t Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] )
1320 const uint8_t d1 = data[0] & 0x7f; /* Removed parity bit */
1321 const uint8_t d2 = data[1] & 0x7f;
1322 eia608_status_t i_screen_status = EIA608_STATUS_DEFAULT;
1324 if( d1 == 0 && d2 == 0 )
1325 return EIA608_STATUS_DEFAULT; /* Ignore padding (parity check are sometimes invalid on them) */
1327 Eia608ParseChannel( h, data );
1328 if( h->i_channel != i_channel_selected )
1329 return false;
1330 //fprintf( stderr, "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC %x %x\n", data[0], data[1] );
1332 if( d1 >= 0x10 )
1334 if( d1 >= 0x20 ||
1335 d1 != h->last.d1 || d2 != h->last.d2 ) /* Command codes can be repeated */
1336 i_screen_status = Eia608ParseData( h, d1,d2 );
1338 h->last.d1 = d1;
1339 h->last.d2 = d2;
1341 else if( ( d1 >= 0x01 && d1 <= 0x0E ) || d1 == 0x0F )
1343 /* XDS block / End of XDS block */
1345 return i_screen_status;