00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avassert.h"
00023 #include "libavutil/audioconvert.h"
00024 #include "libavutil/common.h"
00025
00026 #include "audio.h"
00027 #include "avfilter.h"
00028 #include "internal.h"
00029
00030 AVFilterBufferRef *ff_null_get_audio_buffer(AVFilterLink *link, int perms,
00031 int nb_samples)
00032 {
00033 return ff_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
00034 }
00035
00036 AVFilterBufferRef *ff_default_get_audio_buffer(AVFilterLink *link, int perms,
00037 int nb_samples)
00038 {
00039 AVFilterBufferRef *samplesref = NULL;
00040 uint8_t **data;
00041 int planar = av_sample_fmt_is_planar(link->format);
00042 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
00043 int planes = planar ? nb_channels : 1;
00044 int linesize;
00045 int full_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE |
00046 AV_PERM_REUSE | AV_PERM_REUSE2 | AV_PERM_ALIGN;
00047
00048 av_assert1(!(perms & ~(full_perms | AV_PERM_NEG_LINESIZES)));
00049
00050 if (!(data = av_mallocz(sizeof(*data) * planes)))
00051 goto fail;
00052
00053 if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, link->format, 0) < 0)
00054 goto fail;
00055
00056 samplesref = avfilter_get_audio_buffer_ref_from_arrays(data, linesize, full_perms,
00057 nb_samples, link->format,
00058 link->channel_layout);
00059 if (!samplesref)
00060 goto fail;
00061
00062 samplesref->audio->sample_rate = link->sample_rate;
00063
00064 av_freep(&data);
00065
00066 fail:
00067 if (data)
00068 av_freep(&data[0]);
00069 av_freep(&data);
00070 return samplesref;
00071 }
00072
00073 AVFilterBufferRef *ff_get_audio_buffer(AVFilterLink *link, int perms,
00074 int nb_samples)
00075 {
00076 AVFilterBufferRef *ret = NULL;
00077
00078 if (link->dstpad->get_audio_buffer)
00079 ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
00080
00081 if (!ret)
00082 ret = ff_default_get_audio_buffer(link, perms, nb_samples);
00083
00084 if (ret)
00085 ret->type = AVMEDIA_TYPE_AUDIO;
00086
00087 return ret;
00088 }
00089
00090 AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
00091 int linesize,int perms,
00092 int nb_samples,
00093 enum AVSampleFormat sample_fmt,
00094 uint64_t channel_layout)
00095 {
00096 int planes;
00097 AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
00098 AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
00099
00100 if (!samples || !samplesref)
00101 goto fail;
00102
00103 samplesref->buf = samples;
00104 samplesref->buf->free = ff_avfilter_default_free_buffer;
00105 if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
00106 goto fail;
00107
00108 samplesref->audio->nb_samples = nb_samples;
00109 samplesref->audio->channel_layout = channel_layout;
00110
00111 planes = av_sample_fmt_is_planar(sample_fmt) ?
00112 av_get_channel_layout_nb_channels(channel_layout) : 1;
00113
00114
00115 samplesref->perms = perms | AV_PERM_READ;
00116
00117 samples->refcount = 1;
00118 samplesref->type = AVMEDIA_TYPE_AUDIO;
00119 samplesref->format = sample_fmt;
00120
00121 memcpy(samples->data, data,
00122 FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
00123 memcpy(samplesref->data, samples->data, sizeof(samples->data));
00124
00125 samples->linesize[0] = samplesref->linesize[0] = linesize;
00126
00127 if (planes > FF_ARRAY_ELEMS(samples->data)) {
00128 samples-> extended_data = av_mallocz(sizeof(*samples->extended_data) *
00129 planes);
00130 samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
00131 planes);
00132
00133 if (!samples->extended_data || !samplesref->extended_data)
00134 goto fail;
00135
00136 memcpy(samples-> extended_data, data, sizeof(*data)*planes);
00137 memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
00138 } else {
00139 samples->extended_data = samples->data;
00140 samplesref->extended_data = samplesref->data;
00141 }
00142
00143 samplesref->pts = AV_NOPTS_VALUE;
00144
00145 return samplesref;
00146
00147 fail:
00148 if (samples && samples->extended_data != samples->data)
00149 av_freep(&samples->extended_data);
00150 if (samplesref) {
00151 av_freep(&samplesref->audio);
00152 if (samplesref->extended_data != samplesref->data)
00153 av_freep(&samplesref->extended_data);
00154 }
00155 av_freep(&samplesref);
00156 av_freep(&samples);
00157 return NULL;
00158 }
00159
00160 static int default_filter_samples(AVFilterLink *link,
00161 AVFilterBufferRef *samplesref)
00162 {
00163 return ff_filter_samples(link->dst->outputs[0], samplesref);
00164 }
00165
00166 int ff_filter_samples_framed(AVFilterLink *link, AVFilterBufferRef *samplesref)
00167 {
00168 int (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
00169 AVFilterPad *src = link->srcpad;
00170 AVFilterPad *dst = link->dstpad;
00171 int64_t pts;
00172 AVFilterBufferRef *buf_out;
00173 int ret;
00174
00175 FF_TPRINTF_START(NULL, filter_samples); ff_tlog_link(NULL, link, 1);
00176
00177 if (link->closed) {
00178 avfilter_unref_buffer(samplesref);
00179 return AVERROR_EOF;
00180 }
00181
00182 if (!(filter_samples = dst->filter_samples))
00183 filter_samples = default_filter_samples;
00184
00185 av_assert1((samplesref->perms & src->min_perms) == src->min_perms);
00186 samplesref->perms &= ~ src->rej_perms;
00187
00188
00189 if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
00190 dst->rej_perms & samplesref->perms) {
00191 av_log(link->dst, AV_LOG_DEBUG,
00192 "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
00193 samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
00194
00195 buf_out = ff_default_get_audio_buffer(link, dst->min_perms,
00196 samplesref->audio->nb_samples);
00197 if (!buf_out) {
00198 avfilter_unref_buffer(samplesref);
00199 return AVERROR(ENOMEM);
00200 }
00201 buf_out->pts = samplesref->pts;
00202 buf_out->audio->sample_rate = samplesref->audio->sample_rate;
00203
00204
00205 av_samples_copy(buf_out->extended_data, samplesref->extended_data,
00206 0, 0, samplesref->audio->nb_samples,
00207 av_get_channel_layout_nb_channels(link->channel_layout),
00208 link->format);
00209
00210 avfilter_unref_buffer(samplesref);
00211 } else
00212 buf_out = samplesref;
00213
00214 link->cur_buf = buf_out;
00215 pts = buf_out->pts;
00216 ret = filter_samples(link, buf_out);
00217 ff_update_link_current_pts(link, pts);
00218 return ret;
00219 }
00220
00221 int ff_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
00222 {
00223 int insamples = samplesref->audio->nb_samples, inpos = 0, nb_samples;
00224 AVFilterBufferRef *pbuf = link->partial_buf;
00225 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
00226 int ret = 0;
00227
00228 av_assert1(samplesref->format == link->format);
00229 av_assert1(samplesref->audio->channel_layout == link->channel_layout);
00230 av_assert1(samplesref->audio->sample_rate == link->sample_rate);
00231
00232 if (!link->min_samples ||
00233 (!pbuf &&
00234 insamples >= link->min_samples && insamples <= link->max_samples)) {
00235 return ff_filter_samples_framed(link, samplesref);
00236 }
00237
00238 while (insamples) {
00239 if (!pbuf) {
00240 AVRational samples_tb = { 1, link->sample_rate };
00241 int perms = link->dstpad->min_perms | AV_PERM_WRITE;
00242 pbuf = ff_get_audio_buffer(link, perms, link->partial_buf_size);
00243 if (!pbuf) {
00244 av_log(link->dst, AV_LOG_WARNING,
00245 "Samples dropped due to memory allocation failure.\n");
00246 return 0;
00247 }
00248 avfilter_copy_buffer_ref_props(pbuf, samplesref);
00249 pbuf->pts = samplesref->pts +
00250 av_rescale_q(inpos, samples_tb, link->time_base);
00251 pbuf->audio->nb_samples = 0;
00252 }
00253 nb_samples = FFMIN(insamples,
00254 link->partial_buf_size - pbuf->audio->nb_samples);
00255 av_samples_copy(pbuf->extended_data, samplesref->extended_data,
00256 pbuf->audio->nb_samples, inpos,
00257 nb_samples, nb_channels, link->format);
00258 inpos += nb_samples;
00259 insamples -= nb_samples;
00260 pbuf->audio->nb_samples += nb_samples;
00261 if (pbuf->audio->nb_samples >= link->min_samples) {
00262 ret = ff_filter_samples_framed(link, pbuf);
00263 pbuf = NULL;
00264 }
00265 }
00266 avfilter_unref_buffer(samplesref);
00267 link->partial_buf = pbuf;
00268 return ret;
00269 }