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