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 <media/AudioResampler.h>
33
34using namespace android;
35
36static bool gVerbose = false;
37
38static 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.
67int 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
104int 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            size_t requestedFrames = buffer->frameCount;
277            if (requestedFrames > mNumFrames - mNextFrame) {
278                buffer->frameCount = mNumFrames - mNextFrame;
279            }
280            if (!mPvalues.isEmpty()) {
281                size_t provided = mPvalues[mNextPidx++];
282                printf("mPvalue[%zu]=%zu not %zu\n", mNextPidx-1, provided, buffer->frameCount);
283                if (provided < buffer->frameCount) {
284                    buffer->frameCount = provided;
285                }
286                if (mNextPidx >= mPvalues.size()) {
287                    mNextPidx = 0;
288                }
289            }
290            if (gVerbose) {
291                printf("getNextBuffer() requested %zu frames out of %zu frames available,"
292                        " and returned %zu frames\n",
293                        requestedFrames, (size_t) (mNumFrames - mNextFrame), buffer->frameCount);
294            }
295            mUnrel = buffer->frameCount;
296            if (buffer->frameCount > 0) {
297                buffer->raw = (char *)mAddr + mFrameSize * mNextFrame;
298                return NO_ERROR;
299            } else {
300                buffer->raw = NULL;
301                return NOT_ENOUGH_DATA;
302            }
303        }
304        virtual void releaseBuffer(Buffer* buffer) {
305            if (buffer->frameCount > mUnrel) {
306                fprintf(stderr, "ERROR releaseBuffer() released %zu frames but only %zu available "
307                        "to release\n", buffer->frameCount, mUnrel);
308                mNextFrame += mUnrel;
309                mUnrel = 0;
310            } else {
311                if (gVerbose) {
312                    printf("releaseBuffer() released %zu frames out of %zu frames available "
313                            "to release\n", buffer->frameCount, mUnrel);
314                }
315                mNextFrame += buffer->frameCount;
316                mUnrel -= buffer->frameCount;
317            }
318            buffer->frameCount = 0;
319            buffer->raw = NULL;
320        }
321        void reset() {
322            mNextFrame = 0;
323        }
324    } provider(input_vaddr, input_frames, input_framesize, Pvalues);
325
326    if (gVerbose) {
327        printf("%zu input frames\n", input_frames);
328    }
329
330    audio_format_t format = useFloat ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
331    int output_channels = channels > 2 ? channels : 2; // output is at least stereo samples
332    size_t output_framesize = output_channels * (useFloat ? sizeof(float) : sizeof(int32_t));
333    size_t output_frames = ((int64_t) input_frames * output_freq) / input_freq;
334    size_t output_size = output_frames * output_framesize;
335
336    if (profileFilter) {
337        // Check how fast sample rate changes are that require filter changes.
338        // The delta sample rate changes must indicate a downsampling ratio,
339        // and must be larger than 10% changes.
340        //
341        // On fast devices, filters should be generated between 0.1ms - 1ms.
342        // (single threaded).
343        AudioResampler* resampler = AudioResampler::create(format, channels,
344                8000, quality);
345        int looplimit = 100;
346        timespec start, end;
347        clock_gettime(CLOCK_MONOTONIC, &start);
348        for (int i = 0; i < looplimit; ++i) {
349            resampler->setSampleRate(9000);
350            resampler->setSampleRate(12000);
351            resampler->setSampleRate(20000);
352            resampler->setSampleRate(30000);
353        }
354        clock_gettime(CLOCK_MONOTONIC, &end);
355        int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
356        int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
357        int64_t time = end_ns - start_ns;
358        printf("%.2f sample rate changes with filter calculation/sec\n",
359                looplimit * 4 / (time / 1e9));
360
361        // Check how fast sample rate changes are without filter changes.
362        // This should be very fast, probably 0.1us - 1us per sample rate
363        // change.
364        resampler->setSampleRate(1000);
365        looplimit = 1000;
366        clock_gettime(CLOCK_MONOTONIC, &start);
367        for (int i = 0; i < looplimit; ++i) {
368            resampler->setSampleRate(1000+i);
369        }
370        clock_gettime(CLOCK_MONOTONIC, &end);
371        start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
372        end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
373        time = end_ns - start_ns;
374        printf("%.2f sample rate changes without filter calculation/sec\n",
375                looplimit / (time / 1e9));
376        resampler->reset();
377        delete resampler;
378    }
379
380    void* output_vaddr = malloc(output_size);
381    AudioResampler* resampler = AudioResampler::create(format, channels,
382            output_freq, quality);
383
384    resampler->setSampleRate(input_freq);
385    resampler->setVolume(AudioResampler::UNITY_GAIN_FLOAT, AudioResampler::UNITY_GAIN_FLOAT);
386
387    if (profileResample) {
388        /*
389         * For profiling on mobile devices, upon experimentation
390         * it is better to run a few trials with a shorter loop limit,
391         * and take the minimum time.
392         *
393         * Long tests can cause CPU temperature to build up and thermal throttling
394         * to reduce CPU frequency.
395         *
396         * For frequency checks (index=0, or 1, etc.):
397         * "cat /sys/devices/system/cpu/cpu${index}/cpufreq/scaling_*_freq"
398         *
399         * For temperature checks (index=0, or 1, etc.):
400         * "cat /sys/class/thermal/thermal_zone${index}/temp"
401         *
402         * Another way to avoid thermal throttling is to fix the CPU frequency
403         * at a lower level which prevents excessive temperatures.
404         */
405        const int trials = 4;
406        const int looplimit = 4;
407        timespec start, end;
408        int64_t time = 0;
409
410        for (int n = 0; n < trials; ++n) {
411            clock_gettime(CLOCK_MONOTONIC, &start);
412            for (int i = 0; i < looplimit; ++i) {
413                resampler->resample((int*) output_vaddr, output_frames, &provider);
414                provider.reset(); //  during benchmarking reset only the provider
415            }
416            clock_gettime(CLOCK_MONOTONIC, &end);
417            int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
418            int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
419            int64_t diff_ns = end_ns - start_ns;
420            if (n == 0 || diff_ns < time) {
421                time = diff_ns;   // save the best out of our trials.
422            }
423        }
424        // Mfrms/s is "Millions of output frames per second".
425        printf("quality: %d  channels: %d  msec: %" PRId64 "  Mfrms/s: %.2lf\n",
426                quality, channels, time/1000000, output_frames * looplimit / (time / 1e9) / 1e6);
427        resampler->reset();
428
429        // TODO fix legacy bug: reset does not clear buffers.
430        // delete and recreate resampler here.
431        delete resampler;
432        resampler = AudioResampler::create(format, channels,
433                    output_freq, quality);
434        resampler->setSampleRate(input_freq);
435        resampler->setVolume(AudioResampler::UNITY_GAIN_FLOAT, AudioResampler::UNITY_GAIN_FLOAT);
436    }
437
438    memset(output_vaddr, 0, output_size);
439    if (gVerbose) {
440        printf("resample() %zu output frames\n", output_frames);
441    }
442    if (Ovalues.isEmpty()) {
443        Ovalues.push(output_frames);
444    }
445    for (size_t i = 0, j = 0; i < output_frames; ) {
446        size_t thisFrames = Ovalues[j++];
447        if (j >= Ovalues.size()) {
448            j = 0;
449        }
450        if (thisFrames == 0 || thisFrames > output_frames - i) {
451            thisFrames = output_frames - i;
452        }
453        resampler->resample((int*) output_vaddr + output_channels*i, thisFrames, &provider);
454        i += thisFrames;
455    }
456    if (gVerbose) {
457        printf("resample() complete\n");
458    }
459    resampler->reset();
460    if (gVerbose) {
461        printf("reset() complete\n");
462    }
463    delete resampler;
464    resampler = NULL;
465
466    // For float processing, convert output format from float to Q4.27,
467    // which is then converted to int16_t for final storage.
468    if (useFloat) {
469        memcpy_to_q4_27_from_float(reinterpret_cast<int32_t*>(output_vaddr),
470                reinterpret_cast<float*>(output_vaddr), output_frames * output_channels);
471    }
472
473    // mono takes left channel only (out of stereo output pair)
474    // stereo and multichannel preserve all channels.
475    int32_t* out = (int32_t*) output_vaddr;
476    int16_t* convert = (int16_t*) malloc(output_frames * channels * sizeof(int16_t));
477
478    const int volumeShift = 12; // shift requirement for Q4.27 to Q.15
479    // round to half towards zero and saturate at int16 (non-dithered)
480    const int roundVal = (1<<(volumeShift-1)) - 1; // volumePrecision > 0
481
482    for (size_t i = 0; i < output_frames; i++) {
483        for (int j = 0; j < channels; j++) {
484            int32_t s = out[i * output_channels + j] + roundVal; // add offset here
485            if (s < 0) {
486                s = (s + 1) >> volumeShift; // round to 0
487                if (s < -32768) {
488                    s = -32768;
489                }
490            } else {
491                s = s >> volumeShift;
492                if (s > 32767) {
493                    s = 32767;
494                }
495            }
496            convert[i * channels + j] = int16_t(s);
497        }
498    }
499
500    // write output to disk
501    SF_INFO info;
502    info.frames = 0;
503    info.samplerate = output_freq;
504    info.channels = channels;
505    info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
506    SNDFILE *sf = sf_open(file_out, SFM_WRITE, &info);
507    if (sf == NULL) {
508        perror(file_out);
509        return EXIT_FAILURE;
510    }
511    (void) sf_writef_short(sf, convert, output_frames);
512    sf_close(sf);
513
514    return EXIT_SUCCESS;
515}
516