substext: pass margin and font to regions
[vlc.git] / modules / codec / cc.c
blob2ed58cc27ac8ee57530405e6aea3dc3a98471f61
1 /*****************************************************************************
2 * cc.c : CC 608/708 subtitles decoder
3 *****************************************************************************
4 * Copyright © 2007-2010 Laurent Aimar, 2011 VLC authors and VideoLAN
6 * Authors: Laurent Aimar < fenrir # via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
26 /* The EIA 608 decoder part has been initialy based on ccextractor (GPL)
27 * and rewritten */
29 /* TODO:
30 * Check parity
31 * 708 decoding
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
38 #include <assert.h>
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_codec.h>
43 #include <vlc_charset.h>
45 #include "substext.h"
47 /*****************************************************************************
48 * Module descriptor.
49 *****************************************************************************/
50 static int Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
53 #define OPAQUE_TEXT N_("Opacity")
54 #define OPAQUE_LONGTEXT N_("Setting to true " \
55 "makes the text to be boxed and maybe easier to read." )
57 vlc_module_begin ()
58 set_shortname( N_("CC 608/708"))
59 set_description( N_("Closed Captions decoder") )
60 set_capability( "spu decoder", 50 )
61 set_category( CAT_INPUT )
62 set_subcategory( SUBCAT_INPUT_SCODEC )
63 set_callbacks( Open, Close )
65 add_bool( "cc-opaque", true,
66 OPAQUE_TEXT, OPAQUE_LONGTEXT, false )
67 vlc_module_end ()
69 /*****************************************************************************
70 * Local prototypes
71 *****************************************************************************/
72 typedef enum
74 EIA608_MODE_POPUP = 0,
75 EIA608_MODE_ROLLUP_2 = 1,
76 EIA608_MODE_ROLLUP_3 = 2,
77 EIA608_MODE_ROLLUP_4 = 3,
78 EIA608_MODE_PAINTON = 4,
79 EIA608_MODE_TEXT = 5
80 } eia608_mode_t;
82 typedef enum
84 EIA608_COLOR_WHITE = 0,
85 EIA608_COLOR_GREEN = 1,
86 EIA608_COLOR_BLUE = 2,
87 EIA608_COLOR_CYAN = 3,
88 EIA608_COLOR_RED = 4,
89 EIA608_COLOR_YELLOW = 5,
90 EIA608_COLOR_MAGENTA = 6,
91 EIA608_COLOR_USERDEFINED = 7
92 } eia608_color_t;
94 typedef enum
96 EIA608_FONT_REGULAR = 0x00,
97 EIA608_FONT_ITALICS = 0x01,
98 EIA608_FONT_UNDERLINE = 0x02,
99 EIA608_FONT_UNDERLINE_ITALICS = EIA608_FONT_UNDERLINE | EIA608_FONT_ITALICS
100 } eia608_font_t;
102 #define EIA608_SCREEN_ROWS 15
103 #define EIA608_SCREEN_COLUMNS 32
105 struct eia608_screen // A CC buffer
107 uint8_t characters[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1];
108 eia608_color_t colors[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1];
109 eia608_font_t fonts[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1]; // Extra char at the end for a 0
110 int row_used[EIA608_SCREEN_ROWS]; // Any data in row?
112 typedef struct eia608_screen eia608_screen;
114 typedef enum
116 EIA608_STATUS_DEFAULT = 0x00,
117 EIA608_STATUS_CHANGED = 0x01, /* current screen has been altered */
118 EIA608_STATUS_CAPTION_ENDED = 0x02, /* screen flip */
119 EIA608_STATUS_CAPTION_CLEARED = 0x04, /* active screen erased */
120 EIA608_STATUS_DISPLAY = EIA608_STATUS_CAPTION_CLEARED | EIA608_STATUS_CAPTION_ENDED,
121 } eia608_status_t;
123 static const struct {
124 eia608_color_t i_color;
125 eia608_font_t i_font;
126 int i_column;
127 } pac2_attribs[]= {
128 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 0 },
129 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 0 },
130 { EIA608_COLOR_GREEN, EIA608_FONT_REGULAR, 0 },
131 { EIA608_COLOR_GREEN, EIA608_FONT_UNDERLINE, 0 },
132 { EIA608_COLOR_BLUE, EIA608_FONT_REGULAR, 0 },
133 { EIA608_COLOR_BLUE, EIA608_FONT_UNDERLINE, 0 },
134 { EIA608_COLOR_CYAN, EIA608_FONT_REGULAR, 0 },
135 { EIA608_COLOR_CYAN, EIA608_FONT_UNDERLINE, 0 },
136 { EIA608_COLOR_RED, EIA608_FONT_REGULAR, 0 },
137 { EIA608_COLOR_RED, EIA608_FONT_UNDERLINE, 0 },
138 { EIA608_COLOR_YELLOW, EIA608_FONT_REGULAR, 0 },
139 { EIA608_COLOR_YELLOW, EIA608_FONT_UNDERLINE, 0 },
140 { EIA608_COLOR_MAGENTA, EIA608_FONT_REGULAR, 0 },
141 { EIA608_COLOR_MAGENTA, EIA608_FONT_UNDERLINE, 0 },
142 { EIA608_COLOR_WHITE, EIA608_FONT_ITALICS, 0 },
143 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE_ITALICS, 0 },
145 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 0 },
146 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 0 },
147 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 4 },
148 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 4 },
149 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 8 },
150 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 8 },
151 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 12 },
152 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 12 },
153 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 16 },
154 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 16 },
155 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 20 },
156 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 20 },
157 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 24 },
158 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 24 },
159 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 28 },
160 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 28 } ,
163 #define EIA608_COLOR_DEFAULT EIA608_COLOR_WHITE
165 static const int rgi_eia608_colors[] = {
166 0xffffff, // white
167 0x00ff00, // green
168 0x0000ff, // blue
169 0x00ffff, // cyan
170 0xff0000, // red
171 0xffff00, // yellow
172 0xff00ff, // magenta
173 0xffffff, // user defined XXX we use white
176 typedef struct
178 /* Current channel (used to reject packet without channel information) */
179 int i_channel;
181 /* */
182 int i_screen; /* Displayed screen */
183 eia608_screen screen[2];
185 struct
187 int i_row;
188 int i_column;
189 } cursor;
191 /* */
192 eia608_mode_t mode;
193 eia608_color_t color;
194 eia608_font_t font;
195 int i_row_rollup;
197 /* Last command pair (used to reject duplicated command) */
198 struct
200 uint8_t d1;
201 uint8_t d2;
202 } last;
203 } eia608_t;
205 static void Eia608Init( eia608_t * );
206 static eia608_status_t Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] );
207 static void Eia608FillUpdaterRegions( subpicture_updater_sys_t *p_updater, eia608_t *h );
209 /* It will be enough up to 63 B frames, which is far too high for
210 * broadcast environment */
211 #define CC_MAX_REORDER_SIZE (64)
212 struct decoder_sys_t
214 int i_queue;
215 block_t *p_queue;
217 int i_field;
218 int i_channel;
220 int i_reorder_depth;
222 eia608_t eia608;
223 bool b_opaque;
226 static int Decode( decoder_t *, block_t * );
227 static void Flush( decoder_t * );
229 /*****************************************************************************
230 * Open: probe the decoder and return score
231 *****************************************************************************
232 * Tries to launch a decoder and return score so that the interface is able
233 * to chose.
234 *****************************************************************************/
235 static int Open( vlc_object_t *p_this )
237 decoder_t *p_dec = (decoder_t*)p_this;
238 decoder_sys_t *p_sys;
240 if( p_dec->fmt_in.i_codec != VLC_CODEC_CEA608 ||
241 p_dec->fmt_in.subs.cc.i_channel > 3 )
242 return VLC_EGENERIC;
244 /* 0 -> i_field = 0; i_channel = 1;
245 1 -> i_field = 0; i_channel = 2;
246 2 -> i_field = 1; i_channel = 1;
247 3 -> i_field = 1; i_channel = 2; */
249 const int i_field = p_dec->fmt_in.subs.cc.i_channel >> 1;
250 const int i_channel = 1 + (p_dec->fmt_in.subs.cc.i_channel & 1);
252 p_dec->pf_decode = Decode;
253 p_dec->pf_flush = Flush;
255 /* Allocate the memory needed to store the decoder's structure */
256 p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
257 if( p_sys == NULL )
258 return VLC_ENOMEM;
260 /* init of p_sys */
261 p_sys->i_field = i_field;
262 p_sys->i_channel = i_channel;
264 Eia608Init( &p_sys->eia608 );
265 p_sys->b_opaque = var_InheritBool( p_dec, "cc-opaque" );
266 p_sys->i_reorder_depth = p_dec->fmt_in.subs.cc.i_reorder_depth;
268 p_dec->fmt_out.i_codec = VLC_CODEC_TEXT;
270 return VLC_SUCCESS;
273 /*****************************************************************************
274 * Flush:
275 *****************************************************************************/
276 static void Flush( decoder_t *p_dec )
278 decoder_sys_t *p_sys = p_dec->p_sys;
280 Eia608Init( &p_sys->eia608 );
282 block_ChainRelease( p_sys->p_queue );
283 p_sys->p_queue = NULL;
284 p_sys->i_queue = 0;
287 /****************************************************************************
288 * Decode: the whole thing
289 ****************************************************************************
291 ****************************************************************************/
292 static void Push( decoder_t *, block_t * );
293 static block_t *Pop( decoder_t *, bool );
294 static void Convert( decoder_t *, mtime_t, const uint8_t *, size_t );
296 static bool DoDecode( decoder_t *p_dec, bool b_drain )
298 block_t *p_block = Pop( p_dec, b_drain );
299 if( !p_block )
300 return false;
302 Convert( p_dec, p_block->i_pts, p_block->p_buffer, p_block->i_buffer );
303 block_Release( p_block );
305 return true;
308 static int Decode( decoder_t *p_dec, block_t *p_block )
310 decoder_sys_t *p_sys = p_dec->p_sys;
312 if( p_block )
314 /* Reset decoder if needed */
315 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
317 /* Drain */
318 for( ; DoDecode( p_dec, true ) ; );
319 Eia608Init( &p_sys->eia608 );
321 if( (p_block->i_flags & BLOCK_FLAG_CORRUPTED) || p_block->i_buffer < 1 )
323 block_Release( p_block );
324 return VLCDEC_SUCCESS;
328 /* XXX Cc captions data are OUT OF ORDER (because we receive them in the bitstream
329 * order (ie ordered by video picture dts) instead of the display order.
330 * We will simulate a simple IPB buffer scheme
331 * and reorder with pts.
332 * XXX it won't work with H264 which use non out of order B picture or MMCO */
333 if( p_sys->i_reorder_depth == 0 )
335 /* Wait for a P and output all *previous* picture by pts order (for
336 * hierarchical B frames) */
337 if( (p_block->i_flags & BLOCK_FLAG_TYPE_B) == 0 )
338 for( ; DoDecode( p_dec, true ); );
341 Push( p_dec, p_block );
344 const bool b_no_reorder = (p_dec->fmt_in.subs.cc.i_reorder_depth < 0);
345 for( ; DoDecode( p_dec, (p_block == NULL) || b_no_reorder ); );
347 return VLCDEC_SUCCESS;
350 /*****************************************************************************
351 * CloseDecoder: clean up the decoder
352 *****************************************************************************/
353 static void Close( vlc_object_t *p_this )
355 decoder_t *p_dec = (decoder_t *)p_this;
356 decoder_sys_t *p_sys = p_dec->p_sys;
358 block_ChainRelease( p_sys->p_queue );
359 free( p_sys );
362 /*****************************************************************************
364 *****************************************************************************/
365 static void Push( decoder_t *p_dec, block_t *p_block )
367 decoder_sys_t *p_sys = p_dec->p_sys;
369 if( p_sys->i_queue >= CC_MAX_REORDER_SIZE )
371 block_Release( Pop( p_dec, true ) );
372 msg_Warn( p_dec, "Trashing a CC entry" );
375 block_t **pp_block;
376 /* find insertion point */
377 for( pp_block = &p_sys->p_queue; *pp_block ; pp_block = &((*pp_block)->p_next) )
379 if( p_block->i_pts == VLC_TS_INVALID || (*pp_block)->i_pts == VLC_TS_INVALID )
380 continue;
381 if( p_block->i_pts < (*pp_block)->i_pts )
383 if( p_sys->i_reorder_depth > 0 &&
384 p_sys->i_queue < p_sys->i_reorder_depth &&
385 pp_block == &p_sys->p_queue )
387 msg_Info( p_dec, "Increasing reorder depth to %d", ++p_sys->i_reorder_depth );
389 break;
392 /* Insert, keeping a pts and/or fifo ordered list */
393 p_block->p_next = *pp_block ? *pp_block : NULL;
394 *pp_block = p_block;
395 p_sys->i_queue++;
398 static block_t *Pop( decoder_t *p_dec, bool b_forced )
400 decoder_sys_t *p_sys = p_dec->p_sys;
401 block_t *p_block;
403 if( p_sys->i_queue == 0 )
404 return NULL;
406 if( !b_forced && p_sys->i_queue < CC_MAX_REORDER_SIZE )
408 if( p_sys->i_queue < p_sys->i_reorder_depth || p_sys->i_reorder_depth == 0 )
409 return NULL;
412 /* dequeue head */
413 p_block = p_sys->p_queue;
414 p_sys->p_queue = p_block->p_next;
415 p_block->p_next = NULL;
416 p_sys->i_queue--;
418 return p_block;
421 static subpicture_t *Subtitle( decoder_t *p_dec, eia608_t *h, mtime_t i_pts )
423 //decoder_sys_t *p_sys = p_dec->p_sys;
424 subpicture_t *p_spu = NULL;
426 /* We cannot display a subpicture with no date */
427 if( i_pts <= VLC_TS_INVALID )
428 return NULL;
430 /* Create the subpicture unit */
431 p_spu = decoder_NewSubpictureText( p_dec );
432 if( !p_spu )
433 return NULL;
435 p_spu->i_start = i_pts;
436 p_spu->i_stop = i_pts + 10000000; /* 10s max */
437 p_spu->b_ephemer = true;
438 p_spu->b_absolute = false;
440 subpicture_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
442 /* The "leavetext" alignment is a special mode where the subpicture
443 region itself gets aligned, but the text inside it does not */
444 p_spu_sys->region.align = SUBPICTURE_ALIGN_TOP;
445 p_spu_sys->region.inner_align = SUBPICTURE_ALIGN_LEAVETEXT;
446 p_spu_sys->region.flags = UPDT_REGION_IGNORE_BACKGROUND | UPDT_REGION_USES_GRID_COORDINATES;
447 /* Set style defaults (will be added to segments if none set) */
448 p_spu_sys->p_default_style->i_style_flags |= STYLE_MONOSPACED;
449 if( p_dec->p_sys->b_opaque )
451 p_spu_sys->p_default_style->i_background_alpha = STYLE_ALPHA_OPAQUE;
452 p_spu_sys->p_default_style->i_features |= STYLE_HAS_BACKGROUND_ALPHA;
453 p_spu_sys->p_default_style->i_style_flags |= STYLE_BACKGROUND;
455 p_spu_sys->margin_ratio = 0.10;
456 p_spu_sys->p_default_style->i_font_color = rgi_eia608_colors[EIA608_COLOR_DEFAULT];
457 /* FCC defined "safe area" for EIA-608 captions is 80% of the height of the display */
458 p_spu_sys->p_default_style->f_font_relsize = EIA608_VISIBLE * 100 / EIA608_SCREEN_ROWS /
459 FONT_TO_LINE_HEIGHT_RATIO;
460 p_spu_sys->p_default_style->i_features |= (STYLE_HAS_FONT_COLOR | STYLE_HAS_FLAGS);
462 Eia608FillUpdaterRegions( p_spu_sys, h );
464 return p_spu;
467 static void Convert( decoder_t *p_dec, mtime_t i_pts,
468 const uint8_t *p_buffer, size_t i_buffer )
470 decoder_sys_t *p_sys = p_dec->p_sys;
472 size_t i_ticks = 0;
473 while( i_buffer >= 3 )
475 /* Mask off the specific i_field bit, else some sequences can be lost. */
476 if ( (p_buffer[0] & 0x03) == p_sys->i_field &&
477 (p_buffer[0] & 0x04) /* Valid bit */ )
479 eia608_status_t i_status =
480 Eia608Parse( &p_sys->eia608, p_sys->i_channel, &p_buffer[1] );
482 /* a caption is ready or removed, process its screen */
484 * In case of rollup/painton with 1 packet/frame, we need to update on Changed status.
485 * Batch decoding might be incorrect if those in large number of commands (mp4, ...) then.
486 * see CEAv1.2zero.trp tests
488 if( i_status & (EIA608_STATUS_DISPLAY | EIA608_STATUS_CHANGED) )
490 subpicture_t *p_spu = Subtitle( p_dec, &p_sys->eia608, i_pts + i_ticks * CLOCK_FREQ / 30 );
491 if( p_spu )
492 decoder_QueueSub( p_dec, p_spu );
495 i_ticks++;
497 i_buffer -= 3;
498 p_buffer += 3;
503 /*****************************************************************************
505 *****************************************************************************/
506 static void Eia608Cursor( eia608_t *h, int dx )
508 h->cursor.i_column += dx;
509 if( h->cursor.i_column < 0 )
510 h->cursor.i_column = 0;
511 else if( h->cursor.i_column > EIA608_SCREEN_COLUMNS-1 )
512 h->cursor.i_column = EIA608_SCREEN_COLUMNS-1;
514 static void Eia608ClearScreenRowX( eia608_t *h, int i_screen, int i_row, int x )
516 eia608_screen *screen = &h->screen[i_screen];
518 if( x == 0 )
520 screen->row_used[i_row] = false;
522 else
524 screen->row_used[i_row] = false;
525 for( int i = 0; i < x; i++ )
527 if( screen->characters[i_row][i] != ' ' ||
528 screen->colors[i_row][i] != EIA608_COLOR_DEFAULT ||
529 screen->fonts[i_row][i] != EIA608_FONT_REGULAR )
531 screen->row_used[i_row] = true;
532 break;
537 for( ; x < EIA608_SCREEN_COLUMNS+1; x++ )
539 screen->characters[i_row][x] = x < EIA608_SCREEN_COLUMNS ? ' ' : '\0';
540 screen->colors[i_row][x] = EIA608_COLOR_DEFAULT;
541 screen->fonts[i_row][x] = EIA608_FONT_REGULAR;
545 static void Eia608ClearScreenRow( eia608_t *h, int i_screen, int i_row )
547 Eia608ClearScreenRowX( h, i_screen, i_row, 0 );
550 static void Eia608ClearScreen( eia608_t *h, int i_screen )
552 for( int i = 0; i < EIA608_SCREEN_ROWS; i++ )
553 Eia608ClearScreenRow( h, i_screen, i );
556 static int Eia608GetWritingScreenIndex( eia608_t *h )
558 switch( h->mode )
560 case EIA608_MODE_POPUP: // Non displayed screen
561 return 1 - h->i_screen;
563 case EIA608_MODE_ROLLUP_2: // Displayed screen
564 case EIA608_MODE_ROLLUP_3:
565 case EIA608_MODE_ROLLUP_4:
566 case EIA608_MODE_PAINTON:
567 return h->i_screen;
568 default:
569 /* It cannot happen, else it is a bug */
570 vlc_assert_unreachable();
571 return 0;
575 static void Eia608EraseScreen( eia608_t *h, bool b_displayed )
577 Eia608ClearScreen( h, b_displayed ? h->i_screen : (1-h->i_screen) );
580 static void Eia608Write( eia608_t *h, const uint8_t c )
582 const int i_row = h->cursor.i_row;
583 const int i_column = h->cursor.i_column;
584 eia608_screen *screen;
586 if( h->mode == EIA608_MODE_TEXT )
587 return;
589 screen = &h->screen[Eia608GetWritingScreenIndex( h )];
591 screen->characters[i_row][i_column] = c;
592 screen->colors[i_row][i_column] = h->color;
593 screen->fonts[i_row][i_column] = h->font;
594 screen->row_used[i_row] = true;
595 Eia608Cursor( h, 1 );
597 static void Eia608Erase( eia608_t *h )
599 const int i_row = h->cursor.i_row;
600 const int i_column = h->cursor.i_column - 1;
601 eia608_screen *screen;
603 if( h->mode == EIA608_MODE_TEXT )
604 return;
605 if( i_column < 0 )
606 return;
608 screen = &h->screen[Eia608GetWritingScreenIndex( h )];
610 /* FIXME do we need to reset row_used/colors/font ? */
611 screen->characters[i_row][i_column] = ' ';
612 Eia608Cursor( h, -1 );
614 static void Eia608EraseToEndOfRow( eia608_t *h )
616 if( h->mode == EIA608_MODE_TEXT )
617 return;
619 Eia608ClearScreenRowX( h, Eia608GetWritingScreenIndex( h ), h->cursor.i_row, h->cursor.i_column );
622 static void Eia608RollUp( eia608_t *h )
624 if( h->mode == EIA608_MODE_TEXT )
625 return;
627 const int i_screen = Eia608GetWritingScreenIndex( h );
628 eia608_screen *screen = &h->screen[i_screen];
630 int keep_lines;
632 /* Window size */
633 if( h->mode == EIA608_MODE_ROLLUP_2 )
634 keep_lines = 2;
635 else if( h->mode == EIA608_MODE_ROLLUP_3 )
636 keep_lines = 3;
637 else if( h->mode == EIA608_MODE_ROLLUP_4 )
638 keep_lines = 4;
639 else
640 return;
642 /* Reset the cursor */
643 h->cursor.i_column = 0;
645 /* Erase lines above our window */
646 for( int i = 0; i < h->cursor.i_row - keep_lines; i++ )
647 Eia608ClearScreenRow( h, i_screen, i );
649 /* Move up */
650 for( int i = 0; i < keep_lines-1; i++ )
652 const int i_row = h->cursor.i_row - keep_lines + i + 1;
653 if( i_row < 0 )
654 continue;
655 assert( i_row+1 < EIA608_SCREEN_ROWS );
656 memcpy( screen->characters[i_row], screen->characters[i_row+1], sizeof(*screen->characters) );
657 memcpy( screen->colors[i_row], screen->colors[i_row+1], sizeof(*screen->colors) );
658 memcpy( screen->fonts[i_row], screen->fonts[i_row+1], sizeof(*screen->fonts) );
659 screen->row_used[i_row] = screen->row_used[i_row+1];
661 /* Reset current row */
662 Eia608ClearScreenRow( h, i_screen, h->cursor.i_row );
664 static void Eia608ParseChannel( eia608_t *h, const uint8_t d[2] )
666 /* Check odd parity */
667 static const int p4[16] = {
668 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
670 if( p4[d[0] & 0xf] == p4[d[0] >> 4] ||
671 p4[d[1] & 0xf] == p4[ d[1] >> 4] )
673 h->i_channel = -1;
674 return;
677 /* */
678 const int d1 = d[0] & 0x7f;
679 if( d1 >= 0x10 && d1 <= 0x1f )
680 h->i_channel = 1 + ((d1 & 0x08) != 0);
681 else if( d1 < 0x10 )
682 h->i_channel = 3;
684 static eia608_status_t Eia608ParseTextAttribute( eia608_t *h, uint8_t d2 )
686 const int i_index = d2 - 0x20;
687 assert( d2 >= 0x20 && d2 <= 0x2f );
689 h->color = pac2_attribs[i_index].i_color;
690 h->font = pac2_attribs[i_index].i_font;
691 Eia608Cursor( h, 1 );
693 return EIA608_STATUS_DEFAULT;
695 static eia608_status_t Eia608ParseSingle( eia608_t *h, const uint8_t dx )
697 assert( dx >= 0x20 );
698 Eia608Write( h, dx );
699 return EIA608_STATUS_CHANGED;
701 static eia608_status_t Eia608ParseDouble( eia608_t *h, uint8_t d2 )
703 assert( d2 >= 0x30 && d2 <= 0x3f );
704 Eia608Write( h, d2 + 0x50 ); /* We use charaters 0x80...0x8f */
705 return EIA608_STATUS_CHANGED;
707 static eia608_status_t Eia608ParseExtended( eia608_t *h, uint8_t d1, uint8_t d2 )
709 assert( d2 >= 0x20 && d2 <= 0x3f );
710 assert( d1 == 0x12 || d1 == 0x13 );
711 if( d1 == 0x12 )
712 d2 += 0x70; /* We use charaters 0x90-0xaf */
713 else
714 d2 += 0x90; /* We use charaters 0xb0-0xcf */
716 /* The extended characters replace the previous one with a more
717 * advanced one */
718 Eia608Cursor( h, -1 );
719 Eia608Write( h, d2 );
720 return EIA608_STATUS_CHANGED;
722 static eia608_status_t Eia608ParseCommand0x14( eia608_t *h, uint8_t d2 )
724 eia608_status_t i_status = EIA608_STATUS_DEFAULT;
726 switch( d2 )
728 case 0x20: /* Resume caption loading */
729 h->mode = EIA608_MODE_POPUP;
730 break;
731 case 0x21: /* Backspace */
732 Eia608Erase( h );
733 i_status = EIA608_STATUS_CHANGED;
734 break;
735 case 0x22: /* Reserved */
736 case 0x23:
737 break;
738 case 0x24: /* Delete to end of row */
739 Eia608EraseToEndOfRow( h );
740 break;
741 case 0x25: /* Rollup 2 */
742 case 0x26: /* Rollup 3 */
743 case 0x27: /* Rollup 4 */
744 if( h->mode == EIA608_MODE_POPUP || h->mode == EIA608_MODE_PAINTON )
746 Eia608EraseScreen( h, true );
747 Eia608EraseScreen( h, false );
748 i_status = EIA608_STATUS_CHANGED | EIA608_STATUS_CAPTION_CLEARED;
751 if( d2 == 0x25 )
752 h->mode = EIA608_MODE_ROLLUP_2;
753 else if( d2 == 0x26 )
754 h->mode = EIA608_MODE_ROLLUP_3;
755 else
756 h->mode = EIA608_MODE_ROLLUP_4;
758 h->cursor.i_column = 0;
759 h->cursor.i_row = h->i_row_rollup;
760 break;
761 case 0x28: /* Flash on */
762 /* TODO */
763 break;
764 case 0x29: /* Resume direct captionning */
765 h->mode = EIA608_MODE_PAINTON;
766 break;
767 case 0x2a: /* Text restart */
768 /* TODO */
769 break;
771 case 0x2b: /* Resume text display */
772 h->mode = EIA608_MODE_TEXT;
773 break;
775 case 0x2c: /* Erase displayed memory */
776 Eia608EraseScreen( h, true );
777 i_status = EIA608_STATUS_CHANGED | EIA608_STATUS_CAPTION_CLEARED;
778 break;
779 case 0x2d: /* Carriage return */
780 Eia608RollUp(h);
781 i_status = EIA608_STATUS_CHANGED;
782 break;
783 case 0x2e: /* Erase non displayed memory */
784 Eia608EraseScreen( h, false );
785 break;
786 case 0x2f: /* End of caption (flip screen if not paint on) */
787 if( h->mode != EIA608_MODE_PAINTON )
788 h->i_screen = 1 - h->i_screen;
789 h->mode = EIA608_MODE_POPUP;
790 h->cursor.i_column = 0;
791 h->cursor.i_row = 0;
792 h->color = EIA608_COLOR_DEFAULT;
793 h->font = EIA608_FONT_REGULAR;
794 i_status = EIA608_STATUS_CHANGED | EIA608_STATUS_CAPTION_ENDED;
795 break;
797 return i_status;
799 static bool Eia608ParseCommand0x17( eia608_t *h, uint8_t d2 )
801 switch( d2 )
803 case 0x21: /* Tab offset 1 */
804 Eia608Cursor( h, 1 );
805 break;
806 case 0x22: /* Tab offset 2 */
807 Eia608Cursor( h, 2 );
808 break;
809 case 0x23: /* Tab offset 3 */
810 Eia608Cursor( h, 3 );
811 break;
813 return false;
815 static bool Eia608ParsePac( eia608_t *h, uint8_t d1, uint8_t d2 )
817 static const int pi_row[] = {
818 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
820 const int i_row_index = ( (d1<<1) & 0x0e) | ( (d2>>5) & 0x01 );
822 assert( d2 >= 0x40 && d2 <= 0x7f );
824 if( pi_row[i_row_index] <= 0 )
825 return false;
827 /* Row */
828 if( h->mode != EIA608_MODE_TEXT )
829 h->cursor.i_row = pi_row[i_row_index] - 1;
830 h->i_row_rollup = pi_row[i_row_index] - 1;
831 /* Column */
832 if( d2 >= 0x60 )
833 d2 -= 0x60;
834 else if( d2 >= 0x40 )
835 d2 -= 0x40;
836 h->cursor.i_column = pac2_attribs[d2].i_column;
837 h->color = pac2_attribs[d2].i_color;
838 h->font = pac2_attribs[d2].i_font;
840 return false;
843 static eia608_status_t Eia608ParseData( eia608_t *h, uint8_t d1, uint8_t d2 )
845 eia608_status_t i_status = EIA608_STATUS_DEFAULT;
847 if( d1 >= 0x18 && d1 <= 0x1f )
848 d1 -= 8;
850 #define ON( d2min, d2max, cmd ) do { if( d2 >= d2min && d2 <= d2max ) i_status = cmd; } while(0)
851 switch( d1 )
853 case 0x11:
854 ON( 0x20, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
855 ON( 0x30, 0x3f, Eia608ParseDouble( h, d2 ) );
856 break;
857 case 0x12: case 0x13:
858 ON( 0x20, 0x3f, Eia608ParseExtended( h, d1, d2 ) );
859 break;
860 case 0x14: case 0x15:
861 ON( 0x20, 0x2f, Eia608ParseCommand0x14( h, d2 ) );
862 break;
863 case 0x17:
864 ON( 0x21, 0x23, Eia608ParseCommand0x17( h, d2 ) );
865 ON( 0x2e, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
866 break;
868 if( d1 == 0x10 )
869 ON( 0x40, 0x5f, Eia608ParsePac( h, d1, d2 ) );
870 else if( d1 >= 0x11 && d1 <= 0x17 )
871 ON( 0x40, 0x7f, Eia608ParsePac( h, d1, d2 ) );
872 #undef ON
873 if( d1 >= 0x20 )
875 i_status = Eia608ParseSingle( h, d1 );
876 if( d2 >= 0x20 )
877 i_status |= Eia608ParseSingle( h, d2 );
880 /* Ignore changes occuring to doublebuffer */
881 if( h->mode == EIA608_MODE_POPUP && i_status == EIA608_STATUS_CHANGED )
882 i_status = EIA608_STATUS_DEFAULT;
884 return i_status;
887 static void Eia608TextUtf8( char *psz_utf8, uint8_t c ) // Returns number of bytes used
889 #define E1(c,u) { c, { u, '\0' } }
890 #define E2(c,u1,u2) { c, { u1, u2, '\0' } }
891 #define E3(c,u1,u2,u3) { c, { u1, u2, u3, '\0' } }
892 static const struct {
893 uint8_t c;
894 char utf8[3+1];
895 } c2utf8[] = {
896 // Regular line-21 character set, mostly ASCII except these exceptions
897 E2( 0x2a, 0xc3,0xa1), // lowercase a, acute accent
898 E2( 0x5c, 0xc3,0xa9), // lowercase e, acute accent
899 E2( 0x5e, 0xc3,0xad), // lowercase i, acute accent
900 E2( 0x5f, 0xc3,0xb3), // lowercase o, acute accent
901 E2( 0x60, 0xc3,0xba), // lowercase u, acute accent
902 E2( 0x7b, 0xc3,0xa7), // lowercase c with cedilla
903 E2( 0x7c, 0xc3,0xb7), // division symbol
904 E2( 0x7d, 0xc3,0x91), // uppercase N tilde
905 E2( 0x7e, 0xc3,0xb1), // lowercase n tilde
906 // THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
907 // THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F
908 E2( 0x80, 0xc2,0xae), // Registered symbol (R)
909 E2( 0x81, 0xc2,0xb0), // degree sign
910 E2( 0x82, 0xc2,0xbd), // 1/2 symbol
911 E2( 0x83, 0xc2,0xbf), // Inverted (open) question mark
912 E3( 0x84, 0xe2,0x84,0xa2), // Trademark symbol (TM)
913 E2( 0x85, 0xc2,0xa2), // Cents symbol
914 E2( 0x86, 0xc2,0xa3), // Pounds sterling
915 E3( 0x87, 0xe2,0x99,0xaa), // Music note
916 E2( 0x88, 0xc3,0xa0), // lowercase a, grave accent
917 E2( 0x89, 0xc2,0xa0), // transparent space
918 E2( 0x8a, 0xc3,0xa8), // lowercase e, grave accent
919 E2( 0x8b, 0xc3,0xa2), // lowercase a, circumflex accent
920 E2( 0x8c, 0xc3,0xaa), // lowercase e, circumflex accent
921 E2( 0x8d, 0xc3,0xae), // lowercase i, circumflex accent
922 E2( 0x8e, 0xc3,0xb4), // lowercase o, circumflex accent
923 E2( 0x8f, 0xc3,0xbb), // lowercase u, circumflex accent
924 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
925 // THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F
926 E2( 0x90, 0xc3,0x81), // capital letter A with acute
927 E2( 0x91, 0xc3,0x89), // capital letter E with acute
928 E2( 0x92, 0xc3,0x93), // capital letter O with acute
929 E2( 0x93, 0xc3,0x9a), // capital letter U with acute
930 E2( 0x94, 0xc3,0x9c), // capital letter U with diaresis
931 E2( 0x95, 0xc3,0xbc), // lowercase letter U with diaeresis
932 E1( 0x96, 0x27), // apostrophe
933 E2( 0x97, 0xc2,0xa1), // inverted exclamation mark
934 E1( 0x98, 0x2a), // asterisk
935 E1( 0x99, 0x27), // apostrophe (yes, duped). See CCADI source code.
936 E1( 0x9a, 0x2d), // hyphen-minus
937 E2( 0x9b, 0xc2,0xa9), // copyright sign
938 E3( 0x9c, 0xe2,0x84,0xa0), // Service mark
939 E1( 0x9d, 0x2e), // Full stop (.)
940 E3( 0x9e, 0xe2,0x80,0x9c), // Quotation mark
941 E3( 0x9f, 0xe2,0x80,0x9d), // Quotation mark
942 E2( 0xa0, 0xc3,0x80), // uppercase A, grave accent
943 E2( 0xa1, 0xc3,0x82), // uppercase A, circumflex
944 E2( 0xa2, 0xc3,0x87), // uppercase C with cedilla
945 E2( 0xa3, 0xc3,0x88), // uppercase E, grave accent
946 E2( 0xa4, 0xc3,0x8a), // uppercase E, circumflex
947 E2( 0xa5, 0xc3,0x8b), // capital letter E with diaresis
948 E2( 0xa6, 0xc3,0xab), // lowercase letter e with diaresis
949 E2( 0xa7, 0xc3,0x8e), // uppercase I, circumflex
950 E2( 0xa8, 0xc3,0x8f), // uppercase I, with diaresis
951 E2( 0xa9, 0xc3,0xaf), // lowercase i, with diaresis
952 E2( 0xaa, 0xc3,0x94), // uppercase O, circumflex
953 E2( 0xab, 0xc3,0x99), // uppercase U, grave accent
954 E2( 0xac, 0xc3,0xb9), // lowercase u, grave accent
955 E2( 0xad, 0xc3,0x9b), // uppercase U, circumflex
956 E2( 0xae, 0xc2,0xab), // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
957 E2( 0xaf, 0xc2,0xbb), // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
958 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
959 // THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F
960 E2( 0xb0, 0xc3,0x83), // Uppercase A, tilde
961 E2( 0xb1, 0xc3,0xa3), // Lowercase a, tilde
962 E2( 0xb2, 0xc3,0x8d), // Uppercase I, acute accent
963 E2( 0xb3, 0xc3,0x8c), // Uppercase I, grave accent
964 E2( 0xb4, 0xc3,0xac), // Lowercase i, grave accent
965 E2( 0xb5, 0xc3,0x92), // Uppercase O, grave accent
966 E2( 0xb6, 0xc3,0xb2), // Lowercase o, grave accent
967 E2( 0xb7, 0xc3,0x95), // Uppercase O, tilde
968 E2( 0xb8, 0xc3,0xb5), // Lowercase o, tilde
969 E1( 0xb9, 0x7b), // Open curly brace
970 E1( 0xba, 0x7d), // Closing curly brace
971 E1( 0xbb, 0x5c), // Backslash
972 E1( 0xbc, 0x5e), // Caret
973 E1( 0xbd, 0x5f), // Underscore
974 E2( 0xbe, 0xc2,0xa6), // Pipe (broken bar)
975 E1( 0xbf, 0x7e), // Tilde (utf8 code unsure)
976 E2( 0xc0, 0xc3,0x84), // Uppercase A, umlaut
977 E2( 0xc1, 0xc3,0xa4), // Lowercase A, umlaut
978 E2( 0xc2, 0xc3,0x96), // Uppercase O, umlaut
979 E2( 0xc3, 0xc3,0xb6), // Lowercase o, umlaut
980 E2( 0xc4, 0xc3,0x9f), // Esszett (sharp S)
981 E2( 0xc5, 0xc2,0xa5), // Yen symbol
982 E2( 0xc6, 0xc2,0xa4), // Currency symbol
983 E1( 0xc7, 0x7c), // Vertical bar
984 E2( 0xc8, 0xc3,0x85), // Uppercase A, ring
985 E2( 0xc9, 0xc3,0xa5), // Lowercase A, ring
986 E2( 0xca, 0xc3,0x98), // Uppercase O, slash
987 E2( 0xcb, 0xc3,0xb8), // Lowercase o, slash
988 E3( 0xcc, 0xe2,0x8c,0x9c), // Upper left corner
989 E3( 0xcd, 0xe2,0x8c,0x9d), // Upper right corner
990 E3( 0xce, 0xe2,0x8c,0x9e), // Lower left corner
991 E3( 0xcf, 0xe2,0x8c,0x9f), // Lower right corner
993 E1(0,0)
995 #undef E3
996 #undef E2
997 #undef E1
999 for( size_t i = 0; i < ARRAY_SIZE(c2utf8) ; i++ )
1000 if( c2utf8[i].c == c ) {
1001 strcpy( psz_utf8, c2utf8[i].utf8 );
1002 return;
1005 psz_utf8[0] = c < 0x80 ? c : '?'; /* Normal : Unsupported */
1006 psz_utf8[1] = '\0';
1009 static void Eia608Strlcat( char *d, const char *s, int i_max )
1011 if( i_max > 1 )
1012 strncat( d, s, i_max-1 - strnlen(d, i_max-1));
1013 if( i_max > 0 )
1014 d[i_max-1] = '\0';
1017 #define CAT(t) Eia608Strlcat( psz_text, t, i_text_max )
1019 static text_segment_t * Eia608TextLine( struct eia608_screen *screen, int i_row )
1021 const uint8_t *p_char = screen->characters[i_row];
1022 const eia608_color_t *p_color = screen->colors[i_row];
1023 const eia608_font_t *p_font = screen->fonts[i_row];
1024 int i_start;
1025 int i_end;
1026 int x;
1027 eia608_color_t prev_color = EIA608_COLOR_DEFAULT;
1028 eia608_font_t prev_font = EIA608_FONT_REGULAR;
1030 char utf8[4];
1031 const unsigned i_text_max = 4 * EIA608_SCREEN_COLUMNS + 1;
1032 char psz_text[i_text_max + 1];
1033 psz_text[0] = '\0';
1035 /* Search the start */
1036 i_start = 0;
1038 /* Convert leading spaces to non-breaking so that they don't get
1039 stripped by the RenderHtml routine as regular whitespace */
1040 while( i_start < EIA608_SCREEN_COLUMNS && p_char[i_start] == ' ' ) {
1041 Eia608TextUtf8( utf8, 0x89 );
1042 CAT( utf8 );
1043 i_start++;
1046 /* Search the end */
1047 i_end = EIA608_SCREEN_COLUMNS-1;
1048 while( i_end > i_start && p_char[i_end] == ' ' )
1049 i_end--;
1051 /* */
1052 if( i_start > i_end ) /* Nothing to render */
1053 return NULL;
1055 text_segment_t *p_segment, *p_segments_head = p_segment = text_segment_New( NULL );
1056 if(!p_segment)
1057 return NULL;
1059 p_segment->style = text_style_Create( STYLE_NO_DEFAULTS );
1060 if(!p_segment->style)
1062 text_segment_Delete(p_segment);
1063 return NULL;
1065 /* Ensure we get a monospaced font (required for accurate positioning */
1066 p_segment->style->i_style_flags |= STYLE_MONOSPACED;
1068 for( x = i_start; x <= i_end; x++ )
1070 eia608_color_t color = p_color[x];
1071 eia608_font_t font = p_font[x];
1073 if(font != prev_font || color != prev_color)
1075 EnsureUTF8(psz_text);
1076 p_segment->psz_text = strdup(psz_text);
1077 psz_text[0] = '\0';
1078 p_segment->p_next = text_segment_New( NULL );
1079 p_segment = p_segment->p_next;
1080 if(!p_segment)
1081 return p_segments_head;
1083 p_segment->style = text_style_Create( STYLE_NO_DEFAULTS );
1084 if(!p_segment->style)
1086 text_segment_Delete(p_segment);
1087 return p_segments_head;
1089 p_segment->style->i_style_flags |= STYLE_MONOSPACED;
1091 /* start segment with new style */
1092 if(font & EIA608_FONT_ITALICS)
1094 p_segment->style->i_style_flags |= STYLE_ITALIC;
1095 p_segment->style->i_features |= STYLE_HAS_FLAGS;
1097 if(font & EIA608_FONT_UNDERLINE)
1099 p_segment->style->i_style_flags |= STYLE_UNDERLINE;
1100 p_segment->style->i_features |= STYLE_HAS_FLAGS;
1103 if(color != EIA608_COLOR_DEFAULT)
1105 p_segment->style->i_font_color = rgi_eia608_colors[color];
1106 p_segment->style->i_features |= STYLE_HAS_FONT_COLOR;
1110 Eia608TextUtf8( utf8, p_char[x] );
1111 CAT( utf8 );
1113 /* */
1114 prev_font = font;
1115 prev_color = color;
1118 #undef CAT
1120 if( p_segment )
1122 assert(!p_segment->psz_text); // shouldn't happen
1123 EnsureUTF8(psz_text);
1124 p_segment->psz_text = strdup(psz_text);
1127 return p_segments_head;
1130 static void Eia608FillUpdaterRegions( subpicture_updater_sys_t *p_updater, eia608_t *h )
1132 struct eia608_screen *screen = &h->screen[h->i_screen];
1133 subpicture_updater_sys_region_t *p_region = &p_updater->region;
1134 text_segment_t **pp_last = &p_region->p_segments;
1135 bool b_newregion = false;
1137 for( int i = 0; i < EIA608_SCREEN_ROWS; i++ )
1139 if( !screen->row_used[i] )
1140 continue;
1142 text_segment_t *p_segments = Eia608TextLine( screen, i );
1143 if( p_segments )
1145 if( b_newregion )
1147 subpicture_updater_sys_region_t *p_newregion;
1148 p_newregion = SubpictureUpdaterSysRegionNew();
1149 if( !p_newregion )
1151 text_segment_ChainDelete( p_segments );
1152 return;
1154 SubpictureUpdaterSysRegionAdd( p_region, p_newregion );
1155 p_region = p_newregion;
1156 pp_last = &p_region->p_segments;
1157 b_newregion = false;
1160 if( p_region->p_segments == NULL ) /* First segment in the [new] region */
1162 p_region->origin.y = i; /* set start line number */
1164 else /* Insert line break between region lines */
1166 *pp_last = text_segment_New( "\n" );
1167 if( *pp_last )
1168 pp_last = &((*pp_last)->p_next);
1171 *pp_last = p_segments;
1172 do { pp_last = &((*pp_last)->p_next); } while ( *pp_last != NULL );
1174 else
1176 b_newregion = !!p_region->p_segments;
1181 /* */
1182 static void Eia608Init( eia608_t *h )
1184 memset( h, 0, sizeof(*h) );
1186 /* */
1187 h->i_channel = -1;
1189 h->i_screen = 0;
1190 Eia608ClearScreen( h, 0 );
1191 Eia608ClearScreen( h, 1 );
1193 /* Cursor for writing text */
1194 h->cursor.i_column = 0;
1195 h->cursor.i_row = 0;
1197 h->last.d1 = 0x00;
1198 h->last.d2 = 0x00;
1199 h->mode = EIA608_MODE_POPUP;
1200 h->color = EIA608_COLOR_DEFAULT;
1201 h->font = EIA608_FONT_REGULAR;
1202 h->i_row_rollup = EIA608_SCREEN_ROWS-1;
1204 static eia608_status_t Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] )
1206 const uint8_t d1 = data[0] & 0x7f; /* Removed parity bit */
1207 const uint8_t d2 = data[1] & 0x7f;
1208 eia608_status_t i_screen_status = EIA608_STATUS_DEFAULT;
1210 if( d1 == 0 && d2 == 0 )
1211 return EIA608_STATUS_DEFAULT; /* Ignore padding (parity check are sometimes invalid on them) */
1213 Eia608ParseChannel( h, data );
1214 if( h->i_channel != i_channel_selected )
1215 return false;
1216 //fprintf( stderr, "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC %x %x\n", data[0], data[1] );
1218 if( d1 >= 0x10 )
1220 if( d1 >= 0x20 ||
1221 d1 != h->last.d1 || d2 != h->last.d2 ) /* Command codes can be repeated */
1222 i_screen_status = Eia608ParseData( h, d1,d2 );
1224 h->last.d1 = d1;
1225 h->last.d2 = d2;
1227 else if( ( d1 >= 0x01 && d1 <= 0x0E ) || d1 == 0x0F )
1229 /* XDS block / End of XDS block */
1231 return i_screen_status;