]> git.sesse.net Git - vlc/blob - modules/packetizer/mpegvideo.c
* modules/packetizer/mpegvideo.c: corner case fix for pts/dts.
[vlc] / modules / packetizer / mpegvideo.c
1 /*****************************************************************************
2  * mpegvideo.c: parse and packetize an MPEG1/2 video stream
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Eric Petit <titer@videolan.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Problem with this implementation:
28  *
29  * Although we should time-stamp each picture with a PTS, this isn't possible
30  * with the current implementation.
31  * The problem comes from the fact that for non-low-delay streams we can't
32  * calculate the PTS of pictures used as backward reference. Even the temporal
33  * reference number doesn't help here because all the pictures don't
34  * necessarily have the same duration (eg. 3:2 pulldown).
35  *
36  * However this doesn't really matter as far as the MPEG muxers are concerned
37  * because they allow having empty PTS fields. --gibalou
38  *****************************************************************************/
39
40 /*****************************************************************************
41  * Preamble
42  *****************************************************************************/
43 #include <stdlib.h>                                      /* malloc(), free() */
44
45 #include <vlc/vlc.h>
46 #include <vlc/decoder.h>
47 #include <vlc/input.h>
48
49 #include "vlc_block_helper.h"
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 static int  Open ( vlc_object_t * );
55 static void Close( vlc_object_t * );
56
57 vlc_module_begin();
58     set_description( _("MPEG-I/II video packetizer") );
59     set_capability( "packetizer", 50 );
60     set_callbacks( Open, Close );
61 vlc_module_end();
62
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 static block_t *Packetize( decoder_t *, block_t ** );
67 static block_t *ParseMPEGBlock( decoder_t *, block_t * );
68
69 struct decoder_sys_t
70 {
71     /*
72      * Input properties
73      */
74     block_bytestream_t bytestream;
75     int i_state;
76     int i_offset;
77     uint8_t p_startcode[3];
78
79     /* Sequence header and extension */
80     block_t *p_seq;
81     block_t *p_ext;
82
83     /* Current frame being built */
84     block_t    *p_frame;
85     block_t    **pp_last;
86
87     vlc_bool_t b_frame_slice;
88     mtime_t i_pts;
89     mtime_t i_dts;
90
91     /* Sequence properties */
92     int         i_frame_rate;
93     int         i_frame_rate_base;
94     vlc_bool_t  b_seq_progressive;
95     vlc_bool_t  b_low_delay;
96     int         i_aspect_ratio_info;
97     vlc_bool_t  b_inited;
98
99     /* Picture properties */
100     int i_temporal_ref;
101     int i_picture_type;
102     int i_picture_structure;
103     int i_top_field_first;
104     int i_repeat_first_field;
105     int i_progressive_frame;
106
107     mtime_t i_interpolated_dts;
108     mtime_t i_old_duration;
109     mtime_t i_last_ref_pts;
110
111     /* Number of pictures since last sequence header */
112     int i_seq_old;
113
114 };
115
116 enum {
117     STATE_NOSYNC,
118     STATE_NEXT_SYNC
119 };
120
121 /*****************************************************************************
122  * Open:
123  *****************************************************************************/
124 static int Open( vlc_object_t *p_this )
125 {
126     decoder_t *p_dec = (decoder_t*)p_this;
127     decoder_sys_t *p_sys;
128
129     if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '1' ) &&
130         p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '2' ) &&
131         p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
132     {
133         return VLC_EGENERIC;
134     }
135
136     es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC('m','p','g','v') );
137     p_dec->pf_packetize = Packetize;
138
139     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
140
141     /* Misc init */
142     p_sys->i_state = STATE_NOSYNC;
143     p_sys->bytestream = block_BytestreamInit( p_dec );
144     p_sys->p_startcode[0] = 0;
145     p_sys->p_startcode[1] = 0;
146     p_sys->p_startcode[2] = 1;
147     p_sys->i_offset = 0;
148
149     p_sys->p_seq = NULL;
150     p_sys->p_ext = NULL;
151     p_sys->p_frame = NULL;
152     p_sys->pp_last = &p_sys->p_frame;
153     p_sys->b_frame_slice = VLC_FALSE;
154
155     p_sys->i_dts = p_sys->i_pts = 0;
156
157     p_sys->i_frame_rate = 1;
158     p_sys->i_frame_rate_base = 1;
159     p_sys->b_seq_progressive = VLC_TRUE;
160     p_sys->b_low_delay = VLC_TRUE;
161     p_sys->i_seq_old = 0;
162
163     p_sys->i_temporal_ref = 0;
164     p_sys->i_picture_type = 0;
165     p_sys->i_picture_structure = 0x03; /* frame */
166     p_sys->i_top_field_first = 0;
167     p_sys->i_repeat_first_field = 0;
168     p_sys->i_progressive_frame = 0;
169     p_sys->b_inited = 0;
170
171     p_sys->i_interpolated_dts = 0;
172     p_sys->i_old_duration = 0;
173     p_sys->i_last_ref_pts = 0;
174
175     return VLC_SUCCESS;
176 }
177
178 /*****************************************************************************
179  * Close:
180  *****************************************************************************/
181 static void Close( vlc_object_t *p_this )
182 {
183     decoder_t     *p_dec = (decoder_t*)p_this;
184     decoder_sys_t *p_sys = p_dec->p_sys;
185
186     block_BytestreamRelease( &p_sys->bytestream );
187
188     if( p_sys->p_seq )
189     {
190         block_Release( p_sys->p_seq );
191     }
192     if( p_sys->p_ext )
193     {
194         block_Release( p_sys->p_ext );
195     }
196     if( p_sys->p_frame )
197     {
198         block_ChainRelease( p_sys->p_frame );
199     }
200
201     free( p_sys );
202 }
203
204 /*****************************************************************************
205  * Packetize:
206  *****************************************************************************/
207 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
208 {
209     decoder_sys_t *p_sys = p_dec->p_sys;
210     block_t       *p_pic;
211
212     if( pp_block == NULL || *pp_block == NULL )
213     {
214         return NULL;
215     }
216
217     if( (*pp_block)->i_flags & BLOCK_FLAG_DISCONTINUITY )
218     {
219         p_sys->i_state = STATE_NOSYNC;
220         if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
221         p_sys->p_frame = NULL;
222         p_sys->pp_last = &p_sys->p_frame;
223         p_sys->b_frame_slice = VLC_FALSE;
224         block_Release( *pp_block );
225         return NULL;
226     }
227
228     block_BytestreamPush( &p_sys->bytestream, *pp_block );
229
230     while( 1 )
231     {
232         switch( p_sys->i_state )
233         {
234
235         case STATE_NOSYNC:
236             if( block_FindStartcodeFromOffset( &p_sys->bytestream,
237                     &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS )
238             {
239                 p_sys->i_state = STATE_NEXT_SYNC;
240             }
241
242             if( p_sys->i_offset )
243             {
244                 block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
245                 p_sys->i_offset = 0;
246                 block_BytestreamFlush( &p_sys->bytestream );
247             }
248
249             if( p_sys->i_state != STATE_NEXT_SYNC )
250             {
251                 /* Need more data */
252                 return NULL;
253             }
254
255             p_sys->i_offset = 1; /* To find next startcode */
256
257         case STATE_NEXT_SYNC:
258             /* TODO: If p_block == NULL, flush the buffer without checking the
259              * next sync word */
260
261             /* Find the next startcode */
262             if( block_FindStartcodeFromOffset( &p_sys->bytestream,
263                     &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS )
264             {
265                 /* Need more data */
266                 return NULL;
267             }
268
269             /* Get the new fragment and set the pts/dts */
270             p_pic = block_New( p_dec, p_sys->i_offset );
271             p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
272             p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
273
274             /* FIXME ? Should we flush the bytestream chain before ? */
275             if( p_sys->bytestream.p_block->i_buffer ==
276                 p_sys->bytestream.i_offset &&
277                 p_sys->bytestream.p_block->p_next )
278             {
279                 p_pic->i_pts = p_sys->bytestream.p_block->p_next->i_pts;
280                 p_pic->i_dts = p_sys->bytestream.p_block->p_next->i_dts;
281             }
282
283             block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
284                             p_pic->i_buffer );
285
286             /* don't reuse the same timestamps several times */
287             if( p_pic->i_buffer >= 4 && p_pic->p_buffer[3] == 0x00 )
288             {
289                 /* We have a picture start code */
290                 p_sys->bytestream.p_block->i_pts = 0;
291                 p_sys->bytestream.p_block->i_dts = 0;
292             }
293
294             p_sys->i_offset = 0;
295
296             /* Get picture if any */
297             if( !( p_pic = ParseMPEGBlock( p_dec, p_pic ) ) )
298             {
299                 p_sys->i_state = STATE_NOSYNC;
300                 break;
301             }
302
303             /* We've just started the stream, wait for the first PTS.
304              * We discard here so we can still get the sequence header. */
305             if( p_sys->i_dts <= 0 && p_sys->i_pts <= 0 &&
306                 p_sys->i_interpolated_dts <= 0 )
307             {
308                 msg_Dbg( p_dec, "need a starting pts/dts" );
309                 p_sys->i_state = STATE_NOSYNC;
310                 block_Release( p_pic );
311                 break;
312             }
313
314             /* When starting the stream we can have the first frame with
315              * a null DTS (i_interpolated_pts is initialized to 0) */
316             if( !p_pic->i_dts ) p_pic->i_dts = p_pic->i_pts;
317
318             /* So p_block doesn't get re-added several times */
319             *pp_block = block_BytestreamPop( &p_sys->bytestream );
320
321             p_sys->i_state = STATE_NOSYNC;
322
323             return p_pic;
324         }
325     }
326 }
327
328 /*****************************************************************************
329  * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
330  *****************************************************************************/
331 static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
332 {
333     decoder_sys_t *p_sys = p_dec->p_sys;
334     block_t *p_pic = NULL;
335
336     /*
337      * Check if previous picture is finished
338      */
339     if( ( p_sys->b_frame_slice &&
340           (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) &&
341           p_sys->p_seq == NULL )
342     {
343         /* We have a picture but without a sequence header we can't
344          * do anything */
345         msg_Dbg( p_dec, "waiting for sequence start" );
346         if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
347         p_sys->p_frame = NULL;
348         p_sys->pp_last = &p_sys->p_frame;
349         p_sys->b_frame_slice = VLC_FALSE;
350
351     }
352     else if( p_sys->b_frame_slice &&
353              (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) )
354     {
355         mtime_t i_duration;
356
357         p_pic = block_ChainGather( p_sys->p_frame );
358
359         i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base /
360                                 p_sys->i_frame_rate );
361
362         if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 )
363         {
364             i_duration /= 2;
365         }
366
367         if( p_sys->b_seq_progressive )
368         {
369             if( p_sys->i_top_field_first == 0 &&
370                 p_sys->i_repeat_first_field == 1 )
371             {
372                 i_duration *= 2;
373             }
374             else if( p_sys->i_top_field_first == 1 &&
375                      p_sys->i_repeat_first_field == 1 )
376             {
377                 i_duration *= 3;
378             }
379         }
380         else
381         {
382             if( p_sys->i_picture_structure == 0x03 )
383             {
384                 if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field )
385                 {
386                     i_duration += i_duration / 2;
387                 }
388             }
389         }
390
391         if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
392         {
393             /* Trivial case (DTS == PTS) */
394             /* Correct interpolated dts when we receive a new pts/dts */
395             if( p_sys->i_pts > 0 ) p_sys->i_interpolated_dts = p_sys->i_pts;
396             if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
397         }
398         else
399         {
400             /* Correct interpolated dts when we receive a new pts/dts */
401             if( p_sys->i_last_ref_pts > 0 )
402                 p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
403             if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
404
405             p_sys->i_last_ref_pts = p_sys->i_pts;
406         }
407
408         p_pic->i_dts = p_sys->i_interpolated_dts;
409
410         /* Set PTS only if we have a B frame or if it comes from the stream */
411         if( p_sys->i_pts > 0 )
412         {
413             p_pic->i_pts = p_sys->i_pts;
414         }
415         else if( p_sys->i_picture_type == 0x03 )
416         {
417             p_pic->i_pts = p_pic->i_dts;
418         }
419         else
420         {
421             p_pic->i_pts = 0;
422         }
423
424         if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
425         {
426             /* Trivial case (DTS == PTS) */
427             p_sys->i_interpolated_dts += i_duration;
428         }
429         else
430         {
431             p_sys->i_interpolated_dts += p_sys->i_old_duration;
432             p_sys->i_old_duration = i_duration;
433         }
434
435         switch ( p_sys->i_picture_type )
436         {
437         case 0x01:
438             p_pic->i_flags |= BLOCK_FLAG_TYPE_I;
439             break;
440         case 0x02:
441             p_pic->i_flags |= BLOCK_FLAG_TYPE_P;
442             break;
443         case 0x03:
444             p_pic->i_flags |= BLOCK_FLAG_TYPE_B;
445             break;
446         }
447
448         p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts;
449
450 #if 0
451         msg_Dbg( p_dec, "pic: type=%d dts="I64Fd" pts-dts="I64Fd,
452         p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts);
453 #endif
454
455         /* Reset context */
456         p_sys->p_frame = NULL;
457         p_sys->pp_last = &p_sys->p_frame;
458         p_sys->b_frame_slice = VLC_FALSE;
459     }
460
461     /*
462      * Check info of current fragment
463      */
464     if( p_frag->p_buffer[3] == 0xb8 )
465     {
466         /* Group start code */
467         if( p_sys->p_seq &&
468             p_sys->i_seq_old > p_sys->i_frame_rate/p_sys->i_frame_rate_base )
469         {
470             /* Usefull for mpeg1: repeat sequence header every second */
471             block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_seq ) );
472             if( p_sys->p_ext )
473             {
474                 block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_ext ) );
475             }
476
477             p_sys->i_seq_old = 0;
478         }
479     }
480     else if( p_frag->p_buffer[3] == 0xb3 && p_frag->i_buffer >= 8 )
481     {
482         /* Sequence header code */
483         static const int code_to_frame_rate[16][2] =
484         {
485             { 1, 1 },  /* invalid */
486             { 24000, 1001 }, { 24, 1 }, { 25, 1 },       { 30000, 1001 },
487             { 30, 1 },       { 50, 1 }, { 60000, 1001 }, { 60, 1 },
488             /* Unofficial 15fps from Xing*/
489             { 15, 1001 },
490             /* Unofficial economy rates from libmpeg3 */
491             { 5, 1001 }, { 10, 1001 }, { 12, 1001 }, { 15, 1001 },
492             { 1, 1 },  { 1, 1 }  /* invalid */
493         };
494
495         if( p_sys->p_seq ) block_Release( p_sys->p_seq );
496         if( p_sys->p_ext ) block_Release( p_sys->p_ext );
497
498         p_sys->p_seq = block_Duplicate( p_frag );
499         p_sys->i_seq_old = 0;
500         p_sys->p_ext = NULL;
501
502         p_dec->fmt_out.video.i_width =
503             ( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 );
504         p_dec->fmt_out.video.i_height =
505             ( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6];
506         p_sys->i_aspect_ratio_info = p_frag->p_buffer[7] >> 4;
507
508         /* TODO: MPEG1 aspect ratio */
509
510         p_sys->i_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0];
511         p_sys->i_frame_rate_base =
512             code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1];
513
514         p_sys->b_seq_progressive = VLC_TRUE;
515         p_sys->b_low_delay = VLC_TRUE;
516
517         if ( !p_sys->b_inited )
518         {
519             msg_Dbg( p_dec, "Size %dx%d fps=%.3f",
520                  p_dec->fmt_out.video.i_width, p_dec->fmt_out.video.i_height,
521                  p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base );
522             p_sys->b_inited = 1;
523         }
524     }
525     else if( p_frag->p_buffer[3] == 0xb5 )
526     {
527         int i_type = p_frag->p_buffer[4] >> 4;
528
529         /* Extention start code */
530         if( i_type == 0x01 )
531         {
532             static const int mpeg2_aspect[16][2] =
533             {
534                 {0,1}, {1,1}, {4,3}, {16,9}, {221,100},
535                 {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1},
536                 {0,1}, {0,1}
537             };
538
539             /* sequence extention */
540             if( p_sys->p_ext) block_Release( p_sys->p_ext );
541             p_sys->p_ext = block_Duplicate( p_frag );
542
543             if( p_frag->i_buffer >= 10 )
544             {
545                 p_sys->b_seq_progressive =
546                     p_frag->p_buffer[5]&0x08 ? VLC_TRUE : VLC_FALSE;
547                 p_sys->b_low_delay =
548                     p_frag->p_buffer[9]&0x80 ? VLC_TRUE : VLC_FALSE;
549             }
550
551             p_dec->fmt_out.video.i_aspect =
552                 mpeg2_aspect[p_sys->i_aspect_ratio_info][0] *
553                 VOUT_ASPECT_FACTOR /
554                 mpeg2_aspect[p_sys->i_aspect_ratio_info][1];
555
556         }
557         else if( i_type == 0x08 )
558         {
559             /* picture extention */
560             p_sys->i_picture_structure = p_frag->p_buffer[6]&0x03;
561             p_sys->i_top_field_first   = p_frag->p_buffer[7] >> 7;
562             p_sys->i_repeat_first_field= (p_frag->p_buffer[7]>>1)&0x01;
563             p_sys->i_progressive_frame = p_frag->p_buffer[8] >> 7;
564         }
565     }
566     else if( p_frag->p_buffer[3] == 0x00 )
567     {
568         /* Picture start code */
569         p_sys->i_seq_old++;
570
571         if( p_frag->i_buffer >= 6 )
572         {
573             p_sys->i_temporal_ref =
574                 ( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6);
575             p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 ) & 0x03;
576         }
577
578         p_sys->i_dts = p_frag->i_dts;
579         p_sys->i_pts = p_frag->i_pts;
580     }
581     else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf )
582     {
583         /* Slice start code */
584         p_sys->b_frame_slice = VLC_TRUE;
585     }
586
587     /* Append the block */
588     block_ChainLastAppend( &p_sys->pp_last, p_frag );
589
590     return p_pic;
591 }