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