src/platform/ffmpeg/ffmpeg-resample.c (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "ffmpeg-resample.h"
7
8#include "gba-audio.h"
9
10#include <libavresample/avresample.h>
11#include <libavutil/opt.h>
12
13struct AVAudioResampleContext* GBAAudioOpenLAVR(struct GBAAudio* audio, unsigned outputRate) {
14 AVAudioResampleContext *avr = avresample_alloc_context();
15 av_opt_set_int(avr, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0);
16 av_opt_set_int(avr, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
17 av_opt_set_int(avr, "in_sample_rate", audio->sampleRate, 0);
18 av_opt_set_int(avr, "out_sample_rate", outputRate, 0);
19 av_opt_set_int(avr, "in_sample_fmt", AV_SAMPLE_FMT_S16P, 0);
20 av_opt_set_int(avr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
21 if (avresample_open(avr)) {
22 avresample_free(&avr);
23 return 0;
24 }
25 return avr;
26}
27
28unsigned GBAAudioResampleLAVR(struct GBAAudio* audio, struct AVAudioResampleContext* avr, struct GBAStereoSample* output, unsigned nSamples) {
29 int16_t left[GBA_AUDIO_SAMPLES];
30 int16_t right[GBA_AUDIO_SAMPLES];
31 int16_t* samples[2] = { left, right };
32
33 size_t totalRead = 0;
34 size_t available = avresample_available(avr);
35 if (available) {
36 totalRead = avresample_read(avr, (uint8_t**) &output, nSamples);
37 nSamples -= totalRead;
38 output += totalRead;
39 }
40 while (nSamples) {
41 unsigned read = GBAAudioCopy(audio, left, right, GBA_AUDIO_SAMPLES);
42
43 size_t currentRead = avresample_convert(avr, (uint8_t**) &output, nSamples * sizeof(struct GBAStereoSample), nSamples, (uint8_t**) samples, sizeof(left), read);
44 nSamples -= currentRead;
45 output += currentRead;
46 totalRead += currentRead;
47 if (read < GBA_AUDIO_SAMPLES && nSamples) {
48 memset(output, 0, nSamples * sizeof(struct GBAStereoSample));
49 break;
50 }
51 }
52 return totalRead;
53}