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
SOURCES_packetizer_mpegaudio = modules/packetizer/mpegaudio.c
SOURCES_packetizer_mpegvideo = modules/packetizer/mpegvideo.c
SOURCES_packetizer_mpeg4video = modules/packetizer/mpeg4video.c
SOURCES_packetizer_mpeg4audio = modules/packetizer/mpeg4audio.c
......@@ -2,7 +2,7 @@
* copy.c
*****************************************************************************
* 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>
* Eric Petit <titer@videolan.org>
......@@ -46,6 +46,8 @@ typedef struct packetizer_thread_s
sout_input_t *p_sout_input;
sout_packet_format_t output_format;
mtime_t i_pts_start;
} packetizer_thread_t;
static int Open ( vlc_object_t * );
......@@ -55,6 +57,9 @@ static int InitThread ( packetizer_thread_t * );
static void PacketizeThread ( 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
*****************************************************************************/
......@@ -141,20 +146,44 @@ static int Run( decoder_fifo_t *p_fifo )
static int InitThread( packetizer_thread_t *p_pack )
{
// p_pack->output_format.i_cat = p_pack->p_fifo->i_cat;
p_pack->output_format.i_fourcc = p_pack->p_fifo->i_fourcc;
switch( 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,
&p_pack->output_format );
if( !p_pack->p_sout_input )
{
msg_Err( p_pack->p_fifo,
"cannot add a new stream" );
msg_Err( p_pack->p_fifo, "cannot add a new stream" );
return( -1 );
}
p_pack->i_pts_start = -1;
return( 0 );
}
......@@ -174,10 +203,15 @@ static void PacketizeThread( packetizer_thread_t *p_pack )
p_pack->p_fifo->b_error = 1;
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;
// msg_Dbg( p_pack->p_fifo, "pes size:%d", i_size );
if( i_size > 0 )
{
pes_packet_t *p_pes_next;
data_packet_t *p_data;
size_t i_buffer;
......@@ -205,7 +239,16 @@ static void PacketizeThread( packetizer_thread_t *p_pack )
}
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,
p_sout_buffer );
}
......@@ -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
* $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>
* Eric Petit <titer@videolan.org>
......@@ -34,6 +34,8 @@
#include <stdlib.h> /* malloc(), free() */
#include <string.h> /* strdup() */
#include "codecs.h"
/*****************************************************************************
* Local prototypes
*****************************************************************************/
......@@ -41,17 +43,16 @@ typedef struct packetizer_thread_s
{
/* Input properties */
decoder_fifo_t *p_fifo;
bit_stream_t bit_stream;
mtime_t i_dts;
/* Output properties */
sout_input_t *p_sout_input;
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;
static int Open ( vlc_object_t * );
......@@ -61,12 +62,15 @@ static int InitThread ( packetizer_thread_t * );
static void PacketizeThread ( 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
*****************************************************************************/
vlc_module_begin();
set_description( _("MPEG-4 packetizer") );
set_description( _("MPEG4 Video packetizer") );
set_capability( "packetizer", 50 );
set_callbacks( Open, NULL );
vlc_module_end();
......@@ -91,6 +95,7 @@ vlc_module_end();
#define TEXTURE_SPATIAL_LAYER_START_CODE 0x1bf
#define TEXTURE_SNR_LAYER_START_CODE 0x1c0
/*****************************************************************************
* OpenDecoder: probe the packetizer and return score
*****************************************************************************
......@@ -126,7 +131,7 @@ static int Run( decoder_fifo_t *p_fifo )
packetizer_thread_t *p_pack;
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 ) ) ) )
{
msg_Err( p_fifo, "out of memory" );
......@@ -166,186 +171,229 @@ static int Run( decoder_fifo_t *p_fifo )
#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
*****************************************************************************/
static int InitThread( packetizer_thread_t *p_pack )
{
p_pack->i_dts = 0;
p_pack->p_vol = NULL;
p_pack->i_vop_since_vol = 0;
p_pack->output_format.i_cat = VIDEO_ES;
p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'v' );
if( InitBitstream( &p_pack->bit_stream, p_pack->p_fifo,
NULL, NULL ) != VLC_SUCCESS )
BITMAPINFOHEADER *p_bih;
p_bih = (BITMAPINFOHEADER*)p_pack->p_fifo->p_bitmapinfoheader;
if( p_bih && p_bih->biSize > sizeof( BITMAPINFOHEADER ) )
{
msg_Err( p_pack->p_fifo, "cannot initialize bitstream" );
return -1;
/* We have a vol */
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 )
{
msg_Err( p_pack->p_fifo,
"cannot add a new stream" );
msg_Err( p_pack->p_fifo, "cannot add a new stream" );
return( -1 );
}
p_pack->i_pts_start = -1;
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)
*****************************************************************************/
static void PacketizeThread( packetizer_thread_t *p_pack )
{
sout_instance_t *p_sout = p_pack->p_sout_input->p_sout;
sout_buffer_t *p_frame;
uint32_t i_startcode;
sout_buffer_t *p_sout_buffer;
pes_packet_t *p_pes;
size_t i_size;
/* Idea: Copy until a vop has been found
* Once a videoobject & videoobjectlayer has been found we save it
*/
p_frame = sout_BufferNew( p_sout, 20*1024 ); // FIXME
p_frame->i_size = 0;
for( ;; )
/* **** get samples count **** */
input_ExtractPES( p_pack->p_fifo, &p_pes );
if( !p_pes )
{
while( ( ( i_startcode = ShowBits( &p_pack->bit_stream, 32 ) )&0xffffff00 ) != 0x00000100 )
{
RemoveBits( &p_pack->bit_stream, 8 );
}
p_pack->p_fifo->b_error = 1;
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;
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 )
{
msg_Dbg( p_pack->p_fifo, "<visuel_object_sequence>" );