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