]> git.sesse.net Git - vlc/blob - modules/codec/cc.c
Update copyright and remove unnecessary include (CC)
[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
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 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 General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, 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 static void         Eia608Exit( eia608_t * );
139
140 /* It will be enough up to 63 B frames, which is far too high for
141  * broadcast environment */
142 #define CC_MAX_REORDER_SIZE (64)
143 struct decoder_sys_t
144 {
145     int i;
146
147     int     i_block;
148     block_t *pp_block[CC_MAX_REORDER_SIZE];
149
150     int i_field;
151     int i_channel;
152
153     eia608_t eia608;
154 };
155
156 static subpicture_t *Decode( decoder_t *, block_t ** );
157
158 /*****************************************************************************
159  * Open: probe the decoder and return score
160  *****************************************************************************
161  * Tries to launch a decoder and return score so that the interface is able
162  * to chose.
163  *****************************************************************************/
164 static int Open( vlc_object_t *p_this )
165 {
166     decoder_t     *p_dec = (decoder_t*)p_this;
167     decoder_sys_t *p_sys;
168     int i_field;
169     int i_channel;
170
171     switch( p_dec->fmt_in.i_codec )
172     {
173         case VLC_FOURCC('c','c','1',' '):
174             i_field = 0; i_channel = 1;
175             break;
176         case VLC_FOURCC('c','c','2',' '):
177             i_field = 0; i_channel = 2;
178             break;
179         case VLC_FOURCC('c','c','3',' '):
180             i_field = 1; i_channel = 1;
181             break;
182         case VLC_FOURCC('c','c','4',' '):
183             i_field = 1; i_channel = 2;
184             break;
185
186         default:
187             return VLC_EGENERIC;
188     }
189
190     p_dec->pf_decode_sub = Decode;
191
192     /* Allocate the memory needed to store the decoder's structure */
193     p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
194     if( p_sys == NULL )
195         return VLC_ENOMEM;
196
197     /* init of p_sys */
198     p_sys->i_field = i_field;
199     p_sys->i_channel = i_channel;
200
201     Eia608Init( &p_sys->eia608 );
202
203     p_dec->fmt_out.i_cat = SPU_ES;
204     p_dec->fmt_out.i_codec = VLC_CODEC_TEXT;
205
206     return VLC_SUCCESS;
207 }
208
209 /****************************************************************************
210  * Decode: the whole thing
211  ****************************************************************************
212  *
213  ****************************************************************************/
214 static void     Push( decoder_t *, block_t * );
215 static block_t *Pop( decoder_t * );
216 static subpicture_t *Convert( decoder_t *, block_t * );
217
218 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
219 {
220     if( pp_block && *pp_block )
221     {
222         Push( p_dec, *pp_block );
223         *pp_block = NULL;
224     }
225
226     for( ;; )
227     {
228         block_t *p_block = Pop( p_dec );
229         if( !p_block )
230             break;
231
232         subpicture_t *p_spu = Convert( p_dec, p_block );
233         if( p_spu )
234             return p_spu;
235     }
236     return NULL;
237 }
238
239 /*****************************************************************************
240  * CloseDecoder: clean up the decoder
241  *****************************************************************************/
242 static void Close( vlc_object_t *p_this )
243 {
244     decoder_t *p_dec = (decoder_t *)p_this;
245     decoder_sys_t *p_sys = p_dec->p_sys;
246     int i;
247
248     for( i = 0; i < p_sys->i_block; i++ )
249         block_Release( p_sys->pp_block[i] );
250     Eia608Exit( &p_sys->eia608 );
251     free( p_sys );
252 }
253
254 /*****************************************************************************
255  *
256  *****************************************************************************/
257 static void Push( decoder_t *p_dec, block_t *p_block )
258 {
259     decoder_sys_t *p_sys = p_dec->p_sys;
260
261     if( p_sys->i_block >= CC_MAX_REORDER_SIZE )
262     {
263         msg_Warn( p_dec, "Trashing a CC entry" );
264         memmove( &p_sys->pp_block[0], &p_sys->pp_block[1], sizeof(*p_sys->pp_block) * (CC_MAX_REORDER_SIZE-1) );
265         p_sys->i_block--;
266     }
267     p_sys->pp_block[p_sys->i_block++] = p_block;
268 }
269 static block_t *Pop( decoder_t *p_dec )
270 {
271     decoder_sys_t *p_sys = p_dec->p_sys;
272     block_t *p_block;
273     int i_index;
274     int i;
275     /* XXX Cc captions data are OUT OF ORDER (because we receive them in the bitstream
276      * order (ie ordered by video picture dts) instead of the display order.
277      *  We will simulate a simple IPB buffer scheme
278      * and reorder with pts.
279      * XXX it won't work with H264 which use non out of order B picture or MMCO
280      */
281
282     /* Wait for a P and output all *previous* picture by pts order (for
283      * hierarchical B frames) */
284     if( p_sys->i_block <= 1 ||
285         ( p_sys->pp_block[p_sys->i_block-1]->i_flags & BLOCK_FLAG_TYPE_B ) )
286         return NULL;
287
288     p_block = p_sys->pp_block[i_index = 0];
289     if( p_block->i_pts > VLC_TS_INVALID )
290     {
291         for( i = 1; i < p_sys->i_block-1; i++ )
292         {
293             if( p_sys->pp_block[i]->i_pts > VLC_TS_INVALID && p_block->i_pts > VLC_TS_INVALID &&
294                 p_sys->pp_block[i]->i_pts < p_block->i_pts )
295                 p_block = p_sys->pp_block[i_index = i];
296         }
297     }
298     assert( i_index+1 < p_sys->i_block );
299     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 ) );
300     p_sys->i_block--;
301
302     return p_block;
303 }
304
305 static subpicture_t *Subtitle( decoder_t *p_dec, char *psz_subtitle, char *psz_html, mtime_t i_pts )
306 {
307     //decoder_sys_t *p_sys = p_dec->p_sys;
308     subpicture_t *p_spu = NULL;
309
310     /* We cannot display a subpicture with no date */
311     if( i_pts <= VLC_TS_INVALID )
312     {
313         msg_Warn( p_dec, "subtitle without a date" );
314         return NULL;
315     }
316
317     EnsureUTF8( psz_subtitle );
318     if( psz_html )
319         EnsureUTF8( psz_html );
320
321     /* Create the subpicture unit */
322     p_spu = decoder_NewSubpictureText( p_dec );
323     if( !p_spu )
324     {
325         free( psz_subtitle );
326         free( psz_html );
327         return NULL;
328     }
329     p_spu->i_start    = i_pts;
330     p_spu->i_stop     = i_pts + 10000000;   /* 10s max */
331     p_spu->b_ephemer  = true;
332     p_spu->b_absolute = false;
333
334     subpicture_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
335
336     p_spu_sys->align = SUBPICTURE_ALIGN_BOTTOM;
337     p_spu_sys->text  = psz_subtitle;
338     p_spu_sys->html  = psz_html;
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     = NULL;//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     int i;
430
431     if( x == 0 )
432     {
433         screen->row_used[i_row] = false;
434     }
435     else
436     {
437         screen->row_used[i_row] = false;
438         for( i = 0; i < x; i++ )
439         {
440             if( screen->characters[i_row][i] != ' ' ||
441                 screen->colors[i_row][i] != EIA608_COLOR_DEFAULT ||
442                 screen->fonts[i_row][i] != EIA608_FONT_REGULAR )
443             {
444                 screen->row_used[i_row] = true;
445                 break;
446             }
447         }
448     }
449
450     for( ; x < EIA608_SCREEN_COLUMNS+1; x++ )
451     {
452         screen->characters[i_row][x] = x < EIA608_SCREEN_COLUMNS ? ' ' : '\0';
453         screen->colors[i_row][x] = EIA608_COLOR_DEFAULT;
454         screen->fonts[i_row][x] = EIA608_FONT_REGULAR;
455     }
456 }
457
458 static void Eia608ClearScreenRow( eia608_t *h, int i_screen, int i_row )
459 {
460     Eia608ClearScreenRowX( h, i_screen, i_row, 0 );
461 }
462
463 static void Eia608ClearScreen( eia608_t *h, int i_screen )
464 {
465     int i;
466     for( i = 0; i < EIA608_SCREEN_ROWS; i++ )
467         Eia608ClearScreenRow( h, i_screen, i );
468 }
469
470 static int Eia608GetWritingScreenIndex( eia608_t *h )
471 {
472     switch( h->mode )
473     {
474     case EIA608_MODE_POPUP:    // Non displayed screen
475         return 1 - h->i_screen;
476
477     case EIA608_MODE_ROLLUP_2: // Displayed screen
478     case EIA608_MODE_ROLLUP_3:
479     case EIA608_MODE_ROLLUP_4:
480     case EIA608_MODE_PAINTON:
481         return h->i_screen;
482     default:
483         /* It cannot happen, else it is a bug */
484         assert( 0 );
485         return 0;
486     }
487 }
488
489 static void Eia608EraseScreen( eia608_t *h, bool b_displayed )
490 {
491     Eia608ClearScreen( h, b_displayed ? h->i_screen : (1-h->i_screen) );
492 }
493
494 static void Eia608Write( eia608_t *h, const uint8_t c )
495 {
496     const int i_row = h->cursor.i_row;
497     const int i_column = h->cursor.i_column;
498     eia608_screen *screen;
499
500     if( h->mode == EIA608_MODE_TEXT )
501         return;
502
503     screen = &h->screen[Eia608GetWritingScreenIndex( h )];
504
505     screen->characters[i_row][i_column] = c;
506     screen->colors[i_row][i_column] = h->color;
507     screen->fonts[i_row][i_column] = h->font;
508     screen->row_used[i_row] = true;
509     Eia608Cursor( h, 1 );
510 }
511 static void Eia608Erase( eia608_t *h )
512 {
513     const int i_row = h->cursor.i_row;
514     const int i_column = h->cursor.i_column - 1;
515     eia608_screen *screen;
516
517     if( h->mode == EIA608_MODE_TEXT )
518         return;
519     if( i_column < 0 )
520         return;
521
522     screen = &h->screen[Eia608GetWritingScreenIndex( h )];
523
524     /* FIXME do we need to reset row_used/colors/font ? */
525     screen->characters[i_row][i_column] = ' ';
526     Eia608Cursor( h, -1 );
527 }
528 static void Eia608EraseToEndOfRow( eia608_t *h )
529 {
530     if( h->mode == EIA608_MODE_TEXT )
531         return;
532
533     Eia608ClearScreenRowX( h, Eia608GetWritingScreenIndex( h ), h->cursor.i_row, h->cursor.i_column );
534 }
535
536 static void Eia608RollUp( eia608_t *h )
537 {
538     if( h->mode == EIA608_MODE_TEXT )
539         return;
540
541     const int i_screen = Eia608GetWritingScreenIndex( h );
542     eia608_screen *screen = &h->screen[i_screen];
543
544     int keep_lines;
545     int i;
546
547     /* Window size */
548     if( h->mode == EIA608_MODE_ROLLUP_2 )
549         keep_lines = 2;
550     else if( h->mode == EIA608_MODE_ROLLUP_3 )
551         keep_lines = 3;
552     else if( h->mode == EIA608_MODE_ROLLUP_4 )
553         keep_lines = 4;
554     else
555         return;
556
557     /* Reset the cursor */
558     h->cursor.i_column = 0;
559
560     /* Erase lines above our window */
561     for( i = 0; i < h->cursor.i_row - keep_lines; i++ )
562         Eia608ClearScreenRow( h, i_screen, i );
563
564     /* Move up */
565     for( i = 0; i < keep_lines-1; i++ )
566     {
567         const int i_row = h->cursor.i_row - keep_lines + i + 1;
568         if( i_row < 0 )
569             continue;
570         assert( i_row+1 < EIA608_SCREEN_ROWS );
571         memcpy( screen->characters[i_row], screen->characters[i_row+1], sizeof(*screen->characters) );
572         memcpy( screen->colors[i_row], screen->colors[i_row+1], sizeof(*screen->colors) );
573         memcpy( screen->fonts[i_row], screen->fonts[i_row+1], sizeof(*screen->fonts) );
574         screen->row_used[i_row] = screen->row_used[i_row+1];
575     }
576     /* Reset current row */
577     Eia608ClearScreenRow( h, i_screen, h->cursor.i_row );
578 }
579 static void Eia608ParseChannel( eia608_t *h, const uint8_t d[2] )
580 {
581     /* Check odd parity */
582     static const int p4[16] = {
583         0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
584     };
585     if( p4[d[0] & 0xf] == p4[d[0] >> 4] ||
586         p4[d[1] & 0xf] == p4[ d[1] >> 4] )
587     {
588         h->i_channel = -1;
589         return;
590     }
591
592     /* */
593     const int d1 = d[0] & 0x7f;
594     if( d1 >= 0x10 && d1 <= 0x1f )
595         h->i_channel = 1 + ((d1 & 0x08) != 0);
596     else if( d1 < 0x10 )
597         h->i_channel = 3;
598 }
599 static bool Eia608ParseTextAttribute( eia608_t *h, uint8_t d2 )
600 {
601     const int i_index = d2 - 0x20;
602     assert( d2 >= 0x20 && d2 <= 0x2f );
603
604     h->color = pac2_attribs[i_index].i_color;
605     h->font  = pac2_attribs[i_index].i_font;
606     Eia608Cursor( h, 1 );
607
608     return false;
609 }
610 static bool Eia608ParseSingle( eia608_t *h, const uint8_t dx )
611 {
612     assert( dx >= 0x20 );
613     Eia608Write( h, dx );
614     return true;
615 }
616 static bool Eia608ParseDouble( eia608_t *h, uint8_t d2 )
617 {
618     assert( d2 >= 0x30 && d2 <= 0x3f );
619     Eia608Write( h, d2 + 0x50 ); /* We use charaters 0x80...0x8f */
620     return true;
621 }
622 static bool Eia608ParseExtended( eia608_t *h, uint8_t d1, uint8_t d2 )
623 {
624     assert( d2 >= 0x20 && d2 <= 0x3f );
625     assert( d1 == 0x12 || d1 == 0x13 );
626     if( d1 == 0x12 )
627         d2 += 0x70; /* We use charaters 0x90-0xaf */
628     else
629         d2 += 0x90; /* We use charaters 0xb0-0xcf */
630
631     /* The extended characters replace the previous one with a more
632      * advanced one */
633     Eia608Cursor( h, -1 );
634     Eia608Write( h, d2 );
635     return true;
636 }
637 static bool Eia608ParseCommand0x14( eia608_t *h, uint8_t d2 )
638 {
639     bool b_changed = false;
640
641     switch( d2 )
642     {
643     case 0x20:  /* Resume caption loading */
644         h->mode = EIA608_MODE_POPUP;
645         break;
646     case 0x21:  /* Backspace */
647         Eia608Erase( h );
648         b_changed = true;
649         break;
650     case 0x22:  /* Reserved */
651     case 0x23:
652         break;
653     case 0x24:  /* Delete to end of row */
654         Eia608EraseToEndOfRow( h );
655         break;
656     case 0x25:  /* Rollup 2 */
657     case 0x26:  /* Rollup 3 */
658     case 0x27:  /* Rollup 4 */
659         if( h->mode == EIA608_MODE_POPUP || h->mode == EIA608_MODE_PAINTON )
660         {
661             Eia608EraseScreen( h, true );
662             Eia608EraseScreen( h, false );
663             b_changed = true;
664         }
665
666         if( d2 == 0x25 )
667             h->mode = EIA608_MODE_ROLLUP_2;
668         else if( d2 == 0x26 )
669             h->mode = EIA608_MODE_ROLLUP_3;
670         else
671             h->mode = EIA608_MODE_ROLLUP_4;
672
673         h->cursor.i_column = 0;
674         h->cursor.i_row = h->i_row_rollup;
675         break;
676     case 0x28:  /* Flash on */
677         /* TODO */
678         break;
679     case 0x29:  /* Resume direct captionning */
680         h->mode = EIA608_MODE_PAINTON;
681         break;
682     case 0x2a:  /* Text restart */
683         /* TODO */
684         break;
685
686     case 0x2b: /* Resume text display */
687         h->mode = EIA608_MODE_TEXT;
688         break;
689
690     case 0x2c: /* Erase displayed memory */
691         Eia608EraseScreen( h, true );
692         b_changed = true;
693         break;
694     case 0x2d: /* Carriage return */
695         Eia608RollUp(h);
696         b_changed = true;
697         break;
698     case 0x2e: /* Erase non displayed memory */
699         Eia608EraseScreen( h, false );
700         break;
701     case 0x2f: /* End of caption (flip screen if not paint on) */
702         if( h->mode != EIA608_MODE_PAINTON )
703             h->i_screen = 1 - h->i_screen;
704         h->mode = EIA608_MODE_POPUP;
705         h->cursor.i_column = 0;
706         h->cursor.i_row = 0;
707         h->color = EIA608_COLOR_DEFAULT;
708         h->font = EIA608_FONT_REGULAR;
709         b_changed = true;
710         break;
711     }
712     return b_changed;
713 }
714 static bool Eia608ParseCommand0x17( eia608_t *h, uint8_t d2 )
715 {
716     switch( d2 )
717     {
718     case 0x21:  /* Tab offset 1 */
719         Eia608Cursor( h, 1 );
720         break;
721     case 0x22:  /* Tab offset 2 */
722         Eia608Cursor( h, 2 );
723         break;
724     case 0x23:  /* Tab offset 3 */
725         Eia608Cursor( h, 3 );
726         break;
727     }
728     return false;
729 }
730 static bool Eia608ParsePac( eia608_t *h, uint8_t d1, uint8_t d2 )
731 {
732     static const int pi_row[] = {
733         11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
734     };
735     const int i_row_index = ( (d1<<1) & 0x0e) | ( (d2>>5) & 0x01 );
736
737     assert( d2 >= 0x40 && d2 <= 0x7f );
738
739     if( pi_row[i_row_index] <= 0 )
740         return false;
741
742     /* Row */
743     if( h->mode != EIA608_MODE_TEXT )
744         h->cursor.i_row = pi_row[i_row_index] - 1;
745     h->i_row_rollup = pi_row[i_row_index] - 1;
746     /* Column */
747     if( d2 >= 0x60 )
748         d2 -= 0x60;
749     else if( d2 >= 0x40 )
750         d2 -= 0x40;
751     h->cursor.i_column = pac2_attribs[d2].i_column;
752     h->color = pac2_attribs[d2].i_color;
753     h->font  = pac2_attribs[d2].i_font;
754
755     return false;
756 }
757
758 static bool Eia608ParseData( eia608_t *h, uint8_t d1, uint8_t d2 )
759 {
760     bool b_changed = false;
761
762     if( d1 >= 0x18 && d1 <= 0x1f )
763         d1 -= 8;
764
765 #define ON( d2min, d2max, cmd ) do { if( d2 >= d2min && d2 <= d2max ) b_changed = cmd; } while(0)
766     switch( d1 )
767     {
768     case 0x11:
769         ON( 0x20, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
770         ON( 0x30, 0x3f, Eia608ParseDouble( h, d2 ) );
771         break;
772     case 0x12: case 0x13:
773         ON( 0x20, 0x3f, Eia608ParseExtended( h, d1, d2 ) );
774         break;
775     case 0x14: case 0x15:
776         ON( 0x20, 0x2f, Eia608ParseCommand0x14( h, d2 ) );
777         break;
778     case 0x17:
779         ON( 0x21, 0x22, Eia608ParseCommand0x17( h, d2 ) );
780         ON( 0x2e, 0x2f, Eia608ParseTextAttribute( h, d2 ) );
781         break;
782     }
783     if( d1 == 0x10 )
784         ON( 0x40, 0x5f, Eia608ParsePac( h, d1, d2 ) );
785     else if( d1 >= 0x11 && d1 <= 0x17 )
786         ON( 0x40, 0x7f, Eia608ParsePac( h, d1, d2 ) );
787 #undef ON
788     if( d1 >= 0x20 )
789     {
790         b_changed = Eia608ParseSingle( h, d1 );
791         if( d2 >= 0x20 )
792             b_changed |= Eia608ParseSingle( h, d2 );
793     }
794     return b_changed;
795 }
796
797 static void Eia608TextUtf8( char *psz_utf8, uint8_t c ) // Returns number of bytes used
798 {
799 #define E1(c,u) { c, { u, '\0' } }
800 #define E2(c,u1,u2) { c, { u1, u2, '\0' } }
801 #define E3(c,u1,u2,u3) { c, { u1, u2, u3, '\0' } }
802     static const struct {
803         uint8_t c;
804         char utf8[3+1];
805     } c2utf8[] = {
806         // Regular line-21 character set, mostly ASCII except these exceptions
807         E2( 0x2a, 0xc3,0xa1), // lowercase a, acute accent
808         E2( 0x5c, 0xc3,0xa9), // lowercase e, acute accent
809         E2( 0x5e, 0xc3,0xad), // lowercase i, acute accent
810         E2( 0x5f, 0xc3,0xb3), // lowercase o, acute accent
811         E2( 0x60, 0xc3,0xba), // lowercase u, acute accent
812         E2( 0x7b, 0xc3,0xa7), // lowercase c with cedilla
813         E2( 0x7c, 0xc3,0xb7), // division symbol
814         E2( 0x7d, 0xc3,0x91), // uppercase N tilde
815         E2( 0x7e, 0xc3,0xb1), // lowercase n tilde
816         // THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
817         // THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F
818         E2( 0x80, 0xc2,0xae), // Registered symbol (R)
819         E2( 0x81, 0xc2,0xb0), // degree sign
820         E2( 0x82, 0xc2,0xbd), // 1/2 symbol
821         E2( 0x83, 0xc2,0xbf), // Inverted (open) question mark
822         E3( 0x84, 0xe2,0x84,0xa2), // Trademark symbol (TM)
823         E2( 0x85, 0xc2,0xa2), // Cents symbol
824         E2( 0x86, 0xc2,0xa3), // Pounds sterling
825         E3( 0x87, 0xe2,0x99,0xaa), // Music note
826         E2( 0x88, 0xc3,0xa0), // lowercase a, grave accent
827         E2( 0x89, 0xc2,0xa0), // transparent space
828         E2( 0x8a, 0xc3,0xa8), // lowercase e, grave accent
829         E2( 0x8b, 0xc3,0xa2), // lowercase a, circumflex accent
830         E2( 0x8c, 0xc3,0xaa), // lowercase e, circumflex accent
831         E2( 0x8d, 0xc3,0xae), // lowercase i, circumflex accent
832         E2( 0x8e, 0xc3,0xb4), // lowercase o, circumflex accent
833         E2( 0x8f, 0xc3,0xbb), // lowercase u, circumflex accent
834         // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
835         // THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F
836         E2( 0x90, 0xc3,0x81), // capital letter A with acute
837         E2( 0x91, 0xc3,0x89), // capital letter E with acute
838         E2( 0x92, 0xc3,0x93), // capital letter O with acute
839         E2( 0x93, 0xc3,0x9a), // capital letter U with acute
840         E2( 0x94, 0xc3,0x9c), // capital letter U with diaresis
841         E2( 0x95, 0xc3,0xbc), // lowercase letter U with diaeresis
842         E1( 0x96, 0x27), // apostrophe
843         E2( 0x97, 0xc2,0xa1), // inverted exclamation mark
844         E1( 0x98, 0x2a), // asterisk
845         E1( 0x99, 0x27), // apostrophe (yes, duped). See CCADI source code.
846         E1( 0x9a, 0x2d), // hyphen-minus
847         E2( 0x9b, 0xc2,0xa9), // copyright sign
848         E3( 0x9c, 0xe2,0x84,0xa0), // Service mark
849         E1( 0x9d, 0x2e), // Full stop (.)
850         E3( 0x9e, 0xe2,0x80,0x9c), // Quotation mark
851         E3( 0x9f, 0xe2,0x80,0x9d), // Quotation mark
852         E2( 0xa0, 0xc3,0x80), // uppercase A, grave accent
853         E2( 0xa1, 0xc3,0x82), // uppercase A, circumflex
854         E2( 0xa2, 0xc3,0x87), // uppercase C with cedilla
855         E2( 0xa3, 0xc3,0x88), // uppercase E, grave accent
856         E2( 0xa4, 0xc3,0x8a), // uppercase E, circumflex
857         E2( 0xa5, 0xc3,0x8b), // capital letter E with diaresis
858         E2( 0xa6, 0xc3,0xab), // lowercase letter e with diaresis
859         E2( 0xa7, 0xc3,0x8e), // uppercase I, circumflex
860         E2( 0xa8, 0xc3,0x8f), // uppercase I, with diaresis
861         E2( 0xa9, 0xc3,0xaf), // lowercase i, with diaresis
862         E2( 0xaa, 0xc3,0x94), // uppercase O, circumflex
863         E2( 0xab, 0xc3,0x99), // uppercase U, grave accent
864         E2( 0xac, 0xc3,0xb9), // lowercase u, grave accent
865         E2( 0xad, 0xc3,0x9b), // uppercase U, circumflex
866         E2( 0xae, 0xc2,0xab), // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
867         E2( 0xaf, 0xc2,0xbb), // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
868         // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
869         // THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F
870         E2( 0xb0, 0xc3,0x83), // Uppercase A, tilde
871         E2( 0xb1, 0xc3,0xa3), // Lowercase a, tilde
872         E2( 0xb2, 0xc3,0x8d), // Uppercase I, acute accent
873         E2( 0xb3, 0xc3,0x8c), // Uppercase I, grave accent
874         E2( 0xb4, 0xc3,0xac), // Lowercase i, grave accent
875         E2( 0xb5, 0xc3,0x92), // Uppercase O, grave accent
876         E2( 0xb6, 0xc3,0xb2), // Lowercase o, grave accent
877         E2( 0xb7, 0xc3,0x95), // Uppercase O, tilde
878         E2( 0xb8, 0xc3,0xb5), // Lowercase o, tilde
879         E1( 0xb9, 0x7b), // Open curly brace
880         E1( 0xba, 0x7d), // Closing curly brace
881         E1( 0xbb, 0x5c), // Backslash
882         E1( 0xbc, 0x5e), // Caret
883         E1( 0xbd, 0x5f), // Underscore
884         E2( 0xbe, 0xc2,0xa6), // Pipe (broken bar)
885         E1( 0xbf, 0x7e), // Tilde (utf8 code unsure)
886         E2( 0xc0, 0xc3,0x84), // Uppercase A, umlaut
887         E2( 0xc1, 0xc3,0xa4), // Lowercase A, umlaut
888         E2( 0xc2, 0xc3,0x96), // Uppercase O, umlaut
889         E2( 0xc3, 0xc3,0xb6), // Lowercase o, umlaut
890         E2( 0xc4, 0xc3,0x9f), // Esszett (sharp S)
891         E2( 0xc5, 0xc2,0xa5), // Yen symbol
892         E2( 0xc6, 0xc2,0xa4), // Currency symbol
893         E1( 0xc7, 0x7c), // Vertical bar
894         E2( 0xc8, 0xc3,0x85), // Uppercase A, ring
895         E2( 0xc9, 0xc3,0xa5), // Lowercase A, ring
896         E2( 0xca, 0xc3,0x98), // Uppercase O, slash
897         E2( 0xcb, 0xc3,0xb8), // Lowercase o, slash
898         E3( 0xcc, 0xe2,0x8c,0x9c), // Upper left corner
899         E3( 0xcd, 0xe2,0x8c,0x9d), // Upper right corner
900         E3( 0xce, 0xe2,0x8c,0x9e), // Lower left corner
901         E3( 0xcf, 0xe2,0x8c,0x9f), // Lower right corner
902
903         E1(0,0)
904     };
905 #undef E3
906 #undef E2
907 #undef E1
908
909     static const int i_c2utf8 = sizeof(c2utf8)/sizeof(*c2utf8);
910     int i;
911
912     for( i = 0; i < i_c2utf8; i++ )
913     {
914         if( c2utf8[i].c == c )
915             break;
916     }
917     if( i >= i_c2utf8 )
918     {
919         psz_utf8[0] = c < 0x80 ? c : '?';   /* Normal : Unsupported */
920         psz_utf8[1] = '\0';
921     }
922     else
923     {
924         strcpy( psz_utf8, c2utf8[i].utf8 );
925     }
926 }
927
928 static void Eia608Strlcat( char *d, const char *s, int i_max )
929 {
930     if( i_max > 1 )
931         strncat( d, s, i_max-1 - strnlen(d, i_max-1));
932     if( i_max > 0 )
933         d[i_max-1] = '\0';
934 }
935
936 static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_text_max, int i_row, bool b_html )
937 {
938     const uint8_t *p_char = screen->characters[i_row];
939     const eia608_color_t *p_color = screen->colors[i_row];
940     const eia608_font_t *p_font = screen->fonts[i_row];
941     int i_start;
942     int i_end;
943     int x;
944     eia608_color_t last_color = EIA608_COLOR_DEFAULT;
945     bool     b_last_italics = false;
946     bool     b_last_underline = false;
947
948     /* Search the start */
949     i_start = 0;
950     while( i_start < EIA608_SCREEN_COLUMNS-1 && p_char[i_start] == ' ' )
951         i_start++;
952
953     /* Search the end */
954     i_end = EIA608_SCREEN_COLUMNS-1;
955     while( i_end > i_start && p_char[i_end] == ' ' )
956         i_end--;
957
958     /* */
959 #define CAT(t) Eia608Strlcat( psz_text, t, i_text_max )
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         char utf8[4];
966
967         /* */
968         if( b_html )
969         {
970             bool b_close_color, b_close_italics, b_close_underline;
971
972             /* We create the tags font / i / u in that orders */
973             b_close_color = color != last_color && last_color != EIA608_COLOR_DEFAULT;
974             b_close_italics = !b_italics && b_last_italics;
975             b_close_underline = !b_underline && b_last_underline;
976
977             /* Be sure to create valid html */
978             b_close_italics |= b_last_italics && b_close_color;
979             b_close_underline |= b_last_underline && ( b_close_italics || b_close_color );
980
981             if( b_close_underline )
982                 CAT( "</u>" );
983             if( b_close_italics )
984                 CAT( "</i>" );
985             if( b_close_color )
986                 CAT( "</font>" );
987
988             if( color != EIA608_COLOR_DEFAULT && color != last_color)
989             {
990                 static const char *ppsz_color[] = {
991                     "#ffffff",  // white
992                     "#00ff00",  // green
993                     "#0000ff",  // blue
994                     "#00ffff",  // cyan
995                     "#ff0000",  // red
996                     "#ffff00",  // yellow
997                     "#ff00ff",  // magenta
998                     "#ffffff",  // user defined XXX we use white
999                 };
1000                 CAT( "<font color=" );
1001                 CAT( ppsz_color[color] );
1002                 CAT( ">" );
1003             }
1004             if( ( b_close_italics && b_italics ) || ( b_italics && !b_last_italics ) )
1005                 CAT( "<i>" );
1006             if( ( b_close_underline && b_underline ) || ( b_underline && !b_last_underline ) )
1007                 CAT( "<u>" );
1008         }
1009
1010         /* */ 
1011         Eia608TextUtf8( utf8, p_char[x] );
1012         CAT( utf8 );
1013
1014         /* */
1015         b_last_underline = b_underline;
1016         b_last_italics = b_italics;
1017         last_color = color;
1018     }
1019     if( b_html )
1020     {
1021         if( b_last_underline )
1022             CAT( "</u>" );
1023         if( b_last_italics )
1024             CAT( "</i>" );
1025         if( last_color != EIA608_COLOR_DEFAULT )
1026             CAT( "</font>" );
1027     }
1028 #undef CAT
1029 }
1030
1031 /* */
1032 static void Eia608Init( eia608_t *h )
1033 {
1034     memset( h, 0, sizeof(*h) );
1035
1036     /* */
1037     h->i_channel = -1;
1038
1039     h->i_screen = 0;
1040     Eia608ClearScreen( h, 0 );
1041     Eia608ClearScreen( h, 1 );
1042
1043     /* Cursor for writing text */
1044     h->cursor.i_column = 0;
1045     h->cursor.i_row = 0;
1046
1047     h->last.d1 = 0x00;
1048     h->last.d2 = 0x00;
1049     h->mode = EIA608_MODE_POPUP;
1050     h->color = EIA608_COLOR_DEFAULT;
1051     h->font = EIA608_FONT_REGULAR;
1052     h->i_row_rollup = EIA608_SCREEN_ROWS-1;
1053 }
1054 static bool Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] )
1055 {
1056     const uint8_t d1 = data[0] & 0x7f; /* Removed parity bit */
1057     const uint8_t d2 = data[1] & 0x7f;
1058     bool b_screen_changed = false;
1059
1060     if( d1 == 0 && d2 == 0 )
1061         return false;   /* Ignore padding (parity check are sometimes invalid on them) */
1062
1063     Eia608ParseChannel( h, data );
1064     if( h->i_channel != i_channel_selected )
1065         return false;
1066     //fprintf( stderr, "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC %x %x\n", data[0], data[1] );
1067
1068     if( d1 >= 0x10 )
1069     {
1070         if( d1 >= 0x20 ||
1071             d1 != h->last.d1 || d2 != h->last.d2 ) /* Command codes can be repeated */
1072             b_screen_changed = Eia608ParseData( h, d1,d2 );
1073
1074         h->last.d1 = d1;
1075         h->last.d2 = d2;
1076     }
1077     else if( ( d1 >= 0x01 && d1 <= 0x0E ) || d1 == 0x0F )
1078     {
1079         /* XDS block / End of XDS block */
1080     }
1081     return b_screen_changed;
1082 }
1083
1084 static char *Eia608Text( eia608_t *h, bool b_html )
1085 {
1086     const int i_size = EIA608_SCREEN_ROWS * 3 * EIA608_SCREEN_COLUMNS+1;
1087     struct eia608_screen *screen = &h->screen[h->i_screen];
1088     bool b_first = true;
1089     char *psz;
1090     int i;
1091
1092     /* We allocate a buffer big enough for normal case */
1093     psz = malloc( i_size );
1094     if( !psz )
1095         return NULL;
1096     *psz = '\0';
1097     if( b_html )
1098         Eia608Strlcat( psz, "<text>", i_size );
1099     for( i = 0; i < EIA608_SCREEN_ROWS; i++ )
1100     {
1101         if( !screen->row_used[i] )
1102             continue;
1103
1104         if( !b_first )
1105             Eia608Strlcat( psz, b_html ? "<br />" : "\n", i_size );
1106         b_first = false;
1107
1108         Eia608TextLine( screen, psz, i_size, i, b_html );
1109     }
1110     if( b_html )
1111         Eia608Strlcat( psz, "</text>", i_size );
1112     return psz;
1113 }
1114
1115 static void Eia608Exit( eia608_t *h )
1116 {
1117     VLC_UNUSED( h );
1118 }
1119