]> git.sesse.net Git - vlc/blob - modules/codec/spudec/parse.c
* ALL: Major rework of the subpictures architecture.
[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
82     p_spu_data->pi_alpha[0] = 0x00;
83     p_spu_data->pi_alpha[1] = 0x0f;
84     p_spu_data->pi_alpha[2] = 0x0f;
85     p_spu_data->pi_alpha[3] = 0x0f;
86
87     /* Get display time now. If we do it later, we may miss the PTS. */
88     p_spu_data->i_pts = p_sys->i_pts;
89
90     /* Getting the control part */
91     if( ParseControlSeq( p_dec, p_spu, p_spu_data ) )
92     {
93         /* There was a parse error, delete the subpicture */
94         p_dec->pf_spu_buffer_del( p_dec, p_spu );
95         return NULL;
96     }
97
98      /* We try to display it */
99     if( ParseRLE( 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     msg_Dbg( p_dec, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
107              p_sys->i_spu_size,
108              p_spu_data->pi_offset[0], p_spu_data->pi_offset[1] );
109
110     Render( p_dec, p_spu, p_spu_data );
111     free( p_spu_data );
112
113     return p_spu;
114 }
115
116 /*****************************************************************************
117  * ParseControlSeq: parse all SPU control sequences
118  *****************************************************************************
119  * This is the most important part in SPU decoding. We get dates, palette
120  * information, coordinates, and so on. For more information on the
121  * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
122  *****************************************************************************/
123 static int ParseControlSeq( decoder_t *p_dec, subpicture_t *p_spu,
124                             subpicture_data_t *p_spu_data )
125 {
126     decoder_sys_t *p_sys = p_dec->p_sys;
127
128     /* Our current index in the SPU packet */
129     unsigned int i_index = p_sys->i_rle_size + 4;
130
131     /* The next start-of-control-sequence index and the previous one */
132     unsigned int i_next_seq = 0, i_cur_seq = 0;
133
134     /* Command and date */
135     uint8_t i_command = SPU_CMD_END;
136     mtime_t date = 0;
137
138     unsigned int i, pi_alpha[4];
139
140     /* Initialize the structure */
141     p_spu->i_start = p_spu->i_stop = 0;
142     p_spu->b_ephemer = VLC_FALSE;
143
144     do
145     {
146         if( (int)i_index >= p_sys->i_spu_size + 1 )
147         {
148             /* sanity
149              * XXX only on test by loop as p_sys->buffer is bigger than needed
150              * to avoid checking at each access
151              */
152             break;
153         }
154
155         /* If we just read a command sequence, read the next one;
156          * otherwise, go on with the commands of the current sequence. */
157         if( i_command == SPU_CMD_END )
158         {
159             /* Get the control sequence date */
160             date = (mtime_t)GetWBE( &p_sys->buffer[i_index] ) * 11000;
161             /* FIXME How to access i_rate
162                     * p_spudec->bit_stream.p_pes->i_rate / DEFAULT_RATE;
163             */
164
165             /* Next offset */
166             i_cur_seq = i_index;
167             i_next_seq = GetWBE( &p_sys->buffer[i_index+2] );
168
169             /* Skip what we just read */
170             i_index += 4;
171         }
172
173         i_command = p_sys->buffer[i_index++];
174
175         switch( i_command )
176         {
177         case SPU_CMD_FORCE_DISPLAY: /* 00 (force displaying) */
178             p_spu->i_start = p_spu_data->i_pts + date;
179             p_spu->b_ephemer = VLC_TRUE;
180             break;
181
182         /* Convert the dates in seconds to PTS values */
183         case SPU_CMD_START_DISPLAY: /* 01 (start displaying) */
184             p_spu->i_start = p_spu_data->i_pts + date;
185             break;
186
187         case SPU_CMD_STOP_DISPLAY: /* 02 (stop displaying) */
188             p_spu->i_stop = p_spu_data->i_pts + date;
189             break;
190
191         case SPU_CMD_SET_PALETTE:
192
193             /* 03xxxx (palette) */
194             if( p_dec->fmt_in.subs.spu.palette[0] == 0xBeeF )
195             {
196                 unsigned int idx[4];
197
198                 p_spu_data->b_palette = VLC_TRUE;
199
200                 idx[0] = (p_sys->buffer[i_index+0]>>4)&0x0f;
201                 idx[1] = (p_sys->buffer[i_index+0])&0x0f;
202                 idx[2] = (p_sys->buffer[i_index+1]>>4)&0x0f;
203                 idx[3] = (p_sys->buffer[i_index+1])&0x0f;
204
205                 for( i = 0; i < 4 ; i++ )
206                 {
207                     uint32_t i_color = p_dec->fmt_in.subs.spu.palette[1+idx[i]];
208
209                     /* FIXME: this job should be done sooner */
210                     p_spu_data->pi_yuv[3-i][0] = (i_color>>16) & 0xff;
211                     p_spu_data->pi_yuv[3-i][1] = (i_color>>0) & 0xff;
212                     p_spu_data->pi_yuv[3-i][2] = (i_color>>8) & 0xff;
213                 }
214             }
215             i_index += 2;
216
217             break;
218
219         case SPU_CMD_SET_ALPHACHANNEL: /* 04xxxx (alpha channel) */
220             pi_alpha[3] = (p_sys->buffer[i_index+0]>>4)&0x0f;
221             pi_alpha[2] = (p_sys->buffer[i_index+0])&0x0f;
222             pi_alpha[1] = (p_sys->buffer[i_index+1]>>4)&0x0f;
223             pi_alpha[0] = (p_sys->buffer[i_index+1])&0x0f;
224
225             /* Ignore blank alpha palette. Sometimes spurious blank
226              * alpha palettes are present - dunno why. */
227             if( pi_alpha[0] | pi_alpha[1] | pi_alpha[2] | pi_alpha[3] )
228             {
229                 p_spu_data->pi_alpha[0] = pi_alpha[0];
230                 p_spu_data->pi_alpha[1] = pi_alpha[1];
231                 p_spu_data->pi_alpha[2] = pi_alpha[2];
232                 p_spu_data->pi_alpha[3] = pi_alpha[3];
233             }
234             else
235             {
236                 msg_Warn( p_dec, "ignoring blank alpha palette" );
237             }
238
239             i_index += 2;
240             break;
241
242         case SPU_CMD_SET_COORDINATES: /* 05xxxyyyxxxyyy (coordinates) */
243             p_spu->i_x = (p_sys->buffer[i_index+0]<<4)|
244                          ((p_sys->buffer[i_index+1]>>4)&0x0f);
245             p_spu->i_width = (((p_sys->buffer[i_index+1]&0x0f)<<8)|
246                               p_sys->buffer[i_index+2]) - p_spu->i_x + 1;
247
248             p_spu->i_y = (p_sys->buffer[i_index+3]<<4)|
249                          ((p_sys->buffer[i_index+4]>>4)&0x0f);
250             p_spu->i_height = (((p_sys->buffer[i_index+4]&0x0f)<<8)|
251                               p_sys->buffer[i_index+5]) - p_spu->i_y + 1;
252             
253             i_index += 6;
254             break;
255
256         case SPU_CMD_SET_OFFSETS: /* 06xxxxyyyy (byte offsets) */
257             p_spu_data->pi_offset[0] = GetWBE(&p_sys->buffer[i_index+0]) - 4;
258             p_spu_data->pi_offset[1] = GetWBE(&p_sys->buffer[i_index+2]) - 4;
259             i_index += 4;
260             break;
261
262         case SPU_CMD_END: /* ff (end) */
263             break;
264
265         default: /* xx (unknown command) */
266             msg_Warn( p_dec, "unknown command 0x%.2x", i_command );
267             return VLC_EGENERIC;
268         }
269
270         /* We need to check for quit commands here */
271         if( p_dec->b_die )
272         {
273             return VLC_EGENERIC;
274         }
275
276     } while( i_command != SPU_CMD_END || i_index == i_next_seq );
277
278     /* Check that the next sequence index matches the current one */
279     if( i_next_seq != i_cur_seq )
280     {
281         msg_Err( p_dec, "index mismatch (0x%.4x != 0x%.4x)",
282                  i_next_seq, i_cur_seq );
283         return VLC_EGENERIC;
284     }
285
286     if( (int)i_index > p_sys->i_spu_size )
287     {
288         msg_Err( p_dec, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
289                  i_index, p_sys->i_spu_size );
290         return VLC_EGENERIC;
291     }
292
293     if( !p_spu->i_start )
294     {
295         msg_Err( p_dec, "no `start display' command" );
296     }
297
298     if( p_spu->i_stop <= p_spu->i_start && !p_spu->b_ephemer )
299     {
300         /* This subtitle will live for 5 seconds or until the next subtitle */
301         p_spu->i_stop = p_spu->i_start + (mtime_t)500 * 11000;
302         p_spu->b_ephemer = VLC_TRUE;
303     }
304
305     /* Get rid of padding bytes */
306     if( p_sys->i_spu_size > (int)i_index + 1 )
307     {
308         /* Zero or one padding byte, are quite usual
309          * More than one padding byte - this is very strange, but
310          * we can deal with it */
311         msg_Warn( p_dec, "%i padding bytes, we usually get 0 or 1 of them",
312                   p_sys->i_spu_size - i_index );
313     }
314
315     /* Successfully parsed ! */
316     return VLC_SUCCESS;
317 }
318
319 /*****************************************************************************
320  * ParseRLE: parse the RLE part of the subtitle
321  *****************************************************************************
322  * This part parses the subtitle graphical data and stores it in a more
323  * convenient structure for later decoding. For more information on the
324  * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
325  *****************************************************************************/
326 static int ParseRLE( decoder_t *p_dec, subpicture_t * p_spu,
327                      subpicture_data_t *p_spu_data )
328 {
329     decoder_sys_t *p_sys = p_dec->p_sys;
330     uint8_t       *p_src = &p_sys->buffer[4];
331
332     unsigned int i_code;
333
334     unsigned int i_width = p_spu->i_width;
335     unsigned int i_height = p_spu->i_height;
336     unsigned int i_x, i_y;
337
338     uint16_t *p_dest = (uint16_t *)p_spu_data->p_data;
339
340     /* The subtitles are interlaced, we need two offsets */
341     unsigned int  i_id = 0;                   /* Start on the even SPU layer */
342     unsigned int  pi_table[ 2 ];
343     unsigned int *pi_offset;
344
345     /* Colormap statistics */
346     int i_border = -1;
347     int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
348
349     pi_table[ 0 ] = p_spu_data->pi_offset[ 0 ] << 1;
350     pi_table[ 1 ] = p_spu_data->pi_offset[ 1 ] << 1;
351
352     for( i_y = 0 ; i_y < i_height ; i_y++ )
353     {
354         pi_offset = pi_table + i_id;
355
356         for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
357         {
358             i_code = AddNibble( 0, p_src, pi_offset );
359
360             if( i_code < 0x04 )
361             {
362                 i_code = AddNibble( i_code, p_src, pi_offset );
363
364                 if( i_code < 0x10 )
365                 {
366                     i_code = AddNibble( i_code, p_src, pi_offset );
367
368                     if( i_code < 0x040 )
369                     {
370                         i_code = AddNibble( i_code, p_src, pi_offset );
371
372                         if( i_code < 0x0100 )
373                         {
374                             /* If the 14 first bits are set to 0, then it's a
375                              * new line. We emulate it. */
376                             if( i_code < 0x0004 )
377                             {
378                                 i_code |= ( i_width - i_x ) << 2;
379                             }
380                             else
381                             {
382                                 /* We have a boo boo ! */
383                                 msg_Err( p_dec, "unknown RLE code "
384                                          "0x%.4x", i_code );
385                                 return VLC_EGENERIC;
386                             }
387                         }
388                     }
389                 }
390             }
391
392             if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
393             {
394                 msg_Err( p_dec, "out of bounds, %i at (%i,%i) is out of %ix%i",
395                          i_code >> 2, i_x, i_y, i_width, i_height );
396                 return VLC_EGENERIC;
397             }
398
399             /* Try to find the border color */
400             if( p_spu_data->pi_alpha[ i_code & 0x3 ] != 0x00 )
401             {
402                 i_border = i_code & 0x3;
403                 stats[i_border] += i_code >> 2;
404             }
405
406             *p_dest++ = i_code;
407         }
408
409         /* Check that we didn't go too far */
410         if( i_x > i_width )
411         {
412             msg_Err( p_dec, "i_x overflowed, %i > %i", i_x, i_width );
413             return VLC_EGENERIC;
414         }
415
416         /* Byte-align the stream */
417         if( *pi_offset & 0x1 )
418         {
419             (*pi_offset)++;
420         }
421
422         /* Swap fields */
423         i_id = ~i_id & 0x1;
424     }
425
426     /* We shouldn't get any padding bytes */
427     if( i_y < i_height )
428     {
429         msg_Err( p_dec, "padding bytes found in RLE sequence" );
430         msg_Err( p_dec, "send mail to <sam@zoy.org> if you "
431                         "want to help debugging this" );
432
433         /* Skip them just in case */
434         while( i_y < i_height )
435         {
436             *p_dest++ = i_width << 2;
437             i_y++;
438         }
439
440         return VLC_EGENERIC;
441     }
442
443     msg_Dbg( p_dec, "valid subtitle, size: %ix%i, position: %i,%i",
444              p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
445
446     /* Handle color if no palette was found */
447     if( !p_spu_data->b_palette )
448     {
449         int i, i_inner = -1, i_shade = -1;
450
451         /* Set the border color */
452         p_spu_data->pi_yuv[i_border][0] = 0x00;
453         p_spu_data->pi_yuv[i_border][1] = 0x80;
454         p_spu_data->pi_yuv[i_border][2] = 0x80;
455         stats[i_border] = 0;
456
457         /* Find the inner colors */
458         for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
459         {
460             if( stats[i] )
461             {
462                 i_inner = i;
463             }
464         }
465
466         for(       ; i < 4 && i_shade == -1 ; i++ )
467         {
468             if( stats[i] )
469             {
470                 if( stats[i] > stats[i_inner] )
471                 {
472                     i_shade = i_inner;
473                     i_inner = i;
474                 }
475                 else
476                 {
477                     i_shade = i;
478                 }
479             }
480         }
481
482         /* Set the inner color */
483         if( i_inner != -1 )
484         {
485             p_spu_data->pi_yuv[i_inner][0] = 0xff;
486             p_spu_data->pi_yuv[i_inner][1] = 0x80;
487             p_spu_data->pi_yuv[i_inner][2] = 0x80;
488         }
489
490         /* Set the anti-aliasing color */
491         if( i_shade != -1 )
492         {
493             p_spu_data->pi_yuv[i_shade][0] = 0x80;
494             p_spu_data->pi_yuv[i_shade][1] = 0x80;
495             p_spu_data->pi_yuv[i_shade][2] = 0x80;
496         }
497
498         msg_Dbg( p_dec, "using custom palette (border %i, inner %i, shade %i)",
499                  i_border, i_inner, i_shade );
500     }
501
502     return VLC_SUCCESS;
503 }
504
505 static void Render( decoder_t *p_dec, subpicture_t *p_spu,
506                     subpicture_data_t *p_spu_data )
507 {
508     uint8_t *p_p;
509     int i_x, i_y, i_len, i_color, i_pitch;
510     uint16_t *p_source = (uint16_t *)p_spu_data->p_data;
511     video_format_t fmt;
512
513     /* Create a new subpicture region */
514     memset( &fmt, 0, sizeof(video_format_t) );
515     fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
516     fmt.i_aspect = VOUT_ASPECT_FACTOR;
517     fmt.i_width = fmt.i_visible_width = p_spu->i_width;
518     fmt.i_height = fmt.i_visible_height = p_spu->i_height;
519     fmt.i_x_offset = fmt.i_y_offset = 0;
520     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
521     if( !p_spu->p_region )
522     {
523         msg_Err( p_dec, "cannot allocate SPU region" );
524         return;
525     }
526
527     p_spu->p_region->i_x = p_spu->p_region->i_y = 0;
528     p_p = p_spu->p_region->picture.p->p_pixels;
529     i_pitch = p_spu->p_region->picture.p->i_pitch;
530
531     /* Build palette */
532     for( i_x = 0; i_x < 4; i_x++ )
533     {
534         fmt.p_palette->palette[i_x][0] = p_spu_data->pi_yuv[i_x][0];
535         fmt.p_palette->palette[i_x][1] = p_spu_data->pi_yuv[i_x][1];
536         fmt.p_palette->palette[i_x][2] = p_spu_data->pi_yuv[i_x][2];
537         fmt.p_palette->palette[i_x][3] =
538             p_spu_data->pi_alpha[i_x] == 0xf ? 0xff :
539             p_spu_data->pi_alpha[i_x] << 4;
540     }
541
542     /* Draw until we reach the bottom of the subtitle */
543     for( i_y = 0; i_y < p_spu->i_height * i_pitch; i_y += i_pitch )
544     {
545         /* Draw until we reach the end of the line */
546         for( i_x = 0 ; i_x < p_spu->i_width; i_x += i_len )
547         {
548             /* Get the RLE part, then draw the line */
549             i_color = *p_source & 0x3;
550             i_len = *p_source++ >> 2;
551             memset( p_p + i_x + i_y, i_color, i_len );
552         }
553     }
554 }