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