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