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 {
36  enum AVSampleFormat out_sample_fmt;
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 {
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 };
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:424
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count, const uint8_t *in_arg[SWR_CH_MAX], int in_count)
Definition: swresample.c:735
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
static av_cold int init(AVFilterContext *ctx)
Definition: af_aconvert.c:41
static const AVFilterPad aconvert_inputs[]
Definition: af_aconvert.c:162
static int query_formats(AVFilterContext *ctx)
Definition: af_aconvert.c:73
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
end end
void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:427
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:357
A filter pad used for either input or output.
libswresample public header
enum AVSampleFormat out_sample_fmt
Definition: af_aconvert.c:36
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:350
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:84
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
void av_frame_set_channels(AVFrame *frame, int val)
const char * arg
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:344
int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
Parse a sample format name or a corresponding integer representation.
Definition: formats.c:594
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:331
#define AV_LOG_VERBOSE
Definition: log.h:157
audio channel layout utility functions
ret
Definition: avfilter.c:821
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate.The lists are not just lists
struct SwrContext * swr_alloc_set_opts(struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx)
Allocate SwrContext if needed and set/reset common parameters.
Definition: swresample.c:186
int64_t out_chlayout
Definition: af_aconvert.c:37
struct SwrContext * swr
Definition: af_aconvert.c:38
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:47
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (...
Definition: formats.c:402
A list of supported channel layouts.
Definition: formats.h:85
NULL
Definition: eval.c:55
void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:432
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:220
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
Definition: swresample.c:220
Filter definition.
Definition: avfilter.h:436
const char * name
filter name
Definition: avfilter.h:437
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_aconvert.c:67
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
Definition: avstring.c:183
AVFilter avfilter_af_aconvert
Definition: af_aconvert.c:180
static int config_output(AVFilterLink *outlink)
Definition: af_aconvert.c:104
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common formats
Definition: swscale.txt:33
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
Parse a channel layout or a corresponding integer representation.
Definition: formats.c:632
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
Definition: af_aconvert.c:140
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:524
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
static const AVFilterPad aconvert_outputs[]
Definition: af_aconvert.c:171
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition: swresample.c:242
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.