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