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