]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_fieldorder.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vf_fieldorder.c
1 /*
2  * Copyright (c) 2011 Mark Himsley
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 /* #define DEBUG */
27
28 #include "libavutil/imgutils.h"
29 #include "libavutil/pixdesc.h"
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33 #include "video.h"
34
35 typedef struct
36 {
37     unsigned int dst_tff;      ///< output bff/tff
38     int          line_size[4]; ///< bytes of pixel data per line for each plane
39 } FieldOrderContext;
40
41 static av_cold int init(AVFilterContext *ctx, const char *args)
42 {
43     FieldOrderContext *fieldorder = ctx->priv;
44
45     const char *tff = "tff";
46     const char *bff = "bff";
47
48     if (!args) {
49         fieldorder->dst_tff = 1;
50     } else if (sscanf(args, "%u", &fieldorder->dst_tff) == 1) {
51         fieldorder->dst_tff = !!fieldorder->dst_tff;
52     } else if (!strcmp(tff, args)) {
53         fieldorder->dst_tff = 1;
54     } else if (!strcmp(bff, args)) {
55         fieldorder->dst_tff = 0;
56     } else {
57         av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s'.\n", args);
58         return AVERROR(EINVAL);
59     }
60
61     av_log(ctx, AV_LOG_VERBOSE, "output field order: %s\n",
62             fieldorder->dst_tff ? tff : bff);
63
64     return 0;
65 }
66
67 static int query_formats(AVFilterContext *ctx)
68 {
69     AVFilterFormats  *formats;
70     enum PixelFormat pix_fmt;
71     int              ret;
72
73     /** accept any input pixel format that is not hardware accelerated, not
74      *  a bitstream format, and does not have vertically sub-sampled chroma */
75     if (ctx->inputs[0]) {
76         formats = NULL;
77         for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
78             if (!(  av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL
79                  || av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_BITSTREAM)
80                 && av_pix_fmt_descriptors[pix_fmt].nb_components
81                 && !av_pix_fmt_descriptors[pix_fmt].log2_chroma_h
82                 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
83                 ff_formats_unref(&formats);
84                 return ret;
85             }
86         ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
87         ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
88     }
89
90     return 0;
91 }
92
93 static int config_input(AVFilterLink *inlink)
94 {
95     AVFilterContext   *ctx        = inlink->dst;
96     FieldOrderContext *fieldorder = ctx->priv;
97     int               plane;
98
99     /** full an array with the number of bytes that the video
100      *  data occupies per line for each plane of the input video */
101     for (plane = 0; plane < 4; plane++) {
102         fieldorder->line_size[plane] = av_image_get_linesize(
103                 inlink->format,
104                 inlink->w,
105                 plane);
106     }
107
108     return 0;
109 }
110
111 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
112 {
113     AVFilterContext   *ctx        = inlink->dst;
114     AVFilterLink      *outlink    = ctx->outputs[0];
115
116     return ff_get_video_buffer(outlink, perms, w, h);
117 }
118
119 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
120 {
121     AVFilterContext   *ctx        = inlink->dst;
122     AVFilterLink      *outlink    = ctx->outputs[0];
123
124     AVFilterBufferRef *outpicref, *for_next_filter;
125     int ret = 0;
126
127     outpicref = avfilter_ref_buffer(inpicref, ~0);
128     if (!outpicref)
129         return AVERROR(ENOMEM);
130
131     for_next_filter = avfilter_ref_buffer(outpicref, ~0);
132     if (!for_next_filter) {
133         avfilter_unref_bufferp(&outpicref);
134         return AVERROR(ENOMEM);
135     }
136
137     ret = ff_start_frame(outlink, for_next_filter);
138     if (ret < 0) {
139         avfilter_unref_bufferp(&outpicref);
140         return ret;
141     }
142
143     outlink->out_buf = outpicref;
144     return 0;
145 }
146
147 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
148 {
149     AVFilterContext   *ctx        = inlink->dst;
150     FieldOrderContext *fieldorder = ctx->priv;
151     AVFilterLink      *outlink    = ctx->outputs[0];
152
153     AVFilterBufferRef *inpicref   = inlink->cur_buf;
154
155     /** can only currently do slices if this filter is doing nothing
156      *  because this filter is moving picture content, the output
157      *  slice will contain different video lines than the input slice
158      *  and that complexity will be added later */
159     if (  !inpicref->video->interlaced
160         || inpicref->video->top_field_first == fieldorder->dst_tff) {
161         return ff_draw_slice(outlink, y, h, slice_dir);
162     }
163     return 0;
164 }
165
166 static int end_frame(AVFilterLink *inlink)
167 {
168     AVFilterContext   *ctx        = inlink->dst;
169     FieldOrderContext *fieldorder = ctx->priv;
170     AVFilterLink      *outlink    = ctx->outputs[0];
171
172     AVFilterBufferRef *inpicref   = inlink->cur_buf;
173     AVFilterBufferRef *outpicref  = outlink->out_buf;
174
175     int               h, plane, line_step, line_size, line;
176     uint8_t           *cpy_src, *cpy_dst;
177
178     if (    inpicref->video->interlaced
179          && inpicref->video->top_field_first != fieldorder->dst_tff) {
180         av_dlog(ctx,
181                 "picture will move %s one line\n",
182                 fieldorder->dst_tff ? "up" : "down");
183         h = inpicref->video->h;
184         for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
185             line_step = inpicref->linesize[plane];
186             line_size = fieldorder->line_size[plane];
187             cpy_src = inpicref->data[plane];
188             cpy_dst = outpicref->data[plane];
189             if (fieldorder->dst_tff) {
190                 /** Move every line up one line, working from
191                  *  the top to the bottom of the frame.
192                  *  The original top line is lost.
193                  *  The new last line is created as a copy of the
194                  *  penultimate line from that field. */
195                 for (line = 0; line < h; line++) {
196                     if (1 + line < outpicref->video->h) {
197                         memcpy(cpy_dst, cpy_src + line_step, line_size);
198                     } else {
199                         memcpy(cpy_dst, cpy_src - line_step - line_step, line_size);
200                     }
201                     cpy_src += line_step;
202                     cpy_dst += line_step;
203                 }
204             } else {
205                 /** Move every line down one line, working from
206                  *  the bottom to the top of the frame.
207                  *  The original bottom line is lost.
208                  *  The new first line is created as a copy of the
209                  *  second line from that field. */
210                 cpy_src += (h - 1) * line_step;
211                 cpy_dst += (h - 1) * line_step;
212                 for (line = h - 1; line >= 0 ; line--) {
213                     if (line > 0) {
214                         memcpy(cpy_dst, cpy_src - line_step, line_size);
215                     } else {
216                         memcpy(cpy_dst, cpy_src + line_step + line_step, line_size);
217                     }
218                     cpy_src -= line_step;
219                     cpy_dst -= line_step;
220                 }
221             }
222         }
223         outpicref->video->top_field_first = fieldorder->dst_tff;
224         ff_draw_slice(outlink, 0, h, 1);
225     } else {
226         av_dlog(ctx,
227                 "not interlaced or field order already correct\n");
228     }
229
230     return ff_end_frame(outlink);
231 }
232
233 AVFilter avfilter_vf_fieldorder = {
234     .name          = "fieldorder",
235     .description   = NULL_IF_CONFIG_SMALL("Set the field order."),
236     .init          = init,
237     .priv_size     = sizeof(FieldOrderContext),
238     .query_formats = query_formats,
239     .inputs        = (const AVFilterPad[]) {{ .name             = "default",
240                                               .type             = AVMEDIA_TYPE_VIDEO,
241                                               .config_props     = config_input,
242                                               .start_frame      = start_frame,
243                                               .get_video_buffer = get_video_buffer,
244                                               .draw_slice       = draw_slice,
245                                               .end_frame        = end_frame,
246                                               .min_perms        = AV_PERM_READ,
247                                               .rej_perms        = AV_PERM_REUSE2|AV_PERM_PRESERVE,},
248                                             { .name = NULL}},
249     .outputs       = (const AVFilterPad[]) {{ .name             = "default",
250                                               .type             = AVMEDIA_TYPE_VIDEO, },
251                                             { .name = NULL}},
252 };