FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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