annotate ffmpeg/libavfilter/af_resample.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 *
yading@10 3 * This file is part of Libav.
yading@10 4 *
yading@10 5 * Libav is free software; you can redistribute it and/or
yading@10 6 * modify it under the terms of the GNU Lesser General Public
yading@10 7 * License as published by the Free Software Foundation; either
yading@10 8 * version 2.1 of the License, or (at your option) any later version.
yading@10 9 *
yading@10 10 * Libav is distributed in the hope that it will be useful,
yading@10 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 13 * Lesser General Public License for more details.
yading@10 14 *
yading@10 15 * You should have received a copy of the GNU Lesser General Public
yading@10 16 * License along with Libav; if not, write to the Free Software
yading@10 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 18 */
yading@10 19
yading@10 20 /**
yading@10 21 * @file
yading@10 22 * sample format and channel layout conversion audio filter
yading@10 23 */
yading@10 24
yading@10 25 #include "libavutil/avassert.h"
yading@10 26 #include "libavutil/avstring.h"
yading@10 27 #include "libavutil/common.h"
yading@10 28 #include "libavutil/dict.h"
yading@10 29 #include "libavutil/mathematics.h"
yading@10 30 #include "libavutil/opt.h"
yading@10 31
yading@10 32 #include "libavresample/avresample.h"
yading@10 33
yading@10 34 #include "audio.h"
yading@10 35 #include "avfilter.h"
yading@10 36 #include "formats.h"
yading@10 37 #include "internal.h"
yading@10 38
yading@10 39 typedef struct ResampleContext {
yading@10 40 const AVClass *class;
yading@10 41 AVAudioResampleContext *avr;
yading@10 42 AVDictionary *options;
yading@10 43
yading@10 44 int64_t next_pts;
yading@10 45
yading@10 46 /* set by filter_frame() to signal an output frame to request_frame() */
yading@10 47 int got_output;
yading@10 48 } ResampleContext;
yading@10 49
yading@10 50 static av_cold int init(AVFilterContext *ctx, AVDictionary **opts)
yading@10 51 {
yading@10 52 ResampleContext *s = ctx->priv;
yading@10 53 const AVClass *avr_class = avresample_get_class();
yading@10 54 AVDictionaryEntry *e = NULL;
yading@10 55
yading@10 56 while ((e = av_dict_get(*opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
yading@10 57 if (av_opt_find(&avr_class, e->key, NULL, 0,
yading@10 58 AV_OPT_SEARCH_FAKE_OBJ | AV_OPT_SEARCH_CHILDREN))
yading@10 59 av_dict_set(&s->options, e->key, e->value, 0);
yading@10 60 }
yading@10 61
yading@10 62 e = NULL;
yading@10 63 while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
yading@10 64 av_dict_set(opts, e->key, NULL, 0);
yading@10 65
yading@10 66 /* do not allow the user to override basic format options */
yading@10 67 av_dict_set(&s->options, "in_channel_layout", NULL, 0);
yading@10 68 av_dict_set(&s->options, "out_channel_layout", NULL, 0);
yading@10 69 av_dict_set(&s->options, "in_sample_fmt", NULL, 0);
yading@10 70 av_dict_set(&s->options, "out_sample_fmt", NULL, 0);
yading@10 71 av_dict_set(&s->options, "in_sample_rate", NULL, 0);
yading@10 72 av_dict_set(&s->options, "out_sample_rate", NULL, 0);
yading@10 73
yading@10 74 return 0;
yading@10 75 }
yading@10 76
yading@10 77 static av_cold void uninit(AVFilterContext *ctx)
yading@10 78 {
yading@10 79 ResampleContext *s = ctx->priv;
yading@10 80
yading@10 81 if (s->avr) {
yading@10 82 avresample_close(s->avr);
yading@10 83 avresample_free(&s->avr);
yading@10 84 }
yading@10 85 av_dict_free(&s->options);
yading@10 86 }
yading@10 87
yading@10 88 static int query_formats(AVFilterContext *ctx)
yading@10 89 {
yading@10 90 AVFilterLink *inlink = ctx->inputs[0];
yading@10 91 AVFilterLink *outlink = ctx->outputs[0];
yading@10 92
yading@10 93 AVFilterFormats *in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
yading@10 94 AVFilterFormats *out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
yading@10 95 AVFilterFormats *in_samplerates = ff_all_samplerates();
yading@10 96 AVFilterFormats *out_samplerates = ff_all_samplerates();
yading@10 97 AVFilterChannelLayouts *in_layouts = ff_all_channel_layouts();
yading@10 98 AVFilterChannelLayouts *out_layouts = ff_all_channel_layouts();
yading@10 99
yading@10 100 ff_formats_ref(in_formats, &inlink->out_formats);
yading@10 101 ff_formats_ref(out_formats, &outlink->in_formats);
yading@10 102
yading@10 103 ff_formats_ref(in_samplerates, &inlink->out_samplerates);
yading@10 104 ff_formats_ref(out_samplerates, &outlink->in_samplerates);
yading@10 105
yading@10 106 ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
yading@10 107 ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
yading@10 108
yading@10 109 return 0;
yading@10 110 }
yading@10 111
yading@10 112 static int config_output(AVFilterLink *outlink)
yading@10 113 {
yading@10 114 AVFilterContext *ctx = outlink->src;
yading@10 115 AVFilterLink *inlink = ctx->inputs[0];
yading@10 116 ResampleContext *s = ctx->priv;
yading@10 117 char buf1[64], buf2[64];
yading@10 118 int ret;
yading@10 119
yading@10 120 if (s->avr) {
yading@10 121 avresample_close(s->avr);
yading@10 122 avresample_free(&s->avr);
yading@10 123 }
yading@10 124
yading@10 125 if (inlink->channel_layout == outlink->channel_layout &&
yading@10 126 inlink->sample_rate == outlink->sample_rate &&
yading@10 127 (inlink->format == outlink->format ||
yading@10 128 (av_get_channel_layout_nb_channels(inlink->channel_layout) == 1 &&
yading@10 129 av_get_channel_layout_nb_channels(outlink->channel_layout) == 1 &&
yading@10 130 av_get_planar_sample_fmt(inlink->format) ==
yading@10 131 av_get_planar_sample_fmt(outlink->format))))
yading@10 132 return 0;
yading@10 133
yading@10 134 if (!(s->avr = avresample_alloc_context()))
yading@10 135 return AVERROR(ENOMEM);
yading@10 136
yading@10 137 if (s->options) {
yading@10 138 AVDictionaryEntry *e = NULL;
yading@10 139 while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
yading@10 140 av_log(ctx, AV_LOG_VERBOSE, "lavr option: %s=%s\n", e->key, e->value);
yading@10 141
yading@10 142 av_opt_set_dict(s->avr, &s->options);
yading@10 143 }
yading@10 144
yading@10 145 av_opt_set_int(s->avr, "in_channel_layout", inlink ->channel_layout, 0);
yading@10 146 av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
yading@10 147 av_opt_set_int(s->avr, "in_sample_fmt", inlink ->format, 0);
yading@10 148 av_opt_set_int(s->avr, "out_sample_fmt", outlink->format, 0);
yading@10 149 av_opt_set_int(s->avr, "in_sample_rate", inlink ->sample_rate, 0);
yading@10 150 av_opt_set_int(s->avr, "out_sample_rate", outlink->sample_rate, 0);
yading@10 151
yading@10 152 if ((ret = avresample_open(s->avr)) < 0)
yading@10 153 return ret;
yading@10 154
yading@10 155 outlink->time_base = (AVRational){ 1, outlink->sample_rate };
yading@10 156 s->next_pts = AV_NOPTS_VALUE;
yading@10 157
yading@10 158 av_get_channel_layout_string(buf1, sizeof(buf1),
yading@10 159 -1, inlink ->channel_layout);
yading@10 160 av_get_channel_layout_string(buf2, sizeof(buf2),
yading@10 161 -1, outlink->channel_layout);
yading@10 162 av_log(ctx, AV_LOG_VERBOSE,
yading@10 163 "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
yading@10 164 av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
yading@10 165 av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
yading@10 166
yading@10 167 return 0;
yading@10 168 }
yading@10 169
yading@10 170 static int request_frame(AVFilterLink *outlink)
yading@10 171 {
yading@10 172 AVFilterContext *ctx = outlink->src;
yading@10 173 ResampleContext *s = ctx->priv;
yading@10 174 int ret = 0;
yading@10 175
yading@10 176 s->got_output = 0;
yading@10 177 while (ret >= 0 && !s->got_output)
yading@10 178 ret = ff_request_frame(ctx->inputs[0]);
yading@10 179
yading@10 180 /* flush the lavr delay buffer */
yading@10 181 if (ret == AVERROR_EOF && s->avr) {
yading@10 182 AVFrame *frame;
yading@10 183 int nb_samples = av_rescale_rnd(avresample_get_delay(s->avr),
yading@10 184 outlink->sample_rate,
yading@10 185 ctx->inputs[0]->sample_rate,
yading@10 186 AV_ROUND_UP);
yading@10 187
yading@10 188 if (!nb_samples)
yading@10 189 return ret;
yading@10 190
yading@10 191 frame = ff_get_audio_buffer(outlink, nb_samples);
yading@10 192 if (!frame)
yading@10 193 return AVERROR(ENOMEM);
yading@10 194
yading@10 195 ret = avresample_convert(s->avr, frame->extended_data,
yading@10 196 frame->linesize[0], nb_samples,
yading@10 197 NULL, 0, 0);
yading@10 198 if (ret <= 0) {
yading@10 199 av_frame_free(&frame);
yading@10 200 return (ret == 0) ? AVERROR_EOF : ret;
yading@10 201 }
yading@10 202
yading@10 203 frame->pts = s->next_pts;
yading@10 204 return ff_filter_frame(outlink, frame);
yading@10 205 }
yading@10 206 return ret;
yading@10 207 }
yading@10 208
yading@10 209 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
yading@10 210 {
yading@10 211 AVFilterContext *ctx = inlink->dst;
yading@10 212 ResampleContext *s = ctx->priv;
yading@10 213 AVFilterLink *outlink = ctx->outputs[0];
yading@10 214 int ret;
yading@10 215
yading@10 216 if (s->avr) {
yading@10 217 AVFrame *out;
yading@10 218 int delay, nb_samples;
yading@10 219
yading@10 220 /* maximum possible samples lavr can output */
yading@10 221 delay = avresample_get_delay(s->avr);
yading@10 222 nb_samples = av_rescale_rnd(in->nb_samples + delay,
yading@10 223 outlink->sample_rate, inlink->sample_rate,
yading@10 224 AV_ROUND_UP);
yading@10 225
yading@10 226 out = ff_get_audio_buffer(outlink, nb_samples);
yading@10 227 if (!out) {
yading@10 228 ret = AVERROR(ENOMEM);
yading@10 229 goto fail;
yading@10 230 }
yading@10 231
yading@10 232 ret = avresample_convert(s->avr, out->extended_data, out->linesize[0],
yading@10 233 nb_samples, in->extended_data, in->linesize[0],
yading@10 234 in->nb_samples);
yading@10 235 if (ret <= 0) {
yading@10 236 av_frame_free(&out);
yading@10 237 if (ret < 0)
yading@10 238 goto fail;
yading@10 239 }
yading@10 240
yading@10 241 av_assert0(!avresample_available(s->avr));
yading@10 242
yading@10 243 if (s->next_pts == AV_NOPTS_VALUE) {
yading@10 244 if (in->pts == AV_NOPTS_VALUE) {
yading@10 245 av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
yading@10 246 "assuming 0.\n");
yading@10 247 s->next_pts = 0;
yading@10 248 } else
yading@10 249 s->next_pts = av_rescale_q(in->pts, inlink->time_base,
yading@10 250 outlink->time_base);
yading@10 251 }
yading@10 252
yading@10 253 if (ret > 0) {
yading@10 254 out->nb_samples = ret;
yading@10 255 if (in->pts != AV_NOPTS_VALUE) {
yading@10 256 out->pts = av_rescale_q(in->pts, inlink->time_base,
yading@10 257 outlink->time_base) -
yading@10 258 av_rescale(delay, outlink->sample_rate,
yading@10 259 inlink->sample_rate);
yading@10 260 } else
yading@10 261 out->pts = s->next_pts;
yading@10 262
yading@10 263 s->next_pts = out->pts + out->nb_samples;
yading@10 264
yading@10 265 ret = ff_filter_frame(outlink, out);
yading@10 266 s->got_output = 1;
yading@10 267 }
yading@10 268
yading@10 269 fail:
yading@10 270 av_frame_free(&in);
yading@10 271 } else {
yading@10 272 in->format = outlink->format;
yading@10 273 ret = ff_filter_frame(outlink, in);
yading@10 274 s->got_output = 1;
yading@10 275 }
yading@10 276
yading@10 277 return ret;
yading@10 278 }
yading@10 279
yading@10 280 static const AVClass *resample_child_class_next(const AVClass *prev)
yading@10 281 {
yading@10 282 return prev ? NULL : avresample_get_class();
yading@10 283 }
yading@10 284
yading@10 285 static void *resample_child_next(void *obj, void *prev)
yading@10 286 {
yading@10 287 ResampleContext *s = obj;
yading@10 288 return prev ? NULL : s->avr;
yading@10 289 }
yading@10 290
yading@10 291 static const AVClass resample_class = {
yading@10 292 .class_name = "resample",
yading@10 293 .item_name = av_default_item_name,
yading@10 294 .version = LIBAVUTIL_VERSION_INT,
yading@10 295 .child_class_next = resample_child_class_next,
yading@10 296 .child_next = resample_child_next,
yading@10 297 };
yading@10 298
yading@10 299 static const AVFilterPad avfilter_af_resample_inputs[] = {
yading@10 300 {
yading@10 301 .name = "default",
yading@10 302 .type = AVMEDIA_TYPE_AUDIO,
yading@10 303 .filter_frame = filter_frame,
yading@10 304 },
yading@10 305 { NULL }
yading@10 306 };
yading@10 307
yading@10 308 static const AVFilterPad avfilter_af_resample_outputs[] = {
yading@10 309 {
yading@10 310 .name = "default",
yading@10 311 .type = AVMEDIA_TYPE_AUDIO,
yading@10 312 .config_props = config_output,
yading@10 313 .request_frame = request_frame
yading@10 314 },
yading@10 315 { NULL }
yading@10 316 };
yading@10 317
yading@10 318 AVFilter avfilter_af_resample = {
yading@10 319 .name = "resample",
yading@10 320 .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
yading@10 321 .priv_size = sizeof(ResampleContext),
yading@10 322 .priv_class = &resample_class,
yading@10 323
yading@10 324 .init_dict = init,
yading@10 325 .uninit = uninit,
yading@10 326 .query_formats = query_formats,
yading@10 327
yading@10 328 .inputs = avfilter_af_resample_inputs,
yading@10 329 .outputs = avfilter_af_resample_outputs,
yading@10 330 };