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/opt.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34 #include "video.h"
35 
36 typedef struct {
37  const AVClass *class;
38  int dst_tff; ///< output bff/tff
39  int line_size[4]; ///< bytes of pixel data per line for each plane
41 
43 {
46  int ret;
47 
48  /** accept any input pixel format that is not hardware accelerated, not
49  * a bitstream format, and does not have vertically sub-sampled chroma */
50  if (ctx->inputs[0]) {
51  formats = NULL;
52  for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++) {
53  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
54  if (!(desc->flags & PIX_FMT_HWACCEL ||
55  desc->flags & PIX_FMT_BITSTREAM) &&
56  desc->nb_components && !desc->log2_chroma_h &&
57  (ret = ff_add_format(&formats, pix_fmt)) < 0) {
58  ff_formats_unref(&formats);
59  return ret;
60  }
61  }
62  ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
63  ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
64  }
65 
66  return 0;
67 }
68 
69 static int config_input(AVFilterLink *inlink)
70 {
71  AVFilterContext *ctx = inlink->dst;
72  FieldOrderContext *fieldorder = ctx->priv;
73  int plane;
74 
75  /** full an array with the number of bytes that the video
76  * data occupies per line for each plane of the input video */
77  for (plane = 0; plane < 4; plane++) {
78  fieldorder->line_size[plane] = av_image_get_linesize(
79  inlink->format,
80  inlink->w,
81  plane);
82  }
83 
84  return 0;
85 }
86 
87 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
88 {
89  AVFilterContext *ctx = inlink->dst;
90  AVFilterLink *outlink = ctx->outputs[0];
91 
92  return ff_get_video_buffer(outlink, w, h);
93 }
94 
95 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
96 {
97  AVFilterContext *ctx = inlink->dst;
98  FieldOrderContext *s = ctx->priv;
99  AVFilterLink *outlink = ctx->outputs[0];
100  int h, plane, line_step, line_size, line;
101  uint8_t *data;
102 
103  if (!frame->interlaced_frame ||
104  frame->top_field_first == s->dst_tff)
105  return ff_filter_frame(outlink, frame);
106 
107  av_dlog(ctx,
108  "picture will move %s one line\n",
109  s->dst_tff ? "up" : "down");
110  h = frame->height;
111  for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
112  line_step = frame->linesize[plane];
113  line_size = s->line_size[plane];
114  data = frame->data[plane];
115  if (s->dst_tff) {
116  /** Move every line up one line, working from
117  * the top to the bottom of the frame.
118  * The original top line is lost.
119  * The new last line is created as a copy of the
120  * penultimate line from that field. */
121  for (line = 0; line < h; line++) {
122  if (1 + line < frame->height) {
123  memcpy(data, data + line_step, line_size);
124  } else {
125  memcpy(data, data - line_step - line_step, line_size);
126  }
127  data += line_step;
128  }
129  } else {
130  /** Move every line down one line, working from
131  * the bottom to the top of the frame.
132  * The original bottom line is lost.
133  * The new first line is created as a copy of the
134  * second line from that field. */
135  data += (h - 1) * line_step;
136  for (line = h - 1; line >= 0 ; line--) {
137  if (line > 0) {
138  memcpy(data, data - line_step, line_size);
139  } else {
140  memcpy(data, data + line_step + line_step, line_size);
141  }
142  data -= line_step;
143  }
144  }
145  }
146  frame->top_field_first = s->dst_tff;
147 
148  return ff_filter_frame(outlink, frame);
149 }
150 
151 #define OFFSET(x) offsetof(FieldOrderContext, x)
152 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
153 
154 static const AVOption fieldorder_options[] = {
155  { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
156  { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags=FLAGS, .unit = "order" },
157  { "tff", "top field first", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags=FLAGS, .unit = "order" },
158  { NULL },
159 };
160 
161 AVFILTER_DEFINE_CLASS(fieldorder);
162 
164  {
165  .name = "default",
166  .type = AVMEDIA_TYPE_VIDEO,
167  .config_props = config_input,
168  .get_video_buffer = get_video_buffer,
169  .filter_frame = filter_frame,
170  .needs_writable = 1,
171  },
172  { NULL }
173 };
174 
176  {
177  .name = "default",
178  .type = AVMEDIA_TYPE_VIDEO,
179  },
180  { NULL }
181 };
182 
184  .name = "fieldorder",
185  .description = NULL_IF_CONFIG_SMALL("Set the field order."),
186  .priv_size = sizeof(FieldOrderContext),
187  .priv_class = &fieldorder_class,
189  .inputs = avfilter_vf_fieldorder_inputs,
190  .outputs = avfilter_vf_fieldorder_outputs,
191 };
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane...
Definition: imgutils.c:73
const char * s
Definition: avisynth_c.h:668
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1778
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static const AVOption fieldorder_options[]
AVOption.
Definition: opt.h:251
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
static int query_formats(AVFilterContext *ctx)
Definition: vf_fieldorder.c:42
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:143
output residual component w
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
uint8_t
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
AVOptions.
int dst_tff
output bff/tff
Definition: vf_fieldorder.c:38
AVFILTER_DEFINE_CLASS(fieldorder)
enum AVPixelFormat pix_fmt
Definition: v4l.c:63
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:270
static AVFrame * get_video_buffer(AVFilterLink *inlink, int w, int h)
Definition: vf_fieldorder.c:87
static const AVFilterPad avfilter_vf_fieldorder_inputs[]
frame
Definition: stft.m:14
A filter pad used for either input or output.
int line_size[4]
bytes of pixel data per line for each plane
Definition: vf_fieldorder.c:39
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
void * priv
private data for use by the filter
Definition: avfilter.h:545
Definition: graph2dot.c:48
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:344
#define FLAGS
common internal API header
AVFilter avfilter_vf_fieldorder
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:57
ret
Definition: avfilter.c:821
#define PIX_FMT_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:91
#define OFFSET(x)
#define PIX_FMT_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:92
NULL
Definition: eval.c:55
void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:432
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
uint8_t flags
Definition: pixdesc.h:76
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
void ff_formats_unref(AVFilterFormats **ref)
If *ref is non-NULL, remove *ref as a reference to the format list it currently points to...
Definition: formats.c:468
const char * name
filter name
Definition: avfilter.h:437
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
static const AVFilterPad avfilter_vf_fieldorder_outputs[]
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_fieldorder.c:95
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common formats
Definition: swscale.txt:33
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:275
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip please avoid problems through this extra level of scrutiny For cosmetics only commits you should e g by running git config global user name My Name git config global user email my email which is either set in your personal configuration file through git config core editor or set by one of the following environment VISUAL or EDITOR Log messages should be concise but descriptive Explain why you made a what you did will be obvious from the changes themselves most of the time Saying just bug fix or is bad Remember that people of varying skill levels look at and educate themselves while reading through your code Don t include filenames in log Git provides that information Possibly make the commit message have a descriptive first line
Definition: git-howto.txt:153
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:524
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition: pixfmt.h:237
int height
Definition: frame.h:122
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs