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/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 & AV_PIX_FMT_FLAG_HWACCEL ||
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 *s = 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  s->line_size[plane] = av_image_get_linesize(inlink->format, inlink->w,
79  plane);
80  }
81 
82  return 0;
83 }
84 
85 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
86 {
87  AVFilterContext *ctx = inlink->dst;
88  AVFilterLink *outlink = ctx->outputs[0];
89 
90  return ff_get_video_buffer(outlink, w, h);
91 }
92 
93 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
94 {
95  AVFilterContext *ctx = inlink->dst;
96  FieldOrderContext *s = ctx->priv;
97  AVFilterLink *outlink = ctx->outputs[0];
98  int h, plane, line_step, line_size, line;
99  uint8_t *data;
100 
101  if (!frame->interlaced_frame ||
102  frame->top_field_first == s->dst_tff)
103  return ff_filter_frame(outlink, frame);
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) {
114  /** Move every line up one line, working from
115  * the top to the bottom of the frame.
116  * The original top line is lost.
117  * The new last line is created as a copy of the
118  * penultimate line from that field. */
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 {
128  /** Move every line down one line, working from
129  * the bottom to the top of the frame.
130  * The original bottom line is lost.
131  * The new first line is created as a copy of the
132  * second line from that field. */
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_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
151 
152 static const AVOption fieldorder_options[] = {
153  { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
154  { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags=FLAGS, .unit = "order" },
155  { "tff", "top field first", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags=FLAGS, .unit = "order" },
156  { NULL },
157 };
158 
159 AVFILTER_DEFINE_CLASS(fieldorder);
160 
162  {
163  .name = "default",
164  .type = AVMEDIA_TYPE_VIDEO,
165  .config_props = config_input,
166  .get_video_buffer = get_video_buffer,
167  .filter_frame = filter_frame,
168  .needs_writable = 1,
169  },
170  { NULL }
171 };
172 
174  {
175  .name = "default",
176  .type = AVMEDIA_TYPE_VIDEO,
177  },
178  { NULL }
179 };
180 
182  .name = "fieldorder",
183  .description = NULL_IF_CONFIG_SMALL("Set the field order."),
184  .priv_size = sizeof(FieldOrderContext),
185  .priv_class = &fieldorder_class,
187  .inputs = avfilter_vf_fieldorder_inputs,
188  .outputs = avfilter_vf_fieldorder_outputs,
189 };