]> git.sesse.net Git - ffmpeg/blob - libavfilter/buffer.c
Merge commit 'b114d28a18050b5ebd22fc067332e5487243889c'
[ffmpeg] / libavfilter / buffer.c
1 /*
2  * Copyright Stefano Sabatini <stefasab gmail com>
3  * Copyright Anton Khirnov <anton khirnov net>
4  * Copyright Michael Niedermayer <michaelni gmx at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/avassert.h"
25 #include "libavutil/common.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavcodec/avcodec.h"
29
30 #include "avfilter.h"
31 #include "internal.h"
32 #include "audio.h"
33 #include "avcodec.h"
34 #include "version.h"
35
36 #if FF_API_AVFILTERBUFFER
37 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
38 {
39     if (ptr->extended_data != ptr->data)
40         av_freep(&ptr->extended_data);
41     av_freep(&ptr->data[0]);
42     av_free(ptr);
43 }
44
45 static int copy_video_props(AVFilterBufferRefVideoProps *dst, AVFilterBufferRefVideoProps *src) {
46     *dst = *src;
47     if (src->qp_table) {
48         int qsize = src->qp_table_size;
49         dst->qp_table = av_malloc(qsize);
50         if (!dst->qp_table) {
51             av_log(NULL, AV_LOG_ERROR, "Failed to allocate qp_table\n");
52             dst->qp_table_size = 0;
53             return AVERROR(ENOMEM);
54         }
55         memcpy(dst->qp_table, src->qp_table, qsize);
56     }
57     return 0;
58 }
59
60 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
61 {
62     AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
63     if (!ret)
64         return NULL;
65     *ret = *ref;
66
67     ret->metadata = NULL;
68     av_dict_copy(&ret->metadata, ref->metadata, 0);
69
70     if (ref->type == AVMEDIA_TYPE_VIDEO) {
71         ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
72         if (!ret->video) {
73             av_free(ret);
74             return NULL;
75         }
76         copy_video_props(ret->video, ref->video);
77         ret->extended_data = ret->data;
78     } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
79         ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
80         if (!ret->audio) {
81             av_free(ret);
82             return NULL;
83         }
84         *ret->audio = *ref->audio;
85
86         if (ref->extended_data && ref->extended_data != ref->data) {
87             int nb_channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
88             if (!(ret->extended_data = av_malloc_array(sizeof(*ret->extended_data),
89                                                  nb_channels))) {
90                 av_freep(&ret->audio);
91                 av_freep(&ret);
92                 return NULL;
93             }
94             memcpy(ret->extended_data, ref->extended_data,
95                    sizeof(*ret->extended_data) * nb_channels);
96         } else
97             ret->extended_data = ret->data;
98     }
99     ret->perms &= pmask;
100     ret->buf->refcount ++;
101     return ret;
102 }
103
104 void avfilter_unref_buffer(AVFilterBufferRef *ref)
105 {
106     if (!ref)
107         return;
108     av_assert0(ref->buf->refcount > 0);
109     if (!(--ref->buf->refcount))
110         ref->buf->free(ref->buf);
111     if (ref->extended_data != ref->data)
112         av_freep(&ref->extended_data);
113     if (ref->video)
114         av_freep(&ref->video->qp_table);
115     av_freep(&ref->video);
116     av_freep(&ref->audio);
117     av_dict_free(&ref->metadata);
118     av_free(ref);
119 }
120
121 void avfilter_unref_bufferp(AVFilterBufferRef **ref)
122 {
123 FF_DISABLE_DEPRECATION_WARNINGS
124     avfilter_unref_buffer(*ref);
125 FF_ENABLE_DEPRECATION_WARNINGS
126     *ref = NULL;
127 }
128
129 int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
130 {
131     dst->pts    = src->pts;
132     dst->pos    = av_frame_get_pkt_pos(src);
133     dst->format = src->format;
134
135     av_dict_free(&dst->metadata);
136     av_dict_copy(&dst->metadata, av_frame_get_metadata(src), 0);
137
138     switch (dst->type) {
139     case AVMEDIA_TYPE_VIDEO:
140         dst->video->w                   = src->width;
141         dst->video->h                   = src->height;
142         dst->video->sample_aspect_ratio = src->sample_aspect_ratio;
143         dst->video->interlaced          = src->interlaced_frame;
144         dst->video->top_field_first     = src->top_field_first;
145         dst->video->key_frame           = src->key_frame;
146         dst->video->pict_type           = src->pict_type;
147         break;
148     case AVMEDIA_TYPE_AUDIO:
149         dst->audio->sample_rate         = src->sample_rate;
150         dst->audio->channel_layout      = src->channel_layout;
151         break;
152     default:
153         return AVERROR(EINVAL);
154     }
155
156     return 0;
157 }
158
159 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, const AVFilterBufferRef *src)
160 {
161     // copy common properties
162     dst->pts             = src->pts;
163     dst->pos             = src->pos;
164
165     switch (src->type) {
166     case AVMEDIA_TYPE_VIDEO: {
167         if (dst->video->qp_table)
168             av_freep(&dst->video->qp_table);
169         copy_video_props(dst->video, src->video);
170         break;
171     }
172     case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
173     default: break;
174     }
175
176     av_dict_free(&dst->metadata);
177     av_dict_copy(&dst->metadata, src->metadata, 0);
178 }
179 #endif /* FF_API_AVFILTERBUFFER */