FFmpeg
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 "formats.h"
32 #include "internal.h"
33 #include "video.h"
34 
35 typedef 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 {
43  const AVPixFmtDescriptor *desc = NULL;
45  int ret;
46 
47  /** accept any input pixel format that is not hardware accelerated, not
48  * a bitstream format, and does not have vertically sub-sampled chroma */
49  formats = NULL;
50  while ((desc = av_pix_fmt_desc_next(desc))) {
52  if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
53  desc->flags & AV_PIX_FMT_FLAG_PAL ||
54  desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
55  desc->nb_components && !desc->log2_chroma_h &&
56  (ret = ff_add_format(&formats, pix_fmt)) < 0)
57  return ret;
58  }
60 }
61 
63 {
64  AVFilterContext *ctx = inlink->dst;
65  FieldOrderContext *s = ctx->priv;
66 
67  return av_image_fill_linesizes(s->line_size, inlink->format, inlink->w);
68 }
69 
71 {
72  AVFilterContext *ctx = inlink->dst;
73  FieldOrderContext *s = ctx->priv;
74  AVFilterLink *outlink = ctx->outputs[0];
75  int h, plane, src_line_step, dst_line_step, line_size, line;
76  uint8_t *dst, *src;
77  AVFrame *out;
78 
80  !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) == s->dst_tff) {
82  "Skipping %s.\n",
84  "frame with same field order" : "progressive frame");
85  return ff_filter_frame(outlink, frame);
86  }
87 
89  out = frame;
90  } else {
91  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
92  if (!out) {
94  return AVERROR(ENOMEM);
95  }
97  }
98 
100  "picture will move %s one line\n",
101  s->dst_tff ? "up" : "down");
102  h = frame->height;
103  for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
104  dst_line_step = out->linesize[plane] * (h > 2);
105  src_line_step = frame->linesize[plane] * (h > 2);
106  line_size = s->line_size[plane];
107  dst = out->data[plane];
108  src = frame->data[plane];
109  if (s->dst_tff) {
110  /** Move every line up one line, working from
111  * the top to the bottom of the frame.
112  * The original top line is lost.
113  * The new last line is created as a copy of the
114  * penultimate line from that field. */
115  for (line = 0; line < h; line++) {
116  if (1 + line < frame->height) {
117  memcpy(dst, src + src_line_step, line_size);
118  } else {
119  memcpy(dst, src - 2 * src_line_step, line_size);
120  }
121  dst += dst_line_step;
122  src += src_line_step;
123  }
124  } else {
125  /** Move every line down one line, working from
126  * the bottom to the top of the frame.
127  * The original bottom line is lost.
128  * The new first line is created as a copy of the
129  * second line from that field. */
130  dst += (h - 1) * dst_line_step;
131  src += (h - 1) * src_line_step;
132  for (line = h - 1; line >= 0 ; line--) {
133  if (line > 0) {
134  memcpy(dst, src - src_line_step, line_size);
135  } else {
136  memcpy(dst, src + 2 * src_line_step, line_size);
137  }
138  dst -= dst_line_step;
139  src -= src_line_step;
140  }
141  }
142  }
143 #if FF_API_INTERLACED_FRAME
145  out->top_field_first = s->dst_tff;
147 #endif
148  if (s->dst_tff)
150  else
152 
153  if (frame != out)
155  return ff_filter_frame(outlink, out);
156 }
157 
158 #define OFFSET(x) offsetof(FieldOrderContext, x)
159 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
160 
161 static const AVOption fieldorder_options[] = {
162  { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
163  { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags=FLAGS, .unit = "order" },
164  { "tff", "top field first", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags=FLAGS, .unit = "order" },
165  { NULL }
166 };
167 
168 AVFILTER_DEFINE_CLASS(fieldorder);
169 
171  {
172  .name = "default",
173  .type = AVMEDIA_TYPE_VIDEO,
174  .config_props = config_input,
175  .filter_frame = filter_frame,
176  },
177 };
178 
180  .name = "fieldorder",
181  .description = NULL_IF_CONFIG_SMALL("Set the field order."),
182  .priv_size = sizeof(FieldOrderContext),
183  .priv_class = &fieldorder_class,
188 };
formats
formats
Definition: signature.h:48
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:108
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
FieldOrderContext::line_size
int line_size[4]
bytes of pixel data per line for each plane
Definition: vf_fieldorder.c:38
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
pixdesc.h
AVOption
AVOption.
Definition: opt.h:251
ff_vf_fieldorder
const AVFilter ff_vf_fieldorder
Definition: vf_fieldorder.c:179
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:169
av_pix_fmt_desc_next
const AVPixFmtDescriptor * av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev)
Iterate over all pixel format descriptors known to libavutil.
Definition: pixdesc.c:2971
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:649
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_fieldorder.c:70
video.h
fieldorder_options
static const AVOption fieldorder_options[]
Definition: vf_fieldorder.c:161
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
AV_FRAME_FLAG_TOP_FIELD_FIRST
#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:641
formats.h
AV_PIX_FMT_FLAG_HWACCEL
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:128
OFFSET
#define OFFSET(x)
Definition: vf_fieldorder.c:158
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:47
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:206
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:770
ff_video_default_filterpad
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:36
av_image_fill_linesizes
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
s
#define s(width, name)
Definition: cbs_vp9.c:198
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demux_decode.c:41
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
frame
static AVFrame * frame
Definition: demux_decode.c:54
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:736
ff_add_format
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:470
FLAGS
#define FLAGS
Definition: vf_fieldorder.c:159
FieldOrderContext::dst_tff
int dst_tff
output bff/tff
Definition: vf_fieldorder.c:37
FieldOrderContext
Definition: vf_fieldorder.c:35
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
AV_PIX_FMT_FLAG_BITSTREAM
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:124
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:666
height
#define height
line
Definition: graph2dot.c:48
av_pix_fmt_desc_get_id
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition: pixdesc.c:2983
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_fieldorder.c:62
internal.h
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#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:147
internal.h
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
AV_FRAME_FLAG_INTERLACED
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition: frame.h:636
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
AVFrame::height
int height
Definition: frame.h:412
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:397
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
desc
const char * desc
Definition: libsvtav1.c:83
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:193
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
avfilter_vf_fieldorder_inputs
static const AVFilterPad avfilter_vf_fieldorder_inputs[]
Definition: vf_fieldorder.c:170
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(fieldorder)
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
h
h
Definition: vp9dsp_template.c:2038
AV_PIX_FMT_FLAG_PAL
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:120
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_fieldorder.c:41
line
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common like for each output line the vertical scaler pulls lines from a ring buffer When the ring buffer does not contain the wanted line
Definition: swscale.txt:40