demux: mp4: avoid audio cuts on seek
[vlc.git] / modules / codec / cvdsub.c
blob4a1aeacb4c3eceb1ecc2105def3762e334e6eb13
1 /*****************************************************************************
2 * cvdsub.c : CVD Subtitle decoder
3 *****************************************************************************
4 * Copyright (C) 2003, 2004 VLC authors and VideoLAN
5 * $Id$
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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
38 #include <vlc_bits.h>
40 #define DEBUG_CVDSUB 1
42 /*****************************************************************************
43 * Module descriptor.
44 *****************************************************************************/
45 static int DecoderOpen ( vlc_object_t * );
46 static int PacketizerOpen( vlc_object_t * );
47 static void DecoderClose ( vlc_object_t * );
49 vlc_module_begin ()
50 set_description( N_("CVD subtitle decoder") )
51 set_capability( "spu decoder", 50 )
52 set_callbacks( DecoderOpen, DecoderClose )
54 add_submodule ()
55 set_description( N_("Chaoji VCD subtitle packetizer") )
56 set_capability( "packetizer", 50 )
57 set_callbacks( PacketizerOpen, DecoderClose )
58 vlc_module_end ()
60 /*****************************************************************************
61 * Local prototypes
62 *****************************************************************************/
63 static int Decode( decoder_t *, block_t * );
64 static block_t *Packetize ( decoder_t *, block_t ** );
65 static block_t *Reassemble ( decoder_t *, block_t * );
66 static void ParseMetaInfo ( decoder_t *, block_t * );
67 static void ParseHeader ( decoder_t *, block_t * );
68 static subpicture_t *DecodePacket( decoder_t *, block_t * );
69 static void RenderImage( decoder_t *, block_t *, subpicture_region_t * );
71 #define SUBTITLE_BLOCK_EMPTY 0
72 #define SUBTITLE_BLOCK_PARTIAL 1
73 #define SUBTITLE_BLOCK_COMPLETE 2
75 struct decoder_sys_t
77 int b_packetizer;
79 int i_state; /* data-gathering state for this subtitle */
81 block_t *p_spu; /* Bytes of the packet. */
83 size_t i_spu_size; /* goal for subtitle_data_pos while gathering,
84 size of used subtitle_data later */
86 uint16_t i_image_offset; /* offset from subtitle_data to compressed
87 image data */
88 size_t i_image_length; /* size of the compressed image data */
89 size_t first_field_offset; /* offset of even raster lines */
90 size_t second_field_offset; /* offset of odd raster lines */
91 size_t metadata_offset; /* offset to data describing the image */
92 size_t metadata_length; /* length of metadata */
94 mtime_t i_duration; /* how long to display the image, 0 stands
95 for "until next subtitle" */
97 uint16_t i_x_start, i_y_start; /* position of top leftmost pixel of
98 image when displayed */
99 uint16_t i_width, i_height; /* dimensions in pixels of image */
101 uint8_t p_palette[4][4]; /* Palette of colors used in subtitle */
102 uint8_t p_palette_highlight[4][4];
105 /*****************************************************************************
106 * DecoderOpen: open/initialize the cvdsub decoder.
107 *****************************************************************************/
108 static int DecoderOpen( vlc_object_t *p_this )
110 decoder_t *p_dec = (decoder_t*)p_this;
111 decoder_sys_t *p_sys;
113 if( p_dec->fmt_in.i_codec != VLC_CODEC_CVD )
114 return VLC_EGENERIC;
116 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
117 if( !p_sys )
118 return VLC_ENOMEM;
120 p_sys->b_packetizer = false;
122 p_sys->i_state = SUBTITLE_BLOCK_EMPTY;
123 p_sys->p_spu = NULL;
125 p_dec->pf_decode = Decode;
126 p_dec->pf_packetize = Packetize;
128 p_dec->fmt_out.i_codec = VLC_CODEC_YUVP;
130 return VLC_SUCCESS;
133 /*****************************************************************************
134 * PacketizerOpen: open/initialize the cvdsub packetizer.
135 *****************************************************************************/
136 static int PacketizerOpen( vlc_object_t *p_this )
138 decoder_t *p_dec = (decoder_t*)p_this;
140 if( DecoderOpen( p_this ) != VLC_SUCCESS ) return VLC_EGENERIC;
142 p_dec->fmt_out.i_codec = VLC_CODEC_CVD;
143 p_dec->p_sys->b_packetizer = true;
145 return VLC_SUCCESS;
148 /*****************************************************************************
149 * DecoderClose: closes the cvdsub decoder/packetizer.
150 *****************************************************************************/
151 void DecoderClose( vlc_object_t *p_this )
153 decoder_t *p_dec = (decoder_t*)p_this;
154 decoder_sys_t *p_sys = p_dec->p_sys;
156 if( p_sys->p_spu ) block_ChainRelease( p_sys->p_spu );
157 free( p_sys );
160 /*****************************************************************************
161 * Decode:
162 *****************************************************************************/
163 static int Decode( decoder_t *p_dec, block_t *p_block )
165 block_t *p_data;
167 if( p_block == NULL ) /* No Drain */
168 return VLCDEC_SUCCESS;
170 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
172 block_Release( p_block );
173 return VLCDEC_SUCCESS;
176 if( !(p_data = Reassemble( p_dec, p_block )) )
177 return VLCDEC_SUCCESS;
179 /* Parse and decode */
180 subpicture_t *p_spu = DecodePacket( p_dec, p_data );
181 if( p_spu != NULL )
182 decoder_QueueSub( p_dec, p_spu );
183 return VLCDEC_SUCCESS;
186 /*****************************************************************************
187 * Packetize:
188 *****************************************************************************/
189 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
191 block_t *p_block, *p_spu;
193 if( pp_block == NULL || *pp_block == NULL ) return NULL;
195 p_block = *pp_block;
196 *pp_block = NULL;
198 if( !(p_spu = Reassemble( p_dec, p_block )) ) return NULL;
200 p_spu->i_dts = p_spu->i_pts;
201 p_spu->i_length = 0;
203 return p_spu;
207 /*****************************************************************************
208 Reassemble:
210 Data for single screen subtitle may come in several non-contiguous
211 packets of a stream. This routine is called when the next packet in
212 the stream comes in. The job of this routine is to parse the header,
213 if this is the beginning, and combine the packets into one complete
214 subtitle unit.
216 If everything is complete, we will return a block. Otherwise return
217 NULL.
219 *****************************************************************************/
220 #define SPU_HEADER_LEN 1
222 static block_t *Reassemble( decoder_t *p_dec, block_t *p_block )
224 decoder_sys_t *p_sys = p_dec->p_sys;
226 if( p_block->i_buffer < SPU_HEADER_LEN )
228 msg_Dbg( p_dec, "invalid packet header (size %zu < %u)" ,
229 p_block->i_buffer, SPU_HEADER_LEN );
230 block_Release( p_block );
231 return NULL;
234 /* From the scant data on the format, there is only only way known
235 * to detect the first packet in a subtitle. The first packet
236 * seems to have a valid PTS while later packets for the same
237 * image don't. */
238 if( p_sys->i_state == SUBTITLE_BLOCK_EMPTY && p_block->i_pts <= VLC_TS_INVALID )
240 msg_Warn( p_dec, "first packet expected but no PTS present");
241 return NULL;
244 p_block->p_buffer += SPU_HEADER_LEN;
245 p_block->i_buffer -= SPU_HEADER_LEN;
247 /* First packet in the subtitle block */
248 if( p_sys->i_state == SUBTITLE_BLOCK_EMPTY ) ParseHeader( p_dec, p_block );
250 block_ChainAppend( &p_sys->p_spu, p_block );
251 p_sys->p_spu = block_ChainGather( p_sys->p_spu );
253 if( p_sys->p_spu->i_buffer >= p_sys->i_spu_size )
255 block_t *p_spu = p_sys->p_spu;
257 if( p_spu->i_buffer != p_sys->i_spu_size )
259 msg_Warn( p_dec, "SPU packets size=%zu should be %zu",
260 p_spu->i_buffer, p_sys->i_spu_size );
263 msg_Dbg( p_dec, "subtitle packet complete, size=%zuu", p_spu->i_buffer);
265 ParseMetaInfo( p_dec, p_spu );
267 p_sys->i_state = SUBTITLE_BLOCK_EMPTY;
268 p_sys->p_spu = 0;
269 return p_spu;
271 else
273 /* Not last block in subtitle, so wait for another. */
274 p_sys->i_state = SUBTITLE_BLOCK_PARTIAL;
277 return NULL;
281 We do not have information on the subtitle format used on CVD's
282 except the submux sample code and a couple of samples of dubious
283 origin. Thus, this is the result of reading some code whose
284 correctness is not known and some experimentation.
286 CVD subtitles are different in several ways from SVCD OGT subtitles.
287 Image comes first and metadata is at the end. So that the metadata
288 can be found easily, the subtitle packet starts with two bytes
289 (everything is big-endian again) that give the total size of the
290 subtitle data and the offset to the metadata - i.e. size of the
291 image data plus the four bytes at the beginning.
293 Image data comes interlaced is run-length encoded. Each field is a
294 four-bit nibble. Each nibble contains a two-bit repeat count and a
295 two-bit color number so that up to three pixels can be described in
296 four bits. The function of a 0 repeat count is unknown; it might be
297 used for RLE extension. However when the full nibble is zero, the
298 rest of the line is filled with the color value in the next nibble.
299 It is unknown what happens if the color value is greater than three.
300 The rest seems to use a 4-entries palette. It is not impossible
301 that the fill-line complete case above is not as described and the
302 zero repeat count means fill line. The sample code never produces
303 this, so it may be untested.
306 static void ParseHeader( decoder_t *p_dec, block_t *p_block )
308 decoder_sys_t *p_sys = p_dec->p_sys;
309 uint8_t *p = p_block->p_buffer;
311 p_sys->i_spu_size = (p[0] << 8) + p[1] + 4; p += 2;
313 /* FIXME: check data sanity */
314 p_sys->metadata_offset = (p[0] << 8) + p[1]; p +=2;
315 p_sys->metadata_length = p_sys->i_spu_size - p_sys->metadata_offset;
317 p_sys->i_image_offset = 4;
318 p_sys->i_image_length = p_sys->metadata_offset - p_sys->i_image_offset;
320 #ifdef DEBUG_CVDSUB
321 msg_Dbg( p_dec, "total size: %zu image size: %zu",
322 p_sys->i_spu_size, p_sys->i_image_length );
323 #endif
327 We parse the metadata information here.
329 Although metadata information does not have to come in a fixed field
330 order, every metadata field consists of a tag byte followed by
331 parameters. In all cases known, the size including tag byte is
332 exactly four bytes in length.
335 #define ExtractXY(x, y) x = ((p[1]&0x0f)<<6) + (p[2]>>2); \
336 y = ((p[2]&0x03)<<8) + p[3];
338 static void ParseMetaInfo( decoder_t *p_dec, block_t *p_spu )
340 /* Last packet in subtitle block. */
342 decoder_sys_t *p_sys = p_dec->p_sys;
343 uint8_t *p = p_spu->p_buffer + p_sys->metadata_offset;
344 uint8_t *p_end = p + p_sys->metadata_length;
346 for( ; p < p_end; p += 4 )
348 switch( p[0] )
350 case 0x04: /* subtitle duration in 1/90000ths of a second */
351 p_sys->i_duration = (p[1]<<16) + (p[2]<<8) + p[3];
353 #ifdef DEBUG_CVDSUB
354 msg_Dbg( p_dec, "subtitle display duration %lu secs",
355 (long unsigned int)(p_sys->i_duration / 90000) );
356 #endif
357 p_sys->i_duration *= 100 / 9;
358 break;
360 case 0x0c: /* unknown */
361 #ifdef DEBUG_CVDSUB
362 msg_Dbg( p_dec, "subtitle command unknown 0x%0x 0x%0x 0x%0x 0x%0x",
363 (int)p[0], (int)p[1], (int)p[2], (int)p[3] );
364 #endif
365 break;
367 case 0x17: /* coordinates of subtitle upper left x, y position */
368 ExtractXY(p_sys->i_x_start, p_sys->i_y_start);
370 #ifdef DEBUG_CVDSUB
371 msg_Dbg( p_dec, "start position (%d,%d)",
372 p_sys->i_x_start, p_sys->i_y_start );
373 #endif
374 break;
376 case 0x1f: /* coordinates of subtitle bottom right x, y position */
378 int lastx;
379 int lasty;
380 ExtractXY(lastx, lasty);
381 p_sys->i_width = lastx - p_sys->i_x_start + 1;
382 p_sys->i_height = lasty - p_sys->i_y_start + 1;
384 #ifdef DEBUG_CVDSUB
385 msg_Dbg( p_dec, "end position (%d,%d), w x h: %dx%d",
386 lastx, lasty, p_sys->i_width, p_sys->i_height );
387 #endif
388 break;
391 case 0x24:
392 case 0x25:
393 case 0x26:
394 case 0x27:
396 uint8_t v = p[0] - 0x24;
398 #ifdef DEBUG_CVDSUB
399 /* Primary Palette */
400 msg_Dbg( p_dec, "primary palette %d (y,u,v): (0x%0x,0x%0x,0x%0x)",
401 (int)v, (int)p[1], (int)p[2], (int)p[3] );
402 #endif
404 p_sys->p_palette[v][0] = p[1]; /* Y */
405 p_sys->p_palette[v][1] = p[3]; /* Cr / V */
406 p_sys->p_palette[v][2] = p[2]; /* Cb / U */
407 break;
410 case 0x2c:
411 case 0x2d:
412 case 0x2e:
413 case 0x2f:
415 uint8_t v = p[0] - 0x2c;
417 #ifdef DEBUG_CVDSUB
418 msg_Dbg( p_dec,"highlight palette %d (y,u,v): (0x%0x,0x%0x,0x%0x)",
419 (int)v, (int)p[1], (int)p[2], (int)p[3] );
420 #endif
422 /* Highlight Palette */
423 p_sys->p_palette_highlight[v][0] = p[1]; /* Y */
424 p_sys->p_palette_highlight[v][1] = p[3]; /* Cr / V */
425 p_sys->p_palette_highlight[v][2] = p[2]; /* Cb / U */
426 break;
429 case 0x37:
430 /* transparency for primary palette */
431 p_sys->p_palette[0][3] = (p[3] & 0x0f) << 4;
432 p_sys->p_palette[1][3] = (p[3] >> 4) << 4;
433 p_sys->p_palette[2][3] = (p[2] & 0x0f) << 4;
434 p_sys->p_palette[3][3] = (p[2] >> 4) << 4;
436 #ifdef DEBUG_CVDSUB
437 msg_Dbg( p_dec, "transparency for primary palette 0..3: "
438 "0x%0x 0x%0x 0x%0x 0x%0x",
439 (int)p_sys->p_palette[0][3], (int)p_sys->p_palette[1][3],
440 (int)p_sys->p_palette[2][3], (int)p_sys->p_palette[3][3]);
441 #endif
442 break;
444 case 0x3f:
445 /* transparency for highlight palette */
446 p_sys->p_palette_highlight[0][3] = (p[2] & 0x0f) << 4;
447 p_sys->p_palette_highlight[1][3] = (p[2] >> 4) << 4;
448 p_sys->p_palette_highlight[2][3] = (p[1] & 0x0f) << 4;
449 p_sys->p_palette_highlight[3][3] = (p[1] >> 4) << 4;
451 #ifdef DEBUG_CVDSUB
452 msg_Dbg( p_dec, "transparency for highlight palette 0..3: "
453 "0x%0x 0x%0x 0x%0x 0x%0x",
454 (int)p_sys->p_palette_highlight[0][3],
455 (int)p_sys->p_palette_highlight[1][3],
456 (int)p_sys->p_palette_highlight[2][3],
457 (int)p_sys->p_palette_highlight[3][3] );
458 #endif
459 break;
461 case 0x47:
462 /* offset to start of even rows of interlaced image, we correct
463 * to make it relative to i_image_offset (usually 4) */
464 p_sys->first_field_offset =
465 (p[2] << 8) + p[3] - p_sys->i_image_offset;
466 #ifdef DEBUG_CVDSUB
467 msg_Dbg( p_dec, "1st_field_offset %zu",
468 p_sys->first_field_offset );
469 #endif
470 break;
472 case 0x4f:
473 /* offset to start of odd rows of interlaced image, we correct
474 * to make it relative to i_image_offset (usually 4) */
475 p_sys->second_field_offset =
476 (p[2] << 8) + p[3] - p_sys->i_image_offset;
477 #ifdef DEBUG_CVDSUB
478 msg_Dbg( p_dec, "2nd_field_offset %zu",
479 p_sys->second_field_offset);
480 #endif
481 break;
483 default:
484 #ifdef DEBUG_CVDSUB
485 msg_Warn( p_dec, "unknown sequence in control header "
486 "0x%0x 0x%0x 0x%0x 0x%0x", p[0], p[1], p[2], p[3]);
487 #endif
492 /*****************************************************************************
493 * DecodePacket: parse and decode an SPU packet
494 *****************************************************************************
495 * This function parses and decodes an SPU packet and, if valid, returns a
496 * subpicture.
497 *****************************************************************************/
498 static subpicture_t *DecodePacket( decoder_t *p_dec, block_t *p_data )
500 decoder_sys_t *p_sys = p_dec->p_sys;
501 subpicture_t *p_spu;
502 subpicture_region_t *p_region;
503 video_format_t fmt;
504 video_palette_t palette;
505 int i;
507 /* Allocate the subpicture internal data. */
508 p_spu = decoder_NewSubpicture( p_dec, NULL );
509 if( !p_spu ) return NULL;
511 p_spu->i_start = p_data->i_pts;
512 p_spu->i_stop = p_data->i_pts + p_sys->i_duration;
513 p_spu->b_ephemer = true;
515 /* Create new SPU region */
516 video_format_Init( &fmt, VLC_CODEC_YUVP );
517 fmt.i_sar_num = 1;
518 fmt.i_sar_den = 1;
519 fmt.i_width = fmt.i_visible_width = p_sys->i_width;
520 fmt.i_height = fmt.i_visible_height = p_sys->i_height;
521 fmt.i_x_offset = fmt.i_y_offset = 0;
522 fmt.p_palette = &palette;
523 fmt.p_palette->i_entries = 4;
524 for( i = 0; i < fmt.p_palette->i_entries; i++ )
526 fmt.p_palette->palette[i][0] = p_sys->p_palette[i][0];
527 fmt.p_palette->palette[i][1] = p_sys->p_palette[i][1];
528 fmt.p_palette->palette[i][2] = p_sys->p_palette[i][2];
529 fmt.p_palette->palette[i][3] = p_sys->p_palette[i][3];
532 p_region = subpicture_region_New( &fmt );
533 video_format_Clean( &fmt );
534 if( !p_region )
536 msg_Err( p_dec, "cannot allocate SPU region" );
537 subpicture_Delete( p_spu );
538 return NULL;
541 p_spu->p_region = p_region;
542 p_region->i_x = p_sys->i_x_start;
543 p_region->i_x = p_region->i_x * 3 / 4; /* FIXME: use aspect ratio for x? */
544 p_region->i_y = p_sys->i_y_start;
546 RenderImage( p_dec, p_data, p_region );
548 return p_spu;
551 /*****************************************************************************
552 * ParseImage: parse and render the image part of the subtitle
553 *****************************************************************************
554 This part parses the subtitle graphical data and renders it.
556 Image data comes interlaced and is run-length encoded (RLE). Each
557 field is a four-bit nibbles that is further subdivided in a two-bit
558 repeat count and a two-bit color number - up to three pixels can be
559 described in four bits. What a 0 repeat count means is unknown. It
560 might be used for RLE extension. There is a special case of a 0
561 repeat count though. When the full nibble is zero, the rest of the
562 line is filled with the color value in the next nibble. It is
563 unknown what happens if the color value is greater than three. The
564 rest seems to use a 4-entries palette. It is not impossible that the
565 fill-line complete case above is not as described and the zero repeat
566 count means fill line. The sample code never produces this, so it
567 may be untested.
569 However we'll transform this so that that the RLE is expanded and
570 interlacing will also be removed. On output each pixel entry will by
571 a 4-bit alpha (filling 8 bits), and 8-bit y, u, and v entry.
573 *****************************************************************************/
574 static void RenderImage( decoder_t *p_dec, block_t *p_data,
575 subpicture_region_t *p_region )
577 decoder_sys_t *p_sys = p_dec->p_sys;
578 uint8_t *p_dest = p_region->p_picture->Y_PIXELS;
579 int i_field; /* The subtitles are interlaced */
580 int i_row, i_column; /* scanline row/column number */
581 uint8_t i_color, i_count;
582 bs_t bs;
584 bs_init( &bs, p_data->p_buffer + p_sys->i_image_offset,
585 p_data->i_buffer - p_sys->i_image_offset );
587 for( i_field = 0; i_field < 2; i_field++ )
589 for( i_row = i_field; i_row < p_sys->i_height; i_row += 2 )
591 for( i_column = 0; i_column < p_sys->i_width; i_column++ )
593 uint8_t i_val = bs_read( &bs, 4 );
595 if( i_val == 0 )
597 /* Fill the rest of the line with next color */
598 i_color = bs_read( &bs, 4 );
600 memset( &p_dest[i_row * p_region->p_picture->Y_PITCH +
601 i_column], i_color,
602 p_sys->i_width - i_column );
603 i_column = p_sys->i_width;
604 continue;
606 else
608 /* Normal case: get color and repeat count */
609 i_count = (i_val >> 2);
610 i_color = i_val & 0x3;
612 i_count = __MIN( i_count, p_sys->i_width - i_column );
614 memset( &p_dest[i_row * p_region->p_picture->Y_PITCH +
615 i_column], i_color, i_count );
616 i_column += i_count - 1;
617 continue;
621 bs_align( &bs );