FFmpeg
audio.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) Stefano Sabatini | stefasab at gmail.com
3  * Copyright (c) S.N. Hemanth Meenakshisundaram | smeenaks at ucsd.edu
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/avassert.h"
24 #include "libavutil/common.h"
25 #include "libavutil/cpu.h"
26 #include "libavutil/eval.h"
27 
28 #include "audio.h"
29 #include "avfilter.h"
30 #include "avfilter_internal.h"
31 #include "filters.h"
32 #include "framepool.h"
33 
35  {
36  .name = "default",
37  .type = AVMEDIA_TYPE_AUDIO,
38  }
39 };
40 
42 {
43  return ff_get_audio_buffer(link->dst->outputs[0], nb_samples);
44 }
45 
47 {
48  AVFrame *frame = NULL;
51  int align = av_cpu_max_align();
52 
53  if (!li->frame_pool) {
55  nb_samples, link->format, align);
56  if (!li->frame_pool)
57  return NULL;
58  } else {
59  int pool_channels = 0;
60  int pool_nb_samples = 0;
61  int pool_align = 0;
62  enum AVSampleFormat pool_format = AV_SAMPLE_FMT_NONE;
63 
65  &pool_channels, &pool_nb_samples,
66  &pool_format, &pool_align) < 0) {
67  return NULL;
68  }
69 
70  if (pool_channels != channels || pool_nb_samples < nb_samples ||
71  pool_format != link->format || pool_align != align) {
72 
75  nb_samples, link->format, align);
76  if (!li->frame_pool)
77  return NULL;
78  }
79  }
80 
82  if (!frame)
83  return NULL;
84 
85  frame->nb_samples = nb_samples;
87  av_channel_layout_copy(&frame->ch_layout, &link->ch_layout) < 0) {
89  return NULL;
90  }
91  frame->sample_rate = link->sample_rate;
92 
93  av_samples_set_silence(frame->extended_data, 0, nb_samples, channels, link->format);
94 
95  return frame;
96 }
97 
99 {
100  AVFrame *ret = NULL;
101 
102  if (link->dstpad->get_buffer.audio)
103  ret = link->dstpad->get_buffer.audio(link, nb_samples);
104 
105  if (!ret)
106  ret = ff_default_get_audio_buffer(link, nb_samples);
107 
108  return ret;
109 }
110 
111 int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
112 {
113  char *tail;
114  double srate = av_strtod(arg, &tail);
115  if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
116  av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
117  return AVERROR(EINVAL);
118  }
119  *ret = srate;
120  return 0;
121 }
122 
123 int ff_parse_channel_layout(AVChannelLayout *ret, int *nret, const char *arg,
124  void *log_ctx)
125 {
126  AVChannelLayout chlayout = { 0 };
127  int res;
128 
129  res = av_channel_layout_from_string(&chlayout, arg);
130  if (res < 0) {
131  av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
132  return AVERROR(EINVAL);
133  }
134 
135  if (chlayout.order == AV_CHANNEL_ORDER_UNSPEC && !nret) {
136  av_log(log_ctx, AV_LOG_ERROR, "Unknown channel layout '%s' is not supported.\n", arg);
137  return AVERROR(EINVAL);
138  }
139  *ret = chlayout;
140  if (nret)
141  *nret = chlayout.nb_channels;
142 
143  return 0;
144 }
ff_get_audio_buffer
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:98
ff_link_internal
static FilterLinkInternal * ff_link_internal(AVFilterLink *link)
Definition: avfilter_internal.h:90
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
av_samples_set_silence
int av_samples_set_silence(uint8_t *const *audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition: samplefmt.c:246
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:316
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
FilterLinkInternal
Definition: avfilter_internal.h:34
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:790
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:116
filters.h
ff_null_get_audio_buffer
AVFrame * ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples)
get_audio_buffer() handler for filters which simply pass audio along
Definition: audio.c:41
channels
channels
Definition: aptx.h:31
ff_frame_pool_get
AVFrame * ff_frame_pool_get(FFFramePool *pool)
Allocate a new AVFrame, reussing old buffers from the pool when available.
Definition: framepool.c:191
link
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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 link
Definition: filter_design.txt:23
ff_parse_channel_layout
int ff_parse_channel_layout(AVChannelLayout *ret, int *nret, const char *arg, void *log_ctx)
Parse a channel layout or a corresponding integer representation.
Definition: audio.c:123
arg
const char * arg
Definition: jacosubdec.c:67
ff_parse_sample_rate
int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
Parse a sample rate.
Definition: audio.c:111
NULL
#define NULL
Definition: coverity.c:32
ff_audio_default_filterpad
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition: audio.c:34
av_cpu_max_align
size_t av_cpu_max_align(void)
Get the maximum data alignment that may be required by FFmpeg.
Definition: cpu.c:276
avfilter_internal.h
ff_frame_pool_uninit
void ff_frame_pool_uninit(FFFramePool **pool)
Deallocate the frame pool.
Definition: framepool.c:279
eval.h
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:311
cpu.h
AVFrame::sample_rate
int sample_rate
Sample rate of the audio data.
Definition: frame.h:588
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:476
align
static const uint8_t *BS_FUNC() align(BSCTX *bc)
Skip bits to a byte boundary.
Definition: bitstream_template.h:411
av_channel_layout_from_string
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
Initialize a channel layout from a given string description.
Definition: channel_layout.c:307
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
ff_frame_pool_audio_init
FFFramePool * ff_frame_pool_audio_init(AVBufferRef *(*alloc)(size_t size), int channels, int nb_samples, enum AVSampleFormat format, int align)
Allocate and initialize an audio frame pool.
Definition: framepool.c:115
av_buffer_allocz
AVBufferRef * av_buffer_allocz(size_t size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:93
ret
ret
Definition: filter_design.txt:187
av_strtod
double av_strtod(const char *numstr, char **tail)
Parse the string in numstr and return its value as a double.
Definition: eval.c:107
frame
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 the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
framepool.h
channel_layout.h
avfilter.h
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:444
audio.h
ff_default_get_audio_buffer
AVFrame * ff_default_get_audio_buffer(AVFilterLink *link, int nb_samples)
default handler for get_audio_buffer() for audio inputs
Definition: audio.c:46
FilterLinkInternal::frame_pool
struct FFFramePool * frame_pool
Definition: avfilter_internal.h:37
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_frame_pool_get_audio_config
int ff_frame_pool_get_audio_config(FFFramePool *pool, int *channels, int *nb_samples, enum AVSampleFormat *format, int *align)
Get the audio frame pool configuration.
Definition: framepool.c:172