FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_separatefields.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Paul B Mahol
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 #include "libavutil/pixdesc.h"
22 #include "avfilter.h"
23 #include "internal.h"
24 
25 typedef struct SeparateFieldsContext {
26  int nb_planes;
29 
30 static int config_props_output(AVFilterLink *outlink)
31 {
32  AVFilterContext *ctx = outlink->src;
34  AVFilterLink *inlink = ctx->inputs[0];
35 
37 
38  if (inlink->h & 1) {
39  av_log(ctx, AV_LOG_ERROR, "height must be even\n");
40  return AVERROR_INVALIDDATA;
41  }
42 
43  outlink->time_base.num = inlink->time_base.num;
44  outlink->time_base.den = inlink->time_base.den * 2;
45  outlink->frame_rate.num = inlink->frame_rate.num * 2;
46  outlink->frame_rate.den = inlink->frame_rate.den;
47  outlink->w = inlink->w;
48  outlink->h = inlink->h / 2;
49 
50  return 0;
51 }
52 
53 static void extract_field(AVFrame *frame, int nb_planes, int type)
54 {
55  int i;
56 
57  for (i = 0; i < nb_planes; i++) {
58  if (type)
59  frame->data[i] = frame->data[i] + frame->linesize[i];
60  frame->linesize[i] *= 2;
61  }
62 }
63 
64 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
65 {
66  AVFilterContext *ctx = inlink->dst;
68  AVFilterLink *outlink = ctx->outputs[0];
69  int ret;
70 
71  inpicref->height = outlink->h;
72  inpicref->interlaced_frame = 0;
73 
74  if (!s->second) {
75  goto clone;
76  } else {
77  AVFrame *second = s->second;
78 
79  extract_field(second, s->nb_planes, second->top_field_first);
80 
81  if (second->pts != AV_NOPTS_VALUE &&
82  inpicref->pts != AV_NOPTS_VALUE)
83  second->pts += inpicref->pts;
84  else
85  second->pts = AV_NOPTS_VALUE;
86 
87  ret = ff_filter_frame(outlink, second);
88  if (ret < 0)
89  return ret;
90 clone:
91  s->second = av_frame_clone(inpicref);
92  if (!s->second)
93  return AVERROR(ENOMEM);
94  }
95 
96  extract_field(inpicref, s->nb_planes, !inpicref->top_field_first);
97 
98  if (inpicref->pts != AV_NOPTS_VALUE)
99  inpicref->pts *= 2;
100 
101  return ff_filter_frame(outlink, inpicref);
102 }
103 
104 static int request_frame(AVFilterLink *outlink)
105 {
106  AVFilterContext *ctx = outlink->src;
107  SeparateFieldsContext *s = ctx->priv;
108  int ret;
109 
110  ret = ff_request_frame(ctx->inputs[0]);
111  if (ret == AVERROR_EOF && s->second) {
112  s->second->pts *= 2;
114  ret = ff_filter_frame(outlink, s->second);
115  s->second = 0;
116  }
117 
118  return ret;
119 }
120 
122 {
123  SeparateFieldsContext *s = ctx->priv;
124 
125  av_frame_free(&s->second);
126 }
127 
129  {
130  .name = "default",
131  .type = AVMEDIA_TYPE_VIDEO,
132  .filter_frame = filter_frame,
133  },
134  { NULL }
135 };
136 
138  {
139  .name = "default",
140  .type = AVMEDIA_TYPE_VIDEO,
141  .config_props = config_props_output,
142  .request_frame = request_frame,
143  },
144  { NULL }
145 };
146 
148  .name = "separatefields",
149  .description = NULL_IF_CONFIG_SMALL("Split input video frames into fields."),
150  .priv_size = sizeof(SeparateFieldsContext),
151  .uninit = uninit,
152  .inputs = separatefields_inputs,
153  .outputs = separatefields_outputs,
154 };
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2486
Main libavfilter public API header.
int num
Numerator.
Definition: rational.h:59
static av_cold void uninit(AVFilterContext *ctx)
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
static int config_props_output(AVFilterLink *outlink)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
static const AVFilterPad separatefields_outputs[]
static const AVFilterPad separatefields_inputs[]
#define av_cold
Definition: attributes.h:82
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:319
static AVFrame * frame
#define AVERROR_EOF
End of file.
Definition: error.h:55
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:373
#define av_log(a,...)
static void extract_field(AVFrame *frame, int nb_planes, int type)
A filter pad used for either input or output.
Definition: internal.h:54
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#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:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
GLint GLenum type
Definition: opengl_enc.c:105
Filter definition.
Definition: avfilter.h:144
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
const char * name
Filter name.
Definition: avfilter.h:148
static int request_frame(AVFilterLink *outlink)
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
int den
Denominator.
Definition: rational.h:60
AVFilter ff_vf_separatefields
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:378
An instance of a filter.
Definition: avfilter.h:338
int height
Definition: frame.h:284
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:407
internal API functions
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248