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
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>
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
61 set_shortname( _("CC 608/708"));
62 set_description( _("Closed Captions decoder") );
63 set_capability( "decoder", 50 );
64 set_callbacks( Open, Close );
67 /*****************************************************************************
69 *****************************************************************************/
72 EIA608_MODE_POPUP = 0,
73 EIA608_MODE_ROLLUP_2 = 1,
74 EIA608_MODE_ROLLUP_3 = 2,
75 EIA608_MODE_ROLLUP_4 = 3,
76 EIA608_MODE_PAINTON = 4,
82 EIA608_COLOR_WHITE = 0,
83 EIA608_COLOR_GREEN = 1,
84 EIA608_COLOR_BLUE = 2,
85 EIA608_COLOR_CYAN = 3,
87 EIA608_COLOR_YELLOW = 5,
88 EIA608_COLOR_MAGENTA = 6,
89 EIA608_COLOR_USERDEFINED = 7
94 EIA608_FONT_REGULAR = 0x00,
95 EIA608_FONT_ITALICS = 0x01,
96 EIA608_FONT_UNDERLINE = 0x02,
97 EIA608_FONT_UNDERLINE_ITALICS = EIA608_FONT_UNDERLINE | EIA608_FONT_ITALICS
100 #define EIA608_SCREEN_ROWS 15
101 #define EIA608_SCREEN_COLUMNS 32
103 struct eia608_screen // A CC buffer
105 uint8_t characters[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1];
106 eia608_color_t colors[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1];
107 eia608_font_t fonts[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1]; // Extra char at the end for a 0
108 int row_used[EIA608_SCREEN_ROWS]; // Any data in row?
110 typedef struct eia608_screen eia608_screen;
114 /* Current channel (used to reject packet without channel information) */
118 int i_screen; /* Displayed screen */
119 eia608_screen screen[2];
129 eia608_color_t color;
133 /* Last command pair (used to reject duplicated command) */
141 static void Eia608Init( eia608_t * );
142 static vlc_bool_t Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] );
143 static char *Eia608Text( eia608_t *h, vlc_bool_t b_html );
144 static void Eia608Exit( eia608_t * );
146 /* It will be enough up to 63 B frames, which is far too high for
147 * broadcast environment */
148 #define CC_MAX_REORDER_SIZE (64)
154 block_t *pp_block[CC_MAX_REORDER_SIZE];
162 static subpicture_t *Decode( decoder_t *, block_t ** );
164 /*****************************************************************************
165 * Open: probe the decoder and return score
166 *****************************************************************************
167 * Tries to launch a decoder and return score so that the interface is able
169 *****************************************************************************/
170 static int Open( vlc_object_t *p_this )
172 decoder_t *p_dec = (decoder_t*)p_this;
173 decoder_sys_t *p_sys;
177 switch( p_dec->fmt_in.i_codec )
179 case VLC_FOURCC('c','c','1',' '):
180 i_field = 0; i_channel = 1;
182 case VLC_FOURCC('c','c','2',' '):
183 i_field = 0; i_channel = 2;
185 case VLC_FOURCC('c','c','3',' '):
186 i_field = 1; i_channel = 1;
188 case VLC_FOURCC('c','c','4',' '):
189 i_field = 1; i_channel = 2;
196 p_dec->pf_decode_sub = Decode;
198 /* Allocate the memory needed to store the decoder's structure */
199 p_dec->p_sys = p_sys = malloc( sizeof( *p_sys ) );
202 msg_Err( p_dec, "out of memory" );
207 memset( p_sys, 0, sizeof( *p_sys ) );
210 p_sys->i_field = i_field;
211 p_sys->i_channel = i_channel;
213 Eia608Init( &p_sys->eia608 );
218 /****************************************************************************
219 * Decode: the whole thing
220 ****************************************************************************
222 ****************************************************************************/
223 static void Push( decoder_t *, block_t * );
224 static block_t *Pop( decoder_t * );
225 static subpicture_t *Convert( decoder_t *, block_t * );
227 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
229 if( pp_block && *pp_block )
231 Push( p_dec, *pp_block );
237 block_t *p_block = Pop( p_dec );
241 subpicture_t *p_spu = Convert( p_dec, p_block );
248 /*****************************************************************************
249 * CloseDecoder: clean up the decoder
250 *****************************************************************************/
251 static void Close( vlc_object_t *p_this )
253 decoder_t *p_dec = (decoder_t *)p_this;
254 decoder_sys_t *p_sys = p_dec->p_sys;
257 for( i = 0; i < p_sys->i_block; i++ )
258 block_Release( p_sys->pp_block[i] );
259 Eia608Exit( &p_sys->eia608 );
263 /*****************************************************************************
265 *****************************************************************************/
266 static void Push( decoder_t *p_dec, block_t *p_block )
268 decoder_sys_t *p_sys = p_dec->p_sys;
270 if( p_sys->i_block >= CC_MAX_REORDER_SIZE )
272 msg_Warn( p_dec, "Trashing a CC entry" );
273 memmove( &p_sys->pp_block[0], &p_sys->pp_block[1], sizeof(*p_sys->pp_block) * (CC_MAX_REORDER_SIZE-1) );
276 p_sys->pp_block[p_sys->i_block++] = p_block;
278 static block_t *Pop( decoder_t *p_dec )
280 decoder_sys_t *p_sys = p_dec->p_sys;
284 /* XXX Cc captions data are OUT OF ORDER (because we receive them in the bitstream
285 * order (ie ordered by video picture dts) instead of the display order.
286 * We will simulate a simple IPB buffer scheme
287 * and reorder with pts.
288 * XXX it won't work with H264 which use non out of order B picture or MMCO
291 /* Wait for a P and output all *previous* picture by pts order (for
292 * hierarchical B frames) */
293 if( p_sys->i_block <= 1 ||
294 ( p_sys->pp_block[p_sys->i_block-1]->i_flags & BLOCK_FLAG_TYPE_B ) )
297 p_block = p_sys->pp_block[i_index = 0];
298 if( p_block->i_pts > 0 )
300 for( i = 1; i < p_sys->i_block-1; i++ )
302 if( p_sys->pp_block[i]->i_pts > 0 && p_block->i_pts > 0 &&
303 p_sys->pp_block[i]->i_pts < p_block->i_pts )
304 p_block = p_sys->pp_block[i_index = i];
307 assert( i_index+1 < p_sys->i_block );
308 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 ) );
314 static subpicture_t *Subtitle( decoder_t *p_dec, char *psz_subtitle, char *psz_html, mtime_t i_pts )
316 //decoder_sys_t *p_sys = p_dec->p_sys;
317 subpicture_t *p_spu = NULL;
320 /* We cannot display a subpicture with no date */
323 msg_Warn( p_dec, "subtitle without a date" );
327 EnsureUTF8( psz_subtitle );
329 EnsureUTF8( psz_html );
331 /* Create the subpicture unit */
332 p_spu = p_dec->pf_spu_buffer_new( p_dec );
335 msg_Warn( p_dec, "can't get spu buffer" );
336 free( psz_subtitle );
342 p_spu->b_pausable = VLC_TRUE;
344 /* Create a new subpicture region */
345 memset( &fmt, 0, sizeof(video_format_t) );
346 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
348 fmt.i_width = fmt.i_height = 0;
349 fmt.i_x_offset = fmt.i_y_offset = 0;
350 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
351 if( !p_spu->p_region )
353 msg_Err( p_dec, "cannot allocate SPU region" );
354 free( psz_subtitle );
357 p_dec->pf_spu_buffer_del( p_dec, p_spu );
361 /* Decode and format the subpicture unit */
362 /* Normal text subs, easy markup */
363 p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM;// | SUBPICTURE_ALIGN_LEFT;// | p_sys->i_align;
364 p_spu->i_x = 0; //p_sys->i_align ? 20 : 0;
367 p_spu->p_region->psz_text = psz_subtitle;
368 p_spu->p_region->psz_html = psz_html;
370 p_spu->i_start = i_pts;
371 p_spu->i_stop = i_pts + 10000000; /* 10s max */
372 p_spu->b_ephemer = VLC_TRUE;
373 p_spu->b_absolute = VLC_FALSE;
378 static subpicture_t *Convert( decoder_t *p_dec, block_t *p_block )
380 decoder_sys_t *p_sys = p_dec->p_sys;
381 const int64_t i_pts = p_block->i_pts;
382 vlc_bool_t b_changed = VLC_FALSE;
384 /* TODO do the real decoding here */
385 while( p_block->i_buffer >= 3 )
387 if( p_block->p_buffer[0] == p_sys->i_field )
388 b_changed |= Eia608Parse( &p_sys->eia608, p_sys->i_channel, &p_block->p_buffer[1] );
390 p_block->i_buffer -= 3;
391 p_block->p_buffer += 3;
394 block_Release( p_block );
396 static int64_t i_last = 0;
397 if( b_changed )//&& i_pts - i_last > 100*1000 )
399 char *psz_subtitle = Eia608Text( &p_sys->eia608, VLC_FALSE );
400 char *psz_html = NULL;//Eia608Text( &p_sys->eia608, VLC_TRUE );
402 return Subtitle( p_dec, psz_subtitle, psz_html, i_pts );
408 /*****************************************************************************
410 *****************************************************************************/
411 static const struct {
412 eia608_color_t i_color;
413 eia608_font_t i_font;
416 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 0 },
417 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 0 },
418 { EIA608_COLOR_GREEN, EIA608_FONT_REGULAR, 0 },
419 { EIA608_COLOR_GREEN, EIA608_FONT_UNDERLINE, 0 },
420 { EIA608_COLOR_BLUE, EIA608_FONT_REGULAR, 0 },
421 { EIA608_COLOR_BLUE, EIA608_FONT_UNDERLINE, 0 },
422 { EIA608_COLOR_CYAN, EIA608_FONT_REGULAR, 0 },
423 { EIA608_COLOR_CYAN, EIA608_FONT_UNDERLINE, 0 },
424 { EIA608_COLOR_RED, EIA608_FONT_REGULAR, 0 },
425 { EIA608_COLOR_RED, EIA608_FONT_UNDERLINE, 0 },
426 { EIA608_COLOR_YELLOW, EIA608_FONT_REGULAR, 0 },
427 { EIA608_COLOR_YELLOW, EIA608_FONT_UNDERLINE, 0 },
428 { EIA608_COLOR_MAGENTA, EIA608_FONT_REGULAR, 0 },
429 { EIA608_COLOR_MAGENTA, EIA608_FONT_UNDERLINE, 0 },
430 { EIA608_COLOR_WHITE, EIA608_FONT_ITALICS, 0 },
431 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE_ITALICS, 0 },
433 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 0 },
434 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 0 },
435 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 4 },
436 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 4 },
437 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 8 },
438 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 8 },
439 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 12 },
440 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 12 },
441 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 16 },
442 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 16 },
443 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 20 },
444 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 20 },
445 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 24 },
446 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 24 },
447 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 28 },
448 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 28 } ,
451 #define EIA608_COLOR_DEFAULT EIA608_COLOR_WHITE
453 static void Eia608Cursor( eia608_t *h, int dx )
455 h->cursor.i_column += dx;
456 if( h->cursor.i_column < 0 )
457 h->cursor.i_column = 0;
458 else if( h->cursor.i_column > EIA608_SCREEN_COLUMNS-1 )
459 h->cursor.i_column = EIA608_SCREEN_COLUMNS-1;
461 static void Eia608ClearScreenRowX( eia608_t *h, int i_screen, int i_row, int x )
463 eia608_screen *screen = &h->screen[i_screen];
468 screen->row_used[i_row] = VLC_FALSE;
472 screen->row_used[i_row] = VLC_FALSE;
473 for( i = 0; i < x; i++ )
475 if( screen->characters[i_row][i] != ' ' ||
476 screen->colors[i_row][i] != EIA608_COLOR_DEFAULT ||
477 screen->fonts[i_row][i] != EIA608_FONT_REGULAR )
479 screen->row_used[i_row] = VLC_TRUE;
485 for( ; x < EIA608_SCREEN_COLUMNS+1; x++ )
487 screen->characters[i_row][x] = x < EIA608_SCREEN_COLUMNS ? ' ' : '\0';
488 screen->colors[i_row][x] = EIA608_COLOR_DEFAULT;
489 screen->fonts[i_row][x] = EIA608_FONT_REGULAR;
493 static void Eia608ClearScreenRow( eia608_t *h, int i_screen, int i_row )
495 Eia608ClearScreenRowX( h, i_screen, i_row, 0 );
498 static void Eia608ClearScreen( eia608_t *h, int i_screen )
501 for( i = 0; i < EIA608_SCREEN_ROWS; i++ )
502 Eia608ClearScreenRow( h, i_screen, i );
505 static int Eia608GetWritingScreenIndex( eia608_t *h )
509 case EIA608_MODE_POPUP: // Non displayed screen
510 return 1 - h->i_screen;
512 case EIA608_MODE_ROLLUP_2: // Displayed screen
513 case EIA608_MODE_ROLLUP_3:
514 case EIA608_MODE_ROLLUP_4:
515 case EIA608_MODE_PAINTON:
518 /* It cannot happen, else it is a bug */
524 static void Eia608EraseScreen( eia608_t *h, vlc_bool_t b_displayed )
526 Eia608ClearScreen( h, b_displayed ? h->i_screen : (1-h->i_screen) );
529 static void Eia608Write( eia608_t *h, const uint8_t c )
531 const int i_row = h->cursor.i_row;
532 const int i_column = h->cursor.i_column;
533 eia608_screen *screen;
535 if( h->mode == EIA608_MODE_TEXT )
538 screen = &h->screen[Eia608GetWritingScreenIndex( h )];
540 screen->characters[i_row][i_column] = c;
541 screen->colors[i_row][i_column] = h->color;
542 screen->fonts[i_row][i_column] = h->font;
543 screen->row_used[i_row] = VLC_TRUE;
544 Eia608Cursor( h, 1 );
546 static void Eia608Erase( eia608_t *h )
548 const int i_row = h->cursor.i_row;
549 const int i_column = h->cursor.i_column - 1;
550 eia608_screen *screen;
552 if( h->mode == EIA608_MODE_TEXT )
557 screen = &h->screen[Eia608GetWritingScreenIndex( h )];
559 /* FIXME do we need to reset row_used/colors/font ? */
560 screen->characters[i_row][i_column] = ' ';
561 Eia608Cursor( h, -1 );
563 static void Eia608EraseToEndOfRow( eia608_t *h )
565 if( h->mode == EIA608_MODE_TEXT )
568 Eia608ClearScreenRowX( h, Eia608GetWritingScreenIndex( h ), h->cursor.i_row, h->cursor.i_column );
571 static void Eia608RollUp( eia608_t *h )
573 const int i_screen = Eia608GetWritingScreenIndex( h );
574 eia608_screen *screen = &h->screen[i_screen];
580 if( h->mode == EIA608_MODE_ROLLUP_2 )
582 else if( h->mode == EIA608_MODE_ROLLUP_3 )
584 else if( h->mode == EIA608_MODE_ROLLUP_4 )
589 /* Reset the cursor */
590 h->cursor.i_column = 0;
592 /* Erase lines above our window */
593 for( i = 0; i < h->cursor.i_row - keep_lines; i++ )
594 Eia608ClearScreenRow( h, i_screen, i );
597 for( i = 0; i < keep_lines-1; i++ )
599 const int i_row = h->cursor.i_row - keep_lines + i + 1;
602 assert( i_row+1 < EIA608_SCREEN_ROWS );
603 memcpy( screen->characters[i_row], screen->characters[i_row+1], sizeof(*screen->characters) );
604 memcpy( screen->colors[i_row], screen->colors[i_row+1], sizeof(*screen->colors) );
605 memcpy( screen->fonts[i_row], screen->fonts[i_row+1], sizeof(*screen->fonts) );
606 screen->row_used[i_row] = screen->row_used[i_row+1];
608 /* Reset current row */
609 Eia608ClearScreenRow( h, i_screen, h->cursor.i_row );
611 static void Eia608ParseChannel( eia608_t *h, uint8_t d1 )
615 else if( d1 == 0x1c )
617 else if( ( d1 >= 0x01 && d1 <= 0x0f ) || d1 == 0x15 )
619 else if( d1 == 0x1d )
622 static vlc_bool_t Eia608ParseTextAttribute( eia608_t *h, uint8_t d2 )
624 const int i_index = d2 - 0x20;
625 assert( d2 >= 0x20 && d2 <= 0x2f );
627 h->color = pac2_attribs[i_index].i_color;
628 h->font = pac2_attribs[i_index].i_font;
629 Eia608Cursor( h, 1 );
633 static vlc_bool_t Eia608ParseSingle( eia608_t *h, const uint8_t dx )
635 assert( dx >= 0x20 );
636 Eia608Write( h, dx );
639 static vlc_bool_t Eia608ParseDouble( eia608_t *h, uint8_t d2 )
641 assert( d2 >= 0x30 && d2 <= 0x3f );
642 Eia608Write( h, d2 + 0x50 ); /* We use charaters 0x80...0x8f */
645 static vlc_bool_t Eia608ParseExtended( eia608_t *h, uint8_t d1, uint8_t d2 )
647 assert( d2 >= 0x20 && d2 <= 0x3f );
648 assert( d1 == 0x12 || d1 == 0x13 );
650 d2 += 0x70; /* We use charaters 0x90-0xaf */
652 d2 += 0x90; /* We use charaters 0xb0-0xcf */
654 /* The extended characters replace the previous one with a more
656 Eia608Cursor( h, -1 );
657 Eia608Write( h, d2 );
660 static vlc_bool_t Eia608ParseCommand0x14( eia608_t *h, uint8_t d2 )
662 vlc_bool_t b_changed = VLC_FALSE;
666 case 0x20: /* Resume caption loading */
667 h->mode = EIA608_MODE_POPUP;
669 case 0x21: /* Backspace */
671 b_changed = VLC_TRUE;
673 case 0x22: /* Reserved */
676 case 0x24: /* Delete to end of row */
677 Eia608EraseToEndOfRow( h );
679 case 0x25: /* Rollup 2 */
680 case 0x26: /* Rollup 3 */
681 case 0x27: /* Rollup 4 */
682 if( h->mode == EIA608_MODE_POPUP || h->mode == EIA608_MODE_PAINTON )
684 Eia608EraseScreen( h, VLC_TRUE );
685 Eia608EraseScreen( h, VLC_FALSE );
686 b_changed = VLC_TRUE;
690 h->mode = EIA608_MODE_ROLLUP_2;
691 else if( d2 == 0x26 )
692 h->mode = EIA608_MODE_ROLLUP_3;
694 h->mode = EIA608_MODE_ROLLUP_4;
696 h->cursor.i_column = 0;
697 h->cursor.i_row = h->i_row_rollup;
699 case 0x28: /* Flash on */
702 case 0x29: /* Resume direct captionning */
703 h->mode = EIA608_MODE_PAINTON;
705 case 0x2a: /* Text restart */
709 case 0x2b: /* Resume text display */
710 h->mode = EIA608_MODE_TEXT;
713 case 0x2c: /* Erase displayed memory */
714 Eia608EraseScreen( h, VLC_TRUE );
715 b_changed = VLC_TRUE;
717 case 0x2d: /* Carriage return */
719 b_changed = VLC_TRUE;
721 case 0x2e: /* Erase non displayed memory */
722 Eia608EraseScreen( h, VLC_FALSE );
724 case 0x2f: /* End of caption (flip screen if not paint on) */
725 if( h->mode != EIA608_MODE_PAINTON )
726 h->i_screen = 1 - h->i_screen;
727 h->mode = EIA608_MODE_POPUP;
728 h->cursor.i_column = 0;
730 h->color = EIA608_COLOR_DEFAULT;
731 h->font = EIA608_FONT_REGULAR;
732 b_changed = VLC_TRUE;
737 static vlc_bool_t Eia608ParseCommand0x17( eia608_t *h, uint8_t d2 )
741 case 0x21: /* Tab offset 1 */
742 Eia608Cursor( h, 1 );
744 case 0x22: /* Tab offset 2 */
745 Eia608Cursor( h, 2 );
747 case 0x23: /* Tab offset 3 */
748 Eia608Cursor( h, 3 );
753 static vlc_bool_t Eia608ParsePac( eia608_t *h, uint8_t d1, uint8_t d2 )
755 static const int pi_row[] = {
756 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
758 const int i_row_index = ( (d1<<1) & 0x0e) | ( (d2>>5) & 0x01 );
760 assert( d2 >= 0x40 && d2 <= 0x7f );
762 if( pi_row[i_row_index] <= 0 )
766 if( h->mode != EIA608_MODE_TEXT )
767 h->cursor.i_row = pi_row[i_row_index] - 1;
768 h->i_row_rollup = pi_row[i_row_index] - 1;
772 else if( d2 >= 0x40 )
774 h->cursor.i_column = pac2_attribs[d2].i_column;
778 static vlc_bool_t Eia608ParseData( eia608_t *h, uint8_t d1, uint8_t d2 )
780 vlc_bool_t b_changed = VLC_FALSE;
782 if( d1 >= 0x18 && d1 <= 0x1f )
785 #define ON( d2min, d2max, cmd ) do { if( d2 >= d2min && d2 <= d2max ) b_changed = cmd; } while(0)
789 ON( 0x20, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
790 ON( 0x30, 0x3f, Eia608ParseDouble( h, d2 ) );
792 case 0x12: case 0x13:
793 ON( 0x20, 0x3f, Eia608ParseExtended( h, d1, d2 ) );
795 case 0x14: case 0x15:
796 ON( 0x20, 0x2f, Eia608ParseCommand0x14( h, d2 ) );
799 ON( 0x21, 0x22, Eia608ParseCommand0x17( h, d2 ) );
800 ON( 0x2e, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
804 ON( 0x40, 0x5f, Eia608ParsePac( h, d1, d2 ) );
805 else if( d1 >= 0x11 && d1 <= 0x17 )
806 ON( 0x40, 0x7f, Eia608ParsePac( h, d1, d2 ) );
810 b_changed = Eia608ParseSingle( h, d1 );
812 b_changed |= Eia608ParseSingle( h, d2 );
817 static void Eia608TextUtf8( char *psz_utf8, uint8_t c ) // Returns number of bytes used
819 #define E1(c,u) { c, { u, '\0' } }
820 #define E2(c,u1,u2) { c, { u1, u2, '\0' } }
821 #define E3(c,u1,u2,u3) { c, { u1, u2, u3, '\0' } }
822 static const struct {
826 // Regular line-21 character set, mostly ASCII except these exceptions
827 E2( 0x2a, 0xc3,0xa1), // lowercase a, acute accent
828 E2( 0x5c, 0xc3,0xa9), // lowercase e, acute accent
829 E2( 0x5e, 0xc3,0xad), // lowercase i, acute accent
830 E2( 0x5f, 0xc3,0xb3), // lowercase o, acute accent
831 E2( 0x60, 0xc3,0xba), // lowercase u, acute accent
832 E2( 0x7b, 0xc3,0xa7), // lowercase c with cedilla
833 E2( 0x7c, 0xc3,0xb7), // division symbol
834 E2( 0x7d, 0xc3,0x91), // uppercase N tilde
835 E2( 0x7e, 0xc3,0xb1), // lowercase n tilde
836 // THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
837 // THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F
838 E2( 0x80, 0xc2,0xae), // Registered symbol (R)
839 E2( 0x81, 0xc2,0xb0), // degree sign
840 E2( 0x82, 0xc2,0xbd), // 1/2 symbol
841 E2( 0x83, 0xc2,0xbf), // Inverted (open) question mark
842 E3( 0x84, 0xe2,0x84,0xa2), // Trademark symbol (TM)
843 E2( 0x85, 0xc2,0xa2), // Cents symbol
844 E2( 0x86, 0xc2,0xa3), // Pounds sterling
845 E3( 0x87, 0xe2,0x99,0xaa), // Music note
846 E2( 0x88, 0xc3,0xa0), // lowercase a, grave accent
847 E1( 0x89, 0x20), // transparent space, we make it regular
848 E2( 0x8a, 0xc3,0xa8), // lowercase e, grave accent
849 E2( 0x8b, 0xc3,0xa2), // lowercase a, circumflex accent
850 E2( 0x8c, 0xc3,0xaa), // lowercase e, circumflex accent
851 E2( 0x8d, 0xc3,0xae), // lowercase i, circumflex accent
852 E2( 0x8e, 0xc3,0xb4), // lowercase o, circumflex accent
853 E2( 0x8f, 0xc3,0xbb), // lowercase u, circumflex accent
854 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
855 // THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F
856 E2( 0x90, 0xc3,0x81), // capital letter A with acute
857 E2( 0x91, 0xc3,0x89), // capital letter E with acute
858 E2( 0x92, 0xc3,0x93), // capital letter O with acute
859 E2( 0x93, 0xc3,0x9a), // capital letter U with acute
860 E2( 0x94, 0xc3,0x9c), // capital letter U with diaresis
861 E2( 0x95, 0xc3,0xbc), // lowercase letter U with diaeresis
862 E1( 0x96, 0x27), // apostrophe
863 E2( 0x97, 0xc1,0xa1), // inverted exclamation mark
864 E1( 0x98, 0x2a), // asterisk
865 E1( 0x99, 0x27), // apostrophe (yes, duped). See CCADI source code.
866 E1( 0x9a, 0x2d), // hyphen-minus
867 E2( 0x9b, 0xc2,0xa9), // copyright sign
868 E3( 0x9c, 0xe2,0x84,0xa0), // Service mark
869 E1( 0x9d, 0x2e), // Full stop (.)
870 E1( 0x9e, 0x22), // Quoatation mark
871 E1( 0x9f, 0x22), // Quoatation mark
872 E2( 0xa0, 0xc3,0x80), // uppercase A, grave accent
873 E2( 0xa1, 0xc3,0x82), // uppercase A, circumflex
874 E2( 0xa2, 0xc3,0x87), // uppercase C with cedilla
875 E2( 0xa3, 0xc3,0x88), // uppercase E, grave accent
876 E2( 0xa4, 0xc3,0x8a), // uppercase E, circumflex
877 E2( 0xa5, 0xc3,0x8b), // capital letter E with diaresis
878 E2( 0xa6, 0xc3,0xab), // lowercase letter e with diaresis
879 E2( 0xa7, 0xc3,0x8e), // uppercase I, circumflex
880 E2( 0xa8, 0xc3,0x8f), // uppercase I, with diaresis
881 E2( 0xa9, 0xc3,0xaf), // lowercase i, with diaresis
882 E2( 0xaa, 0xc3,0x94), // uppercase O, circumflex
883 E2( 0xab, 0xc3,0x99), // uppercase U, grave accent
884 E2( 0xac, 0xc3,0xb9), // lowercase u, grave accent
885 E2( 0xad, 0xc3,0x9b), // uppercase U, circumflex
886 E2( 0xae, 0xc2,0xab), // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
887 E2( 0xaf, 0xc2,0xbb), // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
888 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
889 // THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F
890 E2( 0xb0, 0xc3,0x83), // Uppercase A, tilde
891 E2( 0xb1, 0xc3,0xa3), // Lowercase a, tilde
892 E2( 0xb2, 0xc3,0x8d), // Uppercase I, acute accent
893 E2( 0xb3, 0xc3,0x8c), // Uppercase I, grave accent
894 E2( 0xb4, 0xc3,0xac), // Lowercase i, grave accent
895 E2( 0xb5, 0xc3,0x92), // Uppercase O, grave accent
896 E2( 0xb6, 0xc3,0xb2), // Lowercase o, grave accent
897 E2( 0xb7, 0xc3,0x95), // Uppercase O, tilde
898 E2( 0xb8, 0xc3,0xb5), // Lowercase o, tilde
899 E1( 0xb9, 0x7b), // Open curly brace
900 E1( 0xba, 0x7d), // Closing curly brace
901 E1( 0xbb, 0x5c), // Backslash
902 E1( 0xbc, 0x5e), // Caret
903 E1( 0xbd, 0x5f), // Underscore
904 E2( 0xbe, 0xc2,0xa6), // Pipe (broken bar)
905 E1( 0xbf, 0x7e), // Tilde (utf8 code unsure)
906 E2( 0xc0, 0xc3,0x84), // Uppercase A, umlaut
907 E2( 0xc1, 0xc3,0xa4), // Lowercase A, umlaut
908 E2( 0xc2, 0xc3,0x96), // Uppercase O, umlaut
909 E2( 0xc3, 0xc3,0xb6), // Lowercase o, umlaut
910 E2( 0xc4, 0xc3,0x9f), // Esszett (sharp S)
911 E2( 0xc5, 0xc2,0xa5), // Yen symbol
912 E2( 0xc6, 0xc2,0xa4), // Currency symbol
913 E1( 0xc7, 0x7c), // Vertical bar
914 E2( 0xc8, 0xc3,0x85), // Uppercase A, ring
915 E2( 0xc9, 0xc3,0xa5), // Lowercase A, ring
916 E2( 0xca, 0xc3,0x98), // Uppercase O, slash
917 E2( 0xcb, 0xc3,0xb8), // Lowercase o, slash
918 E3( 0xcc, 0xe2,0x8c,0x9c), // Upper left corner
919 E3( 0xcd, 0xe2,0x8c,0x9d), // Upper right corner
920 E3( 0xce, 0xe2,0x8c,0x9e), // Lower left corner
921 E3( 0xcf, 0xe2,0x8c,0x9f), // Lower right corner
929 static const int i_c2utf8 = sizeof(c2utf8)/sizeof(*c2utf8);
932 for( i = 0; i < i_c2utf8; i++ )
934 if( c2utf8[i].c == c )
939 psz_utf8[0] = c < 0x80 ? c : '?'; /* Normal : Unsupported */
944 strcpy( psz_utf8, c2utf8[i].utf8 );
948 static void Eia608Strlcat( char *d, const char *s, int i_max )
951 strncat( d, s, i_max-1 - strnlen(d, i_max-1));
956 static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_text_max, int i_row, vlc_bool_t b_html )
958 const uint8_t *p_char = screen->characters[i_row];
959 const eia608_color_t *p_color = screen->colors[i_row];
960 const eia608_font_t *p_font = screen->fonts[i_row];
964 eia608_color_t last_color = EIA608_COLOR_DEFAULT;
965 vlc_bool_t b_last_italics = VLC_FALSE;
966 vlc_bool_t b_last_underline = VLC_FALSE;
968 /* Search the start */
970 while( i_start < EIA608_SCREEN_COLUMNS-1 && p_char[i_start] == ' ' )
974 i_end = EIA608_SCREEN_COLUMNS-1;
975 while( i_end > i_start && p_char[i_end] == ' ' )
979 #define CAT(t) Eia608Strlcat( psz_text, t, i_text_max )
980 for( x = i_start; x <= i_end; x++ )
982 eia608_color_t color = p_color[x];
983 vlc_bool_t b_italics = p_font[x] & EIA608_FONT_ITALICS;
984 vlc_bool_t b_underline = p_font[x] & EIA608_FONT_UNDERLINE;
990 vlc_bool_t b_close_color, b_close_italics, b_close_underline;
992 /* We create the tags font / i / u in that orders */
993 b_close_color = color != last_color && last_color != EIA608_COLOR_DEFAULT;
994 b_close_italics = !b_italics && b_last_italics;
995 b_close_underline = !b_underline && b_last_underline;
997 /* Be sure to create valid html */
998 b_close_italics |= b_last_italics && b_close_color;
999 b_close_underline = b_last_underline && ( b_close_italics || b_close_color );
1001 if( b_close_underline )
1003 if( b_close_italics )
1008 if( color != EIA608_COLOR_DEFAULT && color != last_color)
1010 static const char *ppsz_color[] = {
1016 "#ffff00", // yellow
1017 "#ff00ff", // magenta
1018 "#ffffff", // user defined XXX we use white
1020 CAT( "<font color=" );
1021 CAT( ppsz_color[color] );
1024 if( ( b_close_italics && b_italics ) || ( b_italics && !b_last_italics ) )
1026 if( ( b_close_underline && b_underline ) || ( b_underline && !b_last_underline ) )
1031 Eia608TextUtf8( utf8, p_char[x] );
1035 b_last_underline = b_underline;
1036 b_last_italics = b_italics;
1041 if( b_last_underline )
1043 if( b_last_italics )
1045 if( last_color != EIA608_COLOR_DEFAULT )
1052 static void Eia608Init( eia608_t *h )
1054 memset( h, 0, sizeof(*h) );
1060 Eia608ClearScreen( h, 0 );
1061 Eia608ClearScreen( h, 1 );
1063 /* Cursor for writing text */
1064 h->cursor.i_column = 0;
1065 h->cursor.i_row = 0;
1069 h->mode = EIA608_MODE_POPUP;
1070 h->color = EIA608_COLOR_DEFAULT;
1071 h->font = EIA608_FONT_REGULAR;
1072 h->i_row_rollup = EIA608_SCREEN_ROWS-1;
1074 static vlc_bool_t Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] )
1076 const uint8_t d1 = data[0] & 0x7f; /* Removed parity bit TODO we might want to check them */
1077 const uint8_t d2 = data[1] & 0x7f;
1078 vlc_bool_t b_screen_changed = VLC_FALSE;
1080 if( d1 == 0 && d2 == 0 )
1081 return VLC_FALSE; /* Ignore padding */
1083 Eia608ParseChannel( h, d1 );
1084 if( h->i_channel != i_channel_selected )
1086 //fprintf( stderr, "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC %x %x\n", data[0], data[1] );
1091 d1 != h->last.d1 || d2 != h->last.d2 ) /* Command codes can be repeated */
1092 b_screen_changed = Eia608ParseData( h, d1,d2 );
1097 else if( ( d1 >= 0x01 && d1 <= 0x0E ) || d1 == 0x0F )
1099 /* XDS block / End of XDS block */
1101 return b_screen_changed;
1104 static char *Eia608Text( eia608_t *h, vlc_bool_t b_html )
1106 const int i_size = EIA608_SCREEN_ROWS * 3 * EIA608_SCREEN_COLUMNS+1;
1107 struct eia608_screen *screen = &h->screen[h->i_screen];
1108 vlc_bool_t b_first = VLC_TRUE;
1112 /* We allocate a buffer big enough for normal case */
1113 psz = malloc( i_size );
1116 Eia608Strlcat( psz, "<text>", i_size );
1117 for( i = 0; i < EIA608_SCREEN_ROWS; i++ )
1119 if( !screen->row_used[i] )
1123 Eia608Strlcat( psz, b_html ? "<br />" : "\n", i_size );
1124 b_first = VLC_FALSE;
1126 Eia608TextLine( screen, psz, i_size, i, b_html );
1129 Eia608Strlcat( psz, "</text>", i_size );
1133 static void Eia608Exit( eia608_t *h )