Commit d83f47c4 authored by Gildas Bazin's avatar Gildas Bazin

* modules/codec/vorbis.c: fixed vorbis encoding.
* modules/codec/ffmpeg/*: fixed ffmpeg encoding.
* modules/stream_out/transcode.c: transcoding is working again.
parent e7d66505
......@@ -2,7 +2,7 @@
* encoder.c: video and audio encoder using the ffmpeg library
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
* $Id: encoder.c,v 1.1 2003/10/27 01:04:38 gbazin Exp $
* $Id: encoder.c,v 1.2 2003/10/27 17:50:54 gbazin Exp $
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
* Gildas Bazin <gbazin@netcourrier.com>
......@@ -80,17 +80,21 @@ struct encoder_sys_t
/*
* Common properties
*/
int i_last_block_size;
int i_samples_delay;
mtime_t i_pts;
char *p_buffer;
char *p_buffer_out;
/*
* Videoo properties
*/
mtime_t i_last_ref_pts;
mtime_t i_buggy_pts_detect;
/*
* Audio properties
*/
int i_frame_size;
char *p_buffer;
char *p_buffer_out;
int i_samples_delay;
mtime_t i_pts;
};
/*****************************************************************************
......@@ -102,24 +106,31 @@ int E_(OpenVideoEncoder)( vlc_object_t *p_this )
encoder_sys_t *p_sys = p_enc->p_sys;
AVCodecContext *p_context;
AVCodec *p_codec;
int i_ff_codec;
int i_codec_id, i_cat;
char *psz_namecodec;
/* find encoder */
i_ff_codec = E_(GetFfmpegCodec)( p_enc->i_fourcc, 0, 0, 0 );
if( !i_ff_codec )
if( !E_(GetFfmpegCodec)( p_enc->i_fourcc, &i_cat, &i_codec_id,
&psz_namecodec ) )
{
return VLC_EGENERIC;
}
p_codec = avcodec_find_encoder( i_ff_codec );
if( !p_codec )
if( i_cat != VIDEO_ES )
{
msg_Err( p_enc, "\"%s\" is not a video encoder", psz_namecodec );
return VLC_EGENERIC;
}
/* libavcodec needs to be initialized */
/* Initialization must be done before avcodec_find_decoder() */
E_(InitLibavcodec)(p_this);
p_codec = avcodec_find_encoder( i_codec_id );
if( !p_codec )
{
msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
return VLC_EGENERIC;
}
/* Allocate the memory needed to store the decoder's structure */
if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
{
......@@ -169,7 +180,7 @@ int E_(OpenVideoEncoder)( vlc_object_t *p_this )
}
#endif
if( i_ff_codec == CODEC_ID_RAWVIDEO )
if( i_codec_id == CODEC_ID_RAWVIDEO )
{
p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->i_fourcc );
}
......@@ -193,6 +204,8 @@ int E_(OpenVideoEncoder)( vlc_object_t *p_this )
p_sys->i_last_ref_pts = 0;
p_sys->i_buggy_pts_detect = 0;
msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
return VLC_SUCCESS;
}
......@@ -229,7 +242,8 @@ static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts;
/* FIXME, 3-2 pulldown is not handled correctly */
p_block->i_length = 0;//in->i_length;
p_block->i_length = I64C(1000000) * p_enc->i_frame_rate_base /
p_enc->i_frame_rate;
p_block->i_pts = p_sys->p_context->coded_frame->pts;
if( !p_sys->p_context->delay ||
......@@ -257,7 +271,8 @@ static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
{
/* Buggy libavcodec which doesn't update coded_frame->pts
* correctly */
p_block->i_length = 0;//in->i_length;
p_block->i_length = I64C(1000000) * p_enc->i_frame_rate_base /
p_enc->i_frame_rate;
p_block->i_dts = p_block->i_pts = p_pict->date;
}
......@@ -287,28 +302,34 @@ void E_(CloseVideoEncoder)( vlc_object_t *p_this )
int E_(OpenAudioEncoder)( vlc_object_t *p_this )
{
encoder_t *p_enc = (encoder_t *)p_this;
encoder_sys_t *p_sys = p_enc->p_sys;
AVCodecContext *p_context;
encoder_sys_t *p_sys;
AVCodecContext *p_context;
AVCodec *p_codec;
int i_ff_codec;
int i_codec_id, i_cat;
char *psz_namecodec;
i_ff_codec = E_(GetFfmpegCodec)( p_enc->i_fourcc, 0, 0, 0 );
if( i_ff_codec == 0 )
if( !E_(GetFfmpegCodec)( p_enc->i_fourcc, &i_cat, &i_codec_id,
&psz_namecodec ) )
{
msg_Err( p_enc, "cannot find encoder id" );
return VLC_EGENERIC;
}
p_codec = avcodec_find_encoder( i_ff_codec );
if( !p_codec )
if( i_cat != AUDIO_ES )
{
msg_Err( p_enc, "cannot find encoder (avcodec)" );
msg_Err( p_enc, "\"%s\" is not an audio encoder", psz_namecodec );
return VLC_EGENERIC;
}
/* libavcodec needs to be initialized */
/* Initialization must be done before avcodec_find_decoder() */
E_(InitLibavcodec)(p_this);
p_codec = avcodec_find_encoder( i_codec_id );
if( !p_codec )
{
msg_Err( p_enc, "cannot find encoder %s", psz_namecodec );
return VLC_EGENERIC;
}
/* Allocate the memory needed to store the decoder's structure */
if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
{
......@@ -333,18 +354,13 @@ int E_(OpenAudioEncoder)( vlc_object_t *p_this )
p_context->extradata = NULL;
p_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
p_sys->i_samples_delay = 0;
p_sys->i_last_block_size = 0;
p_sys->i_pts = 0;
if( avcodec_open( p_context, p_sys->p_codec ) )
if( avcodec_open( p_context, p_codec ) < 0 )
{
#if 0
if( p_context->channels > 2 )
{
p_context->channels = 2;
id->f_dst.i_channels = 2;
if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
//id->f_dst.i_channels = 2;
if( avcodec_open( p_context, p_codec ) < 0 )
{
msg_Err( p_enc, "cannot open encoder" );
return VLC_EGENERIC;
......@@ -356,26 +372,24 @@ int E_(OpenAudioEncoder)( vlc_object_t *p_this )
msg_Err( p_enc, "cannot open encoder" );
return VLC_EGENERIC;
}
#endif
}
p_enc->i_extra_data = p_context->extradata_size;
p_enc->p_extra_data = p_context->extradata;
p_context->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
p_sys->p_buffer = malloc( p_context->frame_size * 2 *
p_context->channels * 2 );
p_sys->i_frame_size = p_context->frame_size * 2 * p_context->channels;
p_sys->p_buffer = malloc( p_sys->i_frame_size );
p_sys->p_buffer_out = malloc( 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
p_sys->i_frame_size = p_sys->p_context->frame_size * 2 *
p_context->channels;
msg_Warn( p_enc, "avcodec_setup_audio: %d %d %d %d",
p_context->frame_size, p_context->bit_rate, p_context->channels,
p_context->sample_rate );
/* Hack for mp3 transcoding support */
if( p_enc->i_fourcc == VLC_FOURCC( 'm','p','3',' ' ) )
{
p_enc->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
}
p_sys->i_samples_delay = 0;
p_sys->i_pts = 0;
msg_Dbg( p_enc, "found encoder %s", psz_namecodec );
return VLC_SUCCESS;
}
......@@ -387,6 +401,7 @@ static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
{
encoder_sys_t *p_sys = p_enc->p_sys;
block_t *p_block, *p_chain = NULL;
char *p_buffer = p_aout_buf->p_buffer;
int i_samples = p_aout_buf->i_nb_samples;
int i_samples_delay = p_sys->i_samples_delay;
......@@ -412,7 +427,7 @@ static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
p_samples = (int16_t *)p_sys->p_buffer;
memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
p_buffer -= i_delay_size;
i_samples += i_samples_delay;
i_samples += i_samples_delay;
i_samples_delay = 0;
}
else
......@@ -423,14 +438,17 @@ static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out,
2 * AVCODEC_MAX_AUDIO_FRAME_SIZE,
p_samples );
if( i_out <= 0 )
{
break;
}
#if 0
msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );
#endif
if( i_out < 0 ) break;
p_buffer += p_sys->i_frame_size;
p_sys->i_samples_delay -= p_sys->p_context->frame_size;
i_samples = p_sys->p_context->frame_size;
i_samples -= p_sys->p_context->frame_size;
if( i_out == 0 ) continue;
p_block = block_New( p_enc, i_out );
memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
......@@ -447,7 +465,7 @@ static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
}
/* Backup the remaining raw samples */
if( p_sys->i_samples_delay > 0 )
if( i_samples )
{
memcpy( p_sys->p_buffer, p_buffer + i_samples_delay,
i_samples * 2 * p_sys->p_context->channels );
......
......@@ -2,7 +2,7 @@
* ffmpeg.c: video decoder using ffmpeg library
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
* $Id: ffmpeg.c,v 1.55 2003/10/27 01:04:38 gbazin Exp $
* $Id: ffmpeg.c,v 1.56 2003/10/27 17:50:54 gbazin Exp $
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
* Gildas Bazin <gbazin@netcourrier.com>
......@@ -136,10 +136,21 @@ vlc_module_end();
static int OpenDecoder( vlc_object_t *p_this )
{
decoder_t *p_dec = (decoder_t*) p_this;
int i_cat;
int i_cat, i_codec_id;
char *psz_namecodec;
if( !E_(GetFfmpegCodec)( p_dec->p_fifo->i_fourcc, &i_cat, &i_codec_id,
&psz_namecodec ) )
{
return VLC_EGENERIC;
}
if( !E_(GetFfmpegCodec)( p_dec->p_fifo->i_fourcc, &i_cat, NULL, NULL ) )
/* Initialization must be done before avcodec_find_decoder() */
E_(InitLibavcodec)(p_this);
if( !avcodec_find_decoder( i_codec_id ) )
{
msg_Err( p_dec, "codec not found (%s)", psz_namecodec );
return VLC_EGENERIC;
}
......@@ -160,8 +171,6 @@ static int InitDecoder( decoder_t *p_dec )
AVCodecContext *p_context;
AVCodec *p_codec;
E_(InitLibavcodec)( VLC_OBJECT(p_dec->p_fifo) );
/* *** determine codec type *** */
E_(GetFfmpegCodec)( p_dec->p_fifo->i_fourcc,
&i_cat, &i_codec_id, &psz_namecodec );
......@@ -620,6 +629,13 @@ int E_(GetFfmpegCodec)( vlc_fourcc_t i_fourcc, int *pi_cat,
psz_name = "A52 Audio (aka AC3)";
break;
/* AAC audio */
case VLC_FOURCC('m','p','4','a'):
i_cat = AUDIO_ES;
i_codec = CODEC_ID_AAC;
psz_name = "MPEG AAC Audio";
break;
default:
i_cat = UNKNOWN_ES;
i_codec = CODEC_ID_NONE;
......@@ -632,10 +648,10 @@ int E_(GetFfmpegCodec)( vlc_fourcc_t i_fourcc, int *pi_cat,
if( pi_cat ) *pi_cat = i_cat;
if( pi_ffmpeg_codec ) *pi_ffmpeg_codec = i_codec;
if( ppsz_name ) *ppsz_name = psz_name;
return( VLC_TRUE );
return VLC_TRUE;
}
return( VLC_FALSE );
return VLC_FALSE;
}
int E_(GetFfmpegChroma)( vlc_fourcc_t i_chroma )
......
/*****************************************************************************
* vorbis.c: vorbis decoder module making use of libvorbis.
* vorbis.c: vorbis decoder/encoder/packetizer module making use of libvorbis.
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
* $Id: vorbis.c,v 1.20 2003/10/27 01:04:38 gbazin Exp $
* $Id: vorbis.c,v 1.21 2003/10/27 17:50:54 gbazin Exp $
*
* Authors: Gildas Bazin <gbazin@netcourrier.com>
*
......@@ -630,6 +630,7 @@ struct encoder_sys_t
int i_last_block_size;
int i_samples_delay;
int i_channels;
/*
* Packetizer output properties
......@@ -649,7 +650,7 @@ struct encoder_sys_t
static int OpenEncoder( vlc_object_t *p_this )
{
encoder_t *p_enc = (encoder_t *)p_this;
encoder_sys_t *p_sys = p_enc->p_sys;
encoder_sys_t *p_sys;
if( p_enc->i_fourcc != VLC_FOURCC('v','o','r','b') )
{
......@@ -685,6 +686,7 @@ static int OpenEncoder( vlc_object_t *p_this )
vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
vorbis_block_init( &p_sys->vd, &p_sys->vb );
p_sys->i_channels = aout_FormatNbChannels( &p_enc->format.audio );
p_sys->i_last_block_size = 0;
p_sys->i_samples_delay = 0;
p_sys->i_headers = 0;
......@@ -701,7 +703,7 @@ static int OpenEncoder( vlc_object_t *p_this )
static block_t *Headers( encoder_t *p_enc )
{
encoder_sys_t *p_sys = p_enc->p_sys;
block_t *p_block, **pp_block = NULL;
block_t *p_block, *p_chain = NULL;
/* Create theora headers */
if( !p_sys->i_headers )
......@@ -718,14 +720,12 @@ static block_t *Headers( encoder_t *p_enc )
p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
block_ChainAppend( pp_block, p_block );
block_ChainAppend( &p_chain, p_block );
}
p_sys->i_headers = 3;
return *pp_block;
}
return NULL;
return p_chain;
}
/****************************************************************************
......@@ -739,16 +739,15 @@ static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
ogg_packet oggpacket;
block_t *p_block, *p_chain = NULL;
float **buffer;
int i_samples;
int i, j;
p_sys->i_pts = p_aout_buf->start_date -
(mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
(mtime_t)p_enc->format.audio.i_rate;
i_samples = p_aout_buf->i_nb_samples;
p_sys->i_samples_delay += i_samples;
p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
buffer = vorbis_analysis_buffer( &p_sys->vd, i_samples );
buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
#if 0
if( id->ff_dec_c->channels != id->ff_enc_c->channels )
......@@ -765,22 +764,24 @@ static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
}
}
}
#endif
/* convert samples to float and uninterleave */
for( i = 0; i < id->f_dst.i_channels; i++ )
for( i = 0; i < p_sys->i_channels; i++ )
{
for( j = 0 ; j < i_samples ; j++ )
for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
{
buffer[i][j]= ((float)( ((int16_t *)id->p_buffer)
[j*id->f_src.i_channels + i ] ))/ 32768.f;
buffer[i][j]= ((float)( ((int16_t *)p_aout_buf->p_buffer )
[j * p_sys->i_channels + i ] )) / 32768.f;
}
}
#endif
vorbis_analysis_wrote( &p_sys->vd, i_samples );
vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
{
int i_samples;
vorbis_analysis( &p_sys->vb, NULL );
vorbis_bitrate_addblock( &p_sys->vb );
......
This diff is collapsed.
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