FFmpeg
Loading...
Searching...
No Matches
vf_fieldorder.c
Go to the documentation of this file.
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#include "libavutil/imgutils.h"
27#include "libavutil/internal.h"
28#include "libavutil/opt.h"
29#include "libavutil/pixdesc.h"
30#include "avfilter.h"
31#include "filters.h"
32#include "formats.h"
33#include "video.h"
34
35typedef struct FieldOrderContext {
36 const AVClass *class;
37 int dst_tff; ///< output bff/tff
38 int line_size[4]; ///< bytes of pixel data per line for each plane
40
42 AVFilterFormatsConfig **cfg_in,
43 AVFilterFormatsConfig **cfg_out)
44{
47 int ret;
48
49 /** accept any input pixel format that is not hardware accelerated, not
50 * a bitstream format, and does not have vertically sub-sampled chroma */
51 formats = NULL;
52 while ((desc = av_pix_fmt_desc_next(desc))) {
54 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
55 desc->flags & AV_PIX_FMT_FLAG_PAL ||
57 desc->nb_components && !desc->log2_chroma_h &&
58 (ret = ff_add_format(&formats, pix_fmt)) < 0)
59 return ret;
60 }
61 return ff_set_common_formats2(ctx, cfg_in, cfg_out, formats);
62}
63
64static int config_input(AVFilterLink *inlink)
65{
66 AVFilterContext *ctx = inlink->dst;
67 FieldOrderContext *s = ctx->priv;
68
69 return av_image_fill_linesizes(s->line_size, inlink->format, inlink->w);
70}
71
73{
74 AVFilterContext *ctx = inlink->dst;
75 FieldOrderContext *s = ctx->priv;
76 AVFilterLink *outlink = ctx->outputs[0];
77 int h, plane, src_line_step, dst_line_step, line_size, line;
78 uint8_t *dst, *src;
79 AVFrame *out;
80
81 if (!(frame->flags & AV_FRAME_FLAG_INTERLACED) ||
82 !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) == s->dst_tff) {
84 "Skipping %s.\n",
86 "frame with same field order" : "progressive frame");
87 return ff_filter_frame(outlink, frame);
88 }
89
91 out = frame;
92 } else {
93 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
94 if (!out) {
96 return AVERROR(ENOMEM);
97 }
99 }
100
102 "picture will move %s one line\n",
103 s->dst_tff ? "up" : "down");
104 h = frame->height;
105 for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
106 dst_line_step = out->linesize[plane] * (h > 2);
107 src_line_step = frame->linesize[plane] * (h > 2);
108 line_size = s->line_size[plane];
109 dst = out->data[plane];
110 src = frame->data[plane];
111 if (s->dst_tff) {
112 /** Move every line up one line, working from
113 * the top to the bottom of the frame.
114 * The original top line is lost.
115 * The new last line is created as a copy of the
116 * penultimate line from that field. */
117 for (line = 0; line < h; line++) {
118 if (1 + line < frame->height) {
119 memcpy(dst, src + src_line_step, line_size);
120 } else {
121 memcpy(dst, src - 2 * src_line_step, line_size);
122 }
123 dst += dst_line_step;
124 src += src_line_step;
125 }
126 } else {
127 /** Move every line down one line, working from
128 * the bottom to the top of the frame.
129 * The original bottom line is lost.
130 * The new first line is created as a copy of the
131 * second line from that field. */
132 dst += (h - 1) * dst_line_step;
133 src += (h - 1) * src_line_step;
134 for (line = h - 1; line >= 0 ; line--) {
135 if (line > 0) {
136 memcpy(dst, src - src_line_step, line_size);
137 } else {
138 memcpy(dst, src + 2 * src_line_step, line_size);
139 }
140 dst -= dst_line_step;
141 src -= src_line_step;
142 }
143 }
144 }
145 if (s->dst_tff)
147 else
149
150 if (frame != out)
152 return ff_filter_frame(outlink, out);
153}
154
155#define OFFSET(x) offsetof(FieldOrderContext, x)
156#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
157
158static const AVOption fieldorder_options[] = {
159 { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, .unit = "order" },
160 { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags=FLAGS, .unit = "order" },
161 { "tff", "top field first", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags=FLAGS, .unit = "order" },
162 { NULL }
163};
164
166
168 {
169 .name = "default",
170 .type = AVMEDIA_TYPE_VIDEO,
171 .config_props = config_input,
172 .filter_frame = filter_frame,
173 },
174};
175
177 .p.name = "fieldorder",
178 .p.description = NULL_IF_CONFIG_SMALL("Set the field order."),
179 .p.priv_class = &fieldorder_class,
181 .priv_size = sizeof(FieldOrderContext),
185};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_fieldorder
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
static enum AVPixelFormat pix_fmt
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition formats.c:1137
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition formats.c:571
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition frame.h:695
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition frame.h:700
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition imgutils.c:89
misc image utilities
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
const char * desc
Definition libsvtav1.c:83
AVOptions.
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition pixdesc.c:3479
const AVPixFmtDescriptor * av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev)
Iterate over all pixel format descriptors known to libavutil.
Definition pixdesc.c:3467
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition pixdesc.h:124
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition pixdesc.h:128
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition pixdesc.h:120
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
formats
Definition signature.h:47
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A list of supported formats for one end of a filter link.
Definition formats.h:64
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
int dst_tff
output bff/tff
int line_size[4]
bytes of pixel data per line for each plane
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
#define height
Definition dsp.h:89
static const AVFilterPad avfilter_vf_fieldorder_inputs[]
static int config_input(AVFilterLink *inlink)
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
#define OFFSET(x)
static const AVOption fieldorder_options[]
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89