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