Mercurial > mplayer.hg
annotate libmpcodecs/dec_audio.c @ 28496:147df13f0d46
Replace a mention of MPlayer by MEncoder in the MEncoder section.
author | diego |
---|---|
date | Wed, 11 Feb 2009 16:46:45 +0000 |
parents | 03bb6c3945ff |
children | df67d03dde3b |
rev | line source |
---|---|
5342 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <unistd.h> | |
24892 | 4 #include <assert.h> |
5342 | 5 |
6 #include "config.h" | |
7 #include "mp_msg.h" | |
8 #include "help_mp.h" | |
9 | |
22599
4faee1254928
Add explicit location for headers from the stream/ directory.
diego
parents:
22180
diff
changeset
|
10 #include "stream/stream.h" |
22601
ed8f90096c65
Add explicit location for headers from the libmpdemux/ directory.
diego
parents:
22599
diff
changeset
|
11 #include "libmpdemux/demuxer.h" |
5342 | 12 |
13 #include "codec-cfg.h" | |
22601
ed8f90096c65
Add explicit location for headers from the libmpdemux/ directory.
diego
parents:
22599
diff
changeset
|
14 #include "libmpdemux/stheader.h" |
5342 | 15 |
16 #include "dec_audio.h" | |
17 #include "ad.h" | |
17012 | 18 #include "libaf/af_format.h" |
5342 | 19 |
17012 | 20 #include "libaf/af.h" |
7604
32efb806436e
aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents:
7561
diff
changeset
|
21 |
17635
db13b7295651
memalign segfaults when used without declaration on amd64 (default function return type is int so the pointer was trunctated to 32bits)
aurel
parents:
17625
diff
changeset
|
22 #ifdef HAVE_MALLOC_H |
db13b7295651
memalign segfaults when used without declaration on amd64 (default function return type is int so the pointer was trunctated to 32bits)
aurel
parents:
17625
diff
changeset
|
23 #include <malloc.h> |
db13b7295651
memalign segfaults when used without declaration on amd64 (default function return type is int so the pointer was trunctated to 32bits)
aurel
parents:
17625
diff
changeset
|
24 #endif |
db13b7295651
memalign segfaults when used without declaration on amd64 (default function return type is int so the pointer was trunctated to 32bits)
aurel
parents:
17625
diff
changeset
|
25 |
27397
d47744b95b78
Give a CONFIG_ prefix to preprocessor directives that lacked one and
diego
parents:
27341
diff
changeset
|
26 #ifdef CONFIG_DYNAMIC_PLUGINS |
8152 | 27 #include <dlfcn.h> |
28 #endif | |
29 | |
27341
e7c989f7a7c9
Start unifying names of internal preprocessor directives.
diego
parents:
27249
diff
changeset
|
30 #ifdef CONFIG_FAKE_MONO |
24883 | 31 int fakemono = 0; |
5342 | 32 #endif |
24883 | 33 |
5342 | 34 /* used for ac3surround decoder - set using -channels option */ |
35 int audio_output_channels = 2; | |
24883 | 36 af_cfg_t af_cfg = { 1, NULL }; // Configuration for audio filters |
5342 | 37 |
24883 | 38 void afm_help(void) |
39 { | |
7191
1eadce15446c
-afm/-vfm help implemenetd, some cosmetics of ad/vd codec names/comments
arpi
parents:
7180
diff
changeset
|
40 int i; |
24883 | 41 mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_AvailableAudioFm); |
18237
4231482179b6
Get ride of the several if(identify) messy lines and rearangment of some of the output, both patches by Kiriuja mplayer-patches AT en-directo_net, his changes are barely unrelated, nevertheless Im commiting them thogeter just for the sake of my mental healt, I had both patches already applied on my local three
reynaldo
parents:
17635
diff
changeset
|
42 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_DRIVERS\n"); |
24883 | 43 mp_msg(MSGT_DECAUDIO, MSGL_INFO, " afm: info: (comment)\n"); |
44 for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++) | |
45 if (mpcodecs_ad_drivers[i]->info->comment | |
46 && mpcodecs_ad_drivers[i]->info->comment[0]) | |
47 mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s (%s)\n", | |
48 mpcodecs_ad_drivers[i]->info->short_name, | |
49 mpcodecs_ad_drivers[i]->info->name, | |
50 mpcodecs_ad_drivers[i]->info->comment); | |
51 else | |
52 mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s\n", | |
53 mpcodecs_ad_drivers[i]->info->short_name, | |
54 mpcodecs_ad_drivers[i]->info->name); | |
7191
1eadce15446c
-afm/-vfm help implemenetd, some cosmetics of ad/vd codec names/comments
arpi
parents:
7180
diff
changeset
|
55 } |
1eadce15446c
-afm/-vfm help implemenetd, some cosmetics of ad/vd codec names/comments
arpi
parents:
7180
diff
changeset
|
56 |
24885 | 57 static int init_audio_codec(sh_audio_t *sh_audio) |
5342 | 58 { |
24883 | 59 if ((af_cfg.force & AF_INIT_FORMAT_MASK) == AF_INIT_FLOAT) { |
60 int fmt = AF_FORMAT_FLOAT_NE; | |
61 if (sh_audio->ad_driver->control(sh_audio, ADCTRL_QUERY_FORMAT, | |
62 &fmt) == CONTROL_TRUE) { | |
63 sh_audio->sample_format = fmt; | |
64 sh_audio->samplesize = 4; | |
65 } | |
66 } | |
67 if (!sh_audio->ad_driver->preinit(sh_audio)) { | |
68 mp_msg(MSGT_DECAUDIO, MSGL_ERR, MSGTR_ADecoderPreinitFailed); | |
69 return 0; | |
70 } | |
5342 | 71 |
24883 | 72 /* allocate audio in buffer: */ |
73 if (sh_audio->audio_in_minsize > 0) { | |
74 sh_audio->a_in_buffer_size = sh_audio->audio_in_minsize; | |
75 mp_msg(MSGT_DECAUDIO, MSGL_V, MSGTR_AllocatingBytesForInputBuffer, | |
76 sh_audio->a_in_buffer_size); | |
27765
03bb6c3945ff
Use av_malloc/av_free for audio-related buffers to avoid crashes due to
reimar
parents:
27397
diff
changeset
|
77 sh_audio->a_in_buffer = av_mallocz(sh_audio->a_in_buffer_size); |
24883 | 78 sh_audio->a_in_buffer_len = 0; |
79 } | |
5458 | 80 |
24883 | 81 sh_audio->a_buffer_size = sh_audio->audio_out_minsize + MAX_OUTBURST; |
5342 | 82 |
24883 | 83 mp_msg(MSGT_DECAUDIO, MSGL_V, MSGTR_AllocatingBytesForOutputBuffer, |
84 sh_audio->audio_out_minsize, MAX_OUTBURST, sh_audio->a_buffer_size); | |
5342 | 85 |
27765
03bb6c3945ff
Use av_malloc/av_free for audio-related buffers to avoid crashes due to
reimar
parents:
27397
diff
changeset
|
86 sh_audio->a_buffer = av_mallocz(sh_audio->a_buffer_size); |
24883 | 87 if (!sh_audio->a_buffer) { |
88 mp_msg(MSGT_DECAUDIO, MSGL_ERR, MSGTR_CantAllocAudioBuf); | |
89 return 0; | |
90 } | |
91 sh_audio->a_buffer_len = 0; | |
5342 | 92 |
24883 | 93 if (!sh_audio->ad_driver->init(sh_audio)) { |
94 mp_msg(MSGT_DECAUDIO, MSGL_WARN, MSGTR_ADecoderInitFailed); | |
95 uninit_audio(sh_audio); // free buffers | |
96 return 0; | |
97 } | |
98 | |
25962 | 99 sh_audio->initialized = 1; |
5458 | 100 |
24883 | 101 if (!sh_audio->channels || !sh_audio->samplerate) { |
102 mp_msg(MSGT_DECAUDIO, MSGL_WARN, MSGTR_UnknownAudio); | |
103 uninit_audio(sh_audio); // free buffers | |
104 return 0; | |
105 } | |
5342 | 106 |
24883 | 107 if (!sh_audio->o_bps) |
108 sh_audio->o_bps = sh_audio->channels * sh_audio->samplerate | |
109 * sh_audio->samplesize; | |
7522 | 110 |
24883 | 111 mp_msg(MSGT_DECAUDIO, MSGL_INFO, |
112 "AUDIO: %d Hz, %d ch, %s, %3.1f kbit/%3.2f%% (ratio: %d->%d)\n", | |
113 sh_audio->samplerate, sh_audio->channels, | |
114 af_fmt2str_short(sh_audio->sample_format), | |
115 sh_audio->i_bps * 8 * 0.001, | |
116 ((float) sh_audio->i_bps / sh_audio->o_bps) * 100.0, | |
117 sh_audio->i_bps, sh_audio->o_bps); | |
118 mp_msg(MSGT_IDENTIFY, MSGL_INFO, | |
119 "ID_AUDIO_BITRATE=%d\nID_AUDIO_RATE=%d\n" "ID_AUDIO_NCH=%d\n", | |
120 sh_audio->i_bps * 8, sh_audio->samplerate, sh_audio->channels); | |
7604
32efb806436e
aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents:
7561
diff
changeset
|
121 |
24884 | 122 sh_audio->a_out_buffer_size = 0; |
123 sh_audio->a_out_buffer = NULL; | |
124 sh_audio->a_out_buffer_len = 0; | |
24883 | 125 |
126 return 1; | |
5342 | 127 } |
128 | |
24885 | 129 static int init_audio(sh_audio_t *sh_audio, char *codecname, char *afm, |
25661
293aeec83153
Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents:
25254
diff
changeset
|
130 int status, stringset_t *selected) |
24883 | 131 { |
132 unsigned int orig_fourcc = sh_audio->wf ? sh_audio->wf->wFormatTag : 0; | |
16321
efbfac98cab1
Allow forcing of demuxers and codecs by prepending '+'
reimar
parents:
16306
diff
changeset
|
133 int force = 0; |
efbfac98cab1
Allow forcing of demuxers and codecs by prepending '+'
reimar
parents:
16306
diff
changeset
|
134 if (codecname && codecname[0] == '+') { |
24883 | 135 codecname = &codecname[1]; |
136 force = 1; | |
16321
efbfac98cab1
Allow forcing of demuxers and codecs by prepending '+'
reimar
parents:
16306
diff
changeset
|
137 } |
24883 | 138 sh_audio->codec = NULL; |
139 while (1) { | |
140 ad_functions_t *mpadec; | |
7522 | 141 int i; |
14819
601e2c8a2922
Remove file-global mpadec, add ad_driver member to sh_audio_t instead.
hzoli
parents:
14818
diff
changeset
|
142 sh_audio->ad_driver = 0; |
7522 | 143 // restore original fourcc: |
24883 | 144 if (sh_audio->wf) |
145 sh_audio->wf->wFormatTag = i = orig_fourcc; | |
146 if (!(sh_audio->codec = find_audio_codec(sh_audio->format, | |
147 sh_audio->wf ? (&i) : NULL, | |
148 sh_audio->codec, force))) | |
149 break; | |
150 if (sh_audio->wf) | |
151 sh_audio->wf->wFormatTag = i; | |
7522 | 152 // ok we found one codec |
25661
293aeec83153
Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents:
25254
diff
changeset
|
153 if (stringset_test(selected, sh_audio->codec->name)) |
24883 | 154 continue; // already tried & failed |
155 if (codecname && strcmp(sh_audio->codec->name, codecname)) | |
156 continue; // -ac | |
157 if (afm && strcmp(sh_audio->codec->drv, afm)) | |
158 continue; // afm doesn't match | |
159 if (!force && sh_audio->codec->status < status) | |
160 continue; // too unstable | |
25661
293aeec83153
Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents:
25254
diff
changeset
|
161 stringset_add(selected, sh_audio->codec->name); // tagging it |
7522 | 162 // ok, it matches all rules, let's find the driver! |
24883 | 163 for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++) |
164 if (!strcmp(mpcodecs_ad_drivers[i]->info->short_name, | |
165 sh_audio->codec->drv)) | |
166 break; | |
167 mpadec = mpcodecs_ad_drivers[i]; | |
27397
d47744b95b78
Give a CONFIG_ prefix to preprocessor directives that lacked one and
diego
parents:
27341
diff
changeset
|
168 #ifdef CONFIG_DYNAMIC_PLUGINS |
24883 | 169 if (!mpadec) { |
8152 | 170 /* try to open shared decoder plugin */ |
171 int buf_len; | |
172 char *buf; | |
173 ad_functions_t *funcs_sym; | |
174 ad_info_t *info_sym; | |
24883 | 175 |
176 buf_len = | |
177 strlen(MPLAYER_LIBDIR) + strlen(sh_audio->codec->drv) + 16; | |
8152 | 178 buf = malloc(buf_len); |
179 if (!buf) | |
180 break; | |
24883 | 181 snprintf(buf, buf_len, "%s/mplayer/ad_%s.so", MPLAYER_LIBDIR, |
182 sh_audio->codec->drv); | |
183 mp_msg(MSGT_DECAUDIO, MSGL_DBG2, | |
184 "Trying to open external plugin: %s\n", buf); | |
8152 | 185 sh_audio->dec_handle = dlopen(buf, RTLD_LAZY); |
186 if (!sh_audio->dec_handle) | |
187 break; | |
188 snprintf(buf, buf_len, "mpcodecs_ad_%s", sh_audio->codec->drv); | |
189 funcs_sym = dlsym(sh_audio->dec_handle, buf); | |
24883 | 190 if (!funcs_sym || !funcs_sym->info || !funcs_sym->preinit |
191 || !funcs_sym->init || !funcs_sym->uninit | |
192 || !funcs_sym->control || !funcs_sym->decode_audio) | |
8152 | 193 break; |
194 info_sym = funcs_sym->info; | |
195 if (strcmp(info_sym->short_name, sh_audio->codec->drv)) | |
196 break; | |
197 free(buf); | |
198 mpadec = funcs_sym; | |
24883 | 199 mp_msg(MSGT_DECAUDIO, MSGL_V, |
200 "Using external decoder plugin (%s/mplayer/ad_%s.so)!\n", | |
201 MPLAYER_LIBDIR, sh_audio->codec->drv); | |
8152 | 202 } |
203 #endif | |
24883 | 204 if (!mpadec) { // driver not available (==compiled in) |
205 mp_msg(MSGT_DECAUDIO, MSGL_ERR, | |
206 MSGTR_AudioCodecFamilyNotAvailableStr, | |
207 sh_audio->codec->name, sh_audio->codec->drv); | |
7522 | 208 continue; |
209 } | |
210 // it's available, let's try to init! | |
211 // init() | |
24883 | 212 mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_OpeningAudioDecoder, |
213 mpadec->info->short_name, mpadec->info->name); | |
14819
601e2c8a2922
Remove file-global mpadec, add ad_driver member to sh_audio_t instead.
hzoli
parents:
14818
diff
changeset
|
214 sh_audio->ad_driver = mpadec; |
24883 | 215 if (!init_audio_codec(sh_audio)) { |
216 mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_ADecoderInitFailed); | |
217 continue; // try next... | |
7522 | 218 } |
219 // Yeah! We got it! | |
220 return 1; | |
221 } | |
222 return 0; | |
223 } | |
224 | |
24883 | 225 int init_best_audio_codec(sh_audio_t *sh_audio, char **audio_codec_list, |
226 char **audio_fm_list) | |
227 { | |
25661
293aeec83153
Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents:
25254
diff
changeset
|
228 stringset_t selected; |
24883 | 229 char *ac_l_default[2] = { "", (char *) NULL }; |
230 // hack: | |
231 if (!audio_codec_list) | |
232 audio_codec_list = ac_l_default; | |
233 // Go through the codec.conf and find the best codec... | |
25962 | 234 sh_audio->initialized = 0; |
25661
293aeec83153
Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents:
25254
diff
changeset
|
235 stringset_init(&selected); |
25962 | 236 while (!sh_audio->initialized && *audio_codec_list) { |
24883 | 237 char *audio_codec = *(audio_codec_list++); |
238 if (audio_codec[0]) { | |
239 if (audio_codec[0] == '-') { | |
240 // disable this codec: | |
25661
293aeec83153
Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents:
25254
diff
changeset
|
241 stringset_add(&selected, audio_codec + 1); |
24883 | 242 } else { |
243 // forced codec by name: | |
244 mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_ForcedAudioCodec, | |
245 audio_codec); | |
25661
293aeec83153
Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents:
25254
diff
changeset
|
246 init_audio(sh_audio, audio_codec, NULL, -1, &selected); |
24883 | 247 } |
248 } else { | |
249 int status; | |
250 // try in stability order: UNTESTED, WORKING, BUGGY. | |
251 // never try CRASHING. | |
252 if (audio_fm_list) { | |
253 char **fmlist = audio_fm_list; | |
254 // try first the preferred codec families: | |
25962 | 255 while (!sh_audio->initialized && *fmlist) { |
24883 | 256 char *audio_fm = *(fmlist++); |
257 mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_TryForceAudioFmtStr, | |
258 audio_fm); | |
259 for (status = CODECS_STATUS__MAX; | |
260 status >= CODECS_STATUS__MIN; --status) | |
25661
293aeec83153
Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents:
25254
diff
changeset
|
261 if (init_audio(sh_audio, NULL, audio_fm, status, &selected)) |
24883 | 262 break; |
263 } | |
264 } | |
25962 | 265 if (!sh_audio->initialized) |
24883 | 266 for (status = CODECS_STATUS__MAX; status >= CODECS_STATUS__MIN; |
267 --status) | |
25661
293aeec83153
Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents:
25254
diff
changeset
|
268 if (init_audio(sh_audio, NULL, NULL, status, &selected)) |
24883 | 269 break; |
270 } | |
7522 | 271 } |
25661
293aeec83153
Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents:
25254
diff
changeset
|
272 stringset_free(&selected); |
24883 | 273 |
25962 | 274 if (!sh_audio->initialized) { |
24883 | 275 mp_msg(MSGT_DECAUDIO, MSGL_ERR, MSGTR_CantFindAudioCodec, |
276 sh_audio->format); | |
277 mp_msg(MSGT_DECAUDIO, MSGL_HINT, MSGTR_RTFMCodecs); | |
278 return 0; // failed | |
7522 | 279 } |
280 | |
24883 | 281 mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_SelectedAudioCodec, |
282 sh_audio->codec->name, sh_audio->codec->drv, sh_audio->codec->info); | |
283 return 1; // success | |
7522 | 284 } |
285 | |
5342 | 286 void uninit_audio(sh_audio_t *sh_audio) |
287 { | |
24883 | 288 if (sh_audio->afilter) { |
289 mp_msg(MSGT_DECAUDIO, MSGL_V, "Uninit audio filters...\n"); | |
7604
32efb806436e
aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents:
7561
diff
changeset
|
290 af_uninit(sh_audio->afilter); |
10803 | 291 free(sh_audio->afilter); |
24883 | 292 sh_audio->afilter = NULL; |
7604
32efb806436e
aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents:
7561
diff
changeset
|
293 } |
25962 | 294 if (sh_audio->initialized) { |
24883 | 295 mp_msg(MSGT_DECAUDIO, MSGL_V, MSGTR_UninitAudioStr, |
296 sh_audio->codec->drv); | |
14819
601e2c8a2922
Remove file-global mpadec, add ad_driver member to sh_audio_t instead.
hzoli
parents:
14818
diff
changeset
|
297 sh_audio->ad_driver->uninit(sh_audio); |
27397
d47744b95b78
Give a CONFIG_ prefix to preprocessor directives that lacked one and
diego
parents:
27341
diff
changeset
|
298 #ifdef CONFIG_DYNAMIC_PLUGINS |
8152 | 299 if (sh_audio->dec_handle) |
300 dlclose(sh_audio->dec_handle); | |
301 #endif | |
25962 | 302 sh_audio->initialized = 0; |
7240 | 303 } |
24884 | 304 free(sh_audio->a_out_buffer); |
24883 | 305 sh_audio->a_out_buffer = NULL; |
306 sh_audio->a_out_buffer_size = 0; | |
27765
03bb6c3945ff
Use av_malloc/av_free for audio-related buffers to avoid crashes due to
reimar
parents:
27397
diff
changeset
|
307 av_freep(&sh_audio->a_buffer); |
03bb6c3945ff
Use av_malloc/av_free for audio-related buffers to avoid crashes due to
reimar
parents:
27397
diff
changeset
|
308 av_freep(&sh_audio->a_in_buffer); |
5342 | 309 } |
310 | |
24883 | 311 |
312 int init_audio_filters(sh_audio_t *sh_audio, int in_samplerate, | |
24894 | 313 int *out_samplerate, int *out_channels, int *out_format) |
24883 | 314 { |
315 af_stream_t *afs = sh_audio->afilter; | |
316 if (!afs) { | |
317 afs = malloc(sizeof(af_stream_t)); | |
318 memset(afs, 0, sizeof(af_stream_t)); | |
319 } | |
320 // input format: same as codec's output format: | |
321 afs->input.rate = in_samplerate; | |
24894 | 322 afs->input.nch = sh_audio->channels; |
323 afs->input.format = sh_audio->sample_format; | |
24883 | 324 af_fix_parameters(&(afs->input)); |
7604
32efb806436e
aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents:
7561
diff
changeset
|
325 |
24883 | 326 // output format: same as ao driver's input format (if missing, fallback to input) |
327 afs->output.rate = *out_samplerate; | |
328 afs->output.nch = *out_channels; | |
329 afs->output.format = *out_format; | |
330 af_fix_parameters(&(afs->output)); | |
331 | |
332 // filter config: | |
333 memcpy(&afs->cfg, &af_cfg, sizeof(af_cfg_t)); | |
7604
32efb806436e
aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents:
7561
diff
changeset
|
334 |
24883 | 335 mp_msg(MSGT_DECAUDIO, MSGL_V, MSGTR_BuildingAudioFilterChain, |
336 afs->input.rate, afs->input.nch, | |
337 af_fmt2str_short(afs->input.format), afs->output.rate, | |
338 afs->output.nch, af_fmt2str_short(afs->output.format)); | |
339 | |
340 // let's autoprobe it! | |
341 if (0 != af_init(afs)) { | |
342 sh_audio->afilter = NULL; | |
343 free(afs); | |
344 return 0; // failed :( | |
345 } | |
346 | |
347 *out_samplerate = afs->output.rate; | |
348 *out_channels = afs->output.nch; | |
349 *out_format = afs->output.format; | |
15811
9b4bbb6098f6
make -srate work again, unify audio filter init and preinit.
reimar
parents:
15789
diff
changeset
|
350 |
24893 | 351 sh_audio->a_out_buffer_len = 0; |
7604
32efb806436e
aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents:
7561
diff
changeset
|
352 |
24883 | 353 // ok! |
354 sh_audio->afilter = (void *) afs; | |
355 return 1; | |
7604
32efb806436e
aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents:
7561
diff
changeset
|
356 } |
32efb806436e
aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents:
7561
diff
changeset
|
357 |
24892 | 358 static int filter_n_bytes(sh_audio_t *sh, int len) |
5342 | 359 { |
25252 | 360 int error = 0; |
25253 | 361 // Filter |
362 af_data_t filter_input = { | |
363 .audio = sh->a_buffer, | |
364 .rate = sh->samplerate, | |
365 .nch = sh->channels, | |
366 .format = sh->sample_format | |
367 }; | |
368 af_data_t *filter_output; | |
24892 | 369 |
25252 | 370 assert(len-1 + sh->audio_out_minsize <= sh->a_buffer_size); |
24892 | 371 |
372 // Decode more bytes if needed | |
373 while (sh->a_buffer_len < len) { | |
374 unsigned char *buf = sh->a_buffer + sh->a_buffer_len; | |
375 int minlen = len - sh->a_buffer_len; | |
376 int maxlen = sh->a_buffer_size - sh->a_buffer_len; | |
377 int ret = sh->ad_driver->decode_audio(sh, buf, minlen, maxlen); | |
378 if (ret <= 0) { | |
379 error = -1; | |
380 len = sh->a_buffer_len; | |
381 break; | |
382 } | |
383 sh->a_buffer_len += ret; | |
384 } | |
24883 | 385 |
25254 | 386 filter_input.len = len; |
24892 | 387 af_fix_parameters(&filter_input); |
25253 | 388 filter_output = af_play(sh->afilter, &filter_input); |
24892 | 389 if (!filter_output) |
390 return -1; | |
391 if (sh->a_out_buffer_size < sh->a_out_buffer_len + filter_output->len) { | |
392 int newlen = sh->a_out_buffer_len + filter_output->len; | |
393 mp_msg(MSGT_DECAUDIO, MSGL_V, "Increasing filtered audio buffer size " | |
394 "from %d to %d\n", sh->a_out_buffer_size, newlen); | |
395 sh->a_out_buffer = realloc(sh->a_out_buffer, newlen); | |
396 sh->a_out_buffer_size = newlen; | |
397 } | |
398 memcpy(sh->a_out_buffer + sh->a_out_buffer_len, filter_output->audio, | |
399 filter_output->len); | |
400 sh->a_out_buffer_len += filter_output->len; | |
401 | |
402 // remove processed data from decoder buffer: | |
403 sh->a_buffer_len -= len; | |
404 memmove(sh->a_buffer, sh->a_buffer + len, sh->a_buffer_len); | |
405 | |
406 return error; | |
407 } | |
408 | |
409 /* Try to get at least minlen decoded+filtered bytes in sh_audio->a_out_buffer | |
410 * (total length including possible existing data). | |
411 * Return 0 on success, -1 on error/EOF (not distinguished). | |
412 * In the former case sh_audio->a_out_buffer_len is always >= minlen | |
413 * on return. In case of EOF/error it might or might not be. | |
414 * Can reallocate sh_audio->a_out_buffer if needed to fit all filter output. */ | |
415 int decode_audio(sh_audio_t *sh_audio, int minlen) | |
416 { | |
24961 | 417 // Indicates that a filter seems to be buffering large amounts of data |
418 int huge_filter_buffer = 0; | |
24892 | 419 // Decoded audio must be cut at boundaries of this many bytes |
27249 | 420 int unitsize = sh_audio->channels * sh_audio->samplesize * 16; |
24892 | 421 |
422 /* Filter output size will be about filter_multiplier times input size. | |
423 * If some filter buffers audio in big blocks this might only hold | |
424 * as average over time. */ | |
425 double filter_multiplier = af_calc_filter_multiplier(sh_audio->afilter); | |
7604
32efb806436e
aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents:
7561
diff
changeset
|
426 |
24892 | 427 /* If the decoder set audio_out_minsize then it can do the equivalent of |
428 * "while (output_len < target_len) output_len += audio_out_minsize;", | |
429 * so we must guarantee there is at least audio_out_minsize-1 bytes | |
430 * more space in the output buffer than the minimum length we try to | |
431 * decode. */ | |
432 int max_decode_len = sh_audio->a_buffer_size - sh_audio->audio_out_minsize; | |
433 max_decode_len -= max_decode_len % unitsize; | |
7604
32efb806436e
aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents:
7561
diff
changeset
|
434 |
24892 | 435 while (sh_audio->a_out_buffer_len < minlen) { |
436 int declen = (minlen - sh_audio->a_out_buffer_len) / filter_multiplier | |
437 + (unitsize << 5); // some extra for possible filter buffering | |
24961 | 438 if (huge_filter_buffer) |
24892 | 439 /* Some filter must be doing significant buffering if the estimated |
440 * input length didn't produce enough output from filters. | |
441 * Feed the filters 2k bytes at a time until we have enough output. | |
442 * Very small amounts could make filtering inefficient while large | |
443 * amounts can make MPlayer demux the file unnecessarily far ahead | |
444 * to get audio data and buffer video frames in memory while doing | |
445 * so. However the performance impact of either is probably not too | |
446 * significant as long as the value is not completely insane. */ | |
24961 | 447 declen = 2000; |
24892 | 448 declen -= declen % unitsize; |
24961 | 449 if (declen > max_decode_len) |
450 declen = max_decode_len; | |
451 else | |
452 /* if this iteration does not fill buffer, we must have lots | |
453 * of buffering in filters */ | |
454 huge_filter_buffer = 1; | |
455 if (filter_n_bytes(sh_audio, declen) < 0) | |
456 return -1; | |
24883 | 457 } |
24892 | 458 return 0; |
5342 | 459 } |
460 | |
461 void resync_audio_stream(sh_audio_t *sh_audio) | |
462 { | |
24883 | 463 sh_audio->a_in_buffer_len = 0; // clear audio input buffer |
25962 | 464 if (!sh_audio->initialized) |
24883 | 465 return; |
466 sh_audio->ad_driver->control(sh_audio, ADCTRL_RESYNC_STREAM, NULL); | |
5342 | 467 } |
468 | |
469 void skip_audio_frame(sh_audio_t *sh_audio) | |
470 { | |
25962 | 471 if (!sh_audio->initialized) |
24883 | 472 return; |
473 if (sh_audio->ad_driver->control(sh_audio, ADCTRL_SKIP_FRAME, NULL) == | |
474 CONTROL_TRUE) | |
475 return; | |
476 // default skip code: | |
477 ds_fill_buffer(sh_audio->ds); // skip block | |
5342 | 478 } |