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