yading@10: /* yading@10: * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram yading@10: * Copyright (c) 2011 Stefano Sabatini yading@10: * Copyright (c) 2011 Mina Nagy Zaki yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * sample format and channel layout conversion audio filter yading@10: */ yading@10: yading@10: #include "libavutil/avstring.h" yading@10: #include "libavutil/channel_layout.h" yading@10: #include "libswresample/swresample.h" yading@10: #include "avfilter.h" yading@10: #include "audio.h" yading@10: #include "internal.h" yading@10: yading@10: typedef struct { yading@10: enum AVSampleFormat out_sample_fmt; yading@10: int64_t out_chlayout; yading@10: struct SwrContext *swr; yading@10: } AConvertContext; yading@10: yading@10: static av_cold int init(AVFilterContext *ctx) yading@10: { yading@10: AConvertContext *aconvert = ctx->priv; yading@10: char *arg, *ptr = NULL; yading@10: int ret = 0; yading@10: char *args = av_strdup(NULL); yading@10: yading@10: av_log(ctx, AV_LOG_WARNING, "This filter is deprecated, use aformat instead\n"); yading@10: yading@10: aconvert->out_sample_fmt = AV_SAMPLE_FMT_NONE; yading@10: aconvert->out_chlayout = 0; yading@10: yading@10: if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) { yading@10: if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0) yading@10: goto end; yading@10: } yading@10: if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) { yading@10: if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0) yading@10: goto end; yading@10: } yading@10: yading@10: end: yading@10: av_freep(&args); yading@10: return ret; yading@10: } yading@10: yading@10: static av_cold void uninit(AVFilterContext *ctx) yading@10: { yading@10: AConvertContext *aconvert = ctx->priv; yading@10: swr_free(&aconvert->swr); yading@10: } yading@10: yading@10: static int query_formats(AVFilterContext *ctx) yading@10: { yading@10: AVFilterFormats *formats = NULL; yading@10: AConvertContext *aconvert = ctx->priv; yading@10: AVFilterLink *inlink = ctx->inputs[0]; yading@10: AVFilterLink *outlink = ctx->outputs[0]; yading@10: AVFilterChannelLayouts *layouts; yading@10: yading@10: ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO), yading@10: &inlink->out_formats); yading@10: if (aconvert->out_sample_fmt != AV_SAMPLE_FMT_NONE) { yading@10: formats = NULL; yading@10: ff_add_format(&formats, aconvert->out_sample_fmt); yading@10: ff_formats_ref(formats, &outlink->in_formats); yading@10: } else yading@10: ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO), yading@10: &outlink->in_formats); yading@10: yading@10: ff_channel_layouts_ref(ff_all_channel_layouts(), yading@10: &inlink->out_channel_layouts); yading@10: if (aconvert->out_chlayout != 0) { yading@10: layouts = NULL; yading@10: ff_add_channel_layout(&layouts, aconvert->out_chlayout); yading@10: ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts); yading@10: } else yading@10: ff_channel_layouts_ref(ff_all_channel_layouts(), yading@10: &outlink->in_channel_layouts); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int config_output(AVFilterLink *outlink) yading@10: { yading@10: int ret; yading@10: AVFilterContext *ctx = outlink->src; yading@10: AVFilterLink *inlink = ctx->inputs[0]; yading@10: AConvertContext *aconvert = ctx->priv; yading@10: char buf1[64], buf2[64]; yading@10: yading@10: /* if not specified in args, use the format and layout of the output */ yading@10: if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE) yading@10: aconvert->out_sample_fmt = outlink->format; yading@10: if (aconvert->out_chlayout == 0) yading@10: aconvert->out_chlayout = outlink->channel_layout; yading@10: yading@10: aconvert->swr = swr_alloc_set_opts(aconvert->swr, yading@10: aconvert->out_chlayout, aconvert->out_sample_fmt, inlink->sample_rate, yading@10: inlink->channel_layout, inlink->format, inlink->sample_rate, yading@10: 0, ctx); yading@10: if (!aconvert->swr) yading@10: return AVERROR(ENOMEM); yading@10: ret = swr_init(aconvert->swr); yading@10: if (ret < 0) yading@10: return ret; yading@10: yading@10: av_get_channel_layout_string(buf1, sizeof(buf1), yading@10: -1, inlink ->channel_layout); yading@10: av_get_channel_layout_string(buf2, sizeof(buf2), yading@10: -1, outlink->channel_layout); yading@10: av_log(ctx, AV_LOG_VERBOSE, yading@10: "fmt:%s cl:%s -> fmt:%s cl:%s\n", yading@10: av_get_sample_fmt_name(inlink ->format), buf1, yading@10: av_get_sample_fmt_name(outlink->format), buf2); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref) yading@10: { yading@10: AConvertContext *aconvert = inlink->dst->priv; yading@10: const int n = insamplesref->nb_samples; yading@10: AVFilterLink *const outlink = inlink->dst->outputs[0]; yading@10: AVFrame *outsamplesref = ff_get_audio_buffer(outlink, n); yading@10: int ret; yading@10: yading@10: if (!outsamplesref) yading@10: return AVERROR(ENOMEM); yading@10: swr_convert(aconvert->swr, outsamplesref->extended_data, n, yading@10: (void *)insamplesref->extended_data, n); yading@10: yading@10: av_frame_copy_props(outsamplesref, insamplesref); yading@10: av_frame_set_channels(outsamplesref, outlink->channels); yading@10: outsamplesref->channel_layout = outlink->channel_layout; yading@10: yading@10: ret = ff_filter_frame(outlink, outsamplesref); yading@10: av_frame_free(&insamplesref); yading@10: return ret; yading@10: } yading@10: yading@10: static const AVFilterPad aconvert_inputs[] = { yading@10: { yading@10: .name = "default", yading@10: .type = AVMEDIA_TYPE_AUDIO, yading@10: .filter_frame = filter_frame, yading@10: }, yading@10: { NULL } yading@10: }; yading@10: yading@10: static const AVFilterPad aconvert_outputs[] = { yading@10: { yading@10: .name = "default", yading@10: .type = AVMEDIA_TYPE_AUDIO, yading@10: .config_props = config_output, yading@10: }, yading@10: { NULL } yading@10: }; yading@10: yading@10: AVFilter avfilter_af_aconvert = { yading@10: .name = "aconvert", yading@10: .description = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout."), yading@10: .priv_size = sizeof(AConvertContext), yading@10: .init = init, yading@10: .uninit = uninit, yading@10: .query_formats = query_formats, yading@10: .inputs = aconvert_inputs, yading@10: .outputs = aconvert_outputs, yading@10: };