]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_headers.c
Corrections de quelques petits bugs et surtout nouvelle synchro qui ne semble
[vlc] / src / video_parser / vpar_headers.c
1 /*****************************************************************************
2  * vpar_headers.c : headers parsing
3  * (c)1999 VideoLAN
4  *****************************************************************************/
5
6 /*****************************************************************************
7  * Preamble
8  *****************************************************************************/
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <sys/uio.h>
15
16 #include "config.h"
17 #include "common.h"
18 #include "mtime.h"
19 #include "vlc_thread.h"
20
21 #include "intf_msg.h"
22 #include "debug.h"                    /* ?? temporaire, requis par netlist.h */
23
24 #include "input.h"
25 #include "input_netlist.h"
26 #include "decoder_fifo.h"
27 #include "video.h"
28 #include "video_output.h"
29
30 #include "vdec_idct.h"
31 #include "video_decoder.h"
32 #include "vdec_motion.h"
33
34 #include "vpar_blocks.h"
35 #include "vpar_headers.h"
36 #include "vpar_synchro.h"
37 #include "video_parser.h"
38 #include "video_fifo.h"
39
40 /*
41  * Function pointer
42  */
43 typedef void    (*f_picture_data_t)( vpar_thread_t*, int );
44
45 /*
46  * Local prototypes
47  */
48 static __inline__ void NextStartCode( vpar_thread_t * p_vpar );
49 static void SequenceHeader( vpar_thread_t * p_vpar );
50 static void GroupHeader( vpar_thread_t * p_vpar );
51 static void PictureHeader( vpar_thread_t * p_vpar );
52 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
53 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
54 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
55 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
56 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
57 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
58 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
59 static void CopyrightExtension( vpar_thread_t * p_vpar );
60
61 /*
62  * Standard variables
63  */
64
65 /*****************************************************************************
66  * pi_default_intra_quant : default quantization matrix
67  *****************************************************************************/
68 #ifndef VDEC_DFT
69 int pi_default_intra_quant[] =
70 {
71     8,  16, 19, 22, 26, 27, 29, 34,
72     16, 16, 22, 24, 27, 29, 34, 37,
73     19, 22, 26, 27, 29, 34, 34, 38,
74     22, 22, 26, 27, 29, 34, 37, 40,
75     22, 26, 27, 29, 32, 35, 40, 48,
76     26, 27, 29, 32, 35, 40, 48, 58,
77     26, 27, 29, 34, 38, 46, 56, 69,
78     27, 29, 35, 38, 46, 56, 69, 83
79 };
80 #else
81 int pi_default_intra_quant[] =
82 {
83     2048,   5681,   6355,   6623,   6656,   5431,   4018,   2401,
84     5681,   7880,   10207,  10021,  9587,   8091,   6534,   3625,
85     6355,   10207,  11363,  10619,  9700,   8935,   6155,   3507,
86     6623,   9186,   10226,  9557,   8730,   8041,   6028,   3322,
87     5632,   9232,   9031,   8730,   8192,   7040,   5542,   3390,
88     5230,   7533,   7621,   7568,   7040,   6321,   5225,   3219,
89     3602,   5189,   5250,   5539,   5265,   5007,   4199,   2638,
90     1907,   2841,   3230,   3156,   3249,   3108,   2638,   1617
91 };
92 #endif
93
94 /*****************************************************************************
95  * pi_default_nonintra_quant : default quantization matrix
96  *****************************************************************************/
97 #ifndef VDEC_DFT
98 int pi_default_nonintra_quant[] =
99 {
100     16, 16, 16, 16, 16, 16, 16, 16,
101     16, 16, 16, 16, 16, 16, 16, 16,
102     16, 16, 16, 16, 16, 16, 16, 16,
103     16, 16, 16, 16, 16, 16, 16, 16,
104     16, 16, 16, 16, 16, 16, 16, 16,
105     16, 16, 16, 16, 16, 16, 16, 16,
106     16, 16, 16, 16, 16, 16, 16, 16,
107     16, 16, 16, 16, 16, 16, 16, 16
108 };
109 #else
110 int pi_default_nonintra_quanit[] =
111 {
112     4096,   5680,   5344,   4816,   4096,   3216,   2224,   1136,
113     5680,   7888,   7424,   6688,   5680,   4464,   3072,   1568,
114     5344,   7424,   6992,   6288,   5344,   4208,   2896,   1472,
115     4816,   6688,   6288,   5664,   4816,   3792,   2608,   1328,
116     4096,   5680,   5344,   4816,   4096,   3216,   2224,   1136,
117     3216,   4464,   4208,   3792,   3216,   2528,   1744,   880,
118     2224,   3072,   2896,   2608,   2224,   1744,   1200,   608,
119     1136,   1568,   1472,   1328,   1136,   880,    608,    304
120 };
121 #endif
122
123 /*****************************************************************************
124  * pi_scan : zig-zag and alternate scan patterns
125  *****************************************************************************/
126 u8 pi_scan[2][64] =
127 {
128     { /* Zig-Zag pattern */
129         0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
130         12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
131         35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
132         58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
133     },
134     { /* Alternate scan pattern */
135         0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
136         41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
137         51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
138         53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
139     }
140 };
141
142 /*
143  * Local inline functions.
144  */
145
146 /*****************************************************************************
147  * ReferenceUpdate : Update the reference pointers when we have a new picture
148  *****************************************************************************/
149 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
150                                         int i_coding_type,
151                                         picture_t * p_newref )
152 {
153     if( i_coding_type != B_CODING_TYPE )
154     {
155         if( p_vpar->sequence.p_forward != NULL )
156             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
157         if( p_vpar->sequence.p_backward != NULL )
158         {
159 #ifdef POLUX_SYNCHRO
160             vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
161                               vpar_SynchroDate( p_vpar ) );
162 #endif
163 #ifdef SAM_SYNCHRO
164             vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
165                               vpar_SynchroDate( p_vpar ) );
166 #endif
167 #ifdef MEUUH_SYNCHRO
168             mtime_t     date;
169             date = vpar_SynchroDate( p_vpar );
170             vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
171                               date );
172             if( p_vpar->synchro.i_coding_type == I_CODING_TYPE )
173                 vpar_SynchroKludge( p_vpar, date );
174 #endif
175         }
176         p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
177         p_vpar->sequence.p_backward = p_newref;
178         if( p_newref != NULL )
179             vout_LinkPicture( p_vpar->p_vout, p_newref );
180 #ifdef MEUUH_SYNCHRO
181         p_vpar->synchro.i_coding_type = i_coding_type;
182 #endif
183     }
184     else if( p_newref != NULL )
185     {
186         /* Put date immediately. */
187         vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
188     }
189 }
190
191 /*****************************************************************************
192  * ReferenceReplace : Replace the last reference pointer when we destroy
193  *                    a picture
194  *****************************************************************************/
195 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
196                                          int i_coding_type,
197                                          picture_t * p_newref )
198 {
199     if( i_coding_type != B_CODING_TYPE )
200     {
201         if( p_vpar->sequence.p_backward != NULL )
202             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
203         p_vpar->sequence.p_backward = p_newref;
204         if( p_newref != NULL )
205             vout_LinkPicture( p_vpar->p_vout, p_newref );
206     }
207 }
208
209 /*****************************************************************************
210  * LoadMatrix : Load a quantization matrix
211  *****************************************************************************/
212 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
213 {
214     int i_dummy;
215
216     if( !p_matrix->b_allocated )
217     {
218         /* Allocate a piece of memory to load the matrix. */
219         if( (p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) )) == NULL )
220         {
221             intf_ErrMsg("vpar error: allocation error in LoadMatrix()\n");
222             p_vpar->b_error = 1;
223             return;
224         }
225         p_matrix->b_allocated = 1;
226     }
227
228     for( i_dummy = 0; i_dummy < 64; i_dummy++ )
229     {
230         p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
231              = GetBits( &p_vpar->bit_stream, 8 );
232     }
233
234 #ifdef VDEC_DFT
235     /* Discrete Fourier Transform requires the quantization matrices to
236      * be normalized before using them. */
237     vdec_NormQuantMatrix( p_matrix->pi_matrix );
238 #endif
239 }
240
241 /*****************************************************************************
242  * LinkMatrix : Link a quantization matrix to another
243  *****************************************************************************/
244 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
245 {
246     if( p_matrix->b_allocated )
247     {
248         /* Deallocate the piece of memory. */
249         free( p_matrix->pi_matrix );
250         p_matrix->b_allocated = 0;
251     }
252
253     p_matrix->pi_matrix = pi_array;
254 }
255
256 /*
257  * Exported functions.
258  */
259
260 /*****************************************************************************
261  * vpar_NextSequenceHeader : Find the next sequence header
262  *****************************************************************************/
263 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
264 {
265     while( !p_vpar->b_die )
266     {
267         NextStartCode( p_vpar );
268         if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
269             return 0;
270         RemoveBits( &p_vpar->bit_stream, 8 );
271     }
272     return 1;
273 }
274
275 /*****************************************************************************
276  * vpar_ParseHeader : Parse the next header
277  *****************************************************************************/
278 int vpar_ParseHeader( vpar_thread_t * p_vpar )
279 {
280     while( !p_vpar->b_die )
281     {
282         NextStartCode( p_vpar );
283         switch( GetBits32( &p_vpar->bit_stream ) )
284         {
285         case SEQUENCE_HEADER_CODE:
286             SequenceHeader( p_vpar );
287             return 0;
288             break;
289
290         case GROUP_START_CODE:
291             GroupHeader( p_vpar );
292             return 0;
293             break;
294
295         case PICTURE_START_CODE:
296             PictureHeader( p_vpar );
297             return 0;
298             break;
299
300         case SEQUENCE_END_CODE:
301             intf_DbgMsg("vpar debug: sequence end code received\n");
302             return 1;
303             break;
304
305         default:
306         }
307     }
308
309     return 0;
310 }
311
312 /*
313  * Following functions are local
314  */
315
316 /*****************************************************************************
317  * SequenceHeader : Parse the next sequence header
318  *****************************************************************************/
319 static void SequenceHeader( vpar_thread_t * p_vpar )
320 {
321 #define RESERVED    -1
322     static float r_frame_rate_table[16] =
323     {
324         0.0,
325         ((23.0*1000.0)/1001.0),
326         24.0,
327         25.0,
328         ((30.0*1000.0)/1001.0),
329         30.0,
330         50.0,
331         ((60.0*1000.0)/1001.0),
332         60.0,
333         RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
334     };
335 #undef RESERVED
336
337     int i_height_save, i_width_save;
338
339     i_height_save = p_vpar->sequence.i_height;
340     i_width_save = p_vpar->sequence.i_width;
341
342     p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
343     p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
344     p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
345     p_vpar->sequence.r_frame_rate =
346             r_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
347
348     /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
349      * constrained_parameters_flag */
350     RemoveBits( &p_vpar->bit_stream, 30 );
351
352     /*
353      * Quantization matrices
354      */
355     if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
356     {
357         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
358     }
359     else
360     {
361         /* Use default matrix. */
362         LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
363     }
364
365     if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
366     {
367         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
368     }
369     else
370     {
371         /* Use default matrix. */
372         LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
373     }
374
375     /* Unless later overwritten by a matrix extension, we have the same
376      * matrices for luminance and chrominance. */
377     LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
378                 p_vpar->sequence.intra_quant.pi_matrix );
379     LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
380                 p_vpar->sequence.nonintra_quant.pi_matrix );
381
382     /*
383      * Sequence Extension
384      */
385     NextStartCode( p_vpar );
386     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
387     {
388         int                         i_dummy;
389
390         /* Turn the MPEG2 flag on */
391         p_vpar->sequence.b_mpeg2 = 1;
392
393         /* Parse sequence_extension */
394         RemoveBits32( &p_vpar->bit_stream );
395         /* extension_start_code_identifier, profile_and_level_indication */
396         RemoveBits( &p_vpar->bit_stream, 12 );
397         p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
398         p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
399         p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
400         p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
401         /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
402         RemoveBits( &p_vpar->bit_stream, 22 );
403         /* frame_rate_extension_n */
404         i_dummy = GetBits( &p_vpar->bit_stream, 2 );
405         /* frame_rate_extension_d */
406         p_vpar->sequence.r_frame_rate *= (i_dummy + 1)
407                                   / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
408     }
409     else
410     {
411         /* It's an MPEG-1 stream. Put adequate parameters. */
412
413         p_vpar->sequence.b_mpeg2 = 0;
414         p_vpar->sequence.b_progressive = 1;
415         p_vpar->sequence.i_chroma_format = CHROMA_420;
416     }
417
418     /* Update sizes */
419     p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
420     p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
421                                    (p_vpar->sequence.i_height + 15) / 16 :
422                                    2 * ((p_vpar->sequence.i_height + 31) / 32);
423     p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
424                                         * p_vpar->sequence.i_mb_height;
425     p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
426     p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
427     p_vpar->sequence.i_size = p_vpar->sequence.i_width
428                                         * p_vpar->sequence.i_height;
429
430     /* Update chromatic information. */
431     switch( p_vpar->sequence.i_chroma_format )
432     {
433     case CHROMA_420:
434         p_vpar->sequence.i_chroma_nb_blocks = 2;
435         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
436         p_vpar->sequence.i_chroma_mb_width = 8;
437         p_vpar->sequence.i_chroma_mb_height = 8;
438         break;
439
440     case CHROMA_422:
441         p_vpar->sequence.i_chroma_nb_blocks = 4;
442         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
443         p_vpar->sequence.i_chroma_mb_width = 8;
444         p_vpar->sequence.i_chroma_mb_height = 16;
445         break;
446
447     case CHROMA_444:
448         p_vpar->sequence.i_chroma_nb_blocks = 8;
449         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
450         p_vpar->sequence.i_chroma_mb_width = 16;
451         p_vpar->sequence.i_chroma_mb_height = 16;
452     }
453
454     /* Reset scalable_mode. */
455     p_vpar->sequence.i_scalable_mode = SC_NONE;
456
457 #if 0
458     if(    p_vpar->sequence.i_width != i_width_save
459         || p_vpar->sequence.i_height != i_height_save )
460     {
461          /* What do we do in case of a size change ??? */
462     }
463 #endif
464
465     /* Extension and User data */
466     ExtensionAndUserData( p_vpar );
467 }
468
469 /*****************************************************************************
470  * GroupHeader : Parse the next group of pictures header
471  *****************************************************************************/
472 static void GroupHeader( vpar_thread_t * p_vpar )
473 {
474     /* Nothing to do, we don't care. */
475     RemoveBits( &p_vpar->bit_stream, 27 );
476     ExtensionAndUserData( p_vpar );
477 }
478
479 /*****************************************************************************
480  * PictureHeader : Parse the next picture header
481  *****************************************************************************/
482 static void PictureHeader( vpar_thread_t * p_vpar )
483 {
484     /* Table of optimized PictureData functions. */
485     static f_picture_data_t ppf_picture_data[4][4] =
486     {
487         {
488             NULL, NULL, NULL, NULL
489         },
490         {
491             /* TOP_FIELD */
492 #if (VPAR_OPTIM_LEVEL > 1)
493             NULL, vpar_PictureData2I420TZ, vpar_PictureData2P420TZ,
494             vpar_PictureData2B420TZ
495 #else
496             NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
497             vpar_PictureDataGENERIC
498 #endif
499         },
500         {
501             /* BOTTOM_FIELD */
502 #if (VPAR_OPTIM_LEVEL > 1)
503             NULL, vpar_PictureData2I420BZ, vpar_PictureData2P420BZ,
504             vpar_PictureData2B420BZ
505 #else
506             NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
507             vpar_PictureDataGENERIC
508 #endif
509         },
510         {
511             /* FRAME_PICTURE */
512 #if (VPAR_OPTIM_LEVEL > 0)
513             NULL, vpar_PictureData2I420F0, vpar_PictureData2P420F0,
514             vpar_PictureData2B420F0
515 #else
516             NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
517             vpar_PictureDataGENERIC
518 #endif
519         }
520     };
521
522     int                 i_structure;
523     int                 i_mb_base;
524     boolean_t           b_parsable;
525 #ifdef VDEC_SMP
526     int                 i_mb;
527 #endif
528
529     RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
530     p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
531     RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
532     
533     if( p_vpar->picture.i_coding_type == P_CODING_TYPE
534         || p_vpar->picture.i_coding_type == B_CODING_TYPE )
535     {
536         p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
537         p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
538     }
539     if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
540     {
541         p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
542         p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
543     }
544
545     /* extra_information_picture */
546     while( GetBits( &p_vpar->bit_stream, 1 ) )
547     {
548         RemoveBits( &p_vpar->bit_stream, 8 );
549     }
550
551     /*
552      * Picture Coding Extension
553      */
554     NextStartCode( p_vpar );
555     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
556     {
557         /* Parse picture_coding_extension */
558         RemoveBits32( &p_vpar->bit_stream );
559         /* extension_start_code_identifier */
560         RemoveBits( &p_vpar->bit_stream, 4 );
561
562         p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
563         p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
564         p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
565         p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
566         p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
567         i_structure = GetBits( &p_vpar->bit_stream, 2 );
568         p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
569         p_vpar->picture.b_frame_pred_frame_dct
570              = GetBits( &p_vpar->bit_stream, 1 );
571         p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
572         p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
573         p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
574         p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
575         p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
576         /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
577          * the length of the picture_display_extension structure.
578          * chroma_420_type (obsolete) */
579         RemoveBits( &p_vpar->bit_stream, 1 );
580         p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
581
582         /* composite_display_flag */
583         if( GetBits( &p_vpar->bit_stream, 1 ) )
584         {
585             /* v_axis, field_sequence, sub_carrier, burst_amplitude,
586              * sub_carrier_phase */
587             RemoveBits( &p_vpar->bit_stream, 20 );
588         }
589     }
590     else
591     {
592         /* MPEG-1 compatibility flags */
593         p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
594         i_structure = FRAME_STRUCTURE;
595         p_vpar->picture.b_frame_pred_frame_dct = 1;
596         p_vpar->picture.b_concealment_mv = 0;
597         p_vpar->picture.b_q_scale_type = 0;
598         p_vpar->picture.b_intra_vlc_format = 0;
599         p_vpar->picture.b_alternate_scan = 0; /* zigzag */
600         p_vpar->picture.b_repeat_first_field = 0;
601         p_vpar->picture.b_progressive_frame = 1;
602     }
603
604     if( p_vpar->picture.i_current_structure &&
605         (i_structure == FRAME_STRUCTURE ||
606          i_structure == p_vpar->picture.i_current_structure) )
607     {
608         /* We don't have the second field of the buffered frame. */
609         if( p_vpar->picture.p_picture != NULL )
610         {
611             ReferenceReplace( p_vpar,
612                       p_vpar->picture.i_coding_type,
613                       NULL );
614
615 #ifdef VDEC_SMP
616             for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
617             {
618                 vpar_DestroyMacroblock( &p_vpar->vfifo,
619                                         p_vpar->picture.pp_mb[i_mb] );
620             }
621 #endif
622             vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
623         }
624
625         p_vpar->picture.i_current_structure = 0;
626
627         intf_DbgMsg("vpar debug: odd number of field picture.\n");
628     }
629
630     /* Do we have the reference pictures ? */
631     b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
632                     (p_vpar->sequence.p_backward == NULL)) ||
633                      /* p_backward will become p_forward later */
634                    ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
635                     (p_vpar->sequence.p_forward == NULL ||
636                      p_vpar->sequence.p_backward == NULL)));
637
638     if( b_parsable )
639     {
640         if( p_vpar->picture.i_current_structure )
641         {
642             /* Second field of a frame. We will decode it if, and only if we
643             * have decoded the first field. */
644             b_parsable = (p_vpar->picture.p_picture != NULL);
645         }
646         else
647         {
648             /* Does synchro say we have enough time to decode it ? */
649             b_parsable = vpar_SynchroChoose( p_vpar,
650                                p_vpar->picture.i_coding_type, i_structure );
651         }
652     }
653 #ifdef POLUX_SYNCHRO
654     else if( !p_vpar->picture.i_current_structure )
655     {
656         vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
657     }
658 #endif
659     
660     if( !b_parsable )
661     {
662         /* Update the reference pointers. */
663         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
664 #ifndef POLUX_SYNCHRO
665         /* Warn Synchro we have trashed a picture. */
666         vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
667 #endif
668         /* Update context. */
669         if( i_structure != FRAME_STRUCTURE )
670             p_vpar->picture.i_current_structure = i_structure;
671         p_vpar->picture.p_picture = NULL;
672
673         return;
674     }
675
676     /* OK, now we are sure we will decode the picture. */
677 #define P_picture p_vpar->picture.p_picture
678     p_vpar->picture.b_error = 0;
679     p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
680
681     if( !p_vpar->picture.i_current_structure )
682     {
683         /* This is a new frame. Get a structure from the video_output. */
684         while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
685                                         99+p_vpar->sequence.i_chroma_format, /*???*/
686                                         p_vpar->sequence.i_width,
687                                         p_vpar->sequence.i_height ) )
688              == NULL )
689         {
690             intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying\n");
691             if( p_vpar->b_die || p_vpar->b_error )
692             {
693                 return;
694             }
695             msleep( VPAR_OUTMEM_SLEEP );
696         }
697
698         /* Initialize values. */
699         vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
700         P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
701         P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
702         p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
703                     << ( 1 - p_vpar->picture.b_frame_structure ) );
704         p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
705                     << ( 1 - p_vpar->picture.b_frame_structure ));
706
707         P_picture->i_deccount = p_vpar->sequence.i_mb_size;
708         vlc_mutex_init( &p_vpar->picture.p_picture->lock_deccount );
709 #ifdef VDEC_SMP
710         memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
711 #endif
712 /* FIXME ! remove asap */
713 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
714
715         /* Update the reference pointers. */
716         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
717
718 #ifdef VDEC_SMP
719         /* Link referenced pictures for the decoder
720          * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
721         if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
722             p_vpar->picture.i_coding_type == B_CODING_TYPE )
723         {
724             vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
725         }
726         if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
727         {
728             vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
729         }
730 #endif
731     }
732     p_vpar->picture.i_current_structure |= i_structure;
733     p_vpar->picture.i_structure = i_structure;
734
735     /* Initialize picture data for decoding. */
736     if( i_structure == BOTTOM_FIELD )
737     {
738         i_mb_base = p_vpar->sequence.i_mb_size >> 1;
739         p_vpar->mb.i_l_y = 1;
740         p_vpar->mb.i_c_y = 1;
741     }
742     else
743     {
744         i_mb_base = 0;
745         p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
746     }
747     p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
748
749     /* Extension and User data. */
750     ExtensionAndUserData( p_vpar );
751
752     /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
753     if( p_vpar->sequence.i_chroma_format != CHROMA_420
754         || !p_vpar->sequence.b_mpeg2 || p_vpar->sequence.i_height > 2800
755         || p_vpar->sequence.i_scalable_mode == SC_DP )
756     {
757         /* Weird stream. Use the slower generic function. */
758         vpar_PictureDataGENERIC( p_vpar, i_mb_base );
759     }
760     else
761     {
762         /* Try to find an optimized function. */
763         ppf_picture_data[p_vpar->picture.i_structure]
764                         [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
765     }
766
767     if( p_vpar->b_die || p_vpar->b_error )
768     {
769         return;
770     }
771
772     if( p_vpar->picture.b_error )
773     {
774         /* Trash picture. */
775 //fprintf(stderr, "Image trashee\n");
776 #ifdef VDEC_SMP
777         for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
778         {
779             vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
780         }
781 #endif
782
783         if( P_picture->i_deccount != 1 )
784         {
785             vout_DestroyPicture( p_vpar->p_vout, P_picture );
786         }
787
788         ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
789
790         /* Prepare context for the next picture. */
791         P_picture = NULL;
792         if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
793             p_vpar->picture.i_current_structure = 0;
794     }
795     else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
796     {
797 //fprintf(stderr, "Image parsee (%d)\n", p_vpar->picture.i_coding_type);
798         /* Frame completely parsed. */
799 #ifdef VDEC_SMP
800         for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
801         {
802             vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
803         }
804
805         /* Send signal to the video_decoder. */
806         vlc_mutex_lock( &p_vpar->vfifo.lock );
807         vlc_cond_signal( &p_vpar->vfifo.wait );
808         vlc_mutex_unlock( &p_vpar->vfifo.lock );
809 #endif
810
811         /* Prepare context for the next picture. */
812         P_picture = NULL;
813         p_vpar->picture.i_current_structure = 0;
814     }
815 #undef P_picture
816 }
817
818 /*****************************************************************************
819  * ExtensionAndUserData : Parse the extension_and_user_data structure
820  *****************************************************************************/
821 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
822 {
823     while( !p_vpar->b_die )
824     {
825         NextStartCode( p_vpar );
826         switch( ShowBits( &p_vpar->bit_stream, 32 ) )
827         {
828         case EXTENSION_START_CODE:
829             RemoveBits32( &p_vpar->bit_stream );
830             switch( GetBits( &p_vpar->bit_stream, 4 ) )
831             {
832             case SEQUENCE_DISPLAY_EXTENSION_ID:
833                 SequenceDisplayExtension( p_vpar );
834                 break;
835             case QUANT_MATRIX_EXTENSION_ID:
836                 QuantMatrixExtension( p_vpar );
837                 break;
838             case SEQUENCE_SCALABLE_EXTENSION_ID:
839                 SequenceScalableExtension( p_vpar );
840                 break;
841             case PICTURE_DISPLAY_EXTENSION_ID:
842                 PictureDisplayExtension( p_vpar );
843                 break;
844             case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
845                 PictureSpatialScalableExtension( p_vpar );
846                 break;
847             case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
848                 PictureTemporalScalableExtension( p_vpar );
849                 break;
850             case COPYRIGHT_EXTENSION_ID:
851                 CopyrightExtension( p_vpar );
852                 break;
853             default:
854             }
855             break;
856
857         case USER_DATA_START_CODE:
858             RemoveBits32( &p_vpar->bit_stream );
859             /* Wait for the next start code */
860             break;
861
862         default:
863             return;
864         }
865     }
866 }
867
868
869 /*****************************************************************************
870  * SequenceDisplayExtension : Parse the sequence_display_extension structure *
871  *****************************************************************************/
872
873 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
874 {
875     /* We don't care sequence_display_extension. */
876     /* video_format */
877     RemoveBits( &p_vpar->bit_stream, 3 );
878     if( GetBits( &p_vpar->bit_stream, 1 ) )
879     {
880         /* Two bytes for color_desciption */
881         RemoveBits( &p_vpar->bit_stream, 16 );
882         p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
883     }
884     /* display_horizontal and vertical_size and a marker_bit */
885     RemoveBits( &p_vpar->bit_stream, 29 );
886 }
887
888
889 /*****************************************************************************
890  * QuantMatrixExtension : Load quantization matrices for luminance           *
891  *                        and chrominance                                    *
892  *****************************************************************************/
893
894 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
895 {
896     if( GetBits( &p_vpar->bit_stream, 1 ) )
897     {
898         /* Load intra_quantiser_matrix for luminance. */
899         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
900     }
901     else
902     {
903         /* Use the default matrix. */
904         LinkMatrix( &p_vpar->sequence.intra_quant,
905                     pi_default_intra_quant );
906     }
907     if( GetBits( &p_vpar->bit_stream, 1 ) )
908     {
909         /* Load non_intra_quantiser_matrix for luminance. */
910         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
911     }
912     else
913     {
914         /* Use the default matrix. */
915         LinkMatrix( &p_vpar->sequence.nonintra_quant,
916                     pi_default_nonintra_quant );
917     }
918     if( GetBits( &p_vpar->bit_stream, 1 ) )
919     {
920         /* Load intra_quantiser_matrix for chrominance. */
921         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
922     }
923     else
924     {
925         /* Link the chrominance intra matrix to the luminance one. */
926         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
927                     p_vpar->sequence.intra_quant.pi_matrix );
928     }
929     if( GetBits( &p_vpar->bit_stream, 1 ) )
930     {
931         /* Load non_intra_quantiser_matrix for chrominance. */
932         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
933     }
934     else
935     {
936         /* Link the chrominance intra matrix to the luminance one. */
937         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
938                     p_vpar->sequence.intra_quant.pi_matrix );
939     }
940     if( GetBits( &p_vpar->bit_stream, 1 ) )
941     {
942         /* Load non_intra_quantiser_matrix for chrominance. */
943         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
944     }
945     else
946     {
947         /* Link the chrominance nonintra matrix to the luminance one. */
948         LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
949                     p_vpar->sequence.nonintra_quant.pi_matrix );
950     }
951 }
952
953
954 /*****************************************************************************
955  * SequenceScalableExtension : Parse the sequence_scalable_extension         *
956  *                             structure to handle scalable coding           *
957  *****************************************************************************/
958
959 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
960 {
961     /* We don't care about anything scalable except the scalable mode. */
962     switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
963     /* The length of the structure depends on the value of the scalable_mode */
964     {
965         case 1:
966             RemoveBits32( &p_vpar->bit_stream );
967             RemoveBits( &p_vpar->bit_stream, 21 );
968             break;
969         case 2:
970             RemoveBits( &p_vpar->bit_stream, 12 );
971             break;
972         default:
973             RemoveBits( &p_vpar->bit_stream, 4 );
974     }
975
976 }
977 /*****************************************************************************
978  * PictureDisplayExtension : Parse the picture_display_extension structure   *
979  *****************************************************************************/
980
981 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
982 {
983     /* Number of frame center offset */
984     int i_nb, i_dummy;
985     /* I am not sure it works but it should
986         (fewer tests than shown in ยง6.3.12) */
987     i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
988                                             p_vpar->picture.b_repeat_first_field +
989                                             p_vpar->picture.b_top_field_first
990                            : ( p_vpar->picture.b_frame_structure + 1 ) +
991                              p_vpar->picture.b_repeat_first_field;
992     for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
993     {
994         RemoveBits( &p_vpar->bit_stream, 17 );
995         RemoveBits( &p_vpar->bit_stream, 17 );
996     }
997 }
998
999
1000 /*****************************************************************************
1001  * PictureSpatialScalableExtension                                           *
1002  *****************************************************************************/
1003
1004 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1005 {
1006     /* That's scalable, so we trash it */
1007     RemoveBits32( &p_vpar->bit_stream );
1008     RemoveBits( &p_vpar->bit_stream, 16 );
1009 }
1010
1011
1012 /*****************************************************************************
1013  * PictureTemporalScalableExtension                                          *
1014  *****************************************************************************/
1015
1016 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1017 {
1018     /* Scalable again, trashed again */
1019     RemoveBits( &p_vpar->bit_stream, 23 );
1020 }
1021
1022
1023 /*****************************************************************************
1024  * CopyrightExtension : Keeps some legal informations                        *
1025  *****************************************************************************/
1026
1027 static void CopyrightExtension( vpar_thread_t * p_vpar )
1028 {
1029     u32     i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1030     p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1031         /* A flag that says whether the copyright information is significant */
1032     p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1033         /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1034     p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1035         /* Reserved bits */
1036     RemoveBits( &p_vpar->bit_stream, 8 );
1037         /* The copyright_number is split in three parts */
1038         /* first part */
1039     i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1040     RemoveBits( &p_vpar->bit_stream, 1 );
1041         /* second part */
1042     i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1043     RemoveBits( &p_vpar->bit_stream, 1 );
1044         /* third part and sum */
1045     p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1046                                       ( (u64)i_copyright_nb_2 << 22 ) |
1047                                       ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );
1048 }