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