]> git.sesse.net Git - vlc/blob - modules/packetizer/vc1.c
Remove stdlib.h
[vlc] / modules / packetizer / vc1.c
1 /*****************************************************************************
2  * vc1.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002, 2006 the VideoLAN team
5  * $Id: copy.c 18231 2006-12-03 17:02:02Z courmisch $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include <vlc/vlc.h>
30 #include <vlc_codec.h>
31 #include <vlc_block.h>
32
33 #include "vlc_bits.h"
34 #include "vlc_block_helper.h"
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
41
42 vlc_module_begin();
43     set_category( CAT_SOUT );
44     set_subcategory( SUBCAT_SOUT_PACKETIZER );
45     set_description( _("VC-1 packetizer") );
46     set_capability( "packetizer", 50 );
47     set_callbacks( Open, Close );
48 vlc_module_end();
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 struct decoder_sys_t
54 {
55     /*
56      * Input properties
57      */
58     block_bytestream_t bytestream;
59     int i_state;
60     int i_offset;
61     uint8_t p_startcode[3];
62
63     /* Current sequence header */
64     vlc_bool_t b_sequence_header;
65     struct
66     {
67         block_t *p_sh;
68         vlc_bool_t b_advanced_profile;
69         vlc_bool_t b_interlaced;
70         vlc_bool_t b_frame_interpolation;
71         vlc_bool_t b_range_reduction;
72         vlc_bool_t b_has_bframe;
73     } sh;
74     vlc_bool_t b_entry_point;
75     struct
76     {
77         block_t *p_ep;
78     } ep;
79
80     /* */
81     vlc_bool_t  b_frame;
82
83     /* Current frame being built */
84     block_t    *p_frame;
85     block_t    **pp_last;
86
87
88     mtime_t i_interpolated_dts;
89 };
90
91 enum
92 {
93     STATE_NOSYNC,
94     STATE_NEXT_SYNC
95 };
96
97 typedef enum
98 {
99     IDU_TYPE_SEQUENCE_HEADER = 0x0f,
100     IDU_TYPE_ENTRY_POINT = 0x0e,
101     IDU_TYPE_FRAME = 0x0D,
102     IDU_TYPE_FIELD = 0x0C,
103     IDU_TYPE_SLICE = 0x0B,
104     IDU_TYPE_END_OF_SEQUENCE = 0x0B,
105
106     IDU_TYPE_SEQUENCE_LEVEL_USER_DATA = 0x1F,
107     IDU_TYPE_ENTRY_POINT_USER_DATA = 0x1E,
108     IDU_TYPE_FRAME_USER_DATA = 0x1D,
109     IDU_TYPE_FIELD_USER_DATA = 0x1C,
110     IDU_TYPE_SLICE_USER_DATA = 0x1B,
111 } idu_type_t;
112
113 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block );
114
115 /*****************************************************************************
116  * Open: probe the packetizer and return score
117  *****************************************************************************
118  * Tries to launch a decoder and return score so that the interface is able
119  * to choose.
120  *****************************************************************************/
121 static int Open( vlc_object_t *p_this )
122 {
123     decoder_t     *p_dec = (decoder_t*)p_this;
124     decoder_sys_t *p_sys;
125
126     if( p_dec->fmt_in.i_codec !=  VLC_FOURCC( 'W', 'V', 'C', '1' ) )
127         return VLC_EGENERIC;
128
129     p_dec->pf_packetize = Packetize;
130
131     /* Create the output format */
132     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
133     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
134
135     p_sys->i_state = STATE_NOSYNC;
136     p_sys->bytestream = block_BytestreamInit( p_dec );
137     p_sys->p_startcode[0] = 0x00;
138     p_sys->p_startcode[1] = 0x00;
139     p_sys->p_startcode[2] = 0x01;
140     p_sys->i_offset = 0;
141
142     p_sys->b_sequence_header = VLC_FALSE;
143     p_sys->sh.p_sh = NULL;
144     p_sys->b_entry_point = VLC_FALSE;
145     p_sys->ep.p_ep = NULL;
146
147     p_sys->b_frame = VLC_FALSE;
148     p_sys->p_frame = NULL;
149     p_sys->pp_last = &p_sys->p_frame;
150
151     p_sys->i_interpolated_dts = -1;
152
153
154     if( p_dec->fmt_in.i_extra > 0 )
155     {
156         block_t *p_init = block_New( p_dec, p_dec->fmt_in.i_extra );
157         block_t *p_pic;
158
159         memcpy( p_init->p_buffer, p_dec->fmt_in.p_extra,
160                 p_dec->fmt_in.i_extra );
161
162         while( ( p_pic = Packetize( p_dec, &p_init ) ) )
163             block_Release( p_pic ); /* Should not happen (only sequence header) */
164     }
165
166     return VLC_SUCCESS;
167 }
168
169 /*****************************************************************************
170  * Close:
171  *****************************************************************************/
172 static void Close( vlc_object_t *p_this )
173 {
174     decoder_t     *p_dec = (decoder_t*)p_this;
175     decoder_sys_t *p_sys = p_dec->p_sys;
176
177     block_BytestreamRelease( &p_sys->bytestream );
178     if( p_sys->p_frame )
179         block_Release( p_sys->p_frame );
180     free( p_sys );
181 }
182
183 /*****************************************************************************
184  * Packetize: packetize an access unit
185  *****************************************************************************/
186 static block_t *ParseIDU( decoder_t *p_dec, block_t *p_frag );
187
188 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
189 {
190     decoder_sys_t *p_sys = p_dec->p_sys;
191     block_t       *p_pic;
192
193     if( pp_block == NULL || *pp_block == NULL )
194         return NULL;
195
196     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
197     {
198         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
199         {
200             p_sys->i_state = STATE_NOSYNC;
201             block_BytestreamFlush( &p_sys->bytestream );
202
203             if( p_sys->p_frame )
204                 block_ChainRelease( p_sys->p_frame );
205             p_sys->p_frame = NULL;
206             p_sys->pp_last = &p_sys->p_frame;
207             p_sys->b_frame = VLC_FALSE;
208         }
209 //        p_sys->i_interpolated_dts = 0;
210         block_Release( *pp_block );
211         return NULL;
212     }
213
214     block_BytestreamPush( &p_sys->bytestream, *pp_block );
215
216     for( ;; )
217     {
218         switch( p_sys->i_state )
219         {
220
221         case STATE_NOSYNC:
222             if( block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS )
223                 p_sys->i_state = STATE_NEXT_SYNC;
224
225             if( p_sys->i_offset )
226             {
227                 block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
228                 p_sys->i_offset = 0;
229                 block_BytestreamFlush( &p_sys->bytestream );
230             }
231
232             if( p_sys->i_state != STATE_NEXT_SYNC )
233                 return NULL; /* Need more data */
234
235             p_sys->i_offset = 4; /* To find next startcode */
236
237         case STATE_NEXT_SYNC:
238             /* TODO: If p_block == NULL, flush the buffer without checking the
239              * next sync word */
240
241             /* Find the next startcode */
242             if( block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS )
243                 return NULL; /* Need more data */
244
245             /* Get the new fragment and set the pts/dts */
246             p_pic = block_New( p_dec, p_sys->i_offset );
247             block_BytestreamFlush( &p_sys->bytestream );
248             p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
249             p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
250             p_pic->i_rate = p_sys->bytestream.p_block->i_rate;
251
252             block_GetBytes( &p_sys->bytestream, p_pic->p_buffer, p_pic->i_buffer );
253
254             p_sys->i_offset = 0;
255
256             /* Parse and return complete frame */
257             p_pic = ParseIDU( p_dec, p_pic );
258
259             /* Don't reuse the same timestamps several times */
260             if( p_sys->p_frame &&
261                 p_sys->p_frame->i_dts == p_sys->bytestream.p_block->i_dts &&
262                 p_sys->p_frame->i_pts == p_sys->bytestream.p_block->i_pts )
263             {
264                 p_sys->bytestream.p_block->i_pts = -1;
265                 p_sys->bytestream.p_block->i_dts = -1;
266             }
267
268             /* */
269             if( !p_pic )
270             {
271                 p_sys->i_state = STATE_NOSYNC;
272                 break;
273             }
274             /* */
275             if( p_sys->i_interpolated_dts < 0 )
276             {
277                 msg_Dbg( p_dec, "need a starting pts/dts" );
278                 p_sys->i_state = STATE_NOSYNC;
279                 block_Release( p_pic );
280                 break;
281             }
282
283             /* So p_block doesn't get re-added several times */
284             *pp_block = block_BytestreamPop( &p_sys->bytestream );
285
286             p_sys->i_state = STATE_NOSYNC;
287
288             return p_pic;
289         }
290     }
291 }
292
293 /* DecodeRIDU: decode the startcode emulation prevention (same than h264) */
294 static void DecodeRIDU( uint8_t *p_ret, int *pi_ret, uint8_t *src, int i_src )
295 {
296     uint8_t *end = &src[i_src];
297     uint8_t *dst_end = &p_ret[*pi_ret];
298     uint8_t *dst = p_ret;
299
300     while( src < end && dst < dst_end )
301     {
302         if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 &&
303             src[2] == 0x03 )
304         {
305             *dst++ = 0x00;
306             *dst++ = 0x00;
307
308             src += 3;
309             continue;
310         }
311         *dst++ = *src++;
312     }
313
314     *pi_ret = dst - p_ret;
315 }
316 /* BuildExtraData: gather sequence header and entry point */
317 static void BuildExtraData( decoder_t *p_dec )
318 {
319     decoder_sys_t *p_sys = p_dec->p_sys;
320     es_format_t *p_es = &p_dec->fmt_out;
321     int i_extra;
322     if( !p_sys->b_sequence_header || !p_sys->b_entry_point )
323         return;
324
325     i_extra = p_sys->sh.p_sh->i_buffer + p_sys->ep.p_ep->i_buffer;
326     if( p_es->i_extra != i_extra )
327     {
328         p_es->i_extra = i_extra;
329         p_es->p_extra = realloc( p_dec->fmt_out.p_extra, p_es->i_extra );
330     }
331     memcpy( p_es->p_extra,
332             p_sys->sh.p_sh->p_buffer, p_sys->sh.p_sh->i_buffer );
333     memcpy( (uint8_t*)p_es->p_extra + p_sys->sh.p_sh->i_buffer,
334             p_sys->ep.p_ep->p_buffer, p_sys->ep.p_ep->i_buffer );
335 }
336 /* ParseIDU: parse an Independant Decoding Unit */
337 static block_t *ParseIDU( decoder_t *p_dec, block_t *p_frag )
338 {
339     decoder_sys_t *p_sys = p_dec->p_sys;
340     block_t *p_pic;
341     const idu_type_t idu = p_frag->p_buffer[3];
342
343     if( !p_sys->b_sequence_header && idu != IDU_TYPE_SEQUENCE_HEADER )
344     {
345         msg_Warn( p_dec, "waiting for sequence header" );
346         block_Release( p_frag );
347         return NULL;
348     }
349     if( p_sys->b_sequence_header && !p_sys->b_entry_point && idu != IDU_TYPE_ENTRY_POINT )
350     {
351         msg_Warn( p_dec, "waiting for entry point" );
352         block_Release( p_frag );
353         return NULL;
354     }
355     /* TODO we do not gather ENTRY_POINT and SEQUENCE_DATA user data
356      * But It should not be a problem for decoder */
357
358     /* Do we have completed a frame */
359     p_pic = NULL;
360     if( p_sys->b_frame &&
361         idu != IDU_TYPE_FRAME_USER_DATA &&
362         idu != IDU_TYPE_FIELD && idu != IDU_TYPE_FIELD_USER_DATA &&
363         idu != IDU_TYPE_SLICE && idu != IDU_TYPE_SLICE_USER_DATA &&
364         idu != IDU_TYPE_END_OF_SEQUENCE )
365     {
366         /* */
367         p_pic = block_ChainGather( p_sys->p_frame );
368
369         /* We can interpolate dts/pts only if we have a frame rate */
370         if( p_dec->fmt_out.video.i_frame_rate != 0 && p_dec->fmt_out.video.i_frame_rate_base != 0 )
371         {
372             //msg_Dbg( p_dec, "-------------- XXX0 dts="I64Fd" pts="I64Fd" interpolated="I64Fd, p_pic->i_dts, p_pic->i_pts, p_sys->i_interpolated_dts );
373             if( p_pic->i_dts <= 0 )
374                 p_pic->i_dts = p_sys->i_interpolated_dts;
375
376             p_sys->i_interpolated_dts += I64C(1000000) * p_dec->fmt_out.video.i_frame_rate_base / p_dec->fmt_out.video.i_frame_rate;
377             if( p_pic->i_pts <= 0 )
378             {
379                 if( !p_sys->sh.b_has_bframe || (p_pic->i_flags & BLOCK_FLAG_TYPE_B ) )
380                     p_pic->i_pts = p_pic->i_dts;
381                 /* TODO compute pts for other case */
382             }
383         }
384         p_sys->i_interpolated_dts = p_pic->i_dts;
385
386         //msg_Dbg( p_dec, "-------------- dts="I64Fd" pts="I64Fd, p_pic->i_dts, p_pic->i_pts );
387
388         /* Reset context */
389         p_sys->p_frame = NULL;
390         p_sys->pp_last = &p_sys->p_frame;
391     }
392
393     /*  */
394     if( p_sys->p_frame )
395     {
396         block_t *p_frame = p_sys->p_frame;
397         if( p_frame->i_dts < 0 )
398             p_frame->i_dts = p_frag->i_dts;
399         if( p_frame->i_pts < 0 )
400             p_frame->i_pts = p_frag->i_pts;
401     }
402     block_ChainLastAppend( &p_sys->pp_last, p_frag );
403
404     /* Parse IDU */
405     if( idu == IDU_TYPE_SEQUENCE_HEADER )
406     {
407         es_format_t *p_es = &p_dec->fmt_out;
408         bs_t s;
409         int i_profile;
410         uint8_t ridu[32];
411         int     i_ridu = sizeof(ridu);
412
413         /* */
414         if( p_sys->sh.p_sh )
415             block_Release( p_sys->sh.p_sh );
416         p_sys->sh.p_sh = block_Duplicate( p_frag );
417
418         /* Extract the raw IDU */
419         DecodeRIDU( ridu, &i_ridu, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
420
421         /* Auto detect VC-1_SPMP_PESpacket_PayloadFormatHeader (SMPTE RP 227) for simple/main profile
422          * TODO find a test case and valid it */
423         if( i_ridu > 4 && (ridu[0]&0x80) == 0 ) /* for advanced profile, the first bit is 1 */
424         {
425             video_format_t *p_v = &p_dec->fmt_in.video;
426             const int i_potential_width  = GetWBE( &ridu[0] );
427             const int i_potential_height = GetWBE( &ridu[2] );
428
429             if( i_potential_width >= 2  && i_potential_width <= 8192 &&
430                 i_potential_height >= 2 && i_potential_height <= 8192 )
431             {
432                 if( ( p_v->i_width <= 0 && p_v->i_height <= 0  ) ||
433                     ( p_v->i_width  == i_potential_width &&  p_v->i_height == i_potential_height ) )
434                 {
435                     static const uint8_t startcode[4] = { 0x00, 0x00, 0x01, IDU_TYPE_SEQUENCE_HEADER };
436                     p_es->video.i_width  = i_potential_width;
437                     p_es->video.i_height = i_potential_height;
438
439                     /* Remove it */
440                     p_frag->p_buffer += 4;
441                     p_frag->i_buffer -= 4;
442                     memcpy( p_frag->p_buffer, startcode, sizeof(startcode) );
443                 }
444             }
445         }
446
447         /* Parse it */
448         bs_init( &s, ridu, i_ridu );
449         i_profile = bs_read( &s, 2 );
450         if( i_profile == 3 )
451         {
452             const int i_level = bs_read( &s, 3 );
453
454             /* Advanced profile */
455             p_sys->sh.b_advanced_profile = VLC_TRUE;
456             p_sys->sh.b_range_reduction = VLC_FALSE;
457             p_sys->sh.b_has_bframe = VLC_TRUE;
458
459             bs_skip( &s, 2+3+5+1 ); // chroma format + frame rate Q + bit rate Q + postprocflag
460
461             p_es->video.i_width  = 2*bs_read( &s, 12 )+2;
462             p_es->video.i_height = 2*bs_read( &s, 12 )+2;
463
464             if( !p_sys->b_sequence_header )
465                 msg_Dbg( p_dec, "found sequence header for advanced profile level L%d resolution %dx%d",
466                          i_level, p_es->video.i_width, p_es->video.i_height);
467
468             bs_skip( &s, 1 );// pulldown
469             p_sys->sh.b_interlaced = bs_read( &s, 1 );
470             bs_skip( &s, 1 );// frame counter
471             p_sys->sh.b_frame_interpolation = bs_read( &s, 1 );
472             bs_skip( &s, 1 );       // Reserved
473             bs_skip( &s, 1 );       // Psf
474
475             if( bs_read( &s, 1 ) )  /* Display extension */
476             {
477                 const int i_display_width  = bs_read( &s, 14 )+1;
478                 const int i_display_height = bs_read( &s, 14 )+1;
479
480                 p_es->video.i_aspect = VOUT_ASPECT_FACTOR * i_display_width / i_display_height;
481
482                 if( !p_sys->b_sequence_header )
483                     msg_Dbg( p_dec, "display size %dx%d", i_display_width, i_display_height );
484
485                 if( bs_read( &s, 1 ) )  /* Pixel aspect ratio (PAR/SAR) */
486                 {
487                     static const int p_ar[16][2] = {
488                         { 0, 0}, { 1, 1}, {12,11}, {10,11}, {16,11}, {40,33},
489                         {24,11}, {20,11}, {32,11}, {80,33}, {18,11}, {15,11},
490                         {64,33}, {160,99},{ 0, 0}, { 0, 0}
491                     };
492                     int i_ar = bs_read( &s, 4 );
493                     int i_ar_w, i_ar_h;
494
495                     if( i_ar == 15 )
496                     {
497                         i_ar_w = bs_read( &s, 8 );
498                         i_ar_h = bs_read( &s, 8 );
499                     }
500                     else
501                     {
502                         i_ar_w = p_ar[i_ar][0];
503                         i_ar_h = p_ar[i_ar][1];
504                     }
505                     vlc_ureduce( &i_ar_w, &i_ar_h, i_ar_w, i_ar_h, 0 );
506                     if( !p_sys->b_sequence_header )
507                         msg_Dbg( p_dec, "aspect ratio %d:%d", i_ar_w, i_ar_h );
508                 }
509             }
510             if( bs_read( &s, 1 ) )  /* Frame rate */
511             {
512                 int i_fps_num = 0;
513                 int i_fps_den = 0;
514                 if( bs_read( &s, 1 ) )
515                 {
516                     i_fps_num = bs_read( &s, 16 )+1;
517                     i_fps_den = 32;
518                 }
519                 else
520                 {
521                     const int i_nr = bs_read( &s, 8 );
522                     const int i_dn = bs_read( &s, 4 );
523
524                     switch( i_nr )
525                     {
526                     case 1: i_fps_num = 24000; break;
527                     case 2: i_fps_num = 25000; break;
528                     case 3: i_fps_num = 30000; break;
529                     case 4: i_fps_num = 50000; break;
530                     case 5: i_fps_num = 60000; break;
531                     case 6: i_fps_num = 48000; break;
532                     case 7: i_fps_num = 72000; break;
533                     }
534                     switch( i_dn )
535                     {
536                     case 1: i_fps_den = 1000; break;
537                     case 2: i_fps_den = 1001; break;
538                     }
539                 }
540                 if( i_fps_num != 0 && i_fps_den != 0 )
541                     vlc_ureduce( &p_es->video.i_frame_rate, &p_es->video.i_frame_rate_base, i_fps_num, i_fps_den, 0 );
542
543                 if( !p_sys->b_sequence_header )
544                     msg_Dbg( p_dec, "frame rate %d/%d", p_es->video.i_frame_rate, p_es->video.i_frame_rate_base );
545             }
546         }
547         else
548         {
549             /* Simple and main profile */
550             p_sys->sh.b_advanced_profile = VLC_FALSE;
551             p_sys->sh.b_interlaced = VLC_FALSE;
552
553             if( !p_sys->b_sequence_header )
554                 msg_Dbg( p_dec, "found sequence header for %s profile", i_profile == 0 ? "simple" : "main" );
555
556             bs_skip( &s, 2+3+5+1+1+     // reserved + frame rate Q + bit rate Q + loop filter + reserved
557                          1+1+1+1+2+     // multiresolution + reserved + fast uv mc + extended mv + dquant
558                          1+1+1+1 );     // variable size transform + reserved + overlap + sync marker
559             p_sys->sh.b_range_reduction = bs_read( &s, 1 );
560             if( bs_read( &s, 3 ) > 0 )
561                 p_sys->sh.b_has_bframe = VLC_TRUE;
562             else
563                 p_sys->sh.b_has_bframe = VLC_FALSE;
564             bs_skip( &s, 2 );           // quantizer
565
566             p_sys->sh.b_frame_interpolation = bs_read( &s, 1 );
567         }
568         p_sys->b_sequence_header = VLC_TRUE;
569         BuildExtraData( p_dec );
570     }
571     else if( idu == IDU_TYPE_ENTRY_POINT )
572     {
573         if( p_sys->ep.p_ep )
574             block_Release( p_sys->ep.p_ep );
575         p_sys->ep.p_ep = block_Duplicate( p_frag );
576
577         p_sys->b_entry_point = VLC_TRUE;
578         BuildExtraData( p_dec );
579     }
580     else if( idu == IDU_TYPE_FRAME )
581     {
582         bs_t s;
583         uint8_t ridu[8];
584         int     i_ridu = sizeof(ridu);
585
586         /* Extract the raw IDU */
587         DecodeRIDU( ridu, &i_ridu, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
588
589         /* Parse it + interpolate pts/dts if possible */
590         bs_init( &s, ridu, i_ridu );
591
592         if( p_sys->sh.b_advanced_profile )
593         {
594             int i_fcm = 0;
595
596             if( p_sys->sh.b_interlaced )
597             {
598                 if( bs_read( &s, 1 ) )
599                 {
600                     if( bs_read( &s, 1 ) )
601                         i_fcm = 1;  /* interlaced field */
602                     else
603                         i_fcm = 2;  /* interlaced frame */
604                 }
605             }
606
607             if( i_fcm == 1 ) /*interlaced field */
608             {
609                 /* XXX for mixed I/P we should check reference usage before marking them I (too much work) */
610                 switch( bs_read( &s, 3 ) )
611                 {
612                 case 0: /* II */
613                 case 1: /* IP */
614                 case 2: /* PI */
615                     p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
616                     p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
617                     p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
618                     break;
619                 case 3: /* PP */
620                     p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
621                     break;
622                 case 4: /* BB */
623                 case 5: /* BBi */
624                 case 6: /* BiB */
625                 case 7: /* BiBi */
626                     p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
627                     break;
628                 }
629             }
630             else
631             {
632                 if( !bs_read( &s, 1 ) )
633                     p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
634                 else if( !bs_read( &s, 1 ) )
635                     p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
636                 else if( !bs_read( &s, 1 ) )
637                     p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
638                 else if( !bs_read( &s, 1 ) )
639                     p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;   /* Bi */
640                 else
641                     p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;   /* P Skip */
642             }
643         }
644         else
645         {
646             if( p_sys->sh.b_frame_interpolation )
647                 bs_skip( &s, 1 );   // interpolate
648             bs_skip( &s, 2 );       // frame count
649             if( p_sys->sh.b_range_reduction )
650                 bs_skip( &s, 1 );   // range reduction
651
652             if( bs_read( &s, 1 ) )
653                 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
654             else if( !p_sys->sh.b_has_bframe || bs_read( &s, 1 ) )
655                 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
656             else
657                 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
658         }
659         p_sys->b_frame = VLC_TRUE;
660     }
661     return p_pic;
662 }
663