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