annotate ffmpeg/libavfilter/af_aconvert.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) 2010 S.N. Hemanth Meenakshisundaram <smeenaks@ucsd.edu>
yading@10 3 * Copyright (c) 2011 Stefano Sabatini
yading@10 4 * Copyright (c) 2011 Mina Nagy Zaki
yading@10 5 *
yading@10 6 * This file is part of FFmpeg.
yading@10 7 *
yading@10 8 * FFmpeg is free software; you can redistribute it and/or
yading@10 9 * modify it under the terms of the GNU Lesser General Public
yading@10 10 * License as published by the Free Software Foundation; either
yading@10 11 * version 2.1 of the License, or (at your option) any later version.
yading@10 12 *
yading@10 13 * FFmpeg is distributed in the hope that it will be useful,
yading@10 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 16 * Lesser General Public License for more details.
yading@10 17 *
yading@10 18 * You should have received a copy of the GNU Lesser General Public
yading@10 19 * License along with FFmpeg; if not, write to the Free Software
yading@10 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 21 */
yading@10 22
yading@10 23 /**
yading@10 24 * @file
yading@10 25 * sample format and channel layout conversion audio filter
yading@10 26 */
yading@10 27
yading@10 28 #include "libavutil/avstring.h"
yading@10 29 #include "libavutil/channel_layout.h"
yading@10 30 #include "libswresample/swresample.h"
yading@10 31 #include "avfilter.h"
yading@10 32 #include "audio.h"
yading@10 33 #include "internal.h"
yading@10 34
yading@10 35 typedef struct {
yading@10 36 enum AVSampleFormat out_sample_fmt;
yading@10 37 int64_t out_chlayout;
yading@10 38 struct SwrContext *swr;
yading@10 39 } AConvertContext;
yading@10 40
yading@10 41 static av_cold int init(AVFilterContext *ctx)
yading@10 42 {
yading@10 43 AConvertContext *aconvert = ctx->priv;
yading@10 44 char *arg, *ptr = NULL;
yading@10 45 int ret = 0;
yading@10 46 char *args = av_strdup(NULL);
yading@10 47
yading@10 48 av_log(ctx, AV_LOG_WARNING, "This filter is deprecated, use aformat instead\n");
yading@10 49
yading@10 50 aconvert->out_sample_fmt = AV_SAMPLE_FMT_NONE;
yading@10 51 aconvert->out_chlayout = 0;
yading@10 52
yading@10 53 if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) {
yading@10 54 if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
yading@10 55 goto end;
yading@10 56 }
yading@10 57 if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
yading@10 58 if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
yading@10 59 goto end;
yading@10 60 }
yading@10 61
yading@10 62 end:
yading@10 63 av_freep(&args);
yading@10 64 return ret;
yading@10 65 }
yading@10 66
yading@10 67 static av_cold void uninit(AVFilterContext *ctx)
yading@10 68 {
yading@10 69 AConvertContext *aconvert = ctx->priv;
yading@10 70 swr_free(&aconvert->swr);
yading@10 71 }
yading@10 72
yading@10 73 static int query_formats(AVFilterContext *ctx)
yading@10 74 {
yading@10 75 AVFilterFormats *formats = NULL;
yading@10 76 AConvertContext *aconvert = ctx->priv;
yading@10 77 AVFilterLink *inlink = ctx->inputs[0];
yading@10 78 AVFilterLink *outlink = ctx->outputs[0];
yading@10 79 AVFilterChannelLayouts *layouts;
yading@10 80
yading@10 81 ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO),
yading@10 82 &inlink->out_formats);
yading@10 83 if (aconvert->out_sample_fmt != AV_SAMPLE_FMT_NONE) {
yading@10 84 formats = NULL;
yading@10 85 ff_add_format(&formats, aconvert->out_sample_fmt);
yading@10 86 ff_formats_ref(formats, &outlink->in_formats);
yading@10 87 } else
yading@10 88 ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO),
yading@10 89 &outlink->in_formats);
yading@10 90
yading@10 91 ff_channel_layouts_ref(ff_all_channel_layouts(),
yading@10 92 &inlink->out_channel_layouts);
yading@10 93 if (aconvert->out_chlayout != 0) {
yading@10 94 layouts = NULL;
yading@10 95 ff_add_channel_layout(&layouts, aconvert->out_chlayout);
yading@10 96 ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
yading@10 97 } else
yading@10 98 ff_channel_layouts_ref(ff_all_channel_layouts(),
yading@10 99 &outlink->in_channel_layouts);
yading@10 100
yading@10 101 return 0;
yading@10 102 }
yading@10 103
yading@10 104 static int config_output(AVFilterLink *outlink)
yading@10 105 {
yading@10 106 int ret;
yading@10 107 AVFilterContext *ctx = outlink->src;
yading@10 108 AVFilterLink *inlink = ctx->inputs[0];
yading@10 109 AConvertContext *aconvert = ctx->priv;
yading@10 110 char buf1[64], buf2[64];
yading@10 111
yading@10 112 /* if not specified in args, use the format and layout of the output */
yading@10 113 if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE)
yading@10 114 aconvert->out_sample_fmt = outlink->format;
yading@10 115 if (aconvert->out_chlayout == 0)
yading@10 116 aconvert->out_chlayout = outlink->channel_layout;
yading@10 117
yading@10 118 aconvert->swr = swr_alloc_set_opts(aconvert->swr,
yading@10 119 aconvert->out_chlayout, aconvert->out_sample_fmt, inlink->sample_rate,
yading@10 120 inlink->channel_layout, inlink->format, inlink->sample_rate,
yading@10 121 0, ctx);
yading@10 122 if (!aconvert->swr)
yading@10 123 return AVERROR(ENOMEM);
yading@10 124 ret = swr_init(aconvert->swr);
yading@10 125 if (ret < 0)
yading@10 126 return ret;
yading@10 127
yading@10 128 av_get_channel_layout_string(buf1, sizeof(buf1),
yading@10 129 -1, inlink ->channel_layout);
yading@10 130 av_get_channel_layout_string(buf2, sizeof(buf2),
yading@10 131 -1, outlink->channel_layout);
yading@10 132 av_log(ctx, AV_LOG_VERBOSE,
yading@10 133 "fmt:%s cl:%s -> fmt:%s cl:%s\n",
yading@10 134 av_get_sample_fmt_name(inlink ->format), buf1,
yading@10 135 av_get_sample_fmt_name(outlink->format), buf2);
yading@10 136
yading@10 137 return 0;
yading@10 138 }
yading@10 139
yading@10 140 static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
yading@10 141 {
yading@10 142 AConvertContext *aconvert = inlink->dst->priv;
yading@10 143 const int n = insamplesref->nb_samples;
yading@10 144 AVFilterLink *const outlink = inlink->dst->outputs[0];
yading@10 145 AVFrame *outsamplesref = ff_get_audio_buffer(outlink, n);
yading@10 146 int ret;
yading@10 147
yading@10 148 if (!outsamplesref)
yading@10 149 return AVERROR(ENOMEM);
yading@10 150 swr_convert(aconvert->swr, outsamplesref->extended_data, n,
yading@10 151 (void *)insamplesref->extended_data, n);
yading@10 152
yading@10 153 av_frame_copy_props(outsamplesref, insamplesref);
yading@10 154 av_frame_set_channels(outsamplesref, outlink->channels);
yading@10 155 outsamplesref->channel_layout = outlink->channel_layout;
yading@10 156
yading@10 157 ret = ff_filter_frame(outlink, outsamplesref);
yading@10 158 av_frame_free(&insamplesref);
yading@10 159 return ret;
yading@10 160 }
yading@10 161
yading@10 162 static const AVFilterPad aconvert_inputs[] = {
yading@10 163 {
yading@10 164 .name = "default",
yading@10 165 .type = AVMEDIA_TYPE_AUDIO,
yading@10 166 .filter_frame = filter_frame,
yading@10 167 },
yading@10 168 { NULL }
yading@10 169 };
yading@10 170
yading@10 171 static const AVFilterPad aconvert_outputs[] = {
yading@10 172 {
yading@10 173 .name = "default",
yading@10 174 .type = AVMEDIA_TYPE_AUDIO,
yading@10 175 .config_props = config_output,
yading@10 176 },
yading@10 177 { NULL }
yading@10 178 };
yading@10 179
yading@10 180 AVFilter avfilter_af_aconvert = {
yading@10 181 .name = "aconvert",
yading@10 182 .description = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout."),
yading@10 183 .priv_size = sizeof(AConvertContext),
yading@10 184 .init = init,
yading@10 185 .uninit = uninit,
yading@10 186 .query_formats = query_formats,
yading@10 187 .inputs = aconvert_inputs,
yading@10 188 .outputs = aconvert_outputs,
yading@10 189 };