FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_field.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2003 Rich Felker
3  * Copyright (c) 2012 Stefano Sabatini
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * field filter, based on libmpcodecs/vf_field.c by Rich Felker
25  */
26 
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "avfilter.h"
30 #include "internal.h"
31 
33 
34 typedef struct {
35  const AVClass *class;
37  int nb_planes; ///< number of planes of the current format
38 } FieldContext;
39 
40 #define OFFSET(x) offsetof(FieldContext, x)
41 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
42 
43 static const AVOption field_options[] = {
44  {"type", "set field type (top or bottom)", OFFSET(type), AV_OPT_TYPE_INT, {.i64=FIELD_TYPE_TOP}, 0, 1, FLAGS, "field_type" },
45  {"top", "select top field", 0, AV_OPT_TYPE_CONST, {.i64=FIELD_TYPE_TOP}, INT_MIN, INT_MAX, FLAGS, "field_type"},
46  {"bottom", "select bottom field", 0, AV_OPT_TYPE_CONST, {.i64=FIELD_TYPE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "field_type"},
47 
48  {NULL}
49 };
50 
52 
53 static int config_props_output(AVFilterLink *outlink)
54 {
55  AVFilterContext *ctx = outlink->src;
56  FieldContext *field = ctx->priv;
57  AVFilterLink *inlink = ctx->inputs[0];
58 
59  field->nb_planes = av_pix_fmt_count_planes(outlink->format);
60 
61  outlink->w = inlink->w;
62  outlink->h = (inlink->h + (field->type == FIELD_TYPE_TOP)) / 2;
63 
64  av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d type:%s -> w:%d h:%d\n",
65  inlink->w, inlink->h, field->type == FIELD_TYPE_BOTTOM ? "bottom" : "top",
66  outlink->w, outlink->h);
67  return 0;
68 }
69 
70 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
71 {
72  FieldContext *field = inlink->dst->priv;
73  AVFilterLink *outlink = inlink->dst->outputs[0];
74  int i;
75 
76  inpicref->height = outlink->h;
77  inpicref->interlaced_frame = 0;
78 
79  for (i = 0; i < field->nb_planes; i++) {
80  if (field->type == FIELD_TYPE_BOTTOM)
81  inpicref->data[i] = inpicref->data[i] + inpicref->linesize[i];
82  inpicref->linesize[i] = 2 * inpicref->linesize[i];
83  }
84  return ff_filter_frame(outlink, inpicref);
85 }
86 
87 static const AVFilterPad field_inputs[] = {
88  {
89  .name = "default",
90  .type = AVMEDIA_TYPE_VIDEO,
91  .get_video_buffer = ff_null_get_video_buffer,
92  .filter_frame = filter_frame,
93  },
94  { NULL }
95 };
96 
97 static const AVFilterPad field_outputs[] = {
98  {
99  .name = "default",
100  .type = AVMEDIA_TYPE_VIDEO,
101  .config_props = config_props_output,
102  },
103  { NULL }
104 };
105 
107  .name = "field",
108  .description = NULL_IF_CONFIG_SMALL("Extract a field from the input video."),
109 
110  .priv_size = sizeof(FieldContext),
111  .inputs = field_inputs,
112  .outputs = field_outputs,
113  .priv_class = &field_class,
114 };