1 /*****************************************************************************
2 * cvd.c : CVD Subtitle decoder
3 *****************************************************************************
4 * Copyright (C) 2003, 2004 the VideoLAN team
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>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
35 #include <vlc_plugin.h>
37 #include <vlc_codec.h>
41 #define DEBUG_CVDSUB 1
43 /*****************************************************************************
45 *****************************************************************************/
46 static int DecoderOpen ( vlc_object_t * );
47 static int PacketizerOpen( vlc_object_t * );
48 static void DecoderClose ( vlc_object_t * );
51 set_description( _("CVD subtitle decoder") );
52 set_capability( "decoder", 50 );
53 set_callbacks( DecoderOpen, DecoderClose );
56 set_description( _("Chaoji VCD subtitle packetizer") );
57 set_capability( "packetizer", 50 );
58 set_callbacks( PacketizerOpen, DecoderClose );
61 /*****************************************************************************
63 *****************************************************************************/
64 static subpicture_t *Decode( decoder_t *, block_t ** );
65 static block_t *Packetize ( decoder_t *, block_t ** );
66 static block_t *Reassemble ( decoder_t *, block_t * );
67 static void ParseMetaInfo ( decoder_t *, block_t * );
68 static void ParseHeader ( decoder_t *, block_t * );
69 static subpicture_t *DecodePacket( decoder_t *, block_t * );
70 static void RenderImage( decoder_t *, block_t *, subpicture_region_t * );
72 #define SUBTITLE_BLOCK_EMPTY 0
73 #define SUBTITLE_BLOCK_PARTIAL 1
74 #define SUBTITLE_BLOCK_COMPLETE 2
80 int i_state; /* data-gathering state for this subtitle */
82 block_t *p_spu; /* Bytes of the packet. */
84 size_t i_spu_size; /* goal for subtitle_data_pos while gathering,
85 size of used subtitle_data later */
87 uint16_t i_image_offset; /* offset from subtitle_data to compressed
89 size_t i_image_length; /* size of the compressed image data */
90 size_t first_field_offset; /* offset of even raster lines */
91 size_t second_field_offset; /* offset of odd raster lines */
92 size_t metadata_offset; /* offset to data describing the image */
93 size_t metadata_length; /* length of metadata */
95 mtime_t i_duration; /* how long to display the image, 0 stands
96 for "until next subtitle" */
98 uint16_t i_x_start, i_y_start; /* position of top leftmost pixel of
99 image when displayed */
100 uint16_t i_width, i_height; /* dimensions in pixels of image */
102 uint8_t p_palette[4][4]; /* Palette of colors used in subtitle */
103 uint8_t p_palette_highlight[4][4];
106 /*****************************************************************************
107 * DecoderOpen: open/initialize the cvdsub decoder.
108 *****************************************************************************/
109 static int DecoderOpen( vlc_object_t *p_this )
111 decoder_t *p_dec = (decoder_t*)p_this;
112 decoder_sys_t *p_sys;
114 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'c','v','d',' ' ) )
119 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
123 p_sys->b_packetizer = false;
125 p_sys->i_state = SUBTITLE_BLOCK_EMPTY;
128 es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'c','v','d',' ' ) );
130 p_dec->pf_decode_sub = Decode;
131 p_dec->pf_packetize = Packetize;
136 /*****************************************************************************
137 * PacketizerOpen: open/initialize the cvdsub packetizer.
138 *****************************************************************************/
139 static int PacketizerOpen( vlc_object_t *p_this )
141 decoder_t *p_dec = (decoder_t*)p_this;
143 if( DecoderOpen( p_this ) != VLC_SUCCESS ) return VLC_EGENERIC;
145 p_dec->p_sys->b_packetizer = true;
150 /*****************************************************************************
151 * DecoderClose: closes the cvdsub decoder/packetizer.
152 *****************************************************************************/
153 void DecoderClose( vlc_object_t *p_this )
155 decoder_t *p_dec = (decoder_t*)p_this;
156 decoder_sys_t *p_sys = p_dec->p_sys;
158 if( p_sys->p_spu ) block_ChainRelease( p_sys->p_spu );
162 /*****************************************************************************
164 *****************************************************************************/
165 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
167 block_t *p_block, *p_spu;
169 if( pp_block == NULL || *pp_block == NULL ) return NULL;
174 if( !(p_spu = Reassemble( p_dec, p_block )) ) return NULL;
176 /* Parse and decode */
177 return DecodePacket( p_dec, p_spu );
180 /*****************************************************************************
182 *****************************************************************************/
183 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
185 block_t *p_block, *p_spu;
187 if( pp_block == NULL || *pp_block == NULL ) return NULL;
192 if( !(p_spu = Reassemble( p_dec, p_block )) ) return NULL;
194 p_spu->i_dts = p_spu->i_pts;
201 /*****************************************************************************
204 Data for single screen subtitle may come in several non-contiguous
205 packets of a stream. This routine is called when the next packet in
206 the stream comes in. The job of this routine is to parse the header,
207 if this is the beginning, and combine the packets into one complete
210 If everything is complete, we will return a block. Otherwise return
213 *****************************************************************************/
214 #define SPU_HEADER_LEN 1
216 static block_t *Reassemble( decoder_t *p_dec, block_t *p_block )
218 decoder_sys_t *p_sys = p_dec->p_sys;
221 if( p_block->i_buffer < SPU_HEADER_LEN )
223 msg_Dbg( p_dec, "invalid packet header (size %d < %d)" ,
224 p_block->i_buffer, SPU_HEADER_LEN );
225 block_Release( p_block );
229 p_buffer = p_block->p_buffer;
231 /* From the scant data on the format, there is only only way known
232 * to detect the first packet in a subtitle. The first packet
233 * seems to have a valid PTS while later packets for the same
235 if( p_sys->i_state == SUBTITLE_BLOCK_EMPTY && p_block->i_pts == 0 )
237 msg_Warn( p_dec, "first packet expected but no PTS present");
241 p_block->p_buffer += SPU_HEADER_LEN;
242 p_block->i_buffer -= SPU_HEADER_LEN;
244 /* First packet in the subtitle block */
245 if( p_sys->i_state == SUBTITLE_BLOCK_EMPTY ) ParseHeader( p_dec, p_block );
247 block_ChainAppend( &p_sys->p_spu, p_block );
248 p_sys->p_spu = block_ChainGather( p_sys->p_spu );
250 if( p_sys->p_spu->i_buffer >= p_sys->i_spu_size )
252 block_t *p_spu = p_sys->p_spu;
254 if( p_spu->i_buffer != p_sys->i_spu_size )
256 msg_Warn( p_dec, "SPU packets size=%d should be %d",
257 p_spu->i_buffer, p_sys->i_spu_size );
260 msg_Dbg( p_dec, "subtitle packet complete, size=%d", p_spu->i_buffer);
262 ParseMetaInfo( p_dec, p_spu );
264 p_sys->i_state = SUBTITLE_BLOCK_EMPTY;
270 /* Not last block in subtitle, so wait for another. */
271 p_sys->i_state = SUBTITLE_BLOCK_PARTIAL;
278 We do not have information on the subtitle format used on CVD's
279 except the submux sample code and a couple of samples of dubious
280 origin. Thus, this is the result of reading some code whose
281 correctness is not known and some experimentation.
283 CVD subtitles are different in several ways from SVCD OGT subtitles.
284 Image comes first and metadata is at the end. So that the metadata
285 can be found easily, the subtitle packet starts with two bytes
286 (everything is big-endian again) that give the total size of the
287 subtitle data and the offset to the metadata - i.e. size of the
288 image data plus the four bytes at the beginning.
290 Image data comes interlaced is run-length encoded. Each field is a
291 four-bit nibble. Each nibble contains a two-bit repeat count and a
292 two-bit color number so that up to three pixels can be described in
293 four bits. The function of a 0 repeat count is unknown; it might be
294 used for RLE extension. However when the full nibble is zero, the
295 rest of the line is filled with the color value in the next nibble.
296 It is unknown what happens if the color value is greater than three.
297 The rest seems to use a 4-entries palette. It is not impossible
298 that the fill-line complete case above is not as described and the
299 zero repeat count means fill line. The sample code never produces
300 this, so it may be untested.
303 static void ParseHeader( decoder_t *p_dec, block_t *p_block )
305 decoder_sys_t *p_sys = p_dec->p_sys;
306 uint8_t *p = p_block->p_buffer;
308 p_sys->i_spu_size = (p[0] << 8) + p[1] + 4; p += 2;
310 /* FIXME: check data sanity */
311 p_sys->metadata_offset = (p[0] << 8) + p[1]; p +=2;
312 p_sys->metadata_length = p_sys->i_spu_size - p_sys->metadata_offset;
314 p_sys->i_image_offset = 4;
315 p_sys->i_image_length = p_sys->metadata_offset - p_sys->i_image_offset;
318 msg_Dbg( p_dec, "total size: %d image size: %d",
319 p_sys->i_spu_size, p_sys->i_image_length );
324 We parse the metadata information here.
326 Although metadata information does not have to come in a fixed field
327 order, every metadata field consists of a tag byte followed by
328 parameters. In all cases known, the size including tag byte is
329 exactly four bytes in length.
332 #define ExtractXY(x, y) x = ((p[1]&0x0f)<<6) + (p[2]>>2); \
333 y = ((p[2]&0x03)<<8) + p[3];
335 static void ParseMetaInfo( decoder_t *p_dec, block_t *p_spu )
337 /* Last packet in subtitle block. */
339 decoder_sys_t *p_sys = p_dec->p_sys;
340 uint8_t *p = p_spu->p_buffer + p_sys->metadata_offset;
341 uint8_t *p_end = p + p_sys->metadata_length;
343 for( ; p < p_end; p += 4 )
347 case 0x04: /* subtitle duration in 1/90000ths of a second */
348 p_sys->i_duration = (p[1]<<16) + (p[2]<<8) + p[3];
351 msg_Dbg( p_dec, "subtitle display duration %lu secs",
352 (long unsigned int)(p_sys->i_duration / 90000) );
354 p_sys->i_duration *= 100 / 9;
357 case 0x0c: /* unknown */
359 msg_Dbg( p_dec, "subtitle command unknown 0x%0x 0x%0x 0x%0x 0x%0x",
360 (int)p[0], (int)p[1], (int)p[2], (int)p[3] );
364 case 0x17: /* coordinates of subtitle upper left x, y position */
365 ExtractXY(p_sys->i_x_start, p_sys->i_y_start);
368 msg_Dbg( p_dec, "start position (%d,%d)",
369 p_sys->i_x_start, p_sys->i_y_start );
373 case 0x1f: /* coordinates of subtitle bottom right x, y position */
377 ExtractXY(lastx, lasty);
378 p_sys->i_width = lastx - p_sys->i_x_start + 1;
379 p_sys->i_height = lasty - p_sys->i_y_start + 1;
382 msg_Dbg( p_dec, "end position (%d,%d), w x h: %dx%d",
383 lastx, lasty, p_sys->i_width, p_sys->i_height );
393 uint8_t v = p[0] - 0x24;
396 /* Primary Palette */
397 msg_Dbg( p_dec, "primary palette %d (y,u,v): (0x%0x,0x%0x,0x%0x)",
398 (int)v, (int)p[1], (int)p[2], (int)p[3] );
401 p_sys->p_palette[v][0] = p[1]; /* Y */
402 p_sys->p_palette[v][1] = p[3]; /* Cr / V */
403 p_sys->p_palette[v][2] = p[2]; /* Cb / U */
412 uint8_t v = p[0] - 0x2c;
415 msg_Dbg( p_dec,"highlight palette %d (y,u,v): (0x%0x,0x%0x,0x%0x)",
416 (int)v, (int)p[1], (int)p[2], (int)p[3] );
419 /* Highlight Palette */
420 p_sys->p_palette_highlight[v][0] = p[1]; /* Y */
421 p_sys->p_palette_highlight[v][1] = p[3]; /* Cr / V */
422 p_sys->p_palette_highlight[v][2] = p[2]; /* Cb / U */
427 /* transparency for primary palette */
428 p_sys->p_palette[0][3] = (p[3] & 0x0f) << 4;
429 p_sys->p_palette[1][3] = (p[3] >> 4) << 4;
430 p_sys->p_palette[2][3] = (p[2] & 0x0f) << 4;
431 p_sys->p_palette[3][3] = (p[2] >> 4) << 4;
434 msg_Dbg( p_dec, "transparency for primary palette 0..3: "
435 "0x%0x 0x%0x 0x%0x 0x%0x",
436 (int)p_sys->p_palette[0][3], (int)p_sys->p_palette[1][3],
437 (int)p_sys->p_palette[2][3], (int)p_sys->p_palette[3][3]);
442 /* transparency for highlight palette */
443 p_sys->p_palette_highlight[0][3] = (p[2] & 0x0f) << 4;
444 p_sys->p_palette_highlight[1][3] = (p[2] >> 4) << 4;
445 p_sys->p_palette_highlight[2][3] = (p[1] & 0x0f) << 4;
446 p_sys->p_palette_highlight[3][3] = (p[1] >> 4) << 4;
449 msg_Dbg( p_dec, "transparency for highlight palette 0..3: "
450 "0x%0x 0x%0x 0x%0x 0x%0x",
451 (int)p_sys->p_palette_highlight[0][3],
452 (int)p_sys->p_palette_highlight[1][3],
453 (int)p_sys->p_palette_highlight[2][3],
454 (int)p_sys->p_palette_highlight[3][3] );
459 /* offset to start of even rows of interlaced image, we correct
460 * to make it relative to i_image_offset (usually 4) */
461 p_sys->first_field_offset =
462 (p[2] << 8) + p[3] - p_sys->i_image_offset;
464 msg_Dbg( p_dec, "1st_field_offset %d", p_sys->first_field_offset );
469 /* offset to start of odd rows of interlaced image, we correct
470 * to make it relative to i_image_offset (usually 4) */
471 p_sys->second_field_offset =
472 (p[2] << 8) + p[3] - p_sys->i_image_offset;
474 msg_Dbg( p_dec, "2nd_field_offset %d", p_sys->second_field_offset);
480 msg_Warn( p_dec, "unknown sequence in control header "
481 "0x%0x 0x%0x 0x%0x 0x%0x", p[0], p[1], p[2], p[3]);
487 /*****************************************************************************
488 * DecodePacket: parse and decode an SPU packet
489 *****************************************************************************
490 * This function parses and decodes an SPU packet and, if valid, returns a
492 *****************************************************************************/
493 static subpicture_t *DecodePacket( decoder_t *p_dec, block_t *p_data )
495 decoder_sys_t *p_sys = p_dec->p_sys;
497 subpicture_region_t *p_region;
501 /* Allocate the subpicture internal data. */
502 p_spu = p_dec->pf_spu_buffer_new( p_dec );
503 if( !p_spu ) return NULL;
505 p_spu->b_pausable = true;
507 p_spu->i_x = p_sys->i_x_start;
508 p_spu->i_x = p_spu->i_x * 3 / 4; /* FIXME: use aspect ratio for x? */
509 p_spu->i_y = p_sys->i_y_start;
510 p_spu->i_start = p_data->i_pts;
511 p_spu->i_stop = p_data->i_pts + p_sys->i_duration;
512 p_spu->b_ephemer = true;
514 /* Create new SPU region */
515 memset( &fmt, 0, sizeof(video_format_t) );
516 fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
517 fmt.i_aspect = VOUT_ASPECT_FACTOR;
518 fmt.i_width = fmt.i_visible_width = p_sys->i_width;
519 fmt.i_height = fmt.i_visible_height = p_sys->i_height;
520 fmt.i_x_offset = fmt.i_y_offset = 0;
521 p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
524 msg_Err( p_dec, "cannot allocate SPU region" );
528 p_spu->p_region = p_region;
529 p_region->i_x = p_region->i_y = 0;
532 fmt.p_palette->i_entries = 4;
533 for( i = 0; i < fmt.p_palette->i_entries; i++ )
535 fmt.p_palette->palette[i][0] = p_sys->p_palette[i][0];
536 fmt.p_palette->palette[i][1] = p_sys->p_palette[i][1];
537 fmt.p_palette->palette[i][2] = p_sys->p_palette[i][2];
538 fmt.p_palette->palette[i][3] = p_sys->p_palette[i][3];
541 RenderImage( p_dec, p_data, p_region );
546 /*****************************************************************************
547 * ParseImage: parse and render the image part of the subtitle
548 *****************************************************************************
549 This part parses the subtitle graphical data and renders it.
551 Image data comes interlaced and is run-length encoded (RLE). Each
552 field is a four-bit nibbles that is further subdivided in a two-bit
553 repeat count and a two-bit color number - up to three pixels can be
554 described in four bits. What a 0 repeat count means is unknown. It
555 might be used for RLE extension. There is a special case of a 0
556 repeat count though. When the full nibble is zero, the rest of the
557 line is filled with the color value in the next nibble. It is
558 unknown what happens if the color value is greater than three. The
559 rest seems to use a 4-entries palette. It is not impossible that the
560 fill-line complete case above is not as described and the zero repeat
561 count means fill line. The sample code never produces this, so it
564 However we'll transform this so that that the RLE is expanded and
565 interlacing will also be removed. On output each pixel entry will by
566 a 4-bit alpha (filling 8 bits), and 8-bit y, u, and v entry.
568 *****************************************************************************/
569 static void RenderImage( decoder_t *p_dec, block_t *p_data,
570 subpicture_region_t *p_region )
572 decoder_sys_t *p_sys = p_dec->p_sys;
573 uint8_t *p_dest = p_region->picture.Y_PIXELS;
574 int i_field; /* The subtitles are interlaced */
575 int i_row, i_column; /* scanline row/column number */
576 uint8_t i_color, i_count;
579 bs_init( &bs, p_data->p_buffer + p_sys->i_image_offset,
580 p_data->i_buffer - p_sys->i_image_offset );
582 for( i_field = 0; i_field < 2; i_field++ )
584 for( i_row = i_field; i_row < p_sys->i_height; i_row += 2 )
586 for( i_column = 0; i_column < p_sys->i_width; i_column++ )
588 uint8_t i_val = bs_read( &bs, 4 );
592 /* Fill the rest of the line with next color */
593 i_color = bs_read( &bs, 4 );
595 memset( &p_dest[i_row * p_region->picture.Y_PITCH +
597 p_sys->i_width - i_column );
598 i_column = p_sys->i_width;
603 /* Normal case: get color and repeat count */
604 i_count = (i_val >> 2);
605 i_color = i_val & 0x3;
607 i_count = __MIN( i_count, p_sys->i_width - i_column );
609 memset( &p_dest[i_row * p_region->picture.Y_PITCH +
610 i_column], i_color, i_count );
611 i_column += i_count - 1;