Mercurial > mplayer.hg
annotate libmpcodecs/ae_toolame.c @ 20194:10cebc4e94ae
code simplification and minor bug fix for files restarting rather than moving to the next file within the playlist.
author | vayne |
---|---|
date | Fri, 13 Oct 2006 17:30:41 +0000 |
parents | 6ff3379a0862 |
children | ca9da45d13e9 |
rev | line source |
---|---|
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" |
17012 | 8 #include "mp_msg.h" |
15234 | 9 #include "aviheader.h" |
17012 | 10 #include "libaf/af_format.h" |
15234 | 11 #include "ms_hdr.h" |
12 #include "muxer.h" | |
13425 | 13 #include "ae_toolame.h" |
17012 | 14 #include "libmpdemux/mp3_hdr.h" |
13425 | 15 |
16 | |
17 static int | |
18 param_bitrate = 192, | |
19 param_psy = 3, | |
15265 | 20 param_maxvbr = 0, |
13425 | 21 param_errprot = 0, |
22 param_debug = 0; | |
23 | |
15362
4ff00aa141ef
updated psycho model range; made a parameter file-static in ae_toolame.c
nicodvb
parents:
15281
diff
changeset
|
24 static float param_vbr = 0; |
13425 | 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}, | |
15362
4ff00aa141ef
updated psycho model range; made a parameter file-static in ae_toolame.c
nicodvb
parents:
15281
diff
changeset
|
30 {"psy", ¶m_psy, CONF_TYPE_INT, CONF_RANGE, -1, 4, NULL}, |
15281 | 31 {"vbr", ¶m_vbr, CONF_TYPE_FLOAT, CONF_RANGE, -50, 50, NULL}, |
13425 | 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 { | |
15265 | 41 mpae_toolame_ctx *ctx = (mpae_toolame_ctx *) encoder->priv; |
42 | |
15234 | 43 mux_a->wf = malloc(sizeof(WAVEFORMATEX)+256); |
44 mux_a->wf->wFormatTag = 0x50; | |
45 mux_a->wf->nChannels = encoder->params.channels; | |
46 mux_a->wf->nSamplesPerSec = encoder->params.sample_rate; | |
47 mux_a->wf->nAvgBytesPerSec = 125 * encoder->params.bitrate; | |
48 | |
15265 | 49 if(ctx->vbr || ((mux_a->wf->nAvgBytesPerSec * encoder->params.samples_per_frame) % mux_a->wf->nSamplesPerSec)) |
15234 | 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 | |
15265 | 54 } |
55 else | |
15234 | 56 { |
15265 | 57 mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * encoder->params.samples_per_frame)/ mux_a->wf->nSamplesPerSec; /* for cbr */ |
58 mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec; | |
15234 | 59 mux_a->h.dwSampleSize = mux_a->h.dwScale; |
60 } | |
61 mux_a->wf->nBlockAlign = mux_a->h.dwScale; | |
62 mux_a->h.dwSuggestedBufferSize = (encoder->params.audio_preload*mux_a->wf->nAvgBytesPerSec)/1000; | |
63 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign; | |
64 | |
15265 | 65 mux_a->wf->cbSize = 0; //12; |
15234 | 66 mux_a->wf->wBitsPerSample = 0; /* does not apply */ |
67 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1; | |
68 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2; | |
69 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign; | |
70 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1; | |
71 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0; | |
72 | |
73 // Fix allocation | |
74 mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize); | |
75 | |
76 encoder->input_format = AF_FORMAT_S16_NE; | |
77 encoder->min_buffer_size = mux_a->h.dwSuggestedBufferSize; | |
78 encoder->max_buffer_size = mux_a->h.dwSuggestedBufferSize*2; | |
79 | |
80 return 1; | |
81 } | |
82 | |
83 static int encode_toolame(audio_encoder_t *encoder, uint8_t *dest, void *src, int len, int max_size) | |
84 { | |
85 mpae_toolame_ctx *ctx = (mpae_toolame_ctx *)encoder->priv; | |
15265 | 86 int ret_size = 0, r2, i, nsamples; |
15234 | 87 int16_t *buffer; |
88 | |
89 nsamples = len / (2*encoder->params.channels); | |
90 buffer = (uint16_t *) src; | |
91 for(i = 0; i < nsamples; i++) | |
92 { | |
93 ctx->left_pcm[i] = buffer[ctx->channels * i]; | |
94 ctx->right_pcm[i] = buffer[(ctx->channels * i) + (ctx->channels - 1)]; | |
95 } | |
96 | |
97 toolame_encode_buffer(ctx->toolame_ctx, ctx->left_pcm, ctx->right_pcm, nsamples, dest, max_size, &ret_size); | |
15265 | 98 r2 = mp_decode_mp3_header(dest); |
16482 | 99 mp_msg(MSGT_MENCODER, MSGL_DBG2, "\nSIZE: %d, max: %d, r2: %d\n", ret_size, max_size, r2); |
15265 | 100 if(r2 > 0) |
101 ret_size = r2; | |
15234 | 102 return ret_size; |
103 } | |
104 | |
105 int close_toolame(audio_encoder_t *encoder) | |
106 { | |
107 free(encoder->priv); | |
108 return 1; | |
109 } | |
110 | |
111 static int get_frame_size(audio_encoder_t *encoder) | |
112 { | |
113 int sz; | |
114 if(encoder->stream->buffer_len < 4) | |
115 return 0; | |
116 sz = mp_decode_mp3_header(encoder->stream->buffer); | |
117 if(sz <= 0) | |
118 return 0; | |
119 return sz; | |
120 } | |
121 | |
122 | |
123 int mpae_init_toolame(audio_encoder_t *encoder) | |
13425 | 124 { |
125 int mode; | |
126 mpae_toolame_ctx *ctx = NULL; | |
127 | |
15234 | 128 if(encoder->params.channels == 1) |
13425 | 129 { |
130 mp_msg(MSGT_MENCODER, MSGL_INFO, "ae_toolame, 1 audio channel, forcing mono mode\n"); | |
131 mode = MPG_MD_MONO; | |
132 } | |
15234 | 133 else if(encoder->params.channels == 2) |
13425 | 134 { |
135 if(! strcasecmp(param_mode, "dual")) | |
136 mode = MPG_MD_DUAL_CHANNEL; | |
137 else if(! strcasecmp(param_mode, "jstereo")) | |
138 mode = MPG_MD_JOINT_STEREO; | |
139 else if(! strcasecmp(param_mode, "stereo")) | |
140 mode = MPG_MD_STEREO; | |
141 else | |
142 { | |
143 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, unknown mode %s, exiting\n", param_mode); | |
144 } | |
145 } | |
146 else | |
147 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, Toolame can't encode > 2 channels, exiting\n"); | |
148 | |
149 ctx = (mpae_toolame_ctx *) calloc(1, sizeof(mpae_toolame_ctx)); | |
150 if(ctx == NULL) | |
151 { | |
152 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, couldn't alloc a %d bytes context, exiting\n", sizeof(mpae_toolame_ctx)); | |
15234 | 153 return 0; |
13425 | 154 } |
155 | |
156 ctx->toolame_ctx = toolame_init(); | |
157 if(ctx->toolame_ctx == NULL) | |
158 { | |
159 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, couldn't initial parameters from libtoolame, exiting\n"); | |
160 free(ctx); | |
15234 | 161 return 0; |
13425 | 162 } |
15265 | 163 ctx->vbr = 0; |
15234 | 164 ctx->channels = encoder->params.channels; |
165 ctx->srate = encoder->params.sample_rate; | |
13425 | 166 |
167 if(toolame_setMode(ctx->toolame_ctx, mode) != 0) | |
15234 | 168 return 0; |
13425 | 169 |
170 if(toolame_setPsymodel(ctx->toolame_ctx, param_psy) != 0) | |
15234 | 171 return 0; |
13425 | 172 |
15234 | 173 if(toolame_setSampleFreq(ctx->toolame_ctx, encoder->params.sample_rate) != 0) |
174 return 0; | |
13425 | 175 |
176 if(toolame_setBitrate(ctx->toolame_ctx, param_bitrate) != 0) | |
15234 | 177 return 0; |
13425 | 178 |
179 if(param_errprot) | |
180 if(toolame_setErrorProtection(ctx->toolame_ctx, TRUE) != 0) | |
15234 | 181 return 0; |
13425 | 182 |
15281 | 183 if(param_vbr != 0) |
13425 | 184 { |
185 if(toolame_setVBR(ctx->toolame_ctx, TRUE) != 0) | |
15234 | 186 return 0; |
15265 | 187 if(toolame_setVBRLevel(ctx->toolame_ctx, param_vbr) != 0) |
15234 | 188 return 0; |
13425 | 189 if(toolame_setPadding(ctx->toolame_ctx, FALSE) != 0) |
15234 | 190 return 0; |
15265 | 191 if(param_maxvbr) |
192 { | |
193 if(toolame_setVBRUpperBitrate(ctx->toolame_ctx, param_maxvbr) != 0) | |
194 return 0; | |
195 } | |
196 ctx->vbr = 1; | |
13425 | 197 } |
198 | |
199 if(toolame_setVerbosity(ctx->toolame_ctx, param_debug) != 0) | |
15234 | 200 return 0; |
13425 | 201 |
202 if(toolame_init_params(ctx->toolame_ctx) != 0) | |
15234 | 203 return 0; |
13425 | 204 |
205 ctx->bitrate = param_bitrate; | |
15234 | 206 encoder->params.bitrate = ctx->bitrate; |
207 encoder->params.samples_per_frame = 1152; | |
208 encoder->priv = ctx; | |
209 encoder->decode_buffer_size = 1152 * 2 * encoder->params.channels; | |
13425 | 210 |
15234 | 211 encoder->bind = bind_toolame; |
212 encoder->get_frame_size = get_frame_size; | |
213 encoder->encode = encode_toolame; | |
214 encoder->close = close_toolame; | |
215 | |
216 return 1; | |
13425 | 217 } |
218 |