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