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  int nb_samples)
28 {
29  return ff_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
30 }
31 
33  int nb_samples)
34 {
35  AVFilterBufferRef *samplesref = NULL;
36  uint8_t **data;
37  int planar = av_sample_fmt_is_planar(link->format);
39  int planes = planar ? nb_channels : 1;
40  int linesize;
41 
42  if (!(data = av_mallocz(sizeof(*data) * planes)))
43  goto fail;
44 
45  if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, link->format, 0) < 0)
46  goto fail;
47 
48  samplesref = avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
49  nb_samples, link->format,
50  link->channel_layout);
51  if (!samplesref)
52  goto fail;
53 
54  av_freep(&data);
55 
56 fail:
57  if (data)
58  av_freep(&data[0]);
59  av_freep(&data);
60  return samplesref;
61 }
62 
64  int nb_samples)
65 {
66  AVFilterBufferRef *ret = NULL;
67 
68  if (link->dstpad->get_audio_buffer)
69  ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
70 
71  if (!ret)
72  ret = ff_default_get_audio_buffer(link, perms, nb_samples);
73 
74  if (ret)
75  ret->type = AVMEDIA_TYPE_AUDIO;
76 
77  return ret;
78 }
79 
81  int linesize,int perms,
82  int nb_samples,
84  uint64_t channel_layout)
85 {
86  int planes;
87  AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
88  AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
89 
90  if (!samples || !samplesref)
91  goto fail;
92 
93  samplesref->buf = samples;
95  if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
96  goto fail;
97 
98  samplesref->audio->nb_samples = nb_samples;
99  samplesref->audio->channel_layout = channel_layout;
100  samplesref->audio->planar = av_sample_fmt_is_planar(sample_fmt);
101 
102  planes = samplesref->audio->planar ? av_get_channel_layout_nb_channels(channel_layout) : 1;
103 
104  /* make sure the buffer gets read permission or it's useless for output */
105  samplesref->perms = perms | AV_PERM_READ;
106 
107  samples->refcount = 1;
108  samplesref->type = AVMEDIA_TYPE_AUDIO;
109  samplesref->format = sample_fmt;
110 
111  memcpy(samples->data, data,
112  FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
113  memcpy(samplesref->data, samples->data, sizeof(samples->data));
114 
115  samples->linesize[0] = samplesref->linesize[0] = linesize;
116 
117  if (planes > FF_ARRAY_ELEMS(samples->data)) {
118  samples-> extended_data = av_mallocz(sizeof(*samples->extended_data) *
119  planes);
120  samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
121  planes);
122 
123  if (!samples->extended_data || !samplesref->extended_data)
124  goto fail;
125 
126  memcpy(samples-> extended_data, data, sizeof(*data)*planes);
127  memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
128  } else {
129  samples->extended_data = samples->data;
130  samplesref->extended_data = samplesref->data;
131  }
132 
133  samplesref->pts = AV_NOPTS_VALUE;
134 
135  return samplesref;
136 
137 fail:
138  if (samples && samples->extended_data != samples->data)
139  av_freep(&samples->extended_data);
140  if (samplesref) {
141  av_freep(&samplesref->audio);
142  if (samplesref->extended_data != samplesref->data)
143  av_freep(&samplesref->extended_data);
144  }
145  av_freep(&samplesref);
146  av_freep(&samples);
147  return NULL;
148 }
uint8_t * data[8]
buffer data for each plane/channel
Definition: avfilter.h:63
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: avfilter.h:156
AVFilterBufferRef * ff_get_audio_buffer(AVFilterLink *link, int perms, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:63
static int16_t * samples
int nb_samples
number of audio samples
Definition: avfilter.h:111
AVFilterBufferRefAudioProps * audio
audio buffer specific properties
Definition: avfilter.h:160
int linesize[8]
number of bytes per line
Definition: avfilter.h:157
enum AVMediaType type
media type of buffer data
Definition: avfilter.h:174
A reference-counted buffer data type used by the filter system.
Definition: avfilter.h:62
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:151
#define AV_PERM_READ
can read from the buffer
Definition: avfilter.h:97
uint8_t
int linesize[8]
number of bytes per line
Definition: avfilter.h:80
const char data[16]
Definition: mxf.c:66
int64_t pts
presentation timestamp.
Definition: avfilter.h:167
AVFilterBuffer * buf
the buffer that this is a reference to
Definition: avfilter.h:140
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
default handler for freeing audio/video buffer when there are no references left
Definition: buffer.c:27
audio channel layout utility functions
unsigned refcount
number of references to this buffer
Definition: avfilter.h:94
void(* free)(struct AVFilterBuffer *buf)
A pointer to the function to deallocate this buffer if the default function is not sufficient...
Definition: avfilter.h:90
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
sample_fmt
Definition: avconv_filter.c:63
AVFilterBufferRef *(* get_audio_buffer)(AVFilterLink *link, int perms, int nb_samples)
Callback function to get an audio buffer.
Definition: internal.h:80
int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Allocate a samples buffer for nb_samples samples, and fill data pointers and linesize accordingly...
Definition: samplefmt.c:160
int perms
permissions, see the AV_PERM_* flags
Definition: avfilter.h:172
AVFilterBufferRef * ff_default_get_audio_buffer(AVFilterLink *link, int perms, int nb_samples)
default handler for get_audio_buffer() for audio inputs
Definition: audio.c:32
out nb_samples
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:101
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: avfilter.h:79
uint64_t channel_layout
channel layout of audio buffer
Definition: avfilter.h:110
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
int planar
audio buffer - planar or packed
Definition: avfilter.h:113
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)
Create an audio buffer reference wrapped around an already allocated samples buffer.
Definition: audio.c:80
common internal and external API header
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:141
int format
media format
Definition: avfilter.h:170
AVFilterBufferRef * ff_null_get_audio_buffer(AVFilterLink *link, int perms, int nb_samples)
get_audio_buffer() handler for filters which simply pass audio along
Definition: audio.c:26
int nb_channels
internal API functions
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:158