comparison libmpcodecs/ae_toolame.c @ 15234:e84a6ae9d51e

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