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