Libav
vf_fieldorder.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Mark Himsley
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
26 #include <stdio.h>
27 #include <string.h>
28 
29 #include "libavutil/imgutils.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "internal.h"
36 #include "video.h"
37 
38 typedef struct FieldOrderContext {
39  const AVClass *class;
40  int dst_tff;
41  int line_size[4];
43 
45 {
48  int ret;
49 
52  if (ctx->inputs[0]) {
53  const AVPixFmtDescriptor *desc = NULL;
54  formats = NULL;
55  while ((desc = av_pix_fmt_desc_next(desc))) {
56  pix_fmt = av_pix_fmt_desc_get_id(desc);
57  if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
59  desc->nb_components && !desc->log2_chroma_h &&
60  (ret = ff_add_format(&formats, pix_fmt)) < 0) {
61  ff_formats_unref(&formats);
62  return ret;
63  }
64  }
65  ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
66  ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
67  }
68 
69  return 0;
70 }
71 
72 static int config_input(AVFilterLink *inlink)
73 {
74  AVFilterContext *ctx = inlink->dst;
75  FieldOrderContext *s = ctx->priv;
76  int plane;
77 
80  for (plane = 0; plane < 4; plane++) {
81  s->line_size[plane] = av_image_get_linesize(inlink->format, inlink->w,
82  plane);
83  }
84 
85  return 0;
86 }
87 
88 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
89 {
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;
95 
96  if (!frame->interlaced_frame ||
97  frame->top_field_first == s->dst_tff) {
99  "Skipping %s.\n",
100  frame->interlaced_frame ?
101  "frame with same field order" : "progressive frame");
102  return ff_filter_frame(outlink, frame);
103  }
104 
105  av_dlog(ctx,
106  "picture will move %s one line\n",
107  s->dst_tff ? "up" : "down");
108  h = frame->height;
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) {
119  for (line = 0; line < h; line++) {
120  if (1 + line < frame->height) {
121  memcpy(data, data + line_step, line_size);
122  } else {
123  memcpy(data, data - line_step - line_step, line_size);
124  }
125  data += line_step;
126  }
127  } else {
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);
139  }
140  data -= line_step;
141  }
142  }
143  }
144  frame->top_field_first = s->dst_tff;
145 
146  return ff_filter_frame(outlink, frame);
147 }
148 
149 #define OFFSET(x) offsetof(FieldOrderContext, x)
150 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
151 static 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 
158 static 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 
166  {
167  .name = "default",
168  .type = AVMEDIA_TYPE_VIDEO,
169  .config_props = config_input,
170  .filter_frame = filter_frame,
171  .needs_writable = 1,
172  },
173  { NULL }
174 };
175 
177  {
178  .name = "default",
179  .type = AVMEDIA_TYPE_VIDEO,
180  },
181  { NULL }
182 };
183 
185  .name = "fieldorder",
186  .description = NULL_IF_CONFIG_SMALL("Set the field order."),
187  .priv_size = sizeof(FieldOrderContext),
188  .priv_class = &fieldorder_class,
190  .inputs = avfilter_vf_fieldorder_inputs,
191  .outputs = avfilter_vf_fieldorder_outputs,
192 };
AVFilter ff_vf_fieldorder
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:50
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
AVOption.
Definition: opt.h:234
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:232
Main libavfilter public API header.
static int query_formats(AVFilterContext *ctx)
Definition: vf_fieldorder.c:44
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)
static enum AVSampleFormat formats[]
const char * name
Pad name.
Definition: internal.h:42
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:571
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:733
uint8_t
AVOptions.
int dst_tff
output bff/tff
Definition: vf_fieldorder.c:40
const char data[16]
Definition: mxf.c:70
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:139
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:320
static const AVOption options[]
static const AVFilterPad avfilter_vf_fieldorder_inputs[]
A filter pad used for either input or output.
Definition: internal.h:36
int line_size[4]
bytes of pixel data per line for each plane
Definition: vf_fieldorder.c:41
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
void * priv
private data for use by the filter
Definition: avfilter.h:584
Definition: graph2dot.c:49
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:120
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
#define FLAGS
common internal API header
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition: pixdesc.c:1615
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
#define OFFSET(x)
enum AVPixelFormat pix_fmt
Definition: movenc.c:843
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
NULL
Definition: eval.c:55
void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:266
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
av_default_item_name
Definition: dnxhdenc.c:52
uint8_t flags
Definition: pixdesc.h:90
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:421
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:221
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:302
const char * name
Filter name.
Definition: avfilter.h:425
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:116
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:578
static const AVFilterPad avfilter_vf_fieldorder_outputs[]
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_fieldorder.c:88
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
int height
Definition: gxfenc.c:72
static int config_input(AVFilterLink *inlink)
Definition: vf_fieldorder.c:72
int ff_add_format(AVFilterFormats **avff, int fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:199
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:325
static const AVClass fieldorder_class
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:563
int height
Definition: frame.h:174
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
const AVPixFmtDescriptor * av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev)
Iterate over all pixel format descriptors known to libavutil.
Definition: pixdesc.c:1606