]> git.sesse.net Git - vlc/blob - modules/codec/spudec/parse.c
* modules/codec/spudec/spudec.c: packetizer needs to duplicate the input es_format_t.
[vlc] / modules / codec / spudec / parse.c
1 /*****************************************************************************
2  * parse.c: SPU parser
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <vlc/vlc.h>
30 #include <vlc/vout.h>
31 #include <vlc/decoder.h>
32
33 #include "spudec.h"
34
35 /*****************************************************************************
36  * Local prototypes.
37  *****************************************************************************/
38 static int  ParseControlSeq( decoder_t *, subpicture_t *, subpicture_data_t *);
39 static int  ParseRLE       ( decoder_t *, subpicture_t *, subpicture_data_t *);
40 static void Render         ( decoder_t *, subpicture_t *, subpicture_data_t *);
41
42 /*****************************************************************************
43  * AddNibble: read a nibble from a source packet and add it to our integer.
44  *****************************************************************************/
45 static inline unsigned int AddNibble( unsigned int i_code,
46                                       uint8_t *p_src, int *pi_index )
47 {
48     if( *pi_index & 0x1 )
49     {
50         return( i_code << 4 | ( p_src[(*pi_index)++ >> 1] & 0xf ) );
51     }
52     else
53     {
54         return( i_code << 4 | p_src[(*pi_index)++ >> 1] >> 4 );
55     }
56 }
57
58 /*****************************************************************************
59  * ParsePacket: parse an SPU packet and send it to the video output
60  *****************************************************************************
61  * This function parses the SPU packet and, if valid, sends it to the
62  * video output.
63  *****************************************************************************/
64 subpicture_t * E_(ParsePacket)( decoder_t *p_dec )
65 {
66     decoder_sys_t *p_sys = p_dec->p_sys;
67     subpicture_data_t *p_spu_data;
68     subpicture_t *p_spu;
69
70     /* Allocate the subpicture internal data. */
71     p_spu = p_dec->pf_spu_buffer_new( p_dec );
72     if( !p_spu ) return NULL;
73
74     /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
75      * expand the RLE stuff so that we won't need to read nibbles later
76      * on. This will speed things up a lot. Plus, we'll only need to do
77      * this stupid interlacing stuff once. */
78     p_spu_data = malloc( sizeof(subpicture_data_t) + 4 * p_sys->i_rle_size );
79     p_spu_data->p_data = (uint8_t *)p_spu_data + sizeof(subpicture_data_t);
80     p_spu_data->b_palette = VLC_FALSE;
81     p_spu_data->b_auto_crop = VLC_FALSE;
82     p_spu_data->i_y_top_offset = 0;
83     p_spu_data->i_y_bottom_offset = 0;
84
85     p_spu_data->pi_alpha[0] = 0x00;
86     p_spu_data->pi_alpha[1] = 0x0f;
87     p_spu_data->pi_alpha[2] = 0x0f;
88     p_spu_data->pi_alpha[3] = 0x0f;
89
90     /* Get display time now. If we do it later, we may miss the PTS. */
91     p_spu_data->i_pts = p_sys->i_pts;
92
93     p_spu->i_original_picture_width =
94         p_dec->fmt_in.subs.spu.i_original_frame_width;
95     p_spu->i_original_picture_height =
96         p_dec->fmt_in.subs.spu.i_original_frame_height;
97
98     /* Getting the control part */
99     if( ParseControlSeq( p_dec, p_spu, p_spu_data ) )
100     {
101         /* There was a parse error, delete the subpicture */
102         p_dec->pf_spu_buffer_del( p_dec, p_spu );
103         return NULL;
104     }
105
106     /* We try to display it */
107     if( ParseRLE( p_dec, p_spu, p_spu_data ) )
108     {
109         /* There was a parse error, delete the subpicture */
110         p_dec->pf_spu_buffer_del( p_dec, p_spu );
111         return NULL;
112     }
113
114     msg_Dbg( p_dec, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
115              p_sys->i_spu_size,
116              p_spu_data->pi_offset[0], p_spu_data->pi_offset[1] );
117
118     Render( p_dec, p_spu, p_spu_data );
119     free( p_spu_data );
120
121     return p_spu;
122 }
123
124 /*****************************************************************************
125  * ParseControlSeq: parse all SPU control sequences
126  *****************************************************************************
127  * This is the most important part in SPU decoding. We get dates, palette
128  * information, coordinates, and so on. For more information on the
129  * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
130  *****************************************************************************/
131 static int ParseControlSeq( decoder_t *p_dec, subpicture_t *p_spu,
132                             subpicture_data_t *p_spu_data )
133 {
134     decoder_sys_t *p_sys = p_dec->p_sys;
135
136     /* Our current index in the SPU packet */
137     unsigned int i_index = p_sys->i_rle_size + 4;
138
139     /* The next start-of-control-sequence index and the previous one */
140     unsigned int i_next_seq = 0, i_cur_seq = 0;
141
142     /* Command and date */
143     uint8_t i_command = SPU_CMD_END;
144     mtime_t date = 0;
145
146     unsigned int i, pi_alpha[4];
147
148     /* Initialize the structure */
149     p_spu->i_start = p_spu->i_stop = 0;
150     p_spu->b_ephemer = VLC_FALSE;
151
152     do
153     {
154         if( (int)i_index >= p_sys->i_spu_size + 1 )
155         {
156             /* sanity
157              * XXX only on test by loop as p_sys->buffer is bigger than needed
158              * to avoid checking at each access
159              */
160             break;
161         }
162
163         /* If we just read a command sequence, read the next one;
164          * otherwise, go on with the commands of the current sequence. */
165         if( i_command == SPU_CMD_END )
166         {
167             /* Get the control sequence date */
168             date = (mtime_t)GetWBE( &p_sys->buffer[i_index] ) * 11000;
169             /* FIXME How to access i_rate
170                     * p_spudec->bit_stream.p_pes->i_rate / DEFAULT_RATE;
171             */
172
173             /* Next offset */
174             i_cur_seq = i_index;
175             i_next_seq = GetWBE( &p_sys->buffer[i_index+2] );
176
177             /* Skip what we just read */
178             i_index += 4;
179         }
180
181         i_command = p_sys->buffer[i_index++];
182
183         switch( i_command )
184         {
185         case SPU_CMD_FORCE_DISPLAY: /* 00 (force displaying) */
186             p_spu->i_start = p_spu_data->i_pts + date;
187             p_spu->b_ephemer = VLC_TRUE;
188             break;
189
190         /* Convert the dates in seconds to PTS values */
191         case SPU_CMD_START_DISPLAY: /* 01 (start displaying) */
192             p_spu->i_start = p_spu_data->i_pts + date;
193             break;
194
195         case SPU_CMD_STOP_DISPLAY: /* 02 (stop displaying) */
196             p_spu->i_stop = p_spu_data->i_pts + date;
197             break;
198
199         case SPU_CMD_SET_PALETTE:
200
201             /* 03xxxx (palette) */
202             if( p_dec->fmt_in.subs.spu.palette[0] == 0xBeeF )
203             {
204                 unsigned int idx[4];
205
206                 p_spu_data->b_palette = VLC_TRUE;
207
208                 idx[0] = (p_sys->buffer[i_index+0]>>4)&0x0f;
209                 idx[1] = (p_sys->buffer[i_index+0])&0x0f;
210                 idx[2] = (p_sys->buffer[i_index+1]>>4)&0x0f;
211                 idx[3] = (p_sys->buffer[i_index+1])&0x0f;
212
213                 for( i = 0; i < 4 ; i++ )
214                 {
215                     uint32_t i_color = p_dec->fmt_in.subs.spu.palette[1+idx[i]];
216
217                     /* FIXME: this job should be done sooner */
218                     p_spu_data->pi_yuv[3-i][0] = (i_color>>16) & 0xff;
219                     p_spu_data->pi_yuv[3-i][1] = (i_color>>0) & 0xff;
220                     p_spu_data->pi_yuv[3-i][2] = (i_color>>8) & 0xff;
221                 }
222             }
223             i_index += 2;
224
225             break;
226
227         case SPU_CMD_SET_ALPHACHANNEL: /* 04xxxx (alpha channel) */
228             pi_alpha[3] = (p_sys->buffer[i_index+0]>>4)&0x0f;
229             pi_alpha[2] = (p_sys->buffer[i_index+0])&0x0f;
230             pi_alpha[1] = (p_sys->buffer[i_index+1]>>4)&0x0f;
231             pi_alpha[0] = (p_sys->buffer[i_index+1])&0x0f;
232
233             /* Ignore blank alpha palette. Sometimes spurious blank
234              * alpha palettes are present - dunno why. */
235             if( pi_alpha[0] | pi_alpha[1] | pi_alpha[2] | pi_alpha[3] )
236             {
237                 p_spu_data->pi_alpha[0] = pi_alpha[0];
238                 p_spu_data->pi_alpha[1] = pi_alpha[1];
239                 p_spu_data->pi_alpha[2] = pi_alpha[2];
240                 p_spu_data->pi_alpha[3] = pi_alpha[3];
241             }
242             else
243             {
244                 msg_Warn( p_dec, "ignoring blank alpha palette" );
245             }
246
247             i_index += 2;
248             break;
249
250         case SPU_CMD_SET_COORDINATES: /* 05xxxyyyxxxyyy (coordinates) */
251             p_spu->i_x = (p_sys->buffer[i_index+0]<<4)|
252                          ((p_sys->buffer[i_index+1]>>4)&0x0f);
253             p_spu->i_width = (((p_sys->buffer[i_index+1]&0x0f)<<8)|
254                               p_sys->buffer[i_index+2]) - p_spu->i_x + 1;
255
256             p_spu->i_y = (p_sys->buffer[i_index+3]<<4)|
257                          ((p_sys->buffer[i_index+4]>>4)&0x0f);
258             p_spu->i_height = (((p_sys->buffer[i_index+4]&0x0f)<<8)|
259                               p_sys->buffer[i_index+5]) - p_spu->i_y + 1;
260
261             /* Auto crop fullscreen subtitles */
262             if( p_spu->i_height > 250 )
263                 p_spu_data->b_auto_crop = VLC_TRUE;
264
265             i_index += 6;
266             break;
267
268         case SPU_CMD_SET_OFFSETS: /* 06xxxxyyyy (byte offsets) */
269             p_spu_data->pi_offset[0] = GetWBE(&p_sys->buffer[i_index+0]) - 4;
270             p_spu_data->pi_offset[1] = GetWBE(&p_sys->buffer[i_index+2]) - 4;
271             i_index += 4;
272             break;
273
274         case SPU_CMD_END: /* ff (end) */
275             break;
276
277         default: /* xx (unknown command) */
278             msg_Warn( p_dec, "unknown command 0x%.2x", i_command );
279             return VLC_EGENERIC;
280         }
281
282         /* We need to check for quit commands here */
283         if( p_dec->b_die )
284         {
285             return VLC_EGENERIC;
286         }
287
288     } while( i_command != SPU_CMD_END || i_index == i_next_seq );
289
290     /* Check that the next sequence index matches the current one */
291     if( i_next_seq != i_cur_seq )
292     {
293         msg_Err( p_dec, "index mismatch (0x%.4x != 0x%.4x)",
294                  i_next_seq, i_cur_seq );
295         return VLC_EGENERIC;
296     }
297
298     if( (int)i_index > p_sys->i_spu_size )
299     {
300         msg_Err( p_dec, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
301                  i_index, p_sys->i_spu_size );
302         return VLC_EGENERIC;
303     }
304
305     if( !p_spu->i_start )
306     {
307         msg_Err( p_dec, "no `start display' command" );
308     }
309
310     if( p_spu->i_stop <= p_spu->i_start && !p_spu->b_ephemer )
311     {
312         /* This subtitle will live for 5 seconds or until the next subtitle */
313         p_spu->i_stop = p_spu->i_start + (mtime_t)500 * 11000;
314         p_spu->b_ephemer = VLC_TRUE;
315     }
316
317     /* Get rid of padding bytes */
318     if( p_sys->i_spu_size > (int)i_index + 1 )
319     {
320         /* Zero or one padding byte, are quite usual
321          * More than one padding byte - this is very strange, but
322          * we can deal with it */
323         msg_Warn( p_dec, "%i padding bytes, we usually get 0 or 1 of them",
324                   p_sys->i_spu_size - i_index );
325     }
326
327     /* Successfully parsed ! */
328     return VLC_SUCCESS;
329 }
330
331 /*****************************************************************************
332  * ParseRLE: parse the RLE part of the subtitle
333  *****************************************************************************
334  * This part parses the subtitle graphical data and stores it in a more
335  * convenient structure for later decoding. For more information on the
336  * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
337  *****************************************************************************/
338 static int ParseRLE( decoder_t *p_dec, subpicture_t * p_spu,
339                      subpicture_data_t *p_spu_data )
340 {
341     decoder_sys_t *p_sys = p_dec->p_sys;
342     uint8_t       *p_src = &p_sys->buffer[4];
343
344     unsigned int i_code;
345
346     unsigned int i_width = p_spu->i_width;
347     unsigned int i_height = p_spu->i_height;
348     unsigned int i_x, i_y;
349
350     uint16_t *p_dest = (uint16_t *)p_spu_data->p_data;
351
352     /* The subtitles are interlaced, we need two offsets */
353     unsigned int  i_id = 0;                   /* Start on the even SPU layer */
354     unsigned int  pi_table[ 2 ];
355     unsigned int *pi_offset;
356
357     /* Cropping */
358     vlc_bool_t b_empty_top = VLC_TRUE;
359     unsigned int i_skipped_top = 0, i_skipped_bottom = 0;
360     unsigned int i_transparent_code = 0;
361  
362     /* Colormap statistics */
363     int i_border = -1;
364     int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
365
366     pi_table[ 0 ] = p_spu_data->pi_offset[ 0 ] << 1;
367     pi_table[ 1 ] = p_spu_data->pi_offset[ 1 ] << 1;
368
369     for( i_y = 0 ; i_y < i_height ; i_y++ )
370     {
371         pi_offset = pi_table + i_id;
372
373         for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
374         {
375             i_code = AddNibble( 0, p_src, pi_offset );
376
377             if( i_code < 0x04 )
378             {
379                 i_code = AddNibble( i_code, p_src, pi_offset );
380
381                 if( i_code < 0x10 )
382                 {
383                     i_code = AddNibble( i_code, p_src, pi_offset );
384
385                     if( i_code < 0x040 )
386                     {
387                         i_code = AddNibble( i_code, p_src, pi_offset );
388
389                         if( i_code < 0x0100 )
390                         {
391                             /* If the 14 first bits are set to 0, then it's a
392                              * new line. We emulate it. */
393                             if( i_code < 0x0004 )
394                             {
395                                 i_code |= ( i_width - i_x ) << 2;
396                             }
397                             else
398                             {
399                                 /* We have a boo boo ! */
400                                 msg_Err( p_dec, "unknown RLE code "
401                                          "0x%.4x", i_code );
402                                 return VLC_EGENERIC;
403                             }
404                         }
405                     }
406                 }
407             }
408
409             if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
410             {
411                 msg_Err( p_dec, "out of bounds, %i at (%i,%i) is out of %ix%i",
412                          i_code >> 2, i_x, i_y, i_width, i_height );
413                 return VLC_EGENERIC;
414             }
415
416             /* Try to find the border color */
417             if( p_spu_data->pi_alpha[ i_code & 0x3 ] != 0x00 )
418             {
419                 i_border = i_code & 0x3;
420                 stats[i_border] += i_code >> 2;
421             }
422
423             /* Auto crop subtitles (a lot more optimized) */
424             if( p_spu_data->b_auto_crop )
425             {
426                 if( !i_y )
427                 {
428                     /* We assume that if the first line is transparent, then
429                      * it is using the palette index for the
430                      * (background) transparent color */
431                     if( (i_code >> 2) == i_width &&
432                         p_spu_data->pi_alpha[ i_code & 0x3 ] == 0x00 )
433                     {
434                         i_transparent_code = i_code;
435                     }
436                     else
437                     {
438                         p_spu_data->b_auto_crop = VLC_FALSE;
439                     }
440                 }
441
442                 if( i_code == i_transparent_code )
443                 {
444                     if( b_empty_top )
445                     {
446                         /* This is a blank top line, we skip it */
447                       i_skipped_top++;
448                     }
449                     else
450                     {
451                         /* We can't be sure the current lines will be skipped,
452                          * so we store the code just in case. */
453                       *p_dest++ = i_code;
454                       i_skipped_bottom++;
455                     }
456                 }
457                 else
458                 {
459                     /* We got a valid code, store it */
460                     *p_dest++ = i_code;
461
462                     /* Valid code means no blank line */
463                     b_empty_top = VLC_FALSE;
464                     i_skipped_bottom = 0;
465                 }
466             }
467             else
468             {
469                 *p_dest++ = i_code;
470             }
471         }
472
473         /* Check that we didn't go too far */
474         if( i_x > i_width )
475         {
476             msg_Err( p_dec, "i_x overflowed, %i > %i", i_x, i_width );
477             return VLC_EGENERIC;
478         }
479
480         /* Byte-align the stream */
481         if( *pi_offset & 0x1 )
482         {
483             (*pi_offset)++;
484         }
485
486         /* Swap fields */
487         i_id = ~i_id & 0x1;
488     }
489
490     /* We shouldn't get any padding bytes */
491     if( i_y < i_height )
492     {
493         msg_Err( p_dec, "padding bytes found in RLE sequence" );
494         msg_Err( p_dec, "send mail to <sam@zoy.org> if you "
495                         "want to help debugging this" );
496
497         /* Skip them just in case */
498         while( i_y < i_height )
499         {
500             *p_dest++ = i_width << 2;
501             i_y++;
502         }
503
504         return VLC_EGENERIC;
505     }
506
507     msg_Dbg( p_dec, "valid subtitle, size: %ix%i, position: %i,%i",
508              p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
509
510     /* Crop if necessary */
511     if( i_skipped_top || i_skipped_bottom )
512     {
513         int i_y = p_spu->i_y + i_skipped_top;
514         int i_height = p_spu->i_height - (i_skipped_top + i_skipped_bottom);
515
516         p_spu_data->i_y_top_offset = i_skipped_top;
517         p_spu_data->i_y_bottom_offset = i_skipped_bottom;
518         msg_Dbg( p_dec, "cropped to: %ix%i, position: %i,%i",
519                  p_spu->i_width, i_height, p_spu->i_x, i_y );
520     }
521  
522     /* Handle color if no palette was found */
523     if( !p_spu_data->b_palette )
524     {
525         int i, i_inner = -1, i_shade = -1;
526
527         /* Set the border color */
528         p_spu_data->pi_yuv[i_border][0] = 0x00;
529         p_spu_data->pi_yuv[i_border][1] = 0x80;
530         p_spu_data->pi_yuv[i_border][2] = 0x80;
531         stats[i_border] = 0;
532
533         /* Find the inner colors */
534         for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
535         {
536             if( stats[i] )
537             {
538                 i_inner = i;
539             }
540         }
541
542         for(       ; i < 4 && i_shade == -1 ; i++ )
543         {
544             if( stats[i] )
545             {
546                 if( stats[i] > stats[i_inner] )
547                 {
548                     i_shade = i_inner;
549                     i_inner = i;
550                 }
551                 else
552                 {
553                     i_shade = i;
554                 }
555             }
556         }
557
558         /* Set the inner color */
559         if( i_inner != -1 )
560         {
561             p_spu_data->pi_yuv[i_inner][0] = 0xff;
562             p_spu_data->pi_yuv[i_inner][1] = 0x80;
563             p_spu_data->pi_yuv[i_inner][2] = 0x80;
564         }
565
566         /* Set the anti-aliasing color */
567         if( i_shade != -1 )
568         {
569             p_spu_data->pi_yuv[i_shade][0] = 0x80;
570             p_spu_data->pi_yuv[i_shade][1] = 0x80;
571             p_spu_data->pi_yuv[i_shade][2] = 0x80;
572         }
573
574         msg_Dbg( p_dec, "using custom palette (border %i, inner %i, shade %i)",
575                  i_border, i_inner, i_shade );
576     }
577
578     return VLC_SUCCESS;
579 }
580
581 static void Render( decoder_t *p_dec, subpicture_t *p_spu,
582                     subpicture_data_t *p_spu_data )
583 {
584     uint8_t *p_p;
585     int i_x, i_y, i_len, i_color, i_pitch;
586     uint16_t *p_source = (uint16_t *)p_spu_data->p_data;
587     video_format_t fmt;
588
589     /* Create a new subpicture region */
590     memset( &fmt, 0, sizeof(video_format_t) );
591     fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
592     fmt.i_aspect = VOUT_ASPECT_FACTOR;
593     fmt.i_width = fmt.i_visible_width = p_spu->i_width;
594     fmt.i_height = fmt.i_visible_height = p_spu->i_height -
595         p_spu_data->i_y_top_offset - p_spu_data->i_y_bottom_offset;
596     fmt.i_x_offset = fmt.i_y_offset = 0;
597     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
598     if( !p_spu->p_region )
599     {
600         msg_Err( p_dec, "cannot allocate SPU region" );
601         return;
602     }
603
604     p_spu->p_region->i_x = 0;
605     p_spu->p_region->i_y = p_spu_data->i_y_top_offset;
606     p_p = p_spu->p_region->picture.p->p_pixels;
607     i_pitch = p_spu->p_region->picture.p->i_pitch;
608
609     /* Build palette */
610     fmt.p_palette->i_entries = 4;
611     for( i_x = 0; i_x < fmt.p_palette->i_entries; i_x++ )
612     {
613         fmt.p_palette->palette[i_x][0] = p_spu_data->pi_yuv[i_x][0];
614         fmt.p_palette->palette[i_x][1] = p_spu_data->pi_yuv[i_x][1];
615         fmt.p_palette->palette[i_x][2] = p_spu_data->pi_yuv[i_x][2];
616         fmt.p_palette->palette[i_x][3] =
617             p_spu_data->pi_alpha[i_x] == 0xf ? 0xff :
618             p_spu_data->pi_alpha[i_x] << 4;
619     }
620
621     /* Draw until we reach the bottom of the subtitle */
622     for( i_y = 0; i_y < (int)fmt.i_height * i_pitch; i_y += i_pitch )
623     {
624         /* Draw until we reach the end of the line */
625         for( i_x = 0 ; i_x < (int)fmt.i_width; i_x += i_len )
626         {
627             /* Get the RLE part, then draw the line */
628             i_color = *p_source & 0x3;
629             i_len = *p_source++ >> 2;
630             memset( p_p + i_x + i_y, i_color, i_len );
631         }
632     }
633 }