]> git.sesse.net Git - vlc/blob - modules/packetizer/mpegvideo.c
* modules/packetizer/mpegvideo.c: fixed major bug where the first frame could be...
[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: mpegvideo.c,v 1.28 2004/01/27 14:05:33 gbazin Exp $
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 extention */
80     block_t *p_seq;
81     block_t *p_ext;
82
83     /* Current frame being built */
84     block_t    *p_frame;
85     vlc_bool_t b_frame_slice;
86     mtime_t i_pts;
87     mtime_t i_dts;
88
89     /* Sequence properties */
90     int         i_frame_rate;
91     int         i_frame_rate_base;
92     vlc_bool_t  b_seq_progressive;
93     vlc_bool_t  b_low_delay;
94     int         i_aspect_ratio_info;
95
96     /* Picture properties */
97     int i_temporal_ref;
98     int i_picture_type;
99     int i_picture_structure;
100     int i_top_field_first;
101     int i_repeat_first_field;
102     int i_progressive_frame;
103
104     mtime_t i_interpolated_dts;
105     mtime_t i_old_duration;
106     mtime_t i_last_ref_pts;
107
108     /* Number of pictues since last sequence header */
109     int i_seq_old;
110
111 };
112
113 enum {
114     STATE_NOSYNC,
115     STATE_NEXT_SYNC
116 };
117
118 /*****************************************************************************
119  * Open:
120  *****************************************************************************/
121 static int Open( vlc_object_t *p_this )
122 {
123     decoder_t *p_dec = (decoder_t*)p_this;
124     decoder_sys_t *p_sys;
125
126     if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '1' ) &&
127         p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '2' ) &&
128         p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
129     {
130         return VLC_EGENERIC;
131     }
132
133     es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC('m','p','g','v') );
134     p_dec->pf_packetize = Packetize;
135
136     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
137
138     /* Misc init */
139     p_sys->i_state = STATE_NOSYNC;
140     p_sys->bytestream = block_BytestreamInit( p_dec );
141     p_sys->p_startcode[0] = 0;
142     p_sys->p_startcode[1] = 0;
143     p_sys->p_startcode[2] = 1;
144     p_sys->i_offset = 0;
145
146     p_sys->p_seq = NULL;
147     p_sys->p_ext = NULL;
148     p_sys->p_frame = NULL;
149     p_sys->b_frame_slice = VLC_FALSE;
150
151     p_sys->i_dts = p_sys->i_pts = 0;
152
153     p_sys->i_frame_rate = 1;
154     p_sys->i_frame_rate_base = 1;
155     p_sys->b_seq_progressive = VLC_TRUE;
156     p_sys->b_low_delay = VLC_TRUE;
157     p_sys->i_seq_old = 0;
158
159     p_sys->i_temporal_ref = 0;
160     p_sys->i_picture_type = 0;
161     p_sys->i_picture_structure = 0x03; /* frame */
162     p_sys->i_top_field_first = 0;
163     p_sys->i_repeat_first_field = 0;
164     p_sys->i_progressive_frame = 0;
165
166     p_sys->i_interpolated_dts = 0;
167     p_sys->i_old_duration = 0;
168     p_sys->i_last_ref_pts = 0;
169
170     return VLC_SUCCESS;
171 }
172
173 /*****************************************************************************
174  * Close:
175  *****************************************************************************/
176 static void Close( vlc_object_t *p_this )
177 {
178     decoder_t     *p_dec = (decoder_t*)p_this;
179     decoder_sys_t *p_sys = p_dec->p_sys;
180
181     block_BytestreamRelease( &p_sys->bytestream );
182
183     if( p_sys->p_seq )
184     {
185         block_Release( p_sys->p_seq );
186     }
187     if( p_sys->p_ext )
188     {
189         block_Release( p_sys->p_ext );
190     }
191     if( p_sys->p_frame )
192     {
193         block_ChainRelease( p_sys->p_frame );
194     }
195
196     free( p_sys );
197 }
198
199 /*****************************************************************************
200  * Packetize:
201  *****************************************************************************/
202 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
203 {
204     decoder_sys_t *p_sys = p_dec->p_sys;
205     block_t       *p_pic;
206
207     if( pp_block == NULL || *pp_block == NULL )
208     {
209         return NULL;
210     }
211
212     if( (*pp_block)->b_discontinuity )
213     {
214         p_sys->i_state = STATE_NOSYNC;
215         if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
216         p_sys->p_frame = NULL;
217         p_sys->b_frame_slice = VLC_FALSE;
218     }
219
220     block_BytestreamPush( &p_sys->bytestream, *pp_block );
221
222     while( 1 )
223     {
224         switch( p_sys->i_state )
225         {
226
227         case STATE_NOSYNC:
228             if( block_FindStartcodeFromOffset( &p_sys->bytestream,
229                     &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS )
230             {
231                 p_sys->i_state = STATE_NEXT_SYNC;
232             }
233
234             if( p_sys->i_offset )
235             {
236                 block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
237                 p_sys->i_offset = 0;
238                 block_BytestreamFlush( &p_sys->bytestream );
239             }
240
241             if( p_sys->i_state != STATE_NEXT_SYNC )
242             {
243                 /* Need more data */
244                 return NULL;
245             }
246
247             p_sys->i_offset = 1; /* To find next startcode */
248
249         case STATE_NEXT_SYNC:
250             /* TODO: If p_block == NULL, flush the buffer without checking the
251              * next sync word */
252
253             /* Find the next startcode */
254             if( block_FindStartcodeFromOffset( &p_sys->bytestream,
255                     &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS )
256             {
257                 /* Need more data */
258                 return NULL;
259             }
260
261             /* Get the new fragment and set the pts/dts */
262             p_pic = block_New( p_dec, p_sys->i_offset );
263             p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
264             p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
265
266             block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
267                             p_pic->i_buffer );
268
269             /* don't reuse the same timestamps several times */
270             if( p_pic->i_buffer >= 4 && p_pic->p_buffer[3] == 0x00 )
271             {
272                 /* We have a picture start code */
273                 p_sys->bytestream.p_block->i_pts = 0;
274                 p_sys->bytestream.p_block->i_dts = 0;
275             }
276
277             p_sys->i_offset = 0;
278
279             /* Get picture if any */
280             if( !( p_pic = ParseMPEGBlock( p_dec, p_pic ) ) )
281             {
282                 p_sys->i_state = STATE_NOSYNC;
283                 break;
284             }
285
286             /* We've just started the stream, wait for the first PTS.
287              * We discard here so we can still get the sequence header. */
288             if( p_sys->i_dts <= 0 && p_sys->i_pts <= 0 &&
289                 p_sys->i_interpolated_dts <= 0 )
290             {
291                 msg_Dbg( p_dec, "need a starting pts/dts" );
292                 p_sys->i_state = STATE_NOSYNC;
293                 block_Release( p_pic );
294                 break;
295             }
296
297             /* When starting the stream we can have the first frame with
298              * a null DTS (i_interpolated_pts is initialized to 0) */
299             if( !p_pic->i_dts ) p_pic->i_dts = p_pic->i_pts;
300
301             /* So p_block doesn't get re-added several times */
302             *pp_block = block_BytestreamPop( &p_sys->bytestream );
303
304             p_sys->i_state = STATE_NOSYNC;
305
306             return p_pic;
307         }
308     }
309 }
310
311 /*****************************************************************************
312  * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
313  *****************************************************************************/
314 static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
315 {
316     decoder_sys_t *p_sys = p_dec->p_sys;
317     block_t *p_pic = NULL;
318
319     /*
320      * Check if previous picture is finished
321      */
322     if( ( p_sys->b_frame_slice &&
323           (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) &&
324           p_sys->p_seq == NULL )
325     {
326         /* We have a picture but without a sequence header we can't
327          * do anything */
328         msg_Dbg( p_dec, "waiting for sequence start" );
329         if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
330         p_sys->p_frame = NULL;
331         p_sys->b_frame_slice = VLC_FALSE;
332
333     }
334     else if( p_sys->b_frame_slice &&
335              (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) )
336     {
337         mtime_t i_duration;
338
339         p_pic = block_ChainGather( p_sys->p_frame );
340
341         i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base /
342                                 p_sys->i_frame_rate );
343
344         if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 )
345         {
346             i_duration /= 2;
347         }
348
349         if( p_sys->b_seq_progressive )
350         {
351             if( p_sys->i_top_field_first == 0 &&
352                 p_sys->i_repeat_first_field == 1 )
353             {
354                 i_duration *= 2;
355             }
356             else if( p_sys->i_top_field_first == 1 &&
357                      p_sys->i_repeat_first_field == 1 )
358             {
359                 i_duration *= 3;
360             }
361         }
362         else
363         {
364             if( p_sys->i_picture_structure == 0x03 )
365             {
366                 if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field )
367                 {
368                     i_duration += i_duration / 2;
369                 }
370             }
371         }
372
373         if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
374         {
375             /* Trivial case (DTS == PTS) */
376             /* Correct interpolated dts when we receive a new pts/dts */
377             if( p_sys->i_pts > 0 ) p_sys->i_interpolated_dts = p_sys->i_pts;
378             if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
379         }
380         else
381         {
382             /* Correct interpolated dts when we receive a new pts/dts */
383             if( p_sys->i_last_ref_pts > 0 )
384                 p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
385             if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
386
387             p_sys->i_last_ref_pts = p_sys->i_pts;
388         }
389
390         p_pic->i_dts = p_sys->i_interpolated_dts;
391
392         /* Set PTS only if we have a B frame or if it comes from the stream */
393         if( p_sys->i_pts > 0 )
394         {
395             p_pic->i_pts = p_sys->i_pts;
396         }
397         else if( p_sys->i_picture_type == 0x03 )
398         {
399             p_pic->i_pts = p_pic->i_dts;
400         }
401         else
402         {
403             p_pic->i_pts = 0;
404         }
405
406         if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
407         {
408             /* Trivial case (DTS == PTS) */
409             p_sys->i_interpolated_dts += i_duration;
410         }
411         else
412         {
413             p_sys->i_interpolated_dts += p_sys->i_old_duration;
414             p_sys->i_old_duration = i_duration;
415         }
416
417         p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts;
418
419 #if 0
420         msg_Dbg( p_dec, "pic: type=%d dts="I64Fd" pts-dts="I64Fd,
421         p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts);
422 #endif
423
424         /* Reset context */
425         p_sys->p_frame = NULL;
426         p_sys->b_frame_slice = VLC_FALSE;
427     }
428
429     /*
430      * Check info of current fragment
431      */
432     if( p_frag->p_buffer[3] == 0xb8 )
433     {
434         /* Group start code */
435         if( p_sys->p_seq &&
436             p_sys->i_seq_old > p_sys->i_frame_rate/p_sys->i_frame_rate_base )
437         {
438             /* Usefull for mpeg1: repeat sequence header every second */
439             block_ChainAppend( &p_sys->p_frame,
440                                block_Duplicate( p_sys->p_seq ) );
441             if( p_sys->p_ext )
442             {
443                 block_ChainAppend( &p_sys->p_frame,
444                                    block_Duplicate( p_sys->p_ext ) );
445             }
446
447             p_sys->i_seq_old = 0;
448         }
449     }
450     else if( p_frag->p_buffer[3] == 0xb3 && p_frag->i_buffer >= 8 )
451     {
452         /* Sequence header code */
453         static const int code_to_frame_rate[16][2] =
454         {
455             { 1, 1 },  /* invalid */
456             { 24000, 1001 }, { 24, 1 }, { 25, 1 },       { 30000, 1001 },
457             { 30, 1 },       { 50, 1 }, { 60000, 1001 }, { 60, 1 },
458             /* Unofficial 15fps from Xing*/
459             { 15, 1001 },
460             /* Unofficial economy rates from libmpeg3 */
461             { 5, 1001 }, { 10, 1001 }, { 12, 1001 }, { 15, 1001 },
462             { 1, 1 },  { 1, 1 }  /* invalid */
463         };
464
465         if( p_sys->p_seq ) block_Release( p_sys->p_seq );
466         if( p_sys->p_ext ) block_Release( p_sys->p_ext );
467
468         p_sys->p_seq = block_Duplicate( p_frag );
469         p_sys->i_seq_old = 0;
470         p_sys->p_ext = NULL;
471
472         p_dec->fmt_out.video.i_width =
473             ( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 );
474         p_dec->fmt_out.video.i_height =
475             ( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6];
476         p_sys->i_aspect_ratio_info = p_frag->p_buffer[7] >> 4;
477
478         /* TODO: MPEG1 aspect ratio */
479
480         p_sys->i_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0];
481         p_sys->i_frame_rate_base =
482             code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1];
483
484         p_sys->b_seq_progressive = VLC_TRUE;
485         p_sys->b_low_delay = VLC_TRUE;
486
487 #if 0
488         msg_Dbg( p_dec, "Size %dx%d fps=%.3f",
489                  p_dec->fmt_out.video.i_width, p_dec->fmt_out.video.i_height,
490                  p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base );
491 #endif
492     }
493     else if( p_frag->p_buffer[3] == 0xb5 )
494     {
495         int i_type = p_frag->p_buffer[4] >> 4;
496
497         /* Extention start code */
498         if( i_type == 0x01 )
499         {
500             static const int mpeg2_aspect[16][2] =
501             {
502                 {0,1}, {1,1}, {4,3}, {16,9}, {221,100},
503                 {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1},
504                 {0,1}, {0,1}
505             };
506
507             /* sequence extention */
508             if( p_sys->p_ext) block_Release( p_sys->p_ext );
509             p_sys->p_ext = block_Duplicate( p_frag );
510
511             if( p_frag->i_buffer >= 10 )
512             {
513                 p_sys->b_seq_progressive =
514                     p_frag->p_buffer[5]&0x08 ? VLC_TRUE : VLC_FALSE;
515                 p_sys->b_low_delay =
516                     p_frag->p_buffer[9]&0x80 ? VLC_TRUE : VLC_FALSE;
517             }
518
519             p_dec->fmt_out.video.i_aspect =
520                 mpeg2_aspect[p_sys->i_aspect_ratio_info][0] *
521                 VOUT_ASPECT_FACTOR /
522                 mpeg2_aspect[p_sys->i_aspect_ratio_info][1];
523
524         }
525         else if( i_type == 0x08 )
526         {
527             /* picture extention */
528             p_sys->i_picture_structure = p_frag->p_buffer[6]&0x03;
529             p_sys->i_top_field_first   = p_frag->p_buffer[7] >> 7;
530             p_sys->i_repeat_first_field= (p_frag->p_buffer[7]>>1)&0x01;
531             p_sys->i_progressive_frame = p_frag->p_buffer[8] >> 7;
532         }
533     }
534     else if( p_frag->p_buffer[3] == 0x00 )
535     {
536         /* Picture start code */
537         p_sys->i_seq_old++;
538
539         if( p_frag->i_buffer >= 6 )
540         {
541             p_sys->i_temporal_ref =
542                 ( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6);
543             p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 ) & 0x03;
544         }
545
546         p_sys->i_dts = p_frag->i_dts;
547         p_sys->i_pts = p_frag->i_pts;
548     }
549     else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf )
550     {
551         /* Slice start code */
552         p_sys->b_frame_slice = VLC_TRUE;
553     }
554
555     /* Append the block */
556     block_ChainAppend( &p_sys->p_frame, p_frag );
557
558     return p_pic;
559 }