Commit 1c332603 authored by Laurent Aimar's avatar Laurent Aimar

* mpeg4audio.c: new packetizer (but for now it don't support adts aac)

 * mpeg4video.c: I 've rewritten it
parent 8c45a841
...@@ -3,3 +3,4 @@ SOURCES_packetizer_a52 = modules/packetizer/a52.c ...@@ -3,3 +3,4 @@ SOURCES_packetizer_a52 = modules/packetizer/a52.c
SOURCES_packetizer_mpegaudio = modules/packetizer/mpegaudio.c SOURCES_packetizer_mpegaudio = modules/packetizer/mpegaudio.c
SOURCES_packetizer_mpegvideo = modules/packetizer/mpegvideo.c SOURCES_packetizer_mpegvideo = modules/packetizer/mpegvideo.c
SOURCES_packetizer_mpeg4video = modules/packetizer/mpeg4video.c SOURCES_packetizer_mpeg4video = modules/packetizer/mpeg4video.c
SOURCES_packetizer_mpeg4audio = modules/packetizer/mpeg4audio.c
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* copy.c * copy.c
***************************************************************************** *****************************************************************************
* Copyright (C) 2001, 2002 VideoLAN * Copyright (C) 2001, 2002 VideoLAN
* $Id: copy.c,v 1.1 2002/12/14 21:32:41 fenrir Exp $ * $Id: copy.c,v 1.2 2003/01/08 10:26:49 fenrir Exp $
* *
* Authors: Laurent Aimar <fenrir@via.ecp.fr> * Authors: Laurent Aimar <fenrir@via.ecp.fr>
* Eric Petit <titer@videolan.org> * Eric Petit <titer@videolan.org>
...@@ -46,6 +46,8 @@ typedef struct packetizer_thread_s ...@@ -46,6 +46,8 @@ typedef struct packetizer_thread_s
sout_input_t *p_sout_input; sout_input_t *p_sout_input;
sout_packet_format_t output_format; sout_packet_format_t output_format;
mtime_t i_pts_start;
} packetizer_thread_t; } packetizer_thread_t;
static int Open ( vlc_object_t * ); static int Open ( vlc_object_t * );
...@@ -55,6 +57,9 @@ static int InitThread ( packetizer_thread_t * ); ...@@ -55,6 +57,9 @@ static int InitThread ( packetizer_thread_t * );
static void PacketizeThread ( packetizer_thread_t * ); static void PacketizeThread ( packetizer_thread_t * );
static void EndThread ( packetizer_thread_t * ); static void EndThread ( packetizer_thread_t * );
static void input_ShowPES( decoder_fifo_t *p_fifo, pes_packet_t **pp_pes );
/***************************************************************************** /*****************************************************************************
* Module descriptor * Module descriptor
*****************************************************************************/ *****************************************************************************/
...@@ -141,20 +146,44 @@ static int Run( decoder_fifo_t *p_fifo ) ...@@ -141,20 +146,44 @@ static int Run( decoder_fifo_t *p_fifo )
static int InitThread( packetizer_thread_t *p_pack ) static int InitThread( packetizer_thread_t *p_pack )
{ {
// p_pack->output_format.i_cat = p_pack->p_fifo->i_cat; switch( p_pack->p_fifo->i_fourcc )
p_pack->output_format.i_fourcc = p_pack->p_fifo->i_fourcc; {
case VLC_FOURCC( 'm', 'p', '4', 'v'):
case VLC_FOURCC( 'D', 'I', 'V', 'X'):
case VLC_FOURCC( 'd', 'i', 'v', 'x'):
case VLC_FOURCC( 'X', 'V', 'I', 'D'):
case VLC_FOURCC( 'X', 'v', 'i', 'D'):
case VLC_FOURCC( 'x', 'v', 'i', 'd'):
case VLC_FOURCC( 'D', 'X', '5', '0'):
p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'v');
p_pack->output_format.i_cat = VIDEO_ES;
break;
case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
case VLC_FOURCC( 'm', 'p', 'g', '1' ):
case VLC_FOURCC( 'm', 'p', 'g', '2' ):
p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'v' );
p_pack->output_format.i_cat = VIDEO_ES;
break;
case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
p_pack->output_format.i_cat = AUDIO_ES;
break;
default:
p_pack->output_format.i_fourcc = p_pack->p_fifo->i_fourcc;
p_pack->output_format.i_cat = UNKNOWN_ES;
break;
}
p_pack->p_sout_input = p_pack->p_sout_input =
sout_InputNew( p_pack->p_fifo, sout_InputNew( p_pack->p_fifo,
&p_pack->output_format ); &p_pack->output_format );
if( !p_pack->p_sout_input ) if( !p_pack->p_sout_input )
{ {
msg_Err( p_pack->p_fifo, msg_Err( p_pack->p_fifo, "cannot add a new stream" );
"cannot add a new stream" );
return( -1 ); return( -1 );
} }
p_pack->i_pts_start = -1;
return( 0 ); return( 0 );
} }
...@@ -174,10 +203,15 @@ static void PacketizeThread( packetizer_thread_t *p_pack ) ...@@ -174,10 +203,15 @@ static void PacketizeThread( packetizer_thread_t *p_pack )
p_pack->p_fifo->b_error = 1; p_pack->p_fifo->b_error = 1;
return; return;
} }
if( p_pack->i_pts_start < 0 && p_pes->i_pts > 0 )
{
p_pack->i_pts_start = p_pes->i_pts;
}
i_size = p_pes->i_pes_size; i_size = p_pes->i_pes_size;
// msg_Dbg( p_pack->p_fifo, "pes size:%d", i_size );
if( i_size > 0 ) if( i_size > 0 )
{ {
pes_packet_t *p_pes_next;
data_packet_t *p_data; data_packet_t *p_data;
size_t i_buffer; size_t i_buffer;
...@@ -205,7 +239,16 @@ static void PacketizeThread( packetizer_thread_t *p_pack ) ...@@ -205,7 +239,16 @@ static void PacketizeThread( packetizer_thread_t *p_pack )
} }
i_buffer += i_copy; i_buffer += i_copy;
} }
p_sout_buffer->i_length = 0;
p_sout_buffer->i_dts = p_pes->i_pts - p_pack->i_pts_start;
p_sout_buffer->i_pts = p_pes->i_pts - p_pack->i_pts_start;
p_sout_buffer->i_bitrate = 0;
input_ShowPES( p_pack->p_fifo, &p_pes_next );
if( p_pes_next )
{
p_sout_buffer->i_length = p_pes_next->i_pts - p_pes->i_pts;
}
sout_InputSendBuffer( p_pack->p_sout_input, sout_InputSendBuffer( p_pack->p_sout_input,
p_sout_buffer ); p_sout_buffer );
} }
...@@ -225,3 +268,35 @@ static void EndThread ( packetizer_thread_t *p_pack) ...@@ -225,3 +268,35 @@ static void EndThread ( packetizer_thread_t *p_pack)
} }
} }
static void input_ShowPES( decoder_fifo_t *p_fifo, pes_packet_t **pp_pes )
{
pes_packet_t *p_pes;
vlc_mutex_lock( &p_fifo->data_lock );
if( p_fifo->p_first == NULL )
{
if( p_fifo->b_die )
{
vlc_mutex_unlock( &p_fifo->data_lock );
if( pp_pes ) *pp_pes = NULL;
return;
}
/* Signal the input thread we're waiting. This is only
* needed in case of slave clock (ES plug-in) but it won't
* harm. */
vlc_cond_signal( &p_fifo->data_wait );
/* Wait for the input to tell us when we received a packet. */
vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
}
p_pes = p_fifo->p_first;
vlc_mutex_unlock( &p_fifo->data_lock );
if( pp_pes )
{
*pp_pes = p_pes;
}
}
/*****************************************************************************
* mpeg4audio.c
*****************************************************************************
* Copyright (C) 2001, 2002 VideoLAN
* $Id: mpeg4audio.c,v 1.1 2003/01/08 10:26:49 fenrir Exp $
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#include <vlc/vlc.h>
#include <vlc/aout.h>
#include <vlc/decoder.h>
#include <vlc/input.h>
#include <vlc/sout.h>
#include "codecs.h"
#include <stdlib.h> /* malloc(), free() */
#include <string.h> /* strdup() */
/*****************************************************************************
* Local prototypes
*****************************************************************************/
/* AAC Config in ES:
*
* AudioObjectType 5 bits
* samplingFrequencyIndex 4 bits
* if (samplingFrequencyIndex == 0xF)
* samplingFrequency 24 bits
* channelConfiguration 4 bits
* GA_SpecificConfig
* FrameLengthFlag 1 bit 1024 or 960
* DependsOnCoreCoder 1 bit (always 0)
* ExtensionFlag 1 bit (always 0)
*/
typedef struct packetizer_thread_s
{
/* Input properties */
int b_adts;
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_pts_start;
mtime_t i_pts;
WAVEFORMATEX *p_wf;
/* Extracted from AAC config */
int i_sample_rate;
int i_frame_size; // 1024 or 960
} packetizer_thread_t;
static int Open ( vlc_object_t * );
static int Run ( decoder_fifo_t * );
static int InitThread ( packetizer_thread_t * );
static void PacketizeThreadMPEG4 ( packetizer_thread_t * );
static void PacketizeThreadADTS ( packetizer_thread_t * );
static void EndThread ( packetizer_thread_t * );
/*****************************************************************************
* Module descriptor
*****************************************************************************/
vlc_module_begin();
set_description( _("MPEG4 Audio packetizer") );
set_capability( "packetizer", 50 );
set_callbacks( Open, NULL );
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.
*****************************************************************************/
static int Open( vlc_object_t *p_this )
{
decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
p_fifo->pf_run = Run;
if( p_fifo->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'a') )
{
return( VLC_SUCCESS );
}
else
{
return( VLC_EGENERIC );
}
}
/*****************************************************************************
* RunDecoder: this function is called just after the thread is created
*****************************************************************************/
static int Run( decoder_fifo_t *p_fifo )
{
packetizer_thread_t *p_pack;
int b_error;
msg_Info( p_fifo, "Running MPEG4 audio packetizer" );
if( !( p_pack = malloc( sizeof( packetizer_thread_t ) ) ) )
{
msg_Err( p_fifo, "out of memory" );
DecoderError( p_fifo );
return( -1 );
}
memset( p_pack, 0, sizeof( packetizer_thread_t ) );
p_pack->p_fifo = p_fifo;
if( InitThread( p_pack ) != 0 )
{
DecoderError( p_fifo );
return( -1 );
}
while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
{
if( p_pack->b_adts )
{
PacketizeThreadADTS( p_pack );
}
else
{
PacketizeThreadMPEG4( p_pack );
}
}
if( ( b_error = p_pack->p_fifo->b_error ) )
{
DecoderError( p_pack->p_fifo );
}
EndThread( p_pack );
if( b_error )
{
return( -1 );
}
return( 0 );
}
#define FREE( p ) if( p ) free( p ); p = NULL
/*****************************************************************************
* InitThread: initialize data before entering main loop
*****************************************************************************/
static int i_sample_rates[] =
{
96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
16000, 12000, 11025, 8000, 7350, 0, 0, 0
};
static int InitThread( packetizer_thread_t *p_pack )
{
WAVEFORMATEX *p_wf;
p_wf = (WAVEFORMATEX*)p_pack->p_fifo->p_waveformatex;
if( p_wf && p_wf->cbSize > 0)
{
uint8_t *p_config = (uint8_t*)&p_wf[1];
int i_wf = sizeof( WAVEFORMATEX ) + p_wf->cbSize;
int i_index;
p_pack->p_wf = malloc( i_wf );
memcpy( p_pack->p_wf,
p_wf,
i_wf );
p_pack->output_format.i_cat = AUDIO_ES;
p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
p_pack->output_format.p_format = p_pack->p_wf;
p_pack->b_adts = 0;
i_index = ( ( p_config[0] << 1 ) | ( p_config[1] >> 7 ) )&0x0f;
if( i_index != 0x0f )
{
p_pack->i_sample_rate = i_sample_rates[i_index];
p_pack->i_frame_size = ( ( p_config[1] >> 2 )&0x01 ) ? 960 : 1024;
}
else
{
p_pack->i_sample_rate = ( ( p_config[1]&0x7f ) << 17 ) | ( p_config[2] << 9 )|
( p_config[3] << 1 ) | ( p_config[4] >> 7 );
p_pack->i_frame_size = ( ( p_config[4] >> 2 )&0x01 ) ? 960 : 1024;
}
msg_Dbg( p_pack->p_fifo,
"aac %dHz %d samples/frame",
p_pack->i_sample_rate,
p_pack->i_frame_size );
}
else
{
int i_wf = sizeof( WAVEFORMATEX ) + 5;
/* we will try to create a AAC Config from adts */
p_pack->output_format.i_cat = UNKNOWN_ES;
p_pack->output_format.i_fourcc = VLC_FOURCC( 'n', 'u', 'l', 'l' );
p_pack->b_adts = 1;
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_pack->p_sout_input =
sout_InputNew( p_pack->p_fifo,
&p_pack->output_format );
if( !p_pack->p_sout_input )
{
msg_Err( p_pack->p_fifo,
"cannot add a new stream" );
return( -1 );
}
p_pack->i_pts_start = -1;
p_pack->i_pts = 0;
return( 0 );
}
/*****************************************************************************
* PacketizeThread: packetize an unit (here copy a complete pes)
*****************************************************************************/
static void PacketizeThreadMPEG4( packetizer_thread_t *p_pack )
{
sout_buffer_t *p_sout_buffer;
pes_packet_t *p_pes;
size_t i_size;
/* **** get samples count **** */
input_ExtractPES( p_pack->p_fifo, &p_pes );
if( !p_pes )
{
p_pack->p_fifo->b_error = 1;
return;
}
#if 0
if( p_pack->i_pts_start < 0 && p_pes->i_pts > 0 )
{
p_pack->i_pts_start = p_pes->i_pts;
}
p_pack->i_pts = p_pes->i_pts - p_pack->i_pts_start;
#endif
i_size = p_pes->i_pes_size;
if( i_size > 0 )
{
data_packet_t *p_data;
size_t i_buffer;
p_sout_buffer =
sout_BufferNew( p_pack->p_sout_input->p_sout, i_size );
if( !p_sout_buffer )
{
p_pack->p_fifo->b_error = 1;
return;
}
/* TODO: memcpy of the pes packet */
for( i_buffer = 0, p_data = p_pes->p_first;
p_data != NULL && i_buffer < i_size;
p_data = p_data->p_next)
{
size_t i_copy;
i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
i_size - i_buffer );
if( i_copy > 0 )
{
p_pack->p_fifo->p_vlc->pf_memcpy( p_sout_buffer->p_buffer + i_buffer,
p_data->p_payload_start,
i_copy );
}
i_buffer += i_copy;
}
p_sout_buffer->i_length = (mtime_t)1000000 * (mtime_t)p_pack->i_frame_size / (mtime_t)p_pack->i_sample_rate;
p_sout_buffer->i_bitrate = 0;
p_sout_buffer->i_dts = p_pack->i_pts;
p_sout_buffer->i_pts = p_pack->i_pts;
sout_InputSendBuffer( p_pack->p_sout_input,
p_sout_buffer );
p_pack->i_pts += (mtime_t)1000000 * (mtime_t)p_pack->i_frame_size / (mtime_t)p_pack->i_sample_rate;
}
input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
}
static void PacketizeThreadADTS( packetizer_thread_t *p_pack )
{
msg_Err( p_pack->p_fifo, "adts stream unsupported" );
p_pack->p_fifo->b_error = 1;
}
/*****************************************************************************
* EndThread : packetizer thread destruction
*****************************************************************************/
static void EndThread ( packetizer_thread_t *p_pack)
{
if( p_pack->p_sout_input )
{
sout_InputDelete( p_pack->p_sout_input );
}
if( p_pack->p_wf )
{
free( p_pack->p_wf );
}
}
/***************************************************************************** /*****************************************************************************
* mpeg4video.c: * mpeg4video.c
***************************************************************************** *****************************************************************************
* Copyright (C) 2001, 2002 VideoLAN * Copyright (C) 2001, 2002 VideoLAN
* $Id: mpeg4video.c,v 1.2 2002/12/18 16:33:09 fenrir Exp $ * $Id: mpeg4video.c,v 1.3 2003/01/08 10:26:49 fenrir Exp $
* *
* Authors: Laurent Aimar <fenrir@via.ecp.fr> * Authors: Laurent Aimar <fenrir@via.ecp.fr>
* Eric Petit <titer@videolan.org> * Eric Petit <titer@videolan.org>
...@@ -34,6 +34,8 @@ ...@@ -34,6 +34,8 @@
#include <stdlib.h> /* malloc(), free() */ #include <stdlib.h> /* malloc(), free() */
#include <string.h> /* strdup() */ #include <string.h> /* strdup() */
#include "codecs.h"
/***************************************************************************** /*****************************************************************************
* Local prototypes * Local prototypes
*****************************************************************************/ *****************************************************************************/
...@@ -41,17 +43,16 @@ typedef struct packetizer_thread_s ...@@ -41,17 +43,16 @@ typedef struct packetizer_thread_s
{ {
/* Input properties */ /* Input properties */
decoder_fifo_t *p_fifo; decoder_fifo_t *p_fifo;
bit_stream_t bit_stream;
mtime_t i_dts;
/* Output properties */ /* Output properties */
sout_input_t *p_sout_input; sout_input_t *p_sout_input;
sout_packet_format_t output_format; sout_packet_format_t output_format;
mtime_t i_pts_start;
int i_vol;
uint8_t *p_vol;
sout_buffer_t *p_vol;
int i_vop_since_vol;
} packetizer_thread_t; } packetizer_thread_t;
static int Open ( vlc_object_t * ); static int Open ( vlc_object_t * );
...@@ -61,12 +62,15 @@ static int InitThread ( packetizer_thread_t * ); ...@@ -61,12 +62,15 @@ static int InitThread ( packetizer_thread_t * );
static void PacketizeThread ( packetizer_thread_t * ); static void PacketizeThread ( packetizer_thread_t * );
static void EndThread ( packetizer_thread_t * ); static void EndThread ( packetizer_thread_t * );
static void input_ShowPES( decoder_fifo_t *p_fifo, pes_packet_t **pp_pes );
/***************************************************************************** /*****************************************************************************
* Module descriptor * Module descriptor
*****************************************************************************/ *****************************************************************************/
vlc_module_begin(); vlc_module_begin();
set_description( _("MPEG-4 packetizer") ); set_description( _("MPEG4 Video packetizer") );
set_capability( "packetizer", 50 ); set_capability( "packetizer", 50 );
set_callbacks( Open, NULL ); set_callbacks( Open, NULL );
vlc_module_end(); vlc_module_end();
...@@ -91,6 +95,7 @@ vlc_module_end(); ...@@ -91,6 +95,7 @@ vlc_module_end();
#define TEXTURE_SPATIAL_LAYER_START_CODE 0x1bf #define TEXTURE_SPATIAL_LAYER_START_CODE 0x1bf
#define TEXTURE_SNR_LAYER_START_CODE 0x1c0 #define TEXTURE_SNR_LAYER_START_CODE 0x1c0
/***************************************************************************** /*****************************************************************************
* OpenDecoder: probe the packetizer and return score * OpenDecoder: probe the packetizer and return score
***************************************************************************** *****************************************************************************
...@@ -126,7 +131,7 @@ static int Run( decoder_fifo_t *p_fifo ) ...@@ -126,7 +131,7 @@ static int Run( decoder_fifo_t *p_fifo )
packetizer_thread_t *p_pack; packetizer_thread_t *p_pack;
int b_error; int b_error;
msg_Info( p_fifo, "Running MPEG-4 packetizer" ); msg_Info( p_fifo, "Running MPEG4 Video packetizer" );
if( !( p_pack = malloc( sizeof( packetizer_thread_t ) ) ) ) if( !( p_pack = malloc( sizeof( packetizer_thread_t ) ) ) )
{ {
msg_Err( p_fifo, "out of memory" ); msg_Err( p_fifo, "out of memory" );
...@@ -166,186 +171,229 @@ static int Run( decoder_fifo_t *p_fifo ) ...@@ -166,186 +171,229 @@ static int Run( decoder_fifo_t *p_fifo )
#define FREE( p ) if( p ) free( p ); p = NULL #define FREE( p ) if( p ) free( p ); p = NULL
static int CopyUntilNextStartCode( packetizer_thread_t *p_pack,
sout_buffer_t *p_sout_buffer,
int *pi_pos )
{
int i_copy = 0;
do
{
p_sout_buffer->p_buffer[(*pi_pos)++] =
GetBits( &p_pack->bit_stream, 8 );
i_copy++;
if( *pi_pos + 2048 > p_sout_buffer->i_allocated_size )
{
sout_BufferRealloc( p_pack->p_sout_input->p_sout,
p_sout_buffer,
p_sout_buffer->i_allocated_size + 50 * 1024);
}
} while( ShowBits( &p_pack->bit_stream, 24 ) != 0x01 &&
!p_pack->p_fifo->b_die && !p_pack->p_fifo->b_error );
return( i_copy );
}
static int sout_BufferAddMem( sout_instance_t *p_sout,
sout_buffer_t *p_buffer,
int i_mem,
uint8_t *p_mem )
{
if( p_buffer->i_size + i_mem >= p_buffer->i_allocated_size )
{
sout_BufferRealloc( p_sout,
p_buffer,
p_buffer->i_size + i_mem + 1024 );
}
memcpy( p_buffer->p_buffer + p_buffer->i_size, p_mem, i_mem );
p_buffer->i_size += i_mem;
return( i_mem );
}
/***************************************************************************** /*****************************************************************************
* InitThread: initialize data before entering main loop * InitThread: initialize data before entering main loop
*****************************************************************************/ *****************************************************************************/
static int InitThread( packetizer_thread_t *p_pack ) static int InitThread( packetizer_thread_t *p_pack )
{ {
p_pack->i_dts = 0; BITMAPINFOHEADER *p_bih;
p_pack->p_vol = NULL;
p_pack->i_vop_since_vol = 0; p_bih = (BITMAPINFOHEADER*)p_pack->p_fifo->p_bitmapinfoheader;
p_pack->output_format.i_cat = VIDEO_ES;
p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'v' ); if( p_bih && p_bih->biSize > sizeof( BITMAPINFOHEADER ) )
if( InitBitstream( &p_pack->bit_stream, p_pack->p_fifo,
NULL, NULL ) != VLC_SUCCESS )
{ {
msg_Err( p_pack->p_fifo, "cannot initialize bitstream" ); /* We have a vol */
return -1; p_pack->i_vol = p_bih->biSize - sizeof( BITMAPINFOHEADER );
p_pack->p_vol = malloc( p_pack->i_vol );
memcpy( p_pack->p_vol, &p_bih[1], p_pack->i_vol );
/* create stream input output */
p_pack->output_format.i_cat = VIDEO_ES;
p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'v' );
p_pack->output_format.p_format = malloc( p_bih->biSize );
memcpy( p_pack->output_format.p_format, p_bih, p_bih->biSize );
msg_Warn( p_pack->p_fifo, "opening with vol size:%d", p_pack->i_vol );
p_pack->p_sout_input =
sout_InputNew( p_pack->p_fifo,
&p_pack->output_format );
}
else
{
p_pack->i_vol = 0;
p_pack->p_vol = 0;
p_pack->output_format.i_cat = UNKNOWN_ES;
p_pack->output_format.i_fourcc = VLC_FOURCC( 'n', 'u', 'l', 'l' );
p_pack->output_format.p_format = NULL;
p_pack->p_sout_input =
sout_InputNew( p_pack->p_fifo,
&p_pack->output_format );
} }
p_pack->p_sout_input =
sout_InputNew( p_pack->p_fifo,
&p_pack->output_format );
if( !p_pack->p_sout_input ) if( !p_pack->p_sout_input )
{ {
msg_Err( p_pack->p_fifo, msg_Err( p_pack->p_fifo, "cannot add a new stream" );
"cannot add a new stream" );
return( -1 ); return( -1 );
} }
p_pack->i_pts_start = -1;
return( 0 ); return( 0 );
} }
static int m4v_FindStartCode( uint8_t **pp_data, uint8_t *p_end )
{
for( ; *pp_data < p_end - 4; (*pp_data)++ )
{
if( (*pp_data)[0] == 0 && (*pp_data)[1] == 0 && (*pp_data)[2] == 1 )
{
return( 0 );
}
}
fprintf( stderr, "\n********* cannot find startcode\n" );
return( -1 );
}
/***************************************************************************** /*****************************************************************************
* PacketizeThread: packetize an unit (here copy a complete pes) * PacketizeThread: packetize an unit (here copy a complete pes)
*****************************************************************************/ *****************************************************************************/
static void PacketizeThread( packetizer_thread_t *p_pack ) static void PacketizeThread( packetizer_thread_t *p_pack )
{ {
sout_instance_t *p_sout = p_pack->p_sout_input->p_sout; sout_buffer_t *p_sout_buffer;
sout_buffer_t *p_frame; pes_packet_t *p_pes;
size_t i_size;
uint32_t i_startcode;
/* Idea: Copy until a vop has been found /* **** get samples count **** */
* Once a videoobject & videoobjectlayer has been found we save it input_ExtractPES( p_pack->p_fifo, &p_pes );
*/ if( !p_pes )
p_frame = sout_BufferNew( p_sout, 20*1024 ); // FIXME
p_frame->i_size = 0;
for( ;; )
{ {
while( ( ( i_startcode = ShowBits( &p_pack->bit_stream, 32 ) )&0xffffff00 ) != 0x00000100 ) p_pack->p_fifo->b_error = 1;
{ return;
RemoveBits( &p_pack->bit_stream, 8 ); }
} if( p_pack->i_pts_start < 0 && p_pes->i_pts > 0 )
{
p_pack->i_pts_start = p_pes->i_pts;
}
i_size = p_pes->i_pes_size;
if( i_size > 0 )
{
pes_packet_t *p_pes_next;
data_packet_t *p_data;
size_t i_buffer;
if( i_startcode == VISUAL_OBJECT_SEQUENCE_START_CODE ) p_sout_buffer =
{ sout_BufferNew( p_pack->p_sout_input->p_sout, i_size );
msg_Dbg( p_pack->p_fifo, "<visuel_object_sequence>" ); if( !p_sout_buffer )
RemoveBits32( &p_pack->bit_stream );
}
else if( i_startcode == VISUAL_OBJECT_SEQUENCE_END_CODE )
{ {
msg_Dbg( p_pack->p_fifo, "</visuel_object_sequence>" ); p_pack->p_fifo->b_error = 1;
RemoveBits32( &p_pack->bit_stream ); return;
} }
else /* TODO: memcpy of the pes packet */
for( i_buffer = 0, p_data = p_pes->p_first;
p_data != NULL && i_buffer < i_size;
p_data = p_data->p_next)
{ {
msg_Dbg( p_pack->p_fifo, "start code:0x%8.8x", i_startcode ); size_t i_copy;
if( ( i_startcode & ~VIDEO_OBJECT_MASK ) == VIDEO_OBJECT_START_CODE ) i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
i_size - i_buffer );
if( i_copy > 0 )
{ {
msg_Dbg( p_pack->p_fifo, "<video_object>" ); p_pack->p_fifo->p_vlc->pf_memcpy( p_sout_buffer->p_buffer + i_buffer,
CopyUntilNextStartCode( p_pack, p_frame, &p_frame->i_size ); p_data->p_payload_start,
i_copy );
} }
else if( ( i_startcode & ~VIDEO_OBJECT_LAYER_MASK ) == VIDEO_OBJECT_LAYER_START_CODE ) i_buffer += i_copy;
}
p_sout_buffer->i_length = 0;
p_sout_buffer->i_dts = p_pes->i_pts - p_pack->i_pts_start;
p_sout_buffer->i_pts = p_pes->i_pts - p_pack->i_pts_start;
p_sout_buffer->i_bitrate = 0;
if( p_pack->p_vol == NULL )
{
uint8_t *p_vol_begin, *p_vol_end, *p_end;
/* search if p_sout_buffer contains with a vol */
p_vol_begin = p_sout_buffer->p_buffer;
p_vol_end = NULL;
p_end = p_sout_buffer->p_buffer + p_sout_buffer->i_size;
for( ;; )
{ {
/* first: save it */ if( m4v_FindStartCode( &p_vol_begin, p_end ) )
if( p_pack->p_vol == NULL )
{ {
p_pack->p_vol = sout_BufferNew( p_sout, 1024 ); break;
} }
p_pack->p_vol->i_size = 0; msg_Dbg( p_pack->p_fifo,
CopyUntilNextStartCode( p_pack, p_pack->p_vol, &p_pack->p_vol->i_size ); "starcode 0x%2.2x%2.2x%2.2x%2.2x",
p_pack->i_vop_since_vol = 0; p_vol_begin[0], p_vol_begin[1], p_vol_begin[2], p_vol_begin[3] );
/* then: add it to p_frame */ if( ( p_vol_begin[3] & ~VIDEO_OBJECT_MASK ) == ( VIDEO_OBJECT_START_CODE&0xff ) )
sout_BufferAddMem( p_sout, p_frame,
p_pack->p_vol->i_size,
p_pack->p_vol->p_buffer );
}
else if( i_startcode == GROUP_OF_VOP_START_CODE )
{
msg_Dbg( p_pack->p_fifo, "<group_of_vop>" );
#if 0
if( p_pack->p_vol && p_pack->i_vop_since_vol > 100 ) // FIXME
{ {
sout_BufferAddMem( p_sout, p_frame, p_vol_end = p_vol_begin + 4;
p_pack->p_vol->i_size, if( m4v_FindStartCode( &p_vol_end, p_end ) )
p_pack->p_vol->p_buffer ); {
p_pack->i_vop_since_vol = 0; break;
}
if( ( p_vol_end[3] & ~VIDEO_OBJECT_LAYER_MASK ) == ( VIDEO_OBJECT_LAYER_START_CODE&0xff ) )
{
p_vol_end += 4;
if( m4v_FindStartCode( &p_vol_end, p_end ) )
{
p_vol_end = p_end;
}
}
else
{
p_vol_end = NULL;
}
} }
#endif else if( ( p_vol_begin[3] & ~VIDEO_OBJECT_LAYER_MASK ) == ( VIDEO_OBJECT_LAYER_START_CODE&0xff) )
CopyUntilNextStartCode( p_pack, p_frame, &p_frame->i_size );
}
else if( i_startcode == VOP_START_CODE )
{
msg_Dbg( p_pack->p_fifo, "<vop>" );
#if 1
if( p_pack->p_vol && p_pack->i_vop_since_vol > 30 ) // FIXME
{ {
sout_BufferAddMem( p_sout, p_frame, p_vol_end = p_vol_begin + 4;
p_pack->p_vol->i_size, if( m4v_FindStartCode( &p_vol_end, p_end ) )
p_pack->p_vol->p_buffer ); {
p_pack->i_vop_since_vol = 0; p_vol_end = p_end;
}
}
if( p_vol_end != NULL && p_vol_begin < p_vol_end )
{
BITMAPINFOHEADER *p_bih;
p_pack->i_vol = p_vol_end - p_vol_begin;
msg_Dbg( p_pack->p_fifo, "Reopening output" );
p_pack->p_vol = malloc( p_pack->i_vol );
memcpy( p_pack->p_vol, p_vol_begin, p_pack->i_vol );
sout_InputDelete( p_pack->p_sout_input );
p_pack->output_format.i_cat = VIDEO_ES;
p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'v' );
p_pack->output_format.p_format =
(void*)p_bih = malloc( sizeof( BITMAPINFOHEADER ) + p_pack->i_vol);
p_bih->biSize = sizeof( BITMAPINFOHEADER ) + p_pack->i_vol;
p_bih->biWidth = 0;
p_bih->biHeight = 0;
p_bih->biPlanes = 1;
p_bih->biBitCount = 0;
p_bih->biCompression = 0; /* FIXME */
p_bih->biSizeImage = 0;
p_bih->biXPelsPerMeter = 0;
p_bih->biYPelsPerMeter = 0;
p_bih->biClrUsed = 0;
p_bih->biClrImportant = 0;
memcpy( &p_bih[1], p_pack->p_vol, p_pack->i_vol );
p_pack->p_sout_input =
sout_InputNew( p_pack->p_fifo,
&p_pack->output_format );
if( !p_pack->p_sout_input )
{
p_pack->p_fifo->b_error = 1;
return;
}
break;
}
else
{
p_vol_begin += 4;
} }
#endif
CopyUntilNextStartCode( p_pack, p_frame, &p_frame->i_size );
p_pack->i_vop_since_vol++;
break;
}
else
{
msg_Dbg( p_pack->p_fifo, "unknown start code" );
CopyUntilNextStartCode( p_pack, p_frame, &p_frame->i_size );
} }
}
input_ShowPES( p_pack->p_fifo, &p_pes_next );
if( p_pes_next )
{
p_sout_buffer->i_length = p_pes_next->i_pts - p_pes->i_pts;
} }
sout_InputSendBuffer( p_pack->p_sout_input,
p_sout_buffer );
} }
p_frame->i_length = 1000000 / 25; input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
p_frame->i_bitrate= 0;
p_frame->i_dts = p_pack->i_dts;
p_frame->i_pts = p_pack->i_dts;
p_pack->i_dts += 1000000 / 25;
sout_InputSendBuffer( p_pack->p_sout_input, p_frame );
} }
...@@ -360,3 +408,35 @@ static void EndThread ( packetizer_thread_t *p_pack) ...@@ -360,3 +408,35 @@ static void EndThread ( packetizer_thread_t *p_pack)
} }
} }
static void input_ShowPES( decoder_fifo_t *p_fifo, pes_packet_t **pp_pes )
{
pes_packet_t *p_pes;
vlc_mutex_lock( &p_fifo->data_lock );
if( p_fifo->p_first == NULL )
{
if( p_fifo->b_die )
{
vlc_mutex_unlock( &p_fifo->data_lock );
if( pp_pes ) *pp_pes = NULL;
return;
}
/* Signal the input thread we're waiting. This is only
* needed in case of slave clock (ES plug-in) but it won't
* harm. */
vlc_cond_signal( &p_fifo->data_wait );
/* Wait for the input to tell us when we received a packet. */
vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
}
p_pes = p_fifo->p_first;
vlc_mutex_unlock( &p_fifo->data_lock );
if( pp_pes )
{
*pp_pes = p_pes;
}
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* mpegvideo.c * mpegvideo.c
***************************************************************************** *****************************************************************************
* Copyright (C) 2001, 2002 VideoLAN * Copyright (C) 2001, 2002 VideoLAN
* $Id: mpegvideo.c,v 1.1 2002/12/14 21:32:41 fenrir Exp $ * $Id: mpegvideo.c,v 1.2 2003/01/08 10:26:49 fenrir Exp $
* *
* Authors: Laurent Aimar <fenrir@via.ecp.fr> * Authors: Laurent Aimar <fenrir@via.ecp.fr>
* Eric Petit <titer@videolan.org> * Eric Petit <titer@videolan.org>
...@@ -83,7 +83,9 @@ static int Open( vlc_object_t *p_this ) ...@@ -83,7 +83,9 @@ static int Open( vlc_object_t *p_this )
{ {
decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this; decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
if( p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', 'v') ) 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') )
{ {
return VLC_EGENERIC; return VLC_EGENERIC;
} }
...@@ -152,7 +154,7 @@ static int InitThread( packetizer_t *p_pack ) ...@@ -152,7 +154,7 @@ static int InitThread( packetizer_t *p_pack )
{ {
p_pack->output_format.i_cat = VIDEO_ES; p_pack->output_format.i_cat = VIDEO_ES;
p_pack->output_format.i_fourcc = p_pack->p_fifo->i_fourcc; p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'v');
p_pack->p_sout_input = NULL; p_pack->p_sout_input = NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment