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