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