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/audioconvert.h"
20 #include "libavcodec/avcodec.h"
25 /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
26 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
28 if (ptr->extended_data != ptr->data)
29 av_freep(&ptr->extended_data);
30 av_free(ptr->data[0]);
34 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
36 AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
40 if (ref->type == AVMEDIA_TYPE_VIDEO) {
41 ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
46 *ret->video = *ref->video;
47 ret->extended_data = ret->data;
48 } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
49 ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
54 *ret->audio = *ref->audio;
56 if (ref->extended_data != ref->data) {
57 int nb_channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
58 if (!(ret->extended_data = av_malloc(sizeof(*ret->extended_data) *
60 av_freep(&ret->audio);
64 memcpy(ret->extended_data, ref->extended_data,
65 sizeof(*ret->extended_data) * nb_channels);
67 ret->extended_data = ret->data;
70 ret->buf->refcount ++;
74 void avfilter_unref_buffer(AVFilterBufferRef *ref)
78 if (!(--ref->buf->refcount))
79 ref->buf->free(ref->buf);
80 if (ref->extended_data != ref->data)
81 av_freep(&ref->extended_data);
87 int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
90 dst->format = src->format;
93 case AVMEDIA_TYPE_VIDEO:
94 dst->video->w = src->width;
95 dst->video->h = src->height;
96 dst->video->pixel_aspect = src->sample_aspect_ratio;
97 dst->video->interlaced = src->interlaced_frame;
98 dst->video->top_field_first = src->top_field_first;
99 dst->video->key_frame = src->key_frame;
100 dst->video->pict_type = src->pict_type;
102 case AVMEDIA_TYPE_AUDIO:
103 dst->audio->sample_rate = src->sample_rate;
104 dst->audio->channel_layout = src->channel_layout;
107 return AVERROR(EINVAL);
113 int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
115 int planes, nb_channels;
117 memcpy(dst->data, src->data, sizeof(dst->data));
118 memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
121 dst->format = src->format;
124 case AVMEDIA_TYPE_VIDEO:
125 dst->width = src->video->w;
126 dst->height = src->video->h;
127 dst->sample_aspect_ratio = src->video->pixel_aspect;
128 dst->interlaced_frame = src->video->interlaced;
129 dst->top_field_first = src->video->top_field_first;
130 dst->key_frame = src->video->key_frame;
131 dst->pict_type = src->video->pict_type;
133 case AVMEDIA_TYPE_AUDIO:
134 nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
135 planes = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
137 if (planes > FF_ARRAY_ELEMS(dst->data)) {
138 dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
139 if (!dst->extended_data)
140 return AVERROR(ENOMEM);
141 memcpy(dst->extended_data, src->extended_data,
142 planes * sizeof(dst->extended_data));
144 dst->extended_data = dst->data;
146 dst->sample_rate = src->audio->sample_rate;
147 dst->channel_layout = src->audio->channel_layout;
148 dst->nb_samples = src->audio->nb_samples;
151 return AVERROR(EINVAL);
157 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
159 // copy common properties
164 case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
165 case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;