File: filter_resample.c

package info (click to toggle)
transcode 3%3A1.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 11,644 kB
  • sloc: ansic: 116,927; sh: 11,468; xml: 2,849; makefile: 1,891; perl: 1,492; pascal: 526; php: 191; python: 144; sed: 43
file content (269 lines) | stat: -rw-r--r-- 8,066 bytes parent folder | download | duplicates (2)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 *  filter_resample.c
 *
 *  Copyright (C) Thomas Oestreich - February 2002
 *
 *  This file is part of transcode, a video stream processing tool
 *
 *  transcode is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  transcode 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 General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#define MOD_NAME    "filter_resample.so"
#define MOD_VERSION "v0.1.6 (2007-06-02)"
#define MOD_CAP     "audio resampling filter plugin using libavcodec"
#define MOD_AUTHOR  "Thomas Oestreich, Stefan Scheffler"

#define MOD_FEATURES \
    TC_MODULE_FEATURE_FILTER|TC_MODULE_FEATURE_AUDIO
#define MOD_FLAGS \
    TC_MODULE_FLAG_RECONFIGURABLE|TC_MODULE_FLAG_BUFFERING

#include "transcode.h"
#include "filter.h"
#include "libtc/libtc.h"
#include "libtc/optstr.h"
#include "libtc/tcavcodec.h"
#include "libtc/tcmodule-plugin.h"


typedef struct {
    uint8_t *resample_buf;
    size_t resample_bufsize;

    int bytes_per_sample;

    ReSampleContext *resample_ctx;
} ResamplePrivateData;

static const char resample_help[] = ""
    "Overview:\n"
    "    This filter resample an audio stream using libavcodec facilties.\n"
    "    i.e. changes input sample rate to 22050 Hz to 48000 Hz.\n"
    "Options:\n"
    "    help    show this message.\n";


/*-------------------------------------------------*/

TC_MODULE_GENERIC_INIT(resample, ResamplePrivateData)

static int resample_configure(TCModuleInstance *self,
                              const char *options, vob_t *vob)
{
    double samples_per_frame, ratio;
    ResamplePrivateData *pd = NULL;

    TC_MODULE_SELF_CHECK(self, "configure");
    TC_MODULE_SELF_CHECK(vob, "configure"); /* paranoia */

    pd = self->userdata;

    if (!vob->a_rate || !vob->mp3frequency) {
        tc_log_error(MOD_NAME, "Invalid settings");
        return TC_ERROR;
    }
    tc_log_info(MOD_NAME, "resampling: %i Hz -> %i Hz",
                vob->a_rate, vob->mp3frequency);
    if (vob->a_rate == vob->mp3frequency) {
        tc_log_error(MOD_NAME, "Frequencies are identical,"
                     " filter skipped");
        return TC_ERROR;
    }
 
    pd->bytes_per_sample = vob->a_chan * vob->a_bits/8;
    samples_per_frame = vob->a_rate/vob->ex_fps;
    ratio = (float)vob->mp3frequency/(float)vob->a_rate;

    pd->resample_bufsize = (int)(samples_per_frame * ratio) * pd->bytes_per_sample + 16 // frame + 16 bytes
                            + ((vob->a_leap_bytes > 0)?(int)(vob->a_leap_bytes * ratio) :0); 
                           // leap bytes .. kinda
    /* XXX */

    pd->resample_buf = tc_malloc(pd->resample_bufsize);
    if (pd->resample_buf == NULL) {
        tc_log_error(MOD_NAME, "Buffer allocation failed");
        return TC_ERROR;
    }

    if (verbose >= TC_DEBUG) {
        tc_log_info(MOD_NAME,
                    "bufsize : %lu, bytes : %i, bytesfreq/fps: %i, rest %i",
                    (unsigned long)pd->resample_bufsize, pd->bytes_per_sample,
                    vob->mp3frequency * pd->bytes_per_sample / (int)vob->fps,
                    (vob->a_leap_bytes > 0 )?(int)(vob->a_leap_bytes * ratio):0);
    }

    if ((size_t)(pd->bytes_per_sample * vob->mp3frequency / vob->fps) > pd->resample_bufsize) {
        goto abort;
    }

    pd->resample_ctx = av_audio_resample_init(vob->a_chan, vob->a_chan,
                                           vob->mp3frequency, vob->a_rate,
                                           AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16,
                                           16, 10, 0, 0.8);
    if (pd->resample_ctx == NULL) {
        tc_log_error(MOD_NAME, "can't get a resample context");
        goto abort;
    }

    /* 
     * this will force this resample filter to do the job, not the export module.
     * Yeah, that's nasty. -- FR.
     */

    vob->a_rate = vob->mp3frequency;
    vob->mp3frequency = 0;
    vob->ex_a_size = pd->resample_bufsize;

    self->userdata = pd;

    return TC_OK;

abort:
    tc_free(pd->resample_buf);
    pd->resample_buf = NULL;
    return TC_ERROR;
}

TC_MODULE_GENERIC_FINI(resample)

static int resample_stop(TCModuleInstance *self)
{
    ResamplePrivateData *pd = NULL;

    TC_MODULE_SELF_CHECK(self, "stop");

    pd = self->userdata;

    if (pd->resample_ctx != NULL) {
        audio_resample_close(pd->resample_ctx);
        pd->resample_ctx = NULL;
    }
    if (pd->resample_buf != NULL) {
        tc_free(pd->resample_buf);
        pd->resample_buf = NULL;
    }

    return TC_OK;
}

static int resample_inspect(TCModuleInstance *self,
                            const char *param, const char **value)
{
    TC_MODULE_SELF_CHECK(self, "inspect");
    TC_MODULE_SELF_CHECK(param, "inspect");
    
    if (optstr_lookup(param, "help")) {
        *value = resample_help;
    }

    return TC_OK;
}

/* internal helper to avoid an useless double if() */
static int resample_filter_audio(TCModuleInstance *self, aframe_list_t *frame)
{
    ResamplePrivateData *pd = self->userdata;

    if (pd->resample_bufsize == 0) {
        /* XXX: really useful? can happen? */
        tc_log_error(__FILE__, "wrong (insane) buffer size");
        return TC_ERROR;
    }
    if (verbose >= TC_STATS)
        tc_log_info(MOD_NAME, "inbuf: %i, bufsize: %lu",
                    frame->audio_size, (unsigned long)pd->resample_bufsize);
    frame->audio_size = audio_resample(pd->resample_ctx,
                                       (int16_t*)pd->resample_buf,
                                       (int16_t*)frame->audio_buf,
                                       frame->audio_size/pd->bytes_per_sample);
    frame->audio_size *= pd->bytes_per_sample;
    if (verbose >= TC_STATS)
        tc_log_info(MOD_NAME, "outbuf: %i", frame->audio_size);

    if (frame->audio_size < 0)
        frame->audio_size = 0;

    ac_memcpy(frame->audio_buf, pd->resample_buf, frame->audio_size);

    return TC_OK;
}


/**************************************************************************/

static const TCCodecID resample_codecs_in[] = { 
    TC_CODEC_PCM, TC_CODEC_ERROR
};
static const TCCodecID resample_codecs_out[] = { 
    TC_CODEC_PCM, TC_CODEC_ERROR
};
TC_MODULE_FILTER_FORMATS(resample);

TC_MODULE_INFO(resample);

static const TCModuleClass resample_class = {
    TC_MODULE_CLASS_HEAD(resample),

    .init         = resample_init,
    .fini         = resample_fini,
    .configure    = resample_configure,
    .stop         = resample_stop,
    .inspect      = resample_inspect,

    .filter_audio = resample_filter_audio,
};

TC_MODULE_ENTRY_POINT(resample)

/*************************************************************************/

static int resample_get_config(TCModuleInstance *self, char *options)
{
    TC_MODULE_SELF_CHECK(self, "get_config");

    optstr_filter_desc(options, MOD_NAME, MOD_CAP, MOD_VERSION,
                       MOD_AUTHOR, "AE", "1");

    return TC_OK;
}


static int resample_process(TCModuleInstance *self, frame_list_t *frame)
{
    TC_MODULE_SELF_CHECK(self, "process");

    if (frame->tag & TC_PRE_S_PROCESS && frame->tag & TC_AUDIO) {
        return resample_filter_audio(self, (aframe_list_t*)frame);
    }
    return TC_OK;
}

/*************************************************************************/

TC_FILTER_OLDINTERFACE(resample)

/*************************************************************************/

/*
 * Local variables:
 *   c-file-style: "stroustrup"
 *   c-file-offsets: ((case-label . *) (statement-case-intro . *))
 *   indent-tabs-mode: nil
 * End:
 *
 * vim: expandtab shiftwidth=4:
 */