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