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 
26 #include "audio.h"
27 #include "avfilter.h"
28 #include "framepool.h"
29 #include "internal.h"
30 
31 #define BUFFER_ALIGN 0
32 
33 
35 {
36  return ff_get_audio_buffer(link->dst->outputs[0], nb_samples);
37 }
38 
40 {
41  AVFrame *frame = NULL;
42  int channels = link->channels;
43  int channel_layout_nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
44 
45  av_assert0(channels == channel_layout_nb_channels || !channel_layout_nb_channels);
46 
47  if (!link->frame_pool) {
49  nb_samples, link->format, BUFFER_ALIGN);
50  if (!link->frame_pool)
51  return NULL;
52  } else {
53  int pool_channels = 0;
54  int pool_nb_samples = 0;
55  int pool_align = 0;
56  enum AVSampleFormat pool_format = AV_SAMPLE_FMT_NONE;
57 
58  if (ff_frame_pool_get_audio_config(link->frame_pool,
59  &pool_channels, &pool_nb_samples,
60  &pool_format, &pool_align) < 0) {
61  return NULL;
62  }
63 
64  if (pool_channels != channels || pool_nb_samples < nb_samples ||
65  pool_format != link->format || pool_align != BUFFER_ALIGN) {
66 
67  ff_frame_pool_uninit((FFFramePool **)&link->frame_pool);
69  nb_samples, link->format, BUFFER_ALIGN);
70  if (!link->frame_pool)
71  return NULL;
72  }
73  }
74 
75  frame = ff_frame_pool_get(link->frame_pool);
76  if (!frame)
77  return NULL;
78 
79  frame->nb_samples = nb_samples;
80  frame->channel_layout = link->channel_layout;
81  frame->sample_rate = link->sample_rate;
82 
83  av_samples_set_silence(frame->extended_data, 0, nb_samples, channels, link->format);
84 
85  return frame;
86 }
87 
89 {
90  AVFrame *ret = NULL;
91 
92  if (link->dstpad->get_buffer.audio)
93  ret = link->dstpad->get_buffer.audio(link, nb_samples);
94 
95  if (!ret)
96  ret = ff_default_get_audio_buffer(link, nb_samples);
97 
98  return ret;
99 }
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:88
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
BUFFER_ALIGN
#define BUFFER_ALIGN
Definition: audio.c:31
FFFramePool
Frame pool.
Definition: framepool.c:30
avassert.h
AVFrame::channels
int channels
number of audio channels, only used for audio.
Definition: frame.h:628
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
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:34
channels
channels
Definition: aptx.h:33
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:194
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
NULL
#define NULL
Definition: coverity.c:32
av_get_channel_layout_nb_channels
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
Definition: channel_layout.c:226
ff_frame_pool_uninit
void ff_frame_pool_uninit(FFFramePool **pool)
Deallocate the frame pool.
Definition: framepool.c:282
AVFrame::sample_rate
int sample_rate
Sample rate of the audio data.
Definition: frame.h:494
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:404
AVFrame::channel_layout
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:499
internal.h
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
av_samples_set_silence
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition: samplefmt.c:244
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:118
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
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
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:39
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:175