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 <utils/Vector.h>
31 #include <media/AudioBufferProvider.h>
32 #include "AudioResampler.h"
33 
34 using namespace android;
35 
36 static bool gVerbose = false;
37 
usage(const char * name)38 static int usage(const char* name) {
39     fprintf(stderr,"Usage: %s [-p] [-f] [-F] [-v] [-c channels]"
40                    " [-q {dq|lq|mq|hq|vhq|dlq|dmq|dhq}]"
41                    " [-i input-sample-rate] [-o output-sample-rate]"
42                    " [-O csv] [-P csv] [<input-file>]"
43                    " <output-file>\n", name);
44     fprintf(stderr,"    -p    enable profiling\n");
45     fprintf(stderr,"    -f    enable filter profiling\n");
46     fprintf(stderr,"    -F    enable floating point -q {dlq|dmq|dhq} only");
47     fprintf(stderr,"    -v    verbose : log buffer provider calls\n");
48     fprintf(stderr,"    -c    # channels (1-2 for lq|mq|hq; 1-8 for dlq|dmq|dhq)\n");
49     fprintf(stderr,"    -q    resampler quality\n");
50     fprintf(stderr,"              dq  : default quality\n");
51     fprintf(stderr,"              lq  : low quality\n");
52     fprintf(stderr,"              mq  : medium quality\n");
53     fprintf(stderr,"              hq  : high quality\n");
54     fprintf(stderr,"              vhq : very high quality\n");
55     fprintf(stderr,"              dlq : dynamic low quality\n");
56     fprintf(stderr,"              dmq : dynamic medium quality\n");
57     fprintf(stderr,"              dhq : dynamic high quality\n");
58     fprintf(stderr,"    -i    input file sample rate (ignored if input file is specified)\n");
59     fprintf(stderr,"    -o    output file sample rate\n");
60     fprintf(stderr,"    -O    # frames output per call to resample() in CSV format\n");
61     fprintf(stderr,"    -P    # frames provided per call to resample() in CSV format\n");
62     return -1;
63 }
64 
65 // Convert a list of integers in CSV format to a Vector of those values.
66 // Returns the number of elements in the list, or -1 on error.
parseCSV(const char * string,Vector<int> & values)67 int parseCSV(const char *string, Vector<int>& values)
68 {
69     // pass 1: count the number of values and do syntax check
70     size_t numValues = 0;
71     bool hadDigit = false;
72     for (const char *p = string; ; ) {
73         switch (*p++) {
74         case '0': case '1': case '2': case '3': case '4':
75         case '5': case '6': case '7': case '8': case '9':
76             hadDigit = true;
77             break;
78         case '\0':
79             if (hadDigit) {
80                 // pass 2: allocate and initialize vector of values
81                 values.resize(++numValues);
82                 values.editItemAt(0) = atoi(p = optarg);
83                 for (size_t i = 1; i < numValues; ) {
84                     if (*p++ == ',') {
85                         values.editItemAt(i++) = atoi(p);
86                     }
87                 }
88                 return numValues;
89             }
90             // fall through
91         case ',':
92             if (hadDigit) {
93                 hadDigit = false;
94                 numValues++;
95                 break;
96             }
97             // fall through
98         default:
99             return -1;
100         }
101     }
102 }
103 
main(int argc,char * argv[])104 int main(int argc, char* argv[]) {
105     const char* const progname = argv[0];
106     bool profileResample = false;
107     bool profileFilter = false;
108     bool useFloat = false;
109     int channels = 1;
110     int input_freq = 0;
111     int output_freq = 0;
112     AudioResampler::src_quality quality = AudioResampler::DEFAULT_QUALITY;
113     Vector<int> Ovalues;
114     Vector<int> Pvalues;
115 
116     int ch;
117     while ((ch = getopt(argc, argv, "pfFvc:q:i:o:O:P:")) != -1) {
118         switch (ch) {
119         case 'p':
120             profileResample = true;
121             break;
122         case 'f':
123             profileFilter = true;
124             break;
125         case 'F':
126             useFloat = true;
127             break;
128         case 'v':
129             gVerbose = true;
130             break;
131         case 'c':
132             channels = atoi(optarg);
133             break;
134         case 'q':
135             if (!strcmp(optarg, "dq"))
136                 quality = AudioResampler::DEFAULT_QUALITY;
137             else if (!strcmp(optarg, "lq"))
138                 quality = AudioResampler::LOW_QUALITY;
139             else if (!strcmp(optarg, "mq"))
140                 quality = AudioResampler::MED_QUALITY;
141             else if (!strcmp(optarg, "hq"))
142                 quality = AudioResampler::HIGH_QUALITY;
143             else if (!strcmp(optarg, "vhq"))
144                 quality = AudioResampler::VERY_HIGH_QUALITY;
145             else if (!strcmp(optarg, "dlq"))
146                 quality = AudioResampler::DYN_LOW_QUALITY;
147             else if (!strcmp(optarg, "dmq"))
148                 quality = AudioResampler::DYN_MED_QUALITY;
149             else if (!strcmp(optarg, "dhq"))
150                 quality = AudioResampler::DYN_HIGH_QUALITY;
151             else {
152                 usage(progname);
153                 return -1;
154             }
155             break;
156         case 'i':
157             input_freq = atoi(optarg);
158             break;
159         case 'o':
160             output_freq = atoi(optarg);
161             break;
162         case 'O':
163             if (parseCSV(optarg, Ovalues) < 0) {
164                 fprintf(stderr, "incorrect syntax for -O option\n");
165                 return -1;
166             }
167             break;
168         case 'P':
169             if (parseCSV(optarg, Pvalues) < 0) {
170                 fprintf(stderr, "incorrect syntax for -P option\n");
171                 return -1;
172             }
173             break;
174         case '?':
175         default:
176             usage(progname);
177             return -1;
178         }
179     }
180 
181     if (channels < 1
182             || channels > (quality < AudioResampler::DYN_LOW_QUALITY ? 2 : 8)) {
183         fprintf(stderr, "invalid number of audio channels %d\n", channels);
184         return -1;
185     }
186     if (useFloat && quality < AudioResampler::DYN_LOW_QUALITY) {
187         fprintf(stderr, "float processing is only possible for dynamic resamplers\n");
188         return -1;
189     }
190 
191     argc -= optind;
192     argv += optind;
193 
194     const char* file_in = NULL;
195     const char* file_out = NULL;
196     if (argc == 1) {
197         file_out = argv[0];
198     } else if (argc == 2) {
199         file_in = argv[0];
200         file_out = argv[1];
201     } else {
202         usage(progname);
203         return -1;
204     }
205 
206     // ----------------------------------------------------------
207 
208     size_t input_size;
209     void* input_vaddr;
210     if (argc == 2) {
211         SF_INFO info;
212         info.format = 0;
213         SNDFILE *sf = sf_open(file_in, SFM_READ, &info);
214         if (sf == NULL) {
215             perror(file_in);
216             return EXIT_FAILURE;
217         }
218         input_size = info.frames * info.channels * sizeof(short);
219         input_vaddr = malloc(input_size);
220         (void) sf_readf_short(sf, (short *) input_vaddr, info.frames);
221         sf_close(sf);
222         channels = info.channels;
223         input_freq = info.samplerate;
224     } else {
225         // data for testing is exactly (input sampling rate/1000)/2 seconds
226         // so 44.1khz input is 22.05 seconds
227         double k = 1000; // Hz / s
228         double time = (input_freq / 2) / k;
229         size_t input_frames = size_t(input_freq * time);
230         input_size = channels * sizeof(int16_t) * input_frames;
231         input_vaddr = malloc(input_size);
232         int16_t* in = (int16_t*)input_vaddr;
233         for (size_t i=0 ; i<input_frames ; i++) {
234             double t = double(i) / input_freq;
235             double y = sin(M_PI * k * t * t);
236             int16_t yi = floor(y * 32767.0 + 0.5);
237             for (int j = 0; j < channels; j++) {
238                 in[i*channels + j] = yi / (1 + j);
239             }
240         }
241     }
242     size_t input_framesize = channels * sizeof(int16_t);
243     size_t input_frames = input_size / input_framesize;
244 
245     // For float processing, convert input int16_t to float array
246     if (useFloat) {
247         void *new_vaddr;
248 
249         input_framesize = channels * sizeof(float);
250         input_size = input_frames * input_framesize;
251         new_vaddr = malloc(input_size);
252         memcpy_to_float_from_i16(reinterpret_cast<float*>(new_vaddr),
253                 reinterpret_cast<int16_t*>(input_vaddr), input_frames * channels);
254         free(input_vaddr);
255         input_vaddr = new_vaddr;
256     }
257 
258     // ----------------------------------------------------------
259 
260     class Provider: public AudioBufferProvider {
261         const void*     mAddr;      // base address
262         const size_t    mNumFrames; // total frames
263         const size_t    mFrameSize; // size of each frame in bytes
264         size_t          mNextFrame; // index of next frame to provide
265         size_t          mUnrel;     // number of frames not yet released
266         const Vector<int> mPvalues; // number of frames provided per call
267         size_t          mNextPidx;  // index of next entry in mPvalues to use
268     public:
269         Provider(const void* addr, size_t frames, size_t frameSize, const Vector<int>& Pvalues)
270           : mAddr(addr),
271             mNumFrames(frames),
272             mFrameSize(frameSize),
273             mNextFrame(0), mUnrel(0), mPvalues(Pvalues), mNextPidx(0) {
274         }
275         virtual status_t getNextBuffer(Buffer* buffer,
276                 int64_t pts = kInvalidPTS) {
277             (void)pts; // suppress warning
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     int16_t* convert = (int16_t*) malloc(output_frames * channels * sizeof(int16_t));
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, output_frames);
514     sf_close(sf);
515 
516     return EXIT_SUCCESS;
517 }
518