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