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