test-resample.cpp revision df383a5a54582811e5e038efc557172b8ec69dd1
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 "AudioResampler.h"
18#include <media/AudioBufferProvider.h>
19#include <unistd.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <fcntl.h>
23#include <string.h>
24#include <sys/mman.h>
25#include <sys/stat.h>
26#include <errno.h>
27#include <inttypes.h>
28#include <time.h>
29#include <math.h>
30#include <audio_utils/primitives.h>
31#include <audio_utils/sndfile.h>
32#include <utils/Vector.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                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    int bit_depth = useFloat ? 32 : 16;
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(bit_depth, 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(bit_depth, channels,
384            output_freq, quality);
385
386
387    /* set volume precision to 12 bits, so the volume scale is 1<<12.
388     * The output int32_t is represented as Q4.27, with 4 bits of guard
389     * followed by the int16_t Q.15 portion, and then 12 trailing bits of
390     * additional precision.
391     *
392     * Generally 0 < volumePrecision <= 14 (due to the limits of
393     * int16_t values for Volume). volumePrecision cannot be 0 due
394     * to rounding and shifts.
395     */
396    const int volumePrecision = 12; // in bits
397
398    resampler->setSampleRate(input_freq);
399    resampler->setVolume(1 << volumePrecision, 1 << volumePrecision);
400
401    if (profileResample) {
402        /*
403         * For profiling on mobile devices, upon experimentation
404         * it is better to run a few trials with a shorter loop limit,
405         * and take the minimum time.
406         *
407         * Long tests can cause CPU temperature to build up and thermal throttling
408         * to reduce CPU frequency.
409         *
410         * For frequency checks (index=0, or 1, etc.):
411         * "cat /sys/devices/system/cpu/cpu${index}/cpufreq/scaling_*_freq"
412         *
413         * For temperature checks (index=0, or 1, etc.):
414         * "cat /sys/class/thermal/thermal_zone${index}/temp"
415         *
416         * Another way to avoid thermal throttling is to fix the CPU frequency
417         * at a lower level which prevents excessive temperatures.
418         */
419        const int trials = 4;
420        const int looplimit = 4;
421        timespec start, end;
422        int64_t time = 0;
423
424        for (int n = 0; n < trials; ++n) {
425            clock_gettime(CLOCK_MONOTONIC, &start);
426            for (int i = 0; i < looplimit; ++i) {
427                resampler->resample((int*) output_vaddr, output_frames, &provider);
428                provider.reset(); //  during benchmarking reset only the provider
429            }
430            clock_gettime(CLOCK_MONOTONIC, &end);
431            int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
432            int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
433            int64_t diff_ns = end_ns - start_ns;
434            if (n == 0 || diff_ns < time) {
435                time = diff_ns;   // save the best out of our trials.
436            }
437        }
438        // Mfrms/s is "Millions of output frames per second".
439        printf("quality: %d  channels: %d  msec: %" PRId64 "  Mfrms/s: %.2lf\n",
440                quality, channels, time/1000000, output_frames * looplimit / (time / 1e9) / 1e6);
441        resampler->reset();
442    }
443
444    memset(output_vaddr, 0, output_size);
445    if (gVerbose) {
446        printf("resample() %zu output frames\n", output_frames);
447    }
448    if (Ovalues.isEmpty()) {
449        Ovalues.push(output_frames);
450    }
451    for (size_t i = 0, j = 0; i < output_frames; ) {
452        size_t thisFrames = Ovalues[j++];
453        if (j >= Ovalues.size()) {
454            j = 0;
455        }
456        if (thisFrames == 0 || thisFrames > output_frames - i) {
457            thisFrames = output_frames - i;
458        }
459        resampler->resample((int*) output_vaddr + output_channels*i, thisFrames, &provider);
460        i += thisFrames;
461    }
462    if (gVerbose) {
463        printf("resample() complete\n");
464    }
465    resampler->reset();
466    if (gVerbose) {
467        printf("reset() complete\n");
468    }
469    delete resampler;
470    resampler = NULL;
471
472    // For float processing, convert output format from float to Q4.27,
473    // which is then converted to int16_t for final storage.
474    if (useFloat) {
475        memcpy_to_q4_27_from_float(reinterpret_cast<int32_t*>(output_vaddr),
476                reinterpret_cast<float*>(output_vaddr), output_frames * output_channels);
477    }
478
479    // mono takes left channel only (out of stereo output pair)
480    // stereo and multichannel preserve all channels.
481    int32_t* out = (int32_t*) output_vaddr;
482    int16_t* convert = (int16_t*) malloc(output_frames * channels * sizeof(int16_t));
483
484    // round to half towards zero and saturate at int16 (non-dithered)
485    const int roundVal = (1<<(volumePrecision-1)) - 1; // volumePrecision > 0
486
487    for (size_t i = 0; i < output_frames; i++) {
488        for (int j = 0; j < channels; j++) {
489            int32_t s = out[i * output_channels + j] + roundVal; // add offset here
490            if (s < 0) {
491                s = (s + 1) >> volumePrecision; // round to 0
492                if (s < -32768) {
493                    s = -32768;
494                }
495            } else {
496                s = s >> volumePrecision;
497                if (s > 32767) {
498                    s = 32767;
499                }
500            }
501            convert[i * channels + j] = int16_t(s);
502        }
503    }
504
505    // write output to disk
506    SF_INFO info;
507    info.frames = 0;
508    info.samplerate = output_freq;
509    info.channels = channels;
510    info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
511    SNDFILE *sf = sf_open(file_out, SFM_WRITE, &info);
512    if (sf == NULL) {
513        perror(file_out);
514        return EXIT_FAILURE;
515    }
516    (void) sf_writef_short(sf, convert, output_frames);
517    sf_close(sf);
518
519    return EXIT_SUCCESS;
520}
521