]> git.sesse.net Git - vlc/blob - modules/packetizer/mpegvideo.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / packetizer / mpegvideo.c
1 /*****************************************************************************
2  * mpegvideo.c: parse and packetize an MPEG1/2 video stream
3  *****************************************************************************
4  * Copyright (C) 2001-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Eric Petit <titer@videolan.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Problem with this implementation:
29  *
30  * Although we should time-stamp each picture with a PTS, this isn't possible
31  * with the current implementation.
32  * The problem comes from the fact that for non-low-delay streams we can't
33  * calculate the PTS of pictures used as backward reference. Even the temporal
34  * reference number doesn't help here because all the pictures don't
35  * necessarily have the same duration (eg. 3:2 pulldown).
36  *
37  * However this doesn't really matter as far as the MPEG muxers are concerned
38  * because they allow having empty PTS fields. --gibalou
39  *****************************************************************************/
40
41 /*****************************************************************************
42  * Preamble
43  *****************************************************************************/
44
45 #ifdef HAVE_CONFIG_H
46 # include "config.h"
47 #endif
48
49 #include <vlc_common.h>
50 #include <vlc_plugin.h>
51 #include <vlc_block.h>
52 #include <vlc_codec.h>
53 #include <vlc_block_helper.h>
54 #include "../codec/cc.h"
55 #include "packetizer_helper.h"
56
57 #define SYNC_INTRAFRAME_TEXT N_("Sync on Intra Frame")
58 #define SYNC_INTRAFRAME_LONGTEXT N_("Normally the packetizer would " \
59     "sync on the next full frame. This flags instructs the packetizer " \
60     "to sync on the first Intra Frame found.")
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 static int  Open ( vlc_object_t * );
66 static void Close( vlc_object_t * );
67
68 vlc_module_begin ()
69     set_category( CAT_SOUT )
70     set_subcategory( SUBCAT_SOUT_PACKETIZER )
71     set_description( N_("MPEG-I/II video packetizer") )
72     set_shortname( N_("MPEG Video") )
73     set_capability( "packetizer", 50 )
74     set_callbacks( Open, Close )
75
76     add_bool( "packetizer-mpegvideo-sync-iframe", false, NULL, SYNC_INTRAFRAME_TEXT,
77               SYNC_INTRAFRAME_LONGTEXT, true )
78 vlc_module_end ()
79
80 /*****************************************************************************
81  * Local prototypes
82  *****************************************************************************/
83 struct decoder_sys_t
84 {
85     /*
86      * Input properties
87      */
88     packetizer_t packetizer;
89
90     /* Sequence header and extension */
91     block_t *p_seq;
92     block_t *p_ext;
93
94     /* Current frame being built */
95     block_t    *p_frame;
96     block_t    **pp_last;
97
98     bool b_frame_slice;
99     mtime_t i_pts;
100     mtime_t i_dts;
101
102     /* Sequence properties */
103     int         i_frame_rate;
104     int         i_frame_rate_base;
105     bool  b_seq_progressive;
106     bool  b_low_delay;
107     int         i_aspect_ratio_info;
108     bool  b_inited;
109
110     /* Picture properties */
111     int i_temporal_ref;
112     int i_picture_type;
113     int i_picture_structure;
114     int i_top_field_first;
115     int i_repeat_first_field;
116     int i_progressive_frame;
117
118     mtime_t i_interpolated_dts;
119     mtime_t i_last_ref_pts;
120     bool b_second_field;
121
122     /* Number of pictures since last sequence header */
123     int i_seq_old;
124
125     /* Sync behaviour */
126     bool  b_sync_on_intra_frame;
127     bool  b_discontinuity;
128
129     /* */
130     bool b_cc_reset;
131     uint32_t i_cc_flags;
132     mtime_t i_cc_pts;
133     mtime_t i_cc_dts;
134     cc_data_t cc;
135 };
136
137 static block_t *Packetize( decoder_t *, block_t ** );
138 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] );
139
140 static void PacketizeReset( void *p_private, bool b_broken );
141 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t * );
142 static int PacketizeValidate( void *p_private, block_t * );
143
144 static block_t *ParseMPEGBlock( decoder_t *, block_t * );
145
146 static const uint8_t p_mp2v_startcode[3] = { 0x00, 0x00, 0x01 };
147
148 /*****************************************************************************
149  * Open:
150  *****************************************************************************/
151 static int Open( vlc_object_t *p_this )
152 {
153     decoder_t *p_dec = (decoder_t*)p_this;
154     decoder_sys_t *p_sys;
155
156     if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGV )
157         return VLC_EGENERIC;
158
159     es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_CODEC_MPGV );
160     p_dec->fmt_out.i_original_fourcc = p_dec->fmt_in.i_original_fourcc;
161
162     p_dec->pf_packetize = Packetize;
163     p_dec->pf_get_cc = GetCc;
164
165     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
166     if( !p_dec->p_sys )
167         return VLC_ENOMEM;
168     memset( p_dec->p_sys, 0, sizeof( decoder_sys_t ) );
169
170     /* Misc init */
171     packetizer_Init( &p_sys->packetizer,
172                      p_mp2v_startcode, sizeof(p_mp2v_startcode),
173                      NULL, 0, 4,
174                      PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
175
176     p_sys->p_seq = NULL;
177     p_sys->p_ext = NULL;
178     p_sys->p_frame = NULL;
179     p_sys->pp_last = &p_sys->p_frame;
180     p_sys->b_frame_slice = false;
181
182     p_sys->i_dts = p_sys->i_pts = VLC_TS_INVALID;
183
184     p_sys->i_frame_rate = 1;
185     p_sys->i_frame_rate_base = 1;
186     p_sys->b_seq_progressive = true;
187     p_sys->b_low_delay = true;
188     p_sys->i_seq_old = 0;
189
190     p_sys->i_temporal_ref = 0;
191     p_sys->i_picture_type = 0;
192     p_sys->i_picture_structure = 0x03; /* frame */
193     p_sys->i_top_field_first = 0;
194     p_sys->i_repeat_first_field = 0;
195     p_sys->i_progressive_frame = 0;
196     p_sys->b_inited = 0;
197
198     p_sys->i_interpolated_dts = VLC_TS_INVALID;
199     p_sys->i_last_ref_pts = VLC_TS_INVALID;
200     p_sys->b_second_field = 0;
201
202     p_sys->b_discontinuity = false;
203     p_sys->b_sync_on_intra_frame = var_CreateGetBool( p_dec, "packetizer-mpegvideo-sync-iframe" );
204     if( p_sys->b_sync_on_intra_frame )
205         msg_Dbg( p_dec, "syncing on intra frame now" );
206
207     p_sys->b_cc_reset = false;
208     p_sys->i_cc_pts = 0;
209     p_sys->i_cc_dts = 0;
210     p_sys->i_cc_flags = 0;
211     cc_Init( &p_sys->cc );
212
213     return VLC_SUCCESS;
214 }
215
216 /*****************************************************************************
217  * Close:
218  *****************************************************************************/
219 static void Close( vlc_object_t *p_this )
220 {
221     decoder_t     *p_dec = (decoder_t*)p_this;
222     decoder_sys_t *p_sys = p_dec->p_sys;
223
224     if( p_sys->p_seq )
225     {
226         block_Release( p_sys->p_seq );
227     }
228     if( p_sys->p_ext )
229     {
230         block_Release( p_sys->p_ext );
231     }
232     if( p_sys->p_frame )
233     {
234         block_ChainRelease( p_sys->p_frame );
235     }
236     packetizer_Clean( &p_sys->packetizer );
237
238     var_Destroy( p_dec, "packetizer-mpegvideo-sync-iframe" );
239
240     free( p_sys );
241 }
242
243 /*****************************************************************************
244  * Packetize:
245  *****************************************************************************/
246 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
247 {
248     decoder_sys_t *p_sys = p_dec->p_sys;
249
250     return packetizer_Packetize( &p_sys->packetizer, pp_block );
251 }
252
253 /*****************************************************************************
254  * GetCc:
255  *****************************************************************************/
256 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] )
257 {
258     decoder_sys_t *p_sys = p_dec->p_sys;
259     block_t *p_cc;
260     int i;
261
262     for( i = 0; i < 4; i++ )
263         pb_present[i] = p_sys->cc.pb_present[i];
264
265     if( p_sys->cc.i_data <= 0 )
266         return NULL;
267
268     p_cc = block_New( p_dec, p_sys->cc.i_data);
269     if( p_cc )
270     {
271         memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
272         p_cc->i_dts = 
273         p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
274         p_cc->i_flags = ( p_sys->cc.b_reorder  ? p_sys->i_cc_flags : BLOCK_FLAG_TYPE_P ) & ( BLOCK_FLAG_TYPE_I|BLOCK_FLAG_TYPE_P|BLOCK_FLAG_TYPE_B);
275     }
276     cc_Flush( &p_sys->cc );
277     return p_cc;
278 }
279
280 /*****************************************************************************
281  * Helpers:
282  *****************************************************************************/
283 static void PacketizeReset( void *p_private, bool b_broken )
284 {
285     decoder_t *p_dec = p_private;
286     decoder_sys_t *p_sys = p_dec->p_sys;
287
288     if( b_broken )
289     {
290         p_sys->b_discontinuity = true;
291         if( p_sys->p_frame )
292             block_ChainRelease( p_sys->p_frame );
293         p_sys->p_frame = NULL;
294         p_sys->pp_last = &p_sys->p_frame;
295         p_sys->b_frame_slice = false;
296     }
297     p_sys->i_dts =
298     p_sys->i_pts =
299     p_sys->i_interpolated_dts =
300     p_sys->i_last_ref_pts = VLC_TS_INVALID;
301 }
302
303 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block )
304 {
305     decoder_t *p_dec = p_private;
306
307     /* Check if we have a picture start code */
308     *pb_ts_used = p_block->p_buffer[3] == 0x00;
309
310     return ParseMPEGBlock( p_dec, p_block );
311 }
312
313
314 static int PacketizeValidate( void *p_private, block_t *p_au )
315 {
316     decoder_t *p_dec = p_private;
317     decoder_sys_t *p_sys = p_dec->p_sys;
318
319     /* If a discontinuity has been encountered, then wait till
320      * the next Intra frame before continuing with packetizing */
321     if( p_sys->b_discontinuity &&
322         p_sys->b_sync_on_intra_frame )
323     {
324         if( (p_au->i_flags & BLOCK_FLAG_TYPE_I) == 0 )
325         {
326             msg_Dbg( p_dec, "waiting on intra frame" );
327             return VLC_EGENERIC;
328         }
329         msg_Dbg( p_dec, "synced on intra frame" );
330         p_sys->b_discontinuity = false;
331         p_au->i_flags |= BLOCK_FLAG_DISCONTINUITY;
332     }
333
334     /* We've just started the stream, wait for the first PTS.
335      * We discard here so we can still get the sequence header. */
336     if( p_sys->i_dts <= VLC_TS_INVALID && p_sys->i_pts <= VLC_TS_INVALID &&
337         p_sys->i_interpolated_dts <= VLC_TS_INVALID )
338     {
339         msg_Dbg( p_dec, "need a starting pts/dts" );
340         return VLC_EGENERIC;
341     }
342
343     /* When starting the stream we can have the first frame with
344      * an invalid DTS (i_interpolated_pts is initialized to VLC_TS_INVALID) */
345     if( p_au->i_dts <= VLC_TS_INVALID )
346         p_au->i_dts = p_au->i_pts;
347
348     return VLC_SUCCESS;
349 }
350 /*****************************************************************************
351  * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
352  *****************************************************************************/
353 static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
354 {
355     decoder_sys_t *p_sys = p_dec->p_sys;
356     block_t *p_pic = NULL;
357
358     /*
359      * Check if previous picture is finished
360      */
361     if( ( p_sys->b_frame_slice &&
362           (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) &&
363           p_sys->p_seq == NULL )
364     {
365         /* We have a picture but without a sequence header we can't
366          * do anything */
367         msg_Dbg( p_dec, "waiting for sequence start" );
368         if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
369         p_sys->p_frame = NULL;
370         p_sys->pp_last = &p_sys->p_frame;
371         p_sys->b_frame_slice = false;
372
373     }
374     else if( p_sys->b_frame_slice &&
375              (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) )
376     {
377         const bool b_eos = p_frag->p_buffer[3] == 0xb7;
378
379         mtime_t i_duration;
380
381         if( b_eos )
382         {
383             block_ChainLastAppend( &p_sys->pp_last, p_frag );
384             p_frag = NULL;
385         }
386
387         p_pic = block_ChainGather( p_sys->p_frame );
388
389         if( b_eos )
390             p_pic->i_flags |= BLOCK_FLAG_END_OF_SEQUENCE;
391
392         i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base /
393                                 p_sys->i_frame_rate );
394
395         if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 )
396         {
397             i_duration /= 2;
398         }
399
400         if( p_sys->b_seq_progressive )
401         {
402             if( p_sys->i_top_field_first == 0 &&
403                 p_sys->i_repeat_first_field == 1 )
404             {
405                 i_duration *= 2;
406             }
407             else if( p_sys->i_top_field_first == 1 &&
408                      p_sys->i_repeat_first_field == 1 )
409             {
410                 i_duration *= 3;
411             }
412         }
413         else
414         {
415             if( p_sys->i_picture_structure == 0x03 )
416             {
417                 if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field )
418                 {
419                     i_duration += i_duration / 2;
420                 }
421             }
422         }
423
424         if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
425         {
426             /* Trivial case (DTS == PTS) */
427             /* Correct interpolated dts when we receive a new pts/dts */
428             if( p_sys->i_pts > VLC_TS_INVALID )
429                 p_sys->i_interpolated_dts = p_sys->i_pts;
430             if( p_sys->i_dts > VLC_TS_INVALID )
431                 p_sys->i_interpolated_dts = p_sys->i_dts;
432         }
433         else
434         {
435             /* Correct interpolated dts when we receive a new pts/dts */
436             if(p_sys->i_last_ref_pts > VLC_TS_INVALID && !p_sys->b_second_field)
437                 p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
438             if( p_sys->i_dts > VLC_TS_INVALID )
439                 p_sys->i_interpolated_dts = p_sys->i_dts;
440
441             if( !p_sys->b_second_field )
442                 p_sys->i_last_ref_pts = p_sys->i_pts;
443         }
444
445         p_pic->i_dts = p_sys->i_interpolated_dts;
446         p_sys->i_interpolated_dts += i_duration;
447
448         /* Set PTS only if we have a B frame or if it comes from the stream */
449         if( p_sys->i_pts > VLC_TS_INVALID )
450         {
451             p_pic->i_pts = p_sys->i_pts;
452         }
453         else if( p_sys->i_picture_type == 0x03 )
454         {
455             p_pic->i_pts = p_pic->i_dts;
456         }
457         else
458         {
459             p_pic->i_pts = VLC_TS_INVALID;
460         }
461
462         switch ( p_sys->i_picture_type )
463         {
464         case 0x01:
465             p_pic->i_flags |= BLOCK_FLAG_TYPE_I;
466             break;
467         case 0x02:
468             p_pic->i_flags |= BLOCK_FLAG_TYPE_P;
469             break;
470         case 0x03:
471             p_pic->i_flags |= BLOCK_FLAG_TYPE_B;
472             break;
473         }
474
475         p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts;
476
477 #if 0
478         msg_Dbg( p_dec, "pic: type=%d dts=%"PRId64" pts-dts=%"PRId64,
479         p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts);
480 #endif
481
482         /* Reset context */
483         p_sys->p_frame = NULL;
484         p_sys->pp_last = &p_sys->p_frame;
485         p_sys->b_frame_slice = false;
486
487         if( p_sys->i_picture_structure != 0x03 )
488         {
489             p_sys->b_second_field = !p_sys->b_second_field;
490         }
491         else
492         {
493             p_sys->b_second_field = 0;
494         }
495
496         /* CC */
497         p_sys->b_cc_reset = true;
498         p_sys->i_cc_pts = p_pic->i_pts;
499         p_sys->i_cc_dts = p_pic->i_dts;
500         p_sys->i_cc_flags = p_pic->i_flags;
501     }
502
503     if( !p_pic && p_sys->b_cc_reset )
504     {
505         p_sys->b_cc_reset = false;
506         cc_Flush( &p_sys->cc );
507     }
508
509     if( !p_frag )
510         return p_pic;
511     /*
512      * Check info of current fragment
513      */
514     if( p_frag->p_buffer[3] == 0xb8 )
515     {
516         /* Group start code */
517         if( p_sys->p_seq &&
518             p_sys->i_seq_old > p_sys->i_frame_rate/p_sys->i_frame_rate_base )
519         {
520             /* Useful for mpeg1: repeat sequence header every second */
521             block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_seq ) );
522             if( p_sys->p_ext )
523             {
524                 block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_ext ) );
525             }
526
527             p_sys->i_seq_old = 0;
528         }
529     }
530     else if( p_frag->p_buffer[3] == 0xb3 && p_frag->i_buffer >= 8 )
531     {
532         /* Sequence header code */
533         static const int code_to_frame_rate[16][2] =
534         {
535             { 1, 1 },  /* invalid */
536             { 24000, 1001 }, { 24, 1 }, { 25, 1 },       { 30000, 1001 },
537             { 30, 1 },       { 50, 1 }, { 60000, 1001 }, { 60, 1 },
538             /* Unofficial 15fps from Xing*/
539             { 15, 1001 },
540             /* Unofficial economy rates from libmpeg3 */
541             { 5, 1001 }, { 10, 1001 }, { 12, 1001 }, { 15, 1001 },
542             { 1, 1 },  { 1, 1 }  /* invalid */
543         };
544
545         if( p_sys->p_seq ) block_Release( p_sys->p_seq );
546         if( p_sys->p_ext ) block_Release( p_sys->p_ext );
547
548         p_sys->p_seq = block_Duplicate( p_frag );
549         p_sys->i_seq_old = 0;
550         p_sys->p_ext = NULL;
551
552         p_dec->fmt_out.video.i_width =
553             ( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 );
554         p_dec->fmt_out.video.i_height =
555             ( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6];
556         p_sys->i_aspect_ratio_info = p_frag->p_buffer[7] >> 4;
557
558         /* TODO: MPEG1 aspect ratio */
559
560         p_sys->i_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0];
561         p_sys->i_frame_rate_base =
562             code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1];
563
564         p_dec->fmt_out.video.i_frame_rate = p_sys->i_frame_rate;
565         p_dec->fmt_out.video.i_frame_rate_base = p_sys->i_frame_rate_base;
566
567         p_sys->b_seq_progressive = true;
568         p_sys->b_low_delay = true;
569
570         if ( !p_sys->b_inited )
571         {
572             msg_Dbg( p_dec, "size %dx%d fps=%.3f",
573                  p_dec->fmt_out.video.i_width, p_dec->fmt_out.video.i_height,
574                  p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base );
575             p_sys->b_inited = 1;
576         }
577     }
578     else if( p_frag->p_buffer[3] == 0xb5 )
579     {
580         int i_type = p_frag->p_buffer[4] >> 4;
581
582         /* Extension start code */
583         if( i_type == 0x01 )
584         {
585 #if 0
586             static const int mpeg2_aspect[16][2] =
587             {
588                 {0,1}, {1,1}, {4,3}, {16,9}, {221,100},
589                 {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1},
590                 {0,1}, {0,1}
591             };
592 #endif
593
594             /* sequence extension */
595             if( p_sys->p_ext) block_Release( p_sys->p_ext );
596             p_sys->p_ext = block_Duplicate( p_frag );
597
598             if( p_frag->i_buffer >= 10 )
599             {
600                 p_sys->b_seq_progressive =
601                     p_frag->p_buffer[5]&0x08 ? true : false;
602                 p_sys->b_low_delay =
603                     p_frag->p_buffer[9]&0x80 ? true : false;
604             }
605
606             /* Do not set aspect ratio : in case we're transcoding,
607              * transcode will take our fmt_out as a fmt_in to libmpeg2.
608              * libmpeg2.c will then believe that the user has requested
609              * a specific aspect ratio, which she hasn't. Thus in case
610              * of aspect ratio change, we're screwed. --Meuuh
611              */
612 #if 0
613             p_dec->fmt_out.video.i_sar_num =
614                 mpeg2_aspect[p_sys->i_aspect_ratio_info][0] *
615                 p_dec->fmt_out.video.i_height;
616             p_dec->fmt_out.video.i_sar_den =
617                 mpeg2_aspect[p_sys->i_aspect_ratio_info][1] *
618                 p_dec->fmt_out.video.i_width;
619 #endif
620
621         }
622         else if( i_type == 0x08 )
623         {
624             /* picture extension */
625             p_sys->i_picture_structure = p_frag->p_buffer[6]&0x03;
626             p_sys->i_top_field_first   = p_frag->p_buffer[7] >> 7;
627             p_sys->i_repeat_first_field= (p_frag->p_buffer[7]>>1)&0x01;
628             p_sys->i_progressive_frame = p_frag->p_buffer[8] >> 7;
629         }
630     }
631     else if( p_frag->p_buffer[3] == 0xb2 && p_frag->i_buffer > 4 )
632     {
633         cc_Extract( &p_sys->cc, p_sys->i_top_field_first,
634                     &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
635     }
636     else if( p_frag->p_buffer[3] == 0x00 )
637     {
638         /* Picture start code */
639         p_sys->i_seq_old++;
640
641         if( p_frag->i_buffer >= 6 )
642         {
643             p_sys->i_temporal_ref =
644                 ( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6);
645             p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 ) & 0x03;
646         }
647
648         p_sys->i_dts = p_frag->i_dts;
649         p_sys->i_pts = p_frag->i_pts;
650     }
651     else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf )
652     {
653         /* Slice start code */
654         p_sys->b_frame_slice = true;
655     }
656
657     /* Append the block */
658     block_ChainLastAppend( &p_sys->pp_last, p_frag );
659
660     return p_pic;
661 }