pthread: Fix deadlock during thread initialization
[platform/upstream/libav.git] / libavfilter / vf_fieldorder.c
1 /*
2  * Copyright (c) 2011 Mark Himsley
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  * video field order filter, heavily influenced by vf_pad.c
24  */
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "libavutil/imgutils.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "internal.h"
36 #include "video.h"
37
38 typedef struct {
39     const AVClass *class;
40     int dst_tff;               ///< output bff/tff
41     int          line_size[4]; ///< bytes of pixel data per line for each plane
42 } FieldOrderContext;
43
44 static int query_formats(AVFilterContext *ctx)
45 {
46     AVFilterFormats  *formats;
47     enum AVPixelFormat pix_fmt;
48     int              ret;
49
50     /** accept any input pixel format that is not hardware accelerated, not
51      *  a bitstream format, and does not have vertically sub-sampled chroma */
52     if (ctx->inputs[0]) {
53         formats = NULL;
54         for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++) {
55             const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
56             if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
57                   desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
58                 desc->nb_components && !desc->log2_chroma_h &&
59                 (ret = ff_add_format(&formats, pix_fmt)) < 0) {
60                 ff_formats_unref(&formats);
61                 return ret;
62             }
63         }
64         ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
65         ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
66     }
67
68     return 0;
69 }
70
71 static int config_input(AVFilterLink *inlink)
72 {
73     AVFilterContext   *ctx = inlink->dst;
74     FieldOrderContext *s   = ctx->priv;
75     int               plane;
76
77     /** full an array with the number of bytes that the video
78      *  data occupies per line for each plane of the input video */
79     for (plane = 0; plane < 4; plane++) {
80         s->line_size[plane] = av_image_get_linesize(inlink->format, inlink->w,
81                                                     plane);
82     }
83
84     return 0;
85 }
86
87 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
88 {
89     AVFilterContext   *ctx        = inlink->dst;
90     AVFilterLink      *outlink    = ctx->outputs[0];
91
92     return ff_get_video_buffer(outlink, w, h);
93 }
94
95 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
96 {
97     AVFilterContext   *ctx     = inlink->dst;
98     FieldOrderContext *s       = ctx->priv;
99     AVFilterLink      *outlink = ctx->outputs[0];
100     int h, plane, line_step, line_size, line;
101     uint8_t *data;
102
103     if (!frame->interlaced_frame ||
104         frame->top_field_first == s->dst_tff)
105         return ff_filter_frame(outlink, frame);
106
107     av_dlog(ctx,
108             "picture will move %s one line\n",
109             s->dst_tff ? "up" : "down");
110     h = frame->height;
111     for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
112         line_step = frame->linesize[plane];
113         line_size = s->line_size[plane];
114         data = frame->data[plane];
115         if (s->dst_tff) {
116             /** Move every line up one line, working from
117              *  the top to the bottom of the frame.
118              *  The original top line is lost.
119              *  The new last line is created as a copy of the
120              *  penultimate line from that field. */
121             for (line = 0; line < h; line++) {
122                 if (1 + line < frame->height) {
123                     memcpy(data, data + line_step, line_size);
124                 } else {
125                     memcpy(data, data - line_step - line_step, line_size);
126                 }
127                 data += line_step;
128             }
129         } else {
130             /** Move every line down one line, working from
131              *  the bottom to the top of the frame.
132              *  The original bottom line is lost.
133              *  The new first line is created as a copy of the
134              *  second line from that field. */
135             data += (h - 1) * line_step;
136             for (line = h - 1; line >= 0 ; line--) {
137                 if (line > 0) {
138                     memcpy(data, data - line_step, line_size);
139                 } else {
140                     memcpy(data, data + line_step + line_step, line_size);
141                 }
142                 data -= line_step;
143             }
144         }
145     }
146     frame->top_field_first = s->dst_tff;
147
148     return ff_filter_frame(outlink, frame);
149 }
150
151 #define OFFSET(x) offsetof(FieldOrderContext, x)
152 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
153 static const AVOption options[] = {
154     { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
155         { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .unit = "order" },
156         { "tff", "top field first",    0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .unit = "order" },
157     { NULL },
158 };
159
160 static const AVClass fieldorder_class = {
161     .class_name = "fieldorder",
162     .item_name  = av_default_item_name,
163     .option     = options,
164     .version    = LIBAVUTIL_VERSION_INT,
165 };
166
167 static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
168     {
169         .name             = "default",
170         .type             = AVMEDIA_TYPE_VIDEO,
171         .config_props     = config_input,
172         .get_video_buffer = get_video_buffer,
173         .filter_frame     = filter_frame,
174         .needs_writable   = 1,
175     },
176     { NULL }
177 };
178
179 static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
180     {
181         .name = "default",
182         .type = AVMEDIA_TYPE_VIDEO,
183     },
184     { NULL }
185 };
186
187 AVFilter avfilter_vf_fieldorder = {
188     .name          = "fieldorder",
189     .description   = NULL_IF_CONFIG_SMALL("Set the field order."),
190     .priv_size     = sizeof(FieldOrderContext),
191     .priv_class    = &fieldorder_class,
192     .query_formats = query_formats,
193     .inputs        = avfilter_vf_fieldorder_inputs,
194     .outputs       = avfilter_vf_fieldorder_outputs,
195 };