]> git.sesse.net Git - vlc/blob - src/video_decoder/vpar_headers.c
Dramatically improve compilation time, with a negligible performance
[vlc] / src / video_decoder / vpar_headers.c
1 /*****************************************************************************
2  * vpar_headers.c : headers parsing
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: vpar_headers.c,v 1.11 2001/10/01 16:44:07 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Stéphane Borel <stef@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29
30 #include <stdlib.h>                                                /* free() */
31 #include <string.h>                                    /* memcpy(), memset() */
32
33 #include "config.h"
34 #include "common.h"
35 #include "threads.h"
36 #include "mtime.h"
37
38 #include "intf_msg.h"
39
40 #include "stream_control.h"
41 #include "input_ext-dec.h"
42
43 #include "video.h"
44 #include "video_output.h"
45
46 #include "vdec_ext-plugins.h"
47 #include "vpar_pool.h"
48 #include "video_parser.h"
49
50 /*
51  * Local prototypes
52  */
53 static __inline__ void NextStartCode( bit_stream_t * );
54 static void SequenceHeader( vpar_thread_t * p_vpar );
55 static void GroupHeader( vpar_thread_t * p_vpar );
56 static void PictureHeader( vpar_thread_t * p_vpar );
57 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
58 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
59 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
60 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
61 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
62 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
63 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
64 static void CopyrightExtension( vpar_thread_t * p_vpar );
65
66 /*
67  * Standard variables
68  */
69
70 /*****************************************************************************
71  * pi_default_intra_quant : default quantization matrix
72  *****************************************************************************/
73 u8 pi_default_intra_quant[] ATTR_ALIGN(16) =
74 {
75     8,  16, 19, 22, 26, 27, 29, 34,
76     16, 16, 22, 24, 27, 29, 34, 37,
77     19, 22, 26, 27, 29, 34, 34, 38,
78     22, 22, 26, 27, 29, 34, 37, 40,
79     22, 26, 27, 29, 32, 35, 40, 48,
80     26, 27, 29, 32, 35, 40, 48, 58,
81     26, 27, 29, 34, 38, 46, 56, 69,
82     27, 29, 35, 38, 46, 56, 69, 83
83 };
84
85 /*****************************************************************************
86  * pi_default_nonintra_quant : default quantization matrix
87  *****************************************************************************/
88 u8 pi_default_nonintra_quant[] ATTR_ALIGN(16) =
89 {
90     16, 16, 16, 16, 16, 16, 16, 16,
91     16, 16, 16, 16, 16, 16, 16, 16,
92     16, 16, 16, 16, 16, 16, 16, 16,
93     16, 16, 16, 16, 16, 16, 16, 16,
94     16, 16, 16, 16, 16, 16, 16, 16,
95     16, 16, 16, 16, 16, 16, 16, 16,
96     16, 16, 16, 16, 16, 16, 16, 16,
97     16, 16, 16, 16, 16, 16, 16, 16
98 };
99
100 /*****************************************************************************
101  * pi_scan : zig-zag and alternate scan patterns
102  *****************************************************************************/
103 u8 pi_scan[2][64] ATTR_ALIGN(16) =
104 {
105     { /* Zig-Zag pattern */
106         0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
107         12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
108         35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
109         58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
110     },
111     { /* Alternate scan pattern */
112         0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
113         41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
114         51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
115         53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
116     }
117 };
118
119 /*
120  * Local inline functions.
121  */
122
123 /*****************************************************************************
124  * ReferenceUpdate : Update the reference pointers when we have a new picture
125  *****************************************************************************/
126 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
127                                         int i_coding_type,
128                                         picture_t * p_newref )
129 {
130     if( i_coding_type != B_CODING_TYPE )
131     {
132         if( p_vpar->sequence.p_forward != NULL )
133         {
134             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
135         }
136         if( p_vpar->sequence.p_backward != NULL )
137         {
138             vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
139                               vpar_SynchroDate( p_vpar ) );
140         }
141         p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
142         p_vpar->sequence.p_backward = p_newref;
143         if( p_newref != NULL )
144         {
145             vout_LinkPicture( p_vpar->p_vout, p_newref );
146         }
147     }
148     else if( p_newref != NULL )
149     {
150         /* Put date immediately. */
151         vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
152     }
153 }
154
155 /*****************************************************************************
156  * ReferenceReplace : Replace the last reference pointer when we destroy
157  *                    a picture
158  *****************************************************************************/
159 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
160                                          int i_coding_type,
161                                          picture_t * p_newref )
162 {
163     if( i_coding_type != B_CODING_TYPE )
164     {
165         if( p_vpar->sequence.p_backward != NULL )
166         {
167             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
168         }
169         p_vpar->sequence.p_backward = p_newref;
170         if( p_newref != NULL )
171         {
172             vout_LinkPicture( p_vpar->p_vout, p_newref );
173         }
174     }
175 }
176
177 /*****************************************************************************
178  * LoadMatrix : Load a quantization matrix
179  *****************************************************************************/
180 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar,
181                                    quant_matrix_t * p_matrix )
182 {
183     int i_dummy;
184
185     if( !p_matrix->b_allocated )
186     {
187         /* Allocate a piece of memory to load the matrix. */
188         if( (p_matrix->pi_matrix = (u8 *)malloc( 64*sizeof(u8) )) == NULL )
189         {
190             intf_ErrMsg( "vpar error: allocation error in LoadMatrix()" );
191             p_vpar->p_fifo->b_error = 1;
192             return;
193         }
194         p_matrix->b_allocated = 1;
195     }
196
197     for( i_dummy = 0; i_dummy < 64; i_dummy++ )
198     {
199         p_matrix->pi_matrix[p_vpar->ppi_scan[0][i_dummy]]
200              = GetBits( &p_vpar->bit_stream, 8 );
201     }
202 }
203
204 /*****************************************************************************
205  * LinkMatrix : Link a quantization matrix to another
206  *****************************************************************************/
207 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, u8 * pi_array )
208 {
209     if( p_matrix->b_allocated )
210     {
211         /* Deallocate the piece of memory. */
212         free( p_matrix->pi_matrix );
213         p_matrix->b_allocated = 0;
214     }
215
216     p_matrix->pi_matrix = pi_array;
217 }
218
219 /*
220  * Exported functions.
221  */
222
223 /*****************************************************************************
224  * vpar_NextSequenceHeader : Find the next sequence header
225  *****************************************************************************/
226 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
227 {
228     while( !p_vpar->p_fifo->b_die )
229     {
230         NextStartCode( &p_vpar->bit_stream );
231         if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
232         {
233             return 0;
234         }
235         RemoveBits( &p_vpar->bit_stream, 8 );
236     }
237     return 1;
238 }
239
240 /*****************************************************************************
241  * vpar_ParseHeader : Parse the next header
242  *****************************************************************************/
243 int vpar_ParseHeader( vpar_thread_t * p_vpar )
244 {
245     while( !p_vpar->p_fifo->b_die )
246     {
247         NextStartCode( &p_vpar->bit_stream );
248         switch( GetBits32( &p_vpar->bit_stream ) )
249         {
250         case SEQUENCE_HEADER_CODE:
251             p_vpar->c_sequences++;
252
253             SequenceHeader( p_vpar );
254             return 0;
255             break;
256
257         case GROUP_START_CODE:
258             GroupHeader( p_vpar );
259             return 0;
260             break;
261
262         case PICTURE_START_CODE:
263             PictureHeader( p_vpar );
264             return 0;
265             break;
266
267         case SEQUENCE_END_CODE:
268             intf_DbgMsg("vpar debug: sequence end code received");
269             return 1;
270             break;
271
272         default:
273             break;
274         }
275     }
276
277     return 0;
278 }
279
280 /*
281  * Following functions are local
282  */
283
284 /*****************************************************************************
285  * SequenceHeader : Parse the next sequence header
286  *****************************************************************************/
287 static void SequenceHeader( vpar_thread_t * p_vpar )
288 {
289 #define RESERVED    -1
290     static int i_frame_rate_table[16] =
291     {
292         0,
293         23 * 1000,
294         24 * 1001,
295         25 * 1001,
296         30 * 1000,
297         30 * 1001,
298         50 * 1001,
299         60 * 1000,
300         60 * 1001,
301         RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
302     };
303 #undef RESERVED
304
305     int i_height_save, i_width_save;
306
307     i_height_save = p_vpar->sequence.i_height;
308     i_width_save = p_vpar->sequence.i_width;
309
310     p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
311     p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
312     p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
313     p_vpar->sequence.i_frame_rate =
314             i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
315
316     /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
317      * constrained_parameters_flag */
318     RemoveBits( &p_vpar->bit_stream, 30 );
319
320     /*
321      * Quantization matrices
322      */
323     if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
324     {
325         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
326     }
327     else
328     {
329         /* Use default matrix. */
330         LinkMatrix( &p_vpar->sequence.intra_quant,
331                     p_vpar->pi_default_intra_quant );
332     }
333
334     if( GetBits(&p_vpar->bit_stream, 1) ) /* load_non_intra_quantizer_matrix */
335     {
336         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
337     }
338     else
339     {
340         /* Use default matrix. */
341         LinkMatrix( &p_vpar->sequence.nonintra_quant,
342                     p_vpar->pi_default_nonintra_quant );
343     }
344
345     /* Unless later overwritten by a matrix extension, we have the same
346      * matrices for luminance and chrominance. */
347     LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
348                 p_vpar->sequence.intra_quant.pi_matrix );
349     LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
350                 p_vpar->sequence.nonintra_quant.pi_matrix );
351
352     /*
353      * Sequence Extension
354      */
355     NextStartCode( &p_vpar->bit_stream );
356     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
357     {
358         int                         i_dummy;
359
360         /* Turn the MPEG2 flag on */
361         p_vpar->sequence.b_mpeg2 = 1;
362
363         /* Parse sequence_extension */
364         RemoveBits32( &p_vpar->bit_stream );
365         /* extension_start_code_identifier, profile_and_level_indication */
366         RemoveBits( &p_vpar->bit_stream, 12 );
367         p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
368         p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
369         p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
370         p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
371         /* bit_rate_extension, marker_bit, vbv_buffer_size_extension,
372          * low_delay */
373         RemoveBits( &p_vpar->bit_stream, 22 );
374         /* frame_rate_extension_n */
375         i_dummy = GetBits( &p_vpar->bit_stream, 2 );
376         /* frame_rate_extension_d */
377         p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
378                                   / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
379     }
380     else
381     {
382         /* It's an MPEG-1 stream. Put adequate parameters. */
383
384         p_vpar->sequence.b_mpeg2 = 0;
385         p_vpar->sequence.b_progressive = 1;
386         p_vpar->sequence.i_chroma_format = CHROMA_420;
387     }
388
389     /* Update sizes */
390     p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
391     p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
392                                    (p_vpar->sequence.i_height + 15) / 16 :
393                                    2 * ((p_vpar->sequence.i_height + 31) / 32);
394     p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
395                                         * p_vpar->sequence.i_mb_height;
396     p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
397     p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
398     p_vpar->sequence.i_size = p_vpar->sequence.i_width
399                                         * p_vpar->sequence.i_height;
400
401 #if 0
402     if(    p_vpar->sequence.i_width != i_width_save
403         || p_vpar->sequence.i_height != i_height_save )
404     {
405          /* FIXME: Warn the video output */
406     }
407 #endif
408
409     /* Extension and User data */
410     ExtensionAndUserData( p_vpar );
411
412     /* XXX: The vout request and fifo opening will eventually be here */
413
414     /* Spawn a video output if there is none */
415     vlc_mutex_lock( &p_vout_bank->lock );
416     
417     if( p_vout_bank->i_count == 0 )
418     {
419         intf_WarnMsg( 1, "vpar: no vout present, spawning one" );
420
421         p_vpar->p_vout = vout_CreateThread( NULL, p_vpar->sequence.i_width,
422                                             p_vpar->sequence.i_height );
423
424         /* Everything failed */
425         if( p_vpar->p_vout == NULL )
426         {
427             intf_ErrMsg( "vpar error: can't open vout, aborting" );
428             vlc_mutex_unlock( &p_vout_bank->lock );
429
430             /* XXX ! XXX ! XXX ! what to do here ? */
431             return;
432         }
433         
434         p_vout_bank->pp_vout[ p_vout_bank->i_count ] = p_vpar->p_vout;
435         p_vout_bank->i_count++;
436     }
437     else
438     {
439         /* Take the first video output FIXME: take the best one */
440         p_vpar->p_vout = p_vout_bank->pp_vout[ 0 ];
441     }
442
443     vlc_mutex_unlock( &p_vout_bank->lock );
444 }
445
446 /*****************************************************************************
447  * GroupHeader : Parse the next group of pictures header
448  *****************************************************************************/
449 static void GroupHeader( vpar_thread_t * p_vpar )
450 {
451     /* Nothing to do, we don't care. */
452     RemoveBits( &p_vpar->bit_stream, 27 );
453     ExtensionAndUserData( p_vpar );
454 }
455
456 /*****************************************************************************
457  * PictureHeader : Parse the next picture header
458  *****************************************************************************/
459 static void PictureHeader( vpar_thread_t * p_vpar )
460 {
461     int                 i_structure, i_previous_coding_type;
462     boolean_t           b_parsable = 0;
463
464     /* Recover in case of stream discontinuity. */
465     if( p_vpar->sequence.b_expect_discontinuity )
466     {
467         ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
468         ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
469         if( p_vpar->picture.p_picture != NULL )
470         {
471             vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
472             p_vpar->picture.p_picture = NULL;
473         }
474         p_vpar->picture.i_current_structure = 0;
475         p_vpar->sequence.b_expect_discontinuity = 0;
476     }
477
478     /* Parse the picture header. */
479     RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
480     i_previous_coding_type = p_vpar->picture.i_coding_type;
481     p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
482     RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
483
484     if( p_vpar->picture.i_coding_type == P_CODING_TYPE
485         || p_vpar->picture.i_coding_type == B_CODING_TYPE )
486     {
487         p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 1 );
488         p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 3 )
489                                             - 1;
490     }
491     if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
492     {
493         p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 1 );
494         p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 3 )
495                                             - 1;
496     }
497
498     /* extra_information_picture */
499     while( GetBits( &p_vpar->bit_stream, 1 ) )
500     {
501         RemoveBits( &p_vpar->bit_stream, 8 );
502     }
503
504     /*
505      * Picture Coding Extension
506      */
507     NextStartCode( &p_vpar->bit_stream );
508     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
509     {
510         /* Parse picture_coding_extension */
511         RemoveBits32( &p_vpar->bit_stream );
512         /* extension_start_code_identifier */
513         RemoveBits( &p_vpar->bit_stream, 4 );
514
515         /* Pre-substract 1 for later use in MotionDelta(). */
516         p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
517         p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
518         p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
519         p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
520         p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
521         i_structure = GetBits( &p_vpar->bit_stream, 2 );
522         p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
523         p_vpar->picture.b_frame_pred_frame_dct
524              = GetBits( &p_vpar->bit_stream, 1 );
525         p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
526         p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
527         p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
528         /* Alternate scan */
529         p_vpar->picture.pi_scan =
530             p_vpar->ppi_scan[ GetBits( &p_vpar->bit_stream, 1 ) ];
531         p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
532         /* chroma_420_type (obsolete) */
533         RemoveBits( &p_vpar->bit_stream, 1 );
534         p_vpar->picture.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
535
536         /* composite_display_flag */
537         if( GetBits( &p_vpar->bit_stream, 1 ) )
538         {
539             /* v_axis, field_sequence, sub_carrier, burst_amplitude,
540              * sub_carrier_phase */
541             RemoveBits( &p_vpar->bit_stream, 20 );
542         }
543     }
544     else
545     {
546         /* MPEG-1 compatibility flags */
547         p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
548         i_structure = FRAME_STRUCTURE;
549         p_vpar->picture.b_top_field_first = 0;
550         p_vpar->picture.b_frame_pred_frame_dct = 1;
551         p_vpar->picture.b_concealment_mv = 0;
552         p_vpar->picture.b_q_scale_type = 0;
553         p_vpar->picture.b_intra_vlc_format = 0;
554         p_vpar->picture.pi_scan = p_vpar->ppi_scan[0];
555         p_vpar->picture.b_repeat_first_field = 0;
556         p_vpar->picture.b_progressive = 1;
557     }
558
559     /* Extension and User data. */
560     ExtensionAndUserData( p_vpar );
561
562     p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
563
564     if( p_vpar->picture.i_current_structure )
565     {
566         if ( (i_structure == FRAME_STRUCTURE ||
567               i_structure == p_vpar->picture.i_current_structure) )
568         {
569             /* We don't have the second field of the buffered frame. */
570             if( p_vpar->picture.p_picture != NULL )
571             {
572                 ReferenceReplace( p_vpar,
573                                   p_vpar->picture.i_coding_type,
574                                   NULL );
575                 vout_DestroyPicture( p_vpar->p_vout,
576                                      p_vpar->picture.p_picture );
577                 p_vpar->picture.p_picture = NULL;
578             }
579
580             p_vpar->picture.i_current_structure = 0;
581
582             intf_WarnMsg( 2, "Odd number of field pictures." );
583         }
584         else
585         {
586             /* Second field of a frame. We will decode it if, and only if we
587              * have decoded the first field. */
588             if( p_vpar->picture.p_picture == NULL )
589             {
590                 if( (p_vpar->picture.i_coding_type == I_CODING_TYPE
591                       && p_vpar->sequence.p_backward == NULL) )
592                 {
593                     /* Exceptionnally, parse the picture if it is I. We need
594                      * this in case of an odd number of field pictures, if the
595                      * previous picture is not intra, we desperately need a
596                      * new reference picture. OK, this is kind of kludgy. */
597                     p_vpar->picture.i_current_structure = 0;
598                 }
599                 else
600                 {
601                     b_parsable = 0;
602                 }
603             }
604             else
605             {
606                 /* We suppose we have the reference pictures, since we already
607                  * decoded the first field and the second field will not need
608                  * any extra reference picture. There is a special case of
609                  * P field being the second field of an I field, but ISO/IEC
610                  * 13818-2 section 7.6.3.5 specifies that this P field will
611                  * not need any reference picture besides the I field. So far
612                  * so good. */
613                 b_parsable = 1;
614
615                 if( p_vpar->picture.i_coding_type == P_CODING_TYPE &&
616                     i_previous_coding_type == I_CODING_TYPE &&
617                     p_vpar->sequence.p_forward == NULL )
618                 {
619                     /* This is the special case of section 7.6.3.5. Create
620                      * a fake reference picture (which will not be used)
621                      * but will prevent us from segfaulting in the slice
622                      * parsing. */
623                     static picture_t    fake_picture;
624                     fake_picture.p_data = NULL; /* We will use it later */
625                     p_vpar->sequence.p_forward = &fake_picture;
626                 }
627             }
628         }
629     }
630
631     if( !p_vpar->picture.i_current_structure )
632     {
633         /* First field of a frame, or new frame picture. */
634         int     i_repeat_field;
635
636         /* Do we have the reference pictures ? */
637         b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE ||
638                          p_vpar->picture.b_concealment_mv) &&
639                         (p_vpar->sequence.p_backward == NULL)) ||
640                          /* p_backward will become p_forward later */
641                        ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
642                         (p_vpar->sequence.p_forward == NULL ||
643                          p_vpar->sequence.p_backward == NULL)));
644
645         /* Compute the number of times the frame will be emitted by the
646          * decoder (number of half-periods). */
647         if( p_vpar->sequence.b_progressive )
648         {
649             i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
650                                 + p_vpar->picture.b_top_field_first) * 2;
651         }
652         else
653         {
654             if( p_vpar->picture.b_progressive )
655             {
656                 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
657             }
658             else
659             {
660                 i_repeat_field = 2;
661             }
662         }
663
664         /* Warn synchro we have a new picture (updates pictures index). */
665         vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
666                                 i_repeat_field );
667
668         if( b_parsable )
669         {
670             /* Does synchro say we have enough time to decode it ? */
671             b_parsable = vpar_SynchroChoose( p_vpar,
672                                p_vpar->picture.i_coding_type, i_structure );
673         }
674     }
675
676     if( !b_parsable )
677     {
678         /* Update the reference pointers. */
679         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
680
681         /* Update context. */
682         if( i_structure != FRAME_STRUCTURE )
683         {
684             if( (p_vpar->picture.i_current_structure | i_structure)
685                     == FRAME_STRUCTURE )
686             {
687                 /* The frame is complete. */
688                 p_vpar->picture.i_current_structure = 0;
689
690                 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
691             }
692             else
693             {
694                 p_vpar->picture.i_current_structure = i_structure;
695             }
696         }
697         else
698         {
699             /* Warn Synchro we have trashed a picture. */
700             vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
701         }
702         p_vpar->picture.p_picture = NULL;
703
704         return;
705     }
706
707     /* OK, now we are sure we will decode the picture. */
708     p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
709
710 #define P_picture p_vpar->picture.p_picture
711     p_vpar->picture.b_error = 0;
712     p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
713
714     if( !p_vpar->picture.i_current_structure )
715     {
716         /* This is a new frame. Get a structure from the video_output. */
717         while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
718                               /* XXX */ 99+p_vpar->sequence.i_chroma_format,
719                                         p_vpar->sequence.i_width,
720                                         p_vpar->sequence.i_height ) )
721              == NULL )
722         {
723             intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying");
724             if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
725             {
726                 return;
727             }
728             msleep( VPAR_OUTMEM_SLEEP );
729         }
730
731         /* Initialize values. */
732         vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
733         P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
734         P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
735         p_vpar->picture.i_field_width = ( p_vpar->sequence.i_width
736                     << ( 1 - p_vpar->picture.b_frame_structure ) );
737
738 /* FIXME ! remove asap ?? */
739 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
740
741         /* Update the reference pointers. */
742         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
743     }
744
745     /* Initialize picture data for decoding. */
746     p_vpar->picture.i_current_structure |= i_structure;
747     p_vpar->picture.i_structure = i_structure;
748     p_vpar->picture.b_second_field =
749         (i_structure != p_vpar->picture.i_current_structure);
750     p_vpar->picture.b_current_field =
751         (i_structure == BOTTOM_FIELD );
752
753     if( p_vpar->sequence.b_mpeg2 )
754     {
755         static f_picture_data_t ppf_picture_data[4][4] =
756         {
757             {
758                 NULL, NULL, NULL, NULL
759             },
760             {
761                 /* TOP_FIELD */
762 #if (VPAR_OPTIM_LEVEL > 1)
763                 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
764                 vpar_PictureData2BT
765 #else
766                 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
767                 vpar_PictureDataGENERIC
768 #endif
769             },
770             {
771                 /* BOTTOM_FIELD */
772 #if (VPAR_OPTIM_LEVEL > 1)
773                 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
774                 vpar_PictureData2BB
775 #else
776                 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
777                 vpar_PictureDataGENERIC
778 #endif
779             },
780             {
781                 /* FRAME_PICTURE */
782 #if (VPAR_OPTIM_LEVEL > 0)
783                 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
784                 vpar_PictureData2BF
785 #else
786                 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
787                 vpar_PictureDataGENERIC
788 #endif
789             }
790         };
791
792         ppf_picture_data[p_vpar->picture.i_structure]
793                         [p_vpar->picture.i_coding_type]( p_vpar );
794     }
795     else
796     {
797 #if (VPAR_OPTIM_LEVEL > 1)
798         static f_picture_data_t pf_picture_data[5] =
799         { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
800           vpar_PictureData1D };
801
802         pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar );
803 #else
804         vpar_PictureDataGENERIC( p_vpar );
805 #endif
806     }
807
808     /* Wait for all the macroblocks to be decoded. */
809     p_vpar->pool.pf_wait_pool( &p_vpar->pool );
810
811     /* Re-spawn decoder threads if the user changed settings. */
812     vpar_SpawnPool( p_vpar );
813
814     if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
815     {
816         return;
817     }
818
819     if( p_vpar->sequence.p_forward != NULL &&
820         p_vpar->sequence.p_forward->p_data == NULL )
821     {
822         /* This can only happen with the fake picture created for section
823          * 7.6.3.5. Clean up our mess. */
824         p_vpar->sequence.p_forward = NULL;
825     }
826
827     if( p_vpar->picture.b_error )
828     {
829         /* Trash picture. */
830         p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
831
832         vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
833                          p_vpar->picture.i_structure, 1 );
834         vout_DestroyPicture( p_vpar->p_vout, P_picture );
835
836         ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
837
838         /* Prepare context for the next picture. */
839         P_picture = NULL;
840         if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
841             p_vpar->picture.i_current_structure = 0;
842     }
843     else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
844     {
845         /* Frame completely parsed. */
846         vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
847                          p_vpar->picture.i_structure, 0 );
848         vout_DisplayPicture( p_vpar->p_vout, P_picture );
849
850         /* Prepare context for the next picture. */
851         P_picture = NULL;
852         p_vpar->picture.i_current_structure = 0;
853     }
854 #undef P_picture
855 }
856
857 /*****************************************************************************
858  * ExtensionAndUserData : Parse the extension_and_user_data structure
859  *****************************************************************************/
860 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
861 {
862     while( !p_vpar->p_fifo->b_die )
863     {
864         NextStartCode( &p_vpar->bit_stream );
865         switch( ShowBits( &p_vpar->bit_stream, 32 ) )
866         {
867         case EXTENSION_START_CODE:
868             RemoveBits32( &p_vpar->bit_stream );
869             switch( GetBits( &p_vpar->bit_stream, 4 ) )
870             {
871             case SEQUENCE_DISPLAY_EXTENSION_ID:
872                 SequenceDisplayExtension( p_vpar );
873                 break;
874             case QUANT_MATRIX_EXTENSION_ID:
875                 QuantMatrixExtension( p_vpar );
876                 break;
877             case SEQUENCE_SCALABLE_EXTENSION_ID:
878                 SequenceScalableExtension( p_vpar );
879                 break;
880             case PICTURE_DISPLAY_EXTENSION_ID:
881                 PictureDisplayExtension( p_vpar );
882                 break;
883             case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
884                 PictureSpatialScalableExtension( p_vpar );
885                 break;
886             case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
887                 PictureTemporalScalableExtension( p_vpar );
888                 break;
889             case COPYRIGHT_EXTENSION_ID:
890                 CopyrightExtension( p_vpar );
891                 break;
892             default:
893                 break;
894             }
895             break;
896
897         case USER_DATA_START_CODE:
898             RemoveBits32( &p_vpar->bit_stream );
899             /* Wait for the next start code */
900             break;
901
902         default:
903             return;
904         }
905     }
906 }
907
908
909 /*****************************************************************************
910  * SequenceDisplayExtension : Parse the sequence_display_extension structure *
911  *****************************************************************************/
912
913 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
914 {
915     /* We don't care sequence_display_extension. */
916     /* video_format */
917     RemoveBits( &p_vpar->bit_stream, 3 );
918     if( GetBits( &p_vpar->bit_stream, 1 ) )
919     {
920         /* Two bytes for color_desciption */
921         RemoveBits( &p_vpar->bit_stream, 16 );
922         p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
923     }
924     /* display_horizontal and vertical_size and a marker_bit */
925     RemoveBits( &p_vpar->bit_stream, 29 );
926 }
927
928
929 /*****************************************************************************
930  * QuantMatrixExtension : Load quantization matrices for luminance           *
931  *                        and chrominance                                    *
932  *****************************************************************************/
933
934 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
935 {
936     if( GetBits( &p_vpar->bit_stream, 1 ) )
937     {
938         /* Load intra_quantiser_matrix for luminance. */
939         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
940     }
941     else
942     {
943         /* Use the default matrix. */
944         LinkMatrix( &p_vpar->sequence.intra_quant,
945                     p_vpar->pi_default_intra_quant );
946     }
947     if( GetBits( &p_vpar->bit_stream, 1 ) )
948     {
949         /* Load non_intra_quantiser_matrix for luminance. */
950         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
951     }
952     else
953     {
954         /* Use the default matrix. */
955         LinkMatrix( &p_vpar->sequence.nonintra_quant,
956                     p_vpar->pi_default_nonintra_quant );
957     }
958     if( GetBits( &p_vpar->bit_stream, 1 ) )
959     {
960         /* Load intra_quantiser_matrix for chrominance. */
961         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
962     }
963     else
964     {
965         /* Link the chrominance intra matrix to the luminance one. */
966         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
967                     p_vpar->sequence.intra_quant.pi_matrix );
968     }
969     if( GetBits( &p_vpar->bit_stream, 1 ) )
970     {
971         /* Load non_intra_quantiser_matrix for chrominance. */
972         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
973     }
974     else
975     {
976         /* Link the chrominance intra matrix to the luminance one. */
977         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
978                     p_vpar->sequence.intra_quant.pi_matrix );
979     }
980     if( GetBits( &p_vpar->bit_stream, 1 ) )
981     {
982         /* Load non_intra_quantiser_matrix for chrominance. */
983         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
984     }
985     else
986     {
987         /* Link the chrominance nonintra matrix to the luminance one. */
988         LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
989                     p_vpar->sequence.nonintra_quant.pi_matrix );
990     }
991 }
992
993
994 /*****************************************************************************
995  * SequenceScalableExtension : Parse the sequence_scalable_extension         *
996  *                             structure to handle scalable coding           *
997  *****************************************************************************/
998
999 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1000 {
1001     /* We don't care about anything scalable except the scalable mode. */
1002     switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1003     /* The length of the structure depends on the value of the scalable_mode */
1004     {
1005         case 1:
1006             RemoveBits32( &p_vpar->bit_stream );
1007             RemoveBits( &p_vpar->bit_stream, 21 );
1008             break;
1009         case 2:
1010             RemoveBits( &p_vpar->bit_stream, 12 );
1011             break;
1012         default:
1013             RemoveBits( &p_vpar->bit_stream, 4 );
1014     }
1015
1016 }
1017 /*****************************************************************************
1018  * PictureDisplayExtension : Parse the picture_display_extension structure   *
1019  *****************************************************************************/
1020
1021 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1022 {
1023     /* Number of frame center offset */
1024     int i_nb, i_dummy;
1025     /* I am not sure it works but it should
1026         (fewer tests than shown in §6.3.12) */
1027     i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1028                                             p_vpar->picture.b_repeat_first_field +
1029                                             p_vpar->picture.b_top_field_first
1030                            : ( p_vpar->picture.b_frame_structure + 1 ) +
1031                              p_vpar->picture.b_repeat_first_field;
1032     for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1033     {
1034         RemoveBits( &p_vpar->bit_stream, 17 );
1035         RemoveBits( &p_vpar->bit_stream, 17 );
1036     }
1037 }
1038
1039
1040 /*****************************************************************************
1041  * PictureSpatialScalableExtension                                           *
1042  *****************************************************************************/
1043
1044 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1045 {
1046     /* That's scalable, so we trash it */
1047     RemoveBits32( &p_vpar->bit_stream );
1048     RemoveBits( &p_vpar->bit_stream, 16 );
1049 }
1050
1051
1052 /*****************************************************************************
1053  * PictureTemporalScalableExtension                                          *
1054  *****************************************************************************/
1055
1056 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1057 {
1058     /* Scalable again, trashed again */
1059     RemoveBits( &p_vpar->bit_stream, 23 );
1060 }
1061
1062
1063 /*****************************************************************************
1064  * CopyrightExtension : Keeps some legal informations                        *
1065  *****************************************************************************/
1066
1067 static void CopyrightExtension( vpar_thread_t * p_vpar )
1068 {
1069     u32     i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1070     p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1071         /* A flag that says whether the copyright information is significant */
1072     p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1073         /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1074     p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1075         /* Reserved bits */
1076     RemoveBits( &p_vpar->bit_stream, 8 );
1077         /* The copyright_number is split in three parts */
1078         /* first part */
1079     i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1080     RemoveBits( &p_vpar->bit_stream, 1 );
1081         /* second part */
1082     i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1083     RemoveBits( &p_vpar->bit_stream, 1 );
1084         /* third part and sum */
1085     p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1086                                       ( (u64)i_copyright_nb_2 << 22 ) |
1087                                       ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );
1088 }