2005-04-22 06:59:59 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <inttypes.h>
|
2005-04-22 13:53:56 +00:00
|
|
|
#include <unistd.h>
|
2005-04-22 06:59:59 +00:00
|
|
|
#include <string.h>
|
2005-04-22 17:53:31 +00:00
|
|
|
#include <sys/types.h>
|
2008-03-14 16:46:13 +00:00
|
|
|
#include "config.h"
|
2005-04-22 06:59:59 +00:00
|
|
|
#include "m_option.h"
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "mp_msg.h"
|
2007-03-15 17:51:32 +00:00
|
|
|
#include "libmpdemux/aviheader.h"
|
|
|
|
#include "libmpdemux/ms_hdr.h"
|
2007-03-15 17:30:55 +00:00
|
|
|
#include "stream/stream.h"
|
2007-03-15 17:51:32 +00:00
|
|
|
#include "libmpdemux/muxer.h"
|
2005-04-22 06:59:59 +00:00
|
|
|
#include "ae_lavc.h"
|
|
|
|
#include "help_mp.h"
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "libaf/af_format.h"
|
2007-12-10 16:53:30 +00:00
|
|
|
#include "libaf/reorder_ch.h"
|
2005-04-22 06:59:59 +00:00
|
|
|
#include "libavcodec/avcodec.h"
|
|
|
|
|
|
|
|
static AVCodec *lavc_acodec;
|
|
|
|
static AVCodecContext *lavc_actx;
|
|
|
|
extern char *lavc_param_acodec;
|
|
|
|
extern int lavc_param_abitrate;
|
|
|
|
extern int lavc_param_atag;
|
2006-03-13 02:40:09 +00:00
|
|
|
extern int lavc_param_audio_global_header;
|
2008-02-14 14:23:55 +00:00
|
|
|
extern int avcodec_initialized;
|
2005-04-22 06:59:59 +00:00
|
|
|
static int compressed_frame_size = 0;
|
2007-04-26 15:10:07 +00:00
|
|
|
#ifdef USE_LIBAVFORMAT
|
2007-01-06 14:40:05 +00:00
|
|
|
#include "libavformat/avformat.h"
|
2007-01-21 15:44:58 +00:00
|
|
|
extern const struct AVCodecTag *mp_wav_taglists[];
|
2005-12-05 01:30:31 +00:00
|
|
|
#endif
|
2005-04-22 06:59:59 +00:00
|
|
|
|
|
|
|
static int bind_lavc(audio_encoder_t *encoder, muxer_stream_t *mux_a)
|
|
|
|
{
|
|
|
|
mux_a->wf = malloc(sizeof(WAVEFORMATEX)+lavc_actx->extradata_size+256);
|
|
|
|
mux_a->wf->wFormatTag = lavc_param_atag;
|
|
|
|
mux_a->wf->nChannels = lavc_actx->channels;
|
|
|
|
mux_a->wf->nSamplesPerSec = lavc_actx->sample_rate;
|
|
|
|
mux_a->wf->nAvgBytesPerSec = (lavc_actx->bit_rate / 8);
|
2006-08-10 21:48:41 +00:00
|
|
|
mux_a->avg_rate= lavc_actx->bit_rate;
|
2005-04-22 06:59:59 +00:00
|
|
|
mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec;
|
|
|
|
if(lavc_actx->block_align)
|
|
|
|
mux_a->h.dwSampleSize = mux_a->h.dwScale = lavc_actx->block_align;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * lavc_actx->frame_size)/ mux_a->wf->nSamplesPerSec; /* for cbr */
|
|
|
|
|
|
|
|
if ((mux_a->wf->nAvgBytesPerSec *
|
|
|
|
lavc_actx->frame_size) % mux_a->wf->nSamplesPerSec)
|
|
|
|
{
|
|
|
|
mux_a->h.dwScale = lavc_actx->frame_size;
|
|
|
|
mux_a->h.dwRate = lavc_actx->sample_rate;
|
|
|
|
mux_a->h.dwSampleSize = 0; // Blocksize not constant
|
|
|
|
}
|
|
|
|
else
|
2006-12-29 20:40:19 +00:00
|
|
|
mux_a->h.dwSampleSize = 0;
|
2005-04-22 06:59:59 +00:00
|
|
|
}
|
2006-12-29 20:40:19 +00:00
|
|
|
if(mux_a->h.dwSampleSize)
|
|
|
|
mux_a->wf->nBlockAlign = mux_a->h.dwSampleSize;
|
|
|
|
else
|
|
|
|
mux_a->wf->nBlockAlign = 1;
|
2005-04-22 06:59:59 +00:00
|
|
|
mux_a->h.dwSuggestedBufferSize = (encoder->params.audio_preload*mux_a->wf->nAvgBytesPerSec)/1000;
|
|
|
|
mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign;
|
|
|
|
|
|
|
|
switch(lavc_param_atag)
|
|
|
|
{
|
|
|
|
case 0x11: /* imaadpcm */
|
|
|
|
mux_a->wf->wBitsPerSample = 4;
|
|
|
|
mux_a->wf->cbSize = 2;
|
|
|
|
((uint16_t*)mux_a->wf)[sizeof(WAVEFORMATEX)] =
|
|
|
|
((lavc_actx->block_align - 4 * lavc_actx->channels) / (4 * lavc_actx->channels)) * 8 + 1;
|
|
|
|
break;
|
|
|
|
case 0x55: /* mp3 */
|
|
|
|
mux_a->wf->cbSize = 12;
|
|
|
|
mux_a->wf->wBitsPerSample = 0; /* does not apply */
|
|
|
|
((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1;
|
|
|
|
((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2;
|
|
|
|
((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign;
|
|
|
|
((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1;
|
|
|
|
((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mux_a->wf->wBitsPerSample = 0; /* Unknown */
|
|
|
|
if (lavc_actx->extradata && (lavc_actx->extradata_size > 0))
|
|
|
|
{
|
2005-05-17 21:49:48 +00:00
|
|
|
memcpy(mux_a->wf+1, lavc_actx->extradata, lavc_actx->extradata_size);
|
2005-04-22 06:59:59 +00:00
|
|
|
mux_a->wf->cbSize = lavc_actx->extradata_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mux_a->wf->cbSize = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix allocation
|
|
|
|
mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize);
|
|
|
|
|
|
|
|
encoder->input_format = AF_FORMAT_S16_NE;
|
|
|
|
encoder->min_buffer_size = mux_a->h.dwSuggestedBufferSize;
|
|
|
|
encoder->max_buffer_size = mux_a->h.dwSuggestedBufferSize*2;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int encode_lavc(audio_encoder_t *encoder, uint8_t *dest, void *src, int size, int max_size)
|
|
|
|
{
|
|
|
|
int n;
|
2007-12-10 16:53:30 +00:00
|
|
|
if ((encoder->params.channels == 6 || encoder->params.channels == 5) &&
|
|
|
|
(!strcmp(lavc_acodec->name,"ac3") ||
|
|
|
|
!strcmp(lavc_acodec->name,"libfaac"))) {
|
|
|
|
int isac3 = !strcmp(lavc_acodec->name,"ac3");
|
|
|
|
reorder_channel_nch(src, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
|
|
|
|
isac3 ? AF_CHANNEL_LAYOUT_LAVC_AC3_DEFAULT
|
|
|
|
: AF_CHANNEL_LAYOUT_AAC_DEFAULT,
|
|
|
|
encoder->params.channels,
|
|
|
|
size / 2, 2);
|
|
|
|
}
|
2005-04-22 06:59:59 +00:00
|
|
|
n = avcodec_encode_audio(lavc_actx, dest, size, src);
|
2006-03-13 17:15:51 +00:00
|
|
|
compressed_frame_size = n;
|
2005-04-22 06:59:59 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int close_lavc(audio_encoder_t *encoder)
|
|
|
|
{
|
|
|
|
compressed_frame_size = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_frame_size(audio_encoder_t *encoder)
|
|
|
|
{
|
2006-03-13 17:15:51 +00:00
|
|
|
int sz = compressed_frame_size;
|
|
|
|
compressed_frame_size = 0;
|
|
|
|
return sz;
|
2005-04-22 06:59:59 +00:00
|
|
|
}
|
|
|
|
|
2007-09-09 09:57:36 +00:00
|
|
|
#ifndef USE_LIBAVFORMAT
|
2005-04-22 23:57:43 +00:00
|
|
|
static uint32_t lavc_find_atag(char *codec)
|
|
|
|
{
|
|
|
|
if(codec == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(! strcasecmp(codec, "mp2"))
|
|
|
|
return 0x50;
|
|
|
|
|
|
|
|
if(! strcasecmp(codec, "mp3"))
|
|
|
|
return 0x55;
|
|
|
|
|
|
|
|
if(! strcasecmp(codec, "ac3"))
|
|
|
|
return 0x2000;
|
|
|
|
|
|
|
|
if(! strcasecmp(codec, "adpcm_ima_wav"))
|
|
|
|
return 0x11;
|
|
|
|
|
|
|
|
if(! strncasecmp(codec, "bonk", 4))
|
|
|
|
return 0x2048;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-09-09 09:57:36 +00:00
|
|
|
#endif
|
2005-04-22 23:57:43 +00:00
|
|
|
|
|
|
|
|
2005-04-22 06:59:59 +00:00
|
|
|
int mpae_init_lavc(audio_encoder_t *encoder)
|
|
|
|
{
|
|
|
|
encoder->params.samples_per_frame = encoder->params.sample_rate;
|
|
|
|
encoder->params.bitrate = encoder->params.sample_rate * encoder->params.channels * 2 * 8;
|
|
|
|
|
|
|
|
if(!lavc_param_acodec)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_NoLavcAudioCodecName);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-14 14:23:55 +00:00
|
|
|
if(!avcodec_initialized){
|
2005-04-22 06:59:59 +00:00
|
|
|
avcodec_init();
|
|
|
|
avcodec_register_all();
|
2008-02-14 14:23:55 +00:00
|
|
|
avcodec_initialized=1;
|
2005-04-22 06:59:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lavc_acodec = avcodec_find_encoder_by_name(lavc_param_acodec);
|
|
|
|
if (!lavc_acodec)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LavcAudioCodecNotFound, lavc_param_acodec);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(lavc_param_atag == 0)
|
|
|
|
{
|
2007-04-26 15:10:07 +00:00
|
|
|
#ifdef USE_LIBAVFORMAT
|
2007-01-21 15:44:58 +00:00
|
|
|
lavc_param_atag = av_codec_get_tag(mp_wav_taglists, lavc_acodec->id);
|
2005-04-22 23:57:43 +00:00
|
|
|
#else
|
|
|
|
lavc_param_atag = lavc_find_atag(lavc_param_acodec);
|
|
|
|
#endif
|
2005-04-22 06:59:59 +00:00
|
|
|
if(!lavc_param_atag)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't find wav tag for specified codec, exit\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lavc_actx = avcodec_alloc_context();
|
|
|
|
if(lavc_actx == NULL)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntAllocateLavcContext);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// put sample parameters
|
|
|
|
lavc_actx->channels = encoder->params.channels;
|
|
|
|
lavc_actx->sample_rate = encoder->params.sample_rate;
|
2007-07-31 17:51:39 +00:00
|
|
|
if(lavc_param_abitrate<1000)
|
|
|
|
lavc_actx->bit_rate = encoder->params.bitrate = lavc_param_abitrate * 1000;
|
|
|
|
else
|
|
|
|
lavc_actx->bit_rate = encoder->params.bitrate = lavc_param_abitrate;
|
2005-04-22 06:59:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Special case for adpcm_ima_wav.
|
2007-02-24 10:27:14 +00:00
|
|
|
* The bitrate is only dependent on samplerate.
|
2005-04-22 06:59:59 +00:00
|
|
|
* We have to known frame_size and block_align in advance,
|
|
|
|
* so I just copied the code from libavcodec/adpcm.c
|
|
|
|
*
|
|
|
|
* However, ms adpcm_ima_wav uses a block_align of 2048,
|
|
|
|
* lavc defaults to 1024
|
|
|
|
*/
|
|
|
|
if(lavc_param_atag == 0x11) {
|
|
|
|
int blkalign = 2048;
|
|
|
|
int framesize = (blkalign - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1;
|
|
|
|
lavc_actx->bit_rate = lavc_actx->sample_rate*8*blkalign/framesize;
|
|
|
|
}
|
2006-03-13 02:40:09 +00:00
|
|
|
if((lavc_param_audio_global_header&1)
|
|
|
|
/*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){
|
|
|
|
lavc_actx->flags |= CODEC_FLAG_GLOBAL_HEADER;
|
|
|
|
}
|
|
|
|
if(lavc_param_audio_global_header&2){
|
|
|
|
lavc_actx->flags2 |= CODEC_FLAG2_LOCAL_HEADER;
|
|
|
|
}
|
2005-04-22 06:59:59 +00:00
|
|
|
|
|
|
|
if(avcodec_open(lavc_actx, lavc_acodec) < 0)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntOpenCodec, lavc_param_acodec, lavc_param_abitrate);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(lavc_param_atag == 0x11) {
|
|
|
|
lavc_actx->block_align = 2048;
|
|
|
|
lavc_actx->frame_size = (lavc_actx->block_align - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder->decode_buffer_size = lavc_actx->frame_size * 2 * encoder->params.channels;
|
|
|
|
encoder->bind = bind_lavc;
|
|
|
|
encoder->get_frame_size = get_frame_size;
|
|
|
|
encoder->encode = encode_lavc;
|
|
|
|
encoder->close = close_lavc;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|