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