FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 {
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  if (ctx->inputs[0]) {
50  const AVPixFmtDescriptor *desc = NULL;
51  formats = NULL;
52  while ((desc = av_pix_fmt_desc_next(desc))) {
53  pix_fmt = av_pix_fmt_desc_get_id(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  if ((ret = ff_formats_ref(formats, &ctx->inputs[0]->out_formats)) < 0 ||
62  (ret = ff_formats_ref(formats, &ctx->outputs[0]->in_formats)) < 0)
63  return ret;
64  }
65 
66  return 0;
67 }
68 
69 static int config_input(AVFilterLink *inlink)
70 {
71  AVFilterContext *ctx = inlink->dst;
72  FieldOrderContext *s = ctx->priv;
73 
74  return av_image_fill_linesizes(s->line_size, inlink->format, inlink->w);
75 }
76 
77 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
78 {
79  AVFilterContext *ctx = inlink->dst;
80  FieldOrderContext *s = ctx->priv;
81  AVFilterLink *outlink = ctx->outputs[0];
82  int h, plane, src_line_step, dst_line_step, line_size, line;
83  uint8_t *dst, *src;
84  AVFrame *out;
85 
86  if (!frame->interlaced_frame ||
87  frame->top_field_first == s->dst_tff) {
89  "Skipping %s.\n",
90  frame->interlaced_frame ?
91  "frame with same field order" : "progressive frame");
92  return ff_filter_frame(outlink, frame);
93  }
94 
95  if (av_frame_is_writable(frame)) {
96  out = frame;
97  } else {
98  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
99  if (!out) {
100  av_frame_free(&frame);
101  return AVERROR(ENOMEM);
102  }
103  av_frame_copy_props(out, frame);
104  }
105 
106  av_log(ctx, AV_LOG_TRACE,
107  "picture will move %s one line\n",
108  s->dst_tff ? "up" : "down");
109  h = frame->height;
110  for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
111  dst_line_step = out->linesize[plane];
112  src_line_step = frame->linesize[plane];
113  line_size = s->line_size[plane];
114  dst = out->data[plane];
115  src = frame->data[plane];
116  if (s->dst_tff) {
117  /** Move every line up one line, working from
118  * the top to the bottom of the frame.
119  * The original top line is lost.
120  * The new last line is created as a copy of the
121  * penultimate line from that field. */
122  for (line = 0; line < h; line++) {
123  if (1 + line < frame->height) {
124  memcpy(dst, src + src_line_step, line_size);
125  } else {
126  memcpy(dst, src - 2 * src_line_step, line_size);
127  }
128  dst += dst_line_step;
129  src += src_line_step;
130  }
131  } else {
132  /** Move every line down one line, working from
133  * the bottom to the top of the frame.
134  * The original bottom line is lost.
135  * The new first line is created as a copy of the
136  * second line from that field. */
137  dst += (h - 1) * dst_line_step;
138  src += (h - 1) * src_line_step;
139  for (line = h - 1; line >= 0 ; line--) {
140  if (line > 0) {
141  memcpy(dst, src - src_line_step, line_size);
142  } else {
143  memcpy(dst, src + 2 * src_line_step, line_size);
144  }
145  dst -= dst_line_step;
146  src -= src_line_step;
147  }
148  }
149  }
150  out->top_field_first = s->dst_tff;
151 
152  if (frame != out)
153  av_frame_free(&frame);
154  return ff_filter_frame(outlink, out);
155 }
156 
157 #define OFFSET(x) offsetof(FieldOrderContext, x)
158 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
159 
160 static const AVOption fieldorder_options[] = {
161  { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
162  { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags=FLAGS, .unit = "order" },
163  { "tff", "top field first", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags=FLAGS, .unit = "order" },
164  { NULL }
165 };
166 
167 AVFILTER_DEFINE_CLASS(fieldorder);
168 
170  {
171  .name = "default",
172  .type = AVMEDIA_TYPE_VIDEO,
173  .config_props = config_input,
174  .filter_frame = filter_frame,
175  },
176  { NULL }
177 };
178 
180  {
181  .name = "default",
182  .type = AVMEDIA_TYPE_VIDEO,
183  },
184  { NULL }
185 };
186 
188  .name = "fieldorder",
189  .description = NULL_IF_CONFIG_SMALL("Set the field order."),
190  .priv_size = sizeof(FieldOrderContext),
191  .priv_class = &fieldorder_class,
193  .inputs = avfilter_vf_fieldorder_inputs,
194  .outputs = avfilter_vf_fieldorder_outputs,
196 };
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:132
int plane
Definition: avisynth_c.h:422
AVFilter ff_vf_fieldorder
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
static enum AVPixelFormat pix_fmt
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
static const AVOption fieldorder_options[]
AVOption.
Definition: opt.h:245
misc image utilities
Main libavfilter public API header.
const char * desc
Definition: nvenc.c:101
static enum AVSampleFormat formats[]
Definition: avresample.c:163
static int query_formats(AVFilterContext *ctx)
Definition: vf_fieldorder.c:41
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:76
#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:125
const char * name
Pad name.
Definition: internal.h:59
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:315
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1189
uint8_t
AVOptions.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
int dst_tff
output bff/tff
Definition: vf_fieldorder.c:37
AVFILTER_DEFINE_CLASS(fieldorder)
static AVFrame * frame
#define height
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:322
static const AVFilterPad avfilter_vf_fieldorder_inputs[]
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:53
int line_size[4]
bytes of pixel data per line for each plane
Definition: vf_fieldorder.c:38
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:158
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void * priv
private data for use by the filter
Definition: avfilter.h:322
Definition: graph2dot.c:48
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:140
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:337
#define FLAGS
common internal API header
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition: pixdesc.c:2285
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:83
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:440
AVFormatContext * ctx
Definition: movenc.c:48
#define OFFSET(x)
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
#define src
Definition: vp9dsp.c:530
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:529
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
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:88
const char * name
Filter name.
Definition: avfilter.h:148
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:136
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:319
static const AVFilterPad avfilter_vf_fieldorder_outputs[]
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_fieldorder.c:77
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
static int config_input(AVFilterLink *inlink)
Definition: vf_fieldorder.c:69
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:327
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:307
int height
Definition: frame.h:236
FILE * out
Definition: movenc.c:54
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:589
const AVPixFmtDescriptor * av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev)
Iterate over all pixel format descriptors known to libavutil.
Definition: pixdesc.c:2273