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