1 /*****************************************************************************
2 * mpegvideo.c: parse and packetize an MPEG1/2 video stream
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
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>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
28 * Problem with this implementation:
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).
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 *****************************************************************************/
41 /*****************************************************************************
43 *****************************************************************************/
50 #include <vlc_block.h>
51 #include <vlc_codec.h>
52 #include <vlc_block_helper.h>
53 #include "../codec/cc.h"
55 #define SYNC_INTRAFRAME_TEXT N_("Sync on Intra Frame")
56 #define SYNC_INTRAFRAME_LONGTEXT N_("Normally the packetizer would " \
57 "sync on the next full frame. This flags instructs the packetizer " \
58 "to sync on the first Intra Frame found.")
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close( vlc_object_t * );
67 set_category( CAT_SOUT );
68 set_subcategory( SUBCAT_SOUT_PACKETIZER );
69 set_description( _("MPEG-I/II video packetizer") );
70 set_capability( "packetizer", 50 );
71 set_callbacks( Open, Close );
73 add_bool( "packetizer-mpegvideo-sync-iframe", 0, NULL, SYNC_INTRAFRAME_TEXT,
74 SYNC_INTRAFRAME_LONGTEXT, true );
77 /*****************************************************************************
79 *****************************************************************************/
80 static block_t *Packetize( decoder_t *, block_t ** );
81 static block_t *ParseMPEGBlock( decoder_t *, block_t * );
82 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] );
89 block_bytestream_t bytestream;
92 uint8_t p_startcode[3];
94 /* Sequence header and extension */
98 /* Current frame being built */
106 /* Sequence properties */
108 int i_frame_rate_base;
109 bool b_seq_progressive;
111 int i_aspect_ratio_info;
114 /* Picture properties */
117 int i_picture_structure;
118 int i_top_field_first;
119 int i_repeat_first_field;
120 int i_progressive_frame;
122 mtime_t i_interpolated_dts;
123 mtime_t i_last_ref_pts;
126 /* Number of pictures since last sequence header */
130 bool b_sync_on_intra_frame;
131 bool b_discontinuity;
146 /*****************************************************************************
148 *****************************************************************************/
149 static int Open( vlc_object_t *p_this )
151 decoder_t *p_dec = (decoder_t*)p_this;
152 decoder_sys_t *p_sys;
154 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '1' ) &&
155 p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '2' ) &&
156 p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
161 es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC('m','p','g','v') );
162 p_dec->pf_packetize = Packetize;
163 p_dec->pf_get_cc = GetCc;
165 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
168 p_sys->i_state = STATE_NOSYNC;
169 p_sys->bytestream = block_BytestreamInit();
170 p_sys->p_startcode[0] = 0;
171 p_sys->p_startcode[1] = 0;
172 p_sys->p_startcode[2] = 1;
177 p_sys->p_frame = NULL;
178 p_sys->pp_last = &p_sys->p_frame;
179 p_sys->b_frame_slice = false;
181 p_sys->i_dts = p_sys->i_pts = 0;
183 p_sys->i_frame_rate = 1;
184 p_sys->i_frame_rate_base = 1;
185 p_sys->b_seq_progressive = true;
186 p_sys->b_low_delay = true;
187 p_sys->i_seq_old = 0;
189 p_sys->i_temporal_ref = 0;
190 p_sys->i_picture_type = 0;
191 p_sys->i_picture_structure = 0x03; /* frame */
192 p_sys->i_top_field_first = 0;
193 p_sys->i_repeat_first_field = 0;
194 p_sys->i_progressive_frame = 0;
197 p_sys->i_interpolated_dts = 0;
198 p_sys->i_last_ref_pts = 0;
199 p_sys->b_second_field = 0;
201 p_sys->b_discontinuity = false;
202 p_sys->b_sync_on_intra_frame = var_CreateGetBool( p_dec, "packetizer-mpegvideo-sync-iframe" );
203 if( p_sys->b_sync_on_intra_frame )
204 msg_Dbg( p_dec, "syncing on intra frame now" );
206 p_sys->b_cc_reset = false;
209 p_sys->i_cc_flags = 0;
210 cc_Init( &p_sys->cc );
215 /*****************************************************************************
217 *****************************************************************************/
218 static void Close( vlc_object_t *p_this )
220 decoder_t *p_dec = (decoder_t*)p_this;
221 decoder_sys_t *p_sys = p_dec->p_sys;
223 block_BytestreamRelease( &p_sys->bytestream );
227 block_Release( p_sys->p_seq );
231 block_Release( p_sys->p_ext );
235 block_ChainRelease( p_sys->p_frame );
238 var_Destroy( p_dec, "packetizer-mpegvideo-sync-iframe" );
243 /*****************************************************************************
245 *****************************************************************************/
246 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
248 decoder_sys_t *p_sys = p_dec->p_sys;
251 if( pp_block == NULL || *pp_block == NULL )
256 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
258 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
260 p_sys->i_state = STATE_NOSYNC;
261 block_BytestreamFlush( &p_sys->bytestream );
263 p_sys->b_discontinuity = true;
265 block_ChainRelease( p_sys->p_frame );
266 p_sys->p_frame = NULL;
267 p_sys->pp_last = &p_sys->p_frame;
268 p_sys->b_frame_slice = false;
270 // p_sys->i_interpolated_dts =
271 // p_sys->i_last_ref_pts = 0;
273 block_Release( *pp_block );
278 block_BytestreamPush( &p_sys->bytestream, *pp_block );
282 switch( p_sys->i_state )
286 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
287 &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS )
289 p_sys->i_state = STATE_NEXT_SYNC;
292 if( p_sys->i_offset )
294 block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
296 block_BytestreamFlush( &p_sys->bytestream );
299 if( p_sys->i_state != STATE_NEXT_SYNC )
305 p_sys->i_offset = 1; /* To find next startcode */
307 case STATE_NEXT_SYNC:
308 /* TODO: If p_block == NULL, flush the buffer without checking the
311 /* Find the next startcode */
312 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
313 &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS )
319 /* Get the new fragment and set the pts/dts */
320 p_pic = block_New( p_dec, p_sys->i_offset );
321 block_BytestreamFlush( &p_sys->bytestream );
322 p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
323 p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
325 block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
328 /* don't reuse the same timestamps several times */
329 if( p_pic->i_buffer >= 4 && p_pic->p_buffer[3] == 0x00 )
331 /* We have a picture start code */
332 p_sys->bytestream.p_block->i_pts = 0;
333 p_sys->bytestream.p_block->i_dts = 0;
338 /* Get picture if any */
339 if( !( p_pic = ParseMPEGBlock( p_dec, p_pic ) ) )
341 p_sys->i_state = STATE_NOSYNC;
345 /* If a discontinuity has been encountered, then wait till
346 * the next Intra frame before continuing with packetizing */
347 if( p_sys->b_discontinuity &&
348 p_sys->b_sync_on_intra_frame )
350 if( p_pic->i_flags & BLOCK_FLAG_TYPE_I )
352 msg_Dbg( p_dec, "synced on intra frame" );
353 p_sys->b_discontinuity = false;
354 p_pic->i_flags |= BLOCK_FLAG_DISCONTINUITY;
358 msg_Dbg( p_dec, "waiting on intra frame" );
359 p_sys->i_state = STATE_NOSYNC;
360 block_Release( p_pic );
365 /* We've just started the stream, wait for the first PTS.
366 * We discard here so we can still get the sequence header. */
367 if( p_sys->i_dts <= 0 && p_sys->i_pts <= 0 &&
368 p_sys->i_interpolated_dts <= 0 )
370 msg_Dbg( p_dec, "need a starting pts/dts" );
371 p_sys->i_state = STATE_NOSYNC;
372 block_Release( p_pic );
376 /* When starting the stream we can have the first frame with
377 * a null DTS (i_interpolated_pts is initialized to 0) */
378 if( !p_pic->i_dts ) p_pic->i_dts = p_pic->i_pts;
380 /* So p_block doesn't get re-added several times */
381 *pp_block = block_BytestreamPop( &p_sys->bytestream );
383 p_sys->i_state = STATE_NOSYNC;
390 /*****************************************************************************
392 *****************************************************************************/
393 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] )
395 decoder_sys_t *p_sys = p_dec->p_sys;
399 for( i = 0; i < 4; i++ )
400 pb_present[i] = p_sys->cc.pb_present[i];
402 if( p_sys->cc.i_data <= 0 )
405 p_cc = block_New( p_dec, p_sys->cc.i_data);
408 memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
410 p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
411 p_cc->i_flags = ( p_sys->cc.b_reorder ? p_sys->i_cc_flags : BLOCK_FLAG_TYPE_P ) & ( BLOCK_FLAG_TYPE_I|BLOCK_FLAG_TYPE_P|BLOCK_FLAG_TYPE_B);
413 cc_Flush( &p_sys->cc );
417 /*****************************************************************************
418 * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
419 *****************************************************************************/
420 static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
422 decoder_sys_t *p_sys = p_dec->p_sys;
423 block_t *p_pic = NULL;
426 * Check if previous picture is finished
428 if( ( p_sys->b_frame_slice &&
429 (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) &&
430 p_sys->p_seq == NULL )
432 /* We have a picture but without a sequence header we can't
434 msg_Dbg( p_dec, "waiting for sequence start" );
435 if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
436 p_sys->p_frame = NULL;
437 p_sys->pp_last = &p_sys->p_frame;
438 p_sys->b_frame_slice = false;
441 else if( p_sys->b_frame_slice &&
442 (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) )
446 p_pic = block_ChainGather( p_sys->p_frame );
448 i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base /
449 p_sys->i_frame_rate );
451 if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 )
456 if( p_sys->b_seq_progressive )
458 if( p_sys->i_top_field_first == 0 &&
459 p_sys->i_repeat_first_field == 1 )
463 else if( p_sys->i_top_field_first == 1 &&
464 p_sys->i_repeat_first_field == 1 )
471 if( p_sys->i_picture_structure == 0x03 )
473 if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field )
475 i_duration += i_duration / 2;
480 if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
482 /* Trivial case (DTS == PTS) */
483 /* Correct interpolated dts when we receive a new pts/dts */
484 if( p_sys->i_pts > 0 ) p_sys->i_interpolated_dts = p_sys->i_pts;
485 if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
489 /* Correct interpolated dts when we receive a new pts/dts */
490 if( p_sys->i_last_ref_pts > 0 && !p_sys->b_second_field )
491 p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
492 if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
494 if( !p_sys->b_second_field )
495 p_sys->i_last_ref_pts = p_sys->i_pts;
498 p_pic->i_dts = p_sys->i_interpolated_dts;
499 p_sys->i_interpolated_dts += i_duration;
501 /* Set PTS only if we have a B frame or if it comes from the stream */
502 if( p_sys->i_pts > 0 )
504 p_pic->i_pts = p_sys->i_pts;
506 else if( p_sys->i_picture_type == 0x03 )
508 p_pic->i_pts = p_pic->i_dts;
515 switch ( p_sys->i_picture_type )
518 p_pic->i_flags |= BLOCK_FLAG_TYPE_I;
521 p_pic->i_flags |= BLOCK_FLAG_TYPE_P;
524 p_pic->i_flags |= BLOCK_FLAG_TYPE_B;
528 p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts;
531 msg_Dbg( p_dec, "pic: type=%d dts="I64Fd" pts-dts="I64Fd,
532 p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts);
536 p_sys->p_frame = NULL;
537 p_sys->pp_last = &p_sys->p_frame;
538 p_sys->b_frame_slice = false;
540 if( p_sys->i_picture_structure != 0x03 )
542 p_sys->b_second_field = !p_sys->b_second_field;
546 p_sys->b_second_field = 0;
550 p_sys->b_cc_reset = true;
551 p_sys->i_cc_pts = p_pic->i_pts;
552 p_sys->i_cc_dts = p_pic->i_dts;
553 p_sys->i_cc_flags = p_pic->i_flags;
556 if( !p_pic && p_sys->b_cc_reset )
558 p_sys->b_cc_reset = false;
559 cc_Flush( &p_sys->cc );
563 * Check info of current fragment
565 if( p_frag->p_buffer[3] == 0xb8 )
567 /* Group start code */
569 p_sys->i_seq_old > p_sys->i_frame_rate/p_sys->i_frame_rate_base )
571 /* Usefull for mpeg1: repeat sequence header every second */
572 block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_seq ) );
575 block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_ext ) );
578 p_sys->i_seq_old = 0;
581 else if( p_frag->p_buffer[3] == 0xb3 && p_frag->i_buffer >= 8 )
583 /* Sequence header code */
584 static const int code_to_frame_rate[16][2] =
586 { 1, 1 }, /* invalid */
587 { 24000, 1001 }, { 24, 1 }, { 25, 1 }, { 30000, 1001 },
588 { 30, 1 }, { 50, 1 }, { 60000, 1001 }, { 60, 1 },
589 /* Unofficial 15fps from Xing*/
591 /* Unofficial economy rates from libmpeg3 */
592 { 5, 1001 }, { 10, 1001 }, { 12, 1001 }, { 15, 1001 },
593 { 1, 1 }, { 1, 1 } /* invalid */
596 if( p_sys->p_seq ) block_Release( p_sys->p_seq );
597 if( p_sys->p_ext ) block_Release( p_sys->p_ext );
599 p_sys->p_seq = block_Duplicate( p_frag );
600 p_sys->i_seq_old = 0;
603 p_dec->fmt_out.video.i_width =
604 ( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 );
605 p_dec->fmt_out.video.i_height =
606 ( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6];
607 p_sys->i_aspect_ratio_info = p_frag->p_buffer[7] >> 4;
609 /* TODO: MPEG1 aspect ratio */
611 p_sys->i_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0];
612 p_sys->i_frame_rate_base =
613 code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1];
615 p_dec->fmt_out.video.i_frame_rate = p_sys->i_frame_rate;
616 p_dec->fmt_out.video.i_frame_rate_base = p_sys->i_frame_rate_base;
618 p_sys->b_seq_progressive = true;
619 p_sys->b_low_delay = true;
621 if ( !p_sys->b_inited )
623 msg_Dbg( p_dec, "size %dx%d fps=%.3f",
624 p_dec->fmt_out.video.i_width, p_dec->fmt_out.video.i_height,
625 p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base );
629 else if( p_frag->p_buffer[3] == 0xb5 )
631 int i_type = p_frag->p_buffer[4] >> 4;
633 /* Extension start code */
637 static const int mpeg2_aspect[16][2] =
639 {0,1}, {1,1}, {4,3}, {16,9}, {221,100},
640 {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1},
645 /* sequence extension */
646 if( p_sys->p_ext) block_Release( p_sys->p_ext );
647 p_sys->p_ext = block_Duplicate( p_frag );
649 if( p_frag->i_buffer >= 10 )
651 p_sys->b_seq_progressive =
652 p_frag->p_buffer[5]&0x08 ? true : false;
654 p_frag->p_buffer[9]&0x80 ? true : false;
657 /* Do not set aspect ratio : in case we're transcoding,
658 * transcode will take our fmt_out as a fmt_in to libmpeg2.
659 * libmpeg2.c will then believe that the user has requested
660 * a specific aspect ratio, which she hasn't. Thus in case
661 * of aspect ratio change, we're screwed. --Meuuh
664 p_dec->fmt_out.video.i_aspect =
665 mpeg2_aspect[p_sys->i_aspect_ratio_info][0] *
667 mpeg2_aspect[p_sys->i_aspect_ratio_info][1];
671 else if( i_type == 0x08 )
673 /* picture extension */
674 p_sys->i_picture_structure = p_frag->p_buffer[6]&0x03;
675 p_sys->i_top_field_first = p_frag->p_buffer[7] >> 7;
676 p_sys->i_repeat_first_field= (p_frag->p_buffer[7]>>1)&0x01;
677 p_sys->i_progressive_frame = p_frag->p_buffer[8] >> 7;
680 else if( p_frag->p_buffer[3] == 0xb2 && p_frag->i_buffer > 4 )
682 cc_Extract( &p_sys->cc, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
684 else if( p_frag->p_buffer[3] == 0x00 )
686 /* Picture start code */
689 if( p_frag->i_buffer >= 6 )
691 p_sys->i_temporal_ref =
692 ( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6);
693 p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 ) & 0x03;
696 p_sys->i_dts = p_frag->i_dts;
697 p_sys->i_pts = p_frag->i_pts;
699 else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf )
701 /* Slice start code */
702 p_sys->b_frame_slice = true;
705 /* Append the block */
706 block_ChainLastAppend( &p_sys->pp_last, p_frag );