]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_fieldorder.c
lavfi: remove palette libmpcodecs wrapper
[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 AVPixelFormat 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 < AV_PIX_FMT_NB; pix_fmt++) {
82             const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
83             if (!(desc->flags & PIX_FMT_HWACCEL ||
84                   desc->flags & PIX_FMT_BITSTREAM) &&
85                 desc->nb_components && !desc->log2_chroma_h &&
86                 (ret = ff_add_format(&formats, pix_fmt)) < 0) {
87                 ff_formats_unref(&formats);
88                 return ret;
89             }
90         }
91         ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
92         ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
93     }
94
95     return 0;
96 }
97
98 static int config_input(AVFilterLink *inlink)
99 {
100     AVFilterContext   *ctx        = inlink->dst;
101     FieldOrderContext *fieldorder = ctx->priv;
102     int               plane;
103
104     /** full an array with the number of bytes that the video
105      *  data occupies per line for each plane of the input video */
106     for (plane = 0; plane < 4; plane++) {
107         fieldorder->line_size[plane] = av_image_get_linesize(
108                 inlink->format,
109                 inlink->w,
110                 plane);
111     }
112
113     return 0;
114 }
115
116 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
117 {
118     AVFilterContext   *ctx        = inlink->dst;
119     AVFilterLink      *outlink    = ctx->outputs[0];
120
121     return ff_get_video_buffer(outlink, perms, w, h);
122 }
123
124 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
125 {
126     AVFilterContext   *ctx        = inlink->dst;
127     AVFilterLink      *outlink    = ctx->outputs[0];
128
129     AVFilterBufferRef *outpicref, *for_next_filter;
130     int ret = 0;
131
132     outpicref = avfilter_ref_buffer(inpicref, ~0);
133     if (!outpicref)
134         return AVERROR(ENOMEM);
135
136     for_next_filter = avfilter_ref_buffer(outpicref, ~0);
137     if (!for_next_filter) {
138         avfilter_unref_bufferp(&outpicref);
139         return AVERROR(ENOMEM);
140     }
141
142     ret = ff_start_frame(outlink, for_next_filter);
143     if (ret < 0) {
144         avfilter_unref_bufferp(&outpicref);
145         return ret;
146     }
147
148     outlink->out_buf = outpicref;
149     return 0;
150 }
151
152 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
153 {
154     AVFilterContext   *ctx        = inlink->dst;
155     FieldOrderContext *fieldorder = ctx->priv;
156     AVFilterLink      *outlink    = ctx->outputs[0];
157
158     AVFilterBufferRef *inpicref   = inlink->cur_buf;
159
160     /** can only currently do slices if this filter is doing nothing
161      *  because this filter is moving picture content, the output
162      *  slice will contain different video lines than the input slice
163      *  and that complexity will be added later */
164     if (  !inpicref->video->interlaced
165         || inpicref->video->top_field_first == fieldorder->dst_tff) {
166         return ff_draw_slice(outlink, y, h, slice_dir);
167     }
168     return 0;
169 }
170
171 static int end_frame(AVFilterLink *inlink)
172 {
173     AVFilterContext   *ctx        = inlink->dst;
174     FieldOrderContext *fieldorder = ctx->priv;
175     AVFilterLink      *outlink    = ctx->outputs[0];
176
177     AVFilterBufferRef *inpicref   = inlink->cur_buf;
178     AVFilterBufferRef *outpicref  = outlink->out_buf;
179
180     int               h, plane, line_step, line_size, line;
181     uint8_t           *cpy_src, *cpy_dst;
182
183     if (    inpicref->video->interlaced
184          && inpicref->video->top_field_first != fieldorder->dst_tff) {
185         av_dlog(ctx,
186                 "picture will move %s one line\n",
187                 fieldorder->dst_tff ? "up" : "down");
188         h = inpicref->video->h;
189         for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
190             line_step = inpicref->linesize[plane];
191             line_size = fieldorder->line_size[plane];
192             cpy_src = inpicref->data[plane];
193             cpy_dst = outpicref->data[plane];
194             if (fieldorder->dst_tff) {
195                 /** Move every line up one line, working from
196                  *  the top to the bottom of the frame.
197                  *  The original top line is lost.
198                  *  The new last line is created as a copy of the
199                  *  penultimate line from that field. */
200                 for (line = 0; line < h; line++) {
201                     if (1 + line < outpicref->video->h) {
202                         memcpy(cpy_dst, cpy_src + line_step, line_size);
203                     } else {
204                         memcpy(cpy_dst, cpy_src - line_step - line_step, line_size);
205                     }
206                     cpy_src += line_step;
207                     cpy_dst += line_step;
208                 }
209             } else {
210                 /** Move every line down one line, working from
211                  *  the bottom to the top of the frame.
212                  *  The original bottom line is lost.
213                  *  The new first line is created as a copy of the
214                  *  second line from that field. */
215                 cpy_src += (h - 1) * line_step;
216                 cpy_dst += (h - 1) * line_step;
217                 for (line = h - 1; line >= 0 ; line--) {
218                     if (line > 0) {
219                         memcpy(cpy_dst, cpy_src - line_step, line_size);
220                     } else {
221                         memcpy(cpy_dst, cpy_src + line_step + line_step, line_size);
222                     }
223                     cpy_src -= line_step;
224                     cpy_dst -= line_step;
225                 }
226             }
227         }
228         outpicref->video->top_field_first = fieldorder->dst_tff;
229         ff_draw_slice(outlink, 0, h, 1);
230     } else {
231         av_dlog(ctx,
232                 "not interlaced or field order already correct\n");
233     }
234
235     return ff_end_frame(outlink);
236 }
237
238 static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
239     {
240         .name             = "default",
241         .type             = AVMEDIA_TYPE_VIDEO,
242         .config_props     = config_input,
243         .start_frame      = start_frame,
244         .get_video_buffer = get_video_buffer,
245         .draw_slice       = draw_slice,
246         .end_frame        = end_frame,
247         .min_perms        = AV_PERM_READ | AV_PERM_PRESERVE,
248     },
249     { NULL }
250 };
251
252 static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
253     {
254         .name = "default",
255         .type = AVMEDIA_TYPE_VIDEO,
256     },
257     { NULL }
258 };
259
260 AVFilter avfilter_vf_fieldorder = {
261     .name          = "fieldorder",
262     .description   = NULL_IF_CONFIG_SMALL("Set the field order."),
263     .init          = init,
264     .priv_size     = sizeof(FieldOrderContext),
265     .query_formats = query_formats,
266     .inputs        = avfilter_vf_fieldorder_inputs,
267     .outputs       = avfilter_vf_fieldorder_outputs,
268 };