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