1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <fcntl.h>
21 #include <string.h>
22 #include <sys/mman.h>
23 #include <sys/stat.h>
24 #include <errno.h>
25 #include <inttypes.h>
26 #include <time.h>
27 #include <math.h>
28 #include <audio_utils/primitives.h>
29 #include <audio_utils/sndfile.h>
30 #include <android-base/macros.h>
31 #include <utils/Vector.h>
32 #include <media/AudioBufferProvider.h>
33 #include <media/AudioResampler.h>
34
35 using namespace android;
36
37 static bool gVerbose = false;
38
usage(const char * name)39 static int usage(const char* name) {
40 fprintf(stderr,"Usage: %s [-p] [-f] [-F] [-v] [-c channels]"
41 " [-q {dq|lq|mq|hq|vhq|dlq|dmq|dhq}]"
42 " [-i input-sample-rate] [-o output-sample-rate]"
43 " [-O csv] [-P csv] [<input-file>]"
44 " <output-file>\n", name);
45 fprintf(stderr," -p enable profiling\n");
46 fprintf(stderr," -f enable filter profiling\n");
47 fprintf(stderr," -F enable floating point -q {dlq|dmq|dhq} only");
48 fprintf(stderr," -v verbose : log buffer provider calls\n");
49 fprintf(stderr," -c # channels (1-2 for lq|mq|hq; 1-8 for dlq|dmq|dhq)\n");
50 fprintf(stderr," -q resampler quality\n");
51 fprintf(stderr," dq : default quality\n");
52 fprintf(stderr," lq : low quality\n");
53 fprintf(stderr," mq : medium quality\n");
54 fprintf(stderr," hq : high quality\n");
55 fprintf(stderr," vhq : very high quality\n");
56 fprintf(stderr," dlq : dynamic low quality\n");
57 fprintf(stderr," dmq : dynamic medium quality\n");
58 fprintf(stderr," dhq : dynamic high quality\n");
59 fprintf(stderr," -i input file sample rate (ignored if input file is specified)\n");
60 fprintf(stderr," -o output file sample rate\n");
61 fprintf(stderr," -O # frames output per call to resample() in CSV format\n");
62 fprintf(stderr," -P # frames provided per call to resample() in CSV format\n");
63 return -1;
64 }
65
66 // Convert a list of integers in CSV format to a Vector of those values.
67 // Returns the number of elements in the list, or -1 on error.
parseCSV(const char * string,Vector<int> & values)68 int parseCSV(const char *string, Vector<int>& values)
69 {
70 // pass 1: count the number of values and do syntax check
71 size_t numValues = 0;
72 bool hadDigit = false;
73 for (const char *p = string; ; ) {
74 switch (*p++) {
75 case '0': case '1': case '2': case '3': case '4':
76 case '5': case '6': case '7': case '8': case '9':
77 hadDigit = true;
78 break;
79 case '\0':
80 if (hadDigit) {
81 // pass 2: allocate and initialize vector of values
82 values.resize(++numValues);
83 values.editItemAt(0) = atoi(p = optarg);
84 for (size_t i = 1; i < numValues; ) {
85 if (*p++ == ',') {
86 values.editItemAt(i++) = atoi(p);
87 }
88 }
89 return numValues;
90 }
91 FALLTHROUGH_INTENDED;
92 case ',':
93 if (hadDigit) {
94 hadDigit = false;
95 numValues++;
96 break;
97 }
98 FALLTHROUGH_INTENDED;
99 default:
100 return -1;
101 }
102 }
103 }
104
main(int argc,char * argv[])105 int main(int argc, char* argv[]) {
106 const char* const progname = argv[0];
107 bool profileResample = false;
108 bool profileFilter = false;
109 bool useFloat = false;
110 int channels = 1;
111 int input_freq = 0;
112 int output_freq = 0;
113 AudioResampler::src_quality quality = AudioResampler::DEFAULT_QUALITY;
114 Vector<int> Ovalues;
115 Vector<int> Pvalues;
116
117 int ch;
118 while ((ch = getopt(argc, argv, "pfFvc:q:i:o:O:P:")) != -1) {
119 switch (ch) {
120 case 'p':
121 profileResample = true;
122 break;
123 case 'f':
124 profileFilter = true;
125 break;
126 case 'F':
127 useFloat = true;
128 break;
129 case 'v':
130 gVerbose = true;
131 break;
132 case 'c':
133 channels = atoi(optarg);
134 break;
135 case 'q':
136 if (!strcmp(optarg, "dq"))
137 quality = AudioResampler::DEFAULT_QUALITY;
138 else if (!strcmp(optarg, "lq"))
139 quality = AudioResampler::LOW_QUALITY;
140 else if (!strcmp(optarg, "mq"))
141 quality = AudioResampler::MED_QUALITY;
142 else if (!strcmp(optarg, "hq"))
143 quality = AudioResampler::HIGH_QUALITY;
144 else if (!strcmp(optarg, "vhq"))
145 quality = AudioResampler::VERY_HIGH_QUALITY;
146 else if (!strcmp(optarg, "dlq"))
147 quality = AudioResampler::DYN_LOW_QUALITY;
148 else if (!strcmp(optarg, "dmq"))
149 quality = AudioResampler::DYN_MED_QUALITY;
150 else if (!strcmp(optarg, "dhq"))
151 quality = AudioResampler::DYN_HIGH_QUALITY;
152 else {
153 usage(progname);
154 return -1;
155 }
156 break;
157 case 'i':
158 input_freq = atoi(optarg);
159 break;
160 case 'o':
161 output_freq = atoi(optarg);
162 break;
163 case 'O':
164 if (parseCSV(optarg, Ovalues) < 0) {
165 fprintf(stderr, "incorrect syntax for -O option\n");
166 return -1;
167 }
168 break;
169 case 'P':
170 if (parseCSV(optarg, Pvalues) < 0) {
171 fprintf(stderr, "incorrect syntax for -P option\n");
172 return -1;
173 }
174 break;
175 case '?':
176 default:
177 usage(progname);
178 return -1;
179 }
180 }
181
182 if (channels < 1
183 || channels > (quality < AudioResampler::DYN_LOW_QUALITY ? 2 : 8)) {
184 fprintf(stderr, "invalid number of audio channels %d\n", channels);
185 return -1;
186 }
187 if (useFloat && quality < AudioResampler::DYN_LOW_QUALITY) {
188 fprintf(stderr, "float processing is only possible for dynamic resamplers\n");
189 return -1;
190 }
191
192 argc -= optind;
193 argv += optind;
194
195 const char* file_in = NULL;
196 const char* file_out = NULL;
197 if (argc == 1) {
198 file_out = argv[0];
199 } else if (argc == 2) {
200 file_in = argv[0];
201 file_out = argv[1];
202 } else {
203 usage(progname);
204 return -1;
205 }
206
207 // ----------------------------------------------------------
208
209 size_t input_size;
210 void* input_vaddr;
211 if (argc == 2) {
212 SF_INFO info;
213 info.format = 0;
214 SNDFILE *sf = sf_open(file_in, SFM_READ, &info);
215 if (sf == NULL) {
216 perror(file_in);
217 return EXIT_FAILURE;
218 }
219 input_size = info.frames * info.channels * sizeof(short);
220 input_vaddr = malloc(input_size);
221 (void) sf_readf_short(sf, (short *) input_vaddr, info.frames);
222 sf_close(sf);
223 channels = info.channels;
224 input_freq = info.samplerate;
225 } else {
226 // data for testing is exactly (input sampling rate/1000)/2 seconds
227 // so 44.1khz input is 22.05 seconds
228 double k = 1000; // Hz / s
229 double time = (input_freq / 2) / k;
230 size_t input_frames = size_t(input_freq * time);
231 input_size = channels * sizeof(int16_t) * input_frames;
232 input_vaddr = malloc(input_size);
233 int16_t* in = (int16_t*)input_vaddr;
234 for (size_t i=0 ; i<input_frames ; i++) {
235 double t = double(i) / input_freq;
236 double y = sin(M_PI * k * t * t);
237 int16_t yi = floor(y * 32767.0 + 0.5);
238 for (int j = 0; j < channels; j++) {
239 in[i*channels + j] = yi / (1 + j);
240 }
241 }
242 }
243 size_t input_framesize = channels * sizeof(int16_t);
244 size_t input_frames = input_size / input_framesize;
245
246 // For float processing, convert input int16_t to float array
247 if (useFloat) {
248 void *new_vaddr;
249
250 input_framesize = channels * sizeof(float);
251 input_size = input_frames * input_framesize;
252 new_vaddr = malloc(input_size);
253 memcpy_to_float_from_i16(reinterpret_cast<float*>(new_vaddr),
254 reinterpret_cast<int16_t*>(input_vaddr), input_frames * channels);
255 free(input_vaddr);
256 input_vaddr = new_vaddr;
257 }
258
259 // ----------------------------------------------------------
260
261 class Provider: public AudioBufferProvider {
262 const void* mAddr; // base address
263 const size_t mNumFrames; // total frames
264 const size_t mFrameSize; // size of each frame in bytes
265 size_t mNextFrame; // index of next frame to provide
266 size_t mUnrel; // number of frames not yet released
267 const Vector<int> mPvalues; // number of frames provided per call
268 size_t mNextPidx; // index of next entry in mPvalues to use
269 public:
270 Provider(const void* addr, size_t frames, size_t frameSize, const Vector<int>& Pvalues)
271 : mAddr(addr),
272 mNumFrames(frames),
273 mFrameSize(frameSize),
274 mNextFrame(0), mUnrel(0), mPvalues(Pvalues), mNextPidx(0) {
275 }
276 virtual status_t getNextBuffer(Buffer* buffer) {
277 size_t requestedFrames = buffer->frameCount;
278 if (requestedFrames > mNumFrames - mNextFrame) {
279 buffer->frameCount = mNumFrames - mNextFrame;
280 }
281 if (!mPvalues.isEmpty()) {
282 size_t provided = mPvalues[mNextPidx++];
283 printf("mPvalue[%zu]=%zu not %zu\n", mNextPidx-1, provided, buffer->frameCount);
284 if (provided < buffer->frameCount) {
285 buffer->frameCount = provided;
286 }
287 if (mNextPidx >= mPvalues.size()) {
288 mNextPidx = 0;
289 }
290 }
291 if (gVerbose) {
292 printf("getNextBuffer() requested %zu frames out of %zu frames available,"
293 " and returned %zu frames\n",
294 requestedFrames, (size_t) (mNumFrames - mNextFrame), buffer->frameCount);
295 }
296 mUnrel = buffer->frameCount;
297 if (buffer->frameCount > 0) {
298 buffer->raw = (char *)mAddr + mFrameSize * mNextFrame;
299 return NO_ERROR;
300 } else {
301 buffer->raw = NULL;
302 return NOT_ENOUGH_DATA;
303 }
304 }
305 virtual void releaseBuffer(Buffer* buffer) {
306 if (buffer->frameCount > mUnrel) {
307 fprintf(stderr, "ERROR releaseBuffer() released %zu frames but only %zu available "
308 "to release\n", buffer->frameCount, mUnrel);
309 mNextFrame += mUnrel;
310 mUnrel = 0;
311 } else {
312 if (gVerbose) {
313 printf("releaseBuffer() released %zu frames out of %zu frames available "
314 "to release\n", buffer->frameCount, mUnrel);
315 }
316 mNextFrame += buffer->frameCount;
317 mUnrel -= buffer->frameCount;
318 }
319 buffer->frameCount = 0;
320 buffer->raw = NULL;
321 }
322 void reset() {
323 mNextFrame = 0;
324 }
325 } provider(input_vaddr, input_frames, input_framesize, Pvalues);
326
327 if (gVerbose) {
328 printf("%zu input frames\n", input_frames);
329 }
330
331 audio_format_t format = useFloat ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
332 int output_channels = channels > 2 ? channels : 2; // output is at least stereo samples
333 size_t output_framesize = output_channels * (useFloat ? sizeof(float) : sizeof(int32_t));
334 size_t output_frames = ((int64_t) input_frames * output_freq) / input_freq;
335 size_t output_size = output_frames * output_framesize;
336
337 if (profileFilter) {
338 // Check how fast sample rate changes are that require filter changes.
339 // The delta sample rate changes must indicate a downsampling ratio,
340 // and must be larger than 10% changes.
341 //
342 // On fast devices, filters should be generated between 0.1ms - 1ms.
343 // (single threaded).
344 AudioResampler* resampler = AudioResampler::create(format, channels,
345 8000, quality);
346 int looplimit = 100;
347 timespec start, end;
348 clock_gettime(CLOCK_MONOTONIC, &start);
349 for (int i = 0; i < looplimit; ++i) {
350 resampler->setSampleRate(9000);
351 resampler->setSampleRate(12000);
352 resampler->setSampleRate(20000);
353 resampler->setSampleRate(30000);
354 }
355 clock_gettime(CLOCK_MONOTONIC, &end);
356 int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
357 int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
358 int64_t time = end_ns - start_ns;
359 printf("%.2f sample rate changes with filter calculation/sec\n",
360 looplimit * 4 / (time / 1e9));
361
362 // Check how fast sample rate changes are without filter changes.
363 // This should be very fast, probably 0.1us - 1us per sample rate
364 // change.
365 resampler->setSampleRate(1000);
366 looplimit = 1000;
367 clock_gettime(CLOCK_MONOTONIC, &start);
368 for (int i = 0; i < looplimit; ++i) {
369 resampler->setSampleRate(1000+i);
370 }
371 clock_gettime(CLOCK_MONOTONIC, &end);
372 start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
373 end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
374 time = end_ns - start_ns;
375 printf("%.2f sample rate changes without filter calculation/sec\n",
376 looplimit / (time / 1e9));
377 resampler->reset();
378 delete resampler;
379 }
380
381 void* output_vaddr = malloc(output_size);
382 AudioResampler* resampler = AudioResampler::create(format, channels,
383 output_freq, quality);
384
385 resampler->setSampleRate(input_freq);
386 resampler->setVolume(AudioResampler::UNITY_GAIN_FLOAT, AudioResampler::UNITY_GAIN_FLOAT);
387
388 if (profileResample) {
389 /*
390 * For profiling on mobile devices, upon experimentation
391 * it is better to run a few trials with a shorter loop limit,
392 * and take the minimum time.
393 *
394 * Long tests can cause CPU temperature to build up and thermal throttling
395 * to reduce CPU frequency.
396 *
397 * For frequency checks (index=0, or 1, etc.):
398 * "cat /sys/devices/system/cpu/cpu${index}/cpufreq/scaling_*_freq"
399 *
400 * For temperature checks (index=0, or 1, etc.):
401 * "cat /sys/class/thermal/thermal_zone${index}/temp"
402 *
403 * Another way to avoid thermal throttling is to fix the CPU frequency
404 * at a lower level which prevents excessive temperatures.
405 */
406 const int trials = 4;
407 const int looplimit = 4;
408 timespec start, end;
409 int64_t time = 0;
410
411 for (int n = 0; n < trials; ++n) {
412 clock_gettime(CLOCK_MONOTONIC, &start);
413 for (int i = 0; i < looplimit; ++i) {
414 resampler->resample((int*) output_vaddr, output_frames, &provider);
415 provider.reset(); // during benchmarking reset only the provider
416 }
417 clock_gettime(CLOCK_MONOTONIC, &end);
418 int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
419 int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
420 int64_t diff_ns = end_ns - start_ns;
421 if (n == 0 || diff_ns < time) {
422 time = diff_ns; // save the best out of our trials.
423 }
424 }
425 // Mfrms/s is "Millions of output frames per second".
426 printf("quality: %d channels: %d msec: %" PRId64 " Mfrms/s: %.2lf\n",
427 quality, channels, time/1000000, output_frames * looplimit / (time / 1e9) / 1e6);
428 resampler->reset();
429
430 // TODO fix legacy bug: reset does not clear buffers.
431 // delete and recreate resampler here.
432 delete resampler;
433 resampler = AudioResampler::create(format, channels,
434 output_freq, quality);
435 resampler->setSampleRate(input_freq);
436 resampler->setVolume(AudioResampler::UNITY_GAIN_FLOAT, AudioResampler::UNITY_GAIN_FLOAT);
437 }
438
439 memset(output_vaddr, 0, output_size);
440 if (gVerbose) {
441 printf("resample() %zu output frames\n", output_frames);
442 }
443 if (Ovalues.isEmpty()) {
444 Ovalues.push(output_frames);
445 }
446 for (size_t i = 0, j = 0; i < output_frames; ) {
447 size_t thisFrames = Ovalues[j++];
448 if (j >= Ovalues.size()) {
449 j = 0;
450 }
451 if (thisFrames == 0 || thisFrames > output_frames - i) {
452 thisFrames = output_frames - i;
453 }
454 resampler->resample((int*) output_vaddr + output_channels*i, thisFrames, &provider);
455 i += thisFrames;
456 }
457 if (gVerbose) {
458 printf("resample() complete\n");
459 }
460 resampler->reset();
461 if (gVerbose) {
462 printf("reset() complete\n");
463 }
464 delete resampler;
465 resampler = NULL;
466
467 // For float processing, convert output format from float to Q4.27,
468 // which is then converted to int16_t for final storage.
469 if (useFloat) {
470 memcpy_to_q4_27_from_float(reinterpret_cast<int32_t*>(output_vaddr),
471 reinterpret_cast<float*>(output_vaddr), output_frames * output_channels);
472 }
473
474 // mono takes left channel only (out of stereo output pair)
475 // stereo and multichannel preserve all channels.
476 int32_t* out = (int32_t*) output_vaddr;
477 int16_t* convert = (int16_t*) malloc(output_frames * channels * sizeof(int16_t));
478
479 const int volumeShift = 12; // shift requirement for Q4.27 to Q.15
480 // round to half towards zero and saturate at int16 (non-dithered)
481 const int roundVal = (1<<(volumeShift-1)) - 1; // volumePrecision > 0
482
483 for (size_t i = 0; i < output_frames; i++) {
484 for (int j = 0; j < channels; j++) {
485 int32_t s = out[i * output_channels + j] + roundVal; // add offset here
486 if (s < 0) {
487 s = (s + 1) >> volumeShift; // round to 0
488 if (s < -32768) {
489 s = -32768;
490 }
491 } else {
492 s = s >> volumeShift;
493 if (s > 32767) {
494 s = 32767;
495 }
496 }
497 convert[i * channels + j] = int16_t(s);
498 }
499 }
500
501 // write output to disk
502 SF_INFO info;
503 info.frames = 0;
504 info.samplerate = output_freq;
505 info.channels = channels;
506 info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
507 SNDFILE *sf = sf_open(file_out, SFM_WRITE, &info);
508 if (sf == NULL) {
509 perror(file_out);
510 return EXIT_FAILURE;
511 }
512 (void) sf_writef_short(sf, convert, output_frames);
513 sf_close(sf);
514
515 return EXIT_SUCCESS;
516 }
517