2010-01-30 16:57:40 +00:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer 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.
|
|
|
|
*
|
|
|
|
* MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#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"
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "libaf/af_format.h"
|
2007-03-15 17:51:32 +00:00
|
|
|
#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-05-07 14:50:14 +00:00
|
|
|
#include "ae_twolame.h"
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "libmpdemux/mp3_hdr.h"
|
2005-05-07 14:50:14 +00:00
|
|
|
|
|
|
|
|
2009-05-13 02:58:57 +00:00
|
|
|
static int
|
2005-05-07 14:50:14 +00:00
|
|
|
param_bitrate = 192,
|
|
|
|
param_psy = 3,
|
|
|
|
param_maxvbr = 0,
|
|
|
|
param_errprot = 0,
|
|
|
|
param_debug = 0;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
static float param_vbr = 0;
|
|
|
|
static char *param_mode = "stereo";
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
m_option_t twolameopts_conf[] = {
|
|
|
|
{"br", ¶m_bitrate, CONF_TYPE_INT, 0, 0, 0, NULL},
|
|
|
|
{"mode", ¶m_mode, CONF_TYPE_STRING, 0, 0, 0, NULL},
|
2005-05-08 09:37:36 +00:00
|
|
|
{"psy", ¶m_psy, CONF_TYPE_INT, CONF_RANGE, -1, 4, NULL},
|
2005-05-07 14:50:14 +00:00
|
|
|
{"vbr", ¶m_vbr, CONF_TYPE_FLOAT, CONF_RANGE, -50, 50, NULL},
|
|
|
|
{"maxvbr", ¶m_maxvbr, CONF_TYPE_INT, 0, 0, 0, NULL},
|
|
|
|
{"errprot", ¶m_errprot, CONF_TYPE_INT, CONF_RANGE, 0, 1, NULL},
|
|
|
|
{"debug", ¶m_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
|
|
|
|
{NULL, NULL, 0, 0, 0, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int bind_twolame(audio_encoder_t *encoder, muxer_stream_t *mux_a)
|
|
|
|
{
|
2008-11-02 11:52:49 +00:00
|
|
|
mpae_twolame_ctx *ctx = encoder->priv;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
mux_a->wf = malloc(sizeof(WAVEFORMATEX)+256);
|
|
|
|
mux_a->wf->wFormatTag = 0x50;
|
|
|
|
mux_a->wf->nChannels = encoder->params.channels;
|
|
|
|
mux_a->wf->nSamplesPerSec = encoder->params.sample_rate;
|
|
|
|
mux_a->wf->nAvgBytesPerSec = encoder->params.bitrate / 8;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
if(ctx->vbr || ((mux_a->wf->nAvgBytesPerSec * encoder->params.samples_per_frame) % mux_a->wf->nSamplesPerSec))
|
|
|
|
{
|
|
|
|
mux_a->h.dwScale = encoder->params.samples_per_frame;
|
|
|
|
mux_a->h.dwRate = encoder->params.sample_rate;
|
|
|
|
mux_a->h.dwSampleSize = 0; // Blocksize not constant
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * encoder->params.samples_per_frame)/ mux_a->wf->nSamplesPerSec; /* for cbr */
|
|
|
|
mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec;
|
|
|
|
mux_a->h.dwSampleSize = mux_a->h.dwScale;
|
|
|
|
}
|
|
|
|
mux_a->wf->nBlockAlign = mux_a->h.dwScale;
|
|
|
|
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;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
mux_a->wf->cbSize = 0; //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;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
|
|
|
// Fix allocation
|
2005-05-07 14:50:14 +00:00
|
|
|
mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize);
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
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_twolame(audio_encoder_t *encoder, uint8_t *dest, void *src, int len, int max_size)
|
|
|
|
{
|
2008-11-02 11:52:49 +00:00
|
|
|
mpae_twolame_ctx *ctx = encoder->priv;
|
2005-05-07 14:50:14 +00:00
|
|
|
int ret_size = 0, r2;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
len /= (2*encoder->params.channels);
|
|
|
|
ret_size = twolame_encode_buffer_interleaved(ctx->twolame_ctx, src, len, dest, max_size);
|
2009-05-13 02:58:57 +00:00
|
|
|
r2 = mp_decode_mp3_header(dest);
|
2005-09-13 21:35:41 +00:00
|
|
|
mp_msg(MSGT_MENCODER, MSGL_DBG2, "\nSIZE: %d, max: %d, r2: %d\n", ret_size, max_size, r2);
|
2005-05-07 14:50:14 +00:00
|
|
|
if(r2 > 0)
|
|
|
|
ret_size = r2;
|
|
|
|
return ret_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int close_twolame(audio_encoder_t *encoder)
|
|
|
|
{
|
|
|
|
free(encoder->priv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_frame_size(audio_encoder_t *encoder)
|
|
|
|
{
|
|
|
|
int sz;
|
|
|
|
if(encoder->stream->buffer_len < 4)
|
|
|
|
return 0;
|
|
|
|
sz = mp_decode_mp3_header(encoder->stream->buffer);
|
|
|
|
if(sz <= 0)
|
|
|
|
return 0;
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int mpae_init_twolame(audio_encoder_t *encoder)
|
|
|
|
{
|
|
|
|
int mode;
|
|
|
|
mpae_twolame_ctx *ctx = NULL;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
if(encoder->params.channels == 1)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_MENCODER, MSGL_INFO, "ae_twolame, 1 audio channel, forcing mono mode\n");
|
|
|
|
mode = TWOLAME_MONO;
|
|
|
|
}
|
|
|
|
else if(encoder->params.channels == 2)
|
|
|
|
{
|
|
|
|
if(! strcasecmp(param_mode, "dual"))
|
|
|
|
mode = TWOLAME_DUAL_CHANNEL;
|
|
|
|
else if(! strcasecmp(param_mode, "jstereo"))
|
|
|
|
mode = TWOLAME_JOINT_STEREO;
|
|
|
|
else if(! strcasecmp(param_mode, "stereo"))
|
|
|
|
mode = TWOLAME_STEREO;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, unknown mode %s, exiting\n", param_mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, Twolame can't encode > 2 channels, exiting\n");
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2008-11-02 11:52:49 +00:00
|
|
|
ctx = calloc(1, sizeof(mpae_twolame_ctx));
|
2005-05-07 14:50:14 +00:00
|
|
|
if(ctx == NULL)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, couldn't alloc a %d bytes context, exiting\n", sizeof(mpae_twolame_ctx));
|
|
|
|
return 0;
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
ctx->twolame_ctx = twolame_init();
|
|
|
|
if(ctx->twolame_ctx == NULL)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, couldn't initial parameters from libtwolame, exiting\n");
|
|
|
|
free(ctx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ctx->vbr = 0;
|
|
|
|
|
|
|
|
if(twolame_set_num_channels(ctx->twolame_ctx, encoder->params.channels) != 0)
|
|
|
|
return 0;
|
|
|
|
if(twolame_set_mode(ctx->twolame_ctx, mode) != 0)
|
|
|
|
return 0;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
if(twolame_set_in_samplerate(ctx->twolame_ctx, encoder->params.sample_rate) != 0)
|
|
|
|
return 0;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
if(twolame_set_out_samplerate(ctx->twolame_ctx, encoder->params.sample_rate) != 0)
|
|
|
|
return 0;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
if(encoder->params.sample_rate < 32000)
|
|
|
|
twolame_set_version(ctx->twolame_ctx, TWOLAME_MPEG2);
|
|
|
|
else
|
|
|
|
twolame_set_version(ctx->twolame_ctx, TWOLAME_MPEG1);
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
if(twolame_set_psymodel(ctx->twolame_ctx, param_psy) != 0)
|
|
|
|
return 0;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
if(twolame_set_bitrate(ctx->twolame_ctx, param_bitrate) != 0)
|
|
|
|
return 0;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
if(param_errprot)
|
|
|
|
if(twolame_set_error_protection(ctx->twolame_ctx, TRUE) != 0)
|
|
|
|
return 0;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
if(param_vbr != 0)
|
|
|
|
{
|
|
|
|
if(twolame_set_VBR(ctx->twolame_ctx, TRUE) != 0)
|
|
|
|
return 0;
|
|
|
|
if(twolame_set_VBR_q(ctx->twolame_ctx, param_vbr) != 0)
|
|
|
|
return 0;
|
|
|
|
if(twolame_set_padding(ctx->twolame_ctx, FALSE) != 0)
|
|
|
|
return 0;
|
|
|
|
if(param_maxvbr)
|
|
|
|
{
|
|
|
|
if(twolame_set_VBR_max_bitrate_kbps(ctx->twolame_ctx, param_maxvbr) != 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ctx->vbr = 1;
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
if(twolame_set_verbosity(ctx->twolame_ctx, param_debug) != 0)
|
|
|
|
return 0;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
if(twolame_init_params(ctx->twolame_ctx) != 0)
|
|
|
|
return 0;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
encoder->params.bitrate = param_bitrate * 1000;
|
|
|
|
encoder->params.samples_per_frame = 1152;
|
|
|
|
encoder->priv = ctx;
|
|
|
|
encoder->decode_buffer_size = 1152 * 2 * encoder->params.channels;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
encoder->bind = bind_twolame;
|
|
|
|
encoder->get_frame_size = get_frame_size;
|
|
|
|
encoder->encode = encode_twolame;
|
|
|
|
encoder->close = close_twolame;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-05-07 14:50:14 +00:00
|
|
|
return 1;
|
|
|
|
}
|