]> git.sesse.net Git - vlc/blob - modules/packetizer/mpegvideo.c
5ccf4c1ea7ce3d56cf20f4ad2de7fef6c53322fd
[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 #include <vlc/vlc.h>
46 #include <vlc_block.h>
47 #include <vlc_codec.h>
48 #include <vlc_block_helper.h>
49 #include "../codec/cc.h"
50
51 #define SYNC_INTRAFRAME_TEXT N_("Sync on Intra Frame")
52 #define SYNC_INTRAFRAME_LONGTEXT N_("Normally the packetizer would " \
53     "sync on the next full frame. This flags instructs the packetizer " \
54     "to sync on the first Intra Frame found.")
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 static int  Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
61
62 vlc_module_begin();
63     set_category( CAT_SOUT );
64     set_subcategory( SUBCAT_SOUT_PACKETIZER );
65     set_description( _("MPEG-I/II video packetizer") );
66     set_capability( "packetizer", 50 );
67     set_callbacks( Open, Close );
68
69     add_bool( "packetizer-mpegvideo-sync-iframe", 0, NULL, SYNC_INTRAFRAME_TEXT,
70               SYNC_INTRAFRAME_LONGTEXT, VLC_TRUE );
71         change_safe();
72 vlc_module_end();
73
74 /*****************************************************************************
75  * Local prototypes
76  *****************************************************************************/
77 static block_t *Packetize( decoder_t *, block_t ** );
78 static block_t *ParseMPEGBlock( decoder_t *, block_t * );
79 static block_t *GetCc( decoder_t *p_dec, vlc_bool_t pb_present[4] );
80
81 struct decoder_sys_t
82 {
83     /*
84      * Input properties
85      */
86     block_bytestream_t bytestream;
87     int i_state;
88     int i_offset;
89     uint8_t p_startcode[3];
90
91     /* Sequence header and extension */
92     block_t *p_seq;
93     block_t *p_ext;
94
95     /* Current frame being built */
96     block_t    *p_frame;
97     block_t    **pp_last;
98
99     vlc_bool_t b_frame_slice;
100     mtime_t i_pts;
101     mtime_t i_dts;
102
103     /* Sequence properties */
104     int         i_frame_rate;
105     int         i_frame_rate_base;
106     vlc_bool_t  b_seq_progressive;
107     vlc_bool_t  b_low_delay;
108     int         i_aspect_ratio_info;
109     vlc_bool_t  b_inited;
110
111     /* Picture properties */
112     int i_temporal_ref;
113     int i_picture_type;
114     int i_picture_structure;
115     int i_top_field_first;
116     int i_repeat_first_field;
117     int i_progressive_frame;
118
119     mtime_t i_interpolated_dts;
120     mtime_t i_last_ref_pts;
121     vlc_bool_t b_second_field;
122
123     /* Number of pictures since last sequence header */
124     int i_seq_old;
125
126     /* Sync behaviour */
127     vlc_bool_t  b_sync_on_intra_frame;
128     vlc_bool_t  b_discontinuity;
129
130     /* */
131     vlc_bool_t b_cc_reset;
132     uint32_t i_cc_flags;
133     mtime_t i_cc_pts;
134     mtime_t i_cc_dts;
135     cc_data_t cc;
136 };
137
138 enum {
139     STATE_NOSYNC,
140     STATE_NEXT_SYNC
141 };
142
143 /*****************************************************************************
144  * Open:
145  *****************************************************************************/
146 static int Open( vlc_object_t *p_this )
147 {
148     decoder_t *p_dec = (decoder_t*)p_this;
149     decoder_sys_t *p_sys;
150
151     if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '1' ) &&
152         p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '2' ) &&
153         p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
154     {
155         return VLC_EGENERIC;
156     }
157
158     es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC('m','p','g','v') );
159     p_dec->pf_packetize = Packetize;
160     p_dec->pf_get_cc = GetCc;
161
162     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
163
164     /* Misc init */
165     p_sys->i_state = STATE_NOSYNC;
166     p_sys->bytestream = block_BytestreamInit( p_dec );
167     p_sys->p_startcode[0] = 0;
168     p_sys->p_startcode[1] = 0;
169     p_sys->p_startcode[2] = 1;
170     p_sys->i_offset = 0;
171
172     p_sys->p_seq = NULL;
173     p_sys->p_ext = NULL;
174     p_sys->p_frame = NULL;
175     p_sys->pp_last = &p_sys->p_frame;
176     p_sys->b_frame_slice = VLC_FALSE;
177
178     p_sys->i_dts = p_sys->i_pts = 0;
179
180     p_sys->i_frame_rate = 1;
181     p_sys->i_frame_rate_base = 1;
182     p_sys->b_seq_progressive = VLC_TRUE;
183     p_sys->b_low_delay = VLC_TRUE;
184     p_sys->i_seq_old = 0;
185
186     p_sys->i_temporal_ref = 0;
187     p_sys->i_picture_type = 0;
188     p_sys->i_picture_structure = 0x03; /* frame */
189     p_sys->i_top_field_first = 0;
190     p_sys->i_repeat_first_field = 0;
191     p_sys->i_progressive_frame = 0;
192     p_sys->b_inited = 0;
193
194     p_sys->i_interpolated_dts = 0;
195     p_sys->i_last_ref_pts = 0;
196     p_sys->b_second_field = 0;
197
198     p_sys->b_discontinuity = VLC_FALSE;
199     p_sys->b_sync_on_intra_frame = var_CreateGetBool( p_dec, "packetizer-mpegvideo-sync-iframe" );
200     if( p_sys->b_sync_on_intra_frame )
201         msg_Dbg( p_dec, "syncing on intra frame now" );
202
203     p_sys->b_cc_reset = VLC_FALSE;
204     p_sys->i_cc_pts = 0;
205     p_sys->i_cc_dts = 0;
206     p_sys->i_cc_flags = 0;
207     cc_Init( &p_sys->cc );
208
209     return VLC_SUCCESS;
210 }
211
212 /*****************************************************************************
213  * Close:
214  *****************************************************************************/
215 static void Close( vlc_object_t *p_this )
216 {
217     decoder_t     *p_dec = (decoder_t*)p_this;
218     decoder_sys_t *p_sys = p_dec->p_sys;
219
220     block_BytestreamRelease( &p_sys->bytestream );
221
222     if( p_sys->p_seq )
223     {
224         block_Release( p_sys->p_seq );
225     }
226     if( p_sys->p_ext )
227     {
228         block_Release( p_sys->p_ext );
229     }
230     if( p_sys->p_frame )
231     {
232         block_ChainRelease( p_sys->p_frame );
233     }
234
235     var_Destroy( p_dec, "packetizer-mpegvideo-sync-iframe" );
236
237     free( p_sys );
238 }
239
240 /*****************************************************************************
241  * Packetize:
242  *****************************************************************************/
243 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
244 {
245     decoder_sys_t *p_sys = p_dec->p_sys;
246     block_t       *p_pic;
247
248     if( pp_block == NULL || *pp_block == NULL )
249     {
250         return NULL;
251     }
252
253     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
254     {
255         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
256         {
257             p_sys->i_state = STATE_NOSYNC;
258             block_BytestreamFlush( &p_sys->bytestream );
259
260             p_sys->b_discontinuity = VLC_TRUE;
261             if( p_sys->p_frame )
262                 block_ChainRelease( p_sys->p_frame );
263             p_sys->p_frame = NULL;
264             p_sys->pp_last = &p_sys->p_frame;
265             p_sys->b_frame_slice = VLC_FALSE;
266         }
267 //        p_sys->i_interpolated_dts =
268 //        p_sys->i_last_ref_pts = 0;
269
270         block_Release( *pp_block );
271         return NULL;
272     }
273
274
275     block_BytestreamPush( &p_sys->bytestream, *pp_block );
276
277     while( 1 )
278     {
279         switch( p_sys->i_state )
280         {
281
282         case STATE_NOSYNC:
283             if( block_FindStartcodeFromOffset( &p_sys->bytestream,
284                     &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS )
285             {
286                 p_sys->i_state = STATE_NEXT_SYNC;
287             }
288
289             if( p_sys->i_offset )
290             {
291                 block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
292                 p_sys->i_offset = 0;
293                 block_BytestreamFlush( &p_sys->bytestream );
294             }
295
296             if( p_sys->i_state != STATE_NEXT_SYNC )
297             {
298                 /* Need more data */
299                 return NULL;
300             }
301
302             p_sys->i_offset = 1; /* To find next startcode */
303
304         case STATE_NEXT_SYNC:
305             /* TODO: If p_block == NULL, flush the buffer without checking the
306              * next sync word */
307
308             /* Find the next startcode */
309             if( block_FindStartcodeFromOffset( &p_sys->bytestream,
310                     &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS )
311             {
312                 /* Need more data */
313                 return NULL;
314             }
315
316             /* Get the new fragment and set the pts/dts */
317             p_pic = block_New( p_dec, p_sys->i_offset );
318             block_BytestreamFlush( &p_sys->bytestream );
319             p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
320             p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
321
322             block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
323                             p_pic->i_buffer );
324
325             /* don't reuse the same timestamps several times */
326             if( p_pic->i_buffer >= 4 && p_pic->p_buffer[3] == 0x00 )
327             {
328                 /* We have a picture start code */
329                 p_sys->bytestream.p_block->i_pts = 0;
330                 p_sys->bytestream.p_block->i_dts = 0;
331             }
332
333             p_sys->i_offset = 0;
334
335             /* Get picture if any */
336             if( !( p_pic = ParseMPEGBlock( p_dec, p_pic ) ) )
337             {
338                 p_sys->i_state = STATE_NOSYNC;
339                 break;
340             }
341
342             /* If a discontinuity has been encountered, then wait till
343              * the next Intra frame before continuing with packetizing */
344             if( p_sys->b_discontinuity &&
345                 p_sys->b_sync_on_intra_frame )
346             {
347                 if( p_pic->i_flags & BLOCK_FLAG_TYPE_I )
348                 {
349                     msg_Dbg( p_dec, "synced on intra frame" );
350                     p_sys->b_discontinuity = VLC_FALSE;
351                     p_pic->i_flags |= BLOCK_FLAG_DISCONTINUITY;
352                 }
353                 else
354                 {
355                     msg_Dbg( p_dec, "waiting on intra frame" );
356                     p_sys->i_state = STATE_NOSYNC;
357                     block_Release( p_pic );
358                     break;
359                 }
360             }
361
362             /* We've just started the stream, wait for the first PTS.
363              * We discard here so we can still get the sequence header. */
364             if( p_sys->i_dts <= 0 && p_sys->i_pts <= 0 &&
365                 p_sys->i_interpolated_dts <= 0 )
366             {
367                 msg_Dbg( p_dec, "need a starting pts/dts" );
368                 p_sys->i_state = STATE_NOSYNC;
369                 block_Release( p_pic );
370                 break;
371             }
372
373             /* When starting the stream we can have the first frame with
374              * a null DTS (i_interpolated_pts is initialized to 0) */
375             if( !p_pic->i_dts ) p_pic->i_dts = p_pic->i_pts;
376
377             /* So p_block doesn't get re-added several times */
378             *pp_block = block_BytestreamPop( &p_sys->bytestream );
379
380             p_sys->i_state = STATE_NOSYNC;
381
382             return p_pic;
383         }
384     }
385 }
386
387 /*****************************************************************************
388  * GetCc:
389  *****************************************************************************/
390 static block_t *GetCc( decoder_t *p_dec, vlc_bool_t pb_present[4] )
391 {
392     decoder_sys_t *p_sys = p_dec->p_sys;
393     block_t *p_cc;
394     int i;
395
396     for( i = 0; i < 4; i++ )
397         pb_present[i] = p_sys->cc.pb_present[i];
398
399     if( p_sys->cc.i_data <= 0 )
400         return NULL;
401
402     p_cc = block_New( p_dec, p_sys->cc.i_data);
403     if( p_cc )
404     {
405         memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
406         p_cc->i_dts = 
407         p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
408         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);
409     }
410     cc_Flush( &p_sys->cc );
411     return p_cc;
412 }
413
414 /*****************************************************************************
415  * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
416  *****************************************************************************/
417 static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
418 {
419     decoder_sys_t *p_sys = p_dec->p_sys;
420     block_t *p_pic = NULL;
421
422     /*
423      * Check if previous picture is finished
424      */
425     if( ( p_sys->b_frame_slice &&
426           (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) &&
427           p_sys->p_seq == NULL )
428     {
429         /* We have a picture but without a sequence header we can't
430          * do anything */
431         msg_Dbg( p_dec, "waiting for sequence start" );
432         if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
433         p_sys->p_frame = NULL;
434         p_sys->pp_last = &p_sys->p_frame;
435         p_sys->b_frame_slice = VLC_FALSE;
436
437     }
438     else if( p_sys->b_frame_slice &&
439              (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) )
440     {
441         mtime_t i_duration;
442
443         p_pic = block_ChainGather( p_sys->p_frame );
444
445         i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base /
446                                 p_sys->i_frame_rate );
447
448         if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 )
449         {
450             i_duration /= 2;
451         }
452
453         if( p_sys->b_seq_progressive )
454         {
455             if( p_sys->i_top_field_first == 0 &&
456                 p_sys->i_repeat_first_field == 1 )
457             {
458                 i_duration *= 2;
459             }
460             else if( p_sys->i_top_field_first == 1 &&
461                      p_sys->i_repeat_first_field == 1 )
462             {
463                 i_duration *= 3;
464             }
465         }
466         else
467         {
468             if( p_sys->i_picture_structure == 0x03 )
469             {
470                 if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field )
471                 {
472                     i_duration += i_duration / 2;
473                 }
474             }
475         }
476
477         if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
478         {
479             /* Trivial case (DTS == PTS) */
480             /* Correct interpolated dts when we receive a new pts/dts */
481             if( p_sys->i_pts > 0 ) p_sys->i_interpolated_dts = p_sys->i_pts;
482             if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
483         }
484         else
485         {
486             /* Correct interpolated dts when we receive a new pts/dts */
487             if( p_sys->i_last_ref_pts > 0 && !p_sys->b_second_field )
488                 p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
489             if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
490
491             if( !p_sys->b_second_field )
492                 p_sys->i_last_ref_pts = p_sys->i_pts;
493         }
494
495         p_pic->i_dts = p_sys->i_interpolated_dts;
496         p_sys->i_interpolated_dts += i_duration;
497
498         /* Set PTS only if we have a B frame or if it comes from the stream */
499         if( p_sys->i_pts > 0 )
500         {
501             p_pic->i_pts = p_sys->i_pts;
502         }
503         else if( p_sys->i_picture_type == 0x03 )
504         {
505             p_pic->i_pts = p_pic->i_dts;
506         }
507         else
508         {
509             p_pic->i_pts = 0;
510         }
511
512         switch ( p_sys->i_picture_type )
513         {
514         case 0x01:
515             p_pic->i_flags |= BLOCK_FLAG_TYPE_I;
516             break;
517         case 0x02:
518             p_pic->i_flags |= BLOCK_FLAG_TYPE_P;
519             break;
520         case 0x03:
521             p_pic->i_flags |= BLOCK_FLAG_TYPE_B;
522             break;
523         }
524
525         p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts;
526
527 #if 0
528         msg_Dbg( p_dec, "pic: type=%d dts="I64Fd" pts-dts="I64Fd,
529         p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts);
530 #endif
531
532         /* Reset context */
533         p_sys->p_frame = NULL;
534         p_sys->pp_last = &p_sys->p_frame;
535         p_sys->b_frame_slice = VLC_FALSE;
536
537         if( p_sys->i_picture_structure != 0x03 )
538         {
539             p_sys->b_second_field = !p_sys->b_second_field;
540         }
541         else
542         {
543             p_sys->b_second_field = 0;
544         }
545
546         /* CC */
547         p_sys->b_cc_reset = VLC_TRUE;
548         p_sys->i_cc_pts = p_pic->i_pts;
549         p_sys->i_cc_dts = p_pic->i_dts;
550         p_sys->i_cc_flags = p_pic->i_flags;
551     }
552
553     if( !p_pic && p_sys->b_cc_reset )
554     {
555         p_sys->b_cc_reset = VLC_FALSE;
556         cc_Flush( &p_sys->cc );
557     }
558
559     /*
560      * Check info of current fragment
561      */
562     if( p_frag->p_buffer[3] == 0xb8 )
563     {
564         /* Group start code */
565         if( p_sys->p_seq &&
566             p_sys->i_seq_old > p_sys->i_frame_rate/p_sys->i_frame_rate_base )
567         {
568             /* Usefull for mpeg1: repeat sequence header every second */
569             block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_seq ) );
570             if( p_sys->p_ext )
571             {
572                 block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_ext ) );
573             }
574
575             p_sys->i_seq_old = 0;
576         }
577     }
578     else if( p_frag->p_buffer[3] == 0xb3 && p_frag->i_buffer >= 8 )
579     {
580         /* Sequence header code */
581         static const int code_to_frame_rate[16][2] =
582         {
583             { 1, 1 },  /* invalid */
584             { 24000, 1001 }, { 24, 1 }, { 25, 1 },       { 30000, 1001 },
585             { 30, 1 },       { 50, 1 }, { 60000, 1001 }, { 60, 1 },
586             /* Unofficial 15fps from Xing*/
587             { 15, 1001 },
588             /* Unofficial economy rates from libmpeg3 */
589             { 5, 1001 }, { 10, 1001 }, { 12, 1001 }, { 15, 1001 },
590             { 1, 1 },  { 1, 1 }  /* invalid */
591         };
592
593         if( p_sys->p_seq ) block_Release( p_sys->p_seq );
594         if( p_sys->p_ext ) block_Release( p_sys->p_ext );
595
596         p_sys->p_seq = block_Duplicate( p_frag );
597         p_sys->i_seq_old = 0;
598         p_sys->p_ext = NULL;
599
600         p_dec->fmt_out.video.i_width =
601             ( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 );
602         p_dec->fmt_out.video.i_height =
603             ( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6];
604         p_sys->i_aspect_ratio_info = p_frag->p_buffer[7] >> 4;
605
606         /* TODO: MPEG1 aspect ratio */
607
608         p_sys->i_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0];
609         p_sys->i_frame_rate_base =
610             code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1];
611
612         p_dec->fmt_out.video.i_frame_rate = p_sys->i_frame_rate;
613         p_dec->fmt_out.video.i_frame_rate_base = p_sys->i_frame_rate_base;
614
615         p_sys->b_seq_progressive = VLC_TRUE;
616         p_sys->b_low_delay = VLC_TRUE;
617
618         if ( !p_sys->b_inited )
619         {
620             msg_Dbg( p_dec, "size %dx%d fps=%.3f",
621                  p_dec->fmt_out.video.i_width, p_dec->fmt_out.video.i_height,
622                  p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base );
623             p_sys->b_inited = 1;
624         }
625     }
626     else if( p_frag->p_buffer[3] == 0xb5 )
627     {
628         int i_type = p_frag->p_buffer[4] >> 4;
629
630         /* Extension start code */
631         if( i_type == 0x01 )
632         {
633 #if 0
634             static const int mpeg2_aspect[16][2] =
635             {
636                 {0,1}, {1,1}, {4,3}, {16,9}, {221,100},
637                 {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1},
638                 {0,1}, {0,1}
639             };
640 #endif
641
642             /* sequence extension */
643             if( p_sys->p_ext) block_Release( p_sys->p_ext );
644             p_sys->p_ext = block_Duplicate( p_frag );
645
646             if( p_frag->i_buffer >= 10 )
647             {
648                 p_sys->b_seq_progressive =
649                     p_frag->p_buffer[5]&0x08 ? VLC_TRUE : VLC_FALSE;
650                 p_sys->b_low_delay =
651                     p_frag->p_buffer[9]&0x80 ? VLC_TRUE : VLC_FALSE;
652             }
653
654             /* Do not set aspect ratio : in case we're transcoding,
655              * transcode will take our fmt_out as a fmt_in to libmpeg2.
656              * libmpeg2.c will then believe that the user has requested
657              * a specific aspect ratio, which she hasn't. Thus in case
658              * of aspect ratio change, we're screwed. --Meuuh
659              */
660 #if 0
661             p_dec->fmt_out.video.i_aspect =
662                 mpeg2_aspect[p_sys->i_aspect_ratio_info][0] *
663                 VOUT_ASPECT_FACTOR /
664                 mpeg2_aspect[p_sys->i_aspect_ratio_info][1];
665 #endif
666
667         }
668         else if( i_type == 0x08 )
669         {
670             /* picture extension */
671             p_sys->i_picture_structure = p_frag->p_buffer[6]&0x03;
672             p_sys->i_top_field_first   = p_frag->p_buffer[7] >> 7;
673             p_sys->i_repeat_first_field= (p_frag->p_buffer[7]>>1)&0x01;
674             p_sys->i_progressive_frame = p_frag->p_buffer[8] >> 7;
675         }
676     }
677     else if( p_frag->p_buffer[3] == 0xb2 && p_frag->i_buffer > 4 )
678     {
679         cc_Extract( &p_sys->cc, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
680     }
681     else if( p_frag->p_buffer[3] == 0x00 )
682     {
683         /* Picture start code */
684         p_sys->i_seq_old++;
685
686         if( p_frag->i_buffer >= 6 )
687         {
688             p_sys->i_temporal_ref =
689                 ( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6);
690             p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 ) & 0x03;
691         }
692
693         p_sys->i_dts = p_frag->i_dts;
694         p_sys->i_pts = p_frag->i_pts;
695     }
696     else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf )
697     {
698         /* Slice start code */
699         p_sys->b_frame_slice = VLC_TRUE;
700     }
701
702     /* Append the block */
703     block_ChainLastAppend( &p_sys->pp_last, p_frag );
704
705     return p_pic;
706 }