pthread: Fix deadlock during thread initialization
[platform/upstream/libav.git] / libavfilter / vf_aspect.c
1 /*
2  * Copyright (c) 2010 Bobby Bingham
3
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * aspect ratio modification video filters
24  */
25
26 #include <float.h>
27
28 #include "libavutil/common.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
31
32 #include "avfilter.h"
33 #include "internal.h"
34 #include "video.h"
35
36 typedef struct {
37     const AVClass *class;
38     AVRational dar;
39     AVRational sar;
40 #if FF_API_OLD_FILTER_OPTS
41     float aspect_num, aspect_den;
42 #endif
43 } AspectContext;
44
45 #if FF_API_OLD_FILTER_OPTS
46 static av_cold int init(AVFilterContext *ctx)
47 {
48     AspectContext *s = ctx->priv;
49
50     if (s->aspect_num > 0 && s->aspect_den > 0) {
51         av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use "
52                "dar=<number> or dar=num/den.\n");
53         s->sar = s->dar = av_d2q(s->aspect_num / s->aspect_den, INT_MAX);
54     }
55
56     return 0;
57 }
58 #endif
59
60 static int filter_frame(AVFilterLink *link, AVFrame *frame)
61 {
62     AspectContext *s = link->dst->priv;
63
64     frame->sample_aspect_ratio = s->sar;
65     return ff_filter_frame(link->dst->outputs[0], frame);
66 }
67
68 #define OFFSET(x) offsetof(AspectContext, x)
69 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
70
71 #if CONFIG_SETDAR_FILTER
72 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
73 static int setdar_config_props(AVFilterLink *inlink)
74 {
75     AspectContext *s = inlink->dst->priv;
76     AVRational dar;
77
78     if (s->dar.num && s->dar.den) {
79         av_reduce(&s->sar.num, &s->sar.den,
80                    s->dar.num * inlink->h,
81                    s->dar.den * inlink->w, 100);
82         inlink->sample_aspect_ratio = s->sar;
83         dar = s->dar;
84     } else {
85         inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
86         dar = (AVRational){ inlink->w, inlink->h };
87     }
88
89     av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
90            inlink->w, inlink->h, dar.num, dar.den,
91            inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
92
93     return 0;
94 }
95
96 static const AVOption setdar_options[] = {
97 #if FF_API_OLD_FILTER_OPTS
98     { "dar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
99     { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
100 #endif
101     { "dar", "display aspect ratio", OFFSET(dar), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
102     { NULL },
103 };
104
105 static const AVClass setdar_class = {
106     .class_name = "setdar",
107     .item_name  = av_default_item_name,
108     .option     = setdar_options,
109     .version    = LIBAVUTIL_VERSION_INT,
110 };
111
112 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
113     {
114         .name             = "default",
115         .type             = AVMEDIA_TYPE_VIDEO,
116         .config_props     = setdar_config_props,
117         .get_video_buffer = ff_null_get_video_buffer,
118         .filter_frame     = filter_frame,
119     },
120     { NULL }
121 };
122
123 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
124     {
125         .name = "default",
126         .type = AVMEDIA_TYPE_VIDEO,
127     },
128     { NULL }
129 };
130
131 AVFilter avfilter_vf_setdar = {
132     .name      = "setdar",
133     .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
134
135 #if FF_API_OLD_FILTER_OPTS
136     .init      = init,
137 #endif
138
139     .priv_size = sizeof(AspectContext),
140     .priv_class = &setdar_class,
141
142     .inputs    = avfilter_vf_setdar_inputs,
143
144     .outputs   = avfilter_vf_setdar_outputs,
145 };
146 #endif /* CONFIG_SETDAR_FILTER */
147
148 #if CONFIG_SETSAR_FILTER
149 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
150 static int setsar_config_props(AVFilterLink *inlink)
151 {
152     AspectContext *s = inlink->dst->priv;
153
154     inlink->sample_aspect_ratio = s->sar;
155
156     return 0;
157 }
158
159 static const AVOption setsar_options[] = {
160 #if FF_API_OLD_FILTER_OPTS
161     { "sar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
162     { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
163 #endif
164     { "sar", "sample (pixel) aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, INT_MAX, FLAGS },
165     { NULL },
166 };
167
168 static const AVClass setsar_class = {
169     .class_name = "setsar",
170     .item_name  = av_default_item_name,
171     .option     = setsar_options,
172     .version    = LIBAVUTIL_VERSION_INT,
173 };
174
175 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
176     {
177         .name             = "default",
178         .type             = AVMEDIA_TYPE_VIDEO,
179         .config_props     = setsar_config_props,
180         .get_video_buffer = ff_null_get_video_buffer,
181         .filter_frame     = filter_frame,
182     },
183     { NULL }
184 };
185
186 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
187     {
188         .name = "default",
189         .type = AVMEDIA_TYPE_VIDEO,
190     },
191     { NULL }
192 };
193
194 AVFilter avfilter_vf_setsar = {
195     .name      = "setsar",
196     .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
197
198 #if FF_API_OLD_FILTER_OPTS
199     .init      = init,
200 #endif
201
202     .priv_size = sizeof(AspectContext),
203     .priv_class = &setsar_class,
204
205     .inputs    = avfilter_vf_setsar_inputs,
206
207     .outputs   = avfilter_vf_setsar_outputs,
208 };
209 #endif /* CONFIG_SETSAR_FILTER */