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