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