/*****************************************************************************
- * mpegvideo.c
+ * mpegvideo.c: parse and packetize an MPEG1/2 video stream
*****************************************************************************
- * Copyright (C) 2001, 2002 VideoLAN
- * $Id: mpegvideo.c,v 1.7 2003/01/22 04:51:16 fenrir Exp $
+ * Copyright (C) 2001-2006 the VideoLAN team
+ * $Id$
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
* Eric Petit <titer@videolan.org>
+ * Gildas Bazin <gbazin@videolan.org>
+ * Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
- * Preamble
+ * Problem with this implementation:
+ *
+ * Although we should time-stamp each picture with a PTS, this isn't possible
+ * with the current implementation.
+ * The problem comes from the fact that for non-low-delay streams we can't
+ * calculate the PTS of pictures used as backward reference. Even the temporal
+ * reference number doesn't help here because all the pictures don't
+ * necessarily have the same duration (eg. 3:2 pulldown).
+ *
+ * However this doesn't really matter as far as the MPEG muxers are concerned
+ * because they allow having empty PTS fields. --gibalou
*****************************************************************************/
-#include <vlc/vlc.h>
-#include <vlc/decoder.h>
-#include <vlc/input.h>
-#include <vlc/sout.h>
-#include "codecs.h" /* WAVEFORMATEX BITMAPINFOHEADER */
-
-#include <stdlib.h> /* malloc(), free() */
-#include <string.h> /* strdup() */
/*****************************************************************************
- * Local prototypes
+ * Preamble
*****************************************************************************/
-typedef struct packetizer_s
-{
- /* Input properties */
- decoder_fifo_t *p_fifo;
- bit_stream_t bit_stream;
-
- /* Output properties */
- sout_input_t *p_sout_input;
- sout_packet_format_t output_format;
- mtime_t i_last_dts;
- mtime_t i_last_ref_pts;
- double d_frame_rate;
- int i_progressive_sequence;
- uint8_t p_sequence_header[150];
- int i_sequence_header_length;
- int i_last_sequence_header;
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
- int i_last_picture_structure;
-} packetizer_t;
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_block.h>
+#include <vlc_codec.h>
+#include <vlc_block_helper.h>
+#include "../codec/cc.h"
-static int Open ( vlc_object_t * );
-static int Run ( decoder_fifo_t * );
-
-static int InitThread ( packetizer_t * );
-static void PacketizeThread ( packetizer_t * );
-static void EndThread ( packetizer_t * );
+#define SYNC_INTRAFRAME_TEXT N_("Sync on Intra Frame")
+#define SYNC_INTRAFRAME_LONGTEXT N_("Normally the packetizer would " \
+ "sync on the next full frame. This flags instructs the packetizer " \
+ "to sync on the first Intra Frame found.")
/*****************************************************************************
* Module descriptor
*****************************************************************************/
+static int Open ( vlc_object_t * );
+static void Close( vlc_object_t * );
vlc_module_begin();
- set_description( _("MPEG-I/II video packetizer") );
+ set_category( CAT_SOUT );
+ set_subcategory( SUBCAT_SOUT_PACKETIZER );
+ set_description( N_("MPEG-I/II video packetizer") );
set_capability( "packetizer", 50 );
- set_callbacks( Open, NULL );
+ set_callbacks( Open, Close );
+
+ add_bool( "packetizer-mpegvideo-sync-iframe", 0, NULL, SYNC_INTRAFRAME_TEXT,
+ SYNC_INTRAFRAME_LONGTEXT, true );
vlc_module_end();
/*****************************************************************************
- * OpenDecoder: probe the packetizer and return score
- *****************************************************************************
- * Tries to launch a decoder and return score so that the interface is able
- * to choose.
+ * Local prototypes
+ *****************************************************************************/
+static block_t *Packetize( decoder_t *, block_t ** );
+static block_t *ParseMPEGBlock( decoder_t *, block_t * );
+static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] );
+
+struct decoder_sys_t
+{
+ /*
+ * Input properties
+ */
+ block_bytestream_t bytestream;
+ int i_state;
+ size_t i_offset;
+ uint8_t p_startcode[3];
+
+ /* Sequence header and extension */
+ block_t *p_seq;
+ block_t *p_ext;
+
+ /* Current frame being built */
+ block_t *p_frame;
+ block_t **pp_last;
+
+ bool b_frame_slice;
+ mtime_t i_pts;
+ mtime_t i_dts;
+
+ /* Sequence properties */
+ int i_frame_rate;
+ int i_frame_rate_base;
+ bool b_seq_progressive;
+ bool b_low_delay;
+ int i_aspect_ratio_info;
+ bool b_inited;
+
+ /* Picture properties */
+ int i_temporal_ref;
+ int i_picture_type;
+ int i_picture_structure;
+ int i_top_field_first;
+ int i_repeat_first_field;
+ int i_progressive_frame;
+
+ mtime_t i_interpolated_dts;
+ mtime_t i_last_ref_pts;
+ bool b_second_field;
+
+ /* Number of pictures since last sequence header */
+ int i_seq_old;
+
+ /* Sync behaviour */
+ bool b_sync_on_intra_frame;
+ bool b_discontinuity;
+
+ /* */
+ bool b_cc_reset;
+ uint32_t i_cc_flags;
+ mtime_t i_cc_pts;
+ mtime_t i_cc_dts;
+ cc_data_t cc;
+};
+
+enum {
+ STATE_NOSYNC,
+ STATE_NEXT_SYNC
+};
+
+/*****************************************************************************
+ * Open:
*****************************************************************************/
static int Open( vlc_object_t *p_this )
{
- decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
+ decoder_t *p_dec = (decoder_t*)p_this;
+ decoder_sys_t *p_sys;
- if( p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', 'v') &&
- p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', '1') &&
- p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', '2') )
+ if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '1' ) &&
+ p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '2' ) &&
+ p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
{
return VLC_EGENERIC;
}
- p_fifo->pf_run = Run;
+ es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC('m','p','g','v') );
+ p_dec->pf_packetize = Packetize;
+ p_dec->pf_get_cc = GetCc;
+
+ p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
+ if( !p_dec->p_sys )
+ return VLC_ENOMEM;
+ memset( p_dec->p_sys, 0, sizeof( decoder_sys_t ) );
+
+ /* Misc init */
+ p_sys->i_state = STATE_NOSYNC;
+ p_sys->bytestream = block_BytestreamInit();
+ p_sys->p_startcode[0] = 0;
+ p_sys->p_startcode[1] = 0;
+ p_sys->p_startcode[2] = 1;
+ p_sys->i_offset = 0;
+
+ p_sys->p_seq = NULL;
+ p_sys->p_ext = NULL;
+ p_sys->p_frame = NULL;
+ p_sys->pp_last = &p_sys->p_frame;
+ p_sys->b_frame_slice = false;
+
+ p_sys->i_dts = p_sys->i_pts = 0;
+
+ p_sys->i_frame_rate = 1;
+ p_sys->i_frame_rate_base = 1;
+ p_sys->b_seq_progressive = true;
+ p_sys->b_low_delay = true;
+ p_sys->i_seq_old = 0;
+
+ p_sys->i_temporal_ref = 0;
+ p_sys->i_picture_type = 0;
+ p_sys->i_picture_structure = 0x03; /* frame */
+ p_sys->i_top_field_first = 0;
+ p_sys->i_repeat_first_field = 0;
+ p_sys->i_progressive_frame = 0;
+ p_sys->b_inited = 0;
+
+ p_sys->i_interpolated_dts = 0;
+ p_sys->i_last_ref_pts = 0;
+ p_sys->b_second_field = 0;
+
+ p_sys->b_discontinuity = false;
+ p_sys->b_sync_on_intra_frame = var_CreateGetBool( p_dec, "packetizer-mpegvideo-sync-iframe" );
+ if( p_sys->b_sync_on_intra_frame )
+ msg_Dbg( p_dec, "syncing on intra frame now" );
+
+ p_sys->b_cc_reset = false;
+ p_sys->i_cc_pts = 0;
+ p_sys->i_cc_dts = 0;
+ p_sys->i_cc_flags = 0;
+ cc_Init( &p_sys->cc );
+
return VLC_SUCCESS;
}
/*****************************************************************************
- * RunDecoder: this function is called just after the thread is created
+ * Close:
*****************************************************************************/
-static int Run( decoder_fifo_t *p_fifo )
+static void Close( vlc_object_t *p_this )
{
- packetizer_t *p_pack;
- int b_error;
+ decoder_t *p_dec = (decoder_t*)p_this;
+ decoder_sys_t *p_sys = p_dec->p_sys;
+
+ block_BytestreamRelease( &p_sys->bytestream );
- msg_Info( p_fifo, "Running mpegvideo packetizer" );
- if( !( p_pack = malloc( sizeof( packetizer_t ) ) ) )
+ if( p_sys->p_seq )
{
- msg_Err( p_fifo, "out of memory" );
- DecoderError( p_fifo );
- return( -1 );
+ block_Release( p_sys->p_seq );
}
- memset( p_pack, 0, sizeof( packetizer_t ) );
-
- p_pack->p_fifo = p_fifo;
-
- if( InitThread( p_pack ) != 0 )
+ if( p_sys->p_ext )
{
- DecoderError( p_fifo );
- return( -1 );
+ block_Release( p_sys->p_ext );
}
-
- while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
+ if( p_sys->p_frame )
{
- PacketizeThread( p_pack );
+ block_ChainRelease( p_sys->p_frame );
}
+ var_Destroy( p_dec, "packetizer-mpegvideo-sync-iframe" );
- if( ( b_error = p_pack->p_fifo->b_error ) )
- {
- DecoderError( p_pack->p_fifo );
- }
+ free( p_sys );
+}
- EndThread( p_pack );
+/*****************************************************************************
+ * Packetize:
+ *****************************************************************************/
+static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
+{
+ decoder_sys_t *p_sys = p_dec->p_sys;
+ block_t *p_pic;
- if( p_pack )
+ if( pp_block == NULL || *pp_block == NULL )
{
- free( p_pack );
+ return NULL;
}
- if( b_error )
+ if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
{
- return( -1 );
+ if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
+ {
+ p_sys->i_state = STATE_NOSYNC;
+ block_BytestreamFlush( &p_sys->bytestream );
+
+ p_sys->b_discontinuity = true;
+ if( p_sys->p_frame )
+ block_ChainRelease( p_sys->p_frame );
+ p_sys->p_frame = NULL;
+ p_sys->pp_last = &p_sys->p_frame;
+ p_sys->b_frame_slice = false;
+ }
+// p_sys->i_interpolated_dts =
+// p_sys->i_last_ref_pts = 0;
+
+ block_Release( *pp_block );
+ return NULL;
}
- return( 0 );
-}
+ block_BytestreamPush( &p_sys->bytestream, *pp_block );
-/*****************************************************************************
- * InitThread: initialize data before entering main loop
- *****************************************************************************/
+ while( 1 )
+ {
+ switch( p_sys->i_state )
+ {
-static int InitThread( packetizer_t *p_pack )
-{
+ case STATE_NOSYNC:
+ if( block_FindStartcodeFromOffset( &p_sys->bytestream,
+ &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS )
+ {
+ p_sys->i_state = STATE_NEXT_SYNC;
+ }
- p_pack->output_format.i_cat = VIDEO_ES;
- p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'v');
+ if( p_sys->i_offset )
+ {
+ block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
+ p_sys->i_offset = 0;
+ block_BytestreamFlush( &p_sys->bytestream );
+ }
- p_pack->p_sout_input = NULL;
+ if( p_sys->i_state != STATE_NEXT_SYNC )
+ {
+ /* Need more data */
+ return NULL;
+ }
- if( InitBitstream( &p_pack->bit_stream, p_pack->p_fifo,
- NULL, NULL ) != VLC_SUCCESS )
- {
- msg_Err( p_pack->p_fifo, "cannot initialize bitstream" );
- return -1;
- }
+ p_sys->i_offset = 1; /* To find next startcode */
- p_pack->i_last_picture_structure = 0x03; /* frame picture */
- return( 0 );
-}
+ case STATE_NEXT_SYNC:
+ /* TODO: If p_block == NULL, flush the buffer without checking the
+ * next sync word */
-/* from ISO 13818-2 */
-/* converting frame_rate_code to frame_rate */
-static const double pd_frame_rates[16] =
-{
- 0, 24000/1001, 24, 25, 30000/1001, 30, 50, 60000/1001, 60,
- 0, 0, 0, 0, 0, 0, 0
-};
+ /* Find the next startcode */
+ if( block_FindStartcodeFromOffset( &p_sys->bytestream,
+ &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS )
+ {
+ /* Need more data */
+ return NULL;
+ }
-static int CopyUntilNextStartCode( packetizer_t *p_pack,
- sout_buffer_t *p_sout_buffer,
- int *pi_pos )
-{
- int i_copy = 0;
+ /* Get the new fragment and set the pts/dts */
+ p_pic = block_New( p_dec, p_sys->i_offset );
+ block_BytestreamFlush( &p_sys->bytestream );
+ p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
+ p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
- do
- {
- p_sout_buffer->p_buffer[(*pi_pos)++] =
- GetBits( &p_pack->bit_stream, 8 );
- i_copy++;
+ block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
+ p_pic->i_buffer );
- if( *pi_pos + 2048 > p_sout_buffer->i_buffer_size )
- {
- sout_BufferRealloc( p_pack->p_sout_input->p_sout,
- p_sout_buffer,
- p_sout_buffer->i_buffer_size + 50 * 1024);
+ /* don't reuse the same timestamps several times */
+ if( p_pic->i_buffer >= 4 && p_pic->p_buffer[3] == 0x00 )
+ {
+ /* We have a picture start code */
+ p_sys->bytestream.p_block->i_pts = 0;
+ p_sys->bytestream.p_block->i_dts = 0;
+ }
+
+ p_sys->i_offset = 0;
+
+ /* Get picture if any */
+ if( !( p_pic = ParseMPEGBlock( p_dec, p_pic ) ) )
+ {
+ p_sys->i_state = STATE_NOSYNC;
+ break;
+ }
+
+ /* If a discontinuity has been encountered, then wait till
+ * the next Intra frame before continuing with packetizing */
+ if( p_sys->b_discontinuity &&
+ p_sys->b_sync_on_intra_frame )
+ {
+ if( p_pic->i_flags & BLOCK_FLAG_TYPE_I )
+ {
+ msg_Dbg( p_dec, "synced on intra frame" );
+ p_sys->b_discontinuity = false;
+ p_pic->i_flags |= BLOCK_FLAG_DISCONTINUITY;
+ }
+ else
+ {
+ msg_Dbg( p_dec, "waiting on intra frame" );
+ p_sys->i_state = STATE_NOSYNC;
+ block_Release( p_pic );
+ break;
+ }
+ }
+
+ /* We've just started the stream, wait for the first PTS.
+ * We discard here so we can still get the sequence header. */
+ if( p_sys->i_dts <= 0 && p_sys->i_pts <= 0 &&
+ p_sys->i_interpolated_dts <= 0 )
+ {
+ msg_Dbg( p_dec, "need a starting pts/dts" );
+ p_sys->i_state = STATE_NOSYNC;
+ block_Release( p_pic );
+ break;
+ }
+
+ /* When starting the stream we can have the first frame with
+ * a null DTS (i_interpolated_pts is initialized to 0) */
+ if( !p_pic->i_dts ) p_pic->i_dts = p_pic->i_pts;
+
+ /* So p_block doesn't get re-added several times */
+ *pp_block = block_BytestreamPop( &p_sys->bytestream );
+
+ p_sys->i_state = STATE_NOSYNC;
+
+ return p_pic;
}
+ }
+}
+
+/*****************************************************************************
+ * GetCc:
+ *****************************************************************************/
+static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] )
+{
+ decoder_sys_t *p_sys = p_dec->p_sys;
+ block_t *p_cc;
+ int i;
- } while( ShowBits( &p_pack->bit_stream, 24 ) != 0x01 &&
- !p_pack->p_fifo->b_die && !p_pack->p_fifo->b_error );
+ for( i = 0; i < 4; i++ )
+ pb_present[i] = p_sys->cc.pb_present[i];
- return( i_copy );
+ if( p_sys->cc.i_data <= 0 )
+ return NULL;
+
+ p_cc = block_New( p_dec, p_sys->cc.i_data);
+ if( p_cc )
+ {
+ memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
+ p_cc->i_dts =
+ p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
+ 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);
+ }
+ cc_Flush( &p_sys->cc );
+ return p_cc;
}
/*****************************************************************************
- * PacketizeThread: packetize an unit (here copy a complete pes)
+ * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
*****************************************************************************/
-static void PacketizeThread( packetizer_t *p_pack )
+static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
{
- sout_buffer_t *p_sout_buffer = NULL;
- int32_t i_pos;
- int i_skipped;
- mtime_t i_duration; /* of the parsed picture */
-
- /* needed to calculate pts/dts */
- int i_temporal_ref = 0;
- int i_picture_structure = 0x03; /* frame picture */
- int i_top_field_first = 0;
- int i_repeat_first_field = 0;
-
- if( !p_pack->p_sout_input )
+ decoder_sys_t *p_sys = p_dec->p_sys;
+ block_t *p_pic = NULL;
+
+ /*
+ * Check if previous picture is finished
+ */
+ if( ( p_sys->b_frame_slice &&
+ (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) &&
+ p_sys->p_seq == NULL )
+ {
+ /* We have a picture but without a sequence header we can't
+ * do anything */
+ msg_Dbg( p_dec, "waiting for sequence start" );
+ if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
+ p_sys->p_frame = NULL;
+ p_sys->pp_last = &p_sys->p_frame;
+ p_sys->b_frame_slice = false;
+
+ }
+ else if( p_sys->b_frame_slice &&
+ (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) )
{
- byte_t p_temp[512];/* 150 bytes is the maximal size
- of a sequence_header + sequence_extension */
- int i_frame_rate_code;
- BITMAPINFOHEADER *p_bih;
-
- p_bih = malloc( sizeof( BITMAPINFOHEADER ) );
- p_pack->output_format.p_format = (void*)p_bih;
-
- /* skip data until we find a sequence_header_code */
- /* TODO: store skipped somewhere so can send it to the mux
- * after the input is created */
- i_skipped = 0;
- while( ShowBits( &p_pack->bit_stream, 32 ) != 0x1B3 )
+ mtime_t i_duration;
+
+ p_pic = block_ChainGather( p_sys->p_frame );
+
+ i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base /
+ p_sys->i_frame_rate );
+
+ if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 )
{
- RemoveBits( &p_pack->bit_stream, 8 );
- i_skipped++;
+ i_duration /= 2;
}
- msg_Warn( p_pack->p_fifo, "sequence_header_code found (%d skipped)",
- i_skipped );
-
- /* copy the start_code */
- i_pos = 0;
- GetChunk( &p_pack->bit_stream, p_temp, 4 ); i_pos += 4;
-
- p_bih->biSize = sizeof( BITMAPINFOHEADER );
- /* horizontal_size_value */
- p_bih->biWidth = ShowBits( &p_pack->bit_stream, 12 );
- /* vertical_size_value */
- p_bih->biHeight = ShowBits( &p_pack->bit_stream, 24 ) & 0xFFF;
- /* frame_rate_code */
- i_frame_rate_code = ShowBits( &p_pack->bit_stream, 32 ) & 0xF;
- p_bih->biPlanes = 1;
- p_bih->biBitCount = 0;
- p_bih->biCompression = VLC_FOURCC( 'm', 'p', 'g', '2' );
- p_bih->biSizeImage = 0;
- p_bih->biXPelsPerMeter = 0;
- p_bih->biYPelsPerMeter = 0;
- p_bih->biClrUsed = 0;
- p_bih->biClrImportant = 0;
-
- /* copy headers */
- GetChunk( &p_pack->bit_stream, p_temp + i_pos, 7 ); i_pos += 7;
-
- /* intra_quantiser_matrix [non_intra_quantiser_matrix] */
- if( ShowBits( &p_pack->bit_stream, 7 ) & 0x1 )
- {
- GetChunk( &p_pack->bit_stream, p_temp + i_pos, 64 ); i_pos += 64;
-
- if( ShowBits( &p_pack->bit_stream, 8 ) & 0x1 )
+ if( p_sys->b_seq_progressive )
+ {
+ if( p_sys->i_top_field_first == 0 &&
+ p_sys->i_repeat_first_field == 1 )
+ {
+ i_duration *= 2;
+ }
+ else if( p_sys->i_top_field_first == 1 &&
+ p_sys->i_repeat_first_field == 1 )
+ {
+ i_duration *= 3;
+ }
+ }
+ else
+ {
+ if( p_sys->i_picture_structure == 0x03 )
{
- GetChunk( &p_pack->bit_stream, p_temp + i_pos, 65); i_pos += 65;
+ if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field )
+ {
+ i_duration += i_duration / 2;
+ }
}
}
- /* non_intra_quantiser_matrix */
- else if( ShowBits( &p_pack->bit_stream, 8 ) & 0x1 )
+
+ if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
{
- GetChunk( &p_pack->bit_stream, p_temp + i_pos, 65); i_pos += 65;
+ /* Trivial case (DTS == PTS) */
+ /* Correct interpolated dts when we receive a new pts/dts */
+ if( p_sys->i_pts > 0 ) p_sys->i_interpolated_dts = p_sys->i_pts;
+ if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
}
- /* nothing */
else
{
- GetChunk( &p_pack->bit_stream, p_temp + i_pos, 1 ); i_pos += 1;
+ /* Correct interpolated dts when we receive a new pts/dts */
+ if( p_sys->i_last_ref_pts > 0 && !p_sys->b_second_field )
+ p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
+ if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
+
+ if( !p_sys->b_second_field )
+ p_sys->i_last_ref_pts = p_sys->i_pts;
}
- /* sequence_extension (10 bytes) */
- if( ShowBits( &p_pack->bit_stream, 32 ) != 0x1B5 )
+ p_pic->i_dts = p_sys->i_interpolated_dts;
+ p_sys->i_interpolated_dts += i_duration;
+
+ /* Set PTS only if we have a B frame or if it comes from the stream */
+ if( p_sys->i_pts > 0 )
{
- msg_Dbg( p_pack->p_fifo, "ARRGG no extension_start_code" );
- p_pack->i_progressive_sequence = 1;
+ p_pic->i_pts = p_sys->i_pts;
+ }
+ else if( p_sys->i_picture_type == 0x03 )
+ {
+ p_pic->i_pts = p_pic->i_dts;
}
else
{
- GetChunk( &p_pack->bit_stream, p_temp + i_pos, 10 );
- p_pack->i_progressive_sequence = ( p_temp[i_pos+5]&0x08 ) ? 1 : 0;
-
- i_pos += 10;
+ p_pic->i_pts = 0;
}
- /* remember sequence_header and sequence_extention */
- memcpy( p_pack->p_sequence_header, p_temp, i_pos );
- p_pack->i_sequence_header_length = i_pos;
+ switch ( p_sys->i_picture_type )
+ {
+ case 0x01:
+ p_pic->i_flags |= BLOCK_FLAG_TYPE_I;
+ break;
+ case 0x02:
+ p_pic->i_flags |= BLOCK_FLAG_TYPE_P;
+ break;
+ case 0x03:
+ p_pic->i_flags |= BLOCK_FLAG_TYPE_B;
+ break;
+ }
- p_pack->d_frame_rate = pd_frame_rates[i_frame_rate_code];
+ p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts;
- msg_Warn( p_pack->p_fifo,
- "creating input (image size %dx%d, frame rate %.2f)",
- p_bih->biWidth, p_bih->biHeight, p_pack->d_frame_rate );
+#if 0
+ msg_Dbg( p_dec, "pic: type=%d dts=%"PRId64" pts-dts=%"PRId64,
+ p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts);
+#endif
- /* now we have informations to create the input */
- p_pack->p_sout_input = sout_InputNew( p_pack->p_fifo,
- &p_pack->output_format );
+ /* Reset context */
+ p_sys->p_frame = NULL;
+ p_sys->pp_last = &p_sys->p_frame;
+ p_sys->b_frame_slice = false;
- if( !p_pack->p_sout_input )
+ if( p_sys->i_picture_structure != 0x03 )
{
- msg_Err( p_pack->p_fifo, "cannot add a new stream" );
- return;
+ p_sys->b_second_field = !p_sys->b_second_field;
+ }
+ else
+ {
+ p_sys->b_second_field = 0;
}
- p_sout_buffer = sout_BufferNew( p_pack->p_sout_input->p_sout,
- 100 * 1024 );
- p_sout_buffer->i_size = i_pos;
- memcpy( p_sout_buffer->p_buffer, p_temp, i_pos );
-
+ /* CC */
+ p_sys->b_cc_reset = true;
+ p_sys->i_cc_pts = p_pic->i_pts;
+ p_sys->i_cc_dts = p_pic->i_dts;
+ p_sys->i_cc_flags = p_pic->i_flags;
}
- else
+
+ if( !p_pic && p_sys->b_cc_reset )
{
- p_sout_buffer =
- sout_BufferNew( p_pack->p_sout_input->p_sout, 100 * 1024 );
- i_pos = 0;
+ p_sys->b_cc_reset = false;
+ cc_Flush( &p_sys->cc );
}
- p_pack->i_last_sequence_header++;
-
- for( ;; )
+ /*
+ * Check info of current fragment
+ */
+ if( p_frag->p_buffer[3] == 0xb8 )
{
- uint32_t i_code;
- if( p_pack->p_fifo->b_die || p_pack->p_fifo->b_error )
- {
- break;
- }
-
- i_code = ShowBits( &p_pack->bit_stream, 32 );
-
- if( i_code == 0x1B8 ) /* GOP */
+ /* Group start code */
+ if( p_sys->p_seq &&
+ p_sys->i_seq_old > p_sys->i_frame_rate/p_sys->i_frame_rate_base )
{
- /* usefull for bad MPEG-1 : repeat the sequence_header
- every second */
- if( p_pack->i_last_sequence_header > (int) p_pack->d_frame_rate )
+ /* Usefull for mpeg1: repeat sequence header every second */
+ block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_seq ) );
+ if( p_sys->p_ext )
{
- memcpy( p_sout_buffer->p_buffer + i_pos,
- p_pack->p_sequence_header,
- p_pack->i_sequence_header_length );
- i_pos += p_pack->i_sequence_header_length;
- p_pack->i_last_sequence_header = 0;
+ block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_ext ) );
}
- p_pack->i_last_ref_pts =
- p_pack->i_last_dts + 40000; /* FIXME */
- CopyUntilNextStartCode( p_pack, p_sout_buffer, &i_pos );
+ p_sys->i_seq_old = 0;
}
- else if( i_code == 0x100 ) /* Picture */
+ }
+ else if( p_frag->p_buffer[3] == 0xb3 && p_frag->i_buffer >= 8 )
+ {
+ /* Sequence header code */
+ static const int code_to_frame_rate[16][2] =
{
- /* picture_start_code */
- GetChunk( &p_pack->bit_stream,
- p_sout_buffer->p_buffer + i_pos, 4 ); i_pos += 4;
- i_temporal_ref = ShowBits( &p_pack->bit_stream, 10 );
-
- CopyUntilNextStartCode( p_pack, p_sout_buffer, &i_pos );
- break;
+ { 1, 1 }, /* invalid */
+ { 24000, 1001 }, { 24, 1 }, { 25, 1 }, { 30000, 1001 },
+ { 30, 1 }, { 50, 1 }, { 60000, 1001 }, { 60, 1 },
+ /* Unofficial 15fps from Xing*/
+ { 15, 1001 },
+ /* Unofficial economy rates from libmpeg3 */
+ { 5, 1001 }, { 10, 1001 }, { 12, 1001 }, { 15, 1001 },
+ { 1, 1 }, { 1, 1 } /* invalid */
+ };
+
+ if( p_sys->p_seq ) block_Release( p_sys->p_seq );
+ if( p_sys->p_ext ) block_Release( p_sys->p_ext );
+
+ p_sys->p_seq = block_Duplicate( p_frag );
+ p_sys->i_seq_old = 0;
+ p_sys->p_ext = NULL;
+
+ p_dec->fmt_out.video.i_width =
+ ( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 );
+ p_dec->fmt_out.video.i_height =
+ ( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6];
+ p_sys->i_aspect_ratio_info = p_frag->p_buffer[7] >> 4;
+
+ /* TODO: MPEG1 aspect ratio */
+
+ p_sys->i_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0];
+ p_sys->i_frame_rate_base =
+ code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1];
+
+ p_dec->fmt_out.video.i_frame_rate = p_sys->i_frame_rate;
+ p_dec->fmt_out.video.i_frame_rate_base = p_sys->i_frame_rate_base;
+
+ p_sys->b_seq_progressive = true;
+ p_sys->b_low_delay = true;
+
+ if ( !p_sys->b_inited )
+ {
+ msg_Dbg( p_dec, "size %dx%d fps=%.3f",
+ p_dec->fmt_out.video.i_width, p_dec->fmt_out.video.i_height,
+ p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base );
+ p_sys->b_inited = 1;
}
- else if( i_code == 0x1b5 )
+ }
+ else if( p_frag->p_buffer[3] == 0xb5 )
+ {
+ int i_type = p_frag->p_buffer[4] >> 4;
+
+ /* Extension start code */
+ if( i_type == 0x01 )
{
- /* extention start code 32 */
- GetChunk( &p_pack->bit_stream,
- p_sout_buffer->p_buffer + i_pos, 4 ); i_pos += 4;
+#if 0
+ static const int mpeg2_aspect[16][2] =
+ {
+ {0,1}, {1,1}, {4,3}, {16,9}, {221,100},
+ {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1},
+ {0,1}, {0,1}
+ };
+#endif
+
+ /* sequence extension */
+ if( p_sys->p_ext) block_Release( p_sys->p_ext );
+ p_sys->p_ext = block_Duplicate( p_frag );
- if( ShowBits( &p_pack->bit_stream, 4 ) == 0x08 )
+ if( p_frag->i_buffer >= 10 )
{
- /* picture coding extention */
-
- /* extention start code identifier(b1000) 4 */
- /* f_code[2][2] 16 */
- /* intra_dc_precision 2 */
- /* picture_structure 2 */
- /* top_field_first 1 */
- i_picture_structure =
- ShowBits( &p_pack->bit_stream, 24 ) & 0x03;
- i_top_field_first =
- ShowBits( &p_pack->bit_stream, 25 ) & 0x01;
- i_repeat_first_field =
- ShowBits( &p_pack->bit_stream, 31 ) & 0x01;
+ p_sys->b_seq_progressive =
+ p_frag->p_buffer[5]&0x08 ? true : false;
+ p_sys->b_low_delay =
+ p_frag->p_buffer[9]&0x80 ? true : false;
}
- CopyUntilNextStartCode( p_pack, p_sout_buffer, &i_pos );
+
+ /* Do not set aspect ratio : in case we're transcoding,
+ * transcode will take our fmt_out as a fmt_in to libmpeg2.
+ * libmpeg2.c will then believe that the user has requested
+ * a specific aspect ratio, which she hasn't. Thus in case
+ * of aspect ratio change, we're screwed. --Meuuh
+ */
+#if 0
+ p_dec->fmt_out.video.i_aspect =
+ mpeg2_aspect[p_sys->i_aspect_ratio_info][0] *
+ VOUT_ASPECT_FACTOR /
+ mpeg2_aspect[p_sys->i_aspect_ratio_info][1];
+#endif
+
}
- else
+ else if( i_type == 0x08 )
{
- if( i_code == 0x1B3 )
- {
- p_pack->i_last_sequence_header = 0;
- }
- CopyUntilNextStartCode( p_pack, p_sout_buffer, &i_pos );
+ /* picture extension */
+ p_sys->i_picture_structure = p_frag->p_buffer[6]&0x03;
+ p_sys->i_top_field_first = p_frag->p_buffer[7] >> 7;
+ p_sys->i_repeat_first_field= (p_frag->p_buffer[7]>>1)&0x01;
+ p_sys->i_progressive_frame = p_frag->p_buffer[8] >> 7;
}
}
-
- sout_BufferRealloc( p_pack->p_sout_input->p_sout,
- p_sout_buffer, i_pos );
- p_sout_buffer->i_size = i_pos;
-
- /* calculate dts/pts */
- if( p_pack->i_progressive_sequence || i_picture_structure == 0x03)
+ else if( p_frag->p_buffer[3] == 0xb2 && p_frag->i_buffer > 4 )
{
- i_duration = (mtime_t)( 1000000 / p_pack->d_frame_rate );
+ cc_Extract( &p_sys->cc, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
}
- else
+ else if( p_frag->p_buffer[3] == 0x00 )
{
- i_duration = (mtime_t)( 1000000 / p_pack->d_frame_rate / 2);
- }
+ /* Picture start code */
+ p_sys->i_seq_old++;
- p_sout_buffer->i_dts = p_pack->i_last_dts;
- p_sout_buffer->i_pts = p_pack->i_last_ref_pts +
- i_temporal_ref * (mtime_t)( 1000000 / p_pack->d_frame_rate );
- p_sout_buffer->i_length = i_duration;
- p_sout_buffer->i_bitrate = (int)( 8 * i_pos * p_pack->d_frame_rate );
- sout_InputSendBuffer( p_pack->p_sout_input, p_sout_buffer );
-
- if( p_pack->i_progressive_sequence )
- {
- if( i_top_field_first == 0 && i_repeat_first_field == 0 )
- {
- p_pack->i_last_dts += i_duration;
- }
- else if( i_top_field_first == 0 && i_repeat_first_field == 1 )
- {
- p_pack->i_last_dts += 2 * i_duration;
- }
- else if( i_top_field_first == 1 && i_repeat_first_field == 1 )
+ if( p_frag->i_buffer >= 6 )
{
- p_pack->i_last_dts += 3 * i_duration;
+ p_sys->i_temporal_ref =
+ ( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6);
+ p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 ) & 0x03;
}
+
+ p_sys->i_dts = p_frag->i_dts;
+ p_sys->i_pts = p_frag->i_pts;
}
- else
+ else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf )
{
- if( i_picture_structure == 0x03 )
- {
- p_pack->i_last_dts += i_duration;
- }
- else if( i_picture_structure == p_pack->i_last_picture_structure )
- {
- p_pack->i_last_dts += 2 * i_duration;
- }
- else if( ( !i_top_field_first && i_picture_structure == 0x01 ) ||
- ( i_top_field_first && i_picture_structure == 0x02 ) )
- {
- p_pack->i_last_dts += 2 * i_duration;
- }
+ /* Slice start code */
+ p_sys->b_frame_slice = true;
}
- p_pack->i_last_picture_structure = i_picture_structure;
-}
+ /* Append the block */
+ block_ChainLastAppend( &p_sys->pp_last, p_frag );
-/*****************************************************************************
- * EndThread : packetizer thread destruction
- *****************************************************************************/
-static void EndThread ( packetizer_t *p_pack)
-{
- if( p_pack->p_sout_input )
- {
- sout_InputDelete( p_pack->p_sout_input );
- }
+ return p_pic;
}