h264: on reference overflow, reset the reference count to 0, not 1.
[FFMpeg-mirror/mplayer-patches.git] / libavfilter / audio.c
blobbbe12b211be5d11889eca93cea17257ff954d367
1 /*
2 * This file is part of Libav.
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.
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.
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
19 #include "libavutil/channel_layout.h"
20 #include "libavutil/common.h"
22 #include "audio.h"
23 #include "avfilter.h"
24 #include "internal.h"
26 AVFilterBufferRef *ff_null_get_audio_buffer(AVFilterLink *link, int perms,
27 int nb_samples)
29 return ff_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
32 AVFilterBufferRef *ff_default_get_audio_buffer(AVFilterLink *link, int perms,
33 int nb_samples)
35 AVFilterBufferRef *samplesref = NULL;
36 uint8_t **data;
37 int planar = av_sample_fmt_is_planar(link->format);
38 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
39 int planes = planar ? nb_channels : 1;
40 int linesize;
42 if (!(data = av_mallocz(sizeof(*data) * planes)))
43 goto fail;
45 if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, link->format, 0) < 0)
46 goto fail;
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;
54 av_freep(&data);
56 fail:
57 if (data)
58 av_freep(&data[0]);
59 av_freep(&data);
60 return samplesref;
63 AVFilterBufferRef *ff_get_audio_buffer(AVFilterLink *link, int perms,
64 int nb_samples)
66 AVFilterBufferRef *ret = NULL;
68 if (link->dstpad->get_audio_buffer)
69 ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
71 if (!ret)
72 ret = ff_default_get_audio_buffer(link, perms, nb_samples);
74 if (ret)
75 ret->type = AVMEDIA_TYPE_AUDIO;
77 return ret;
80 AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
81 int linesize,int perms,
82 int nb_samples,
83 enum AVSampleFormat sample_fmt,
84 uint64_t channel_layout)
86 int planes;
87 AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
88 AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
90 if (!samples || !samplesref)
91 goto fail;
93 samplesref->buf = samples;
94 samplesref->buf->free = ff_avfilter_default_free_buffer;
95 if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
96 goto fail;
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);
102 planes = samplesref->audio->planar ? av_get_channel_layout_nb_channels(channel_layout) : 1;
104 /* make sure the buffer gets read permission or it's useless for output */
105 samplesref->perms = perms | AV_PERM_READ;
107 samples->refcount = 1;
108 samplesref->type = AVMEDIA_TYPE_AUDIO;
109 samplesref->format = sample_fmt;
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));
115 samples->linesize[0] = samplesref->linesize[0] = linesize;
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);
123 if (!samples->extended_data || !samplesref->extended_data)
124 goto fail;
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;
133 samplesref->pts = AV_NOPTS_VALUE;
135 return samplesref;
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);
145 av_freep(&samplesref);
146 av_freep(&samples);
147 return NULL;