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