1 /*****************************************************************************
2 * cc608.c : CC 608/708 subtitles decoder
3 *****************************************************************************
4 * Copyright (C) 2007 Laurent Aimar
7 * Authors: Laurent Aimar < fenrir # via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 /* The EIA 608 decoder part has been initialy based on ccextractor (GPL)
31 * On discontinuity reset the decoder state
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_codec.h>
43 #include <vlc_input.h>
46 #include <vlc_filter.h>
47 #include <vlc_image.h>
48 #include <vlc_charset.h>
49 #include <vlc_stream.h>
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
63 set_shortname( N_("CC 608/708"))
64 set_description( N_("Closed Captions decoder") )
65 set_capability( "decoder", 50 )
66 set_callbacks( Open, Close )
69 /*****************************************************************************
71 *****************************************************************************/
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,
84 EIA608_COLOR_WHITE = 0,
85 EIA608_COLOR_GREEN = 1,
86 EIA608_COLOR_BLUE = 2,
87 EIA608_COLOR_CYAN = 3,
89 EIA608_COLOR_YELLOW = 5,
90 EIA608_COLOR_MAGENTA = 6,
91 EIA608_COLOR_USERDEFINED = 7
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
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;
116 /* Current channel (used to reject packet without channel information) */
120 int i_screen; /* Displayed screen */
121 eia608_screen screen[2];
131 eia608_color_t color;
135 /* Last command pair (used to reject duplicated command) */
143 static void Eia608Init( eia608_t * );
144 static bool Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] );
145 static char *Eia608Text( eia608_t *h, bool b_html );
146 static void Eia608Exit( eia608_t * );
148 /* It will be enough up to 63 B frames, which is far too high for
149 * broadcast environment */
150 #define CC_MAX_REORDER_SIZE (64)
156 block_t *pp_block[CC_MAX_REORDER_SIZE];
164 static subpicture_t *Decode( decoder_t *, block_t ** );
166 /*****************************************************************************
167 * Open: probe the decoder and return score
168 *****************************************************************************
169 * Tries to launch a decoder and return score so that the interface is able
171 *****************************************************************************/
172 static int Open( vlc_object_t *p_this )
174 decoder_t *p_dec = (decoder_t*)p_this;
175 decoder_sys_t *p_sys;
179 switch( p_dec->fmt_in.i_codec )
181 case VLC_FOURCC('c','c','1',' '):
182 i_field = 0; i_channel = 1;
184 case VLC_FOURCC('c','c','2',' '):
185 i_field = 0; i_channel = 2;
187 case VLC_FOURCC('c','c','3',' '):
188 i_field = 1; i_channel = 1;
190 case VLC_FOURCC('c','c','4',' '):
191 i_field = 1; i_channel = 2;
198 p_dec->pf_decode_sub = Decode;
200 /* Allocate the memory needed to store the decoder's structure */
201 p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
206 p_sys->i_field = i_field;
207 p_sys->i_channel = i_channel;
209 Eia608Init( &p_sys->eia608 );
211 p_dec->fmt_out.i_cat = SPU_ES;
212 p_dec->fmt_out.i_codec = VLC_CODEC_TEXT;
217 /****************************************************************************
218 * Decode: the whole thing
219 ****************************************************************************
221 ****************************************************************************/
222 static void Push( decoder_t *, block_t * );
223 static block_t *Pop( decoder_t * );
224 static subpicture_t *Convert( decoder_t *, block_t * );
226 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
228 if( pp_block && *pp_block )
230 Push( p_dec, *pp_block );
236 block_t *p_block = Pop( p_dec );
240 subpicture_t *p_spu = Convert( p_dec, p_block );
247 /*****************************************************************************
248 * CloseDecoder: clean up the decoder
249 *****************************************************************************/
250 static void Close( vlc_object_t *p_this )
252 decoder_t *p_dec = (decoder_t *)p_this;
253 decoder_sys_t *p_sys = p_dec->p_sys;
256 for( i = 0; i < p_sys->i_block; i++ )
257 block_Release( p_sys->pp_block[i] );
258 Eia608Exit( &p_sys->eia608 );
262 /*****************************************************************************
264 *****************************************************************************/
265 static void Push( decoder_t *p_dec, block_t *p_block )
267 decoder_sys_t *p_sys = p_dec->p_sys;
269 if( p_sys->i_block >= CC_MAX_REORDER_SIZE )
271 msg_Warn( p_dec, "Trashing a CC entry" );
272 memmove( &p_sys->pp_block[0], &p_sys->pp_block[1], sizeof(*p_sys->pp_block) * (CC_MAX_REORDER_SIZE-1) );
275 p_sys->pp_block[p_sys->i_block++] = p_block;
277 static block_t *Pop( decoder_t *p_dec )
279 decoder_sys_t *p_sys = p_dec->p_sys;
283 /* XXX Cc captions data are OUT OF ORDER (because we receive them in the bitstream
284 * order (ie ordered by video picture dts) instead of the display order.
285 * We will simulate a simple IPB buffer scheme
286 * and reorder with pts.
287 * XXX it won't work with H264 which use non out of order B picture or MMCO
290 /* Wait for a P and output all *previous* picture by pts order (for
291 * hierarchical B frames) */
292 if( p_sys->i_block <= 1 ||
293 ( p_sys->pp_block[p_sys->i_block-1]->i_flags & BLOCK_FLAG_TYPE_B ) )
296 p_block = p_sys->pp_block[i_index = 0];
297 if( p_block->i_pts > VLC_TS_INVALID )
299 for( i = 1; i < p_sys->i_block-1; i++ )
301 if( p_sys->pp_block[i]->i_pts > VLC_TS_INVALID && p_block->i_pts > VLC_TS_INVALID &&
302 p_sys->pp_block[i]->i_pts < p_block->i_pts )
303 p_block = p_sys->pp_block[i_index = i];
306 assert( i_index+1 < p_sys->i_block );
307 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 ) );
313 static subpicture_t *Subtitle( decoder_t *p_dec, char *psz_subtitle, char *psz_html, mtime_t i_pts )
315 //decoder_sys_t *p_sys = p_dec->p_sys;
316 subpicture_t *p_spu = NULL;
319 /* We cannot display a subpicture with no date */
320 if( i_pts <= VLC_TS_INVALID )
322 msg_Warn( p_dec, "subtitle without a date" );
326 EnsureUTF8( psz_subtitle );
328 EnsureUTF8( psz_html );
330 /* Create the subpicture unit */
331 p_spu = decoder_NewSubpicture( p_dec );
334 msg_Warn( p_dec, "can't get spu buffer" );
335 free( psz_subtitle );
340 /* Create a new subpicture region */
341 memset( &fmt, 0, sizeof(video_format_t) );
342 fmt.i_chroma = VLC_CODEC_TEXT;
343 fmt.i_width = fmt.i_height = 0;
344 fmt.i_x_offset = fmt.i_y_offset = 0;
345 p_spu->p_region = subpicture_region_New( &fmt );
346 if( !p_spu->p_region )
348 msg_Err( p_dec, "cannot allocate SPU region" );
349 free( psz_subtitle );
351 decoder_DeleteSubpicture( p_dec, p_spu );
355 /* Decode and format the subpicture unit */
356 /* Normal text subs, easy markup */
357 p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM;// | SUBPICTURE_ALIGN_LEFT;// | p_sys->i_align;
358 p_spu->p_region->i_x = 0; //p_sys->i_align ? 20 : 0;
359 p_spu->p_region->i_y = 10;
361 p_spu->p_region->psz_text = psz_subtitle;
362 p_spu->p_region->psz_html = psz_html;
364 p_spu->i_start = i_pts;
365 p_spu->i_stop = i_pts + 10000000; /* 10s max */
366 p_spu->b_ephemer = true;
367 p_spu->b_absolute = false;
372 static subpicture_t *Convert( decoder_t *p_dec, block_t *p_block )
376 decoder_sys_t *p_sys = p_dec->p_sys;
377 const int64_t i_pts = p_block->i_pts;
378 bool b_changed = false;
380 /* TODO do the real decoding here */
381 while( p_block->i_buffer >= 3 )
383 if( p_block->p_buffer[0] == p_sys->i_field )
384 b_changed |= Eia608Parse( &p_sys->eia608, p_sys->i_channel, &p_block->p_buffer[1] );
386 p_block->i_buffer -= 3;
387 p_block->p_buffer += 3;
390 block_Release( p_block );
394 char *psz_subtitle = Eia608Text( &p_sys->eia608, false );
395 char *psz_html = NULL;//Eia608Text( &p_sys->eia608, true );
396 return Subtitle( p_dec, psz_subtitle, psz_html, i_pts );
402 /*****************************************************************************
404 *****************************************************************************/
405 static const struct {
406 eia608_color_t i_color;
407 eia608_font_t i_font;
410 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 0 },
411 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 0 },
412 { EIA608_COLOR_GREEN, EIA608_FONT_REGULAR, 0 },
413 { EIA608_COLOR_GREEN, EIA608_FONT_UNDERLINE, 0 },
414 { EIA608_COLOR_BLUE, EIA608_FONT_REGULAR, 0 },
415 { EIA608_COLOR_BLUE, EIA608_FONT_UNDERLINE, 0 },
416 { EIA608_COLOR_CYAN, EIA608_FONT_REGULAR, 0 },
417 { EIA608_COLOR_CYAN, EIA608_FONT_UNDERLINE, 0 },
418 { EIA608_COLOR_RED, EIA608_FONT_REGULAR, 0 },
419 { EIA608_COLOR_RED, EIA608_FONT_UNDERLINE, 0 },
420 { EIA608_COLOR_YELLOW, EIA608_FONT_REGULAR, 0 },
421 { EIA608_COLOR_YELLOW, EIA608_FONT_UNDERLINE, 0 },
422 { EIA608_COLOR_MAGENTA, EIA608_FONT_REGULAR, 0 },
423 { EIA608_COLOR_MAGENTA, EIA608_FONT_UNDERLINE, 0 },
424 { EIA608_COLOR_WHITE, EIA608_FONT_ITALICS, 0 },
425 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE_ITALICS, 0 },
427 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 0 },
428 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 0 },
429 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 4 },
430 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 4 },
431 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 8 },
432 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 8 },
433 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 12 },
434 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 12 },
435 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 16 },
436 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 16 },
437 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 20 },
438 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 20 },
439 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 24 },
440 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 24 },
441 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 28 },
442 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 28 } ,
445 #define EIA608_COLOR_DEFAULT EIA608_COLOR_WHITE
447 static void Eia608Cursor( eia608_t *h, int dx )
449 h->cursor.i_column += dx;
450 if( h->cursor.i_column < 0 )
451 h->cursor.i_column = 0;
452 else if( h->cursor.i_column > EIA608_SCREEN_COLUMNS-1 )
453 h->cursor.i_column = EIA608_SCREEN_COLUMNS-1;
455 static void Eia608ClearScreenRowX( eia608_t *h, int i_screen, int i_row, int x )
457 eia608_screen *screen = &h->screen[i_screen];
462 screen->row_used[i_row] = false;
466 screen->row_used[i_row] = false;
467 for( i = 0; i < x; i++ )
469 if( screen->characters[i_row][i] != ' ' ||
470 screen->colors[i_row][i] != EIA608_COLOR_DEFAULT ||
471 screen->fonts[i_row][i] != EIA608_FONT_REGULAR )
473 screen->row_used[i_row] = true;
479 for( ; x < EIA608_SCREEN_COLUMNS+1; x++ )
481 screen->characters[i_row][x] = x < EIA608_SCREEN_COLUMNS ? ' ' : '\0';
482 screen->colors[i_row][x] = EIA608_COLOR_DEFAULT;
483 screen->fonts[i_row][x] = EIA608_FONT_REGULAR;
487 static void Eia608ClearScreenRow( eia608_t *h, int i_screen, int i_row )
489 Eia608ClearScreenRowX( h, i_screen, i_row, 0 );
492 static void Eia608ClearScreen( eia608_t *h, int i_screen )
495 for( i = 0; i < EIA608_SCREEN_ROWS; i++ )
496 Eia608ClearScreenRow( h, i_screen, i );
499 static int Eia608GetWritingScreenIndex( eia608_t *h )
503 case EIA608_MODE_POPUP: // Non displayed screen
504 return 1 - h->i_screen;
506 case EIA608_MODE_ROLLUP_2: // Displayed screen
507 case EIA608_MODE_ROLLUP_3:
508 case EIA608_MODE_ROLLUP_4:
509 case EIA608_MODE_PAINTON:
512 /* It cannot happen, else it is a bug */
518 static void Eia608EraseScreen( eia608_t *h, bool b_displayed )
520 Eia608ClearScreen( h, b_displayed ? h->i_screen : (1-h->i_screen) );
523 static void Eia608Write( eia608_t *h, const uint8_t c )
525 const int i_row = h->cursor.i_row;
526 const int i_column = h->cursor.i_column;
527 eia608_screen *screen;
529 if( h->mode == EIA608_MODE_TEXT )
532 screen = &h->screen[Eia608GetWritingScreenIndex( h )];
534 screen->characters[i_row][i_column] = c;
535 screen->colors[i_row][i_column] = h->color;
536 screen->fonts[i_row][i_column] = h->font;
537 screen->row_used[i_row] = true;
538 Eia608Cursor( h, 1 );
540 static void Eia608Erase( eia608_t *h )
542 const int i_row = h->cursor.i_row;
543 const int i_column = h->cursor.i_column - 1;
544 eia608_screen *screen;
546 if( h->mode == EIA608_MODE_TEXT )
551 screen = &h->screen[Eia608GetWritingScreenIndex( h )];
553 /* FIXME do we need to reset row_used/colors/font ? */
554 screen->characters[i_row][i_column] = ' ';
555 Eia608Cursor( h, -1 );
557 static void Eia608EraseToEndOfRow( eia608_t *h )
559 if( h->mode == EIA608_MODE_TEXT )
562 Eia608ClearScreenRowX( h, Eia608GetWritingScreenIndex( h ), h->cursor.i_row, h->cursor.i_column );
565 static void Eia608RollUp( eia608_t *h )
567 const int i_screen = Eia608GetWritingScreenIndex( h );
568 eia608_screen *screen = &h->screen[i_screen];
574 if( h->mode == EIA608_MODE_ROLLUP_2 )
576 else if( h->mode == EIA608_MODE_ROLLUP_3 )
578 else if( h->mode == EIA608_MODE_ROLLUP_4 )
583 /* Reset the cursor */
584 h->cursor.i_column = 0;
586 /* Erase lines above our window */
587 for( i = 0; i < h->cursor.i_row - keep_lines; i++ )
588 Eia608ClearScreenRow( h, i_screen, i );
591 for( i = 0; i < keep_lines-1; i++ )
593 const int i_row = h->cursor.i_row - keep_lines + i + 1;
596 assert( i_row+1 < EIA608_SCREEN_ROWS );
597 memcpy( screen->characters[i_row], screen->characters[i_row+1], sizeof(*screen->characters) );
598 memcpy( screen->colors[i_row], screen->colors[i_row+1], sizeof(*screen->colors) );
599 memcpy( screen->fonts[i_row], screen->fonts[i_row+1], sizeof(*screen->fonts) );
600 screen->row_used[i_row] = screen->row_used[i_row+1];
602 /* Reset current row */
603 Eia608ClearScreenRow( h, i_screen, h->cursor.i_row );
605 static void Eia608ParseChannel( eia608_t *h, const uint8_t d[2] )
607 /* Check odd parity */
608 static const int p4[16] = {
609 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
611 if( p4[d[0] & 0xf] == p4[d[0] >> 4] ||
612 p4[d[1] & 0xf] == p4[ d[1] >> 4] )
619 const int d1 = d[0] & 0x7f;
620 // const int d2 = d[1] & 0x7f;
623 else if( d1 == 0x1c )
625 else if( d1 == 0x15 )
627 else if( d1 == 0x1d )
630 static bool Eia608ParseTextAttribute( eia608_t *h, uint8_t d2 )
632 const int i_index = d2 - 0x20;
633 assert( d2 >= 0x20 && d2 <= 0x2f );
635 h->color = pac2_attribs[i_index].i_color;
636 h->font = pac2_attribs[i_index].i_font;
637 Eia608Cursor( h, 1 );
641 static bool Eia608ParseSingle( eia608_t *h, const uint8_t dx )
643 assert( dx >= 0x20 );
644 Eia608Write( h, dx );
647 static bool Eia608ParseDouble( eia608_t *h, uint8_t d2 )
649 assert( d2 >= 0x30 && d2 <= 0x3f );
650 Eia608Write( h, d2 + 0x50 ); /* We use charaters 0x80...0x8f */
653 static bool Eia608ParseExtended( eia608_t *h, uint8_t d1, uint8_t d2 )
655 assert( d2 >= 0x20 && d2 <= 0x3f );
656 assert( d1 == 0x12 || d1 == 0x13 );
658 d2 += 0x70; /* We use charaters 0x90-0xaf */
660 d2 += 0x90; /* We use charaters 0xb0-0xcf */
662 /* The extended characters replace the previous one with a more
664 Eia608Cursor( h, -1 );
665 Eia608Write( h, d2 );
668 static bool Eia608ParseCommand0x14( eia608_t *h, uint8_t d2 )
670 bool b_changed = false;
674 case 0x20: /* Resume caption loading */
675 h->mode = EIA608_MODE_POPUP;
677 case 0x21: /* Backspace */
681 case 0x22: /* Reserved */
684 case 0x24: /* Delete to end of row */
685 Eia608EraseToEndOfRow( h );
687 case 0x25: /* Rollup 2 */
688 case 0x26: /* Rollup 3 */
689 case 0x27: /* Rollup 4 */
690 if( h->mode == EIA608_MODE_POPUP || h->mode == EIA608_MODE_PAINTON )
692 Eia608EraseScreen( h, true );
693 Eia608EraseScreen( h, false );
698 h->mode = EIA608_MODE_ROLLUP_2;
699 else if( d2 == 0x26 )
700 h->mode = EIA608_MODE_ROLLUP_3;
702 h->mode = EIA608_MODE_ROLLUP_4;
704 h->cursor.i_column = 0;
705 h->cursor.i_row = h->i_row_rollup;
707 case 0x28: /* Flash on */
710 case 0x29: /* Resume direct captionning */
711 h->mode = EIA608_MODE_PAINTON;
713 case 0x2a: /* Text restart */
717 case 0x2b: /* Resume text display */
718 h->mode = EIA608_MODE_TEXT;
721 case 0x2c: /* Erase displayed memory */
722 Eia608EraseScreen( h, true );
725 case 0x2d: /* Carriage return */
729 case 0x2e: /* Erase non displayed memory */
730 Eia608EraseScreen( h, false );
732 case 0x2f: /* End of caption (flip screen if not paint on) */
733 if( h->mode != EIA608_MODE_PAINTON )
734 h->i_screen = 1 - h->i_screen;
735 h->mode = EIA608_MODE_POPUP;
736 h->cursor.i_column = 0;
738 h->color = EIA608_COLOR_DEFAULT;
739 h->font = EIA608_FONT_REGULAR;
745 static bool Eia608ParseCommand0x17( eia608_t *h, uint8_t d2 )
749 case 0x21: /* Tab offset 1 */
750 Eia608Cursor( h, 1 );
752 case 0x22: /* Tab offset 2 */
753 Eia608Cursor( h, 2 );
755 case 0x23: /* Tab offset 3 */
756 Eia608Cursor( h, 3 );
761 static bool Eia608ParsePac( eia608_t *h, uint8_t d1, uint8_t d2 )
763 static const int pi_row[] = {
764 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
766 const int i_row_index = ( (d1<<1) & 0x0e) | ( (d2>>5) & 0x01 );
768 assert( d2 >= 0x40 && d2 <= 0x7f );
770 if( pi_row[i_row_index] <= 0 )
774 if( h->mode != EIA608_MODE_TEXT )
775 h->cursor.i_row = pi_row[i_row_index] - 1;
776 h->i_row_rollup = pi_row[i_row_index] - 1;
780 else if( d2 >= 0x40 )
782 h->cursor.i_column = pac2_attribs[d2].i_column;
786 static bool Eia608ParseData( eia608_t *h, uint8_t d1, uint8_t d2 )
788 bool b_changed = false;
790 if( d1 >= 0x18 && d1 <= 0x1f )
793 #define ON( d2min, d2max, cmd ) do { if( d2 >= d2min && d2 <= d2max ) b_changed = cmd; } while(0)
797 ON( 0x20, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
798 ON( 0x30, 0x3f, Eia608ParseDouble( h, d2 ) );
800 case 0x12: case 0x13:
801 ON( 0x20, 0x3f, Eia608ParseExtended( h, d1, d2 ) );
803 case 0x14: case 0x15:
804 ON( 0x20, 0x2f, Eia608ParseCommand0x14( h, d2 ) );
807 ON( 0x21, 0x22, Eia608ParseCommand0x17( h, d2 ) );
808 ON( 0x2e, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
812 ON( 0x40, 0x5f, Eia608ParsePac( h, d1, d2 ) );
813 else if( d1 >= 0x11 && d1 <= 0x17 )
814 ON( 0x40, 0x7f, Eia608ParsePac( h, d1, d2 ) );
818 b_changed = Eia608ParseSingle( h, d1 );
820 b_changed |= Eia608ParseSingle( h, d2 );
825 static void Eia608TextUtf8( char *psz_utf8, uint8_t c ) // Returns number of bytes used
827 #define E1(c,u) { c, { u, '\0' } }
828 #define E2(c,u1,u2) { c, { u1, u2, '\0' } }
829 #define E3(c,u1,u2,u3) { c, { u1, u2, u3, '\0' } }
830 static const struct {
834 // Regular line-21 character set, mostly ASCII except these exceptions
835 E2( 0x2a, 0xc3,0xa1), // lowercase a, acute accent
836 E2( 0x5c, 0xc3,0xa9), // lowercase e, acute accent
837 E2( 0x5e, 0xc3,0xad), // lowercase i, acute accent
838 E2( 0x5f, 0xc3,0xb3), // lowercase o, acute accent
839 E2( 0x60, 0xc3,0xba), // lowercase u, acute accent
840 E2( 0x7b, 0xc3,0xa7), // lowercase c with cedilla
841 E2( 0x7c, 0xc3,0xb7), // division symbol
842 E2( 0x7d, 0xc3,0x91), // uppercase N tilde
843 E2( 0x7e, 0xc3,0xb1), // lowercase n tilde
844 // THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
845 // THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F
846 E2( 0x80, 0xc2,0xae), // Registered symbol (R)
847 E2( 0x81, 0xc2,0xb0), // degree sign
848 E2( 0x82, 0xc2,0xbd), // 1/2 symbol
849 E2( 0x83, 0xc2,0xbf), // Inverted (open) question mark
850 E3( 0x84, 0xe2,0x84,0xa2), // Trademark symbol (TM)
851 E2( 0x85, 0xc2,0xa2), // Cents symbol
852 E2( 0x86, 0xc2,0xa3), // Pounds sterling
853 E3( 0x87, 0xe2,0x99,0xaa), // Music note
854 E2( 0x88, 0xc3,0xa0), // lowercase a, grave accent
855 E1( 0x89, 0x20), // transparent space, we make it regular
856 E2( 0x8a, 0xc3,0xa8), // lowercase e, grave accent
857 E2( 0x8b, 0xc3,0xa2), // lowercase a, circumflex accent
858 E2( 0x8c, 0xc3,0xaa), // lowercase e, circumflex accent
859 E2( 0x8d, 0xc3,0xae), // lowercase i, circumflex accent
860 E2( 0x8e, 0xc3,0xb4), // lowercase o, circumflex accent
861 E2( 0x8f, 0xc3,0xbb), // lowercase u, circumflex accent
862 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
863 // THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F
864 E2( 0x90, 0xc3,0x81), // capital letter A with acute
865 E2( 0x91, 0xc3,0x89), // capital letter E with acute
866 E2( 0x92, 0xc3,0x93), // capital letter O with acute
867 E2( 0x93, 0xc3,0x9a), // capital letter U with acute
868 E2( 0x94, 0xc3,0x9c), // capital letter U with diaresis
869 E2( 0x95, 0xc3,0xbc), // lowercase letter U with diaeresis
870 E1( 0x96, 0x27), // apostrophe
871 E2( 0x97, 0xc1,0xa1), // inverted exclamation mark
872 E1( 0x98, 0x2a), // asterisk
873 E1( 0x99, 0x27), // apostrophe (yes, duped). See CCADI source code.
874 E1( 0x9a, 0x2d), // hyphen-minus
875 E2( 0x9b, 0xc2,0xa9), // copyright sign
876 E3( 0x9c, 0xe2,0x84,0xa0), // Service mark
877 E1( 0x9d, 0x2e), // Full stop (.)
878 E1( 0x9e, 0x22), // Quoatation mark
879 E1( 0x9f, 0x22), // Quoatation mark
880 E2( 0xa0, 0xc3,0x80), // uppercase A, grave accent
881 E2( 0xa1, 0xc3,0x82), // uppercase A, circumflex
882 E2( 0xa2, 0xc3,0x87), // uppercase C with cedilla
883 E2( 0xa3, 0xc3,0x88), // uppercase E, grave accent
884 E2( 0xa4, 0xc3,0x8a), // uppercase E, circumflex
885 E2( 0xa5, 0xc3,0x8b), // capital letter E with diaresis
886 E2( 0xa6, 0xc3,0xab), // lowercase letter e with diaresis
887 E2( 0xa7, 0xc3,0x8e), // uppercase I, circumflex
888 E2( 0xa8, 0xc3,0x8f), // uppercase I, with diaresis
889 E2( 0xa9, 0xc3,0xaf), // lowercase i, with diaresis
890 E2( 0xaa, 0xc3,0x94), // uppercase O, circumflex
891 E2( 0xab, 0xc3,0x99), // uppercase U, grave accent
892 E2( 0xac, 0xc3,0xb9), // lowercase u, grave accent
893 E2( 0xad, 0xc3,0x9b), // uppercase U, circumflex
894 E2( 0xae, 0xc2,0xab), // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
895 E2( 0xaf, 0xc2,0xbb), // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
896 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
897 // THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F
898 E2( 0xb0, 0xc3,0x83), // Uppercase A, tilde
899 E2( 0xb1, 0xc3,0xa3), // Lowercase a, tilde
900 E2( 0xb2, 0xc3,0x8d), // Uppercase I, acute accent
901 E2( 0xb3, 0xc3,0x8c), // Uppercase I, grave accent
902 E2( 0xb4, 0xc3,0xac), // Lowercase i, grave accent
903 E2( 0xb5, 0xc3,0x92), // Uppercase O, grave accent
904 E2( 0xb6, 0xc3,0xb2), // Lowercase o, grave accent
905 E2( 0xb7, 0xc3,0x95), // Uppercase O, tilde
906 E2( 0xb8, 0xc3,0xb5), // Lowercase o, tilde
907 E1( 0xb9, 0x7b), // Open curly brace
908 E1( 0xba, 0x7d), // Closing curly brace
909 E1( 0xbb, 0x5c), // Backslash
910 E1( 0xbc, 0x5e), // Caret
911 E1( 0xbd, 0x5f), // Underscore
912 E2( 0xbe, 0xc2,0xa6), // Pipe (broken bar)
913 E1( 0xbf, 0x7e), // Tilde (utf8 code unsure)
914 E2( 0xc0, 0xc3,0x84), // Uppercase A, umlaut
915 E2( 0xc1, 0xc3,0xa4), // Lowercase A, umlaut
916 E2( 0xc2, 0xc3,0x96), // Uppercase O, umlaut
917 E2( 0xc3, 0xc3,0xb6), // Lowercase o, umlaut
918 E2( 0xc4, 0xc3,0x9f), // Esszett (sharp S)
919 E2( 0xc5, 0xc2,0xa5), // Yen symbol
920 E2( 0xc6, 0xc2,0xa4), // Currency symbol
921 E1( 0xc7, 0x7c), // Vertical bar
922 E2( 0xc8, 0xc3,0x85), // Uppercase A, ring
923 E2( 0xc9, 0xc3,0xa5), // Lowercase A, ring
924 E2( 0xca, 0xc3,0x98), // Uppercase O, slash
925 E2( 0xcb, 0xc3,0xb8), // Lowercase o, slash
926 E3( 0xcc, 0xe2,0x8c,0x9c), // Upper left corner
927 E3( 0xcd, 0xe2,0x8c,0x9d), // Upper right corner
928 E3( 0xce, 0xe2,0x8c,0x9e), // Lower left corner
929 E3( 0xcf, 0xe2,0x8c,0x9f), // Lower right corner
937 static const int i_c2utf8 = sizeof(c2utf8)/sizeof(*c2utf8);
940 for( i = 0; i < i_c2utf8; i++ )
942 if( c2utf8[i].c == c )
947 psz_utf8[0] = c < 0x80 ? c : '?'; /* Normal : Unsupported */
952 strcpy( psz_utf8, c2utf8[i].utf8 );
956 static void Eia608Strlcat( char *d, const char *s, int i_max )
959 strncat( d, s, i_max-1 - strnlen(d, i_max-1));
964 static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_text_max, int i_row, bool b_html )
966 const uint8_t *p_char = screen->characters[i_row];
967 const eia608_color_t *p_color = screen->colors[i_row];
968 const eia608_font_t *p_font = screen->fonts[i_row];
972 eia608_color_t last_color = EIA608_COLOR_DEFAULT;
973 bool b_last_italics = false;
974 bool b_last_underline = false;
976 /* Search the start */
978 while( i_start < EIA608_SCREEN_COLUMNS-1 && p_char[i_start] == ' ' )
982 i_end = EIA608_SCREEN_COLUMNS-1;
983 while( i_end > i_start && p_char[i_end] == ' ' )
987 #define CAT(t) Eia608Strlcat( psz_text, t, i_text_max )
988 for( x = i_start; x <= i_end; x++ )
990 eia608_color_t color = p_color[x];
991 bool b_italics = p_font[x] & EIA608_FONT_ITALICS;
992 bool b_underline = p_font[x] & EIA608_FONT_UNDERLINE;
998 bool b_close_color, b_close_italics, b_close_underline;
1000 /* We create the tags font / i / u in that orders */
1001 b_close_color = color != last_color && last_color != EIA608_COLOR_DEFAULT;
1002 b_close_italics = !b_italics && b_last_italics;
1003 b_close_underline = !b_underline && b_last_underline;
1005 /* Be sure to create valid html */
1006 b_close_italics |= b_last_italics && b_close_color;
1007 b_close_underline = b_last_underline && ( b_close_italics || b_close_color );
1009 if( b_close_underline )
1011 if( b_close_italics )
1016 if( color != EIA608_COLOR_DEFAULT && color != last_color)
1018 static const char *ppsz_color[] = {
1024 "#ffff00", // yellow
1025 "#ff00ff", // magenta
1026 "#ffffff", // user defined XXX we use white
1028 CAT( "<font color=" );
1029 CAT( ppsz_color[color] );
1032 if( ( b_close_italics && b_italics ) || ( b_italics && !b_last_italics ) )
1034 if( ( b_close_underline && b_underline ) || ( b_underline && !b_last_underline ) )
1039 Eia608TextUtf8( utf8, p_char[x] );
1043 b_last_underline = b_underline;
1044 b_last_italics = b_italics;
1049 if( b_last_underline )
1051 if( b_last_italics )
1053 if( last_color != EIA608_COLOR_DEFAULT )
1060 static void Eia608Init( eia608_t *h )
1062 memset( h, 0, sizeof(*h) );
1068 Eia608ClearScreen( h, 0 );
1069 Eia608ClearScreen( h, 1 );
1071 /* Cursor for writing text */
1072 h->cursor.i_column = 0;
1073 h->cursor.i_row = 0;
1077 h->mode = EIA608_MODE_POPUP;
1078 h->color = EIA608_COLOR_DEFAULT;
1079 h->font = EIA608_FONT_REGULAR;
1080 h->i_row_rollup = EIA608_SCREEN_ROWS-1;
1082 static bool Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] )
1084 const uint8_t d1 = data[0] & 0x7f; /* Removed parity bit */
1085 const uint8_t d2 = data[1] & 0x7f;
1086 bool b_screen_changed = false;
1088 if( d1 == 0 && d2 == 0 )
1089 return false; /* Ignore padding (parity check are sometimes invalid on them) */
1091 Eia608ParseChannel( h, data );
1092 if( h->i_channel != i_channel_selected )
1094 //fprintf( stderr, "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC %x %x\n", data[0], data[1] );
1099 d1 != h->last.d1 || d2 != h->last.d2 ) /* Command codes can be repeated */
1100 b_screen_changed = Eia608ParseData( h, d1,d2 );
1105 else if( ( d1 >= 0x01 && d1 <= 0x0E ) || d1 == 0x0F )
1107 /* XDS block / End of XDS block */
1109 return b_screen_changed;
1112 static char *Eia608Text( eia608_t *h, bool b_html )
1114 const int i_size = EIA608_SCREEN_ROWS * 3 * EIA608_SCREEN_COLUMNS+1;
1115 struct eia608_screen *screen = &h->screen[h->i_screen];
1116 bool b_first = true;
1120 /* We allocate a buffer big enough for normal case */
1121 psz = malloc( i_size );
1126 Eia608Strlcat( psz, "<text>", i_size );
1127 for( i = 0; i < EIA608_SCREEN_ROWS; i++ )
1129 if( !screen->row_used[i] )
1133 Eia608Strlcat( psz, b_html ? "<br />" : "\n", i_size );
1136 Eia608TextLine( screen, psz, i_size, i, b_html );
1139 Eia608Strlcat( psz, "</text>", i_size );
1143 static void Eia608Exit( eia608_t *h )