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