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