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