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 "libavcodec/avcodec.h"
26 
27 #include "audio.h"
28 #include "avfilter.h"
29 #include "internal.h"
30 
31 int avfilter_ref_get_channels(AVFilterBufferRef *ref)
32 {
33  return ref->audio ? ref->audio->channels : 0;
34 }
35 
37 {
38  return ff_get_audio_buffer(link->dst->outputs[0], nb_samples);
39 }
40 
42 {
44  int channels = link->channels;
45  int buf_size, ret;
46 
48 
49  if (!frame)
50  return NULL;
51 
52  buf_size = av_samples_get_buffer_size(NULL, channels, nb_samples,
53  link->format, 0);
54  if (buf_size < 0)
55  goto fail;
56 
57  frame->buf[0] = av_buffer_alloc(buf_size);
58  if (!frame->buf[0])
59  goto fail;
60 
61  frame->nb_samples = nb_samples;
62  ret = avcodec_fill_audio_frame(frame, channels, link->format,
63  frame->buf[0]->data, buf_size, 0);
64  if (ret < 0)
65  goto fail;
66 
67  av_samples_set_silence(frame->extended_data, 0, nb_samples, channels,
68  link->format);
69 
70  frame->nb_samples = nb_samples;
71  frame->format = link->format;
72  av_frame_set_channels(frame, link->channels);
73  frame->channel_layout = link->channel_layout;
74  frame->sample_rate = link->sample_rate;
75 
76  return frame;
77 
78 fail:
79  av_buffer_unref(&frame->buf[0]);
80  av_frame_free(&frame);
81  return NULL;
82 }
83 
85 {
86  AVFrame *ret = NULL;
87 
88  if (link->dstpad->get_audio_buffer)
89  ret = link->dstpad->get_audio_buffer(link, nb_samples);
90 
91  if (!ret)
92  ret = ff_default_get_audio_buffer(link, nb_samples);
93 
94  return ret;
95 }
96 
97 #if FF_API_AVFILTERBUFFER
98 AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays_channels(uint8_t **data,
99  int linesize,int perms,
100  int nb_samples,
101  enum AVSampleFormat sample_fmt,
102  int channels,
103  uint64_t channel_layout)
104 {
105  int planes;
106  AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
107  AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
108 
109  if (!samples || !samplesref)
110  goto fail;
111 
112  av_assert0(channels);
113  av_assert0(channel_layout == 0 ||
114  channels == av_get_channel_layout_nb_channels(channel_layout));
115 
116  samplesref->buf = samples;
117  samplesref->buf->free = ff_avfilter_default_free_buffer;
118  if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
119  goto fail;
120 
121  samplesref->audio->nb_samples = nb_samples;
122  samplesref->audio->channel_layout = channel_layout;
123  samplesref->audio->channels = channels;
124 
125  planes = av_sample_fmt_is_planar(sample_fmt) ? channels : 1;
126 
127  /* make sure the buffer gets read permission or it's useless for output */
128  samplesref->perms = perms | AV_PERM_READ;
129 
130  samples->refcount = 1;
131  samplesref->type = AVMEDIA_TYPE_AUDIO;
132  samplesref->format = sample_fmt;
133 
134  memcpy(samples->data, data,
135  FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
136  memcpy(samplesref->data, samples->data, sizeof(samples->data));
137 
138  samples->linesize[0] = samplesref->linesize[0] = linesize;
139 
140  if (planes > FF_ARRAY_ELEMS(samples->data)) {
141  samples-> extended_data = av_mallocz(sizeof(*samples->extended_data) *
142  planes);
143  samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
144  planes);
145 
146  if (!samples->extended_data || !samplesref->extended_data)
147  goto fail;
148 
149  memcpy(samples-> extended_data, data, sizeof(*data)*planes);
150  memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
151  } else {
152  samples->extended_data = samples->data;
153  samplesref->extended_data = samplesref->data;
154  }
155 
156  samplesref->pts = AV_NOPTS_VALUE;
157 
158  return samplesref;
159 
160 fail:
161  if (samples && samples->extended_data != samples->data)
162  av_freep(&samples->extended_data);
163  if (samplesref) {
164  av_freep(&samplesref->audio);
165  if (samplesref->extended_data != samplesref->data)
166  av_freep(&samplesref->extended_data);
167  }
168  av_freep(&samplesref);
169  av_freep(&samples);
170  return NULL;
171 }
172 
173 AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
174  int linesize,int perms,
175  int nb_samples,
176  enum AVSampleFormat sample_fmt,
177  uint64_t channel_layout)
178 {
179  int channels = av_get_channel_layout_nb_channels(channel_layout);
180  return avfilter_get_audio_buffer_ref_from_arrays_channels(data, linesize, perms,
181  nb_samples, sample_fmt,
182  channels, channel_layout);
183 }
184 #endif
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
FIXME Range Coding of cr are ref
Definition: snow.txt:367
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Get the required buffer size for the given audio parameters.
Definition: samplefmt.c:125
external API header
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:343
#define FF_ARRAY_ELEMS(a)
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
void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
default handler for freeing audio/video buffer when there are no references left
int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels, enum AVSampleFormat sample_fmt, const uint8_t *buf, int buf_size, int align)
Fill AVFrame audio data and linesize pointers.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
frame
Definition: stft.m:14
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
Spectrum Plot time data
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
void av_frame_set_channels(AVFrame *frame, int val)
simple assert() macros that are a bit more flexible than ISO C assert().
external API header
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 link
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:331
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:58
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:249
ret
Definition: avfilter.c:821
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:36
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:134
NULL
Definition: eval.c:55
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
uint8_t * data
The data buffer.
Definition: buffer.h:89
AVFrame *(* get_audio_buffer)(AVFilterLink *link, int nb_samples)
Callback function to get an audio buffer.
int sample_rate
Sample rate of the audio data.
Definition: frame.h:326
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:118
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:95
int avfilter_ref_get_channels(AVFilterBufferRef *ref)
Get the number of channels of a buffer reference.
Definition: audio.c:31
common internal and external API header
AVFrame * ff_default_get_audio_buffer(AVFilterLink *link, int nb_samples)
default handler for get_audio_buffer() for audio inputs
Definition: audio.c:41
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
Filter the word “frame” indicates either a video frame or a group of audio samples
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190