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
* );
54 #define OPAQUE_TEXT N_("Opacity")
55 #define OPAQUE_LONGTEXT N_("Setting to true " \
56 "makes the text to be boxed and maybe easier to read." )
59 set_shortname( N_("CC 608/708"))
60 set_description( N_("Closed Captions decoder") )
61 set_capability( "decoder", 50 )
62 set_category( CAT_INPUT
)
63 set_subcategory( SUBCAT_INPUT_SCODEC
)
64 set_callbacks( Open
, Close
)
66 add_bool( "cc-opaque", true,
67 OPAQUE_TEXT
, OPAQUE_LONGTEXT
, false )
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 EIA608_STATUS_DEFAULT
= 0x00,
118 EIA608_STATUS_CHANGED
= 0x01, /* current screen has been altered */
119 EIA608_STATUS_CAPTION_ENDED
= 0x02, /* screen flip */
120 EIA608_STATUS_CAPTION_CLEARED
= 0x04, /* active screen erased */
121 EIA608_STATUS_DISPLAY
= EIA608_STATUS_CAPTION_CLEARED
| EIA608_STATUS_CAPTION_ENDED
,
124 static const struct {
125 eia608_color_t i_color
;
126 eia608_font_t i_font
;
129 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 0 },
130 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 0 },
131 { EIA608_COLOR_GREEN
, EIA608_FONT_REGULAR
, 0 },
132 { EIA608_COLOR_GREEN
, EIA608_FONT_UNDERLINE
, 0 },
133 { EIA608_COLOR_BLUE
, EIA608_FONT_REGULAR
, 0 },
134 { EIA608_COLOR_BLUE
, EIA608_FONT_UNDERLINE
, 0 },
135 { EIA608_COLOR_CYAN
, EIA608_FONT_REGULAR
, 0 },
136 { EIA608_COLOR_CYAN
, EIA608_FONT_UNDERLINE
, 0 },
137 { EIA608_COLOR_RED
, EIA608_FONT_REGULAR
, 0 },
138 { EIA608_COLOR_RED
, EIA608_FONT_UNDERLINE
, 0 },
139 { EIA608_COLOR_YELLOW
, EIA608_FONT_REGULAR
, 0 },
140 { EIA608_COLOR_YELLOW
, EIA608_FONT_UNDERLINE
, 0 },
141 { EIA608_COLOR_MAGENTA
, EIA608_FONT_REGULAR
, 0 },
142 { EIA608_COLOR_MAGENTA
, EIA608_FONT_UNDERLINE
, 0 },
143 { EIA608_COLOR_WHITE
, EIA608_FONT_ITALICS
, 0 },
144 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE_ITALICS
, 0 },
146 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 0 },
147 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 0 },
148 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 4 },
149 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 4 },
150 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 8 },
151 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 8 },
152 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 12 },
153 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 12 },
154 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 16 },
155 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 16 },
156 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 20 },
157 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 20 },
158 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 24 },
159 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 24 },
160 { EIA608_COLOR_WHITE
, EIA608_FONT_REGULAR
, 28 },
161 { EIA608_COLOR_WHITE
, EIA608_FONT_UNDERLINE
, 28 } ,
164 #define EIA608_COLOR_DEFAULT EIA608_COLOR_WHITE
166 static const int rgi_eia608_colors
[] = {
174 0xffffff, // user defined XXX we use white
179 /* Current channel (used to reject packet without channel information) */
183 int i_screen
; /* Displayed screen */
184 eia608_screen screen
[2];
194 eia608_color_t color
;
198 /* Last command pair (used to reject duplicated command) */
206 static void Eia608Init( eia608_t
* );
207 static eia608_status_t
Eia608Parse( eia608_t
*h
, int i_channel_selected
, const uint8_t data
[2] );
208 static void Eia608FillUpdaterRegions( subpicture_updater_sys_t
*p_updater
, eia608_t
*h
);
210 /* It will be enough up to 63 B frames, which is far too high for
211 * broadcast environment */
212 #define CC_MAX_REORDER_SIZE (64)
216 block_t
*pp_block
[CC_MAX_REORDER_SIZE
];
217 block_t
*p_block
; /* currently processed block (if incomplely) */
222 mtime_t i_display_time
;
228 static int Decode( decoder_t
*, block_t
* );
229 static void Flush( decoder_t
* );
231 /*****************************************************************************
232 * Open: probe the decoder and return score
233 *****************************************************************************
234 * Tries to launch a decoder and return score so that the interface is able
236 *****************************************************************************/
237 static int Open( vlc_object_t
*p_this
)
239 decoder_t
*p_dec
= (decoder_t
*)p_this
;
240 decoder_sys_t
*p_sys
;
244 switch( p_dec
->fmt_in
.i_codec
)
246 case VLC_CODEC_EIA608_1
:
247 i_field
= 0; i_channel
= 1;
249 case VLC_CODEC_EIA608_2
:
250 i_field
= 0; i_channel
= 2;
252 case VLC_CODEC_EIA608_3
:
253 i_field
= 1; i_channel
= 1;
255 case VLC_CODEC_EIA608_4
:
256 i_field
= 1; i_channel
= 2;
263 p_dec
->pf_decode
= Decode
;
264 p_dec
->pf_flush
= Flush
;
266 /* Allocate the memory needed to store the decoder's structure */
267 p_dec
->p_sys
= p_sys
= calloc( 1, sizeof( *p_sys
) );
272 p_sys
->i_field
= i_field
;
273 p_sys
->i_channel
= i_channel
;
275 Eia608Init( &p_sys
->eia608
);
276 p_sys
->b_opaque
= var_InheritBool( p_dec
, "cc-opaque" );
278 p_dec
->fmt_out
.i_cat
= SPU_ES
;
279 p_dec
->fmt_out
.i_codec
= VLC_CODEC_TEXT
;
284 /*****************************************************************************
286 *****************************************************************************/
287 static void Flush( decoder_t
*p_dec
)
289 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
291 Eia608Init( &p_sys
->eia608
);
292 p_sys
->i_display_time
= VLC_TS_INVALID
;
295 /****************************************************************************
296 * Decode: the whole thing
297 ****************************************************************************
299 ****************************************************************************/
300 static void Push( decoder_t
*, block_t
* );
301 static block_t
*Pop( decoder_t
* );
302 static subpicture_t
*Convert( decoder_t
*, block_t
** );
304 static int Decode( decoder_t
*p_dec
, block_t
*p_block
)
306 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
309 Push( p_dec
, p_block
);
313 if( !p_sys
->p_block
)
314 p_sys
->p_block
= Pop( p_dec
);
316 /* Reset decoder if needed */
317 if( p_sys
->p_block
&&
318 (p_sys
->p_block
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
| BLOCK_FLAG_CORRUPTED
)) )
321 /* clear flags, as we might process it more than once */
322 p_sys
->p_block
->i_flags
&= ~(BLOCK_FLAG_DISCONTINUITY
| BLOCK_FLAG_CORRUPTED
);
326 if( !p_sys
->p_block
)
329 subpicture_t
*p_spu
= Convert( p_dec
, &p_sys
->p_block
);
331 decoder_QueueSub( p_dec
, p_spu
);
333 return VLCDEC_SUCCESS
;
336 /*****************************************************************************
337 * CloseDecoder: clean up the decoder
338 *****************************************************************************/
339 static void Close( vlc_object_t
*p_this
)
341 decoder_t
*p_dec
= (decoder_t
*)p_this
;
342 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
344 for( int i
= 0; i
< p_sys
->i_block
; i
++ )
345 block_Release( p_sys
->pp_block
[i
] );
349 /*****************************************************************************
351 *****************************************************************************/
352 static void Push( decoder_t
*p_dec
, block_t
*p_block
)
354 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
356 if( p_sys
->i_block
>= CC_MAX_REORDER_SIZE
)
358 msg_Warn( p_dec
, "Trashing a CC entry" );
359 memmove( &p_sys
->pp_block
[0], &p_sys
->pp_block
[1], sizeof(*p_sys
->pp_block
) * (CC_MAX_REORDER_SIZE
-1) );
362 p_sys
->pp_block
[p_sys
->i_block
++] = p_block
;
364 static block_t
*Pop( decoder_t
*p_dec
)
366 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
369 /* XXX Cc captions data are OUT OF ORDER (because we receive them in the bitstream
370 * order (ie ordered by video picture dts) instead of the display order.
371 * We will simulate a simple IPB buffer scheme
372 * and reorder with pts.
373 * XXX it won't work with H264 which use non out of order B picture or MMCO
376 if( p_sys
->i_block
&& (p_sys
->pp_block
[0]->i_flags
& BLOCK_FLAG_PRIVATE_MASK
) )
379 return p_sys
->pp_block
[0];
382 /* Wait for a P and output all *previous* picture by pts order (for
383 * hierarchical B frames) */
384 if( p_sys
->i_block
<= 1 ||
385 ( p_sys
->pp_block
[p_sys
->i_block
-1]->i_flags
& BLOCK_FLAG_TYPE_B
) )
388 p_block
= p_sys
->pp_block
[i_index
= 0];
389 if( p_block
->i_pts
> VLC_TS_INVALID
)
391 for( int i
= 1; i
< p_sys
->i_block
-1; i
++ )
393 if( p_sys
->pp_block
[i
]->i_pts
> VLC_TS_INVALID
&& p_block
->i_pts
> VLC_TS_INVALID
&&
394 p_sys
->pp_block
[i
]->i_pts
< p_block
->i_pts
)
395 p_block
= p_sys
->pp_block
[i_index
= i
];
398 assert( i_index
+1 < p_sys
->i_block
);
399 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 ) );
405 static subpicture_t
*Subtitle( decoder_t
*p_dec
, eia608_t
*h
, mtime_t i_pts
)
407 //decoder_sys_t *p_sys = p_dec->p_sys;
408 subpicture_t
*p_spu
= NULL
;
410 /* We cannot display a subpicture with no date */
411 if( i_pts
<= VLC_TS_INVALID
)
414 /* Create the subpicture unit */
415 p_spu
= decoder_NewSubpictureText( p_dec
);
419 p_spu
->i_start
= i_pts
;
420 p_spu
->i_stop
= i_pts
+ 10000000; /* 10s max */
421 p_spu
->b_ephemer
= true;
422 p_spu
->b_absolute
= false;
424 subpicture_updater_sys_t
*p_spu_sys
= p_spu
->updater
.p_sys
;
426 /* The "leavetext" alignment is a special mode where the subpicture
427 region itself gets aligned, but the text inside it does not */
428 p_spu_sys
->region
.align
= SUBPICTURE_ALIGN_TOP
;
429 p_spu_sys
->region
.inner_align
= SUBPICTURE_ALIGN_LEAVETEXT
;
430 p_spu_sys
->region
.flags
= UPDT_REGION_IGNORE_BACKGROUND
| UPDT_REGION_USES_GRID_COORDINATES
;
431 /* Set style defaults (will be added to segments if none set) */
432 p_spu_sys
->p_default_style
->i_style_flags
|= STYLE_MONOSPACED
;
433 if( p_dec
->p_sys
->b_opaque
)
435 p_spu_sys
->p_default_style
->i_background_alpha
= STYLE_ALPHA_OPAQUE
;
436 p_spu_sys
->p_default_style
->i_features
|= STYLE_HAS_BACKGROUND_ALPHA
;
437 p_spu_sys
->p_default_style
->i_style_flags
|= STYLE_BACKGROUND
;
439 p_spu_sys
->p_default_style
->i_font_color
= rgi_eia608_colors
[EIA608_COLOR_DEFAULT
];
440 /* FCC defined "safe area" for EIA-608 captions is 80% of the height of the display */
441 p_spu_sys
->p_default_style
->f_font_relsize
= 100 * 8 / 10 / EIA608_SCREEN_ROWS
;
442 p_spu_sys
->p_default_style
->i_features
|= (STYLE_HAS_FONT_COLOR
| STYLE_HAS_FLAGS
);
444 Eia608FillUpdaterRegions( p_spu_sys
, h
);
449 static subpicture_t
*Convert( decoder_t
*p_dec
, block_t
**pp_block
)
451 assert( pp_block
&& *pp_block
);
453 block_t
*p_block
= *pp_block
;
455 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
457 if( p_sys
->i_display_time
== VLC_TS_INVALID
)
458 p_sys
->i_display_time
= p_block
->i_pts
;
460 eia608_status_t i_status
= EIA608_STATUS_DEFAULT
;
462 /* TODO do the real decoding here */
463 while( p_block
->i_buffer
>= 3 && !(i_status
& EIA608_STATUS_DISPLAY
) )
465 /* Mask off the specific i_field bit, else some sequences can be lost. */
466 if ( (p_block
->p_buffer
[0] & 0x03) == p_sys
->i_field
&&
467 (p_block
->p_buffer
[0] & 0x04) /* Valid bit */ )
469 i_status
= Eia608Parse( &p_sys
->eia608
, p_sys
->i_channel
, &p_block
->p_buffer
[1] );
470 p_sys
->i_display_time
+= CLOCK_FREQ
/ 30;
473 p_block
->i_buffer
-= 3;
474 p_block
->p_buffer
+= 3;
477 const mtime_t i_pts
= p_sys
->i_display_time
;
479 if( p_block
->i_buffer
< 3 )
481 block_Release( p_block
);
482 p_sys
->i_display_time
= VLC_TS_INVALID
;
486 /* a caption is ready or removed, process its screen */
488 * In case of rollup/painton with 1 packet/frame, we need to update on Changed status.
489 * Batch decoding might be incorrect if those in large number of commands (mp4, ...) then.
490 * see CEAv1.2zero.trp tests
492 if( i_status
& (EIA608_STATUS_DISPLAY
| EIA608_STATUS_CHANGED
) )
494 return Subtitle( p_dec
, &p_sys
->eia608
, i_pts
);
500 /*****************************************************************************
502 *****************************************************************************/
503 static void Eia608Cursor( eia608_t
*h
, int dx
)
505 h
->cursor
.i_column
+= dx
;
506 if( h
->cursor
.i_column
< 0 )
507 h
->cursor
.i_column
= 0;
508 else if( h
->cursor
.i_column
> EIA608_SCREEN_COLUMNS
-1 )
509 h
->cursor
.i_column
= EIA608_SCREEN_COLUMNS
-1;
511 static void Eia608ClearScreenRowX( eia608_t
*h
, int i_screen
, int i_row
, int x
)
513 eia608_screen
*screen
= &h
->screen
[i_screen
];
517 screen
->row_used
[i_row
] = false;
521 screen
->row_used
[i_row
] = false;
522 for( int i
= 0; i
< x
; i
++ )
524 if( screen
->characters
[i_row
][i
] != ' ' ||
525 screen
->colors
[i_row
][i
] != EIA608_COLOR_DEFAULT
||
526 screen
->fonts
[i_row
][i
] != EIA608_FONT_REGULAR
)
528 screen
->row_used
[i_row
] = true;
534 for( ; x
< EIA608_SCREEN_COLUMNS
+1; x
++ )
536 screen
->characters
[i_row
][x
] = x
< EIA608_SCREEN_COLUMNS
? ' ' : '\0';
537 screen
->colors
[i_row
][x
] = EIA608_COLOR_DEFAULT
;
538 screen
->fonts
[i_row
][x
] = EIA608_FONT_REGULAR
;
542 static void Eia608ClearScreenRow( eia608_t
*h
, int i_screen
, int i_row
)
544 Eia608ClearScreenRowX( h
, i_screen
, i_row
, 0 );
547 static void Eia608ClearScreen( eia608_t
*h
, int i_screen
)
549 for( int i
= 0; i
< EIA608_SCREEN_ROWS
; i
++ )
550 Eia608ClearScreenRow( h
, i_screen
, i
);
553 static int Eia608GetWritingScreenIndex( eia608_t
*h
)
557 case EIA608_MODE_POPUP
: // Non displayed screen
558 return 1 - h
->i_screen
;
560 case EIA608_MODE_ROLLUP_2
: // Displayed screen
561 case EIA608_MODE_ROLLUP_3
:
562 case EIA608_MODE_ROLLUP_4
:
563 case EIA608_MODE_PAINTON
:
566 /* It cannot happen, else it is a bug */
567 vlc_assert_unreachable();
572 static void Eia608EraseScreen( eia608_t
*h
, bool b_displayed
)
574 Eia608ClearScreen( h
, b_displayed
? h
->i_screen
: (1-h
->i_screen
) );
577 static void Eia608Write( eia608_t
*h
, const uint8_t c
)
579 const int i_row
= h
->cursor
.i_row
;
580 const int i_column
= h
->cursor
.i_column
;
581 eia608_screen
*screen
;
583 if( h
->mode
== EIA608_MODE_TEXT
)
586 screen
= &h
->screen
[Eia608GetWritingScreenIndex( h
)];
588 screen
->characters
[i_row
][i_column
] = c
;
589 screen
->colors
[i_row
][i_column
] = h
->color
;
590 screen
->fonts
[i_row
][i_column
] = h
->font
;
591 screen
->row_used
[i_row
] = true;
592 Eia608Cursor( h
, 1 );
594 static void Eia608Erase( eia608_t
*h
)
596 const int i_row
= h
->cursor
.i_row
;
597 const int i_column
= h
->cursor
.i_column
- 1;
598 eia608_screen
*screen
;
600 if( h
->mode
== EIA608_MODE_TEXT
)
605 screen
= &h
->screen
[Eia608GetWritingScreenIndex( h
)];
607 /* FIXME do we need to reset row_used/colors/font ? */
608 screen
->characters
[i_row
][i_column
] = ' ';
609 Eia608Cursor( h
, -1 );
611 static void Eia608EraseToEndOfRow( eia608_t
*h
)
613 if( h
->mode
== EIA608_MODE_TEXT
)
616 Eia608ClearScreenRowX( h
, Eia608GetWritingScreenIndex( h
), h
->cursor
.i_row
, h
->cursor
.i_column
);
619 static void Eia608RollUp( eia608_t
*h
)
621 if( h
->mode
== EIA608_MODE_TEXT
)
624 const int i_screen
= Eia608GetWritingScreenIndex( h
);
625 eia608_screen
*screen
= &h
->screen
[i_screen
];
630 if( h
->mode
== EIA608_MODE_ROLLUP_2
)
632 else if( h
->mode
== EIA608_MODE_ROLLUP_3
)
634 else if( h
->mode
== EIA608_MODE_ROLLUP_4
)
639 /* Reset the cursor */
640 h
->cursor
.i_column
= 0;
642 /* Erase lines above our window */
643 for( int i
= 0; i
< h
->cursor
.i_row
- keep_lines
; i
++ )
644 Eia608ClearScreenRow( h
, i_screen
, i
);
647 for( int i
= 0; i
< keep_lines
-1; i
++ )
649 const int i_row
= h
->cursor
.i_row
- keep_lines
+ i
+ 1;
652 assert( i_row
+1 < EIA608_SCREEN_ROWS
);
653 memcpy( screen
->characters
[i_row
], screen
->characters
[i_row
+1], sizeof(*screen
->characters
) );
654 memcpy( screen
->colors
[i_row
], screen
->colors
[i_row
+1], sizeof(*screen
->colors
) );
655 memcpy( screen
->fonts
[i_row
], screen
->fonts
[i_row
+1], sizeof(*screen
->fonts
) );
656 screen
->row_used
[i_row
] = screen
->row_used
[i_row
+1];
658 /* Reset current row */
659 Eia608ClearScreenRow( h
, i_screen
, h
->cursor
.i_row
);
661 static void Eia608ParseChannel( eia608_t
*h
, const uint8_t d
[2] )
663 /* Check odd parity */
664 static const int p4
[16] = {
665 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
667 if( p4
[d
[0] & 0xf] == p4
[d
[0] >> 4] ||
668 p4
[d
[1] & 0xf] == p4
[ d
[1] >> 4] )
675 const int d1
= d
[0] & 0x7f;
676 if( d1
>= 0x10 && d1
<= 0x1f )
677 h
->i_channel
= 1 + ((d1
& 0x08) != 0);
681 static eia608_status_t
Eia608ParseTextAttribute( eia608_t
*h
, uint8_t d2
)
683 const int i_index
= d2
- 0x20;
684 assert( d2
>= 0x20 && d2
<= 0x2f );
686 h
->color
= pac2_attribs
[i_index
].i_color
;
687 h
->font
= pac2_attribs
[i_index
].i_font
;
688 Eia608Cursor( h
, 1 );
690 return EIA608_STATUS_DEFAULT
;
692 static eia608_status_t
Eia608ParseSingle( eia608_t
*h
, const uint8_t dx
)
694 assert( dx
>= 0x20 );
695 Eia608Write( h
, dx
);
696 return EIA608_STATUS_CHANGED
;
698 static eia608_status_t
Eia608ParseDouble( eia608_t
*h
, uint8_t d2
)
700 assert( d2
>= 0x30 && d2
<= 0x3f );
701 Eia608Write( h
, d2
+ 0x50 ); /* We use charaters 0x80...0x8f */
702 return EIA608_STATUS_CHANGED
;
704 static eia608_status_t
Eia608ParseExtended( eia608_t
*h
, uint8_t d1
, uint8_t d2
)
706 assert( d2
>= 0x20 && d2
<= 0x3f );
707 assert( d1
== 0x12 || d1
== 0x13 );
709 d2
+= 0x70; /* We use charaters 0x90-0xaf */
711 d2
+= 0x90; /* We use charaters 0xb0-0xcf */
713 /* The extended characters replace the previous one with a more
715 Eia608Cursor( h
, -1 );
716 Eia608Write( h
, d2
);
717 return EIA608_STATUS_CHANGED
;
719 static eia608_status_t
Eia608ParseCommand0x14( eia608_t
*h
, uint8_t d2
)
721 eia608_status_t i_status
= EIA608_STATUS_DEFAULT
;
725 case 0x20: /* Resume caption loading */
726 h
->mode
= EIA608_MODE_POPUP
;
728 case 0x21: /* Backspace */
730 i_status
= EIA608_STATUS_CHANGED
;
732 case 0x22: /* Reserved */
735 case 0x24: /* Delete to end of row */
736 Eia608EraseToEndOfRow( h
);
738 case 0x25: /* Rollup 2 */
739 case 0x26: /* Rollup 3 */
740 case 0x27: /* Rollup 4 */
741 if( h
->mode
== EIA608_MODE_POPUP
|| h
->mode
== EIA608_MODE_PAINTON
)
743 Eia608EraseScreen( h
, true );
744 Eia608EraseScreen( h
, false );
745 i_status
= EIA608_STATUS_CHANGED
| EIA608_STATUS_CAPTION_CLEARED
;
749 h
->mode
= EIA608_MODE_ROLLUP_2
;
750 else if( d2
== 0x26 )
751 h
->mode
= EIA608_MODE_ROLLUP_3
;
753 h
->mode
= EIA608_MODE_ROLLUP_4
;
755 h
->cursor
.i_column
= 0;
756 h
->cursor
.i_row
= h
->i_row_rollup
;
758 case 0x28: /* Flash on */
761 case 0x29: /* Resume direct captionning */
762 h
->mode
= EIA608_MODE_PAINTON
;
764 case 0x2a: /* Text restart */
768 case 0x2b: /* Resume text display */
769 h
->mode
= EIA608_MODE_TEXT
;
772 case 0x2c: /* Erase displayed memory */
773 Eia608EraseScreen( h
, true );
774 i_status
= EIA608_STATUS_CHANGED
| EIA608_STATUS_CAPTION_CLEARED
;
776 case 0x2d: /* Carriage return */
778 i_status
= EIA608_STATUS_CHANGED
;
780 case 0x2e: /* Erase non displayed memory */
781 Eia608EraseScreen( h
, false );
783 case 0x2f: /* End of caption (flip screen if not paint on) */
784 if( h
->mode
!= EIA608_MODE_PAINTON
)
785 h
->i_screen
= 1 - h
->i_screen
;
786 h
->mode
= EIA608_MODE_POPUP
;
787 h
->cursor
.i_column
= 0;
789 h
->color
= EIA608_COLOR_DEFAULT
;
790 h
->font
= EIA608_FONT_REGULAR
;
791 i_status
= EIA608_STATUS_CHANGED
| EIA608_STATUS_CAPTION_ENDED
;
796 static bool Eia608ParseCommand0x17( eia608_t
*h
, uint8_t d2
)
800 case 0x21: /* Tab offset 1 */
801 Eia608Cursor( h
, 1 );
803 case 0x22: /* Tab offset 2 */
804 Eia608Cursor( h
, 2 );
806 case 0x23: /* Tab offset 3 */
807 Eia608Cursor( h
, 3 );
812 static bool Eia608ParsePac( eia608_t
*h
, uint8_t d1
, uint8_t d2
)
814 static const int pi_row
[] = {
815 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
817 const int i_row_index
= ( (d1
<<1) & 0x0e) | ( (d2
>>5) & 0x01 );
819 assert( d2
>= 0x40 && d2
<= 0x7f );
821 if( pi_row
[i_row_index
] <= 0 )
825 if( h
->mode
!= EIA608_MODE_TEXT
)
826 h
->cursor
.i_row
= pi_row
[i_row_index
] - 1;
827 h
->i_row_rollup
= pi_row
[i_row_index
] - 1;
831 else if( d2
>= 0x40 )
833 h
->cursor
.i_column
= pac2_attribs
[d2
].i_column
;
834 h
->color
= pac2_attribs
[d2
].i_color
;
835 h
->font
= pac2_attribs
[d2
].i_font
;
840 static eia608_status_t
Eia608ParseData( eia608_t
*h
, uint8_t d1
, uint8_t d2
)
842 eia608_status_t i_status
= EIA608_STATUS_DEFAULT
;
844 if( d1
>= 0x18 && d1
<= 0x1f )
847 #define ON( d2min, d2max, cmd ) do { if( d2 >= d2min && d2 <= d2max ) i_status = cmd; } while(0)
851 ON( 0x20, 0x2f, Eia608ParseTextAttribute( h
, d2
) );
852 ON( 0x30, 0x3f, Eia608ParseDouble( h
, d2
) );
854 case 0x12: case 0x13:
855 ON( 0x20, 0x3f, Eia608ParseExtended( h
, d1
, d2
) );
857 case 0x14: case 0x15:
858 ON( 0x20, 0x2f, Eia608ParseCommand0x14( h
, d2
) );
861 ON( 0x21, 0x23, Eia608ParseCommand0x17( h
, d2
) );
862 ON( 0x2e, 0x2f, Eia608ParseTextAttribute( h
, d2
) );
866 ON( 0x40, 0x5f, Eia608ParsePac( h
, d1
, d2
) );
867 else if( d1
>= 0x11 && d1
<= 0x17 )
868 ON( 0x40, 0x7f, Eia608ParsePac( h
, d1
, d2
) );
872 i_status
= Eia608ParseSingle( h
, d1
);
874 i_status
|= Eia608ParseSingle( h
, d2
);
877 /* Ignore changes occuring to doublebuffer */
878 if( h
->mode
== EIA608_MODE_POPUP
&& i_status
== EIA608_STATUS_CHANGED
)
879 i_status
= EIA608_STATUS_DEFAULT
;
884 static void Eia608TextUtf8( char *psz_utf8
, uint8_t c
) // Returns number of bytes used
886 #define E1(c,u) { c, { u, '\0' } }
887 #define E2(c,u1,u2) { c, { u1, u2, '\0' } }
888 #define E3(c,u1,u2,u3) { c, { u1, u2, u3, '\0' } }
889 static const struct {
893 // Regular line-21 character set, mostly ASCII except these exceptions
894 E2( 0x2a, 0xc3,0xa1), // lowercase a, acute accent
895 E2( 0x5c, 0xc3,0xa9), // lowercase e, acute accent
896 E2( 0x5e, 0xc3,0xad), // lowercase i, acute accent
897 E2( 0x5f, 0xc3,0xb3), // lowercase o, acute accent
898 E2( 0x60, 0xc3,0xba), // lowercase u, acute accent
899 E2( 0x7b, 0xc3,0xa7), // lowercase c with cedilla
900 E2( 0x7c, 0xc3,0xb7), // division symbol
901 E2( 0x7d, 0xc3,0x91), // uppercase N tilde
902 E2( 0x7e, 0xc3,0xb1), // lowercase n tilde
903 // THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
904 // THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F
905 E2( 0x80, 0xc2,0xae), // Registered symbol (R)
906 E2( 0x81, 0xc2,0xb0), // degree sign
907 E2( 0x82, 0xc2,0xbd), // 1/2 symbol
908 E2( 0x83, 0xc2,0xbf), // Inverted (open) question mark
909 E3( 0x84, 0xe2,0x84,0xa2), // Trademark symbol (TM)
910 E2( 0x85, 0xc2,0xa2), // Cents symbol
911 E2( 0x86, 0xc2,0xa3), // Pounds sterling
912 E3( 0x87, 0xe2,0x99,0xaa), // Music note
913 E2( 0x88, 0xc3,0xa0), // lowercase a, grave accent
914 E2( 0x89, 0xc2,0xa0), // transparent space
915 E2( 0x8a, 0xc3,0xa8), // lowercase e, grave accent
916 E2( 0x8b, 0xc3,0xa2), // lowercase a, circumflex accent
917 E2( 0x8c, 0xc3,0xaa), // lowercase e, circumflex accent
918 E2( 0x8d, 0xc3,0xae), // lowercase i, circumflex accent
919 E2( 0x8e, 0xc3,0xb4), // lowercase o, circumflex accent
920 E2( 0x8f, 0xc3,0xbb), // lowercase u, circumflex accent
921 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
922 // THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F
923 E2( 0x90, 0xc3,0x81), // capital letter A with acute
924 E2( 0x91, 0xc3,0x89), // capital letter E with acute
925 E2( 0x92, 0xc3,0x93), // capital letter O with acute
926 E2( 0x93, 0xc3,0x9a), // capital letter U with acute
927 E2( 0x94, 0xc3,0x9c), // capital letter U with diaresis
928 E2( 0x95, 0xc3,0xbc), // lowercase letter U with diaeresis
929 E1( 0x96, 0x27), // apostrophe
930 E2( 0x97, 0xc2,0xa1), // inverted exclamation mark
931 E1( 0x98, 0x2a), // asterisk
932 E1( 0x99, 0x27), // apostrophe (yes, duped). See CCADI source code.
933 E1( 0x9a, 0x2d), // hyphen-minus
934 E2( 0x9b, 0xc2,0xa9), // copyright sign
935 E3( 0x9c, 0xe2,0x84,0xa0), // Service mark
936 E1( 0x9d, 0x2e), // Full stop (.)
937 E3( 0x9e, 0xe2,0x80,0x9c), // Quotation mark
938 E3( 0x9f, 0xe2,0x80,0x9d), // Quotation mark
939 E2( 0xa0, 0xc3,0x80), // uppercase A, grave accent
940 E2( 0xa1, 0xc3,0x82), // uppercase A, circumflex
941 E2( 0xa2, 0xc3,0x87), // uppercase C with cedilla
942 E2( 0xa3, 0xc3,0x88), // uppercase E, grave accent
943 E2( 0xa4, 0xc3,0x8a), // uppercase E, circumflex
944 E2( 0xa5, 0xc3,0x8b), // capital letter E with diaresis
945 E2( 0xa6, 0xc3,0xab), // lowercase letter e with diaresis
946 E2( 0xa7, 0xc3,0x8e), // uppercase I, circumflex
947 E2( 0xa8, 0xc3,0x8f), // uppercase I, with diaresis
948 E2( 0xa9, 0xc3,0xaf), // lowercase i, with diaresis
949 E2( 0xaa, 0xc3,0x94), // uppercase O, circumflex
950 E2( 0xab, 0xc3,0x99), // uppercase U, grave accent
951 E2( 0xac, 0xc3,0xb9), // lowercase u, grave accent
952 E2( 0xad, 0xc3,0x9b), // uppercase U, circumflex
953 E2( 0xae, 0xc2,0xab), // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
954 E2( 0xaf, 0xc2,0xbb), // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
955 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
956 // THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F
957 E2( 0xb0, 0xc3,0x83), // Uppercase A, tilde
958 E2( 0xb1, 0xc3,0xa3), // Lowercase a, tilde
959 E2( 0xb2, 0xc3,0x8d), // Uppercase I, acute accent
960 E2( 0xb3, 0xc3,0x8c), // Uppercase I, grave accent
961 E2( 0xb4, 0xc3,0xac), // Lowercase i, grave accent
962 E2( 0xb5, 0xc3,0x92), // Uppercase O, grave accent
963 E2( 0xb6, 0xc3,0xb2), // Lowercase o, grave accent
964 E2( 0xb7, 0xc3,0x95), // Uppercase O, tilde
965 E2( 0xb8, 0xc3,0xb5), // Lowercase o, tilde
966 E1( 0xb9, 0x7b), // Open curly brace
967 E1( 0xba, 0x7d), // Closing curly brace
968 E1( 0xbb, 0x5c), // Backslash
969 E1( 0xbc, 0x5e), // Caret
970 E1( 0xbd, 0x5f), // Underscore
971 E2( 0xbe, 0xc2,0xa6), // Pipe (broken bar)
972 E1( 0xbf, 0x7e), // Tilde (utf8 code unsure)
973 E2( 0xc0, 0xc3,0x84), // Uppercase A, umlaut
974 E2( 0xc1, 0xc3,0xa4), // Lowercase A, umlaut
975 E2( 0xc2, 0xc3,0x96), // Uppercase O, umlaut
976 E2( 0xc3, 0xc3,0xb6), // Lowercase o, umlaut
977 E2( 0xc4, 0xc3,0x9f), // Esszett (sharp S)
978 E2( 0xc5, 0xc2,0xa5), // Yen symbol
979 E2( 0xc6, 0xc2,0xa4), // Currency symbol
980 E1( 0xc7, 0x7c), // Vertical bar
981 E2( 0xc8, 0xc3,0x85), // Uppercase A, ring
982 E2( 0xc9, 0xc3,0xa5), // Lowercase A, ring
983 E2( 0xca, 0xc3,0x98), // Uppercase O, slash
984 E2( 0xcb, 0xc3,0xb8), // Lowercase o, slash
985 E3( 0xcc, 0xe2,0x8c,0x9c), // Upper left corner
986 E3( 0xcd, 0xe2,0x8c,0x9d), // Upper right corner
987 E3( 0xce, 0xe2,0x8c,0x9e), // Lower left corner
988 E3( 0xcf, 0xe2,0x8c,0x9f), // Lower right corner
996 for( size_t i
= 0; i
< ARRAY_SIZE(c2utf8
) ; i
++ )
997 if( c2utf8
[i
].c
== c
) {
998 strcpy( psz_utf8
, c2utf8
[i
].utf8
);
1002 psz_utf8
[0] = c
< 0x80 ? c
: '?'; /* Normal : Unsupported */
1006 static void Eia608Strlcat( char *d
, const char *s
, int i_max
)
1009 strncat( d
, s
, i_max
-1 - strnlen(d
, i_max
-1));
1014 #define CAT(t) Eia608Strlcat( psz_text, t, i_text_max )
1016 static text_segment_t
* Eia608TextLine( struct eia608_screen
*screen
, int i_row
)
1018 const uint8_t *p_char
= screen
->characters
[i_row
];
1019 const eia608_color_t
*p_color
= screen
->colors
[i_row
];
1020 const eia608_font_t
*p_font
= screen
->fonts
[i_row
];
1024 eia608_color_t prev_color
= EIA608_COLOR_DEFAULT
;
1025 eia608_font_t prev_font
= EIA608_FONT_REGULAR
;
1028 const unsigned i_text_max
= 4 * EIA608_SCREEN_COLUMNS
+ 1;
1029 char psz_text
[i_text_max
+ 1];
1032 /* Search the start */
1035 /* Convert leading spaces to non-breaking so that they don't get
1036 stripped by the RenderHtml routine as regular whitespace */
1037 while( i_start
< EIA608_SCREEN_COLUMNS
&& p_char
[i_start
] == ' ' ) {
1038 Eia608TextUtf8( utf8
, 0x89 );
1043 /* Search the end */
1044 i_end
= EIA608_SCREEN_COLUMNS
-1;
1045 while( i_end
> i_start
&& p_char
[i_end
] == ' ' )
1049 if( i_start
> i_end
) /* Nothing to render */
1052 text_segment_t
*p_segment
, *p_segments_head
= p_segment
= text_segment_New( NULL
);
1056 p_segment
->style
= text_style_Create( STYLE_NO_DEFAULTS
);
1057 if(!p_segment
->style
)
1059 text_segment_Delete(p_segment
);
1062 /* Ensure we get a monospaced font (required for accurate positioning */
1063 p_segment
->style
->i_style_flags
|= STYLE_MONOSPACED
;
1065 for( x
= i_start
; x
<= i_end
; x
++ )
1067 eia608_color_t color
= p_color
[x
];
1068 eia608_font_t font
= p_font
[x
];
1070 if(font
!= prev_font
|| color
!= prev_color
)
1072 EnsureUTF8(psz_text
);
1073 p_segment
->psz_text
= strdup(psz_text
);
1075 p_segment
->p_next
= text_segment_New( NULL
);
1076 p_segment
= p_segment
->p_next
;
1078 return p_segments_head
;
1080 p_segment
->style
= text_style_Create( STYLE_NO_DEFAULTS
);
1081 if(!p_segment
->style
)
1083 text_segment_Delete(p_segment
);
1084 return p_segments_head
;
1086 p_segment
->style
->i_style_flags
|= STYLE_MONOSPACED
;
1088 /* start segment with new style */
1089 if(font
& EIA608_FONT_ITALICS
)
1091 p_segment
->style
->i_style_flags
|= STYLE_ITALIC
;
1092 p_segment
->style
->i_features
|= STYLE_HAS_FLAGS
;
1094 if(font
& EIA608_FONT_UNDERLINE
)
1096 p_segment
->style
->i_style_flags
|= STYLE_UNDERLINE
;
1097 p_segment
->style
->i_features
|= STYLE_HAS_FLAGS
;
1100 if(color
!= EIA608_COLOR_DEFAULT
)
1102 p_segment
->style
->i_font_color
= rgi_eia608_colors
[color
];
1103 p_segment
->style
->i_features
|= STYLE_HAS_FONT_COLOR
;
1107 Eia608TextUtf8( utf8
, p_char
[x
] );
1119 assert(!p_segment
->psz_text
); // shouldn't happen
1120 EnsureUTF8(psz_text
);
1121 p_segment
->psz_text
= strdup(psz_text
);
1124 return p_segments_head
;
1127 static void Eia608FillUpdaterRegions( subpicture_updater_sys_t
*p_updater
, eia608_t
*h
)
1129 struct eia608_screen
*screen
= &h
->screen
[h
->i_screen
];
1130 subpicture_updater_sys_region_t
*p_region
= &p_updater
->region
;
1131 text_segment_t
**pp_last
= &p_region
->p_segments
;
1132 bool b_newregion
= false;
1134 for( int i
= 0; i
< EIA608_SCREEN_ROWS
; i
++ )
1136 if( !screen
->row_used
[i
] )
1139 text_segment_t
*p_segments
= Eia608TextLine( screen
, i
);
1144 subpicture_updater_sys_region_t
*p_newregion
;
1145 p_newregion
= SubpictureUpdaterSysRegionNew();
1148 text_segment_ChainDelete( p_segments
);
1151 SubpictureUpdaterSysRegionAdd( p_region
, p_newregion
);
1152 p_region
= p_newregion
;
1153 pp_last
= &p_region
->p_segments
;
1154 b_newregion
= false;
1157 if( p_region
->p_segments
== NULL
) /* First segment in the [new] region */
1159 p_region
->origin
.y
= i
; /* set start line number */
1161 else /* Insert line break between region lines */
1163 *pp_last
= text_segment_New( "\n" );
1165 pp_last
= &((*pp_last
)->p_next
);
1168 *pp_last
= p_segments
;
1169 do { pp_last
= &((*pp_last
)->p_next
); } while ( *pp_last
!= NULL
);
1173 b_newregion
= !!p_region
->p_segments
;
1179 static void Eia608Init( eia608_t
*h
)
1181 memset( h
, 0, sizeof(*h
) );
1187 Eia608ClearScreen( h
, 0 );
1188 Eia608ClearScreen( h
, 1 );
1190 /* Cursor for writing text */
1191 h
->cursor
.i_column
= 0;
1192 h
->cursor
.i_row
= 0;
1196 h
->mode
= EIA608_MODE_POPUP
;
1197 h
->color
= EIA608_COLOR_DEFAULT
;
1198 h
->font
= EIA608_FONT_REGULAR
;
1199 h
->i_row_rollup
= EIA608_SCREEN_ROWS
-1;
1201 static eia608_status_t
Eia608Parse( eia608_t
*h
, int i_channel_selected
, const uint8_t data
[2] )
1203 const uint8_t d1
= data
[0] & 0x7f; /* Removed parity bit */
1204 const uint8_t d2
= data
[1] & 0x7f;
1205 eia608_status_t i_screen_status
= EIA608_STATUS_DEFAULT
;
1207 if( d1
== 0 && d2
== 0 )
1208 return EIA608_STATUS_DEFAULT
; /* Ignore padding (parity check are sometimes invalid on them) */
1210 Eia608ParseChannel( h
, data
);
1211 if( h
->i_channel
!= i_channel_selected
)
1213 //fprintf( stderr, "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC %x %x\n", data[0], data[1] );
1218 d1
!= h
->last
.d1
|| d2
!= h
->last
.d2
) /* Command codes can be repeated */
1219 i_screen_status
= Eia608ParseData( h
, d1
,d2
);
1224 else if( ( d1
>= 0x01 && d1
<= 0x0E ) || d1
== 0x0F )
1226 /* XDS block / End of XDS block */
1228 return i_screen_status
;