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 }