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