FFmpeg
 All Data Structures Namespaces 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)
42 {
43  AConvertContext *aconvert = ctx->priv;
44  char *arg, *ptr = NULL;
45  int ret = 0;
46  char *args = av_strdup(NULL);
47 
48  av_log(ctx, AV_LOG_WARNING, "This filter is deprecated, use aformat instead\n");
49 
51  aconvert->out_chlayout = 0;
52 
53  if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) {
54  if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
55  goto end;
56  }
57  if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
58  if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
59  goto end;
60  }
61 
62 end:
63  av_freep(&args);
64  return ret;
65 }
66 
67 static av_cold void uninit(AVFilterContext *ctx)
68 {
69  AConvertContext *aconvert = ctx->priv;
70  swr_free(&aconvert->swr);
71 }
72 
74 {
75  AVFilterFormats *formats = NULL;
76  AConvertContext *aconvert = ctx->priv;
77  AVFilterLink *inlink = ctx->inputs[0];
78  AVFilterLink *outlink = ctx->outputs[0];
80 
82  &inlink->out_formats);
83  if (aconvert->out_sample_fmt != AV_SAMPLE_FMT_NONE) {
84  formats = NULL;
85  ff_add_format(&formats, aconvert->out_sample_fmt);
86  ff_formats_ref(formats, &outlink->in_formats);
87  } else
89  &outlink->in_formats);
90 
92  &inlink->out_channel_layouts);
93  if (aconvert->out_chlayout != 0) {
94  layouts = NULL;
95  ff_add_channel_layout(&layouts, aconvert->out_chlayout);
96  ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
97  } else
99  &outlink->in_channel_layouts);
100 
101  return 0;
102 }
103 
104 static int config_output(AVFilterLink *outlink)
105 {
106  int ret;
107  AVFilterContext *ctx = outlink->src;
108  AVFilterLink *inlink = ctx->inputs[0];
109  AConvertContext *aconvert = ctx->priv;
110  char buf1[64], buf2[64];
111 
112  /* if not specified in args, use the format and layout of the output */
113  if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE)
114  aconvert->out_sample_fmt = outlink->format;
115  if (aconvert->out_chlayout == 0)
116  aconvert->out_chlayout = outlink->channel_layout;
117 
118  aconvert->swr = swr_alloc_set_opts(aconvert->swr,
119  aconvert->out_chlayout, aconvert->out_sample_fmt, inlink->sample_rate,
120  inlink->channel_layout, inlink->format, inlink->sample_rate,
121  0, ctx);
122  if (!aconvert->swr)
123  return AVERROR(ENOMEM);
124  ret = swr_init(aconvert->swr);
125  if (ret < 0)
126  return ret;
127 
128  av_get_channel_layout_string(buf1, sizeof(buf1),
129  -1, inlink ->channel_layout);
130  av_get_channel_layout_string(buf2, sizeof(buf2),
131  -1, outlink->channel_layout);
132  av_log(ctx, AV_LOG_VERBOSE,
133  "fmt:%s cl:%s -> fmt:%s cl:%s\n",
134  av_get_sample_fmt_name(inlink ->format), buf1,
135  av_get_sample_fmt_name(outlink->format), buf2);
136 
137  return 0;
138 }
139 
140 static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
141 {
142  AConvertContext *aconvert = inlink->dst->priv;
143  const int n = insamplesref->nb_samples;
144  AVFilterLink *const outlink = inlink->dst->outputs[0];
145  AVFrame *outsamplesref = ff_get_audio_buffer(outlink, n);
146  int ret;
147 
148  if (!outsamplesref)
149  return AVERROR(ENOMEM);
150  swr_convert(aconvert->swr, outsamplesref->extended_data, n,
151  (void *)insamplesref->extended_data, n);
152 
153  av_frame_copy_props(outsamplesref, insamplesref);
154  av_frame_set_channels(outsamplesref, outlink->channels);
155  outsamplesref->channel_layout = outlink->channel_layout;
156 
157  ret = ff_filter_frame(outlink, outsamplesref);
158  av_frame_free(&insamplesref);
159  return ret;
160 }
161 
162 static const AVFilterPad aconvert_inputs[] = {
163  {
164  .name = "default",
165  .type = AVMEDIA_TYPE_AUDIO,
166  .filter_frame = filter_frame,
167  },
168  { NULL }
169 };
170 
171 static const AVFilterPad aconvert_outputs[] = {
172  {
173  .name = "default",
174  .type = AVMEDIA_TYPE_AUDIO,
175  .config_props = config_output,
176  },
177  { NULL }
178 };
179 
181  .name = "aconvert",
182  .description = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout."),
183  .priv_size = sizeof(AConvertContext),
184  .init = init,
185  .uninit = uninit,
187  .inputs = aconvert_inputs,
188  .outputs = aconvert_outputs,
189 };