15234
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3 #include <inttypes.h>
|
15238
|
4 #include <unistd.h>
|
15234
|
5 #include <string.h>
|
15240
|
6 #include <sys/types.h>
|
13425
|
7 #include "m_option.h"
|
|
8 #include "../mp_msg.h"
|
15234
|
9 #include "aviheader.h"
|
|
10 #include "../libaf/af_format.h"
|
|
11 #include "ms_hdr.h"
|
|
12 #include "muxer.h"
|
13425
|
13 #include "ae_toolame.h"
|
15234
|
14 #include "../libmpdemux/mp3_hdr.h"
|
13425
|
15
|
|
16
|
|
17 static int
|
|
18 param_bitrate = 192,
|
|
19 param_psy = 3,
|
|
20 param_maxvbr = 192,
|
|
21 param_errprot = 0,
|
|
22 param_debug = 0;
|
|
23
|
|
24 float param_vbr = 0;
|
|
25 static char *param_mode = "stereo";
|
|
26
|
|
27 m_option_t toolameopts_conf[] = {
|
|
28 {"br", ¶m_bitrate, CONF_TYPE_INT, 0, 0, 0, NULL},
|
|
29 {"mode", ¶m_mode, CONF_TYPE_STRING, 0, 0, 0, NULL},
|
|
30 {"psy", ¶m_psy, CONF_TYPE_INT, CONF_RANGE, 0, 3, NULL},
|
|
31 {"vbr", ¶m_vbr, CONF_TYPE_FLOAT, CONF_RANGE, 0, 50, NULL},
|
|
32 {"maxvbr", ¶m_maxvbr, CONF_TYPE_INT, 0, 0, 0, NULL},
|
|
33 {"errprot", ¶m_errprot, CONF_TYPE_INT, CONF_RANGE, 0, 1, NULL},
|
|
34 {"debug", ¶m_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
|
|
35 {NULL, NULL, 0, 0, 0, 0, NULL}
|
|
36 };
|
|
37
|
|
38
|
15234
|
39 static int bind_toolame(audio_encoder_t *encoder, muxer_stream_t *mux_a)
|
|
40 {
|
|
41 mux_a->wf = malloc(sizeof(WAVEFORMATEX)+256);
|
|
42 mux_a->wf->wFormatTag = 0x50;
|
|
43 mux_a->wf->nChannels = encoder->params.channels;
|
|
44 mux_a->wf->nSamplesPerSec = encoder->params.sample_rate;
|
|
45 mux_a->wf->nAvgBytesPerSec = 125 * encoder->params.bitrate;
|
|
46 mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec;
|
|
47 mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * encoder->params.samples_per_frame)/ mux_a->wf->nSamplesPerSec; /* for cbr */
|
|
48
|
|
49 if((mux_a->wf->nAvgBytesPerSec * encoder->params.samples_per_frame) % mux_a->wf->nSamplesPerSec)
|
|
50 {
|
|
51 mux_a->h.dwScale = encoder->params.samples_per_frame;
|
|
52 mux_a->h.dwRate = encoder->params.sample_rate;
|
|
53 mux_a->h.dwSampleSize = 0; // Blocksize not constant
|
|
54 }
|
|
55 else
|
|
56 {
|
|
57 mux_a->h.dwSampleSize = mux_a->h.dwScale;
|
|
58 }
|
|
59 mux_a->wf->nBlockAlign = mux_a->h.dwScale;
|
|
60 mux_a->h.dwSuggestedBufferSize = (encoder->params.audio_preload*mux_a->wf->nAvgBytesPerSec)/1000;
|
|
61 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign;
|
|
62
|
|
63 mux_a->wf->cbSize = 12;
|
|
64 mux_a->wf->wBitsPerSample = 0; /* does not apply */
|
|
65 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1;
|
|
66 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2;
|
|
67 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign;
|
|
68 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1;
|
|
69 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0;
|
|
70
|
|
71 // Fix allocation
|
|
72 mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize);
|
|
73
|
|
74 encoder->input_format = AF_FORMAT_S16_NE;
|
|
75 encoder->min_buffer_size = mux_a->h.dwSuggestedBufferSize;
|
|
76 encoder->max_buffer_size = mux_a->h.dwSuggestedBufferSize*2;
|
|
77
|
|
78 return 1;
|
|
79 }
|
|
80
|
|
81 static int encode_toolame(audio_encoder_t *encoder, uint8_t *dest, void *src, int len, int max_size)
|
|
82 {
|
|
83 mpae_toolame_ctx *ctx = (mpae_toolame_ctx *)encoder->priv;
|
|
84 int ret_size = 0, i, nsamples;
|
|
85 int16_t *buffer;
|
|
86
|
|
87 nsamples = len / (2*encoder->params.channels);
|
|
88 buffer = (uint16_t *) src;
|
|
89 for(i = 0; i < nsamples; i++)
|
|
90 {
|
|
91 ctx->left_pcm[i] = buffer[ctx->channels * i];
|
|
92 ctx->right_pcm[i] = buffer[(ctx->channels * i) + (ctx->channels - 1)];
|
|
93 }
|
|
94
|
|
95 toolame_encode_buffer(ctx->toolame_ctx, ctx->left_pcm, ctx->right_pcm, nsamples, dest, max_size, &ret_size);
|
|
96 return ret_size;
|
|
97 }
|
|
98
|
|
99 int close_toolame(audio_encoder_t *encoder)
|
|
100 {
|
|
101 free(encoder->priv);
|
|
102 return 1;
|
|
103 }
|
|
104
|
|
105 static int get_frame_size(audio_encoder_t *encoder)
|
|
106 {
|
|
107 int sz;
|
|
108 if(encoder->stream->buffer_len < 4)
|
|
109 return 0;
|
|
110 sz = mp_decode_mp3_header(encoder->stream->buffer);
|
|
111 if(sz <= 0)
|
|
112 return 0;
|
|
113 return sz;
|
|
114 }
|
|
115
|
|
116
|
|
117 int mpae_init_toolame(audio_encoder_t *encoder)
|
13425
|
118 {
|
|
119 int mode;
|
|
120 mpae_toolame_ctx *ctx = NULL;
|
|
121
|
15234
|
122 if(encoder->params.channels == 1)
|
13425
|
123 {
|
|
124 mp_msg(MSGT_MENCODER, MSGL_INFO, "ae_toolame, 1 audio channel, forcing mono mode\n");
|
|
125 mode = MPG_MD_MONO;
|
|
126 }
|
15234
|
127 else if(encoder->params.channels == 2)
|
13425
|
128 {
|
|
129 if(! strcasecmp(param_mode, "dual"))
|
|
130 mode = MPG_MD_DUAL_CHANNEL;
|
|
131 else if(! strcasecmp(param_mode, "jstereo"))
|
|
132 mode = MPG_MD_JOINT_STEREO;
|
|
133 else if(! strcasecmp(param_mode, "stereo"))
|
|
134 mode = MPG_MD_STEREO;
|
|
135 else
|
|
136 {
|
|
137 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, unknown mode %s, exiting\n", param_mode);
|
|
138 }
|
|
139 }
|
|
140 else
|
|
141 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, Toolame can't encode > 2 channels, exiting\n");
|
|
142
|
|
143 ctx = (mpae_toolame_ctx *) calloc(1, sizeof(mpae_toolame_ctx));
|
|
144 if(ctx == NULL)
|
|
145 {
|
|
146 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, couldn't alloc a %d bytes context, exiting\n", sizeof(mpae_toolame_ctx));
|
15234
|
147 return 0;
|
13425
|
148 }
|
|
149
|
|
150 ctx->toolame_ctx = toolame_init();
|
|
151 if(ctx->toolame_ctx == NULL)
|
|
152 {
|
|
153 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, couldn't initial parameters from libtoolame, exiting\n");
|
|
154 free(ctx);
|
15234
|
155 return 0;
|
13425
|
156 }
|
15234
|
157 ctx->channels = encoder->params.channels;
|
|
158 ctx->srate = encoder->params.sample_rate;
|
13425
|
159
|
|
160 if(toolame_setMode(ctx->toolame_ctx, mode) != 0)
|
15234
|
161 return 0;
|
13425
|
162
|
|
163 if(toolame_setPsymodel(ctx->toolame_ctx, param_psy) != 0)
|
15234
|
164 return 0;
|
13425
|
165
|
15234
|
166 if(toolame_setSampleFreq(ctx->toolame_ctx, encoder->params.sample_rate) != 0)
|
|
167 return 0;
|
13425
|
168
|
|
169 if(toolame_setBitrate(ctx->toolame_ctx, param_bitrate) != 0)
|
15234
|
170 return 0;
|
13425
|
171
|
|
172 if(param_errprot)
|
|
173 if(toolame_setErrorProtection(ctx->toolame_ctx, TRUE) != 0)
|
15234
|
174 return 0;
|
13425
|
175
|
|
176 if(param_vbr > 0)
|
|
177 {
|
|
178 if(toolame_setVBR(ctx->toolame_ctx, TRUE) != 0)
|
15234
|
179 return 0;
|
13425
|
180 if(toolame_setVBRLevel(ctx->toolame_ctx, param_maxvbr) != 0)
|
15234
|
181 return 0;
|
13425
|
182 if(toolame_setPadding(ctx->toolame_ctx, FALSE) != 0)
|
15234
|
183 return 0;
|
13425
|
184 if(toolame_setVBRUpperBitrate(ctx->toolame_ctx, param_maxvbr) != 0)
|
15234
|
185 return 0;
|
13425
|
186 }
|
|
187
|
|
188 if(toolame_setVerbosity(ctx->toolame_ctx, param_debug) != 0)
|
15234
|
189 return 0;
|
13425
|
190
|
|
191 if(toolame_init_params(ctx->toolame_ctx) != 0)
|
15234
|
192 return 0;
|
13425
|
193
|
|
194 ctx->bitrate = param_bitrate;
|
15234
|
195 encoder->params.bitrate = ctx->bitrate;
|
|
196 encoder->params.samples_per_frame = 1152;
|
|
197 encoder->priv = ctx;
|
|
198 encoder->decode_buffer_size = 1152 * 2 * encoder->params.channels;
|
13425
|
199
|
15234
|
200 encoder->bind = bind_toolame;
|
|
201 encoder->get_frame_size = get_frame_size;
|
|
202 encoder->encode = encode_toolame;
|
|
203 encoder->close = close_toolame;
|
|
204
|
|
205 return 1;
|
13425
|
206 }
|
|
207
|