modules: use plane_SwapUV()
[vlc.git] / modules / codec / cc.c
blob7e987225317d1ce8ad3fc39a69abe900ab0a2bc3
1 /*****************************************************************************
2 * cc.c : CC 608/708 subtitles decoder
3 *****************************************************************************
4 * Copyright © 2007-2011 Laurent Aimar, VLC authors and VideoLAN
5 * 2011-2016 VLC authors and VideoLAN
6 * 2016-2017 VideoLabs, VLC authors and VideoLAN
8 * Authors: Laurent Aimar < fenrir # via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 /* The EIA 608 decoder part has been initialy based on ccextractor (GPL)
29 * and rewritten */
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
35 #include <assert.h>
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_codec.h>
40 #include <vlc_charset.h>
42 #include "substext.h"
43 #include "cea708.h"
45 /*****************************************************************************
46 * Module descriptor.
47 *****************************************************************************/
48 static int Open ( vlc_object_t * );
49 static void Close( vlc_object_t * );
51 #define OPAQUE_TEXT N_("Opacity")
52 #define OPAQUE_LONGTEXT N_("Setting to true " \
53 "makes the text to be boxed and maybe easier to read." )
55 vlc_module_begin ()
56 set_shortname( N_("CC 608/708"))
57 set_description( N_("Closed Captions decoder") )
58 set_capability( "spu decoder", 50 )
59 set_category( CAT_INPUT )
60 set_subcategory( SUBCAT_INPUT_SCODEC )
61 set_callbacks( Open, Close )
63 add_bool( "cc-opaque", true,
64 OPAQUE_TEXT, OPAQUE_LONGTEXT, false )
65 vlc_module_end ()
67 /*****************************************************************************
68 * Local prototypes
69 *****************************************************************************/
70 typedef enum
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,
77 EIA608_MODE_TEXT = 5
78 } eia608_mode_t;
80 typedef enum
82 EIA608_COLOR_WHITE = 0,
83 EIA608_COLOR_GREEN = 1,
84 EIA608_COLOR_BLUE = 2,
85 EIA608_COLOR_CYAN = 3,
86 EIA608_COLOR_RED = 4,
87 EIA608_COLOR_YELLOW = 5,
88 EIA608_COLOR_MAGENTA = 6,
89 EIA608_COLOR_USERDEFINED = 7
90 } eia608_color_t;
92 typedef enum
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
98 } eia608_font_t;
100 #define EIA608_SCREEN_ROWS 15
101 #define EIA608_SCREEN_COLUMNS 32
103 #define EIA608_MARGIN 0.10
104 #define EIA608_VISIBLE (1.0 - EIA608_MARGIN * 2)
105 #define FONT_TO_LINE_HEIGHT_RATIO 1.06
107 struct eia608_screen // A CC buffer
109 uint8_t characters[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1];
110 eia608_color_t colors[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1];
111 eia608_font_t fonts[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1]; // Extra char at the end for a 0
112 int row_used[EIA608_SCREEN_ROWS]; // Any data in row?
114 typedef struct eia608_screen eia608_screen;
116 typedef enum
118 EIA608_STATUS_DEFAULT = 0x00,
119 EIA608_STATUS_CHANGED = 0x01, /* current screen has been altered */
120 EIA608_STATUS_CAPTION_ENDED = 0x02, /* screen flip */
121 EIA608_STATUS_CAPTION_CLEARED = 0x04, /* active screen erased */
122 EIA608_STATUS_DISPLAY = EIA608_STATUS_CAPTION_CLEARED | EIA608_STATUS_CAPTION_ENDED,
123 } eia608_status_t;
125 static const struct {
126 eia608_color_t i_color;
127 eia608_font_t i_font;
128 int i_column;
129 } pac2_attribs[]= {
130 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 0 },
131 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 0 },
132 { EIA608_COLOR_GREEN, EIA608_FONT_REGULAR, 0 },
133 { EIA608_COLOR_GREEN, EIA608_FONT_UNDERLINE, 0 },
134 { EIA608_COLOR_BLUE, EIA608_FONT_REGULAR, 0 },
135 { EIA608_COLOR_BLUE, EIA608_FONT_UNDERLINE, 0 },
136 { EIA608_COLOR_CYAN, EIA608_FONT_REGULAR, 0 },
137 { EIA608_COLOR_CYAN, EIA608_FONT_UNDERLINE, 0 },
138 { EIA608_COLOR_RED, EIA608_FONT_REGULAR, 0 },
139 { EIA608_COLOR_RED, EIA608_FONT_UNDERLINE, 0 },
140 { EIA608_COLOR_YELLOW, EIA608_FONT_REGULAR, 0 },
141 { EIA608_COLOR_YELLOW, EIA608_FONT_UNDERLINE, 0 },
142 { EIA608_COLOR_MAGENTA, EIA608_FONT_REGULAR, 0 },
143 { EIA608_COLOR_MAGENTA, EIA608_FONT_UNDERLINE, 0 },
144 { EIA608_COLOR_WHITE, EIA608_FONT_ITALICS, 0 },
145 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE_ITALICS, 0 },
147 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 0 },
148 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 0 },
149 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 4 },
150 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 4 },
151 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 8 },
152 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 8 },
153 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 12 },
154 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 12 },
155 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 16 },
156 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 16 },
157 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 20 },
158 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 20 },
159 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 24 },
160 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 24 },
161 { EIA608_COLOR_WHITE, EIA608_FONT_REGULAR, 28 },
162 { EIA608_COLOR_WHITE, EIA608_FONT_UNDERLINE, 28 } ,
165 #define EIA608_COLOR_DEFAULT EIA608_COLOR_WHITE
167 static const int rgi_eia608_colors[] = {
168 0xffffff, // white
169 0x00ff00, // green
170 0x0000ff, // blue
171 0x00ffff, // cyan
172 0xff0000, // red
173 0xffff00, // yellow
174 0xff00ff, // magenta
175 0xffffff, // user defined XXX we use white
178 typedef struct
180 /* Current channel (used to reject packet without channel information) */
181 int i_channel;
183 /* */
184 int i_screen; /* Displayed screen */
185 eia608_screen screen[2];
187 struct
189 int i_row;
190 int i_column;
191 } cursor;
193 /* */
194 eia608_mode_t mode;
195 eia608_color_t color;
196 eia608_font_t font;
197 int i_row_rollup;
199 /* Last command pair (used to reject duplicated command) */
200 struct
202 uint8_t d1;
203 uint8_t d2;
204 } last;
205 } eia608_t;
207 static void Eia608Init( eia608_t * );
208 static eia608_status_t Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] );
209 static void Eia608FillUpdaterRegions( subpicture_updater_sys_t *p_updater, eia608_t *h );
211 /* It will be enough up to 63 B frames, which is far too high for
212 * broadcast environment */
213 #define CC_MAX_REORDER_SIZE (64)
214 struct decoder_sys_t
216 int i_queue;
217 block_t *p_queue;
219 int i_field;
220 int i_channel;
222 int i_reorder_depth;
224 cea708_demux_t *p_dtvcc;
226 cea708_t *p_cea708;
227 eia608_t *p_eia608;
228 bool b_opaque;
231 static int Decode( decoder_t *, block_t * );
232 static void Flush( decoder_t * );
234 static void DTVCC_ServiceData_Handler( void *priv, uint8_t i_sid, mtime_t i_time,
235 const uint8_t *p_data, size_t i_data )
237 decoder_t *p_dec = priv;
238 decoder_sys_t *p_sys = p_dec->p_sys;
239 //msg_Err( p_dec, "DTVCC_ServiceData_Handler sid %d bytes %ld", i_sid, i_data );
240 if( i_sid == 1 )
241 CEA708_Decoder_Push( p_sys->p_cea708, i_time, p_data, i_data );
244 /*****************************************************************************
245 * Open: probe the decoder and return score
246 *****************************************************************************
247 * Tries to launch a decoder and return score so that the interface is able
248 * to chose.
249 *****************************************************************************/
250 static int Open( vlc_object_t *p_this )
252 decoder_t *p_dec = (decoder_t*)p_this;
253 decoder_sys_t *p_sys;
255 if( ( p_dec->fmt_in.i_codec != VLC_CODEC_CEA608 ||
256 p_dec->fmt_in.subs.cc.i_channel > 3 ) &&
257 ( p_dec->fmt_in.i_codec != VLC_CODEC_CEA708 ||
258 p_dec->fmt_in.subs.cc.i_channel > 63 ) )
259 return VLC_EGENERIC;
261 p_dec->pf_decode = Decode;
262 p_dec->pf_flush = Flush;
264 /* Allocate the memory needed to store the decoder's structure */
265 p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
266 if( p_sys == NULL )
267 return VLC_ENOMEM;
269 if( p_dec->fmt_in.i_codec == VLC_CODEC_CEA608 )
271 /* 0 -> i_field = 0; i_channel = 1;
272 1 -> i_field = 0; i_channel = 2;
273 2 -> i_field = 1; i_channel = 1;
274 3 -> i_field = 1; i_channel = 2; */
275 p_sys->i_field = p_dec->fmt_in.subs.cc.i_channel >> 1;
276 p_sys->i_channel = 1 + (p_dec->fmt_in.subs.cc.i_channel & 1);
278 p_sys->p_eia608 = malloc(sizeof(*p_sys->p_eia608));
279 if( !p_sys->p_eia608 )
281 free( p_sys );
282 return VLC_ENOMEM;
284 Eia608Init( p_sys->p_eia608 );
286 else
288 p_sys->p_dtvcc = CEA708_DTVCC_Demuxer_New( p_dec, DTVCC_ServiceData_Handler );
289 if( !p_sys->p_dtvcc )
291 free( p_sys );
292 return VLC_ENOMEM;
295 p_sys->p_cea708 = CEA708_Decoder_New( p_dec );
296 if( !p_sys->p_cea708 )
298 CEA708_DTVCC_Demuxer_Release( p_sys->p_dtvcc );
299 free( p_sys );
300 return VLC_ENOMEM;
303 p_sys->i_channel = p_dec->fmt_in.subs.cc.i_channel;
306 p_sys->b_opaque = var_InheritBool( p_dec, "cc-opaque" );
307 p_sys->i_reorder_depth = p_dec->fmt_in.subs.cc.i_reorder_depth;
309 p_dec->fmt_out.i_codec = VLC_CODEC_TEXT;
311 return VLC_SUCCESS;
314 /*****************************************************************************
315 * Flush:
316 *****************************************************************************/
317 static void Flush( decoder_t *p_dec )
319 decoder_sys_t *p_sys = p_dec->p_sys;
321 if( p_sys->p_eia608 )
323 Eia608Init( p_sys->p_eia608 );
325 else
327 CEA708_DTVCC_Demuxer_Flush( p_sys->p_dtvcc );
328 CEA708_Decoder_Flush( p_sys->p_cea708 );
331 block_ChainRelease( p_sys->p_queue );
332 p_sys->p_queue = NULL;
333 p_sys->i_queue = 0;
336 /****************************************************************************
337 * Decode: the whole thing
338 ****************************************************************************
340 ****************************************************************************/
341 static void Push( decoder_t *, block_t * );
342 static block_t *Pop( decoder_t *, bool );
343 static void Convert( decoder_t *, mtime_t, const uint8_t *, size_t );
345 static bool DoDecode( decoder_t *p_dec, bool b_drain )
347 block_t *p_block = Pop( p_dec, b_drain );
348 if( !p_block )
349 return false;
351 Convert( p_dec, p_block->i_pts, p_block->p_buffer, p_block->i_buffer );
352 block_Release( p_block );
354 return true;
357 static int Decode( decoder_t *p_dec, block_t *p_block )
359 decoder_sys_t *p_sys = p_dec->p_sys;
361 if( p_block )
363 /* Reset decoder if needed */
364 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
366 /* Drain */
367 for( ; DoDecode( p_dec, true ) ; );
368 if( p_sys->p_eia608 )
370 Eia608Init( p_sys->p_eia608 );
372 else
374 CEA708_DTVCC_Demuxer_Flush( p_sys->p_dtvcc );
375 CEA708_Decoder_Flush( p_sys->p_cea708 );
378 if( (p_block->i_flags & BLOCK_FLAG_CORRUPTED) || p_block->i_buffer < 1 )
380 block_Release( p_block );
381 return VLCDEC_SUCCESS;
385 /* XXX Cc captions data are OUT OF ORDER (because we receive them in the bitstream
386 * order (ie ordered by video picture dts) instead of the display order.
387 * We will simulate a simple IPB buffer scheme
388 * and reorder with pts.
389 * XXX it won't work with H264 which use non out of order B picture or MMCO */
390 if( p_sys->i_reorder_depth == 0 )
392 /* Wait for a P and output all *previous* picture by pts order (for
393 * hierarchical B frames) */
394 if( (p_block->i_flags & BLOCK_FLAG_TYPE_B) == 0 )
395 for( ; DoDecode( p_dec, true ); );
398 Push( p_dec, p_block );
401 const bool b_no_reorder = (p_dec->fmt_in.subs.cc.i_reorder_depth < 0);
402 for( ; DoDecode( p_dec, (p_block == NULL) || b_no_reorder ); );
404 return VLCDEC_SUCCESS;
407 /*****************************************************************************
408 * CloseDecoder: clean up the decoder
409 *****************************************************************************/
410 static void Close( vlc_object_t *p_this )
412 decoder_t *p_dec = (decoder_t *)p_this;
413 decoder_sys_t *p_sys = p_dec->p_sys;
415 free( p_sys->p_eia608 );
416 if( p_sys->p_cea708 )
418 CEA708_Decoder_Release( p_sys->p_cea708 );
419 CEA708_DTVCC_Demuxer_Release( p_sys->p_dtvcc );
422 block_ChainRelease( p_sys->p_queue );
423 free( p_sys );
426 /*****************************************************************************
428 *****************************************************************************/
429 static void Push( decoder_t *p_dec, block_t *p_block )
431 decoder_sys_t *p_sys = p_dec->p_sys;
433 if( p_sys->i_queue >= CC_MAX_REORDER_SIZE )
435 block_Release( Pop( p_dec, true ) );
436 msg_Warn( p_dec, "Trashing a CC entry" );
439 block_t **pp_block;
440 /* find insertion point */
441 for( pp_block = &p_sys->p_queue; *pp_block ; pp_block = &((*pp_block)->p_next) )
443 if( p_block->i_pts == VLC_TS_INVALID || (*pp_block)->i_pts == VLC_TS_INVALID )
444 continue;
445 if( p_block->i_pts < (*pp_block)->i_pts )
447 if( p_sys->i_reorder_depth > 0 &&
448 p_sys->i_queue < p_sys->i_reorder_depth &&
449 pp_block == &p_sys->p_queue )
451 msg_Info( p_dec, "Increasing reorder depth to %d", ++p_sys->i_reorder_depth );
453 break;
456 /* Insert, keeping a pts and/or fifo ordered list */
457 p_block->p_next = *pp_block ? *pp_block : NULL;
458 *pp_block = p_block;
459 p_sys->i_queue++;
462 static block_t *Pop( decoder_t *p_dec, bool b_forced )
464 decoder_sys_t *p_sys = p_dec->p_sys;
465 block_t *p_block;
467 if( p_sys->i_queue == 0 )
468 return NULL;
470 if( !b_forced && p_sys->i_queue < CC_MAX_REORDER_SIZE )
472 if( p_sys->i_queue < p_sys->i_reorder_depth || p_sys->i_reorder_depth == 0 )
473 return NULL;
476 /* dequeue head */
477 p_block = p_sys->p_queue;
478 p_sys->p_queue = p_block->p_next;
479 p_block->p_next = NULL;
480 p_sys->i_queue--;
482 return p_block;
485 static subpicture_t *Subtitle( decoder_t *p_dec, eia608_t *h, mtime_t i_pts )
487 //decoder_sys_t *p_sys = p_dec->p_sys;
488 subpicture_t *p_spu = NULL;
490 /* We cannot display a subpicture with no date */
491 if( i_pts <= VLC_TS_INVALID )
492 return NULL;
494 /* Create the subpicture unit */
495 p_spu = decoder_NewSubpictureText( p_dec );
496 if( !p_spu )
497 return NULL;
499 p_spu->i_start = i_pts;
500 p_spu->i_stop = i_pts + 10000000; /* 10s max */
501 p_spu->b_ephemer = true;
502 p_spu->b_absolute = false;
504 subpicture_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
506 /* Set first region defaults */
507 /* The "leavetext" alignment is a special mode where the subpicture
508 region itself gets aligned, but the text inside it does not */
509 p_spu_sys->region.align = SUBPICTURE_ALIGN_TOP|SUBPICTURE_ALIGN_LEFT;
510 p_spu_sys->region.inner_align = SUBPICTURE_ALIGN_BOTTOM|SUBPICTURE_ALIGN_LEFT;
511 p_spu_sys->region.flags = UPDT_REGION_IGNORE_BACKGROUND | UPDT_REGION_USES_GRID_COORDINATES;
513 /* Set style defaults (will be added to segments if none set) */
514 p_spu_sys->p_default_style->i_style_flags |= STYLE_MONOSPACED;
515 if( p_dec->p_sys->b_opaque )
517 p_spu_sys->p_default_style->i_background_alpha = STYLE_ALPHA_OPAQUE >> 1;
518 p_spu_sys->p_default_style->i_features |= STYLE_HAS_BACKGROUND_ALPHA;
519 p_spu_sys->p_default_style->i_style_flags |= STYLE_BACKGROUND;
521 p_spu_sys->margin_ratio = EIA608_MARGIN;
522 p_spu_sys->p_default_style->i_font_color = rgi_eia608_colors[EIA608_COLOR_DEFAULT];
523 /* FCC defined "safe area" for EIA-608 captions is 80% of the height of the display */
524 p_spu_sys->p_default_style->f_font_relsize = EIA608_VISIBLE * 100 / EIA608_SCREEN_ROWS /
525 FONT_TO_LINE_HEIGHT_RATIO;
526 p_spu_sys->p_default_style->i_features |= (STYLE_HAS_FONT_COLOR | STYLE_HAS_FLAGS);
528 Eia608FillUpdaterRegions( p_spu_sys, h );
530 return p_spu;
533 static void Convert( decoder_t *p_dec, mtime_t i_pts,
534 const uint8_t *p_buffer, size_t i_buffer )
536 decoder_sys_t *p_sys = p_dec->p_sys;
538 unsigned i_ticks = 0;
539 while( i_buffer >= 3 )
541 if( (p_buffer[0] & 0x04) /* Valid bit */ )
543 const mtime_t i_spupts = i_pts + i_ticks * CLOCK_FREQ / 30;
544 /* Mask off the specific i_field bit, else some sequences can be lost. */
545 if ( p_sys->p_eia608 &&
546 (p_buffer[0] & 0x03) == p_sys->i_field )
548 eia608_status_t i_status = Eia608Parse( p_sys->p_eia608,
549 p_sys->i_channel, &p_buffer[1] );
551 /* a caption is ready or removed, process its screen */
553 * In case of rollup/painton with 1 packet/frame, we need
554 * to update on Changed status.
555 * Batch decoding might be incorrect if those in
556 * large number of commands (mp4, ...) then.
557 * see CEAv1.2zero.trp tests */
558 if( i_status & (EIA608_STATUS_DISPLAY | EIA608_STATUS_CHANGED) )
560 subpicture_t *p_spu = Subtitle( p_dec, p_sys->p_eia608, i_spupts );
561 if( p_spu )
562 decoder_QueueSub( p_dec, p_spu );
565 else if( p_sys->p_cea708 && (p_buffer[0] & 0x03) >= 2 )
567 CEA708_DTVCC_Demuxer_Push( p_sys->p_dtvcc, i_spupts, p_buffer );
571 i_ticks++;
573 i_buffer -= 3;
574 p_buffer += 3;
579 /*****************************************************************************
581 *****************************************************************************/
582 static void Eia608Cursor( eia608_t *h, int dx )
584 h->cursor.i_column += dx;
585 if( h->cursor.i_column < 0 )
586 h->cursor.i_column = 0;
587 else if( h->cursor.i_column > EIA608_SCREEN_COLUMNS-1 )
588 h->cursor.i_column = EIA608_SCREEN_COLUMNS-1;
590 static void Eia608ClearScreenRowX( eia608_t *h, int i_screen, int i_row, int x )
592 eia608_screen *screen = &h->screen[i_screen];
594 if( x == 0 )
596 screen->row_used[i_row] = false;
598 else
600 screen->row_used[i_row] = false;
601 for( int i = 0; i < x; i++ )
603 if( screen->characters[i_row][i] != ' ' ||
604 screen->colors[i_row][i] != EIA608_COLOR_DEFAULT ||
605 screen->fonts[i_row][i] != EIA608_FONT_REGULAR )
607 screen->row_used[i_row] = true;
608 break;
613 for( ; x < EIA608_SCREEN_COLUMNS+1; x++ )
615 screen->characters[i_row][x] = x < EIA608_SCREEN_COLUMNS ? ' ' : '\0';
616 screen->colors[i_row][x] = EIA608_COLOR_DEFAULT;
617 screen->fonts[i_row][x] = EIA608_FONT_REGULAR;
621 static void Eia608ClearScreenRow( eia608_t *h, int i_screen, int i_row )
623 Eia608ClearScreenRowX( h, i_screen, i_row, 0 );
626 static void Eia608ClearScreen( eia608_t *h, int i_screen )
628 for( int i = 0; i < EIA608_SCREEN_ROWS; i++ )
629 Eia608ClearScreenRow( h, i_screen, i );
632 static int Eia608GetWritingScreenIndex( eia608_t *h )
634 switch( h->mode )
636 case EIA608_MODE_POPUP: // Non displayed screen
637 return 1 - h->i_screen;
639 case EIA608_MODE_ROLLUP_2: // Displayed screen
640 case EIA608_MODE_ROLLUP_3:
641 case EIA608_MODE_ROLLUP_4:
642 case EIA608_MODE_PAINTON:
643 return h->i_screen;
644 default:
645 /* It cannot happen, else it is a bug */
646 vlc_assert_unreachable();
647 return 0;
651 static void Eia608EraseScreen( eia608_t *h, bool b_displayed )
653 Eia608ClearScreen( h, b_displayed ? h->i_screen : (1-h->i_screen) );
656 static void Eia608Write( eia608_t *h, const uint8_t c )
658 const int i_row = h->cursor.i_row;
659 const int i_column = h->cursor.i_column;
660 eia608_screen *screen;
662 if( h->mode == EIA608_MODE_TEXT )
663 return;
665 screen = &h->screen[Eia608GetWritingScreenIndex( h )];
667 screen->characters[i_row][i_column] = c;
668 screen->colors[i_row][i_column] = h->color;
669 screen->fonts[i_row][i_column] = h->font;
670 screen->row_used[i_row] = true;
671 Eia608Cursor( h, 1 );
673 static void Eia608Erase( eia608_t *h )
675 const int i_row = h->cursor.i_row;
676 const int i_column = h->cursor.i_column - 1;
677 eia608_screen *screen;
679 if( h->mode == EIA608_MODE_TEXT )
680 return;
681 if( i_column < 0 )
682 return;
684 screen = &h->screen[Eia608GetWritingScreenIndex( h )];
686 /* FIXME do we need to reset row_used/colors/font ? */
687 screen->characters[i_row][i_column] = ' ';
688 Eia608Cursor( h, -1 );
690 static void Eia608EraseToEndOfRow( eia608_t *h )
692 if( h->mode == EIA608_MODE_TEXT )
693 return;
695 Eia608ClearScreenRowX( h, Eia608GetWritingScreenIndex( h ), h->cursor.i_row, h->cursor.i_column );
698 static void Eia608RollUp( eia608_t *h )
700 if( h->mode == EIA608_MODE_TEXT )
701 return;
703 const int i_screen = Eia608GetWritingScreenIndex( h );
704 eia608_screen *screen = &h->screen[i_screen];
706 int keep_lines;
708 /* Window size */
709 if( h->mode == EIA608_MODE_ROLLUP_2 )
710 keep_lines = 2;
711 else if( h->mode == EIA608_MODE_ROLLUP_3 )
712 keep_lines = 3;
713 else if( h->mode == EIA608_MODE_ROLLUP_4 )
714 keep_lines = 4;
715 else
716 return;
718 /* Reset the cursor */
719 h->cursor.i_column = 0;
721 /* Erase lines above our window */
722 for( int i = 0; i < h->cursor.i_row - keep_lines; i++ )
723 Eia608ClearScreenRow( h, i_screen, i );
725 /* Move up */
726 for( int i = 0; i < keep_lines-1; i++ )
728 const int i_row = h->cursor.i_row - keep_lines + i + 1;
729 if( i_row < 0 )
730 continue;
731 assert( i_row+1 < EIA608_SCREEN_ROWS );
732 memcpy( screen->characters[i_row], screen->characters[i_row+1], sizeof(*screen->characters) );
733 memcpy( screen->colors[i_row], screen->colors[i_row+1], sizeof(*screen->colors) );
734 memcpy( screen->fonts[i_row], screen->fonts[i_row+1], sizeof(*screen->fonts) );
735 screen->row_used[i_row] = screen->row_used[i_row+1];
737 /* Reset current row */
738 Eia608ClearScreenRow( h, i_screen, h->cursor.i_row );
740 static void Eia608ParseChannel( eia608_t *h, const uint8_t d[2] )
742 /* Check odd parity */
743 static const int p4[16] = {
744 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
746 if( p4[d[0] & 0xf] == p4[d[0] >> 4] ||
747 p4[d[1] & 0xf] == p4[ d[1] >> 4] )
749 h->i_channel = -1;
750 return;
753 /* */
754 const int d1 = d[0] & 0x7f;
755 if( d1 >= 0x10 && d1 <= 0x1f )
756 h->i_channel = 1 + ((d1 & 0x08) != 0);
757 else if( d1 < 0x10 )
758 h->i_channel = 3;
760 static eia608_status_t Eia608ParseTextAttribute( eia608_t *h, uint8_t d2 )
762 const int i_index = d2 - 0x20;
763 assert( d2 >= 0x20 && d2 <= 0x2f );
765 h->color = pac2_attribs[i_index].i_color;
766 h->font = pac2_attribs[i_index].i_font;
767 Eia608Cursor( h, 1 );
769 return EIA608_STATUS_DEFAULT;
771 static eia608_status_t Eia608ParseSingle( eia608_t *h, const uint8_t dx )
773 assert( dx >= 0x20 );
774 Eia608Write( h, dx );
775 return EIA608_STATUS_CHANGED;
777 static eia608_status_t Eia608ParseDouble( eia608_t *h, uint8_t d2 )
779 assert( d2 >= 0x30 && d2 <= 0x3f );
780 Eia608Write( h, d2 + 0x50 ); /* We use charaters 0x80...0x8f */
781 return EIA608_STATUS_CHANGED;
783 static eia608_status_t Eia608ParseExtended( eia608_t *h, uint8_t d1, uint8_t d2 )
785 assert( d2 >= 0x20 && d2 <= 0x3f );
786 assert( d1 == 0x12 || d1 == 0x13 );
787 if( d1 == 0x12 )
788 d2 += 0x70; /* We use charaters 0x90-0xaf */
789 else
790 d2 += 0x90; /* We use charaters 0xb0-0xcf */
792 /* The extended characters replace the previous one with a more
793 * advanced one */
794 Eia608Cursor( h, -1 );
795 Eia608Write( h, d2 );
796 return EIA608_STATUS_CHANGED;
798 static eia608_status_t Eia608ParseCommand0x14( eia608_t *h, uint8_t d2 )
800 eia608_status_t i_status = EIA608_STATUS_DEFAULT;
802 switch( d2 )
804 case 0x20: /* Resume caption loading */
805 h->mode = EIA608_MODE_POPUP;
806 break;
807 case 0x21: /* Backspace */
808 Eia608Erase( h );
809 i_status = EIA608_STATUS_CHANGED;
810 break;
811 case 0x22: /* Reserved */
812 case 0x23:
813 break;
814 case 0x24: /* Delete to end of row */
815 Eia608EraseToEndOfRow( h );
816 break;
817 case 0x25: /* Rollup 2 */
818 case 0x26: /* Rollup 3 */
819 case 0x27: /* Rollup 4 */
820 if( h->mode == EIA608_MODE_POPUP || h->mode == EIA608_MODE_PAINTON )
822 Eia608EraseScreen( h, true );
823 Eia608EraseScreen( h, false );
824 i_status = EIA608_STATUS_CHANGED | EIA608_STATUS_CAPTION_CLEARED;
827 if( d2 == 0x25 )
828 h->mode = EIA608_MODE_ROLLUP_2;
829 else if( d2 == 0x26 )
830 h->mode = EIA608_MODE_ROLLUP_3;
831 else
832 h->mode = EIA608_MODE_ROLLUP_4;
834 h->cursor.i_column = 0;
835 h->cursor.i_row = h->i_row_rollup;
836 break;
837 case 0x28: /* Flash on */
838 /* TODO */
839 break;
840 case 0x29: /* Resume direct captionning */
841 h->mode = EIA608_MODE_PAINTON;
842 break;
843 case 0x2a: /* Text restart */
844 /* TODO */
845 break;
847 case 0x2b: /* Resume text display */
848 h->mode = EIA608_MODE_TEXT;
849 break;
851 case 0x2c: /* Erase displayed memory */
852 Eia608EraseScreen( h, true );
853 i_status = EIA608_STATUS_CHANGED | EIA608_STATUS_CAPTION_CLEARED;
854 break;
855 case 0x2d: /* Carriage return */
856 Eia608RollUp(h);
857 i_status = EIA608_STATUS_CHANGED;
858 break;
859 case 0x2e: /* Erase non displayed memory */
860 Eia608EraseScreen( h, false );
861 break;
862 case 0x2f: /* End of caption (flip screen if not paint on) */
863 if( h->mode != EIA608_MODE_PAINTON )
864 h->i_screen = 1 - h->i_screen;
865 h->mode = EIA608_MODE_POPUP;
866 h->cursor.i_column = 0;
867 h->cursor.i_row = 0;
868 h->color = EIA608_COLOR_DEFAULT;
869 h->font = EIA608_FONT_REGULAR;
870 i_status = EIA608_STATUS_CHANGED | EIA608_STATUS_CAPTION_ENDED;
871 break;
873 return i_status;
875 static bool Eia608ParseCommand0x17( eia608_t *h, uint8_t d2 )
877 switch( d2 )
879 case 0x21: /* Tab offset 1 */
880 Eia608Cursor( h, 1 );
881 break;
882 case 0x22: /* Tab offset 2 */
883 Eia608Cursor( h, 2 );
884 break;
885 case 0x23: /* Tab offset 3 */
886 Eia608Cursor( h, 3 );
887 break;
889 return false;
891 static bool Eia608ParsePac( eia608_t *h, uint8_t d1, uint8_t d2 )
893 static const int pi_row[] = {
894 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
896 const int i_row_index = ( (d1<<1) & 0x0e) | ( (d2>>5) & 0x01 );
898 assert( d2 >= 0x40 && d2 <= 0x7f );
900 if( pi_row[i_row_index] <= 0 )
901 return false;
903 /* Row */
904 if( h->mode != EIA608_MODE_TEXT )
905 h->cursor.i_row = pi_row[i_row_index] - 1;
906 h->i_row_rollup = pi_row[i_row_index] - 1;
907 /* Column */
908 if( d2 >= 0x60 )
909 d2 -= 0x60;
910 else if( d2 >= 0x40 )
911 d2 -= 0x40;
912 h->cursor.i_column = pac2_attribs[d2].i_column;
913 h->color = pac2_attribs[d2].i_color;
914 h->font = pac2_attribs[d2].i_font;
916 return false;
919 static eia608_status_t Eia608ParseData( eia608_t *h, uint8_t d1, uint8_t d2 )
921 eia608_status_t i_status = EIA608_STATUS_DEFAULT;
923 if( d1 >= 0x18 && d1 <= 0x1f )
924 d1 -= 8;
926 #define ON( d2min, d2max, cmd ) do { if( d2 >= d2min && d2 <= d2max ) i_status = cmd; } while(0)
927 switch( d1 )
929 case 0x11:
930 ON( 0x20, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
931 ON( 0x30, 0x3f, Eia608ParseDouble( h, d2 ) );
932 break;
933 case 0x12: case 0x13:
934 ON( 0x20, 0x3f, Eia608ParseExtended( h, d1, d2 ) );
935 break;
936 case 0x14: case 0x15:
937 ON( 0x20, 0x2f, Eia608ParseCommand0x14( h, d2 ) );
938 break;
939 case 0x17:
940 ON( 0x21, 0x23, Eia608ParseCommand0x17( h, d2 ) );
941 ON( 0x2e, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
942 break;
944 if( d1 == 0x10 )
945 ON( 0x40, 0x5f, Eia608ParsePac( h, d1, d2 ) );
946 else if( d1 >= 0x11 && d1 <= 0x17 )
947 ON( 0x40, 0x7f, Eia608ParsePac( h, d1, d2 ) );
948 #undef ON
949 if( d1 >= 0x20 )
951 i_status = Eia608ParseSingle( h, d1 );
952 if( d2 >= 0x20 )
953 i_status |= Eia608ParseSingle( h, d2 );
956 /* Ignore changes occuring to doublebuffer */
957 if( h->mode == EIA608_MODE_POPUP && i_status == EIA608_STATUS_CHANGED )
958 i_status = EIA608_STATUS_DEFAULT;
960 return i_status;
963 static void Eia608TextUtf8( char *psz_utf8, uint8_t c ) // Returns number of bytes used
965 #define E1(c,u) { c, { u, '\0' } }
966 #define E2(c,u1,u2) { c, { u1, u2, '\0' } }
967 #define E3(c,u1,u2,u3) { c, { u1, u2, u3, '\0' } }
968 static const struct {
969 uint8_t c;
970 char utf8[3+1];
971 } c2utf8[] = {
972 // Regular line-21 character set, mostly ASCII except these exceptions
973 E2( 0x2a, 0xc3,0xa1), // lowercase a, acute accent
974 E2( 0x5c, 0xc3,0xa9), // lowercase e, acute accent
975 E2( 0x5e, 0xc3,0xad), // lowercase i, acute accent
976 E2( 0x5f, 0xc3,0xb3), // lowercase o, acute accent
977 E2( 0x60, 0xc3,0xba), // lowercase u, acute accent
978 E2( 0x7b, 0xc3,0xa7), // lowercase c with cedilla
979 E2( 0x7c, 0xc3,0xb7), // division symbol
980 E2( 0x7d, 0xc3,0x91), // uppercase N tilde
981 E2( 0x7e, 0xc3,0xb1), // lowercase n tilde
982 // THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
983 // THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F
984 E2( 0x80, 0xc2,0xae), // Registered symbol (R)
985 E2( 0x81, 0xc2,0xb0), // degree sign
986 E2( 0x82, 0xc2,0xbd), // 1/2 symbol
987 E2( 0x83, 0xc2,0xbf), // Inverted (open) question mark
988 E3( 0x84, 0xe2,0x84,0xa2), // Trademark symbol (TM)
989 E2( 0x85, 0xc2,0xa2), // Cents symbol
990 E2( 0x86, 0xc2,0xa3), // Pounds sterling
991 E3( 0x87, 0xe2,0x99,0xaa), // Music note
992 E2( 0x88, 0xc3,0xa0), // lowercase a, grave accent
993 E2( 0x89, 0xc2,0xa0), // transparent space
994 E2( 0x8a, 0xc3,0xa8), // lowercase e, grave accent
995 E2( 0x8b, 0xc3,0xa2), // lowercase a, circumflex accent
996 E2( 0x8c, 0xc3,0xaa), // lowercase e, circumflex accent
997 E2( 0x8d, 0xc3,0xae), // lowercase i, circumflex accent
998 E2( 0x8e, 0xc3,0xb4), // lowercase o, circumflex accent
999 E2( 0x8f, 0xc3,0xbb), // lowercase u, circumflex accent
1000 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
1001 // THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F
1002 E2( 0x90, 0xc3,0x81), // capital letter A with acute
1003 E2( 0x91, 0xc3,0x89), // capital letter E with acute
1004 E2( 0x92, 0xc3,0x93), // capital letter O with acute
1005 E2( 0x93, 0xc3,0x9a), // capital letter U with acute
1006 E2( 0x94, 0xc3,0x9c), // capital letter U with diaresis
1007 E2( 0x95, 0xc3,0xbc), // lowercase letter U with diaeresis
1008 E1( 0x96, 0x27), // apostrophe
1009 E2( 0x97, 0xc2,0xa1), // inverted exclamation mark
1010 E1( 0x98, 0x2a), // asterisk
1011 E1( 0x99, 0x27), // apostrophe (yes, duped). See CCADI source code.
1012 E1( 0x9a, 0x2d), // hyphen-minus
1013 E2( 0x9b, 0xc2,0xa9), // copyright sign
1014 E3( 0x9c, 0xe2,0x84,0xa0), // Service mark
1015 E1( 0x9d, 0x2e), // Full stop (.)
1016 E3( 0x9e, 0xe2,0x80,0x9c), // Quotation mark
1017 E3( 0x9f, 0xe2,0x80,0x9d), // Quotation mark
1018 E2( 0xa0, 0xc3,0x80), // uppercase A, grave accent
1019 E2( 0xa1, 0xc3,0x82), // uppercase A, circumflex
1020 E2( 0xa2, 0xc3,0x87), // uppercase C with cedilla
1021 E2( 0xa3, 0xc3,0x88), // uppercase E, grave accent
1022 E2( 0xa4, 0xc3,0x8a), // uppercase E, circumflex
1023 E2( 0xa5, 0xc3,0x8b), // capital letter E with diaresis
1024 E2( 0xa6, 0xc3,0xab), // lowercase letter e with diaresis
1025 E2( 0xa7, 0xc3,0x8e), // uppercase I, circumflex
1026 E2( 0xa8, 0xc3,0x8f), // uppercase I, with diaresis
1027 E2( 0xa9, 0xc3,0xaf), // lowercase i, with diaresis
1028 E2( 0xaa, 0xc3,0x94), // uppercase O, circumflex
1029 E2( 0xab, 0xc3,0x99), // uppercase U, grave accent
1030 E2( 0xac, 0xc3,0xb9), // lowercase u, grave accent
1031 E2( 0xad, 0xc3,0x9b), // uppercase U, circumflex
1032 E2( 0xae, 0xc2,0xab), // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
1033 E2( 0xaf, 0xc2,0xbb), // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
1034 // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
1035 // THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F
1036 E2( 0xb0, 0xc3,0x83), // Uppercase A, tilde
1037 E2( 0xb1, 0xc3,0xa3), // Lowercase a, tilde
1038 E2( 0xb2, 0xc3,0x8d), // Uppercase I, acute accent
1039 E2( 0xb3, 0xc3,0x8c), // Uppercase I, grave accent
1040 E2( 0xb4, 0xc3,0xac), // Lowercase i, grave accent
1041 E2( 0xb5, 0xc3,0x92), // Uppercase O, grave accent
1042 E2( 0xb6, 0xc3,0xb2), // Lowercase o, grave accent
1043 E2( 0xb7, 0xc3,0x95), // Uppercase O, tilde
1044 E2( 0xb8, 0xc3,0xb5), // Lowercase o, tilde
1045 E1( 0xb9, 0x7b), // Open curly brace
1046 E1( 0xba, 0x7d), // Closing curly brace
1047 E1( 0xbb, 0x5c), // Backslash
1048 E1( 0xbc, 0x5e), // Caret
1049 E1( 0xbd, 0x5f), // Underscore
1050 E2( 0xbe, 0xc2,0xa6), // Pipe (broken bar)
1051 E1( 0xbf, 0x7e), // Tilde (utf8 code unsure)
1052 E2( 0xc0, 0xc3,0x84), // Uppercase A, umlaut
1053 E2( 0xc1, 0xc3,0xa4), // Lowercase A, umlaut
1054 E2( 0xc2, 0xc3,0x96), // Uppercase O, umlaut
1055 E2( 0xc3, 0xc3,0xb6), // Lowercase o, umlaut
1056 E2( 0xc4, 0xc3,0x9f), // Esszett (sharp S)
1057 E2( 0xc5, 0xc2,0xa5), // Yen symbol
1058 E2( 0xc6, 0xc2,0xa4), // Currency symbol
1059 E1( 0xc7, 0x7c), // Vertical bar
1060 E2( 0xc8, 0xc3,0x85), // Uppercase A, ring
1061 E2( 0xc9, 0xc3,0xa5), // Lowercase A, ring
1062 E2( 0xca, 0xc3,0x98), // Uppercase O, slash
1063 E2( 0xcb, 0xc3,0xb8), // Lowercase o, slash
1064 E3( 0xcc, 0xe2,0x8c,0x9c), // Upper left corner
1065 E3( 0xcd, 0xe2,0x8c,0x9d), // Upper right corner
1066 E3( 0xce, 0xe2,0x8c,0x9e), // Lower left corner
1067 E3( 0xcf, 0xe2,0x8c,0x9f), // Lower right corner
1069 E1(0,0)
1071 #undef E3
1072 #undef E2
1073 #undef E1
1075 for( size_t i = 0; i < ARRAY_SIZE(c2utf8) ; i++ )
1076 if( c2utf8[i].c == c ) {
1077 strcpy( psz_utf8, c2utf8[i].utf8 );
1078 return;
1081 psz_utf8[0] = c < 0x80 ? c : '?'; /* Normal : Unsupported */
1082 psz_utf8[1] = '\0';
1085 static void Eia608Strlcat( char *d, const char *s, int i_max )
1087 if( i_max > 1 )
1088 strncat( d, s, i_max-1 - strnlen(d, i_max-1));
1089 if( i_max > 0 )
1090 d[i_max-1] = '\0';
1093 #define CAT(t) Eia608Strlcat( psz_text, t, i_text_max )
1095 static text_segment_t * Eia608TextLine( struct eia608_screen *screen, int i_row )
1097 const uint8_t *p_char = screen->characters[i_row];
1098 const eia608_color_t *p_color = screen->colors[i_row];
1099 const eia608_font_t *p_font = screen->fonts[i_row];
1100 int i_start;
1101 int i_end;
1102 int x;
1103 eia608_color_t prev_color = EIA608_COLOR_DEFAULT;
1104 eia608_font_t prev_font = EIA608_FONT_REGULAR;
1106 char utf8[4];
1107 const unsigned i_text_max = 4 * EIA608_SCREEN_COLUMNS + 1;
1108 char psz_text[i_text_max + 1];
1109 psz_text[0] = '\0';
1111 /* Search the start */
1112 i_start = 0;
1114 /* Convert leading spaces to non-breaking so that they don't get
1115 stripped by the RenderHtml routine as regular whitespace */
1116 while( i_start < EIA608_SCREEN_COLUMNS && p_char[i_start] == ' ' ) {
1117 Eia608TextUtf8( utf8, 0x89 );
1118 CAT( utf8 );
1119 i_start++;
1122 /* Search the end */
1123 i_end = EIA608_SCREEN_COLUMNS-1;
1124 while( i_end > i_start && p_char[i_end] == ' ' )
1125 i_end--;
1127 /* */
1128 if( i_start > i_end ) /* Nothing to render */
1129 return NULL;
1131 text_segment_t *p_segment, *p_segments_head = p_segment = text_segment_New( NULL );
1132 if(!p_segment)
1133 return NULL;
1135 p_segment->style = text_style_Create( STYLE_NO_DEFAULTS );
1136 if(!p_segment->style)
1138 text_segment_Delete(p_segment);
1139 return NULL;
1141 /* Ensure we get a monospaced font (required for accurate positioning */
1142 p_segment->style->i_style_flags |= STYLE_MONOSPACED;
1144 for( x = i_start; x <= i_end; x++ )
1146 eia608_color_t color = p_color[x];
1147 eia608_font_t font = p_font[x];
1149 if(font != prev_font || color != prev_color)
1151 EnsureUTF8(psz_text);
1152 p_segment->psz_text = strdup(psz_text);
1153 psz_text[0] = '\0';
1154 p_segment->p_next = text_segment_New( NULL );
1155 p_segment = p_segment->p_next;
1156 if(!p_segment)
1157 return p_segments_head;
1159 p_segment->style = text_style_Create( STYLE_NO_DEFAULTS );
1160 if(!p_segment->style)
1162 text_segment_Delete(p_segment);
1163 return p_segments_head;
1165 p_segment->style->i_style_flags |= STYLE_MONOSPACED;
1167 /* start segment with new style */
1168 if(font & EIA608_FONT_ITALICS)
1170 p_segment->style->i_style_flags |= STYLE_ITALIC;
1171 p_segment->style->i_features |= STYLE_HAS_FLAGS;
1173 if(font & EIA608_FONT_UNDERLINE)
1175 p_segment->style->i_style_flags |= STYLE_UNDERLINE;
1176 p_segment->style->i_features |= STYLE_HAS_FLAGS;
1179 if(color != EIA608_COLOR_DEFAULT)
1181 p_segment->style->i_font_color = rgi_eia608_colors[color];
1182 p_segment->style->i_features |= STYLE_HAS_FONT_COLOR;
1186 Eia608TextUtf8( utf8, p_char[x] );
1187 CAT( utf8 );
1189 /* */
1190 prev_font = font;
1191 prev_color = color;
1194 #undef CAT
1196 if( p_segment )
1198 assert(!p_segment->psz_text); // shouldn't happen
1199 EnsureUTF8(psz_text);
1200 p_segment->psz_text = strdup(psz_text);
1203 return p_segments_head;
1206 static void Eia608FillUpdaterRegions( subpicture_updater_sys_t *p_updater, eia608_t *h )
1208 struct eia608_screen *screen = &h->screen[h->i_screen];
1209 subpicture_updater_sys_region_t *p_region = &p_updater->region;
1210 text_segment_t **pp_last = &p_region->p_segments;
1211 bool b_newregion = false;
1213 for( int i = 0; i < EIA608_SCREEN_ROWS; i++ )
1215 if( !screen->row_used[i] )
1216 continue;
1218 text_segment_t *p_segments = Eia608TextLine( screen, i );
1219 if( p_segments )
1221 if( b_newregion )
1223 subpicture_updater_sys_region_t *p_newregion;
1224 p_newregion = SubpictureUpdaterSysRegionNew();
1225 if( !p_newregion )
1227 text_segment_ChainDelete( p_segments );
1228 return;
1230 /* Copy defaults */
1231 p_newregion->align = p_region->align;
1232 p_newregion->inner_align = p_region->inner_align;
1233 p_newregion->flags = p_region->flags;
1234 SubpictureUpdaterSysRegionAdd( p_region, p_newregion );
1235 p_region = p_newregion;
1236 pp_last = &p_region->p_segments;
1237 b_newregion = false;
1240 if( p_region->p_segments == NULL ) /* First segment in the [new] region */
1242 p_region->origin.y = (float) i /* start line number */
1243 / (EIA608_SCREEN_ROWS * FONT_TO_LINE_HEIGHT_RATIO);
1244 p_region->flags |= UPDT_REGION_ORIGIN_Y_IS_RATIO;
1246 else /* Insert line break between region lines */
1248 *pp_last = text_segment_New( "\n" );
1249 if( *pp_last )
1250 pp_last = &((*pp_last)->p_next);
1253 *pp_last = p_segments;
1254 do { pp_last = &((*pp_last)->p_next); } while ( *pp_last != NULL );
1256 else
1258 b_newregion = !!p_region->p_segments;
1263 /* */
1264 static void Eia608Init( eia608_t *h )
1266 memset( h, 0, sizeof(*h) );
1268 /* */
1269 h->i_channel = -1;
1271 h->i_screen = 0;
1272 Eia608ClearScreen( h, 0 );
1273 Eia608ClearScreen( h, 1 );
1275 /* Cursor for writing text */
1276 h->cursor.i_column = 0;
1277 h->cursor.i_row = 0;
1279 h->last.d1 = 0x00;
1280 h->last.d2 = 0x00;
1281 h->mode = EIA608_MODE_POPUP;
1282 h->color = EIA608_COLOR_DEFAULT;
1283 h->font = EIA608_FONT_REGULAR;
1284 h->i_row_rollup = EIA608_SCREEN_ROWS-1;
1286 static eia608_status_t Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] )
1288 const uint8_t d1 = data[0] & 0x7f; /* Removed parity bit */
1289 const uint8_t d2 = data[1] & 0x7f;
1290 eia608_status_t i_screen_status = EIA608_STATUS_DEFAULT;
1292 if( d1 == 0 && d2 == 0 )
1293 return EIA608_STATUS_DEFAULT; /* Ignore padding (parity check are sometimes invalid on them) */
1295 Eia608ParseChannel( h, data );
1296 if( h->i_channel != i_channel_selected )
1297 return false;
1298 //fprintf( stderr, "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC %x %x\n", data[0], data[1] );
1300 if( d1 >= 0x10 )
1302 if( d1 >= 0x20 ||
1303 d1 != h->last.d1 || d2 != h->last.d2 ) /* Command codes can be repeated */
1304 i_screen_status = Eia608ParseData( h, d1,d2 );
1306 h->last.d1 = d1;
1307 h->last.d2 = d2;
1309 else if( ( d1 >= 0x01 && d1 <= 0x0E ) || d1 == 0x0F )
1311 /* XDS block / End of XDS block */
1313 return i_screen_status;