]> git.sesse.net Git - vlc/blob - modules/codec/cc.c
upnp: change item b_net and i_type
[vlc] / modules / codec / cc.c
1 /*****************************************************************************
2  * cc.c : CC 608/708 subtitles decoder
3  *****************************************************************************
4  * Copyright © 2007-2010 Laurent Aimar, 2011 VLC authors and VideoLAN
5  *
6  * Authors: Laurent Aimar < fenrir # via.ecp.fr>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 /* The EIA 608 decoder part has been initialy based on ccextractor (GPL)
27  * and rewritten */
28
29 /* TODO:
30  *  On discontinuity reset the decoder state
31  *  Check parity
32  *  708 decoding
33  */
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <assert.h>
40
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
43 #include <vlc_codec.h>
44 #include <vlc_charset.h>
45
46 #include "substext.h"
47
48 /*****************************************************************************
49  * Module descriptor.
50  *****************************************************************************/
51 static int  Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
53
54 vlc_module_begin ()
55     set_shortname( N_("CC 608/708"))
56     set_description( N_("Closed Captions decoder") )
57     set_capability( "decoder", 50 )
58     set_callbacks( Open, Close )
59 vlc_module_end ()
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 typedef enum
65 {
66     EIA608_MODE_POPUP = 0,
67     EIA608_MODE_ROLLUP_2 = 1,
68     EIA608_MODE_ROLLUP_3 = 2,
69     EIA608_MODE_ROLLUP_4 = 3,
70     EIA608_MODE_PAINTON = 4,
71     EIA608_MODE_TEXT = 5
72 } eia608_mode_t;
73
74 typedef enum
75 {
76     EIA608_COLOR_WHITE = 0,
77     EIA608_COLOR_GREEN = 1,
78     EIA608_COLOR_BLUE = 2,
79     EIA608_COLOR_CYAN = 3,
80     EIA608_COLOR_RED = 4,
81     EIA608_COLOR_YELLOW = 5,
82     EIA608_COLOR_MAGENTA = 6,
83     EIA608_COLOR_USERDEFINED = 7
84 } eia608_color_t;
85
86 typedef enum
87 {
88     EIA608_FONT_REGULAR    = 0x00,
89     EIA608_FONT_ITALICS    = 0x01,
90     EIA608_FONT_UNDERLINE  = 0x02,
91     EIA608_FONT_UNDERLINE_ITALICS = EIA608_FONT_UNDERLINE | EIA608_FONT_ITALICS
92 } eia608_font_t;
93
94 #define EIA608_SCREEN_ROWS 15
95 #define EIA608_SCREEN_COLUMNS 32
96
97 struct eia608_screen // A CC buffer
98 {
99     uint8_t characters[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1];
100     eia608_color_t colors[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1];
101     eia608_font_t fonts[EIA608_SCREEN_ROWS][EIA608_SCREEN_COLUMNS+1]; // Extra char at the end for a 0
102     int row_used[EIA608_SCREEN_ROWS]; // Any data in row?
103 };
104 typedef struct eia608_screen eia608_screen;
105
106 typedef struct
107 {
108     /* Current channel (used to reject packet without channel information) */
109     int i_channel;
110
111     /* */
112     int           i_screen; /* Displayed screen */
113     eia608_screen screen[2];
114
115     struct
116     {
117         int i_row;
118         int i_column;
119     } cursor;
120
121     /* */
122     eia608_mode_t mode;
123     eia608_color_t color;
124     eia608_font_t font;
125     int i_row_rollup;
126
127     /* Last command pair (used to reject duplicated command) */
128     struct
129     {
130         uint8_t d1;
131         uint8_t d2;
132     } last;
133 } eia608_t;
134
135 static void         Eia608Init( eia608_t * );
136 static bool   Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] );
137 static char        *Eia608Text( eia608_t *h, bool b_html );
138
139 /* It will be enough up to 63 B frames, which is far too high for
140  * broadcast environment */
141 #define CC_MAX_REORDER_SIZE (64)
142 struct decoder_sys_t
143 {
144     int     i_block;
145     block_t *pp_block[CC_MAX_REORDER_SIZE];
146
147     int i_field;
148     int i_channel;
149
150     eia608_t eia608;
151 };
152
153 static subpicture_t *Decode( decoder_t *, block_t ** );
154
155 /*****************************************************************************
156  * Open: probe the decoder and return score
157  *****************************************************************************
158  * Tries to launch a decoder and return score so that the interface is able
159  * to chose.
160  *****************************************************************************/
161 static int Open( vlc_object_t *p_this )
162 {
163     decoder_t     *p_dec = (decoder_t*)p_this;
164     decoder_sys_t *p_sys;
165     int i_field;
166     int i_channel;
167
168     switch( p_dec->fmt_in.i_codec )
169     {
170         case VLC_CODEC_EIA608_1:
171             i_field = 0; i_channel = 1;
172             break;
173         case VLC_CODEC_EIA608_2:
174             i_field = 0; i_channel = 2;
175             break;
176         case VLC_CODEC_EIA608_3:
177             i_field = 1; i_channel = 1;
178             break;
179         case VLC_CODEC_EIA608_4:
180             i_field = 1; i_channel = 2;
181             break;
182
183         default:
184             return VLC_EGENERIC;
185     }
186
187     p_dec->pf_decode_sub = Decode;
188
189     /* Allocate the memory needed to store the decoder's structure */
190     p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
191     if( p_sys == NULL )
192         return VLC_ENOMEM;
193
194     /* init of p_sys */
195     p_sys->i_field = i_field;
196     p_sys->i_channel = i_channel;
197
198     Eia608Init( &p_sys->eia608 );
199
200     p_dec->fmt_out.i_cat = SPU_ES;
201     p_dec->fmt_out.i_codec = VLC_CODEC_TEXT;
202
203     return VLC_SUCCESS;
204 }
205
206 /****************************************************************************
207  * Decode: the whole thing
208  ****************************************************************************
209  *
210  ****************************************************************************/
211 static void     Push( decoder_t *, block_t * );
212 static block_t *Pop( decoder_t * );
213 static subpicture_t *Convert( decoder_t *, block_t * );
214
215 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
216 {
217     if( pp_block && *pp_block )
218     {
219         Push( p_dec, *pp_block );
220         *pp_block = NULL;
221     }
222
223     for( ;; )
224     {
225         block_t *p_block = Pop( p_dec );
226         if( !p_block )
227             break;
228
229         subpicture_t *p_spu = Convert( p_dec, p_block );
230         if( p_spu )
231             return p_spu;
232     }
233     return NULL;
234 }
235
236 /*****************************************************************************
237  * CloseDecoder: clean up the decoder
238  *****************************************************************************/
239 static void Close( vlc_object_t *p_this )
240 {
241     decoder_t *p_dec = (decoder_t *)p_this;
242     decoder_sys_t *p_sys = p_dec->p_sys;
243
244     for( int i = 0; i < p_sys->i_block; i++ )
245         block_Release( p_sys->pp_block[i] );
246     free( p_sys );
247 }
248
249 /*****************************************************************************
250  *
251  *****************************************************************************/
252 static void Push( decoder_t *p_dec, block_t *p_block )
253 {
254     decoder_sys_t *p_sys = p_dec->p_sys;
255
256     if( p_sys->i_block >= CC_MAX_REORDER_SIZE )
257     {
258         msg_Warn( p_dec, "Trashing a CC entry" );
259         memmove( &p_sys->pp_block[0], &p_sys->pp_block[1], sizeof(*p_sys->pp_block) * (CC_MAX_REORDER_SIZE-1) );
260         p_sys->i_block--;
261     }
262     p_sys->pp_block[p_sys->i_block++] = p_block;
263 }
264 static block_t *Pop( decoder_t *p_dec )
265 {
266     decoder_sys_t *p_sys = p_dec->p_sys;
267     block_t *p_block;
268     int i_index;
269     /* XXX Cc captions data are OUT OF ORDER (because we receive them in the bitstream
270      * order (ie ordered by video picture dts) instead of the display order.
271      *  We will simulate a simple IPB buffer scheme
272      * and reorder with pts.
273      * XXX it won't work with H264 which use non out of order B picture or MMCO
274      */
275
276     /* Wait for a P and output all *previous* picture by pts order (for
277      * hierarchical B frames) */
278     if( p_sys->i_block <= 1 ||
279         ( p_sys->pp_block[p_sys->i_block-1]->i_flags & BLOCK_FLAG_TYPE_B ) )
280         return NULL;
281
282     p_block = p_sys->pp_block[i_index = 0];
283     if( p_block->i_pts > VLC_TS_INVALID )
284     {
285         for( int i = 1; i < p_sys->i_block-1; i++ )
286         {
287             if( p_sys->pp_block[i]->i_pts > VLC_TS_INVALID && p_block->i_pts > VLC_TS_INVALID &&
288                 p_sys->pp_block[i]->i_pts < p_block->i_pts )
289                 p_block = p_sys->pp_block[i_index = i];
290         }
291     }
292     assert( i_index+1 < p_sys->i_block );
293     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 ) );
294     p_sys->i_block--;
295
296     return p_block;
297 }
298
299 static subpicture_t *Subtitle( decoder_t *p_dec, char *psz_subtitle, char *psz_html, mtime_t i_pts )
300 {
301     //decoder_sys_t *p_sys = p_dec->p_sys;
302     subpicture_t *p_spu = NULL;
303
304     /* We cannot display a subpicture with no date */
305     if( i_pts <= VLC_TS_INVALID )
306     {
307         msg_Warn( p_dec, "subtitle without a date" );
308         free( psz_subtitle );
309         free( psz_html );
310         return NULL;
311     }
312
313     EnsureUTF8( psz_subtitle );
314     if( psz_html )
315         EnsureUTF8( psz_html );
316
317     /* Create the subpicture unit */
318     p_spu = decoder_NewSubpictureText( p_dec );
319     if( !p_spu )
320     {
321         free( psz_subtitle );
322         free( psz_html );
323         return NULL;
324     }
325     p_spu->i_start    = i_pts;
326     p_spu->i_stop     = i_pts + 10000000;   /* 10s max */
327     p_spu->b_ephemer  = true;
328     p_spu->b_absolute = false;
329
330     subpicture_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
331
332     /* The "leavetext" alignment is a special mode where the subpicture
333        region itself gets aligned, but the text inside it does not */
334     p_spu_sys->align = SUBPICTURE_ALIGN_LEAVETEXT;
335     p_spu_sys->text  = psz_subtitle;
336     p_spu_sys->html  = psz_html;
337     p_spu_sys->i_font_height_percent = 5;
338     p_spu_sys->renderbg = true;
339
340     return p_spu;
341 }
342
343 static subpicture_t *Convert( decoder_t *p_dec, block_t *p_block )
344 {
345     assert( p_block );
346
347     decoder_sys_t *p_sys = p_dec->p_sys;
348     const int64_t i_pts = p_block->i_pts;
349     bool b_changed = false;
350
351     /* TODO do the real decoding here */
352     while( p_block->i_buffer >= 3 )
353     {
354         if( p_block->p_buffer[0] == p_sys->i_field )
355             b_changed |= Eia608Parse( &p_sys->eia608, p_sys->i_channel, &p_block->p_buffer[1] );
356
357         p_block->i_buffer -= 3;
358         p_block->p_buffer += 3;
359     }
360     if( p_block )
361         block_Release( p_block );
362
363     if( b_changed )
364     {
365         char *psz_subtitle = Eia608Text( &p_sys->eia608, false );
366         char *psz_html = Eia608Text( &p_sys->eia608, true );
367         return Subtitle( p_dec, psz_subtitle, psz_html, i_pts );
368     }
369     return NULL;
370 }
371
372
373 /*****************************************************************************
374  *
375  *****************************************************************************/
376 static const struct {
377     eia608_color_t  i_color;
378     eia608_font_t   i_font;
379     int             i_column;
380 } pac2_attribs[]= {
381     { EIA608_COLOR_WHITE,   EIA608_FONT_REGULAR,           0 },
382     { EIA608_COLOR_WHITE,   EIA608_FONT_UNDERLINE,         0 },
383     { EIA608_COLOR_GREEN,   EIA608_FONT_REGULAR,           0 },
384     { EIA608_COLOR_GREEN,   EIA608_FONT_UNDERLINE,         0 },
385     { EIA608_COLOR_BLUE,    EIA608_FONT_REGULAR,           0 },
386     { EIA608_COLOR_BLUE,    EIA608_FONT_UNDERLINE,         0 },
387     { EIA608_COLOR_CYAN,    EIA608_FONT_REGULAR,           0 },
388     { EIA608_COLOR_CYAN,    EIA608_FONT_UNDERLINE,         0 },
389     { EIA608_COLOR_RED,     EIA608_FONT_REGULAR,           0 },
390     { EIA608_COLOR_RED,     EIA608_FONT_UNDERLINE,         0 },
391     { EIA608_COLOR_YELLOW,  EIA608_FONT_REGULAR,           0 },
392     { EIA608_COLOR_YELLOW,  EIA608_FONT_UNDERLINE,         0 },
393     { EIA608_COLOR_MAGENTA, EIA608_FONT_REGULAR,           0 },
394     { EIA608_COLOR_MAGENTA, EIA608_FONT_UNDERLINE,         0 },
395     { EIA608_COLOR_WHITE,   EIA608_FONT_ITALICS,           0 },
396     { EIA608_COLOR_WHITE,   EIA608_FONT_UNDERLINE_ITALICS, 0 },
397
398     { EIA608_COLOR_WHITE,   EIA608_FONT_REGULAR,           0 },
399     { EIA608_COLOR_WHITE,   EIA608_FONT_UNDERLINE,         0 },
400     { EIA608_COLOR_WHITE,   EIA608_FONT_REGULAR,           4 },
401     { EIA608_COLOR_WHITE,   EIA608_FONT_UNDERLINE,         4 },
402     { EIA608_COLOR_WHITE,   EIA608_FONT_REGULAR,           8 },
403     { EIA608_COLOR_WHITE,   EIA608_FONT_UNDERLINE,         8 },
404     { EIA608_COLOR_WHITE,   EIA608_FONT_REGULAR,          12 },
405     { EIA608_COLOR_WHITE,   EIA608_FONT_UNDERLINE,        12 },
406     { EIA608_COLOR_WHITE,   EIA608_FONT_REGULAR,          16 },
407     { EIA608_COLOR_WHITE,   EIA608_FONT_UNDERLINE,        16 },
408     { EIA608_COLOR_WHITE,   EIA608_FONT_REGULAR,          20 },
409     { EIA608_COLOR_WHITE,   EIA608_FONT_UNDERLINE,        20 },
410     { EIA608_COLOR_WHITE,   EIA608_FONT_REGULAR,          24 },
411     { EIA608_COLOR_WHITE,   EIA608_FONT_UNDERLINE,        24 },
412     { EIA608_COLOR_WHITE,   EIA608_FONT_REGULAR,          28 },
413     { EIA608_COLOR_WHITE,   EIA608_FONT_UNDERLINE,        28 } ,
414 };
415
416 #define EIA608_COLOR_DEFAULT EIA608_COLOR_WHITE
417
418 static void Eia608Cursor( eia608_t *h, int dx )
419 {
420     h->cursor.i_column += dx;
421     if( h->cursor.i_column < 0 )
422         h->cursor.i_column = 0;
423     else if( h->cursor.i_column > EIA608_SCREEN_COLUMNS-1 )
424         h->cursor.i_column = EIA608_SCREEN_COLUMNS-1;
425 }
426 static void Eia608ClearScreenRowX( eia608_t *h, int i_screen, int i_row, int x )
427 {
428     eia608_screen *screen = &h->screen[i_screen];
429
430     if( x == 0 )
431     {
432         screen->row_used[i_row] = false;
433     }
434     else
435     {
436         screen->row_used[i_row] = false;
437         for( int i = 0; i < x; i++ )
438         {
439             if( screen->characters[i_row][i] != ' ' ||
440                 screen->colors[i_row][i] != EIA608_COLOR_DEFAULT ||
441                 screen->fonts[i_row][i] != EIA608_FONT_REGULAR )
442             {
443                 screen->row_used[i_row] = true;
444                 break;
445             }
446         }
447     }
448
449     for( ; x < EIA608_SCREEN_COLUMNS+1; x++ )
450     {
451         screen->characters[i_row][x] = x < EIA608_SCREEN_COLUMNS ? ' ' : '\0';
452         screen->colors[i_row][x] = EIA608_COLOR_DEFAULT;
453         screen->fonts[i_row][x] = EIA608_FONT_REGULAR;
454     }
455 }
456
457 static void Eia608ClearScreenRow( eia608_t *h, int i_screen, int i_row )
458 {
459     Eia608ClearScreenRowX( h, i_screen, i_row, 0 );
460 }
461
462 static void Eia608ClearScreen( eia608_t *h, int i_screen )
463 {
464     for( int i = 0; i < EIA608_SCREEN_ROWS; i++ )
465         Eia608ClearScreenRow( h, i_screen, i );
466 }
467
468 static int Eia608GetWritingScreenIndex( eia608_t *h )
469 {
470     switch( h->mode )
471     {
472     case EIA608_MODE_POPUP:    // Non displayed screen
473         return 1 - h->i_screen;
474
475     case EIA608_MODE_ROLLUP_2: // Displayed screen
476     case EIA608_MODE_ROLLUP_3:
477     case EIA608_MODE_ROLLUP_4:
478     case EIA608_MODE_PAINTON:
479         return h->i_screen;
480     default:
481         /* It cannot happen, else it is a bug */
482         vlc_assert_unreachable();
483         return 0;
484     }
485 }
486
487 static void Eia608EraseScreen( eia608_t *h, bool b_displayed )
488 {
489     Eia608ClearScreen( h, b_displayed ? h->i_screen : (1-h->i_screen) );
490 }
491
492 static void Eia608Write( eia608_t *h, const uint8_t c )
493 {
494     const int i_row = h->cursor.i_row;
495     const int i_column = h->cursor.i_column;
496     eia608_screen *screen;
497
498     if( h->mode == EIA608_MODE_TEXT )
499         return;
500
501     screen = &h->screen[Eia608GetWritingScreenIndex( h )];
502
503     screen->characters[i_row][i_column] = c;
504     screen->colors[i_row][i_column] = h->color;
505     screen->fonts[i_row][i_column] = h->font;
506     screen->row_used[i_row] = true;
507     Eia608Cursor( h, 1 );
508 }
509 static void Eia608Erase( eia608_t *h )
510 {
511     const int i_row = h->cursor.i_row;
512     const int i_column = h->cursor.i_column - 1;
513     eia608_screen *screen;
514
515     if( h->mode == EIA608_MODE_TEXT )
516         return;
517     if( i_column < 0 )
518         return;
519
520     screen = &h->screen[Eia608GetWritingScreenIndex( h )];
521
522     /* FIXME do we need to reset row_used/colors/font ? */
523     screen->characters[i_row][i_column] = ' ';
524     Eia608Cursor( h, -1 );
525 }
526 static void Eia608EraseToEndOfRow( eia608_t *h )
527 {
528     if( h->mode == EIA608_MODE_TEXT )
529         return;
530
531     Eia608ClearScreenRowX( h, Eia608GetWritingScreenIndex( h ), h->cursor.i_row, h->cursor.i_column );
532 }
533
534 static void Eia608RollUp( eia608_t *h )
535 {
536     if( h->mode == EIA608_MODE_TEXT )
537         return;
538
539     const int i_screen = Eia608GetWritingScreenIndex( h );
540     eia608_screen *screen = &h->screen[i_screen];
541
542     int keep_lines;
543
544     /* Window size */
545     if( h->mode == EIA608_MODE_ROLLUP_2 )
546         keep_lines = 2;
547     else if( h->mode == EIA608_MODE_ROLLUP_3 )
548         keep_lines = 3;
549     else if( h->mode == EIA608_MODE_ROLLUP_4 )
550         keep_lines = 4;
551     else
552         return;
553
554     /* Reset the cursor */
555     h->cursor.i_column = 0;
556
557     /* Erase lines above our window */
558     for( int i = 0; i < h->cursor.i_row - keep_lines; i++ )
559         Eia608ClearScreenRow( h, i_screen, i );
560
561     /* Move up */
562     for( int i = 0; i < keep_lines-1; i++ )
563     {
564         const int i_row = h->cursor.i_row - keep_lines + i + 1;
565         if( i_row < 0 )
566             continue;
567         assert( i_row+1 < EIA608_SCREEN_ROWS );
568         memcpy( screen->characters[i_row], screen->characters[i_row+1], sizeof(*screen->characters) );
569         memcpy( screen->colors[i_row], screen->colors[i_row+1], sizeof(*screen->colors) );
570         memcpy( screen->fonts[i_row], screen->fonts[i_row+1], sizeof(*screen->fonts) );
571         screen->row_used[i_row] = screen->row_used[i_row+1];
572     }
573     /* Reset current row */
574     Eia608ClearScreenRow( h, i_screen, h->cursor.i_row );
575 }
576 static void Eia608ParseChannel( eia608_t *h, const uint8_t d[2] )
577 {
578     /* Check odd parity */
579     static const int p4[16] = {
580         0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
581     };
582     if( p4[d[0] & 0xf] == p4[d[0] >> 4] ||
583         p4[d[1] & 0xf] == p4[ d[1] >> 4] )
584     {
585         h->i_channel = -1;
586         return;
587     }
588
589     /* */
590     const int d1 = d[0] & 0x7f;
591     if( d1 >= 0x10 && d1 <= 0x1f )
592         h->i_channel = 1 + ((d1 & 0x08) != 0);
593     else if( d1 < 0x10 )
594         h->i_channel = 3;
595 }
596 static bool Eia608ParseTextAttribute( eia608_t *h, uint8_t d2 )
597 {
598     const int i_index = d2 - 0x20;
599     assert( d2 >= 0x20 && d2 <= 0x2f );
600
601     h->color = pac2_attribs[i_index].i_color;
602     h->font  = pac2_attribs[i_index].i_font;
603     Eia608Cursor( h, 1 );
604
605     return false;
606 }
607 static bool Eia608ParseSingle( eia608_t *h, const uint8_t dx )
608 {
609     assert( dx >= 0x20 );
610     Eia608Write( h, dx );
611     return true;
612 }
613 static bool Eia608ParseDouble( eia608_t *h, uint8_t d2 )
614 {
615     assert( d2 >= 0x30 && d2 <= 0x3f );
616     Eia608Write( h, d2 + 0x50 ); /* We use charaters 0x80...0x8f */
617     return true;
618 }
619 static bool Eia608ParseExtended( eia608_t *h, uint8_t d1, uint8_t d2 )
620 {
621     assert( d2 >= 0x20 && d2 <= 0x3f );
622     assert( d1 == 0x12 || d1 == 0x13 );
623     if( d1 == 0x12 )
624         d2 += 0x70; /* We use charaters 0x90-0xaf */
625     else
626         d2 += 0x90; /* We use charaters 0xb0-0xcf */
627
628     /* The extended characters replace the previous one with a more
629      * advanced one */
630     Eia608Cursor( h, -1 );
631     Eia608Write( h, d2 );
632     return true;
633 }
634 static bool Eia608ParseCommand0x14( eia608_t *h, uint8_t d2 )
635 {
636     bool b_changed = false;
637
638     switch( d2 )
639     {
640     case 0x20:  /* Resume caption loading */
641         h->mode = EIA608_MODE_POPUP;
642         break;
643     case 0x21:  /* Backspace */
644         Eia608Erase( h );
645         b_changed = true;
646         break;
647     case 0x22:  /* Reserved */
648     case 0x23:
649         break;
650     case 0x24:  /* Delete to end of row */
651         Eia608EraseToEndOfRow( h );
652         break;
653     case 0x25:  /* Rollup 2 */
654     case 0x26:  /* Rollup 3 */
655     case 0x27:  /* Rollup 4 */
656         if( h->mode == EIA608_MODE_POPUP || h->mode == EIA608_MODE_PAINTON )
657         {
658             Eia608EraseScreen( h, true );
659             Eia608EraseScreen( h, false );
660             b_changed = true;
661         }
662
663         if( d2 == 0x25 )
664             h->mode = EIA608_MODE_ROLLUP_2;
665         else if( d2 == 0x26 )
666             h->mode = EIA608_MODE_ROLLUP_3;
667         else
668             h->mode = EIA608_MODE_ROLLUP_4;
669
670         h->cursor.i_column = 0;
671         h->cursor.i_row = h->i_row_rollup;
672         break;
673     case 0x28:  /* Flash on */
674         /* TODO */
675         break;
676     case 0x29:  /* Resume direct captionning */
677         h->mode = EIA608_MODE_PAINTON;
678         break;
679     case 0x2a:  /* Text restart */
680         /* TODO */
681         break;
682
683     case 0x2b: /* Resume text display */
684         h->mode = EIA608_MODE_TEXT;
685         break;
686
687     case 0x2c: /* Erase displayed memory */
688         Eia608EraseScreen( h, true );
689         b_changed = true;
690         break;
691     case 0x2d: /* Carriage return */
692         Eia608RollUp(h);
693         b_changed = true;
694         break;
695     case 0x2e: /* Erase non displayed memory */
696         Eia608EraseScreen( h, false );
697         break;
698     case 0x2f: /* End of caption (flip screen if not paint on) */
699         if( h->mode != EIA608_MODE_PAINTON )
700             h->i_screen = 1 - h->i_screen;
701         h->mode = EIA608_MODE_POPUP;
702         h->cursor.i_column = 0;
703         h->cursor.i_row = 0;
704         h->color = EIA608_COLOR_DEFAULT;
705         h->font = EIA608_FONT_REGULAR;
706         b_changed = true;
707         break;
708     }
709     return b_changed;
710 }
711 static bool Eia608ParseCommand0x17( eia608_t *h, uint8_t d2 )
712 {
713     switch( d2 )
714     {
715     case 0x21:  /* Tab offset 1 */
716         Eia608Cursor( h, 1 );
717         break;
718     case 0x22:  /* Tab offset 2 */
719         Eia608Cursor( h, 2 );
720         break;
721     case 0x23:  /* Tab offset 3 */
722         Eia608Cursor( h, 3 );
723         break;
724     }
725     return false;
726 }
727 static bool Eia608ParsePac( eia608_t *h, uint8_t d1, uint8_t d2 )
728 {
729     static const int pi_row[] = {
730         11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
731     };
732     const int i_row_index = ( (d1<<1) & 0x0e) | ( (d2>>5) & 0x01 );
733
734     assert( d2 >= 0x40 && d2 <= 0x7f );
735
736     if( pi_row[i_row_index] <= 0 )
737         return false;
738
739     /* Row */
740     if( h->mode != EIA608_MODE_TEXT )
741         h->cursor.i_row = pi_row[i_row_index] - 1;
742     h->i_row_rollup = pi_row[i_row_index] - 1;
743     /* Column */
744     if( d2 >= 0x60 )
745         d2 -= 0x60;
746     else if( d2 >= 0x40 )
747         d2 -= 0x40;
748     h->cursor.i_column = pac2_attribs[d2].i_column;
749     h->color = pac2_attribs[d2].i_color;
750     h->font  = pac2_attribs[d2].i_font;
751
752     return false;
753 }
754
755 static bool Eia608ParseData( eia608_t *h, uint8_t d1, uint8_t d2 )
756 {
757     bool b_changed = false;
758
759     if( d1 >= 0x18 && d1 <= 0x1f )
760         d1 -= 8;
761
762 #define ON( d2min, d2max, cmd ) do { if( d2 >= d2min && d2 <= d2max ) b_changed = cmd; } while(0)
763     switch( d1 )
764     {
765     case 0x11:
766         ON( 0x20, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
767         ON( 0x30, 0x3f, Eia608ParseDouble( h, d2 ) );
768         break;
769     case 0x12: case 0x13:
770         ON( 0x20, 0x3f, Eia608ParseExtended( h, d1, d2 ) );
771         break;
772     case 0x14: case 0x15:
773         ON( 0x20, 0x2f, Eia608ParseCommand0x14( h, d2 ) );
774         break;
775     case 0x17:
776         ON( 0x21, 0x22, Eia608ParseCommand0x17( h, d2 ) );
777         ON( 0x2e, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
778         break;
779     }
780     if( d1 == 0x10 )
781         ON( 0x40, 0x5f, Eia608ParsePac( h, d1, d2 ) );
782     else if( d1 >= 0x11 && d1 <= 0x17 )
783         ON( 0x40, 0x7f, Eia608ParsePac( h, d1, d2 ) );
784 #undef ON
785     if( d1 >= 0x20 )
786     {
787         b_changed = Eia608ParseSingle( h, d1 );
788         if( d2 >= 0x20 )
789             b_changed |= Eia608ParseSingle( h, d2 );
790     }
791     return b_changed;
792 }
793
794 static void Eia608TextUtf8( char *psz_utf8, uint8_t c ) // Returns number of bytes used
795 {
796 #define E1(c,u) { c, { u, '\0' } }
797 #define E2(c,u1,u2) { c, { u1, u2, '\0' } }
798 #define E3(c,u1,u2,u3) { c, { u1, u2, u3, '\0' } }
799     static const struct {
800         uint8_t c;
801         char utf8[3+1];
802     } c2utf8[] = {
803         // Regular line-21 character set, mostly ASCII except these exceptions
804         E2( 0x2a, 0xc3,0xa1), // lowercase a, acute accent
805         E2( 0x5c, 0xc3,0xa9), // lowercase e, acute accent
806         E2( 0x5e, 0xc3,0xad), // lowercase i, acute accent
807         E2( 0x5f, 0xc3,0xb3), // lowercase o, acute accent
808         E2( 0x60, 0xc3,0xba), // lowercase u, acute accent
809         E2( 0x7b, 0xc3,0xa7), // lowercase c with cedilla
810         E2( 0x7c, 0xc3,0xb7), // division symbol
811         E2( 0x7d, 0xc3,0x91), // uppercase N tilde
812         E2( 0x7e, 0xc3,0xb1), // lowercase n tilde
813         // THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
814         // THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F
815         E2( 0x80, 0xc2,0xae), // Registered symbol (R)
816         E2( 0x81, 0xc2,0xb0), // degree sign
817         E2( 0x82, 0xc2,0xbd), // 1/2 symbol
818         E2( 0x83, 0xc2,0xbf), // Inverted (open) question mark
819         E3( 0x84, 0xe2,0x84,0xa2), // Trademark symbol (TM)
820         E2( 0x85, 0xc2,0xa2), // Cents symbol
821         E2( 0x86, 0xc2,0xa3), // Pounds sterling
822         E3( 0x87, 0xe2,0x99,0xaa), // Music note
823         E2( 0x88, 0xc3,0xa0), // lowercase a, grave accent
824         E2( 0x89, 0xc2,0xa0), // transparent space
825         E2( 0x8a, 0xc3,0xa8), // lowercase e, grave accent
826         E2( 0x8b, 0xc3,0xa2), // lowercase a, circumflex accent
827         E2( 0x8c, 0xc3,0xaa), // lowercase e, circumflex accent
828         E2( 0x8d, 0xc3,0xae), // lowercase i, circumflex accent
829         E2( 0x8e, 0xc3,0xb4), // lowercase o, circumflex accent
830         E2( 0x8f, 0xc3,0xbb), // lowercase u, circumflex accent
831         // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
832         // THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F
833         E2( 0x90, 0xc3,0x81), // capital letter A with acute
834         E2( 0x91, 0xc3,0x89), // capital letter E with acute
835         E2( 0x92, 0xc3,0x93), // capital letter O with acute
836         E2( 0x93, 0xc3,0x9a), // capital letter U with acute
837         E2( 0x94, 0xc3,0x9c), // capital letter U with diaresis
838         E2( 0x95, 0xc3,0xbc), // lowercase letter U with diaeresis
839         E1( 0x96, 0x27), // apostrophe
840         E2( 0x97, 0xc2,0xa1), // inverted exclamation mark
841         E1( 0x98, 0x2a), // asterisk
842         E1( 0x99, 0x27), // apostrophe (yes, duped). See CCADI source code.
843         E1( 0x9a, 0x2d), // hyphen-minus
844         E2( 0x9b, 0xc2,0xa9), // copyright sign
845         E3( 0x9c, 0xe2,0x84,0xa0), // Service mark
846         E1( 0x9d, 0x2e), // Full stop (.)
847         E3( 0x9e, 0xe2,0x80,0x9c), // Quotation mark
848         E3( 0x9f, 0xe2,0x80,0x9d), // Quotation mark
849         E2( 0xa0, 0xc3,0x80), // uppercase A, grave accent
850         E2( 0xa1, 0xc3,0x82), // uppercase A, circumflex
851         E2( 0xa2, 0xc3,0x87), // uppercase C with cedilla
852         E2( 0xa3, 0xc3,0x88), // uppercase E, grave accent
853         E2( 0xa4, 0xc3,0x8a), // uppercase E, circumflex
854         E2( 0xa5, 0xc3,0x8b), // capital letter E with diaresis
855         E2( 0xa6, 0xc3,0xab), // lowercase letter e with diaresis
856         E2( 0xa7, 0xc3,0x8e), // uppercase I, circumflex
857         E2( 0xa8, 0xc3,0x8f), // uppercase I, with diaresis
858         E2( 0xa9, 0xc3,0xaf), // lowercase i, with diaresis
859         E2( 0xaa, 0xc3,0x94), // uppercase O, circumflex
860         E2( 0xab, 0xc3,0x99), // uppercase U, grave accent
861         E2( 0xac, 0xc3,0xb9), // lowercase u, grave accent
862         E2( 0xad, 0xc3,0x9b), // uppercase U, circumflex
863         E2( 0xae, 0xc2,0xab), // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
864         E2( 0xaf, 0xc2,0xbb), // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
865         // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
866         // THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F
867         E2( 0xb0, 0xc3,0x83), // Uppercase A, tilde
868         E2( 0xb1, 0xc3,0xa3), // Lowercase a, tilde
869         E2( 0xb2, 0xc3,0x8d), // Uppercase I, acute accent
870         E2( 0xb3, 0xc3,0x8c), // Uppercase I, grave accent
871         E2( 0xb4, 0xc3,0xac), // Lowercase i, grave accent
872         E2( 0xb5, 0xc3,0x92), // Uppercase O, grave accent
873         E2( 0xb6, 0xc3,0xb2), // Lowercase o, grave accent
874         E2( 0xb7, 0xc3,0x95), // Uppercase O, tilde
875         E2( 0xb8, 0xc3,0xb5), // Lowercase o, tilde
876         E1( 0xb9, 0x7b), // Open curly brace
877         E1( 0xba, 0x7d), // Closing curly brace
878         E1( 0xbb, 0x5c), // Backslash
879         E1( 0xbc, 0x5e), // Caret
880         E1( 0xbd, 0x5f), // Underscore
881         E2( 0xbe, 0xc2,0xa6), // Pipe (broken bar)
882         E1( 0xbf, 0x7e), // Tilde (utf8 code unsure)
883         E2( 0xc0, 0xc3,0x84), // Uppercase A, umlaut
884         E2( 0xc1, 0xc3,0xa4), // Lowercase A, umlaut
885         E2( 0xc2, 0xc3,0x96), // Uppercase O, umlaut
886         E2( 0xc3, 0xc3,0xb6), // Lowercase o, umlaut
887         E2( 0xc4, 0xc3,0x9f), // Esszett (sharp S)
888         E2( 0xc5, 0xc2,0xa5), // Yen symbol
889         E2( 0xc6, 0xc2,0xa4), // Currency symbol
890         E1( 0xc7, 0x7c), // Vertical bar
891         E2( 0xc8, 0xc3,0x85), // Uppercase A, ring
892         E2( 0xc9, 0xc3,0xa5), // Lowercase A, ring
893         E2( 0xca, 0xc3,0x98), // Uppercase O, slash
894         E2( 0xcb, 0xc3,0xb8), // Lowercase o, slash
895         E3( 0xcc, 0xe2,0x8c,0x9c), // Upper left corner
896         E3( 0xcd, 0xe2,0x8c,0x9d), // Upper right corner
897         E3( 0xce, 0xe2,0x8c,0x9e), // Lower left corner
898         E3( 0xcf, 0xe2,0x8c,0x9f), // Lower right corner
899
900         E1(0,0)
901     };
902 #undef E3
903 #undef E2
904 #undef E1
905
906     for( size_t i = 0; i < ARRAY_SIZE(c2utf8) ; i++ )
907         if( c2utf8[i].c == c ) {
908             strcpy( psz_utf8, c2utf8[i].utf8 );
909             return;
910         }
911
912     psz_utf8[0] = c < 0x80 ? c : '?';   /* Normal : Unsupported */
913     psz_utf8[1] = '\0';
914 }
915
916 static void Eia608Strlcat( char *d, const char *s, int i_max )
917 {
918     if( i_max > 1 )
919         strncat( d, s, i_max-1 - strnlen(d, i_max-1));
920     if( i_max > 0 )
921         d[i_max-1] = '\0';
922 }
923
924 #define CAT(t) Eia608Strlcat( psz_text, t, i_text_max )
925
926 static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_text_max, int i_row, bool b_html )
927 {
928     const uint8_t *p_char = screen->characters[i_row];
929     const eia608_color_t *p_color = screen->colors[i_row];
930     const eia608_font_t *p_font = screen->fonts[i_row];
931     int i_start;
932     int i_end;
933     int x;
934     eia608_color_t last_color = EIA608_COLOR_DEFAULT;
935     bool     b_last_italics = false;
936     bool     b_last_underline = false;
937     char utf8[4];
938
939     /* Search the start */
940     i_start = 0;
941
942     /* Ensure we get a monospaced font (required for accurate positioning */
943     if( b_html )
944         CAT( "<tt>" );
945
946     /* Convert leading spaces to non-breaking so that they don't get
947        stripped by the RenderHtml routine as regular whitespace */
948     while( i_start < EIA608_SCREEN_COLUMNS && p_char[i_start] == ' ' ) {
949         Eia608TextUtf8( utf8, 0x89 );
950         CAT( utf8 );
951         i_start++;
952     }
953
954     /* Search the end */
955     i_end = EIA608_SCREEN_COLUMNS-1;
956     while( i_end > i_start && p_char[i_end] == ' ' )
957         i_end--;
958
959     /* */
960     for( x = i_start; x <= i_end; x++ )
961     {
962         eia608_color_t color = p_color[x];
963         bool b_italics = p_font[x] & EIA608_FONT_ITALICS;
964         bool b_underline = p_font[x] & EIA608_FONT_UNDERLINE;
965
966         /* */
967         if( b_html )
968         {
969             bool b_close_color, b_close_italics, b_close_underline;
970
971             /* We create the tags font / i / u in that orders */
972             b_close_color = color != last_color && last_color != EIA608_COLOR_DEFAULT;
973             b_close_italics = !b_italics && b_last_italics;
974             b_close_underline = !b_underline && b_last_underline;
975
976             /* Be sure to create valid html */
977             b_close_italics |= b_last_italics && b_close_color;
978             b_close_underline |= b_last_underline && ( b_close_italics || b_close_color );
979
980             if( b_close_underline )
981                 CAT( "</u>" );
982             if( b_close_italics )
983                 CAT( "</i>" );
984             if( b_close_color )
985                 CAT( "</font>" );
986
987             if( color != EIA608_COLOR_DEFAULT && color != last_color)
988             {
989                 static const char *ppsz_color[] = {
990                     "#ffffff",  // white
991                     "#00ff00",  // green
992                     "#0000ff",  // blue
993                     "#00ffff",  // cyan
994                     "#ff0000",  // red
995                     "#ffff00",  // yellow
996                     "#ff00ff",  // magenta
997                     "#ffffff",  // user defined XXX we use white
998                 };
999                 CAT( "<font color=\"" );
1000                 CAT( ppsz_color[color] );
1001                 CAT( "\">" );
1002             }
1003             if( ( b_close_italics && b_italics ) || ( b_italics && !b_last_italics ) )
1004                 CAT( "<i>" );
1005             if( ( b_close_underline && b_underline ) || ( b_underline && !b_last_underline ) )
1006                 CAT( "<u>" );
1007         }
1008
1009         if( b_html ) {
1010             /* Escape XML reserved characters
1011                http://www.w3.org/TR/xml/#syntax */
1012             switch (p_char[x]) {
1013             case '>':
1014                 CAT( "&gt;" );
1015                 break;
1016             case '<':
1017                 CAT( "&lt;" );
1018                 break;
1019             case '"':
1020                 CAT( "&quot;" );
1021                 break;
1022             case '\'':
1023                 CAT( "&apos;" );
1024                 break;
1025             case '&':
1026                 CAT( "&amp;" );
1027                 break;
1028             default:
1029                 Eia608TextUtf8( utf8, p_char[x] );
1030                 CAT( utf8 );
1031                 break;
1032             }
1033         } else {
1034             Eia608TextUtf8( utf8, p_char[x] );
1035             CAT( utf8 );
1036         }
1037
1038         /* */
1039         b_last_underline = b_underline;
1040         b_last_italics = b_italics;
1041         last_color = color;
1042     }
1043     if( b_html )
1044     {
1045         if( b_last_underline )
1046             CAT( "</u>" );
1047         if( b_last_italics )
1048             CAT( "</i>" );
1049         if( last_color != EIA608_COLOR_DEFAULT )
1050             CAT( "</font>" );
1051         CAT( "</tt>" );
1052     }
1053 #undef CAT
1054 }
1055
1056 /* */
1057 static void Eia608Init( eia608_t *h )
1058 {
1059     memset( h, 0, sizeof(*h) );
1060
1061     /* */
1062     h->i_channel = -1;
1063
1064     h->i_screen = 0;
1065     Eia608ClearScreen( h, 0 );
1066     Eia608ClearScreen( h, 1 );
1067
1068     /* Cursor for writing text */
1069     h->cursor.i_column = 0;
1070     h->cursor.i_row = 0;
1071
1072     h->last.d1 = 0x00;
1073     h->last.d2 = 0x00;
1074     h->mode = EIA608_MODE_POPUP;
1075     h->color = EIA608_COLOR_DEFAULT;
1076     h->font = EIA608_FONT_REGULAR;
1077     h->i_row_rollup = EIA608_SCREEN_ROWS-1;
1078 }
1079 static bool Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] )
1080 {
1081     const uint8_t d1 = data[0] & 0x7f; /* Removed parity bit */
1082     const uint8_t d2 = data[1] & 0x7f;
1083     bool b_screen_changed = false;
1084
1085     if( d1 == 0 && d2 == 0 )
1086         return false;   /* Ignore padding (parity check are sometimes invalid on them) */
1087
1088     Eia608ParseChannel( h, data );
1089     if( h->i_channel != i_channel_selected )
1090         return false;
1091     //fprintf( stderr, "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC %x %x\n", data[0], data[1] );
1092
1093     if( d1 >= 0x10 )
1094     {
1095         if( d1 >= 0x20 ||
1096             d1 != h->last.d1 || d2 != h->last.d2 ) /* Command codes can be repeated */
1097             b_screen_changed = Eia608ParseData( h, d1,d2 );
1098
1099         h->last.d1 = d1;
1100         h->last.d2 = d2;
1101     }
1102     else if( ( d1 >= 0x01 && d1 <= 0x0E ) || d1 == 0x0F )
1103     {
1104         /* XDS block / End of XDS block */
1105     }
1106     return b_screen_changed;
1107 }
1108
1109 static char *Eia608Text( eia608_t *h, bool b_html )
1110 {
1111     const int i_size = EIA608_SCREEN_ROWS * 10 * EIA608_SCREEN_COLUMNS+1;
1112     struct eia608_screen *screen = &h->screen[h->i_screen];
1113     bool b_first = true;
1114     char *psz;
1115
1116     /* We allocate a buffer big enough for normal case */
1117     psz = malloc( i_size );
1118     if( !psz )
1119         return NULL;
1120     *psz = '\0';
1121     if( b_html )
1122         Eia608Strlcat( psz, "<text>", i_size );
1123     for( int i = 0; i < EIA608_SCREEN_ROWS; i++ )
1124     {
1125         if( !b_first )
1126             Eia608Strlcat( psz, b_html ? "<br />" : "\n", i_size );
1127         b_first = false;
1128
1129         Eia608TextLine( screen, psz, i_size, i, b_html );
1130     }
1131     if( b_html )
1132         Eia608Strlcat( psz, "</text>", i_size );
1133     return psz;
1134 }