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 /*****************************************************************************
25 *****************************************************************************/
26 /* The EIA 608 decoder part has been initialy based on ccextractor (GPL)
30 * On discontinuity reset the decoder state
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
43 #include <vlc_codec.h>
44 #include <vlc_charset.h>
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
55 set_shortname( N_("CC 608/708"))
56 set_description( N_("Closed Captions decoder") )
57 set_capability( "decoder", 50 )
58 set_callbacks( Open, Close )
61 /*****************************************************************************
63 *****************************************************************************/
66 EIA608_MODE_POPUP = 0,
67 EIA608_MODE_ROLLUP_2 = 1,
68 EIA608_MODE_ROLLUP_3 = 2,
69 EIA608_MODE_ROLLUP_4 = 3,
70 EIA608_MODE_PAINTON = 4,
76 EIA608_COLOR_WHITE = 0,
77 EIA608_COLOR_GREEN = 1,
78 EIA608_COLOR_BLUE = 2,
79 EIA608_COLOR_CYAN = 3,
81 EIA608_COLOR_YELLOW = 5,
82 EIA608_COLOR_MAGENTA = 6,
83 EIA608_COLOR_USERDEFINED = 7
88 EIA608_FONT_REGULAR = 0x00,
89 EIA608_FONT_ITALICS = 0x01,
90 EIA608_FONT_UNDERLINE = 0x02,
91 EIA608_FONT_UNDERLINE_ITALICS = EIA608_FONT_UNDERLINE | EIA608_FONT_ITALICS
94 #define EIA608_SCREEN_ROWS 15
95 #define EIA608_SCREEN_COLUMNS 32
97 struct eia608_screen // A CC buffer
99 uint8_t characters[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1];
100 eia608_color_t colors[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1];
101 eia608_font_t fonts[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1]; // Extra char at the end for a 0
102 int row_used[EIA608_SCREEN_ROWS]; // Any data in row?
104 typedef struct eia608_screen eia608_screen;
108 /* Current channel (used to reject packet without channel information) */
112 int i_screen; /* Displayed screen */
113 eia608_screen screen[2];
123 eia608_color_t color;
127 /* Last command pair (used to reject duplicated command) */
135 static void Eia608Init( eia608_t * );
136 static bool Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] );
137 static char *Eia608Text( eia608_t *h, bool b_html );
139 /* It will be enough up to 63 B frames, which is far too high for
140 * broadcast environment */
141 #define CC_MAX_REORDER_SIZE (64)
145 block_t *pp_block[CC_MAX_REORDER_SIZE];
153 static subpicture_t *Decode( decoder_t *, block_t ** );
155 /*****************************************************************************
156 * Open: probe the decoder and return score
157 *****************************************************************************
158 * Tries to launch a decoder and return score so that the interface is able
160 *****************************************************************************/
161 static int Open( vlc_object_t *p_this )
163 decoder_t *p_dec = (decoder_t*)p_this;
164 decoder_sys_t *p_sys;
168 switch( p_dec->fmt_in.i_codec )
170 case VLC_FOURCC('c','c','1',' '):
171 i_field = 0; i_channel = 1;
173 case VLC_FOURCC('c','c','2',' '):
174 i_field = 0; i_channel = 2;
176 case VLC_FOURCC('c','c','3',' '):
177 i_field = 1; i_channel = 1;
179 case VLC_FOURCC('c','c','4',' '):
180 i_field = 1; i_channel = 2;
187 p_dec->pf_decode_sub = Decode;
189 /* Allocate the memory needed to store the decoder's structure */
190 p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
195 p_sys->i_field = i_field;
196 p_sys->i_channel = i_channel;
198 Eia608Init( &p_sys->eia608 );
200 p_dec->fmt_out.i_cat = SPU_ES;
201 p_dec->fmt_out.i_codec = VLC_CODEC_TEXT;
206 /****************************************************************************
207 * Decode: the whole thing
208 ****************************************************************************
210 ****************************************************************************/
211 static void Push( decoder_t *, block_t * );
212 static block_t *Pop( decoder_t * );
213 static subpicture_t *Convert( decoder_t *, block_t * );
215 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
217 if( pp_block && *pp_block )
219 Push( p_dec, *pp_block );
225 block_t *p_block = Pop( p_dec );
229 subpicture_t *p_spu = Convert( p_dec, p_block );
236 /*****************************************************************************
237 * CloseDecoder: clean up the decoder
238 *****************************************************************************/
239 static void Close( vlc_object_t *p_this )
241 decoder_t *p_dec = (decoder_t *)p_this;
242 decoder_sys_t *p_sys = p_dec->p_sys;
244 for( int i = 0; i < p_sys->i_block; i++ )
245 block_Release( p_sys->pp_block[i] );
249 /*****************************************************************************
251 *****************************************************************************/
252 static void Push( decoder_t *p_dec, block_t *p_block )
254 decoder_sys_t *p_sys = p_dec->p_sys;
256 if( p_sys->i_block >= CC_MAX_REORDER_SIZE )
258 msg_Warn( p_dec, "Trashing a CC entry" );
259 memmove( &p_sys->pp_block[0], &p_sys->pp_block[1], sizeof(*p_sys->pp_block) * (CC_MAX_REORDER_SIZE-1) );
262 p_sys->pp_block[p_sys->i_block++] = p_block;
264 static block_t *Pop( decoder_t *p_dec )
266 decoder_sys_t *p_sys = p_dec->p_sys;
269 /* XXX Cc captions data are OUT OF ORDER (because we receive them in the bitstream
270 * order (ie ordered by video picture dts) instead of the display order.
271 * We will simulate a simple IPB buffer scheme
272 * and reorder with pts.
273 * XXX it won't work with H264 which use non out of order B picture or MMCO
276 /* Wait for a P and output all *previous* picture by pts order (for
277 * hierarchical B frames) */
278 if( p_sys->i_block <= 1 ||
279 ( p_sys->pp_block[p_sys->i_block-1]->i_flags & BLOCK_FLAG_TYPE_B ) )
282 p_block = p_sys->pp_block[i_index = 0];
283 if( p_block->i_pts > VLC_TS_INVALID )
285 for( int i = 1; i < p_sys->i_block-1; i++ )
287 if( p_sys->pp_block[i]->i_pts > VLC_TS_INVALID && p_block->i_pts > VLC_TS_INVALID &&
288 p_sys->pp_block[i]->i_pts < p_block->i_pts )
289 p_block = p_sys->pp_block[i_index = i];
292 assert( i_index+1 < p_sys->i_block );
293 memmove( &p_sys->pp_block[i_index], &p_sys->pp_block[i_index+1], sizeof(*p_sys->pp_block) * ( p_sys->i_block - i_index - 1 ) );
299 static subpicture_t *Subtitle( decoder_t *p_dec, char *psz_subtitle, char *psz_html, mtime_t i_pts )
301 //decoder_sys_t *p_sys = p_dec->p_sys;
302 subpicture_t *p_spu = NULL;
304 /* We cannot display a subpicture with no date */
305 if( i_pts <= VLC_TS_INVALID )
307 msg_Warn( p_dec, "subtitle without a date" );
311 EnsureUTF8( psz_subtitle );
313 EnsureUTF8( psz_html );
315 /* Create the subpicture unit */
316 p_spu = decoder_NewSubpictureText( p_dec );
319 free( psz_subtitle );
323 p_spu->i_start = i_pts;
324 p_spu->i_stop = i_pts + 10000000; /* 10s max */
325 p_spu->b_ephemer = true;
326 p_spu->b_absolute = false;
328 subpicture_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
330 /* The "leavetext" alignment is a special mode where the subpicture
331 region itself gets aligned, but the text inside it does not */
332 p_spu_sys->align = SUBPICTURE_ALIGN_LEAVETEXT;
333 p_spu_sys->text = psz_subtitle;
334 p_spu_sys->html = psz_html;
335 p_spu_sys->i_font_height_percent = 5;
336 p_spu_sys->renderbg = true;
341 static subpicture_t *Convert( decoder_t *p_dec, block_t *p_block )
345 decoder_sys_t *p_sys = p_dec->p_sys;
346 const int64_t i_pts = p_block->i_pts;
347 bool b_changed = false;
349 /* TODO do the real decoding here */
350 while( p_block->i_buffer >= 3 )
352 if( p_block->p_buffer[0] == p_sys->i_field )
353 b_changed |= Eia608Parse( &p_sys->eia608, p_sys->i_channel, &p_block->p_buffer[1] );
355 p_block->i_buffer -= 3;
356 p_block->p_buffer += 3;
359 block_Release( p_block );
363 char *psz_subtitle = Eia608Text( &p_sys->eia608, false );
364 char *psz_html = Eia608Text( &p_sys->eia608, true );
365 return Subtitle( p_dec, psz_subtitle, psz_html, i_pts );
371 /*****************************************************************************
373 *****************************************************************************/
374 static const struct {
375 eia608_color_t i_color;
376 eia608_font_t i_font;
379 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 0 },
380 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 0 },
381 { EIA608_COLOR_GREEN, EIA608_FONT_REGULAR, 0 },
382 { EIA608_COLOR_GREEN, EIA608_FONT_UNDERLINE, 0 },
383 { EIA608_COLOR_BLUE, EIA608_FONT_REGULAR, 0 },
384 { EIA608_COLOR_BLUE, EIA608_FONT_UNDERLINE, 0 },
385 { EIA608_COLOR_CYAN, EIA608_FONT_REGULAR, 0 },
386 { EIA608_COLOR_CYAN, EIA608_FONT_UNDERLINE, 0 },
387 { EIA608_COLOR_RED, EIA608_FONT_REGULAR, 0 },
388 { EIA608_COLOR_RED, EIA608_FONT_UNDERLINE, 0 },
389 { EIA608_COLOR_YELLOW, EIA608_FONT_REGULAR, 0 },
390 { EIA608_COLOR_YELLOW, EIA608_FONT_UNDERLINE, 0 },
391 { EIA608_COLOR_MAGENTA, EIA608_FONT_REGULAR, 0 },
392 { EIA608_COLOR_MAGENTA, EIA608_FONT_UNDERLINE, 0 },
393 { EIA608_COLOR_WHITE, EIA608_FONT_ITALICS, 0 },
394 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE_ITALICS, 0 },
396 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 0 },
397 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 0 },
398 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 4 },
399 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 4 },
400 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 8 },
401 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 8 },
402 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 12 },
403 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 12 },
404 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 16 },
405 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 16 },
406 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 20 },
407 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 20 },
408 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 24 },
409 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 24 },
410 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 28 },
411 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 28 } ,
414 #define EIA608_COLOR_DEFAULT EIA608_COLOR_WHITE
416 static void Eia608Cursor( eia608_t *h, int dx )
418 h->cursor.i_column += dx;
419 if( h->cursor.i_column < 0 )
420 h->cursor.i_column = 0;
421 else if( h->cursor.i_column > EIA608_SCREEN_COLUMNS-1 )
422 h->cursor.i_column = EIA608_SCREEN_COLUMNS-1;
424 static void Eia608ClearScreenRowX( eia608_t *h, int i_screen, int i_row, int x )
426 eia608_screen *screen = &h->screen[i_screen];
430 screen->row_used[i_row] = false;
434 screen->row_used[i_row] = false;
435 for( int i = 0; i < x; i++ )
437 if( screen->characters[i_row][i] != ' ' ||
438 screen->colors[i_row][i] != EIA608_COLOR_DEFAULT ||
439 screen->fonts[i_row][i] != EIA608_FONT_REGULAR )
441 screen->row_used[i_row] = true;
447 for( ; x < EIA608_SCREEN_COLUMNS+1; x++ )
449 screen->characters[i_row][x] = x < EIA608_SCREEN_COLUMNS ? ' ' : '\0';
450 screen->colors[i_row][x] = EIA608_COLOR_DEFAULT;
451 screen->fonts[i_row][x] = EIA608_FONT_REGULAR;
455 static void Eia608ClearScreenRow( eia608_t *h, int i_screen, int i_row )
457 Eia608ClearScreenRowX( h, i_screen, i_row, 0 );
460 static void Eia608ClearScreen( eia608_t *h, int i_screen )
462 for( int i = 0; i < EIA608_SCREEN_ROWS; i++ )
463 Eia608ClearScreenRow( h, i_screen, i );
466 static int Eia608GetWritingScreenIndex( eia608_t *h )
470 case EIA608_MODE_POPUP: // Non displayed screen
471 return 1 - h->i_screen;
473 case EIA608_MODE_ROLLUP_2: // Displayed screen
474 case EIA608_MODE_ROLLUP_3:
475 case EIA608_MODE_ROLLUP_4:
476 case EIA608_MODE_PAINTON:
479 /* It cannot happen, else it is a bug */
485 static void Eia608EraseScreen( eia608_t *h, bool b_displayed )
487 Eia608ClearScreen( h, b_displayed ? h->i_screen : (1-h->i_screen) );
490 static void Eia608Write( eia608_t *h, const uint8_t c )
492 const int i_row = h->cursor.i_row;
493 const int i_column = h->cursor.i_column;
494 eia608_screen *screen;
496 if( h->mode == EIA608_MODE_TEXT )
499 screen = &h->screen[Eia608GetWritingScreenIndex( h )];
501 screen->characters[i_row][i_column] = c;
502 screen->colors[i_row][i_column] = h->color;
503 screen->fonts[i_row][i_column] = h->font;
504 screen->row_used[i_row] = true;
505 Eia608Cursor( h, 1 );
507 static void Eia608Erase( eia608_t *h )
509 const int i_row = h->cursor.i_row;
510 const int i_column = h->cursor.i_column - 1;
511 eia608_screen *screen;
513 if( h->mode == EIA608_MODE_TEXT )
518 screen = &h->screen[Eia608GetWritingScreenIndex( h )];
520 /* FIXME do we need to reset row_used/colors/font ? */
521 screen->characters[i_row][i_column] = ' ';
522 Eia608Cursor( h, -1 );
524 static void Eia608EraseToEndOfRow( eia608_t *h )
526 if( h->mode == EIA608_MODE_TEXT )
529 Eia608ClearScreenRowX( h, Eia608GetWritingScreenIndex( h ), h->cursor.i_row, h->cursor.i_column );
532 static void Eia608RollUp( eia608_t *h )
534 if( h->mode == EIA608_MODE_TEXT )
537 const int i_screen = Eia608GetWritingScreenIndex( h );
538 eia608_screen *screen = &h->screen[i_screen];
543 if( h->mode == EIA608_MODE_ROLLUP_2 )
545 else if( h->mode == EIA608_MODE_ROLLUP_3 )
547 else if( h->mode == EIA608_MODE_ROLLUP_4 )
552 /* Reset the cursor */
553 h->cursor.i_column = 0;
555 /* Erase lines above our window */
556 for( int i = 0; i < h->cursor.i_row - keep_lines; i++ )
557 Eia608ClearScreenRow( h, i_screen, i );
560 for( int i = 0; i < keep_lines-1; i++ )
562 const int i_row = h->cursor.i_row - keep_lines + i + 1;
565 assert( i_row+1 < EIA608_SCREEN_ROWS );
566 memcpy( screen->characters[i_row], screen->characters[i_row+1], sizeof(*screen->characters) );
567 memcpy( screen->colors[i_row], screen->colors[i_row+1], sizeof(*screen->colors) );
568 memcpy( screen->fonts[i_row], screen->fonts[i_row+1], sizeof(*screen->fonts) );
569 screen->row_used[i_row] = screen->row_used[i_row+1];
571 /* Reset current row */
572 Eia608ClearScreenRow( h, i_screen, h->cursor.i_row );
574 static void Eia608ParseChannel( eia608_t *h, const uint8_t d[2] )
576 /* Check odd parity */
577 static const int p4[16] = {
578 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
580 if( p4[d[0] & 0xf] == p4[d[0] >> 4] ||
581 p4[d[1] & 0xf] == p4[ d[1] >> 4] )
588 const int d1 = d[0] & 0x7f;
589 if( d1 >= 0x10 && d1 <= 0x1f )
590 h->i_channel = 1 + ((d1 & 0x08) != 0);
594 static bool Eia608ParseTextAttribute( eia608_t *h, uint8_t d2 )
596 const int i_index = d2 - 0x20;
597 assert( d2 >= 0x20 && d2 <= 0x2f );
599 h->color = pac2_attribs[i_index].i_color;
600 h->font = pac2_attribs[i_index].i_font;
601 Eia608Cursor( h, 1 );
605 static bool Eia608ParseSingle( eia608_t *h, const uint8_t dx )
607 assert( dx >= 0x20 );
608 Eia608Write( h, dx );
611 static bool Eia608ParseDouble( eia608_t *h, uint8_t d2 )
613 assert( d2 >= 0x30 && d2 <= 0x3f );
614 Eia608Write( h, d2 + 0x50 ); /* We use charaters 0x80...0x8f */
617 static bool Eia608ParseExtended( eia608_t *h, uint8_t d1, uint8_t d2 )
619 assert( d2 >= 0x20 && d2 <= 0x3f );
620 assert( d1 == 0x12 || d1 == 0x13 );
622 d2 += 0x70; /* We use charaters 0x90-0xaf */
624 d2 += 0x90; /* We use charaters 0xb0-0xcf */
626 /* The extended characters replace the previous one with a more
628 Eia608Cursor( h, -1 );
629 Eia608Write( h, d2 );
632 static bool Eia608ParseCommand0x14( eia608_t *h, uint8_t d2 )
634 bool b_changed = false;
638 case 0x20: /* Resume caption loading */
639 h->mode = EIA608_MODE_POPUP;
641 case 0x21: /* Backspace */
645 case 0x22: /* Reserved */
648 case 0x24: /* Delete to end of row */
649 Eia608EraseToEndOfRow( h );
651 case 0x25: /* Rollup 2 */
652 case 0x26: /* Rollup 3 */
653 case 0x27: /* Rollup 4 */
654 if( h->mode == EIA608_MODE_POPUP || h->mode == EIA608_MODE_PAINTON )
656 Eia608EraseScreen( h, true );
657 Eia608EraseScreen( h, false );
662 h->mode = EIA608_MODE_ROLLUP_2;
663 else if( d2 == 0x26 )
664 h->mode = EIA608_MODE_ROLLUP_3;
666 h->mode = EIA608_MODE_ROLLUP_4;
668 h->cursor.i_column = 0;
669 h->cursor.i_row = h->i_row_rollup;
671 case 0x28: /* Flash on */
674 case 0x29: /* Resume direct captionning */
675 h->mode = EIA608_MODE_PAINTON;
677 case 0x2a: /* Text restart */
681 case 0x2b: /* Resume text display */
682 h->mode = EIA608_MODE_TEXT;
685 case 0x2c: /* Erase displayed memory */
686 Eia608EraseScreen( h, true );
689 case 0x2d: /* Carriage return */
693 case 0x2e: /* Erase non displayed memory */
694 Eia608EraseScreen( h, false );
696 case 0x2f: /* End of caption (flip screen if not paint on) */
697 if( h->mode != EIA608_MODE_PAINTON )
698 h->i_screen = 1 - h->i_screen;
699 h->mode = EIA608_MODE_POPUP;
700 h->cursor.i_column = 0;
702 h->color = EIA608_COLOR_DEFAULT;
703 h->font = EIA608_FONT_REGULAR;
709 static bool Eia608ParseCommand0x17( eia608_t *h, uint8_t d2 )
713 case 0x21: /* Tab offset 1 */
714 Eia608Cursor( h, 1 );
716 case 0x22: /* Tab offset 2 */
717 Eia608Cursor( h, 2 );
719 case 0x23: /* Tab offset 3 */
720 Eia608Cursor( h, 3 );
725 static bool Eia608ParsePac( eia608_t *h, uint8_t d1, uint8_t d2 )
727 static const int pi_row[] = {
728 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
730 const int i_row_index = ( (d1<<1) & 0x0e) | ( (d2>>5) & 0x01 );
732 assert( d2 >= 0x40 && d2 <= 0x7f );
734 if( pi_row[i_row_index] <= 0 )
738 if( h->mode != EIA608_MODE_TEXT )
739 h->cursor.i_row = pi_row[i_row_index] - 1;
740 h->i_row_rollup = pi_row[i_row_index] - 1;
744 else if( d2 >= 0x40 )
746 h->cursor.i_column = pac2_attribs[d2].i_column;
747 h->color = pac2_attribs[d2].i_color;
748 h->font = pac2_attribs[d2].i_font;
753 static bool Eia608ParseData( eia608_t *h, uint8_t d1, uint8_t d2 )
755 bool b_changed = false;
757 if( d1 >= 0x18 && d1 <= 0x1f )
760 #define ON( d2min, d2max, cmd ) do { if( d2 >= d2min && d2 <= d2max ) b_changed = cmd; } while(0)
764 ON( 0x20, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
765 ON( 0x30, 0x3f, Eia608ParseDouble( h, d2 ) );
767 case 0x12: case 0x13:
768 ON( 0x20, 0x3f, Eia608ParseExtended( h, d1, d2 ) );
770 case 0x14: case 0x15:
771 ON( 0x20, 0x2f, Eia608ParseCommand0x14( h, d2 ) );
774 ON( 0x21, 0x22, Eia608ParseCommand0x17( h, d2 ) );
775 ON( 0x2e, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
779 ON( 0x40, 0x5f, Eia608ParsePac( h, d1, d2 ) );
780 else if( d1 >= 0x11 && d1 <= 0x17 )
781 ON( 0x40, 0x7f, Eia608ParsePac( h, d1, d2 ) );
785 b_changed = Eia608ParseSingle( h, d1 );
787 b_changed |= Eia608ParseSingle( h, d2 );
792 static void Eia608TextUtf8( char *psz_utf8, uint8_t c ) // Returns number of bytes used
794 #define E1(c,u) { c, { u, '\0' } }
795 #define E2(c,u1,u2) { c, { u1, u2, '\0' } }
796 #define E3(c,u1,u2,u3) { c, { u1, u2, u3, '\0' } }
797 static const struct {
801 // Regular line-21 character set, mostly ASCII except these exceptions
802 E2( 0x2a, 0xc3,0xa1), // lowercase a, acute accent
803 E2( 0x5c, 0xc3,0xa9), // lowercase e, acute accent
804 E2( 0x5e, 0xc3,0xad), // lowercase i, acute accent
805 E2( 0x5f, 0xc3,0xb3), // lowercase o, acute accent
806 E2( 0x60, 0xc3,0xba), // lowercase u, acute accent
807 E2( 0x7b, 0xc3,0xa7), // lowercase c with cedilla
808 E2( 0x7c, 0xc3,0xb7), // division symbol
809 E2( 0x7d, 0xc3,0x91), // uppercase N tilde
810 E2( 0x7e, 0xc3,0xb1), // lowercase n tilde
811 // THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
812 // THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F
813 E2( 0x80, 0xc2,0xae), // Registered symbol (R)
814 E2( 0x81, 0xc2,0xb0), // degree sign
815 E2( 0x82, 0xc2,0xbd), // 1/2 symbol
816 E2( 0x83, 0xc2,0xbf), // Inverted (open) question mark
817 E3( 0x84, 0xe2,0x84,0xa2), // Trademark symbol (TM)
818 E2( 0x85, 0xc2,0xa2), // Cents symbol
819 E2( 0x86, 0xc2,0xa3), // Pounds sterling
820 E3( 0x87, 0xe2,0x99,0xaa), // Music note
821 E2( 0x88, 0xc3,0xa0), // lowercase a, grave accent
822 E2( 0x89, 0xc2,0xa0), // transparent space
823 E2( 0x8a, 0xc3,0xa8), // lowercase e, grave accent
824 E2( 0x8b, 0xc3,0xa2), // lowercase a, circumflex accent
825 E2( 0x8c, 0xc3,0xaa), // lowercase e, circumflex accent
826 E2( 0x8d, 0xc3,0xae), // lowercase i, circumflex accent
827 E2( 0x8e, 0xc3,0xb4), // lowercase o, circumflex accent
828 E2( 0x8f, 0xc3,0xbb), // lowercase u, circumflex accent
829 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
830 // THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F
831 E2( 0x90, 0xc3,0x81), // capital letter A with acute
832 E2( 0x91, 0xc3,0x89), // capital letter E with acute
833 E2( 0x92, 0xc3,0x93), // capital letter O with acute
834 E2( 0x93, 0xc3,0x9a), // capital letter U with acute
835 E2( 0x94, 0xc3,0x9c), // capital letter U with diaresis
836 E2( 0x95, 0xc3,0xbc), // lowercase letter U with diaeresis
837 E1( 0x96, 0x27), // apostrophe
838 E2( 0x97, 0xc2,0xa1), // inverted exclamation mark
839 E1( 0x98, 0x2a), // asterisk
840 E1( 0x99, 0x27), // apostrophe (yes, duped). See CCADI source code.
841 E1( 0x9a, 0x2d), // hyphen-minus
842 E2( 0x9b, 0xc2,0xa9), // copyright sign
843 E3( 0x9c, 0xe2,0x84,0xa0), // Service mark
844 E1( 0x9d, 0x2e), // Full stop (.)
845 E3( 0x9e, 0xe2,0x80,0x9c), // Quotation mark
846 E3( 0x9f, 0xe2,0x80,0x9d), // Quotation mark
847 E2( 0xa0, 0xc3,0x80), // uppercase A, grave accent
848 E2( 0xa1, 0xc3,0x82), // uppercase A, circumflex
849 E2( 0xa2, 0xc3,0x87), // uppercase C with cedilla
850 E2( 0xa3, 0xc3,0x88), // uppercase E, grave accent
851 E2( 0xa4, 0xc3,0x8a), // uppercase E, circumflex
852 E2( 0xa5, 0xc3,0x8b), // capital letter E with diaresis
853 E2( 0xa6, 0xc3,0xab), // lowercase letter e with diaresis
854 E2( 0xa7, 0xc3,0x8e), // uppercase I, circumflex
855 E2( 0xa8, 0xc3,0x8f), // uppercase I, with diaresis
856 E2( 0xa9, 0xc3,0xaf), // lowercase i, with diaresis
857 E2( 0xaa, 0xc3,0x94), // uppercase O, circumflex
858 E2( 0xab, 0xc3,0x99), // uppercase U, grave accent
859 E2( 0xac, 0xc3,0xb9), // lowercase u, grave accent
860 E2( 0xad, 0xc3,0x9b), // uppercase U, circumflex
861 E2( 0xae, 0xc2,0xab), // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
862 E2( 0xaf, 0xc2,0xbb), // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
863 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
864 // THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F
865 E2( 0xb0, 0xc3,0x83), // Uppercase A, tilde
866 E2( 0xb1, 0xc3,0xa3), // Lowercase a, tilde
867 E2( 0xb2, 0xc3,0x8d), // Uppercase I, acute accent
868 E2( 0xb3, 0xc3,0x8c), // Uppercase I, grave accent
869 E2( 0xb4, 0xc3,0xac), // Lowercase i, grave accent
870 E2( 0xb5, 0xc3,0x92), // Uppercase O, grave accent
871 E2( 0xb6, 0xc3,0xb2), // Lowercase o, grave accent
872 E2( 0xb7, 0xc3,0x95), // Uppercase O, tilde
873 E2( 0xb8, 0xc3,0xb5), // Lowercase o, tilde
874 E1( 0xb9, 0x7b), // Open curly brace
875 E1( 0xba, 0x7d), // Closing curly brace
876 E1( 0xbb, 0x5c), // Backslash
877 E1( 0xbc, 0x5e), // Caret
878 E1( 0xbd, 0x5f), // Underscore
879 E2( 0xbe, 0xc2,0xa6), // Pipe (broken bar)
880 E1( 0xbf, 0x7e), // Tilde (utf8 code unsure)
881 E2( 0xc0, 0xc3,0x84), // Uppercase A, umlaut
882 E2( 0xc1, 0xc3,0xa4), // Lowercase A, umlaut
883 E2( 0xc2, 0xc3,0x96), // Uppercase O, umlaut
884 E2( 0xc3, 0xc3,0xb6), // Lowercase o, umlaut
885 E2( 0xc4, 0xc3,0x9f), // Esszett (sharp S)
886 E2( 0xc5, 0xc2,0xa5), // Yen symbol
887 E2( 0xc6, 0xc2,0xa4), // Currency symbol
888 E1( 0xc7, 0x7c), // Vertical bar
889 E2( 0xc8, 0xc3,0x85), // Uppercase A, ring
890 E2( 0xc9, 0xc3,0xa5), // Lowercase A, ring
891 E2( 0xca, 0xc3,0x98), // Uppercase O, slash
892 E2( 0xcb, 0xc3,0xb8), // Lowercase o, slash
893 E3( 0xcc, 0xe2,0x8c,0x9c), // Upper left corner
894 E3( 0xcd, 0xe2,0x8c,0x9d), // Upper right corner
895 E3( 0xce, 0xe2,0x8c,0x9e), // Lower left corner
896 E3( 0xcf, 0xe2,0x8c,0x9f), // Lower right corner
904 for( size_t i = 0; i < ARRAY_SIZE(c2utf8) ; i++ )
905 if( c2utf8[i].c == c ) {
906 strcpy( psz_utf8, c2utf8[i].utf8 );
910 psz_utf8[0] = c < 0x80 ? c : '?'; /* Normal : Unsupported */
914 static void Eia608Strlcat( char *d, const char *s, int i_max )
917 strncat( d, s, i_max-1 - strnlen(d, i_max-1));
922 #define CAT(t) Eia608Strlcat( psz_text, t, i_text_max )
924 static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_text_max, int i_row, bool b_html )
926 const uint8_t *p_char = screen->characters[i_row];
927 const eia608_color_t *p_color = screen->colors[i_row];
928 const eia608_font_t *p_font = screen->fonts[i_row];
932 eia608_color_t last_color = EIA608_COLOR_DEFAULT;
933 bool b_last_italics = false;
934 bool b_last_underline = false;
937 /* Search the start */
940 /* Ensure we get a monospaced font (required for accurate positioning */
944 /* Convert leading spaces to non-breaking so that they don't get
945 stripped by the RenderHtml routine as regular whitespace */
946 while( i_start < EIA608_SCREEN_COLUMNS && p_char[i_start] == ' ' ) {
947 Eia608TextUtf8( utf8, 0x89 );
953 i_end = EIA608_SCREEN_COLUMNS-1;
954 while( i_end > i_start && p_char[i_end] == ' ' )
958 for( x = i_start; x <= i_end; x++ )
960 eia608_color_t color = p_color[x];
961 bool b_italics = p_font[x] & EIA608_FONT_ITALICS;
962 bool b_underline = p_font[x] & EIA608_FONT_UNDERLINE;
967 bool b_close_color, b_close_italics, b_close_underline;
969 /* We create the tags font / i / u in that orders */
970 b_close_color = color != last_color && last_color != EIA608_COLOR_DEFAULT;
971 b_close_italics = !b_italics && b_last_italics;
972 b_close_underline = !b_underline && b_last_underline;
974 /* Be sure to create valid html */
975 b_close_italics |= b_last_italics && b_close_color;
976 b_close_underline |= b_last_underline && ( b_close_italics || b_close_color );
978 if( b_close_underline )
980 if( b_close_italics )
985 if( color != EIA608_COLOR_DEFAULT && color != last_color)
987 static const char *ppsz_color[] = {
994 "#ff00ff", // magenta
995 "#ffffff", // user defined XXX we use white
997 CAT( "<font color=\"" );
998 CAT( ppsz_color[color] );
1001 if( ( b_close_italics && b_italics ) || ( b_italics && !b_last_italics ) )
1003 if( ( b_close_underline && b_underline ) || ( b_underline && !b_last_underline ) )
1008 /* Escape XML reserved characters
1009 http://www.w3.org/TR/xml/#syntax */
1010 switch (p_char[x]) {
1027 Eia608TextUtf8( utf8, p_char[x] );
1032 Eia608TextUtf8( utf8, p_char[x] );
1037 b_last_underline = b_underline;
1038 b_last_italics = b_italics;
1043 if( b_last_underline )
1045 if( b_last_italics )
1047 if( last_color != EIA608_COLOR_DEFAULT )
1055 static void Eia608Init( eia608_t *h )
1057 memset( h, 0, sizeof(*h) );
1063 Eia608ClearScreen( h, 0 );
1064 Eia608ClearScreen( h, 1 );
1066 /* Cursor for writing text */
1067 h->cursor.i_column = 0;
1068 h->cursor.i_row = 0;
1072 h->mode = EIA608_MODE_POPUP;
1073 h->color = EIA608_COLOR_DEFAULT;
1074 h->font = EIA608_FONT_REGULAR;
1075 h->i_row_rollup = EIA608_SCREEN_ROWS-1;
1077 static bool Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] )
1079 const uint8_t d1 = data[0] & 0x7f; /* Removed parity bit */
1080 const uint8_t d2 = data[1] & 0x7f;
1081 bool b_screen_changed = false;
1083 if( d1 == 0 && d2 == 0 )
1084 return false; /* Ignore padding (parity check are sometimes invalid on them) */
1086 Eia608ParseChannel( h, data );
1087 if( h->i_channel != i_channel_selected )
1089 //fprintf( stderr, "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC %x %x\n", data[0], data[1] );
1094 d1 != h->last.d1 || d2 != h->last.d2 ) /* Command codes can be repeated */
1095 b_screen_changed = Eia608ParseData( h, d1,d2 );
1100 else if( ( d1 >= 0x01 && d1 <= 0x0E ) || d1 == 0x0F )
1102 /* XDS block / End of XDS block */
1104 return b_screen_changed;
1107 static char *Eia608Text( eia608_t *h, bool b_html )
1109 const int i_size = EIA608_SCREEN_ROWS * 10 * EIA608_SCREEN_COLUMNS+1;
1110 struct eia608_screen *screen = &h->screen[h->i_screen];
1111 bool b_first = true;
1114 /* We allocate a buffer big enough for normal case */
1115 psz = malloc( i_size );
1120 Eia608Strlcat( psz, "<text>", i_size );
1121 for( int i = 0; i < EIA608_SCREEN_ROWS; i++ )
1124 Eia608Strlcat( psz, b_html ? "<br />" : "\n", i_size );
1127 Eia608TextLine( screen, psz, i_size, i, b_html );
1130 Eia608Strlcat( psz, "</text>", i_size );