annotate ffmpeg/libavfilter/af_aresample.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 * Copyright (c) 2011 Stefano Sabatini
yading@10 3 * Copyright (c) 2011 Mina Nagy Zaki
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 /**
yading@10 23 * @file
yading@10 24 * resampling audio filter
yading@10 25 */
yading@10 26
yading@10 27 #include "libavutil/avstring.h"
yading@10 28 #include "libavutil/channel_layout.h"
yading@10 29 #include "libavutil/opt.h"
yading@10 30 #include "libavutil/samplefmt.h"
yading@10 31 #include "libavutil/avassert.h"
yading@10 32 #include "libswresample/swresample.h"
yading@10 33 #include "avfilter.h"
yading@10 34 #include "audio.h"
yading@10 35 #include "internal.h"
yading@10 36
yading@10 37 typedef struct {
yading@10 38 const AVClass *class;
yading@10 39 int sample_rate_arg;
yading@10 40 double ratio;
yading@10 41 struct SwrContext *swr;
yading@10 42 int64_t next_pts;
yading@10 43 int req_fullfilled;
yading@10 44 } AResampleContext;
yading@10 45
yading@10 46 static av_cold int init_dict(AVFilterContext *ctx, AVDictionary **opts)
yading@10 47 {
yading@10 48 AResampleContext *aresample = ctx->priv;
yading@10 49 int ret = 0;
yading@10 50
yading@10 51 aresample->next_pts = AV_NOPTS_VALUE;
yading@10 52 aresample->swr = swr_alloc();
yading@10 53 if (!aresample->swr) {
yading@10 54 ret = AVERROR(ENOMEM);
yading@10 55 goto end;
yading@10 56 }
yading@10 57
yading@10 58 if (opts) {
yading@10 59 AVDictionaryEntry *e = NULL;
yading@10 60
yading@10 61 while ((e = av_dict_get(*opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
yading@10 62 const char *token = e->key;
yading@10 63 const char *value = e->value;
yading@10 64 if ((ret = av_opt_set(aresample->swr, token, value, 0)) < 0)
yading@10 65 goto end;
yading@10 66 }
yading@10 67 av_dict_free(opts);
yading@10 68 }
yading@10 69 if (aresample->sample_rate_arg > 0)
yading@10 70 av_opt_set_int(aresample->swr, "osr", aresample->sample_rate_arg, 0);
yading@10 71 end:
yading@10 72 return ret;
yading@10 73 }
yading@10 74
yading@10 75 static av_cold void uninit(AVFilterContext *ctx)
yading@10 76 {
yading@10 77 AResampleContext *aresample = ctx->priv;
yading@10 78 swr_free(&aresample->swr);
yading@10 79 }
yading@10 80
yading@10 81 static int query_formats(AVFilterContext *ctx)
yading@10 82 {
yading@10 83 AResampleContext *aresample = ctx->priv;
yading@10 84 int out_rate = av_get_int(aresample->swr, "osr", NULL);
yading@10 85 uint64_t out_layout = av_get_int(aresample->swr, "ocl", NULL);
yading@10 86 enum AVSampleFormat out_format = av_get_int(aresample->swr, "osf", NULL);
yading@10 87
yading@10 88 AVFilterLink *inlink = ctx->inputs[0];
yading@10 89 AVFilterLink *outlink = ctx->outputs[0];
yading@10 90
yading@10 91 AVFilterFormats *in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
yading@10 92 AVFilterFormats *out_formats;
yading@10 93 AVFilterFormats *in_samplerates = ff_all_samplerates();
yading@10 94 AVFilterFormats *out_samplerates;
yading@10 95 AVFilterChannelLayouts *in_layouts = ff_all_channel_counts();
yading@10 96 AVFilterChannelLayouts *out_layouts;
yading@10 97
yading@10 98 ff_formats_ref (in_formats, &inlink->out_formats);
yading@10 99 ff_formats_ref (in_samplerates, &inlink->out_samplerates);
yading@10 100 ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
yading@10 101
yading@10 102 if(out_rate > 0) {
yading@10 103 out_samplerates = ff_make_format_list((int[]){ out_rate, -1 });
yading@10 104 } else {
yading@10 105 out_samplerates = ff_all_samplerates();
yading@10 106 }
yading@10 107 ff_formats_ref(out_samplerates, &outlink->in_samplerates);
yading@10 108
yading@10 109 if(out_format != AV_SAMPLE_FMT_NONE) {
yading@10 110 out_formats = ff_make_format_list((int[]){ out_format, -1 });
yading@10 111 } else
yading@10 112 out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
yading@10 113 ff_formats_ref(out_formats, &outlink->in_formats);
yading@10 114
yading@10 115 if(out_layout) {
yading@10 116 out_layouts = avfilter_make_format64_list((int64_t[]){ out_layout, -1 });
yading@10 117 } else
yading@10 118 out_layouts = ff_all_channel_counts();
yading@10 119 ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
yading@10 120
yading@10 121 return 0;
yading@10 122 }
yading@10 123
yading@10 124
yading@10 125 static int config_output(AVFilterLink *outlink)
yading@10 126 {
yading@10 127 int ret;
yading@10 128 AVFilterContext *ctx = outlink->src;
yading@10 129 AVFilterLink *inlink = ctx->inputs[0];
yading@10 130 AResampleContext *aresample = ctx->priv;
yading@10 131 int out_rate;
yading@10 132 uint64_t out_layout;
yading@10 133 enum AVSampleFormat out_format;
yading@10 134 char inchl_buf[128], outchl_buf[128];
yading@10 135
yading@10 136 aresample->swr = swr_alloc_set_opts(aresample->swr,
yading@10 137 outlink->channel_layout, outlink->format, outlink->sample_rate,
yading@10 138 inlink->channel_layout, inlink->format, inlink->sample_rate,
yading@10 139 0, ctx);
yading@10 140 if (!aresample->swr)
yading@10 141 return AVERROR(ENOMEM);
yading@10 142 if (!inlink->channel_layout)
yading@10 143 av_opt_set_int(aresample->swr, "ich", inlink->channels, 0);
yading@10 144 if (!outlink->channel_layout)
yading@10 145 av_opt_set_int(aresample->swr, "och", outlink->channels, 0);
yading@10 146
yading@10 147 ret = swr_init(aresample->swr);
yading@10 148 if (ret < 0)
yading@10 149 return ret;
yading@10 150
yading@10 151 out_rate = av_get_int(aresample->swr, "osr", NULL);
yading@10 152 out_layout = av_get_int(aresample->swr, "ocl", NULL);
yading@10 153 out_format = av_get_int(aresample->swr, "osf", NULL);
yading@10 154 outlink->time_base = (AVRational) {1, out_rate};
yading@10 155
yading@10 156 av_assert0(outlink->sample_rate == out_rate);
yading@10 157 av_assert0(outlink->channel_layout == out_layout || !outlink->channel_layout);
yading@10 158 av_assert0(outlink->format == out_format);
yading@10 159
yading@10 160 aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
yading@10 161
yading@10 162 av_get_channel_layout_string(inchl_buf, sizeof(inchl_buf), inlink ->channels, inlink ->channel_layout);
yading@10 163 av_get_channel_layout_string(outchl_buf, sizeof(outchl_buf), outlink->channels, outlink->channel_layout);
yading@10 164
yading@10 165 av_log(ctx, AV_LOG_VERBOSE, "ch:%d chl:%s fmt:%s r:%dHz -> ch:%d chl:%s fmt:%s r:%dHz\n",
yading@10 166 inlink ->channels, inchl_buf, av_get_sample_fmt_name(inlink->format), inlink->sample_rate,
yading@10 167 outlink->channels, outchl_buf, av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
yading@10 168 return 0;
yading@10 169 }
yading@10 170
yading@10 171 static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
yading@10 172 {
yading@10 173 AResampleContext *aresample = inlink->dst->priv;
yading@10 174 const int n_in = insamplesref->nb_samples;
yading@10 175 int n_out = n_in * aresample->ratio * 2 + 256;
yading@10 176 AVFilterLink *const outlink = inlink->dst->outputs[0];
yading@10 177 AVFrame *outsamplesref = ff_get_audio_buffer(outlink, n_out);
yading@10 178 int ret;
yading@10 179
yading@10 180 if(!outsamplesref)
yading@10 181 return AVERROR(ENOMEM);
yading@10 182
yading@10 183 av_frame_copy_props(outsamplesref, insamplesref);
yading@10 184 outsamplesref->format = outlink->format;
yading@10 185 av_frame_set_channels(outsamplesref, outlink->channels);
yading@10 186 outsamplesref->channel_layout = outlink->channel_layout;
yading@10 187 outsamplesref->sample_rate = outlink->sample_rate;
yading@10 188
yading@10 189 if(insamplesref->pts != AV_NOPTS_VALUE) {
yading@10 190 int64_t inpts = av_rescale(insamplesref->pts, inlink->time_base.num * (int64_t)outlink->sample_rate * inlink->sample_rate, inlink->time_base.den);
yading@10 191 int64_t outpts= swr_next_pts(aresample->swr, inpts);
yading@10 192 aresample->next_pts =
yading@10 193 outsamplesref->pts = ROUNDED_DIV(outpts, inlink->sample_rate);
yading@10 194 } else {
yading@10 195 outsamplesref->pts = AV_NOPTS_VALUE;
yading@10 196 }
yading@10 197 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out,
yading@10 198 (void *)insamplesref->extended_data, n_in);
yading@10 199 if (n_out <= 0) {
yading@10 200 av_frame_free(&outsamplesref);
yading@10 201 av_frame_free(&insamplesref);
yading@10 202 return 0;
yading@10 203 }
yading@10 204
yading@10 205 outsamplesref->nb_samples = n_out;
yading@10 206
yading@10 207 ret = ff_filter_frame(outlink, outsamplesref);
yading@10 208 aresample->req_fullfilled= 1;
yading@10 209 av_frame_free(&insamplesref);
yading@10 210 return ret;
yading@10 211 }
yading@10 212
yading@10 213 static int request_frame(AVFilterLink *outlink)
yading@10 214 {
yading@10 215 AVFilterContext *ctx = outlink->src;
yading@10 216 AResampleContext *aresample = ctx->priv;
yading@10 217 AVFilterLink *const inlink = outlink->src->inputs[0];
yading@10 218 int ret;
yading@10 219
yading@10 220 aresample->req_fullfilled = 0;
yading@10 221 do{
yading@10 222 ret = ff_request_frame(ctx->inputs[0]);
yading@10 223 }while(!aresample->req_fullfilled && ret>=0);
yading@10 224
yading@10 225 if (ret == AVERROR_EOF) {
yading@10 226 AVFrame *outsamplesref;
yading@10 227 int n_out = 4096;
yading@10 228
yading@10 229 outsamplesref = ff_get_audio_buffer(outlink, n_out);
yading@10 230 if (!outsamplesref)
yading@10 231 return AVERROR(ENOMEM);
yading@10 232 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, 0, 0);
yading@10 233 if (n_out <= 0) {
yading@10 234 av_frame_free(&outsamplesref);
yading@10 235 return (n_out == 0) ? AVERROR_EOF : n_out;
yading@10 236 }
yading@10 237
yading@10 238 outsamplesref->sample_rate = outlink->sample_rate;
yading@10 239 outsamplesref->nb_samples = n_out;
yading@10 240 #if 0
yading@10 241 outsamplesref->pts = aresample->next_pts;
yading@10 242 if(aresample->next_pts != AV_NOPTS_VALUE)
yading@10 243 aresample->next_pts += av_rescale_q(n_out, (AVRational){1 ,outlink->sample_rate}, outlink->time_base);
yading@10 244 #else
yading@10 245 outsamplesref->pts = swr_next_pts(aresample->swr, INT64_MIN);
yading@10 246 outsamplesref->pts = ROUNDED_DIV(outsamplesref->pts, inlink->sample_rate);
yading@10 247 #endif
yading@10 248
yading@10 249 return ff_filter_frame(outlink, outsamplesref);
yading@10 250 }
yading@10 251 return ret;
yading@10 252 }
yading@10 253
yading@10 254 static const AVClass *resample_child_class_next(const AVClass *prev)
yading@10 255 {
yading@10 256 return prev ? NULL : swr_get_class();
yading@10 257 }
yading@10 258
yading@10 259 static void *resample_child_next(void *obj, void *prev)
yading@10 260 {
yading@10 261 AResampleContext *s = obj;
yading@10 262 return prev ? NULL : s->swr;
yading@10 263 }
yading@10 264
yading@10 265 #define OFFSET(x) offsetof(AResampleContext, x)
yading@10 266 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
yading@10 267
yading@10 268 static const AVOption options[] = {
yading@10 269 {"sample_rate", NULL, OFFSET(sample_rate_arg), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
yading@10 270 {NULL}
yading@10 271 };
yading@10 272
yading@10 273 static const AVClass aresample_class = {
yading@10 274 .class_name = "aresample",
yading@10 275 .item_name = av_default_item_name,
yading@10 276 .option = options,
yading@10 277 .version = LIBAVUTIL_VERSION_INT,
yading@10 278 .child_class_next = resample_child_class_next,
yading@10 279 .child_next = resample_child_next,
yading@10 280 };
yading@10 281
yading@10 282 static const AVFilterPad aresample_inputs[] = {
yading@10 283 {
yading@10 284 .name = "default",
yading@10 285 .type = AVMEDIA_TYPE_AUDIO,
yading@10 286 .filter_frame = filter_frame,
yading@10 287 },
yading@10 288 { NULL },
yading@10 289 };
yading@10 290
yading@10 291 static const AVFilterPad aresample_outputs[] = {
yading@10 292 {
yading@10 293 .name = "default",
yading@10 294 .config_props = config_output,
yading@10 295 .request_frame = request_frame,
yading@10 296 .type = AVMEDIA_TYPE_AUDIO,
yading@10 297 },
yading@10 298 { NULL },
yading@10 299 };
yading@10 300
yading@10 301 AVFilter avfilter_af_aresample = {
yading@10 302 .name = "aresample",
yading@10 303 .description = NULL_IF_CONFIG_SMALL("Resample audio data."),
yading@10 304 .init_dict = init_dict,
yading@10 305 .uninit = uninit,
yading@10 306 .query_formats = query_formats,
yading@10 307 .priv_size = sizeof(AResampleContext),
yading@10 308 .priv_class = &aresample_class,
yading@10 309 .inputs = aresample_inputs,
yading@10 310 .outputs = aresample_outputs,
yading@10 311 };