Commit 630b0fc7 authored by Gildas Bazin's avatar Gildas Bazin

* modules/codec/ffmpeg/*: ported the ffmpeg audio and video decoders to the new api.
   Isolated the video postprocessing routines in postprocess.c

* modules/codec/ffmpeg/encoder.c, modules/codec/vorbis.c, modules/stream_out/transcode.c,
   include/vlc_block.h, include/vlc_codec.h: extracted the encoders out of transcode.c.
   transcode now uses encoder plugins (currently ffmpeg, vorbis and theora).

PS: transcoding is currently a bit broken but I'll fix that ASAP.
parent 074f7be5
dnl Autoconf settings for vlc
dnl $Id: configure.ac,v 1.98 2003/10/26 17:11:56 gbazin Exp $
dnl $Id: configure.ac,v 1.99 2003/10/27 01:04:38 gbazin Exp $
AC_INIT(vlc,0.6.3-cvs)
......@@ -1706,13 +1706,6 @@ then
AC_MSG_ERROR([cannot find ${real_ffmpeg_tree}/libavcodec/libavcodec.a, make sure you compiled libavcodec in ${with_ffmpeg_tree}])
fi
fi
ac_have_vorbis_headers=yes
AC_CHECK_HEADERS(vorbis/vorbisenc.h vorbis/codec.h,,
ac_have_vorbis_headers=no)
if test "$ac_have_vorbis_headers" = "yes"; then
AX_ADD_LDFLAGS([stream_out_transcode],[-lvorbisenc -lvorbis -logg])
fi
fi
dnl
......@@ -2020,6 +2013,9 @@ then
AC_CHECK_HEADERS(vorbis/codec.h, [
AX_ADD_PLUGINS([vorbis])
AX_ADD_LDFLAGS([vorbis],[-lvorbis -logg]) ],[])
AC_CHECK_HEADERS(vorbis/vorbisenc.h, [
AX_ADD_LDFLAGS([vorbis],[-lvorbisenc]) ],[])
fi
dnl
......
......@@ -2,7 +2,7 @@
* vlc_block.h: Data blocks management functions
*****************************************************************************
* Copyright (C) 2003 VideoLAN
* $Id: vlc_block.h,v 1.3 2003/09/30 20:23:03 gbazin Exp $
* $Id: vlc_block.h,v 1.4 2003/10/27 01:04:38 gbazin Exp $
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
*
......@@ -37,6 +37,7 @@ struct block_t
vlc_bool_t b_frame_start;
mtime_t i_pts;
mtime_t i_dts;
mtime_t i_length;
vlc_bool_t b_discontinuity; /* only temporary */
......
......@@ -2,7 +2,7 @@
* vlc_codec.h: codec related structures
*****************************************************************************
* Copyright (C) 1999-2003 VideoLAN
* $Id: vlc_codec.h,v 1.1 2003/10/08 21:01:07 gbazin Exp $
* $Id: vlc_codec.h,v 1.2 2003/10/27 01:04:38 gbazin Exp $
*
* Authors: Gildas Bazin <gbazin@netcourrier.com>
*
......@@ -87,6 +87,20 @@ struct encoder_t
/* Properties of the output of the encoder */
vlc_fourcc_t i_fourcc;
int i_bitrate;
int i_extra_data;
uint8_t *p_extra_data;
/* FIXME: move these to the ffmpeg encoder */
int i_frame_rate;
int i_frame_rate_base;
int i_key_int;
int i_b_frames;
int i_vtolerance;
int i_qmin;
int i_qmax;
int i_hq;
};
/**
......
......@@ -2,9 +2,9 @@ SOURCES_ffmpeg = \
ffmpeg.c \
ffmpeg.h \
video.c \
video.h \
audio.c \
audio.h \
chroma.c \
encoder.c \
postprocess.c \
$(NULL)
/*****************************************************************************
* audio.c: audio decoder using ffmpeg library
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
* $Id: audio.c,v 1.19 2003/07/10 01:33:41 fenrir Exp $
* Copyright (C) 1999-2003 VideoLAN
* $Id: audio.c,v 1.20 2003/10/27 01:04:38 gbazin Exp $
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
* Gildas Bazin <gbazin@netcourrier.com>
*
* 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
......@@ -25,6 +26,7 @@
* Preamble
*****************************************************************************/
#include <stdlib.h> /* malloc(), free() */
#include <string.h>
#include <vlc/vlc.h>
#include <vlc/vout.h>
......@@ -36,12 +38,10 @@
#include <unistd.h> /* getpid() */
#endif
#include <errno.h>
#include <string.h>
#ifdef HAVE_SYS_TIMES_H
# include <sys/times.h>
#endif
#include "codecs.h"
#include "aout_internal.h"
......@@ -52,304 +52,241 @@
# include <avcodec.h>
#endif
//#include "postprocessing/postprocessing.h"
#include "ffmpeg.h"
#include "audio.h"
/*
* Local prototypes
*/
int E_( InitThread_Audio ) ( adec_thread_t * );
void E_( EndThread_Audio ) ( adec_thread_t * );
void E_( DecodeThread_Audio ) ( adec_thread_t * );
static unsigned int pi_channels_maps[6] =
{
0,
AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT,
AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
| AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
};
/*****************************************************************************
* locales Functions
*****************************************************************************/
/*****************************************************************************
*
* Functions that initialize, decode and end the decoding process
*
* Functions exported for ffmpeg.c
* * E_( InitThread_Audio )
* * E_( DecodeThread_Audio )
* * E_( EndThread_Video_Audio )
* decoder_sys_t : decoder descriptor
*****************************************************************************/
struct decoder_sys_t
{
/* Common part between video and audio decoder */
int i_cat;
int i_codec_id;
char *psz_namecodec;
AVCodecContext *p_context;
AVCodec *p_codec;
/* Temporary buffer for libavcodec */
uint8_t *p_output;
/*
* Output properties
*/
aout_instance_t *p_aout;
aout_input_t *p_aout_input;
audio_sample_format_t aout_format;
audio_date_t end_date;
};
/*****************************************************************************
* InitThread: initialize vdec output thread
* InitAudioDec: initialize audio decoder
*****************************************************************************
* This function is called from decoder_Run and performs the second step
* of the initialization. It returns 0 on success. Note that the thread's
* flag are not modified inside this function.
*
* ffmpeg codec will be open, some memory allocated.
* The ffmpeg codec will be opened, some memory allocated.
*****************************************************************************/
int E_( InitThread_Audio )( adec_thread_t *p_adec )
int E_(InitAudioDec)( decoder_t *p_dec, AVCodecContext *p_context,
AVCodec *p_codec, int i_codec_id, char *psz_namecodec )
{
decoder_sys_t *p_sys;
WAVEFORMATEX wf, *p_wf;
if( ( p_wf = p_adec->p_fifo->p_waveformatex ) == NULL )
/* Allocate the memory needed to store the decoder's structure */
if( ( p_dec->p_sys = p_sys =
(decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
{
msg_Warn( p_adec->p_fifo, "audio informations missing" );
msg_Err( p_dec, "out of memory" );
return VLC_EGENERIC;
}
p_dec->p_sys->p_context = p_context;
p_dec->p_sys->p_codec = p_codec;
p_dec->p_sys->i_codec_id = i_codec_id;
p_dec->p_sys->psz_namecodec = psz_namecodec;
if( ( p_wf = p_dec->p_fifo->p_waveformatex ) == NULL )
{
msg_Warn( p_dec, "audio informations missing" );
p_wf = &wf;
memset( p_wf, 0, sizeof( WAVEFORMATEX ) );
}
/* ***** Fill p_context with init values ***** */
p_adec->p_context->sample_rate = p_wf->nSamplesPerSec;
p_adec->p_context->channels = p_wf->nChannels;
p_adec->p_context->block_align = p_wf->nBlockAlign;
p_adec->p_context->bit_rate = p_wf->nAvgBytesPerSec * 8;
p_sys->p_context->sample_rate = p_wf->nSamplesPerSec;
p_sys->p_context->channels = p_wf->nChannels;
p_sys->p_context->block_align = p_wf->nBlockAlign;
p_sys->p_context->bit_rate = p_wf->nAvgBytesPerSec * 8;
if( ( p_adec->p_context->extradata_size = p_wf->cbSize ) > 0 )
if( ( p_sys->p_context->extradata_size = p_wf->cbSize ) > 0 )
{
p_adec->p_context->extradata = malloc( p_wf->cbSize + FF_INPUT_BUFFER_PADDING_SIZE );
memcpy( p_adec->p_context->extradata, &p_wf[1], p_wf->cbSize);
memset( &((uint8_t*)p_adec->p_context->extradata)[p_wf->cbSize], 0, FF_INPUT_BUFFER_PADDING_SIZE );
p_sys->p_context->extradata =
malloc( p_wf->cbSize + FF_INPUT_BUFFER_PADDING_SIZE );
memcpy( p_sys->p_context->extradata, &p_wf[1], p_wf->cbSize);
memset( &((uint8_t*)p_sys->p_context->extradata)[p_wf->cbSize], 0,
FF_INPUT_BUFFER_PADDING_SIZE );
}
/* ***** Open the codec ***** */
if (avcodec_open(p_adec->p_context, p_adec->p_codec) < 0)
if (avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0)
{
msg_Err( p_adec->p_fifo,
"cannot open codec (%s)",
p_adec->psz_namecodec );
return( -1 );
msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
return VLC_EGENERIC;
}
else
{
msg_Dbg( p_adec->p_fifo,
"ffmpeg codec (%s) started",
p_adec->psz_namecodec );
msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
}
p_adec->p_output = malloc( 3 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
p_sys->p_output = malloc( 3 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
p_adec->output_format.i_format = AOUT_FMT_S16_NE;
p_adec->output_format.i_rate = p_wf->nSamplesPerSec;
p_adec->output_format.i_physical_channels
= p_adec->output_format.i_original_channels
= pi_channels_maps[p_wf->nChannels];
p_sys->p_aout = NULL;
p_sys->p_aout_input = NULL;
p_adec->p_aout = NULL;
p_adec->p_aout_input = NULL;
aout_DateSet( &p_sys->end_date, 0 );
return( 0 );
return VLC_SUCCESS;
}
/*****************************************************************************
* DecodeThread: Called for decode one frame
* DecodeAudio: Called to decode one frame
*****************************************************************************/
void E_( DecodeThread_Audio )( adec_thread_t *p_adec )
int E_( DecodeAudio )( decoder_t *p_dec, block_t *p_block )
{
pes_packet_t *p_pes;
aout_buffer_t *p_aout_buffer;
decoder_sys_t *p_sys = p_dec->p_sys;
aout_buffer_t *p_aout_buffer;
mtime_t i_pts;
int i_samplesperchannel;
int i_output_size;
int i_frame_size;
int i_used;
uint8_t *p_buffer, *p_samples;
int i_buffer, i_samples;
uint8_t *p;
if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
{
/* We've just started the stream, wait for the first PTS. */
block_Release( p_block );
return VLC_SUCCESS;
}
do
i_pts = p_block->i_pts;
i_buffer = p_block->i_buffer;
p_buffer = p_block->p_buffer;
while( i_buffer )
{
input_ExtractPES( p_adec->p_fifo, &p_pes );
if( !p_pes )
int i_used, i_output;
i_used = avcodec_decode_audio( p_sys->p_context,
(int16_t*)p_sys->p_output, &i_output,
p_buffer, i_buffer );
if( i_used < 0 )
{
p_adec->p_fifo->b_error = 1;
return;
msg_Warn( p_dec, "cannot decode one frame (%d bytes)", i_buffer );
break;
}
p_adec->pts = p_pes->i_pts;
i_frame_size = p_pes->i_pes_size;
if( i_frame_size > 0 )
{
int i_need;
i_buffer -= i_used;
p_buffer += i_used;
if( p_sys->p_context->channels <= 0 || p_sys->p_context->channels > 6 )
{
msg_Warn( p_dec, "invalid channels count %d",
p_sys->p_context->channels );
break;
}
i_need = i_frame_size + FF_INPUT_BUFFER_PADDING_SIZE + p_adec->i_buffer;
if( p_adec->i_buffer_size < i_need )
/* **** First check if we have a valid output **** */
if( p_sys->p_aout_input == NULL ||
p_sys->aout_format.i_original_channels !=
pi_channels_maps[p_sys->p_context->channels] )
{
if( p_sys->p_aout_input != NULL )
{
uint8_t *p_last = p_adec->p_buffer;
p_adec->p_buffer = malloc( i_need );
p_adec->i_buffer_size = i_need;
if( p_adec->i_buffer > 0 )
{
memcpy( p_adec->p_buffer, p_last, p_adec->i_buffer );
}
FREE( p_last );
/* **** Delete the old **** */
aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
}
i_frame_size =
E_( GetPESData )( p_adec->p_buffer + p_adec->i_buffer,
i_frame_size,
p_pes );
/* make ffmpeg happier but I'm not sure it's needed for audio */
memset( p_adec->p_buffer + p_adec->i_buffer + i_frame_size, 0, FF_INPUT_BUFFER_PADDING_SIZE );
}
input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
} while( i_frame_size <= 0 );
i_frame_size += p_adec->i_buffer;
/* **** Create a new audio output **** */
p_sys->aout_format.i_format = AOUT_FMT_S16_NE;
p_sys->aout_format.i_rate = p_sys->p_context->sample_rate;
p_sys->aout_format.i_physical_channels =
p_sys->aout_format.i_original_channels =
pi_channels_maps[p_sys->p_context->channels];
usenextdata:
i_used = avcodec_decode_audio( p_adec->p_context,
(int16_t*)p_adec->p_output,
&i_output_size,
p_adec->p_buffer,
i_frame_size );
if( i_used < 0 )
{
msg_Warn( p_adec->p_fifo,
"cannot decode one frame (%d bytes)",
i_frame_size );
p_adec->i_buffer = 0;
return;
}
else if( i_used < i_frame_size )
{
memmove( p_adec->p_buffer,
p_adec->p_buffer + i_used,
p_adec->i_buffer_size - i_used );
p_adec->i_buffer = i_frame_size - i_used;
}
else
{
p_adec->i_buffer = 0;
}
i_frame_size -= i_used;
//msg_Dbg( p_adec->p_fifo, "frame size:%d buffer used:%d", i_frame_size, i_used );
if( i_output_size <= 0 )
{
msg_Warn( p_adec->p_fifo,
"decoded %d samples bytes",
i_output_size );
}
if( p_adec->p_context->channels <= 0 ||
p_adec->p_context->channels > 5 )
{
msg_Warn( p_adec->p_fifo,
"invalid channels count %d",
p_adec->p_context->channels );
}
aout_DateInit( &p_sys->end_date, p_sys->aout_format.i_rate );
p_sys->p_aout_input = aout_DecNew( p_dec, &p_sys->p_aout,
&p_sys->aout_format );
}
/* **** First check if we have a valid output **** */
if( ( p_adec->p_aout_input == NULL )||
( p_adec->output_format.i_original_channels !=
pi_channels_maps[p_adec->p_context->channels] ) )
{
if( p_adec->p_aout_input != NULL )
if( !p_sys->p_aout_input )
{
/* **** Delete the old **** */
aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
msg_Err( p_dec, "cannot create audio output" );
block_Release( p_block );
return VLC_EGENERIC;
}
/* **** Create a new audio output **** */
p_adec->output_format.i_physical_channels =
p_adec->output_format.i_original_channels =
pi_channels_maps[p_adec->p_context->channels];
aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
&p_adec->p_aout,
&p_adec->output_format );
}
if( !p_adec->p_aout_input )
{
msg_Err( p_adec->p_fifo, "cannot create aout" );
return;
}
if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
{
aout_DateSet( &p_adec->date, p_adec->pts );
}
else if( !aout_DateGet( &p_adec->date ) )
{
return;
}
/* **** Now we can output these samples **** */
i_samplesperchannel = i_output_size / 2
/ aout_FormatNbChannels( &p_adec->output_format );
p = &p_adec->p_output[0];
while( i_samplesperchannel > 0 )
{
int i_samples;
if( i_pts != 0 && i_pts != aout_DateGet( &p_sys->end_date ) )
{
aout_DateSet( &p_sys->end_date, i_pts );
i_pts = 0;
}
i_samples = __MIN( 8000, i_samplesperchannel );
/* **** Now we can output these samples **** */
i_samples = i_output / 2 / p_sys->p_context->channels;
p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
p_adec->p_aout_input,
i_samples );
if( !p_aout_buffer )
p_samples = p_sys->p_output;
while( i_samples > 0 )
{
msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
p_adec->p_fifo->b_error = 1;
return;
}
int i_smaller_samples;
p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
i_samples );
memcpy( p_aout_buffer->p_buffer,
p,
p_aout_buffer->i_nb_bytes );
i_smaller_samples = __MIN( 8000, i_samples );
aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
p_aout_buffer = aout_DecNewBuffer( p_sys->p_aout,
p_sys->p_aout_input,
i_smaller_samples );
if( !p_aout_buffer )
{
msg_Err( p_dec, "cannot get aout buffer" );
block_Release( p_block );
return VLC_EGENERIC;
}
p += i_samples * 2 * aout_FormatNbChannels( &p_adec->output_format );
i_samplesperchannel -= i_samples;
p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
i_smaller_samples );
memcpy( p_aout_buffer->p_buffer, p_samples,
p_aout_buffer->i_nb_bytes );
}
aout_DecPlay( p_sys->p_aout, p_sys->p_aout_input, p_aout_buffer );
if( i_frame_size > 0 )
{
goto usenextdata;
p_samples += i_smaller_samples * 2 * p_sys->p_context->channels;
i_samples -= i_smaller_samples;
}
}
return;
block_Release( p_block );
return VLC_SUCCESS;
}
/*****************************************************************************
* EndThread: thread destruction
*****************************************************************************
* This function is called when the thread ends after a sucessful
* initialization.
* EndAudioDec: audio decoder destruction
*****************************************************************************/
void E_( EndThread_Audio )( adec_thread_t *p_adec )
void E_(EndAudioDec)( decoder_t *p_dec )
{
// FREE( p_adec->format.p_data );
FREE( p_adec->p_output );
decoder_sys_t *p_sys = p_dec->p_sys;
if( p_sys->p_output ) free( p_sys->p_output );
if( p_adec->p_aout_input )
if( p_sys->p_aout_input )
{
aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
}
}
/*****************************************************************************
* audio.h: video decoder using ffmpeg library
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
* $Id: audio.h,v 1.4 2003/02/07 01:22:55 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.
*****************************************************************************/
typedef struct adec_thread_s
{
DECODER_THREAD_COMMON
// waveformatex_t format;
/*
* Output properties
*/
uint8_t * p_output;
aout_instance_t * p_aout; /* opaque */
aout_input_t * p_aout_input; /* opaque */
audio_sample_format_t output_format;
audio_date_t date;
} adec_thread_t;
/*
* Local prototypes
*/
int E_( InitThread_Audio ) ( adec_thread_t * );
void E_( EndThread_Audio ) ( adec_thread_t * );
void E_( DecodeThread_Audio ) ( adec_thread_t * );
......@@ -2,7 +2,7 @@
* chroma.c: chroma conversion using ffmpeg library
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
* $Id: chroma.c,v 1.2 2003/09/26 16:10:24 gbazin Exp $
* $Id: chroma.c,v 1.3 2003/10/27 01:04:38 gbazin Exp $
*