sync with en/mplayer.1 rev. 30936
[mplayer/glamo.git] / libmpcodecs / ae_toolame.c
blob851fa845f9163e44f5e67cdb3a4d2e77adbc3b7d
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <inttypes.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include "m_option.h"
26 #include "mp_msg.h"
27 #include "libmpdemux/aviheader.h"
28 #include "libaf/af_format.h"
29 #include "libmpdemux/ms_hdr.h"
30 #include "stream/stream.h"
31 #include "libmpdemux/muxer.h"
32 #include "ae_toolame.h"
33 #include "libmpdemux/mp3_hdr.h"
36 static int
37 param_bitrate = 192,
38 param_psy = 3,
39 param_maxvbr = 0,
40 param_errprot = 0,
41 param_debug = 0;
43 static float param_vbr = 0;
44 static char *param_mode = "stereo";
46 m_option_t toolameopts_conf[] = {
47 {"br", &param_bitrate, CONF_TYPE_INT, 0, 0, 0, NULL},
48 {"mode", &param_mode, CONF_TYPE_STRING, 0, 0, 0, NULL},
49 {"psy", &param_psy, CONF_TYPE_INT, CONF_RANGE, -1, 4, NULL},
50 {"vbr", &param_vbr, CONF_TYPE_FLOAT, CONF_RANGE, -50, 50, NULL},
51 {"maxvbr", &param_maxvbr, CONF_TYPE_INT, 0, 0, 0, NULL},
52 {"errprot", &param_errprot, CONF_TYPE_INT, CONF_RANGE, 0, 1, NULL},
53 {"debug", &param_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
54 {NULL, NULL, 0, 0, 0, 0, NULL}
58 static int bind_toolame(audio_encoder_t *encoder, muxer_stream_t *mux_a)
60 mpae_toolame_ctx *ctx = (mpae_toolame_ctx *) encoder->priv;
62 mux_a->wf = malloc(sizeof(WAVEFORMATEX)+256);
63 mux_a->wf->wFormatTag = 0x50;
64 mux_a->wf->nChannels = encoder->params.channels;
65 mux_a->wf->nSamplesPerSec = encoder->params.sample_rate;
66 mux_a->wf->nAvgBytesPerSec = 125 * encoder->params.bitrate;
68 if(ctx->vbr || ((mux_a->wf->nAvgBytesPerSec * encoder->params.samples_per_frame) % mux_a->wf->nSamplesPerSec))
70 mux_a->h.dwScale = encoder->params.samples_per_frame;
71 mux_a->h.dwRate = encoder->params.sample_rate;
72 mux_a->h.dwSampleSize = 0; // Blocksize not constant
74 else
76 mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * encoder->params.samples_per_frame)/ mux_a->wf->nSamplesPerSec; /* for cbr */
77 mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec;
78 mux_a->h.dwSampleSize = mux_a->h.dwScale;
80 mux_a->wf->nBlockAlign = mux_a->h.dwScale;
81 mux_a->h.dwSuggestedBufferSize = (encoder->params.audio_preload*mux_a->wf->nAvgBytesPerSec)/1000;
82 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign;
84 mux_a->wf->cbSize = 0; //12;
85 mux_a->wf->wBitsPerSample = 0; /* does not apply */
86 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1;
87 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2;
88 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign;
89 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1;
90 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0;
92 // Fix allocation
93 mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize);
95 encoder->input_format = AF_FORMAT_S16_NE;
96 encoder->min_buffer_size = mux_a->h.dwSuggestedBufferSize;
97 encoder->max_buffer_size = mux_a->h.dwSuggestedBufferSize*2;
99 return 1;
102 static int encode_toolame(audio_encoder_t *encoder, uint8_t *dest, void *src, int len, int max_size)
104 mpae_toolame_ctx *ctx = (mpae_toolame_ctx *)encoder->priv;
105 int ret_size = 0, r2, i, nsamples;
106 int16_t *buffer;
108 nsamples = len / (2*encoder->params.channels);
109 buffer = (uint16_t *) src;
110 for(i = 0; i < nsamples; i++)
112 ctx->left_pcm[i] = buffer[ctx->channels * i];
113 ctx->right_pcm[i] = buffer[(ctx->channels * i) + (ctx->channels - 1)];
116 toolame_encode_buffer(ctx->toolame_ctx, ctx->left_pcm, ctx->right_pcm, nsamples, dest, max_size, &ret_size);
117 r2 = mp_decode_mp3_header(dest);
118 mp_msg(MSGT_MENCODER, MSGL_DBG2, "\nSIZE: %d, max: %d, r2: %d\n", ret_size, max_size, r2);
119 if(r2 > 0)
120 ret_size = r2;
121 return ret_size;
124 static int close_toolame(audio_encoder_t *encoder)
126 free(encoder->priv);
127 return 1;
130 static int get_frame_size(audio_encoder_t *encoder)
132 int sz;
133 if(encoder->stream->buffer_len < 4)
134 return 0;
135 sz = mp_decode_mp3_header(encoder->stream->buffer);
136 if(sz <= 0)
137 return 0;
138 return sz;
142 int mpae_init_toolame(audio_encoder_t *encoder)
144 int mode;
145 mpae_toolame_ctx *ctx = NULL;
147 if(encoder->params.channels == 1)
149 mp_msg(MSGT_MENCODER, MSGL_INFO, "ae_toolame, 1 audio channel, forcing mono mode\n");
150 mode = MPG_MD_MONO;
152 else if(encoder->params.channels == 2)
154 if(! strcasecmp(param_mode, "dual"))
155 mode = MPG_MD_DUAL_CHANNEL;
156 else if(! strcasecmp(param_mode, "jstereo"))
157 mode = MPG_MD_JOINT_STEREO;
158 else if(! strcasecmp(param_mode, "stereo"))
159 mode = MPG_MD_STEREO;
160 else
162 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, unknown mode %s, exiting\n", param_mode);
165 else
166 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, Toolame can't encode > 2 channels, exiting\n");
168 ctx = calloc(1, sizeof(mpae_toolame_ctx));
169 if(ctx == NULL)
171 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, couldn't alloc a %d bytes context, exiting\n", sizeof(mpae_toolame_ctx));
172 return 0;
175 ctx->toolame_ctx = toolame_init();
176 if(ctx->toolame_ctx == NULL)
178 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, couldn't initial parameters from libtoolame, exiting\n");
179 free(ctx);
180 return 0;
182 ctx->vbr = 0;
183 ctx->channels = encoder->params.channels;
184 ctx->srate = encoder->params.sample_rate;
186 if(toolame_setMode(ctx->toolame_ctx, mode) != 0)
187 return 0;
189 if(toolame_setPsymodel(ctx->toolame_ctx, param_psy) != 0)
190 return 0;
192 if(toolame_setSampleFreq(ctx->toolame_ctx, encoder->params.sample_rate) != 0)
193 return 0;
195 if(toolame_setBitrate(ctx->toolame_ctx, param_bitrate) != 0)
196 return 0;
198 if(param_errprot)
199 if(toolame_setErrorProtection(ctx->toolame_ctx, TRUE) != 0)
200 return 0;
202 if(param_vbr != 0)
204 if(toolame_setVBR(ctx->toolame_ctx, TRUE) != 0)
205 return 0;
206 if(toolame_setVBRLevel(ctx->toolame_ctx, param_vbr) != 0)
207 return 0;
208 if(toolame_setPadding(ctx->toolame_ctx, FALSE) != 0)
209 return 0;
210 if(param_maxvbr)
212 if(toolame_setVBRUpperBitrate(ctx->toolame_ctx, param_maxvbr) != 0)
213 return 0;
215 ctx->vbr = 1;
218 if(toolame_setVerbosity(ctx->toolame_ctx, param_debug) != 0)
219 return 0;
221 if(toolame_init_params(ctx->toolame_ctx) != 0)
222 return 0;
224 ctx->bitrate = param_bitrate;
225 encoder->params.bitrate = ctx->bitrate;
226 encoder->params.samples_per_frame = 1152;
227 encoder->priv = ctx;
228 encoder->decode_buffer_size = 1152 * 2 * encoder->params.channels;
230 encoder->bind = bind_toolame;
231 encoder->get_frame_size = get_frame_size;
232 encoder->encode = encode_toolame;
233 encoder->close = close_toolame;
235 return 1;