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