Mercurial > mplayer.hg
annotate libmpcodecs/ae_twolame.c @ 30536:39a4dd7ec420
Move GUI-related extern declarations to a GUI header file.
author | diego |
---|---|
date | Sun, 14 Feb 2010 14:33:16 +0000 |
parents | bbb6ebec87a0 |
children | 32725ca88fed |
rev | line source |
---|---|
30421
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
1 /* |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
2 * This file is part of MPlayer. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
3 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
4 * MPlayer is free software; you can redistribute it and/or modify |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
6 * the Free Software Foundation; either version 2 of the License, or |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
7 * (at your option) any later version. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
8 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
9 * MPlayer is distributed in the hope that it will be useful, |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
12 * GNU General Public License for more details. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
13 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
14 * You should have received a copy of the GNU General Public License along |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
17 */ |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
18 |
15359 | 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" | |
17012 | 26 #include "mp_msg.h" |
22601
ed8f90096c65
Add explicit location for headers from the libmpdemux/ directory.
diego
parents:
22600
diff
changeset
|
27 #include "libmpdemux/aviheader.h" |
17012 | 28 #include "libaf/af_format.h" |
22601
ed8f90096c65
Add explicit location for headers from the libmpdemux/ directory.
diego
parents:
22600
diff
changeset
|
29 #include "libmpdemux/ms_hdr.h" |
22600
3c2b4a866c6a
Add explicit location for headers from the stream/ directory.
diego
parents:
21660
diff
changeset
|
30 #include "stream/stream.h" |
22601
ed8f90096c65
Add explicit location for headers from the libmpdemux/ directory.
diego
parents:
22600
diff
changeset
|
31 #include "libmpdemux/muxer.h" |
15359 | 32 #include "ae_twolame.h" |
17012 | 33 #include "libmpdemux/mp3_hdr.h" |
15359 | 34 |
35 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
36 static int |
15359 | 37 param_bitrate = 192, |
38 param_psy = 3, | |
39 param_maxvbr = 0, | |
40 param_errprot = 0, | |
41 param_debug = 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
42 |
15359 | 43 static float param_vbr = 0; |
44 static char *param_mode = "stereo"; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
45 |
15359 | 46 m_option_t twolameopts_conf[] = { |
47 {"br", ¶m_bitrate, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
48 {"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:
15359
diff
changeset
|
49 {"psy", ¶m_psy, CONF_TYPE_INT, CONF_RANGE, -1, 4, NULL}, |
15359 | 50 {"vbr", ¶m_vbr, CONF_TYPE_FLOAT, CONF_RANGE, -50, 50, NULL}, |
51 {"maxvbr", ¶m_maxvbr, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
52 {"errprot", ¶m_errprot, CONF_TYPE_INT, CONF_RANGE, 0, 1, NULL}, | |
53 {"debug", ¶m_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL}, | |
54 {NULL, NULL, 0, 0, 0, 0, NULL} | |
55 }; | |
56 | |
57 | |
58 static int bind_twolame(audio_encoder_t *encoder, muxer_stream_t *mux_a) | |
59 { | |
27843 | 60 mpae_twolame_ctx *ctx = encoder->priv; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
61 |
15359 | 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 = encoder->params.bitrate / 8; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
67 |
15359 | 68 if(ctx->vbr || ((mux_a->wf->nAvgBytesPerSec * encoder->params.samples_per_frame) % mux_a->wf->nSamplesPerSec)) |
69 { | |
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 | |
73 } | |
74 else | |
75 { | |
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; | |
79 } | |
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; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
83 |
15359 | 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; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
91 |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
92 // Fix allocation |
15359 | 93 mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
94 |
15359 | 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; | |
98 | |
99 return 1; | |
100 } | |
101 | |
102 static int encode_twolame(audio_encoder_t *encoder, uint8_t *dest, void *src, int len, int max_size) | |
103 { | |
27843 | 104 mpae_twolame_ctx *ctx = encoder->priv; |
15359 | 105 int ret_size = 0, r2; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
106 |
15359 | 107 len /= (2*encoder->params.channels); |
108 ret_size = twolame_encode_buffer_interleaved(ctx->twolame_ctx, src, len, dest, max_size); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
109 r2 = mp_decode_mp3_header(dest); |
16482 | 110 mp_msg(MSGT_MENCODER, MSGL_DBG2, "\nSIZE: %d, max: %d, r2: %d\n", ret_size, max_size, r2); |
15359 | 111 if(r2 > 0) |
112 ret_size = r2; | |
113 return ret_size; | |
114 } | |
115 | |
116 int close_twolame(audio_encoder_t *encoder) | |
117 { | |
118 free(encoder->priv); | |
119 return 1; | |
120 } | |
121 | |
122 static int get_frame_size(audio_encoder_t *encoder) | |
123 { | |
124 int sz; | |
125 if(encoder->stream->buffer_len < 4) | |
126 return 0; | |
127 sz = mp_decode_mp3_header(encoder->stream->buffer); | |
128 if(sz <= 0) | |
129 return 0; | |
130 return sz; | |
131 } | |
132 | |
133 | |
134 int mpae_init_twolame(audio_encoder_t *encoder) | |
135 { | |
136 int mode; | |
137 mpae_twolame_ctx *ctx = NULL; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
138 |
15359 | 139 if(encoder->params.channels == 1) |
140 { | |
141 mp_msg(MSGT_MENCODER, MSGL_INFO, "ae_twolame, 1 audio channel, forcing mono mode\n"); | |
142 mode = TWOLAME_MONO; | |
143 } | |
144 else if(encoder->params.channels == 2) | |
145 { | |
146 if(! strcasecmp(param_mode, "dual")) | |
147 mode = TWOLAME_DUAL_CHANNEL; | |
148 else if(! strcasecmp(param_mode, "jstereo")) | |
149 mode = TWOLAME_JOINT_STEREO; | |
150 else if(! strcasecmp(param_mode, "stereo")) | |
151 mode = TWOLAME_STEREO; | |
152 else | |
153 { | |
154 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, unknown mode %s, exiting\n", param_mode); | |
155 } | |
156 } | |
157 else | |
158 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, Twolame can't encode > 2 channels, exiting\n"); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
159 |
27843 | 160 ctx = calloc(1, sizeof(mpae_twolame_ctx)); |
15359 | 161 if(ctx == NULL) |
162 { | |
163 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, couldn't alloc a %d bytes context, exiting\n", sizeof(mpae_twolame_ctx)); | |
164 return 0; | |
165 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
166 |
15359 | 167 ctx->twolame_ctx = twolame_init(); |
168 if(ctx->twolame_ctx == NULL) | |
169 { | |
170 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, couldn't initial parameters from libtwolame, exiting\n"); | |
171 free(ctx); | |
172 return 0; | |
173 } | |
174 ctx->vbr = 0; | |
175 | |
176 if(twolame_set_num_channels(ctx->twolame_ctx, encoder->params.channels) != 0) | |
177 return 0; | |
178 if(twolame_set_mode(ctx->twolame_ctx, mode) != 0) | |
179 return 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
180 |
15359 | 181 if(twolame_set_in_samplerate(ctx->twolame_ctx, encoder->params.sample_rate) != 0) |
182 return 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
183 |
15359 | 184 if(twolame_set_out_samplerate(ctx->twolame_ctx, encoder->params.sample_rate) != 0) |
185 return 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
186 |
15359 | 187 if(encoder->params.sample_rate < 32000) |
188 twolame_set_version(ctx->twolame_ctx, TWOLAME_MPEG2); | |
189 else | |
190 twolame_set_version(ctx->twolame_ctx, TWOLAME_MPEG1); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
191 |
15359 | 192 if(twolame_set_psymodel(ctx->twolame_ctx, param_psy) != 0) |
193 return 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
194 |
15359 | 195 if(twolame_set_bitrate(ctx->twolame_ctx, param_bitrate) != 0) |
196 return 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
197 |
15359 | 198 if(param_errprot) |
199 if(twolame_set_error_protection(ctx->twolame_ctx, TRUE) != 0) | |
200 return 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
201 |
15359 | 202 if(param_vbr != 0) |
203 { | |
204 if(twolame_set_VBR(ctx->twolame_ctx, TRUE) != 0) | |
205 return 0; | |
206 if(twolame_set_VBR_q(ctx->twolame_ctx, param_vbr) != 0) | |
207 return 0; | |
208 if(twolame_set_padding(ctx->twolame_ctx, FALSE) != 0) | |
209 return 0; | |
210 if(param_maxvbr) | |
211 { | |
212 if(twolame_set_VBR_max_bitrate_kbps(ctx->twolame_ctx, param_maxvbr) != 0) | |
213 return 0; | |
214 } | |
215 ctx->vbr = 1; | |
216 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
217 |
15359 | 218 if(twolame_set_verbosity(ctx->twolame_ctx, param_debug) != 0) |
219 return 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
220 |
15359 | 221 if(twolame_init_params(ctx->twolame_ctx) != 0) |
222 return 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
223 |
15359 | 224 encoder->params.bitrate = param_bitrate * 1000; |
225 encoder->params.samples_per_frame = 1152; | |
226 encoder->priv = ctx; | |
227 encoder->decode_buffer_size = 1152 * 2 * encoder->params.channels; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
228 |
15359 | 229 encoder->bind = bind_twolame; |
230 encoder->get_frame_size = get_frame_size; | |
231 encoder->encode = encode_twolame; | |
232 encoder->close = close_twolame; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27843
diff
changeset
|
233 |
15359 | 234 return 1; |
235 } | |
236 |