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