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