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