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