h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / vf_fieldorder.c
CommitLineData
2f84bb42
MH
1/*
2 * Copyright (c) 2011 Mark Himsley
3 *
a3b7dabb 4 * This file is part of Libav.
2f84bb42 5 *
a3b7dabb 6 * Libav is free software; you can redistribute it and/or
2f84bb42
MH
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 *
a3b7dabb 11 * Libav is distributed in the hope that it will be useful,
2f84bb42
MH
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
a3b7dabb 17 * License along with Libav; if not, write to the Free Software
2f84bb42
MH
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
1d9c2dc8
MS
26#include <stdio.h>
27#include <string.h>
28
2f84bb42 29#include "libavutil/imgutils.h"
1d9c2dc8 30#include "libavutil/internal.h"
a39c1540 31#include "libavutil/opt.h"
2f84bb42
MH
32#include "libavutil/pixdesc.h"
33#include "avfilter.h"
b74a1da4 34#include "formats.h"
9d0bfc50 35#include "internal.h"
803391f7 36#include "video.h"
2f84bb42 37
58400ac1 38typedef struct FieldOrderContext {
a39c1540
AK
39 const AVClass *class;
40 int dst_tff; ///< output bff/tff
2f84bb42
MH
41 int line_size[4]; ///< bytes of pixel data per line for each plane
42} FieldOrderContext;
43
2f84bb42
MH
44static int query_formats(AVFilterContext *ctx)
45{
46 AVFilterFormats *formats;
716d413c 47 enum AVPixelFormat pix_fmt;
2f84bb42
MH
48 int ret;
49
50 /** accept any input pixel format that is not hardware accelerated, not
51 * a bitstream format, and does not have vertically sub-sampled chroma */
52 if (ctx->inputs[0]) {
a7d070ac 53 const AVPixFmtDescriptor *desc = NULL;
2f84bb42 54 formats = NULL;
a7d070ac
AK
55 while ((desc = av_pix_fmt_desc_next(desc))) {
56 pix_fmt = av_pix_fmt_desc_get_id(desc);
e6c4ac7b
AK
57 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
58 desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
59ee9f78
AK
59 desc->nb_components && !desc->log2_chroma_h &&
60 (ret = ff_add_format(&formats, pix_fmt)) < 0) {
b74a1da4 61 ff_formats_unref(&formats);
2f84bb42
MH
62 return ret;
63 }
59ee9f78 64 }
b74a1da4
AK
65 ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
66 ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
2f84bb42
MH
67 }
68
69 return 0;
70}
71
72static int config_input(AVFilterLink *inlink)
73{
d64cf54b
AK
74 AVFilterContext *ctx = inlink->dst;
75 FieldOrderContext *s = ctx->priv;
2f84bb42
MH
76 int plane;
77
78 /** full an array with the number of bytes that the video
79 * data occupies per line for each plane of the input video */
80 for (plane = 0; plane < 4; plane++) {
d64cf54b
AK
81 s->line_size[plane] = av_image_get_linesize(inlink->format, inlink->w,
82 plane);
2f84bb42
MH
83 }
84
85 return 0;
86}
87
7e350379 88static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
2f84bb42 89{
565e4993
AK
90 AVFilterContext *ctx = inlink->dst;
91 FieldOrderContext *s = ctx->priv;
92 AVFilterLink *outlink = ctx->outputs[0];
93 int h, plane, line_step, line_size, line;
94 uint8_t *data;
2f84bb42 95
7e350379 96 if (!frame->interlaced_frame ||
00d7369d
VG
97 frame->top_field_first == s->dst_tff) {
98 av_log(ctx, AV_LOG_VERBOSE,
99 "Skipping %s.\n",
100 frame->interlaced_frame ?
101 "frame with same field order" : "progressive frame");
565e4993 102 return ff_filter_frame(outlink, frame);
00d7369d 103 }
2f84bb42 104
1a3eb042 105 av_log(ctx, AV_LOG_TRACE,
49dd71a6
AK
106 "picture will move %s one line\n",
107 s->dst_tff ? "up" : "down");
7e350379 108 h = frame->height;
49dd71a6
AK
109 for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
110 line_step = frame->linesize[plane];
111 line_size = s->line_size[plane];
112 data = frame->data[plane];
113 if (s->dst_tff) {
114 /** Move every line up one line, working from
115 * the top to the bottom of the frame.
116 * The original top line is lost.
117 * The new last line is created as a copy of the
118 * penultimate line from that field. */
119 for (line = 0; line < h; line++) {
7e350379 120 if (1 + line < frame->height) {
49dd71a6
AK
121 memcpy(data, data + line_step, line_size);
122 } else {
123 memcpy(data, data - line_step - line_step, line_size);
2f84bb42 124 }
49dd71a6
AK
125 data += line_step;
126 }
127 } else {
128 /** Move every line down one line, working from
129 * the bottom to the top of the frame.
130 * The original bottom line is lost.
131 * The new first line is created as a copy of the
132 * second line from that field. */
133 data += (h - 1) * line_step;
134 for (line = h - 1; line >= 0 ; line--) {
135 if (line > 0) {
136 memcpy(data, data - line_step, line_size);
137 } else {
138 memcpy(data, data + line_step + line_step, line_size);
2f84bb42 139 }
49dd71a6 140 data -= line_step;
2f84bb42
MH
141 }
142 }
49dd71a6 143 }
7e350379 144 frame->top_field_first = s->dst_tff;
2f84bb42 145
565e4993 146 return ff_filter_frame(outlink, frame);
2f84bb42
MH
147}
148
a39c1540
AK
149#define OFFSET(x) offsetof(FieldOrderContext, x)
150#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
151static const AVOption options[] = {
152 { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
153 { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .unit = "order" },
154 { "tff", "top field first", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .unit = "order" },
155 { NULL },
156};
157
158static const AVClass fieldorder_class = {
159 .class_name = "fieldorder",
160 .item_name = av_default_item_name,
161 .option = options,
162 .version = LIBAVUTIL_VERSION_INT,
163};
164
568c70e7
MR
165static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
166 {
167 .name = "default",
168 .type = AVMEDIA_TYPE_VIDEO,
169 .config_props = config_input,
565e4993 170 .filter_frame = filter_frame,
7e350379 171 .needs_writable = 1,
568c70e7
MR
172 },
173 { NULL }
174};
175
176static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
177 {
178 .name = "default",
179 .type = AVMEDIA_TYPE_VIDEO,
180 },
181 { NULL }
182};
183
cd43ca04 184AVFilter ff_vf_fieldorder = {
2f84bb42
MH
185 .name = "fieldorder",
186 .description = NULL_IF_CONFIG_SMALL("Set the field order."),
2f84bb42 187 .priv_size = sizeof(FieldOrderContext),
a39c1540 188 .priv_class = &fieldorder_class,
2f84bb42 189 .query_formats = query_formats,
568c70e7
MR
190 .inputs = avfilter_vf_fieldorder_inputs,
191 .outputs = avfilter_vf_fieldorder_outputs,
2f84bb42 192};