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>
43 #include <vlc_codec.h>
44 #include <vlc_input.h>
47 #include <vlc_filter.h>
48 #include <vlc_image.h>
49 #include <vlc_charset.h>
50 #include <vlc_stream.h>
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Open ( vlc_object_t
* );
61 static void Close( vlc_object_t
* );
64 set_shortname( N_("CC 608/708"))
65 set_description( N_("Closed Captions decoder") )
66 set_capability( "decoder", 50 )
67 set_callbacks( Open
, Close
)
70 /*****************************************************************************
72 *****************************************************************************/
75 EIA608_MODE_POPUP
= 0,
76 EIA608_MODE_ROLLUP_2
= 1,
77 EIA608_MODE_ROLLUP_3
= 2,
78 EIA608_MODE_ROLLUP_4
= 3,
79 EIA608_MODE_PAINTON
= 4,
85 EIA608_COLOR_WHITE
= 0,
86 EIA608_COLOR_GREEN
= 1,
87 EIA608_COLOR_BLUE
= 2,
88 EIA608_COLOR_CYAN
= 3,
90 EIA608_COLOR_YELLOW
= 5,
91 EIA608_COLOR_MAGENTA
= 6,
92 EIA608_COLOR_USERDEFINED
= 7
97 EIA608_FONT_REGULAR
= 0x00,
98 EIA608_FONT_ITALICS
= 0x01,
99 EIA608_FONT_UNDERLINE
= 0x02,
100 EIA608_FONT_UNDERLINE_ITALICS
= EIA608_FONT_UNDERLINE
| EIA608_FONT_ITALICS
103 #define EIA608_SCREEN_ROWS 15
104 #define EIA608_SCREEN_COLUMNS 32
106 struct eia608_screen
// A CC buffer
108 uint8_t characters
[EIA608_SCREEN_ROWS
][EIA608_SCREEN_COLUMNS
+1];
109 eia608_color_t colors
[EIA608_SCREEN_ROWS
][EIA608_SCREEN_COLUMNS
+1];
110 eia608_font_t fonts
[EIA608_SCREEN_ROWS
][EIA608_SCREEN_COLUMNS
+1]; // Extra char at the end for a 0
111 int row_used
[EIA608_SCREEN_ROWS
]; // Any data in row?
113 typedef struct eia608_screen eia608_screen
;
117 /* Current channel (used to reject packet without channel information) */
121 int i_screen
; /* Displayed screen */
122 eia608_screen screen
[2];
132 eia608_color_t color
;
136 /* Last command pair (used to reject duplicated command) */
144 static void Eia608Init( eia608_t
* );
145 static bool Eia608Parse( eia608_t
*h
, int i_channel_selected
, const uint8_t data
[2] );
146 static char *Eia608Text( eia608_t
*h
, bool b_html
);
147 static void Eia608Exit( eia608_t
* );
149 /* It will be enough up to 63 B frames, which is far too high for
150 * broadcast environment */
151 #define CC_MAX_REORDER_SIZE (64)
157 block_t
*pp_block
[CC_MAX_REORDER_SIZE
];
165 static subpicture_t
*Decode( decoder_t
*, block_t
** );
167 /*****************************************************************************
168 * Open: probe the decoder and return score
169 *****************************************************************************
170 * Tries to launch a decoder and return score so that the interface is able
172 *****************************************************************************/
173 static int Open( vlc_object_t
*p_this
)
175 decoder_t
*p_dec
= (decoder_t
*)p_this
;
176 decoder_sys_t
*p_sys
;
180 switch( p_dec
->fmt_in
.i_codec
)
182 case VLC_FOURCC('c','c','1',' '):
183 i_field
= 0; i_channel
= 1;
185 case VLC_FOURCC('c','c','2',' '):
186 i_field
= 0; i_channel
= 2;
188 case VLC_FOURCC('c','c','3',' '):
189 i_field
= 1; i_channel
= 1;
191 case VLC_FOURCC('c','c','4',' '):
192 i_field
= 1; i_channel
= 2;
199 p_dec
->pf_decode_sub
= Decode
;
201 /* Allocate the memory needed to store the decoder's structure */
202 p_dec
->p_sys
= p_sys
= calloc( 1, sizeof( *p_sys
) );
207 p_sys
->i_field
= i_field
;
208 p_sys
->i_channel
= i_channel
;
210 Eia608Init( &p_sys
->eia608
);
215 /****************************************************************************
216 * Decode: the whole thing
217 ****************************************************************************
219 ****************************************************************************/
220 static void Push( decoder_t
*, block_t
* );
221 static block_t
*Pop( decoder_t
* );
222 static subpicture_t
*Convert( decoder_t
*, block_t
* );
224 static subpicture_t
*Decode( decoder_t
*p_dec
, block_t
**pp_block
)
226 if( pp_block
&& *pp_block
)
228 Push( p_dec
, *pp_block
);
234 block_t
*p_block
= Pop( p_dec
);
238 subpicture_t
*p_spu
= Convert( p_dec
, p_block
);
245 /*****************************************************************************
246 * CloseDecoder: clean up the decoder
247 *****************************************************************************/
248 static void Close( vlc_object_t
*p_this
)
250 decoder_t
*p_dec
= (decoder_t
*)p_this
;
251 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
254 for( i
= 0; i
< p_sys
->i_block
; i
++ )
255 block_Release( p_sys
->pp_block
[i
] );
256 Eia608Exit( &p_sys
->eia608
);
260 /*****************************************************************************
262 *****************************************************************************/
263 static void Push( decoder_t
*p_dec
, block_t
*p_block
)
265 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
267 if( p_sys
->i_block
>= CC_MAX_REORDER_SIZE
)
269 msg_Warn( p_dec
, "Trashing a CC entry" );
270 memmove( &p_sys
->pp_block
[0], &p_sys
->pp_block
[1], sizeof(*p_sys
->pp_block
) * (CC_MAX_REORDER_SIZE
-1) );
273 p_sys
->pp_block
[p_sys
->i_block
++] = p_block
;
275 static block_t
*Pop( decoder_t
*p_dec
)
277 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
281 /* XXX Cc captions data are OUT OF ORDER (because we receive them in the bitstream
282 * order (ie ordered by video picture dts) instead of the display order.
283 * We will simulate a simple IPB buffer scheme
284 * and reorder with pts.
285 * XXX it won't work with H264 which use non out of order B picture or MMCO
288 /* Wait for a P and output all *previous* picture by pts order (for
289 * hierarchical B frames) */
290 if( p_sys
->i_block
<= 1 ||
291 ( p_sys
->pp_block
[p_sys
->i_block
-1]->i_flags
& BLOCK_FLAG_TYPE_B
) )
294 p_block
= p_sys
->pp_block
[i_index
= 0];
295 if( p_block
->i_pts
> 0 )
297 for( i
= 1; i
< p_sys
->i_block
-1; i
++ )
299 if( p_sys
->pp_block
[i
]->i_pts
> 0 && p_block
->i_pts
> 0 &&
300 p_sys
->pp_block
[i
]->i_pts
< p_block
->i_pts
)
301 p_block
= p_sys
->pp_block
[i_index
= i
];
304 assert( i_index
+1 < p_sys
->i_block
);
305 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 ) );
311 static subpicture_t
*Subtitle( decoder_t
*p_dec
, char *psz_subtitle
, char *psz_html
, mtime_t i_pts
)
313 //decoder_sys_t *p_sys = p_dec->p_sys;
314 subpicture_t
*p_spu
= NULL
;
317 /* We cannot display a subpicture with no date */
320 msg_Warn( p_dec
, "subtitle without a date" );
324 EnsureUTF8( psz_subtitle
);
326 EnsureUTF8( psz_html
);
328 /* Create the subpicture unit */
329 p_spu
= decoder_NewSubpicture( p_dec
);
332 msg_Warn( p_dec
, "can't get spu buffer" );
333 free( psz_subtitle
);
338 /* Create a new subpicture region */
339 memset( &fmt
, 0, sizeof(video_format_t
) );
340 fmt
.i_chroma
= VLC_FOURCC('T','E','X','T');
342 fmt
.i_width
= fmt
.i_height
= 0;
343 fmt
.i_x_offset
= fmt
.i_y_offset
= 0;
344 p_spu
->p_region
= subpicture_region_New( &fmt
);
345 if( !p_spu
->p_region
)
347 msg_Err( p_dec
, "cannot allocate SPU region" );
348 free( psz_subtitle
);
350 decoder_DeleteSubpicture( p_dec
, p_spu
);
354 /* Decode and format the subpicture unit */
355 /* Normal text subs, easy markup */
356 p_spu
->p_region
->i_align
= SUBPICTURE_ALIGN_BOTTOM
;// | SUBPICTURE_ALIGN_LEFT;// | p_sys->i_align;
357 p_spu
->p_region
->i_x
= 0; //p_sys->i_align ? 20 : 0;
358 p_spu
->p_region
->i_y
= 10;
360 p_spu
->p_region
->psz_text
= psz_subtitle
;
361 p_spu
->p_region
->psz_html
= psz_html
;
363 p_spu
->i_start
= i_pts
;
364 p_spu
->i_stop
= i_pts
+ 10000000; /* 10s max */
365 p_spu
->b_ephemer
= true;
366 p_spu
->b_absolute
= false;
371 static subpicture_t
*Convert( decoder_t
*p_dec
, block_t
*p_block
)
375 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
376 const int64_t i_pts
= p_block
->i_pts
;
377 bool b_changed
= false;
379 /* TODO do the real decoding here */
380 while( p_block
->i_buffer
>= 3 )
382 if( p_block
->p_buffer
[0] == p_sys
->i_field
)
383 b_changed
|= Eia608Parse( &p_sys
->eia608
, p_sys
->i_channel
, &p_block
->p_buffer
[1] );
385 p_block
->i_buffer
-= 3;
386 p_block
->p_buffer
+= 3;
389 block_Release( p_block
);
393 char *psz_subtitle
= Eia608Text( &p_sys
->eia608
, false );
394 char *psz_html
= NULL
;//Eia608Text( &p_sys->eia608, true );
395 return Subtitle( p_dec
, psz_subtitle
, psz_html
, i_pts
);
401 /*****************************************************************************
403 *****************************************************************************/
404 static const struct {
405 eia608_color_t i_color
;
406 eia608_font_t i_font
;
409 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 0 },
410 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 0 },
411 { EIA608_COLOR_GREEN
, EIA608_FONT_REGULAR
, 0 },
412 { EIA608_COLOR_GREEN
, EIA608_FONT_UNDERLINE
, 0 },
413 { EIA608_COLOR_BLUE
, EIA608_FONT_REGULAR
, 0 },
414 { EIA608_COLOR_BLUE
, EIA608_FONT_UNDERLINE
, 0 },
415 { EIA608_COLOR_CYAN
, EIA608_FONT_REGULAR
, 0 },
416 { EIA608_COLOR_CYAN
, EIA608_FONT_UNDERLINE
, 0 },
417 { EIA608_COLOR_RED
, EIA608_FONT_REGULAR
, 0 },
418 { EIA608_COLOR_RED
, EIA608_FONT_UNDERLINE
, 0 },
419 { EIA608_COLOR_YELLOW
, EIA608_FONT_REGULAR
, 0 },
420 { EIA608_COLOR_YELLOW
, EIA608_FONT_UNDERLINE
, 0 },
421 { EIA608_COLOR_MAGENTA
, EIA608_FONT_REGULAR
, 0 },
422 { EIA608_COLOR_MAGENTA
, EIA608_FONT_UNDERLINE
, 0 },
423 { EIA608_COLOR_WHITE
, EIA608_FONT_ITALICS
, 0 },
424 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE_ITALICS
, 0 },
426 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 0 },
427 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 0 },
428 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 4 },
429 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 4 },
430 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 8 },
431 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 8 },
432 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 12 },
433 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 12 },
434 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 16 },
435 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 16 },
436 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 20 },
437 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 20 },
438 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 24 },
439 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 24 },
440 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 28 },
441 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 28 } ,
444 #define EIA608_COLOR_DEFAULT EIA608_COLOR_WHITE
446 static void Eia608Cursor( eia608_t
*h
, int dx
)
448 h
->cursor
.i_column
+= dx
;
449 if( h
->cursor
.i_column
< 0 )
450 h
->cursor
.i_column
= 0;
451 else if( h
->cursor
.i_column
> EIA608_SCREEN_COLUMNS
-1 )
452 h
->cursor
.i_column
= EIA608_SCREEN_COLUMNS
-1;
454 static void Eia608ClearScreenRowX( eia608_t
*h
, int i_screen
, int i_row
, int x
)
456 eia608_screen
*screen
= &h
->screen
[i_screen
];
461 screen
->row_used
[i_row
] = false;
465 screen
->row_used
[i_row
] = false;
466 for( i
= 0; i
< x
; i
++ )
468 if( screen
->characters
[i_row
][i
] != ' ' ||
469 screen
->colors
[i_row
][i
] != EIA608_COLOR_DEFAULT
||
470 screen
->fonts
[i_row
][i
] != EIA608_FONT_REGULAR
)
472 screen
->row_used
[i_row
] = true;
478 for( ; x
< EIA608_SCREEN_COLUMNS
+1; x
++ )
480 screen
->characters
[i_row
][x
] = x
< EIA608_SCREEN_COLUMNS
? ' ' : '\0';
481 screen
->colors
[i_row
][x
] = EIA608_COLOR_DEFAULT
;
482 screen
->fonts
[i_row
][x
] = EIA608_FONT_REGULAR
;
486 static void Eia608ClearScreenRow( eia608_t
*h
, int i_screen
, int i_row
)
488 Eia608ClearScreenRowX( h
, i_screen
, i_row
, 0 );
491 static void Eia608ClearScreen( eia608_t
*h
, int i_screen
)
494 for( i
= 0; i
< EIA608_SCREEN_ROWS
; i
++ )
495 Eia608ClearScreenRow( h
, i_screen
, i
);
498 static int Eia608GetWritingScreenIndex( eia608_t
*h
)
502 case EIA608_MODE_POPUP
: // Non displayed screen
503 return 1 - h
->i_screen
;
505 case EIA608_MODE_ROLLUP_2
: // Displayed screen
506 case EIA608_MODE_ROLLUP_3
:
507 case EIA608_MODE_ROLLUP_4
:
508 case EIA608_MODE_PAINTON
:
511 /* It cannot happen, else it is a bug */
517 static void Eia608EraseScreen( eia608_t
*h
, bool b_displayed
)
519 Eia608ClearScreen( h
, b_displayed
? h
->i_screen
: (1-h
->i_screen
) );
522 static void Eia608Write( eia608_t
*h
, const uint8_t c
)
524 const int i_row
= h
->cursor
.i_row
;
525 const int i_column
= h
->cursor
.i_column
;
526 eia608_screen
*screen
;
528 if( h
->mode
== EIA608_MODE_TEXT
)
531 screen
= &h
->screen
[Eia608GetWritingScreenIndex( h
)];
533 screen
->characters
[i_row
][i_column
] = c
;
534 screen
->colors
[i_row
][i_column
] = h
->color
;
535 screen
->fonts
[i_row
][i_column
] = h
->font
;
536 screen
->row_used
[i_row
] = true;
537 Eia608Cursor( h
, 1 );
539 static void Eia608Erase( eia608_t
*h
)
541 const int i_row
= h
->cursor
.i_row
;
542 const int i_column
= h
->cursor
.i_column
- 1;
543 eia608_screen
*screen
;
545 if( h
->mode
== EIA608_MODE_TEXT
)
550 screen
= &h
->screen
[Eia608GetWritingScreenIndex( h
)];
552 /* FIXME do we need to reset row_used/colors/font ? */
553 screen
->characters
[i_row
][i_column
] = ' ';
554 Eia608Cursor( h
, -1 );
556 static void Eia608EraseToEndOfRow( eia608_t
*h
)
558 if( h
->mode
== EIA608_MODE_TEXT
)
561 Eia608ClearScreenRowX( h
, Eia608GetWritingScreenIndex( h
), h
->cursor
.i_row
, h
->cursor
.i_column
);
564 static void Eia608RollUp( eia608_t
*h
)
566 const int i_screen
= Eia608GetWritingScreenIndex( h
);
567 eia608_screen
*screen
= &h
->screen
[i_screen
];
573 if( h
->mode
== EIA608_MODE_ROLLUP_2
)
575 else if( h
->mode
== EIA608_MODE_ROLLUP_3
)
577 else if( h
->mode
== EIA608_MODE_ROLLUP_4
)
582 /* Reset the cursor */
583 h
->cursor
.i_column
= 0;
585 /* Erase lines above our window */
586 for( i
= 0; i
< h
->cursor
.i_row
- keep_lines
; i
++ )
587 Eia608ClearScreenRow( h
, i_screen
, i
);
590 for( i
= 0; i
< keep_lines
-1; i
++ )
592 const int i_row
= h
->cursor
.i_row
- keep_lines
+ i
+ 1;
595 assert( i_row
+1 < EIA608_SCREEN_ROWS
);
596 memcpy( screen
->characters
[i_row
], screen
->characters
[i_row
+1], sizeof(*screen
->characters
) );
597 memcpy( screen
->colors
[i_row
], screen
->colors
[i_row
+1], sizeof(*screen
->colors
) );
598 memcpy( screen
->fonts
[i_row
], screen
->fonts
[i_row
+1], sizeof(*screen
->fonts
) );
599 screen
->row_used
[i_row
] = screen
->row_used
[i_row
+1];
601 /* Reset current row */
602 Eia608ClearScreenRow( h
, i_screen
, h
->cursor
.i_row
);
604 static void Eia608ParseChannel( eia608_t
*h
, uint8_t d1
)
608 else if( d1
== 0x1c )
610 else if( ( d1
>= 0x01 && d1
<= 0x0f ) || d1
== 0x15 )
612 else if( d1
== 0x1d )
615 static bool Eia608ParseTextAttribute( eia608_t
*h
, uint8_t d2
)
617 const int i_index
= d2
- 0x20;
618 assert( d2
>= 0x20 && d2
<= 0x2f );
620 h
->color
= pac2_attribs
[i_index
].i_color
;
621 h
->font
= pac2_attribs
[i_index
].i_font
;
622 Eia608Cursor( h
, 1 );
626 static bool Eia608ParseSingle( eia608_t
*h
, const uint8_t dx
)
628 assert( dx
>= 0x20 );
629 Eia608Write( h
, dx
);
632 static bool Eia608ParseDouble( eia608_t
*h
, uint8_t d2
)
634 assert( d2
>= 0x30 && d2
<= 0x3f );
635 Eia608Write( h
, d2
+ 0x50 ); /* We use charaters 0x80...0x8f */
638 static bool Eia608ParseExtended( eia608_t
*h
, uint8_t d1
, uint8_t d2
)
640 assert( d2
>= 0x20 && d2
<= 0x3f );
641 assert( d1
== 0x12 || d1
== 0x13 );
643 d2
+= 0x70; /* We use charaters 0x90-0xaf */
645 d2
+= 0x90; /* We use charaters 0xb0-0xcf */
647 /* The extended characters replace the previous one with a more
649 Eia608Cursor( h
, -1 );
650 Eia608Write( h
, d2
);
653 static bool Eia608ParseCommand0x14( eia608_t
*h
, uint8_t d2
)
655 bool b_changed
= false;
659 case 0x20: /* Resume caption loading */
660 h
->mode
= EIA608_MODE_POPUP
;
662 case 0x21: /* Backspace */
666 case 0x22: /* Reserved */
669 case 0x24: /* Delete to end of row */
670 Eia608EraseToEndOfRow( h
);
672 case 0x25: /* Rollup 2 */
673 case 0x26: /* Rollup 3 */
674 case 0x27: /* Rollup 4 */
675 if( h
->mode
== EIA608_MODE_POPUP
|| h
->mode
== EIA608_MODE_PAINTON
)
677 Eia608EraseScreen( h
, true );
678 Eia608EraseScreen( h
, false );
683 h
->mode
= EIA608_MODE_ROLLUP_2
;
684 else if( d2
== 0x26 )
685 h
->mode
= EIA608_MODE_ROLLUP_3
;
687 h
->mode
= EIA608_MODE_ROLLUP_4
;
689 h
->cursor
.i_column
= 0;
690 h
->cursor
.i_row
= h
->i_row_rollup
;
692 case 0x28: /* Flash on */
695 case 0x29: /* Resume direct captionning */
696 h
->mode
= EIA608_MODE_PAINTON
;
698 case 0x2a: /* Text restart */
702 case 0x2b: /* Resume text display */
703 h
->mode
= EIA608_MODE_TEXT
;
706 case 0x2c: /* Erase displayed memory */
707 Eia608EraseScreen( h
, true );
710 case 0x2d: /* Carriage return */
714 case 0x2e: /* Erase non displayed memory */
715 Eia608EraseScreen( h
, false );
717 case 0x2f: /* End of caption (flip screen if not paint on) */
718 if( h
->mode
!= EIA608_MODE_PAINTON
)
719 h
->i_screen
= 1 - h
->i_screen
;
720 h
->mode
= EIA608_MODE_POPUP
;
721 h
->cursor
.i_column
= 0;
723 h
->color
= EIA608_COLOR_DEFAULT
;
724 h
->font
= EIA608_FONT_REGULAR
;
730 static bool Eia608ParseCommand0x17( eia608_t
*h
, uint8_t d2
)
734 case 0x21: /* Tab offset 1 */
735 Eia608Cursor( h
, 1 );
737 case 0x22: /* Tab offset 2 */
738 Eia608Cursor( h
, 2 );
740 case 0x23: /* Tab offset 3 */
741 Eia608Cursor( h
, 3 );
746 static bool Eia608ParsePac( eia608_t
*h
, uint8_t d1
, uint8_t d2
)
748 static const int pi_row
[] = {
749 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
751 const int i_row_index
= ( (d1
<<1) & 0x0e) | ( (d2
>>5) & 0x01 );
753 assert( d2
>= 0x40 && d2
<= 0x7f );
755 if( pi_row
[i_row_index
] <= 0 )
759 if( h
->mode
!= EIA608_MODE_TEXT
)
760 h
->cursor
.i_row
= pi_row
[i_row_index
] - 1;
761 h
->i_row_rollup
= pi_row
[i_row_index
] - 1;
765 else if( d2
>= 0x40 )
767 h
->cursor
.i_column
= pac2_attribs
[d2
].i_column
;
771 static bool Eia608ParseData( eia608_t
*h
, uint8_t d1
, uint8_t d2
)
773 bool b_changed
= false;
775 if( d1
>= 0x18 && d1
<= 0x1f )
778 #define ON( d2min, d2max, cmd ) do { if( d2 >= d2min && d2 <= d2max ) b_changed = cmd; } while(0)
782 ON( 0x20, 0x2f, Eia608ParseTextAttribute( h
, d2
) );
783 ON( 0x30, 0x3f, Eia608ParseDouble( h
, d2
) );
785 case 0x12: case 0x13:
786 ON( 0x20, 0x3f, Eia608ParseExtended( h
, d1
, d2
) );
788 case 0x14: case 0x15:
789 ON( 0x20, 0x2f, Eia608ParseCommand0x14( h
, d2
) );
792 ON( 0x21, 0x22, Eia608ParseCommand0x17( h
, d2
) );
793 ON( 0x2e, 0x2f, Eia608ParseTextAttribute( h
, d2
) );
797 ON( 0x40, 0x5f, Eia608ParsePac( h
, d1
, d2
) );
798 else if( d1
>= 0x11 && d1
<= 0x17 )
799 ON( 0x40, 0x7f, Eia608ParsePac( h
, d1
, d2
) );
803 b_changed
= Eia608ParseSingle( h
, d1
);
805 b_changed
|= Eia608ParseSingle( h
, d2
);
810 static void Eia608TextUtf8( char *psz_utf8
, uint8_t c
) // Returns number of bytes used
812 #define E1(c,u) { c, { u, '\0' } }
813 #define E2(c,u1,u2) { c, { u1, u2, '\0' } }
814 #define E3(c,u1,u2,u3) { c, { u1, u2, u3, '\0' } }
815 static const struct {
819 // Regular line-21 character set, mostly ASCII except these exceptions
820 E2( 0x2a, 0xc3,0xa1), // lowercase a, acute accent
821 E2( 0x5c, 0xc3,0xa9), // lowercase e, acute accent
822 E2( 0x5e, 0xc3,0xad), // lowercase i, acute accent
823 E2( 0x5f, 0xc3,0xb3), // lowercase o, acute accent
824 E2( 0x60, 0xc3,0xba), // lowercase u, acute accent
825 E2( 0x7b, 0xc3,0xa7), // lowercase c with cedilla
826 E2( 0x7c, 0xc3,0xb7), // division symbol
827 E2( 0x7d, 0xc3,0x91), // uppercase N tilde
828 E2( 0x7e, 0xc3,0xb1), // lowercase n tilde
829 // THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
830 // THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F
831 E2( 0x80, 0xc2,0xae), // Registered symbol (R)
832 E2( 0x81, 0xc2,0xb0), // degree sign
833 E2( 0x82, 0xc2,0xbd), // 1/2 symbol
834 E2( 0x83, 0xc2,0xbf), // Inverted (open) question mark
835 E3( 0x84, 0xe2,0x84,0xa2), // Trademark symbol (TM)
836 E2( 0x85, 0xc2,0xa2), // Cents symbol
837 E2( 0x86, 0xc2,0xa3), // Pounds sterling
838 E3( 0x87, 0xe2,0x99,0xaa), // Music note
839 E2( 0x88, 0xc3,0xa0), // lowercase a, grave accent
840 E1( 0x89, 0x20), // transparent space, we make it regular
841 E2( 0x8a, 0xc3,0xa8), // lowercase e, grave accent
842 E2( 0x8b, 0xc3,0xa2), // lowercase a, circumflex accent
843 E2( 0x8c, 0xc3,0xaa), // lowercase e, circumflex accent
844 E2( 0x8d, 0xc3,0xae), // lowercase i, circumflex accent
845 E2( 0x8e, 0xc3,0xb4), // lowercase o, circumflex accent
846 E2( 0x8f, 0xc3,0xbb), // lowercase u, circumflex accent
847 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
848 // THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F
849 E2( 0x90, 0xc3,0x81), // capital letter A with acute
850 E2( 0x91, 0xc3,0x89), // capital letter E with acute
851 E2( 0x92, 0xc3,0x93), // capital letter O with acute
852 E2( 0x93, 0xc3,0x9a), // capital letter U with acute
853 E2( 0x94, 0xc3,0x9c), // capital letter U with diaresis
854 E2( 0x95, 0xc3,0xbc), // lowercase letter U with diaeresis
855 E1( 0x96, 0x27), // apostrophe
856 E2( 0x97, 0xc1,0xa1), // inverted exclamation mark
857 E1( 0x98, 0x2a), // asterisk
858 E1( 0x99, 0x27), // apostrophe (yes, duped). See CCADI source code.
859 E1( 0x9a, 0x2d), // hyphen-minus
860 E2( 0x9b, 0xc2,0xa9), // copyright sign
861 E3( 0x9c, 0xe2,0x84,0xa0), // Service mark
862 E1( 0x9d, 0x2e), // Full stop (.)
863 E1( 0x9e, 0x22), // Quoatation mark
864 E1( 0x9f, 0x22), // Quoatation mark
865 E2( 0xa0, 0xc3,0x80), // uppercase A, grave accent
866 E2( 0xa1, 0xc3,0x82), // uppercase A, circumflex
867 E2( 0xa2, 0xc3,0x87), // uppercase C with cedilla
868 E2( 0xa3, 0xc3,0x88), // uppercase E, grave accent
869 E2( 0xa4, 0xc3,0x8a), // uppercase E, circumflex
870 E2( 0xa5, 0xc3,0x8b), // capital letter E with diaresis
871 E2( 0xa6, 0xc3,0xab), // lowercase letter e with diaresis
872 E2( 0xa7, 0xc3,0x8e), // uppercase I, circumflex
873 E2( 0xa8, 0xc3,0x8f), // uppercase I, with diaresis
874 E2( 0xa9, 0xc3,0xaf), // lowercase i, with diaresis
875 E2( 0xaa, 0xc3,0x94), // uppercase O, circumflex
876 E2( 0xab, 0xc3,0x99), // uppercase U, grave accent
877 E2( 0xac, 0xc3,0xb9), // lowercase u, grave accent
878 E2( 0xad, 0xc3,0x9b), // uppercase U, circumflex
879 E2( 0xae, 0xc2,0xab), // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
880 E2( 0xaf, 0xc2,0xbb), // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
881 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
882 // THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F
883 E2( 0xb0, 0xc3,0x83), // Uppercase A, tilde
884 E2( 0xb1, 0xc3,0xa3), // Lowercase a, tilde
885 E2( 0xb2, 0xc3,0x8d), // Uppercase I, acute accent
886 E2( 0xb3, 0xc3,0x8c), // Uppercase I, grave accent
887 E2( 0xb4, 0xc3,0xac), // Lowercase i, grave accent
888 E2( 0xb5, 0xc3,0x92), // Uppercase O, grave accent
889 E2( 0xb6, 0xc3,0xb2), // Lowercase o, grave accent
890 E2( 0xb7, 0xc3,0x95), // Uppercase O, tilde
891 E2( 0xb8, 0xc3,0xb5), // Lowercase o, tilde
892 E1( 0xb9, 0x7b), // Open curly brace
893 E1( 0xba, 0x7d), // Closing curly brace
894 E1( 0xbb, 0x5c), // Backslash
895 E1( 0xbc, 0x5e), // Caret
896 E1( 0xbd, 0x5f), // Underscore
897 E2( 0xbe, 0xc2,0xa6), // Pipe (broken bar)
898 E1( 0xbf, 0x7e), // Tilde (utf8 code unsure)
899 E2( 0xc0, 0xc3,0x84), // Uppercase A, umlaut
900 E2( 0xc1, 0xc3,0xa4), // Lowercase A, umlaut
901 E2( 0xc2, 0xc3,0x96), // Uppercase O, umlaut
902 E2( 0xc3, 0xc3,0xb6), // Lowercase o, umlaut
903 E2( 0xc4, 0xc3,0x9f), // Esszett (sharp S)
904 E2( 0xc5, 0xc2,0xa5), // Yen symbol
905 E2( 0xc6, 0xc2,0xa4), // Currency symbol
906 E1( 0xc7, 0x7c), // Vertical bar
907 E2( 0xc8, 0xc3,0x85), // Uppercase A, ring
908 E2( 0xc9, 0xc3,0xa5), // Lowercase A, ring
909 E2( 0xca, 0xc3,0x98), // Uppercase O, slash
910 E2( 0xcb, 0xc3,0xb8), // Lowercase o, slash
911 E3( 0xcc, 0xe2,0x8c,0x9c), // Upper left corner
912 E3( 0xcd, 0xe2,0x8c,0x9d), // Upper right corner
913 E3( 0xce, 0xe2,0x8c,0x9e), // Lower left corner
914 E3( 0xcf, 0xe2,0x8c,0x9f), // Lower right corner
922 static const int i_c2utf8
= sizeof(c2utf8
)/sizeof(*c2utf8
);
925 for( i
= 0; i
< i_c2utf8
; i
++ )
927 if( c2utf8
[i
].c
== c
)
932 psz_utf8
[0] = c
< 0x80 ? c
: '?'; /* Normal : Unsupported */
937 strcpy( psz_utf8
, c2utf8
[i
].utf8
);
941 static void Eia608Strlcat( char *d
, const char *s
, int i_max
)
944 strncat( d
, s
, i_max
-1 - strnlen(d
, i_max
-1));
949 static void Eia608TextLine( struct eia608_screen
*screen
, char *psz_text
, int i_text_max
, int i_row
, bool b_html
)
951 const uint8_t *p_char
= screen
->characters
[i_row
];
952 const eia608_color_t
*p_color
= screen
->colors
[i_row
];
953 const eia608_font_t
*p_font
= screen
->fonts
[i_row
];
957 eia608_color_t last_color
= EIA608_COLOR_DEFAULT
;
958 bool b_last_italics
= false;
959 bool b_last_underline
= false;
961 /* Search the start */
963 while( i_start
< EIA608_SCREEN_COLUMNS
-1 && p_char
[i_start
] == ' ' )
967 i_end
= EIA608_SCREEN_COLUMNS
-1;
968 while( i_end
> i_start
&& p_char
[i_end
] == ' ' )
972 #define CAT(t) Eia608Strlcat( psz_text, t, i_text_max )
973 for( x
= i_start
; x
<= i_end
; x
++ )
975 eia608_color_t color
= p_color
[x
];
976 bool b_italics
= p_font
[x
] & EIA608_FONT_ITALICS
;
977 bool b_underline
= p_font
[x
] & EIA608_FONT_UNDERLINE
;
983 bool b_close_color
, b_close_italics
, b_close_underline
;
985 /* We create the tags font / i / u in that orders */
986 b_close_color
= color
!= last_color
&& last_color
!= EIA608_COLOR_DEFAULT
;
987 b_close_italics
= !b_italics
&& b_last_italics
;
988 b_close_underline
= !b_underline
&& b_last_underline
;
990 /* Be sure to create valid html */
991 b_close_italics
|= b_last_italics
&& b_close_color
;
992 b_close_underline
= b_last_underline
&& ( b_close_italics
|| b_close_color
);
994 if( b_close_underline
)
996 if( b_close_italics
)
1001 if( color
!= EIA608_COLOR_DEFAULT
&& color
!= last_color
)
1003 static const char *ppsz_color
[] = {
1009 "#ffff00", // yellow
1010 "#ff00ff", // magenta
1011 "#ffffff", // user defined XXX we use white
1013 CAT( "<font color=" );
1014 CAT( ppsz_color
[color
] );
1017 if( ( b_close_italics
&& b_italics
) || ( b_italics
&& !b_last_italics
) )
1019 if( ( b_close_underline
&& b_underline
) || ( b_underline
&& !b_last_underline
) )
1024 Eia608TextUtf8( utf8
, p_char
[x
] );
1028 b_last_underline
= b_underline
;
1029 b_last_italics
= b_italics
;
1034 if( b_last_underline
)
1036 if( b_last_italics
)
1038 if( last_color
!= EIA608_COLOR_DEFAULT
)
1045 static void Eia608Init( eia608_t
*h
)
1047 memset( h
, 0, sizeof(*h
) );
1053 Eia608ClearScreen( h
, 0 );
1054 Eia608ClearScreen( h
, 1 );
1056 /* Cursor for writing text */
1057 h
->cursor
.i_column
= 0;
1058 h
->cursor
.i_row
= 0;
1062 h
->mode
= EIA608_MODE_POPUP
;
1063 h
->color
= EIA608_COLOR_DEFAULT
;
1064 h
->font
= EIA608_FONT_REGULAR
;
1065 h
->i_row_rollup
= EIA608_SCREEN_ROWS
-1;
1067 static bool Eia608Parse( eia608_t
*h
, int i_channel_selected
, const uint8_t data
[2] )
1069 const uint8_t d1
= data
[0] & 0x7f; /* Removed parity bit TODO we might want to check them */
1070 const uint8_t d2
= data
[1] & 0x7f;
1071 bool b_screen_changed
= false;
1073 if( d1
== 0 && d2
== 0 )
1074 return false; /* Ignore padding */
1076 Eia608ParseChannel( h
, d1
);
1077 if( h
->i_channel
!= i_channel_selected
)
1079 //fprintf( stderr, "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC %x %x\n", data[0], data[1] );
1084 d1
!= h
->last
.d1
|| d2
!= h
->last
.d2
) /* Command codes can be repeated */
1085 b_screen_changed
= Eia608ParseData( h
, d1
,d2
);
1090 else if( ( d1
>= 0x01 && d1
<= 0x0E ) || d1
== 0x0F )
1092 /* XDS block / End of XDS block */
1094 return b_screen_changed
;
1097 static char *Eia608Text( eia608_t
*h
, bool b_html
)
1099 const int i_size
= EIA608_SCREEN_ROWS
* 3 * EIA608_SCREEN_COLUMNS
+1;
1100 struct eia608_screen
*screen
= &h
->screen
[h
->i_screen
];
1101 bool b_first
= true;
1105 /* We allocate a buffer big enough for normal case */
1106 psz
= malloc( i_size
);
1111 Eia608Strlcat( psz
, "<text>", i_size
);
1112 for( i
= 0; i
< EIA608_SCREEN_ROWS
; i
++ )
1114 if( !screen
->row_used
[i
] )
1118 Eia608Strlcat( psz
, b_html
? "<br />" : "\n", i_size
);
1121 Eia608TextLine( screen
, psz
, i_size
, i
, b_html
);
1124 Eia608Strlcat( psz
, "</text>", i_size
);
1128 static void Eia608Exit( eia608_t
*h
)