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