1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <pulse/xmalloc.h>
#include "pulsecore/ffmpeg/avcodec.h"
#include <pulsecore/resampler.h>
struct ffmpeg_data { /* data specific to ffmpeg */
struct AVResampleContext *state;
};
static unsigned ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
unsigned used_frames = 0, c;
int previous_consumed_frames = -1;
struct ffmpeg_data *ffmpeg_data;
pa_assert(r);
pa_assert(input);
pa_assert(output);
pa_assert(out_n_frames);
ffmpeg_data = r->impl.data;
for (c = 0; c < r->work_channels; c++) {
unsigned u;
pa_memblock *b, *w;
int16_t *p, *t, *k, *q, *s;
int consumed_frames;
/* Allocate a new block */
b = pa_memblock_new(r->mempool, in_n_frames * sizeof(int16_t));
p = pa_memblock_acquire(b);
/* Now copy the input data, splitting up channels */
t = (int16_t*) pa_memblock_acquire_chunk(input) + c;
k = p;
for (u = 0; u < in_n_frames; u++) {
*k = *t;
t += r->work_channels;
k ++;
}
pa_memblock_release(input->memblock);
/* Allocate buffer for the result */
w = pa_memblock_new(r->mempool, *out_n_frames * sizeof(int16_t));
q = pa_memblock_acquire(w);
/* Now, resample */
used_frames = (unsigned) av_resample(ffmpeg_data->state,
q, p,
&consumed_frames,
(int) in_n_frames, (int) *out_n_frames,
c >= (unsigned) (r->work_channels-1));
pa_memblock_release(b);
pa_memblock_unref(b);
pa_assert(consumed_frames <= (int) in_n_frames);
pa_assert(previous_consumed_frames == -1 || consumed_frames == previous_consumed_frames);
previous_consumed_frames = consumed_frames;
/* And place the results in the output buffer */
s = (int16_t *) pa_memblock_acquire_chunk(output) + c;
for (u = 0; u < used_frames; u++) {
*s = *q;
q++;
s += r->work_channels;
}
pa_memblock_release(output->memblock);
pa_memblock_release(w);
pa_memblock_unref(w);
}
*out_n_frames = used_frames;
return in_n_frames - previous_consumed_frames;
}
static void ffmpeg_free(pa_resampler *r) {
struct ffmpeg_data *ffmpeg_data;
pa_assert(r);
ffmpeg_data = r->impl.data;
if (ffmpeg_data->state)
av_resample_close(ffmpeg_data->state);
}
int pa_resampler_ffmpeg_init(pa_resampler *r) {
struct ffmpeg_data *ffmpeg_data;
pa_assert(r);
ffmpeg_data = pa_xnew(struct ffmpeg_data, 1);
/* We could probably implement different quality levels by
* adjusting the filter parameters here. However, ffmpeg
* internally only uses these hardcoded values, so let's use them
* here for now as well until ffmpeg makes this configurable. */
if (!(ffmpeg_data->state = av_resample_init((int) r->o_ss.rate, (int) r->i_ss.rate, 16, 10, 0, 0.8))) {
pa_xfree(ffmpeg_data);
return -1;
}
r->impl.free = ffmpeg_free;
r->impl.resample = ffmpeg_resample;
r->impl.data = (void *) ffmpeg_data;
return 0;
}
|