• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***
2   This file is part of PulseAudio.
3 
4   Copyright 2004-2006 Lennart Poettering
5 
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.
10 
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.
15 
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/>.
18 ***/
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include <pulse/xmalloc.h>
25 #include "pulsecore/ffmpeg/avcodec.h"
26 
27 #include <pulsecore/resampler.h>
28 
29 struct ffmpeg_data { /* data specific to ffmpeg */
30     struct AVResampleContext *state;
31 };
32 
ffmpeg_resample(pa_resampler * r,const pa_memchunk * input,unsigned in_n_frames,pa_memchunk * output,unsigned * out_n_frames)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;
37 
38     pa_assert(r);
39     pa_assert(input);
40     pa_assert(output);
41     pa_assert(out_n_frames);
42 
43     ffmpeg_data = r->impl.data;
44 
45     for (c = 0; c < r->work_channels; c++) {
46         unsigned u;
47         pa_memblock *b, *w;
48         int16_t *p, *t, *k, *q, *s;
49         int consumed_frames;
50 
51         /* Allocate a new block */
52         b = pa_memblock_new(r->mempool, in_n_frames * sizeof(int16_t));
53         p = pa_memblock_acquire(b);
54 
55         /* Now copy the input data, splitting up channels */
56         t = (int16_t*) pa_memblock_acquire_chunk(input) + c;
57         k = p;
58         for (u = 0; u < in_n_frames; u++) {
59             *k = *t;
60             t += r->work_channels;
61             k ++;
62         }
63         pa_memblock_release(input->memblock);
64 
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);
68 
69         /* Now, resample */
70         used_frames = (unsigned) av_resample(ffmpeg_data->state,
71                                              q, p,
72                                              &consumed_frames,
73                                              (int) in_n_frames, (int) *out_n_frames,
74                                              c >= (unsigned) (r->work_channels-1));
75 
76         pa_memblock_release(b);
77         pa_memblock_unref(b);
78 
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;
82 
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++) {
86             *s = *q;
87             q++;
88             s += r->work_channels;
89         }
90         pa_memblock_release(output->memblock);
91         pa_memblock_release(w);
92         pa_memblock_unref(w);
93     }
94 
95     *out_n_frames = used_frames;
96 
97     return in_n_frames - previous_consumed_frames;
98 }
99 
ffmpeg_free(pa_resampler * r)100 static void ffmpeg_free(pa_resampler *r) {
101     struct ffmpeg_data *ffmpeg_data;
102 
103     pa_assert(r);
104 
105     ffmpeg_data = r->impl.data;
106     if (ffmpeg_data->state)
107         av_resample_close(ffmpeg_data->state);
108 }
109 
pa_resampler_ffmpeg_init(pa_resampler * r)110 int pa_resampler_ffmpeg_init(pa_resampler *r) {
111     struct ffmpeg_data *ffmpeg_data;
112 
113     pa_assert(r);
114 
115     ffmpeg_data = pa_xnew(struct ffmpeg_data, 1);
116 
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. */
121 
122     if (!(ffmpeg_data->state = av_resample_init((int) r->o_ss.rate, (int) r->i_ss.rate, 16, 10, 0, 0.8))) {
123         pa_xfree(ffmpeg_data);
124         return -1;
125     }
126 
127     r->impl.free = ffmpeg_free;
128     r->impl.resample = ffmpeg_resample;
129     r->impl.data = (void *) ffmpeg_data;
130 
131     return 0;
132 }
133