00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "libavutil/audioconvert.h"
00029 #include "libavutil/avstring.h"
00030 #include "libswresample/swresample.h"
00031 #include "avfilter.h"
00032 #include "audio.h"
00033 #include "internal.h"
00034
00035 typedef struct {
00036 enum AVSampleFormat out_sample_fmt;
00037 int64_t out_chlayout;
00038 struct SwrContext *swr;
00039 } AConvertContext;
00040
00041 static av_cold int init(AVFilterContext *ctx, const char *args0)
00042 {
00043 AConvertContext *aconvert = ctx->priv;
00044 char *arg, *ptr = NULL;
00045 int ret = 0;
00046 char *args = av_strdup(args0);
00047
00048 aconvert->out_sample_fmt = AV_SAMPLE_FMT_NONE;
00049 aconvert->out_chlayout = 0;
00050
00051 if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) {
00052 if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
00053 goto end;
00054 }
00055 if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
00056 if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
00057 goto end;
00058 }
00059
00060 end:
00061 av_freep(&args);
00062 return ret;
00063 }
00064
00065 static av_cold void uninit(AVFilterContext *ctx)
00066 {
00067 AConvertContext *aconvert = ctx->priv;
00068 swr_free(&aconvert->swr);
00069 }
00070
00071 static int query_formats(AVFilterContext *ctx)
00072 {
00073 AVFilterFormats *formats = NULL;
00074 AConvertContext *aconvert = ctx->priv;
00075 AVFilterLink *inlink = ctx->inputs[0];
00076 AVFilterLink *outlink = ctx->outputs[0];
00077 AVFilterChannelLayouts *layouts;
00078
00079 ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO),
00080 &inlink->out_formats);
00081 if (aconvert->out_sample_fmt != AV_SAMPLE_FMT_NONE) {
00082 formats = NULL;
00083 ff_add_format(&formats, aconvert->out_sample_fmt);
00084 ff_formats_ref(formats, &outlink->in_formats);
00085 } else
00086 ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO),
00087 &outlink->in_formats);
00088
00089 ff_channel_layouts_ref(ff_all_channel_layouts(),
00090 &inlink->out_channel_layouts);
00091 if (aconvert->out_chlayout != 0) {
00092 layouts = NULL;
00093 ff_add_channel_layout(&layouts, aconvert->out_chlayout);
00094 ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
00095 } else
00096 ff_channel_layouts_ref(ff_all_channel_layouts(),
00097 &outlink->in_channel_layouts);
00098
00099 return 0;
00100 }
00101
00102 static int config_output(AVFilterLink *outlink)
00103 {
00104 int ret;
00105 AVFilterContext *ctx = outlink->src;
00106 AVFilterLink *inlink = ctx->inputs[0];
00107 AConvertContext *aconvert = ctx->priv;
00108 char buf1[64], buf2[64];
00109
00110
00111 if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE)
00112 aconvert->out_sample_fmt = outlink->format;
00113 if (aconvert->out_chlayout == 0)
00114 aconvert->out_chlayout = outlink->channel_layout;
00115
00116 aconvert->swr = swr_alloc_set_opts(aconvert->swr,
00117 aconvert->out_chlayout, aconvert->out_sample_fmt, inlink->sample_rate,
00118 inlink->channel_layout, inlink->format, inlink->sample_rate,
00119 0, ctx);
00120 if (!aconvert->swr)
00121 return AVERROR(ENOMEM);
00122 ret = swr_init(aconvert->swr);
00123 if (ret < 0)
00124 return ret;
00125
00126 av_get_channel_layout_string(buf1, sizeof(buf1),
00127 -1, inlink ->channel_layout);
00128 av_get_channel_layout_string(buf2, sizeof(buf2),
00129 -1, outlink->channel_layout);
00130 av_log(ctx, AV_LOG_VERBOSE,
00131 "fmt:%s cl:%s -> fmt:%s cl:%s\n",
00132 av_get_sample_fmt_name(inlink ->format), buf1,
00133 av_get_sample_fmt_name(outlink->format), buf2);
00134
00135 return 0;
00136 }
00137
00138 static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
00139 {
00140 AConvertContext *aconvert = inlink->dst->priv;
00141 const int n = insamplesref->audio->nb_samples;
00142 AVFilterLink *const outlink = inlink->dst->outputs[0];
00143 AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n);
00144 int ret;
00145
00146 swr_convert(aconvert->swr, outsamplesref->data, n,
00147 (void *)insamplesref->data, n);
00148
00149 avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
00150 outsamplesref->audio->channel_layout = outlink->channel_layout;
00151
00152 ret = ff_filter_samples(outlink, outsamplesref);
00153 avfilter_unref_buffer(insamplesref);
00154 return ret;
00155 }
00156
00157 AVFilter avfilter_af_aconvert = {
00158 .name = "aconvert",
00159 .description = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout."),
00160 .priv_size = sizeof(AConvertContext),
00161 .init = init,
00162 .uninit = uninit,
00163 .query_formats = query_formats,
00164
00165 .inputs = (const AVFilterPad[]) {{ .name = "default",
00166 .type = AVMEDIA_TYPE_AUDIO,
00167 .filter_samples = filter_samples,
00168 .min_perms = AV_PERM_READ, },
00169 { .name = NULL}},
00170 .outputs = (const AVFilterPad[]) {{ .name = "default",
00171 .type = AVMEDIA_TYPE_AUDIO,
00172 .config_props = config_output, },
00173 { .name = NULL}},
00174 };