Libav
audio.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
20 #include "libavutil/common.h"
21 
22 #include "audio.h"
23 #include "avfilter.h"
24 #include "internal.h"
25 
27 {
28  return ff_get_audio_buffer(link->dst->outputs[0], nb_samples);
29 }
30 
32 {
33  AVFrame *frame = av_frame_alloc();
35  int ret;
36 
37  if (!frame)
38  return NULL;
39 
40  frame->nb_samples = nb_samples;
41  frame->format = link->format;
42  frame->channel_layout = link->channel_layout;
43  frame->sample_rate = link->sample_rate;
44  ret = av_frame_get_buffer(frame, 0);
45  if (ret < 0) {
46  av_frame_free(&frame);
47  return NULL;
48  }
49 
50  av_samples_set_silence(frame->extended_data, 0, nb_samples, channels,
51  link->format);
52 
53 
54  return frame;
55 }
56 
57 AVFrame *ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
58 {
59  AVFrame *ret = NULL;
60 
61  if (link->dstpad->get_audio_buffer)
62  ret = link->dstpad->get_audio_buffer(link, nb_samples);
63 
64  if (!ret)
65  ret = ff_default_get_audio_buffer(link, nb_samples);
66 
67  return ret;
68 }
69 
70 #if FF_API_AVFILTERBUFFER
72  int linesize,int perms,
73  int nb_samples,
75  uint64_t channel_layout)
76 {
77  int planes;
78  AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
79  AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
80 
81  if (!samples || !samplesref)
82  goto fail;
83 
84  samplesref->buf = samples;
85  samplesref->buf->free = ff_avfilter_default_free_buffer;
86  if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
87  goto fail;
88 
89  samplesref->audio->nb_samples = nb_samples;
90  samplesref->audio->channel_layout = channel_layout;
91  samplesref->audio->planar = av_sample_fmt_is_planar(sample_fmt);
92 
93  planes = samplesref->audio->planar ? av_get_channel_layout_nb_channels(channel_layout) : 1;
94 
95  /* make sure the buffer gets read permission or it's useless for output */
96  samplesref->perms = perms | AV_PERM_READ;
97 
98  samples->refcount = 1;
99  samplesref->type = AVMEDIA_TYPE_AUDIO;
100  samplesref->format = sample_fmt;
101 
102  memcpy(samples->data, data,
103  FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
104  memcpy(samplesref->data, samples->data, sizeof(samples->data));
105 
106  samples->linesize[0] = samplesref->linesize[0] = linesize;
107 
108  if (planes > FF_ARRAY_ELEMS(samples->data)) {
109  samples-> extended_data = av_mallocz(sizeof(*samples->extended_data) *
110  planes);
111  samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
112  planes);
113 
114  if (!samples->extended_data || !samplesref->extended_data)
115  goto fail;
116 
117  memcpy(samples-> extended_data, data, sizeof(*data)*planes);
118  memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
119  } else {
120  samples->extended_data = samples->data;
121  samplesref->extended_data = samplesref->data;
122  }
123 
124  samplesref->pts = AV_NOPTS_VALUE;
125 
126  return samplesref;
127 
128 fail:
129  if (samples && samples->extended_data != samples->data)
130  av_freep(&samples->extended_data);
131  if (samplesref) {
132  av_freep(&samplesref->audio);
133  if (samplesref->extended_data != samplesref->data)
134  av_freep(&samplesref->extended_data);
135  }
136  av_freep(&samplesref);
137  av_freep(&samples);
138  return NULL;
139 }
140 #endif
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
Main libavfilter public API header.
#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
Definition: buffer.c:30
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
const char data[16]
Definition: mxf.c:70
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:101
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:206
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:57
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:381
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:57
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:26
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:186
NULL
Definition: eval.c:55
sample_fmt
Definition: avconv_filter.c:68
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:61
int sample_rate
Sample rate of the audio data.
Definition: frame.h:376
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:578
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:175
AVFilterBufferRef * avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data, int linesize, int perms, int nb_samples, enum AVSampleFormat sample_fmt, uint64_t channel_layout)
Definition: audio.c:71
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:31
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:169
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179
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
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228