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