pthread: Fix deadlock during thread initialization
[platform/upstream/libav.git] / libavfilter / buffer.c
1 /*
2  * This file is part of Libav.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "libavutil/channel_layout.h"
20 #include "libavutil/common.h"
21 #include "libavcodec/avcodec.h"
22
23 #include "avfilter.h"
24 #include "internal.h"
25 #include "version.h"
26
27 #if FF_API_AVFILTERBUFFER
28 /* TODO: buffer pool.  see comment for avfilter_default_get_video_buffer() */
29 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
30 {
31     if (ptr->extended_data != ptr->data)
32         av_freep(&ptr->extended_data);
33     av_free(ptr->data[0]);
34     av_free(ptr);
35 }
36
37 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
38 {
39     AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
40     if (!ret)
41         return NULL;
42     *ret = *ref;
43     if (ref->type == AVMEDIA_TYPE_VIDEO) {
44         ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
45         if (!ret->video) {
46             av_free(ret);
47             return NULL;
48         }
49         *ret->video = *ref->video;
50         ret->extended_data = ret->data;
51     } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
52         ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
53         if (!ret->audio) {
54             av_free(ret);
55             return NULL;
56         }
57         *ret->audio = *ref->audio;
58
59         if (ref->extended_data != ref->data) {
60             int nb_channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
61             if (!(ret->extended_data = av_malloc(sizeof(*ret->extended_data) *
62                                                  nb_channels))) {
63                 av_freep(&ret->audio);
64                 av_freep(&ret);
65                 return NULL;
66             }
67             memcpy(ret->extended_data, ref->extended_data,
68                    sizeof(*ret->extended_data) * nb_channels);
69         } else
70             ret->extended_data = ret->data;
71     }
72     ret->perms &= pmask;
73     ret->buf->refcount ++;
74     return ret;
75 }
76
77 void avfilter_unref_buffer(AVFilterBufferRef *ref)
78 {
79     if (!ref)
80         return;
81     if (!(--ref->buf->refcount))
82         ref->buf->free(ref->buf);
83     if (ref->extended_data != ref->data)
84         av_freep(&ref->extended_data);
85     av_free(ref->video);
86     av_free(ref->audio);
87     av_free(ref);
88 }
89
90 void avfilter_unref_bufferp(AVFilterBufferRef **ref)
91 {
92     avfilter_unref_buffer(*ref);
93     *ref = NULL;
94 }
95
96 int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
97 {
98     dst->pts    = src->pts;
99     dst->format = src->format;
100
101     switch (dst->type) {
102     case AVMEDIA_TYPE_VIDEO:
103         dst->video->w                   = src->width;
104         dst->video->h                   = src->height;
105         dst->video->pixel_aspect        = src->sample_aspect_ratio;
106         dst->video->interlaced          = src->interlaced_frame;
107         dst->video->top_field_first     = src->top_field_first;
108         dst->video->key_frame           = src->key_frame;
109         dst->video->pict_type           = src->pict_type;
110         break;
111     case AVMEDIA_TYPE_AUDIO:
112         dst->audio->sample_rate         = src->sample_rate;
113         dst->audio->channel_layout      = src->channel_layout;
114         break;
115     default:
116         return AVERROR(EINVAL);
117     }
118
119     return 0;
120 }
121
122 int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
123 {
124     int planes, nb_channels;
125
126     memcpy(dst->data, src->data, sizeof(dst->data));
127     memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
128
129     dst->pts     = src->pts;
130     dst->format  = src->format;
131
132     switch (src->type) {
133     case AVMEDIA_TYPE_VIDEO:
134         dst->width               = src->video->w;
135         dst->height              = src->video->h;
136         dst->sample_aspect_ratio = src->video->pixel_aspect;
137         dst->interlaced_frame    = src->video->interlaced;
138         dst->top_field_first     = src->video->top_field_first;
139         dst->key_frame           = src->video->key_frame;
140         dst->pict_type           = src->video->pict_type;
141         break;
142     case AVMEDIA_TYPE_AUDIO:
143         nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
144         planes      = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
145
146         if (planes > FF_ARRAY_ELEMS(dst->data)) {
147             dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
148             if (!dst->extended_data)
149                 return AVERROR(ENOMEM);
150             memcpy(dst->extended_data, src->extended_data,
151                    planes * sizeof(*dst->extended_data));
152         } else
153             dst->extended_data = dst->data;
154
155         dst->sample_rate         = src->audio->sample_rate;
156         dst->channel_layout      = src->audio->channel_layout;
157         dst->nb_samples          = src->audio->nb_samples;
158         break;
159     default:
160         return AVERROR(EINVAL);
161     }
162
163     return 0;
164 }
165
166 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
167 {
168     // copy common properties
169     dst->pts             = src->pts;
170     dst->pos             = src->pos;
171
172     switch (src->type) {
173     case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
174     case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
175     default: break;
176     }
177 }
178 #endif /* FF_API_AVFILTERBUFFER */