1 /*****************************************************************************
2 * cvdsub.c : CVD Subtitle decoder
3 *****************************************************************************
4 * Copyright (C) 2003, 2004 VLC authors and VideoLAN
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 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
40 #include "../demux/mpeg/timestamps.h"
42 #define DEBUG_CVDSUB 1
44 /*****************************************************************************
46 *****************************************************************************/
47 static int DecoderOpen ( vlc_object_t
* );
48 static int PacketizerOpen( vlc_object_t
* );
49 static void DecoderClose ( vlc_object_t
* );
52 set_description( N_("CVD subtitle decoder") )
53 set_capability( "spu decoder", 50 )
54 set_callbacks( DecoderOpen
, DecoderClose
)
57 set_description( N_("Chaoji VCD subtitle packetizer") )
58 set_capability( "packetizer", 50 )
59 set_callbacks( PacketizerOpen
, DecoderClose
)
62 /*****************************************************************************
64 *****************************************************************************/
65 static int Decode( decoder_t
*, block_t
* );
66 static block_t
*Packetize ( decoder_t
*, block_t
** );
67 static block_t
*Reassemble ( decoder_t
*, block_t
* );
68 static void ParseMetaInfo ( decoder_t
*, block_t
* );
69 static void ParseHeader ( decoder_t
*, block_t
* );
70 static subpicture_t
*DecodePacket( decoder_t
*, block_t
* );
71 static void RenderImage( decoder_t
*, block_t
*, subpicture_region_t
* );
73 #define SUBTITLE_BLOCK_EMPTY 0
74 #define SUBTITLE_BLOCK_PARTIAL 1
75 #define SUBTITLE_BLOCK_COMPLETE 2
81 int i_state
; /* data-gathering state for this subtitle */
83 block_t
*p_spu
; /* Bytes of the packet. */
85 size_t i_spu_size
; /* goal for subtitle_data_pos while gathering,
86 size of used subtitle_data later */
88 uint16_t i_image_offset
; /* offset from subtitle_data to compressed
90 size_t i_image_length
; /* size of the compressed image data */
91 size_t first_field_offset
; /* offset of even raster lines */
92 size_t second_field_offset
; /* offset of odd raster lines */
93 size_t metadata_offset
; /* offset to data describing the image */
94 size_t metadata_length
; /* length of metadata */
96 vlc_tick_t i_duration
; /* how long to display the image, 0 stands
97 for "until next subtitle" */
99 uint16_t i_x_start
, i_y_start
; /* position of top leftmost pixel of
100 image when displayed */
101 uint16_t i_width
, i_height
; /* dimensions in pixels of image */
103 uint8_t p_palette
[4][4]; /* Palette of colors used in subtitle */
104 uint8_t p_palette_highlight
[4][4];
107 static int OpenCommon( vlc_object_t
*p_this
, bool b_packetizer
)
109 decoder_t
*p_dec
= (decoder_t
*)p_this
;
110 decoder_sys_t
*p_sys
;
112 if( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_CVD
)
115 p_dec
->p_sys
= p_sys
= malloc( sizeof( decoder_sys_t
) );
119 p_sys
->b_packetizer
= b_packetizer
;
121 p_sys
->i_state
= SUBTITLE_BLOCK_EMPTY
;
126 p_dec
->pf_packetize
= Packetize
;
127 p_dec
->fmt_out
.i_codec
= VLC_CODEC_CVD
;
131 p_dec
->pf_decode
= Decode
;
132 p_dec
->fmt_out
.i_codec
= VLC_CODEC_YUVP
;
137 /*****************************************************************************
138 * DecoderOpen: open/initialize the cvdsub decoder.
139 *****************************************************************************/
140 static int DecoderOpen( vlc_object_t
*p_this
)
142 return OpenCommon( p_this
, false );
145 /*****************************************************************************
146 * PacketizerOpen: open/initialize the cvdsub packetizer.
147 *****************************************************************************/
148 static int PacketizerOpen( vlc_object_t
*p_this
)
150 return OpenCommon( p_this
, true );
153 /*****************************************************************************
154 * DecoderClose: closes the cvdsub decoder/packetizer.
155 *****************************************************************************/
156 void DecoderClose( vlc_object_t
*p_this
)
158 decoder_t
*p_dec
= (decoder_t
*)p_this
;
159 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
161 if( p_sys
->p_spu
) block_ChainRelease( p_sys
->p_spu
);
165 /*****************************************************************************
167 *****************************************************************************/
168 static int Decode( decoder_t
*p_dec
, block_t
*p_block
)
172 if( p_block
== NULL
) /* No Drain */
173 return VLCDEC_SUCCESS
;
175 if( p_block
->i_flags
& BLOCK_FLAG_CORRUPTED
)
177 block_Release( p_block
);
178 return VLCDEC_SUCCESS
;
181 if( !(p_data
= Reassemble( p_dec
, p_block
)) )
182 return VLCDEC_SUCCESS
;
184 /* Parse and decode */
185 subpicture_t
*p_spu
= DecodePacket( p_dec
, p_data
);
186 block_Release( p_data
);
188 decoder_QueueSub( p_dec
, p_spu
);
189 return VLCDEC_SUCCESS
;
192 /*****************************************************************************
194 *****************************************************************************/
195 static block_t
*Packetize( decoder_t
*p_dec
, block_t
**pp_block
)
197 block_t
*p_block
, *p_spu
;
199 if( pp_block
== NULL
|| *pp_block
== NULL
) return NULL
;
204 if( !(p_spu
= Reassemble( p_dec
, p_block
)) ) return NULL
;
206 p_spu
->i_dts
= p_spu
->i_pts
;
207 p_spu
->i_length
= VLC_TICK_INVALID
;
213 /*****************************************************************************
216 Data for single screen subtitle may come in several non-contiguous
217 packets of a stream. This routine is called when the next packet in
218 the stream comes in. The job of this routine is to parse the header,
219 if this is the beginning, and combine the packets into one complete
222 If everything is complete, we will return a block. Otherwise return
225 *****************************************************************************/
226 #define SPU_HEADER_LEN 1
228 static block_t
*Reassemble( decoder_t
*p_dec
, block_t
*p_block
)
230 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
232 if( p_block
->i_buffer
< SPU_HEADER_LEN
)
234 msg_Dbg( p_dec
, "invalid packet header (size %zu < %u)" ,
235 p_block
->i_buffer
, SPU_HEADER_LEN
);
236 block_Release( p_block
);
240 /* From the scant data on the format, there is only only way known
241 * to detect the first packet in a subtitle. The first packet
242 * seems to have a valid PTS while later packets for the same
244 if( p_sys
->i_state
== SUBTITLE_BLOCK_EMPTY
&& p_block
->i_pts
== VLC_TICK_INVALID
)
246 msg_Warn( p_dec
, "first packet expected but no PTS present");
250 p_block
->p_buffer
+= SPU_HEADER_LEN
;
251 p_block
->i_buffer
-= SPU_HEADER_LEN
;
253 /* First packet in the subtitle block */
254 if( p_sys
->i_state
== SUBTITLE_BLOCK_EMPTY
) ParseHeader( p_dec
, p_block
);
256 block_ChainAppend( &p_sys
->p_spu
, p_block
);
257 p_sys
->p_spu
= block_ChainGather( p_sys
->p_spu
);
259 if( p_sys
->p_spu
->i_buffer
>= p_sys
->i_spu_size
)
261 block_t
*p_spu
= p_sys
->p_spu
;
263 if( p_spu
->i_buffer
!= p_sys
->i_spu_size
)
265 msg_Warn( p_dec
, "SPU packets size=%zu should be %zu",
266 p_spu
->i_buffer
, p_sys
->i_spu_size
);
269 msg_Dbg( p_dec
, "subtitle packet complete, size=%zuu", p_spu
->i_buffer
);
271 ParseMetaInfo( p_dec
, p_spu
);
273 p_sys
->i_state
= SUBTITLE_BLOCK_EMPTY
;
279 /* Not last block in subtitle, so wait for another. */
280 p_sys
->i_state
= SUBTITLE_BLOCK_PARTIAL
;
287 We do not have information on the subtitle format used on CVD's
288 except the submux sample code and a couple of samples of dubious
289 origin. Thus, this is the result of reading some code whose
290 correctness is not known and some experimentation.
292 CVD subtitles are different in several ways from SVCD OGT subtitles.
293 Image comes first and metadata is at the end. So that the metadata
294 can be found easily, the subtitle packet starts with two bytes
295 (everything is big-endian again) that give the total size of the
296 subtitle data and the offset to the metadata - i.e. size of the
297 image data plus the four bytes at the beginning.
299 Image data comes interlaced is run-length encoded. Each field is a
300 four-bit nibble. Each nibble contains a two-bit repeat count and a
301 two-bit color number so that up to three pixels can be described in
302 four bits. The function of a 0 repeat count is unknown; it might be
303 used for RLE extension. However when the full nibble is zero, the
304 rest of the line is filled with the color value in the next nibble.
305 It is unknown what happens if the color value is greater than three.
306 The rest seems to use a 4-entries palette. It is not impossible
307 that the fill-line complete case above is not as described and the
308 zero repeat count means fill line. The sample code never produces
309 this, so it may be untested.
312 static void ParseHeader( decoder_t
*p_dec
, block_t
*p_block
)
314 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
315 uint8_t *p
= p_block
->p_buffer
;
317 p_sys
->i_spu_size
= (p
[0] << 8) + p
[1] + 4; p
+= 2;
319 /* FIXME: check data sanity */
320 p_sys
->metadata_offset
= (p
[0] << 8) + p
[1]; p
+=2;
321 p_sys
->metadata_length
= p_sys
->i_spu_size
- p_sys
->metadata_offset
;
323 p_sys
->i_image_offset
= 4;
324 p_sys
->i_image_length
= p_sys
->metadata_offset
- p_sys
->i_image_offset
;
327 msg_Dbg( p_dec
, "total size: %zu image size: %zu",
328 p_sys
->i_spu_size
, p_sys
->i_image_length
);
333 We parse the metadata information here.
335 Although metadata information does not have to come in a fixed field
336 order, every metadata field consists of a tag byte followed by
337 parameters. In all cases known, the size including tag byte is
338 exactly four bytes in length.
341 #define ExtractXY(x, y) x = ((p[1]&0x0f)<<6) + (p[2]>>2); \
342 y = ((p[2]&0x03)<<8) + p[3];
344 static void ParseMetaInfo( decoder_t
*p_dec
, block_t
*p_spu
)
346 /* Last packet in subtitle block. */
348 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
349 uint8_t *p
= p_spu
->p_buffer
+ p_sys
->metadata_offset
;
350 uint8_t *p_end
= p
+ p_sys
->metadata_length
;
352 for( ; p
< p_end
; p
+= 4 )
356 case 0x04: /* subtitle duration in 1/90000ths of a second */
357 p_sys
->i_duration
= FROM_SCALE_NZ( (p
[1]<<16) + (p
[2]<<8) + p
[3] );
360 msg_Dbg( p_dec
, "subtitle display duration %"PRIu64
" ms",
361 MS_FROM_VLC_TICK(p_sys
->i_duration
) );
365 case 0x0c: /* unknown */
367 msg_Dbg( p_dec
, "subtitle command unknown "
368 "0x%02"PRIx8
" 0x%02"PRIx8
" 0x%02"PRIx8
" 0x%02"PRIx8
,
369 p
[0], p
[1], p
[2], p
[3] );
373 case 0x17: /* coordinates of subtitle upper left x, y position */
374 ExtractXY(p_sys
->i_x_start
, p_sys
->i_y_start
);
377 msg_Dbg( p_dec
, "start position (%"PRIu16
",%"PRIu16
")",
378 p_sys
->i_x_start
, p_sys
->i_y_start
);
382 case 0x1f: /* coordinates of subtitle bottom right x, y position */
386 ExtractXY(lastx
, lasty
);
387 p_sys
->i_width
= lastx
- p_sys
->i_x_start
+ 1;
388 p_sys
->i_height
= lasty
- p_sys
->i_y_start
+ 1;
391 msg_Dbg( p_dec
, "end position (%d,%d), w x h: %"PRIu16
"x%"PRIu16
,
392 lastx
, lasty
, p_sys
->i_width
, p_sys
->i_height
);
402 uint8_t v
= p
[0] - 0x24;
405 /* Primary Palette */
406 msg_Dbg( p_dec
, "primary palette %"PRIu8
" (y,u,v): "
407 "(0x%02"PRIx8
",0x%02"PRIx8
",0x%02"PRIx8
")",
408 v
, p
[1], p
[2], p
[3] );
411 p_sys
->p_palette
[v
][0] = p
[1]; /* Y */
412 p_sys
->p_palette
[v
][1] = p
[3]; /* Cr / V */
413 p_sys
->p_palette
[v
][2] = p
[2]; /* Cb / U */
422 uint8_t v
= p
[0] - 0x2c;
425 msg_Dbg( p_dec
,"highlight palette %"PRIu8
" (y,u,v): "
426 "(0x%02"PRIx8
",0x%02"PRIx8
",0x%02"PRIx8
")",
427 v
, p
[1], p
[2], p
[3] );
430 /* Highlight Palette */
431 p_sys
->p_palette_highlight
[v
][0] = p
[1]; /* Y */
432 p_sys
->p_palette_highlight
[v
][1] = p
[3]; /* Cr / V */
433 p_sys
->p_palette_highlight
[v
][2] = p
[2]; /* Cb / U */
438 /* transparency for primary palette */
439 p_sys
->p_palette
[0][3] = (p
[3] & 0x0f) << 4;
440 p_sys
->p_palette
[1][3] = (p
[3] >> 4) << 4;
441 p_sys
->p_palette
[2][3] = (p
[2] & 0x0f) << 4;
442 p_sys
->p_palette
[3][3] = (p
[2] >> 4) << 4;
445 msg_Dbg( p_dec
, "transparency for primary palette 0..3: "
446 "0x%02"PRIx8
" 0x%02"PRIx8
" 0x%02"PRIx8
" 0x%02"PRIx8
,
447 p_sys
->p_palette
[0][3], p_sys
->p_palette
[1][3],
448 p_sys
->p_palette
[2][3], p_sys
->p_palette
[3][3]);
453 /* transparency for highlight palette */
454 p_sys
->p_palette_highlight
[0][3] = (p
[2] & 0x0f) << 4;
455 p_sys
->p_palette_highlight
[1][3] = (p
[2] >> 4) << 4;
456 p_sys
->p_palette_highlight
[2][3] = (p
[1] & 0x0f) << 4;
457 p_sys
->p_palette_highlight
[3][3] = (p
[1] >> 4) << 4;
460 msg_Dbg( p_dec
, "transparency for highlight palette 0..3: "
461 "0x%02"PRIx8
" 0x%02"PRIx8
" 0x%02"PRIx8
" 0x%02"PRIx8
,
462 p_sys
->p_palette_highlight
[0][3],
463 p_sys
->p_palette_highlight
[1][3],
464 p_sys
->p_palette_highlight
[2][3],
465 p_sys
->p_palette_highlight
[3][3] );
470 /* offset to start of even rows of interlaced image, we correct
471 * to make it relative to i_image_offset (usually 4) */
472 p_sys
->first_field_offset
=
473 (p
[2] << 8) + p
[3] - p_sys
->i_image_offset
;
475 msg_Dbg( p_dec
, "1st_field_offset %zu",
476 p_sys
->first_field_offset
);
481 /* offset to start of odd rows of interlaced image, we correct
482 * to make it relative to i_image_offset (usually 4) */
483 p_sys
->second_field_offset
=
484 (p
[2] << 8) + p
[3] - p_sys
->i_image_offset
;
486 msg_Dbg( p_dec
, "2nd_field_offset %zu",
487 p_sys
->second_field_offset
);
493 msg_Warn( p_dec
, "unknown sequence in control header "
494 "0x%02"PRIx8
" 0x%02"PRIx8
" 0x%02"PRIx8
" 0x%02"PRIx8
,
495 p
[0], p
[1], p
[2], p
[3]);
501 /*****************************************************************************
502 * DecodePacket: parse and decode an SPU packet
503 *****************************************************************************
504 * This function parses and decodes an SPU packet and, if valid, returns a
506 *****************************************************************************/
507 static subpicture_t
*DecodePacket( decoder_t
*p_dec
, block_t
*p_data
)
509 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
511 subpicture_region_t
*p_region
;
513 video_palette_t palette
;
516 /* Allocate the subpicture internal data. */
517 p_spu
= decoder_NewSubpicture( p_dec
, NULL
);
518 if( !p_spu
) return NULL
;
520 p_spu
->i_start
= p_data
->i_pts
;
521 p_spu
->i_stop
= p_data
->i_pts
+ p_sys
->i_duration
;
522 p_spu
->b_ephemer
= true;
524 /* Create new SPU region */
525 video_format_Init( &fmt
, VLC_CODEC_YUVP
);
528 fmt
.i_width
= fmt
.i_visible_width
= p_sys
->i_width
;
529 fmt
.i_height
= fmt
.i_visible_height
= p_sys
->i_height
;
530 fmt
.i_x_offset
= fmt
.i_y_offset
= 0;
531 fmt
.p_palette
= &palette
;
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 p_region
= subpicture_region_New( &fmt
);
544 msg_Err( p_dec
, "cannot allocate SPU region" );
545 subpicture_Delete( p_spu
);
549 p_spu
->p_region
= p_region
;
550 p_region
->i_x
= p_sys
->i_x_start
;
551 p_region
->i_x
= p_region
->i_x
* 3 / 4; /* FIXME: use aspect ratio for x? */
552 p_region
->i_y
= p_sys
->i_y_start
;
554 RenderImage( p_dec
, p_data
, p_region
);
559 /*****************************************************************************
560 * ParseImage: parse and render the image part of the subtitle
561 *****************************************************************************
562 This part parses the subtitle graphical data and renders it.
564 Image data comes interlaced and is run-length encoded (RLE). Each
565 field is a four-bit nibbles that is further subdivided in a two-bit
566 repeat count and a two-bit color number - up to three pixels can be
567 described in four bits. What a 0 repeat count means is unknown. It
568 might be used for RLE extension. There is a special case of a 0
569 repeat count though. When the full nibble is zero, the rest of the
570 line is filled with the color value in the next nibble. It is
571 unknown what happens if the color value is greater than three. The
572 rest seems to use a 4-entries palette. It is not impossible that the
573 fill-line complete case above is not as described and the zero repeat
574 count means fill line. The sample code never produces this, so it
577 However we'll transform this so that that the RLE is expanded and
578 interlacing will also be removed. On output each pixel entry will by
579 a 4-bit alpha (filling 8 bits), and 8-bit y, u, and v entry.
581 *****************************************************************************/
582 static void RenderImage( decoder_t
*p_dec
, block_t
*p_data
,
583 subpicture_region_t
*p_region
)
585 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
586 uint8_t *p_dest
= p_region
->p_picture
->Y_PIXELS
;
587 int i_field
; /* The subtitles are interlaced */
588 int i_row
, i_column
; /* scanline row/column number */
589 uint8_t i_color
, i_count
;
592 bs_init( &bs
, p_data
->p_buffer
+ p_sys
->i_image_offset
,
593 p_data
->i_buffer
- p_sys
->i_image_offset
);
595 for( i_field
= 0; i_field
< 2; i_field
++ )
597 for( i_row
= i_field
; i_row
< p_sys
->i_height
; i_row
+= 2 )
599 for( i_column
= 0; i_column
< p_sys
->i_width
; i_column
++ )
601 uint8_t i_val
= bs_read( &bs
, 4 );
605 /* Fill the rest of the line with next color */
606 i_color
= bs_read( &bs
, 4 );
608 memset( &p_dest
[i_row
* p_region
->p_picture
->Y_PITCH
+
610 p_sys
->i_width
- i_column
);
611 i_column
= p_sys
->i_width
;
616 /* Normal case: get color and repeat count */
617 i_count
= (i_val
>> 2);
618 i_color
= i_val
& 0x3;
620 i_count
= __MIN( i_count
, p_sys
->i_width
- i_column
);
622 memset( &p_dest
[i_row
* p_region
->p_picture
->Y_PITCH
+
623 i_column
], i_color
, i_count
);
624 i_column
+= i_count
- 1;