]> git.sesse.net Git - vlc/blob - modules/codec/cvdsub.c
Removes trailing spaces. Removes tabs.
[vlc] / modules / codec / cvdsub.c
1 /*****************************************************************************
2  * cvd.c : CVD Subtitle decoder
3  *****************************************************************************
4  * Copyright (C) 2003, 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Rocky Bernstein
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Julio Sanchez Fernandez (http://subhandler.sourceforge.net)
10  *          Laurent Aimar <fenrir@via.ecp.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <vlc/vlc.h>
31 #include <vlc_vout.h>
32 #include <vlc_codec.h>
33
34 #include "vlc_bits.h"
35
36 #define DEBUG_CVDSUB 1
37
38 /*****************************************************************************
39  * Module descriptor.
40  *****************************************************************************/
41 static int  DecoderOpen   ( vlc_object_t * );
42 static int  PacketizerOpen( vlc_object_t * );
43 static void DecoderClose  ( vlc_object_t * );
44
45 vlc_module_begin();
46     set_description( _("CVD subtitle decoder") );
47     set_capability( "decoder", 50 );
48     set_callbacks( DecoderOpen, DecoderClose );
49
50     add_submodule();
51     set_description( _("Chaoji VCD subtitle packetizer") );
52     set_capability( "packetizer", 50 );
53     set_callbacks( PacketizerOpen, DecoderClose );
54 vlc_module_end();
55
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59 static subpicture_t *Decode( decoder_t *, block_t ** );
60 static block_t *Packetize  ( decoder_t *, block_t ** );
61 static block_t *Reassemble ( decoder_t *, block_t * );
62 static void ParseMetaInfo  ( decoder_t *, block_t * );
63 static void ParseHeader    ( decoder_t *, block_t * );
64 static subpicture_t *DecodePacket( decoder_t *, block_t * );
65 static void RenderImage( decoder_t *, block_t *, subpicture_region_t * );
66
67 #define SUBTITLE_BLOCK_EMPTY 0
68 #define SUBTITLE_BLOCK_PARTIAL 1
69 #define SUBTITLE_BLOCK_COMPLETE 2
70
71 struct decoder_sys_t
72 {
73   int      b_packetizer;
74
75   int      i_state;    /* data-gathering state for this subtitle */
76
77   block_t  *p_spu;   /* Bytes of the packet. */
78
79   int     i_spu_size;     /* goal for subtitle_data_pos while gathering,
80                              size of used subtitle_data later */
81
82   uint16_t i_image_offset;      /* offset from subtitle_data to compressed
83                                    image data */
84   int i_image_length;           /* size of the compressed image data */
85   int first_field_offset;       /* offset of even raster lines */
86   int second_field_offset;      /* offset of odd raster lines */
87   int metadata_offset;          /* offset to data describing the image */
88   int metadata_length;          /* length of metadata */
89
90   mtime_t i_duration;   /* how long to display the image, 0 stands
91                            for "until next subtitle" */
92
93   uint16_t i_x_start, i_y_start; /* position of top leftmost pixel of
94                                     image when displayed */
95   uint16_t i_width, i_height;    /* dimensions in pixels of image */
96
97   uint8_t p_palette[4][4];       /* Palette of colors used in subtitle */
98   uint8_t p_palette_highlight[4][4];
99 };
100
101 /*****************************************************************************
102  * DecoderOpen: open/initialize the cvdsub decoder.
103  *****************************************************************************/
104 static int DecoderOpen( vlc_object_t *p_this )
105 {
106     decoder_t     *p_dec = (decoder_t*)p_this;
107     decoder_sys_t *p_sys;
108
109     if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'c','v','d',' ' ) )
110     {
111         return VLC_EGENERIC;
112     }
113
114     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
115
116     p_sys->b_packetizer  = VLC_FALSE;
117
118     p_sys->i_state = SUBTITLE_BLOCK_EMPTY;
119     p_sys->p_spu   = NULL;
120
121     es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'c','v','d',' ' ) );
122
123     p_dec->pf_decode_sub = Decode;
124     p_dec->pf_packetize  = Packetize;
125
126     return VLC_SUCCESS;
127 }
128
129 /*****************************************************************************
130  * PacketizerOpen: open/initialize the cvdsub packetizer.
131  *****************************************************************************/
132 static int PacketizerOpen( vlc_object_t *p_this )
133 {
134     decoder_t *p_dec = (decoder_t*)p_this;
135
136     if( DecoderOpen( p_this ) != VLC_SUCCESS ) return VLC_EGENERIC;
137
138     p_dec->p_sys->b_packetizer = VLC_TRUE;
139
140     return VLC_SUCCESS;
141 }
142
143 /*****************************************************************************
144  * DecoderClose: closes the cvdsub decoder/packetizer.
145  *****************************************************************************/
146 void DecoderClose( vlc_object_t *p_this )
147 {
148     decoder_t     *p_dec = (decoder_t*)p_this;
149     decoder_sys_t *p_sys = p_dec->p_sys;
150
151     if( p_sys->p_spu ) block_ChainRelease( p_sys->p_spu );
152     free( p_sys );
153 }
154
155 /*****************************************************************************
156  * Decode:
157  *****************************************************************************/
158 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
159 {
160     block_t *p_block, *p_spu;
161
162     if( pp_block == NULL || *pp_block == NULL ) return NULL;
163
164     p_block = *pp_block;
165     *pp_block = NULL;
166
167     if( !(p_spu = Reassemble( p_dec, p_block )) ) return NULL;
168
169     /* Parse and decode */
170     return DecodePacket( p_dec, p_spu );
171 }
172
173 /*****************************************************************************
174  * Packetize:
175  *****************************************************************************/
176 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
177 {
178     block_t *p_block, *p_spu;
179
180     if( pp_block == NULL || *pp_block == NULL ) return NULL;
181
182     p_block = *pp_block;
183     *pp_block = NULL;
184
185     if( !(p_spu = Reassemble( p_dec, p_block )) ) return NULL;
186
187     p_spu->i_dts = p_spu->i_pts;
188     p_spu->i_length = 0;
189
190     return p_spu;
191 }
192
193
194 /*****************************************************************************
195  Reassemble:
196
197  Data for single screen subtitle may come in several non-contiguous
198  packets of a stream. This routine is called when the next packet in
199  the stream comes in. The job of this routine is to parse the header,
200  if this is the beginning, and combine the packets into one complete
201  subtitle unit.
202
203  If everything is complete, we will return a block. Otherwise return
204  NULL.
205
206  *****************************************************************************/
207 #define SPU_HEADER_LEN 1
208
209 static block_t *Reassemble( decoder_t *p_dec, block_t *p_block )
210 {
211     decoder_sys_t *p_sys = p_dec->p_sys;
212     uint8_t *p_buffer;
213
214     if( p_block->i_buffer < SPU_HEADER_LEN )
215     {
216         msg_Dbg( p_dec, "invalid packet header (size %d < %d)" ,
217                  p_block->i_buffer, SPU_HEADER_LEN );
218         block_Release( p_block );
219         return NULL;
220     }
221
222     p_buffer = p_block->p_buffer;
223
224     /* From the scant data on the format, there is only only way known
225      * to detect the first packet in a subtitle.  The first packet
226      * seems to have a valid PTS while later packets for the same
227      * image don't. */
228     if( p_sys->i_state == SUBTITLE_BLOCK_EMPTY && p_block->i_pts == 0 )
229     {
230         msg_Warn( p_dec, "first packet expected but no PTS present");
231         return NULL;
232     }
233
234     p_block->p_buffer += SPU_HEADER_LEN;
235     p_block->i_buffer -= SPU_HEADER_LEN;
236
237     /* First packet in the subtitle block */
238     if( p_sys->i_state == SUBTITLE_BLOCK_EMPTY ) ParseHeader( p_dec, p_block );
239
240     block_ChainAppend( &p_sys->p_spu, p_block );
241     p_sys->p_spu = block_ChainGather( p_sys->p_spu );
242
243     if( p_sys->p_spu->i_buffer >= p_sys->i_spu_size )
244     {
245         block_t *p_spu = p_sys->p_spu;
246
247         if( p_spu->i_buffer != p_sys->i_spu_size )
248         {
249             msg_Warn( p_dec, "SPU packets size=%d should be %d",
250                       p_spu->i_buffer, p_sys->i_spu_size );
251         }
252
253         msg_Dbg( p_dec, "subtitle packet complete, size=%d", p_spu->i_buffer);
254
255         ParseMetaInfo( p_dec, p_spu );
256
257         p_sys->i_state = SUBTITLE_BLOCK_EMPTY;
258         p_sys->p_spu = 0;
259         return p_spu;
260     }
261     else
262     {
263         /* Not last block in subtitle, so wait for another. */
264         p_sys->i_state = SUBTITLE_BLOCK_PARTIAL;
265     }
266
267     return NULL;
268 }
269
270 /*
271   We do not have information on the subtitle format used on CVD's
272   except the submux sample code and a couple of samples of dubious
273   origin. Thus, this is the result of reading some code whose
274   correctness is not known and some experimentation.
275
276   CVD subtitles are different in several ways from SVCD OGT subtitles.
277   Image comes first and metadata is at the end.  So that the metadata
278   can be found easily, the subtitle packet starts with two bytes
279   (everything is big-endian again) that give the total size of the
280   subtitle data and the offset to the metadata - i.e. size of the
281   image data plus the four bytes at the beginning.
282
283   Image data comes interlaced is run-length encoded.  Each field is a
284   four-bit nibble. Each nibble contains a two-bit repeat count and a
285   two-bit color number so that up to three pixels can be described in
286   four bits.  The function of a 0 repeat count is unknown; it might be
287   used for RLE extension.  However when the full nibble is zero, the
288   rest of the line is filled with the color value in the next nibble.
289   It is unknown what happens if the color value is greater than three.
290   The rest seems to use a 4-entries palette.  It is not impossible
291   that the fill-line complete case above is not as described and the
292   zero repeat count means fill line.  The sample code never produces
293   this, so it may be untested.
294 */
295
296 static void ParseHeader( decoder_t *p_dec, block_t *p_block )
297 {
298     decoder_sys_t *p_sys = p_dec->p_sys;
299     uint8_t *p = p_block->p_buffer;
300
301     p_sys->i_spu_size = (p[0] << 8) + p[1] + 4; p += 2;
302
303     /* FIXME: check data sanity */
304     p_sys->metadata_offset = (p[0] <<  8) +   p[1]; p +=2;
305     p_sys->metadata_length = p_sys->i_spu_size - p_sys->metadata_offset;
306
307     p_sys->i_image_offset = 4;
308     p_sys->i_image_length = p_sys->metadata_offset - p_sys->i_image_offset;
309
310 #ifdef DEBUG_CVDSUB
311     msg_Dbg( p_dec, "total size: %d  image size: %d",
312              p_sys->i_spu_size, p_sys->i_image_length );
313 #endif
314 }
315
316 /*
317   We parse the metadata information here.
318
319   Although metadata information does not have to come in a fixed field
320   order, every metadata field consists of a tag byte followed by
321   parameters. In all cases known, the size including tag byte is
322   exactly four bytes in length.
323 */
324
325 #define ExtractXY(x, y) x = ((p[1]&0x0f)<<6) + (p[2]>>2); \
326                         y = ((p[2]&0x03)<<8) + p[3];
327
328 static void ParseMetaInfo( decoder_t *p_dec, block_t *p_spu  )
329 {
330     /* Last packet in subtitle block. */
331
332     decoder_sys_t *p_sys = p_dec->p_sys;
333     uint8_t       *p     = p_spu->p_buffer + p_sys->metadata_offset;
334     uint8_t       *p_end = p + p_sys->metadata_length;
335
336     for( ; p < p_end; p += 4 )
337     {
338         switch( p[0] )
339         {
340         case 0x04: /* subtitle duration in 1/90000ths of a second */
341             p_sys->i_duration = (p[1]<<16) + (p[2]<<8) + p[3];
342
343 #ifdef DEBUG_CVDSUB
344             msg_Dbg( p_dec, "subtitle display duration %lu secs",
345                      (long unsigned int)(p_sys->i_duration / 90000) );
346 #endif
347             p_sys->i_duration *= 100 / 9;
348             break;
349
350         case 0x0c: /* unknown */
351 #ifdef DEBUG_CVDSUB
352             msg_Dbg( p_dec, "subtitle command unknown 0x%0x 0x%0x 0x%0x 0x%0x",
353                      (int)p[0], (int)p[1], (int)p[2], (int)p[3] );
354 #endif
355             break;
356
357         case 0x17: /* coordinates of subtitle upper left x, y position */
358             ExtractXY(p_sys->i_x_start, p_sys->i_y_start);
359
360 #ifdef DEBUG_CVDSUB
361             msg_Dbg( p_dec, "start position (%d,%d)",
362                      p_sys->i_x_start, p_sys->i_y_start );
363 #endif
364             break;
365
366         case 0x1f: /* coordinates of subtitle bottom right x, y position */
367         {
368             int lastx;
369             int lasty;
370             ExtractXY(lastx, lasty);
371             p_sys->i_width  = lastx - p_sys->i_x_start + 1;
372             p_sys->i_height = lasty - p_sys->i_y_start + 1;
373
374 #ifdef DEBUG_CVDSUB
375             msg_Dbg( p_dec, "end position (%d,%d), w x h: %dx%d",
376                      lastx, lasty, p_sys->i_width, p_sys->i_height );
377 #endif
378             break;
379         }
380
381         case 0x24:
382         case 0x25:
383         case 0x26:
384         case 0x27:
385         {
386             uint8_t v = p[0] - 0x24;
387
388 #ifdef DEBUG_CVDSUB
389             /* Primary Palette */
390             msg_Dbg( p_dec, "primary palette %d (y,u,v): (0x%0x,0x%0x,0x%0x)",
391                      (int)v, (int)p[1], (int)p[2], (int)p[3] );
392 #endif
393
394             p_sys->p_palette[v][0] = p[1]; /* Y */
395             p_sys->p_palette[v][1] = p[3]; /* Cr / V */
396             p_sys->p_palette[v][2] = p[2]; /* Cb / U */
397             break;
398         }
399
400         case 0x2c:
401         case 0x2d:
402         case 0x2e:
403         case 0x2f:
404         {
405             uint8_t v = p[0] - 0x2c;
406
407 #ifdef DEBUG_CVDSUB
408             msg_Dbg( p_dec,"highlight palette %d (y,u,v): (0x%0x,0x%0x,0x%0x)",
409                      (int)v, (int)p[1], (int)p[2], (int)p[3] );
410 #endif
411
412             /* Highlight Palette */
413             p_sys->p_palette_highlight[v][0] = p[1]; /* Y */
414             p_sys->p_palette_highlight[v][1] = p[3]; /* Cr / V */
415             p_sys->p_palette_highlight[v][2] = p[2]; /* Cb / U */
416             break;
417         }
418
419         case 0x37:
420             /* transparency for primary palette */
421             p_sys->p_palette[0][3] = (p[3] & 0x0f) << 4;
422             p_sys->p_palette[1][3] = (p[3] >> 4) << 4;
423             p_sys->p_palette[2][3] = (p[2] & 0x0f) << 4;
424             p_sys->p_palette[3][3] = (p[2] >> 4) << 4;
425
426 #ifdef DEBUG_CVDSUB
427             msg_Dbg( p_dec, "transparency for primary palette 0..3: "
428                      "0x%0x 0x%0x 0x%0x 0x%0x",
429                      (int)p_sys->p_palette[0][3], (int)p_sys->p_palette[1][3],
430                      (int)p_sys->p_palette[2][3], (int)p_sys->p_palette[3][3]);
431 #endif
432             break;
433
434         case 0x3f:
435             /* transparency for highlight palette */
436             p_sys->p_palette_highlight[0][3] = (p[2] & 0x0f) << 4;
437             p_sys->p_palette_highlight[1][3] = (p[2] >> 4) << 4;
438             p_sys->p_palette_highlight[2][3] = (p[1] & 0x0f) << 4;
439             p_sys->p_palette_highlight[3][3] = (p[1] >> 4) << 4;
440
441 #ifdef DEBUG_CVDSUB
442             msg_Dbg( p_dec, "transparency for highlight palette 0..3: "
443                      "0x%0x 0x%0x 0x%0x 0x%0x",
444                      (int)p_sys->p_palette_highlight[0][3],
445                      (int)p_sys->p_palette_highlight[1][3],
446                      (int)p_sys->p_palette_highlight[2][3],
447                      (int)p_sys->p_palette_highlight[3][3] );
448 #endif
449             break;
450
451         case 0x47:
452             /* offset to start of even rows of interlaced image, we correct
453              * to make it relative to i_image_offset (usually 4) */
454             p_sys->first_field_offset =
455                 (p[2] << 8) + p[3] - p_sys->i_image_offset;
456 #ifdef DEBUG_CVDSUB
457             msg_Dbg( p_dec, "1st_field_offset %d", p_sys->first_field_offset );
458 #endif
459             break;
460
461         case 0x4f:
462             /* offset to start of odd rows of interlaced image, we correct
463              * to make it relative to i_image_offset (usually 4) */
464             p_sys->second_field_offset =
465                 (p[2] << 8) + p[3] - p_sys->i_image_offset;
466 #ifdef DEBUG_CVDSUB
467             msg_Dbg( p_dec, "2nd_field_offset %d", p_sys->second_field_offset);
468 #endif
469             break;
470
471         default:
472 #ifdef DEBUG_CVDSUB
473             msg_Warn( p_dec, "unknown sequence in control header "
474                       "0x%0x 0x%0x 0x%0x 0x%0x", p[0], p[1], p[2], p[3]);
475 #endif
476         }
477     }
478 }
479
480 /*****************************************************************************
481  * DecodePacket: parse and decode an SPU packet
482  *****************************************************************************
483  * This function parses and decodes an SPU packet and, if valid, returns a
484  * subpicture.
485  *****************************************************************************/
486 static subpicture_t *DecodePacket( decoder_t *p_dec, block_t *p_data )
487 {
488     decoder_sys_t *p_sys = p_dec->p_sys;
489     subpicture_t  *p_spu;
490     subpicture_region_t *p_region;
491     video_format_t fmt;
492     int i;
493
494     /* Allocate the subpicture internal data. */
495     p_spu = p_dec->pf_spu_buffer_new( p_dec );
496     if( !p_spu ) return NULL;
497
498     p_spu->b_pausable = VLC_TRUE;
499
500     p_spu->i_x = p_sys->i_x_start;
501     p_spu->i_x = p_spu->i_x * 3 / 4; /* FIXME: use aspect ratio for x? */
502     p_spu->i_y = p_sys->i_y_start;
503     p_spu->i_start = p_data->i_pts;
504     p_spu->i_stop  = p_data->i_pts + p_sys->i_duration;
505     p_spu->b_ephemer = VLC_TRUE;
506
507     /* Create new SPU region */
508     memset( &fmt, 0, sizeof(video_format_t) );
509     fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
510     fmt.i_aspect = VOUT_ASPECT_FACTOR;
511     fmt.i_width = fmt.i_visible_width = p_sys->i_width;
512     fmt.i_height = fmt.i_visible_height = p_sys->i_height;
513     fmt.i_x_offset = fmt.i_y_offset = 0;
514     p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
515     if( !p_region )
516     {
517         msg_Err( p_dec, "cannot allocate SPU region" );
518         //goto error;
519     }
520
521     p_spu->p_region = p_region;
522     p_region->i_x = p_region->i_y = 0;
523
524     /* Build palette */
525     fmt.p_palette->i_entries = 4;
526     for( i = 0; i < fmt.p_palette->i_entries; i++ )
527     {
528         fmt.p_palette->palette[i][0] = p_sys->p_palette[i][0];
529         fmt.p_palette->palette[i][1] = p_sys->p_palette[i][1];
530         fmt.p_palette->palette[i][2] = p_sys->p_palette[i][2];
531         fmt.p_palette->palette[i][3] = p_sys->p_palette[i][3];
532     }
533
534     RenderImage( p_dec, p_data, p_region );
535
536     return p_spu;
537 }
538
539 /*****************************************************************************
540  * ParseImage: parse and render the image part of the subtitle
541  *****************************************************************************
542  This part parses the subtitle graphical data and renders it.
543
544  Image data comes interlaced and is run-length encoded (RLE). Each
545  field is a four-bit nibbles that is further subdivided in a two-bit
546  repeat count and a two-bit color number - up to three pixels can be
547  described in four bits.  What a 0 repeat count means is unknown.  It
548  might be used for RLE extension.  There is a special case of a 0
549  repeat count though.  When the full nibble is zero, the rest of the
550  line is filled with the color value in the next nibble.  It is
551  unknown what happens if the color value is greater than three.  The
552  rest seems to use a 4-entries palette.  It is not impossible that the
553  fill-line complete case above is not as described and the zero repeat
554  count means fill line.  The sample code never produces this, so it
555  may be untested.
556
557  However we'll transform this so that that the RLE is expanded and
558  interlacing will also be removed. On output each pixel entry will by
559  a 4-bit alpha (filling 8 bits), and 8-bit y, u, and v entry.
560
561  *****************************************************************************/
562 static void RenderImage( decoder_t *p_dec, block_t *p_data,
563                          subpicture_region_t *p_region )
564 {
565     decoder_sys_t *p_sys = p_dec->p_sys;
566     uint8_t *p_dest = p_region->picture.Y_PIXELS;
567     int i_field;            /* The subtitles are interlaced */
568     int i_row, i_column;    /* scanline row/column number */
569     uint8_t i_color, i_count;
570     bs_t bs;
571
572     bs_init( &bs, p_data->p_buffer + p_sys->i_image_offset,
573              p_data->i_buffer - p_sys->i_image_offset );
574
575     for( i_field = 0; i_field < 2; i_field++ )
576     {
577         for( i_row = i_field; i_row < p_sys->i_height; i_row += 2 )
578         {
579             for( i_column = 0; i_column < p_sys->i_width; i_column++ )
580             {
581                 uint8_t i_val = bs_read( &bs, 4 );
582
583                 if( i_val == 0 )
584                 {
585                     /* Fill the rest of the line with next color */
586                     i_color = bs_read( &bs, 4 );
587
588                     memset( &p_dest[i_row * p_region->picture.Y_PITCH +
589                                     i_column], i_color,
590                             p_sys->i_width - i_column );
591                     i_column = p_sys->i_width;
592                     continue;
593                 }
594                 else
595                 {
596                     /* Normal case: get color and repeat count */
597                     i_count = (i_val >> 2);
598                     i_color = i_val & 0x3;
599
600                     i_count = __MIN( i_count, p_sys->i_width - i_column );
601
602                     memset( &p_dest[i_row * p_region->picture.Y_PITCH +
603                                     i_column], i_color, i_count );
604                     i_column += i_count - 1;
605                     continue;
606                 }
607             }
608
609             bs_align( &bs );
610         }
611     }
612 }