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