]> git.sesse.net Git - ffmpeg/blob - libavfilter/audio.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / audio.c
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"
23 #include "libavutil/audioconvert.h"
24
25 #include "audio.h"
26 #include "avfilter.h"
27 #include "internal.h"
28
29 AVFilterBufferRef *ff_null_get_audio_buffer(AVFilterLink *link, int perms,
30                                             int nb_samples)
31 {
32     return ff_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
33 }
34
35 AVFilterBufferRef *ff_default_get_audio_buffer(AVFilterLink *link, int perms,
36                                                int nb_samples)
37 {
38     AVFilterBufferRef *samplesref = NULL;
39     uint8_t **data;
40     int planar      = av_sample_fmt_is_planar(link->format);
41     int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
42     int planes      = planar ? nb_channels : 1;
43     int linesize;
44
45     if (!(data = av_mallocz(sizeof(*data) * planes)))
46         goto fail;
47
48     if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, link->format, 0) < 0)
49         goto fail;
50
51     samplesref = avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
52                                                            nb_samples, link->format,
53                                                            link->channel_layout);
54     if (!samplesref)
55         goto fail;
56
57     av_freep(&data);
58
59 fail:
60     if (data)
61         av_freep(&data[0]);
62     av_freep(&data);
63     return samplesref;
64 }
65
66 AVFilterBufferRef *ff_get_audio_buffer(AVFilterLink *link, int perms,
67                                        int nb_samples)
68 {
69     AVFilterBufferRef *ret = NULL;
70
71     if (link->dstpad->get_audio_buffer)
72         ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
73
74     if (!ret)
75         ret = ff_default_get_audio_buffer(link, perms, nb_samples);
76
77     if (ret)
78         ret->type = AVMEDIA_TYPE_AUDIO;
79
80     return ret;
81 }
82
83 AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
84                                                              int linesize,int perms,
85                                                              int nb_samples,
86                                                              enum AVSampleFormat sample_fmt,
87                                                              uint64_t channel_layout)
88 {
89     int planes;
90     AVFilterBuffer    *samples    = av_mallocz(sizeof(*samples));
91     AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
92
93     if (!samples || !samplesref)
94         goto fail;
95
96     samplesref->buf         = samples;
97     samplesref->buf->free   = ff_avfilter_default_free_buffer;
98     if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
99         goto fail;
100
101     samplesref->audio->nb_samples     = nb_samples;
102     samplesref->audio->channel_layout = channel_layout;
103
104     planes = av_sample_fmt_is_planar(sample_fmt) ?
105         av_get_channel_layout_nb_channels(channel_layout) : 1;
106
107     /* make sure the buffer gets read permission or it's useless for output */
108     samplesref->perms = perms | AV_PERM_READ;
109
110     samples->refcount  = 1;
111     samplesref->type   = AVMEDIA_TYPE_AUDIO;
112     samplesref->format = sample_fmt;
113
114     memcpy(samples->data, data,
115            FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
116     memcpy(samplesref->data, samples->data, sizeof(samples->data));
117
118     samples->linesize[0] = samplesref->linesize[0] = linesize;
119
120     if (planes > FF_ARRAY_ELEMS(samples->data)) {
121         samples->   extended_data = av_mallocz(sizeof(*samples->extended_data) *
122                                                planes);
123         samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
124                                                planes);
125
126         if (!samples->extended_data || !samplesref->extended_data)
127             goto fail;
128
129         memcpy(samples->   extended_data, data, sizeof(*data)*planes);
130         memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
131     } else {
132         samples->extended_data    = samples->data;
133         samplesref->extended_data = samplesref->data;
134     }
135
136     samplesref->pts = AV_NOPTS_VALUE;
137
138     return samplesref;
139
140 fail:
141     if (samples && samples->extended_data != samples->data)
142         av_freep(&samples->extended_data);
143     if (samplesref) {
144         av_freep(&samplesref->audio);
145         if (samplesref->extended_data != samplesref->data)
146             av_freep(&samplesref->extended_data);
147     }
148     av_freep(&samplesref);
149     av_freep(&samples);
150     return NULL;
151 }
152
153 void ff_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
154 {
155     ff_filter_samples(link->dst->outputs[0], samplesref);
156 }
157
158 /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
159 void ff_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
160 {
161     AVFilterLink *outlink = NULL;
162
163     if (inlink->dst->nb_outputs)
164         outlink = inlink->dst->outputs[0];
165
166     if (outlink) {
167         outlink->out_buf = ff_default_get_audio_buffer(inlink, AV_PERM_WRITE,
168                                                        samplesref->audio->nb_samples);
169         outlink->out_buf->pts                = samplesref->pts;
170         outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
171         ff_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
172         avfilter_unref_buffer(outlink->out_buf);
173         outlink->out_buf = NULL;
174     }
175     avfilter_unref_buffer(samplesref);
176     inlink->cur_buf = NULL;
177 }
178
179 void ff_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
180 {
181     void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
182     AVFilterPad *dst = link->dstpad;
183     int64_t pts;
184
185     FF_TPRINTF_START(NULL, filter_samples); ff_tlog_link(NULL, link, 1);
186
187     if (!(filter_samples = dst->filter_samples))
188         filter_samples = ff_default_filter_samples;
189
190     /* prepare to copy the samples if the buffer has insufficient permissions */
191     if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
192         dst->rej_perms & samplesref->perms) {
193         int size;
194         av_log(link->dst, AV_LOG_DEBUG,
195                "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
196                samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
197
198         link->cur_buf = ff_default_get_audio_buffer(link, dst->min_perms,
199                                                     samplesref->audio->nb_samples);
200         link->cur_buf->pts                = samplesref->pts;
201         link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
202
203         /* Copy actual data into new samples buffer */
204         av_samples_copy(link->cur_buf->extended_data, samplesref->extended_data,
205                         0, 0, samplesref->audio->nb_samples,
206                         av_get_channel_layout_nb_channels(link->channel_layout),
207                         link->format);
208
209         avfilter_unref_buffer(samplesref);
210     } else
211         link->cur_buf = samplesref;
212
213     pts = link->cur_buf->pts;
214     filter_samples(link, link->cur_buf);
215     ff_update_link_current_pts(link, pts);
216 }