2
This file is part of PulseAudio.
4
Copyright 2004-2006 Lennart Poettering
6
PulseAudio is free software; you can redistribute it and/or modify
7
it under the terms of the GNU Lesser General Public License as published
8
by the Free Software Foundation; either version 2.1 of the License,
9
or (at your option) any later version.
11
PulseAudio is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public License
17
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
24
#include <pulse/xmalloc.h>
25
#include "pulsecore/ffmpeg/avcodec.h"
27
#include <pulsecore/resampler.h>
29
struct ffmpeg_data { /* data specific to ffmpeg */
30
struct AVResampleContext *state;
33
static unsigned ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
34
unsigned used_frames = 0, c;
35
int previous_consumed_frames = -1;
36
struct ffmpeg_data *ffmpeg_data;
41
pa_assert(out_n_frames);
43
ffmpeg_data = r->impl.data;
45
for (c = 0; c < r->work_channels; c++) {
48
int16_t *p, *t, *k, *q, *s;
51
/* Allocate a new block */
52
b = pa_memblock_new(r->mempool, in_n_frames * sizeof(int16_t));
53
p = pa_memblock_acquire(b);
55
/* Now copy the input data, splitting up channels */
56
t = (int16_t*) pa_memblock_acquire_chunk(input) + c;
58
for (u = 0; u < in_n_frames; u++) {
60
t += r->work_channels;
63
pa_memblock_release(input->memblock);
65
/* Allocate buffer for the result */
66
w = pa_memblock_new(r->mempool, *out_n_frames * sizeof(int16_t));
67
q = pa_memblock_acquire(w);
70
used_frames = (unsigned) av_resample(ffmpeg_data->state,
73
(int) in_n_frames, (int) *out_n_frames,
74
c >= (unsigned) (r->work_channels-1));
76
pa_memblock_release(b);
79
pa_assert(consumed_frames <= (int) in_n_frames);
80
pa_assert(previous_consumed_frames == -1 || consumed_frames == previous_consumed_frames);
81
previous_consumed_frames = consumed_frames;
83
/* And place the results in the output buffer */
84
s = (int16_t *) pa_memblock_acquire_chunk(output) + c;
85
for (u = 0; u < used_frames; u++) {
88
s += r->work_channels;
90
pa_memblock_release(output->memblock);
91
pa_memblock_release(w);
95
*out_n_frames = used_frames;
97
return in_n_frames - previous_consumed_frames;
100
static void ffmpeg_free(pa_resampler *r) {
101
struct ffmpeg_data *ffmpeg_data;
105
ffmpeg_data = r->impl.data;
106
if (ffmpeg_data->state)
107
av_resample_close(ffmpeg_data->state);
110
int pa_resampler_ffmpeg_init(pa_resampler *r) {
111
struct ffmpeg_data *ffmpeg_data;
115
ffmpeg_data = pa_xnew(struct ffmpeg_data, 1);
117
/* We could probably implement different quality levels by
118
* adjusting the filter parameters here. However, ffmpeg
119
* internally only uses these hardcoded values, so let's use them
120
* here for now as well until ffmpeg makes this configurable. */
122
if (!(ffmpeg_data->state = av_resample_init((int) r->o_ss.rate, (int) r->i_ss.rate, 16, 10, 0, 0.8)))
125
r->impl.free = ffmpeg_free;
126
r->impl.resample = ffmpeg_resample;
127
r->impl.data = (void *) ffmpeg_data;