FFmpeg
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 "filters.h"
24 #include "video.h"
25 
26 typedef struct SeparateFieldsContext {
27  int nb_planes;
30 
31 static int config_props_output(AVFilterLink *outlink)
32 {
33  AVFilterContext *ctx = outlink->src;
34  SeparateFieldsContext *s = ctx->priv;
35  AVFilterLink *inlink = ctx->inputs[0];
37  FilterLink *ol = ff_filter_link(outlink);
38 
39  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
40 
41  if (inlink->h & 1) {
42  av_log(ctx, AV_LOG_ERROR, "height must be even\n");
43  return AVERROR_INVALIDDATA;
44  }
45 
46  outlink->time_base.num = inlink->time_base.num;
47  outlink->time_base.den = inlink->time_base.den * 2;
48  ol->frame_rate.num = il->frame_rate.num * 2;
49  ol->frame_rate.den = il->frame_rate.den;
50  outlink->w = inlink->w;
51  outlink->h = inlink->h / 2;
52 
53  return 0;
54 }
55 
56 static void extract_field(AVFrame *frame, int nb_planes, int type)
57 {
58  int i;
59 
60  for (i = 0; i < nb_planes; i++) {
61  if (type)
62  frame->data[i] = frame->data[i] + frame->linesize[i];
63  frame->linesize[i] *= 2;
64  }
65 }
66 
67 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
68 {
69  AVFilterContext *ctx = inlink->dst;
70  SeparateFieldsContext *s = ctx->priv;
71  AVFilterLink *outlink = ctx->outputs[0];
72  int ret;
73 
74  inpicref->height = outlink->h;
75 #if FF_API_INTERLACED_FRAME
77  inpicref->interlaced_frame = 0;
79 #endif
80  inpicref->flags &= ~AV_FRAME_FLAG_INTERLACED;
81 
82  if (!s->second) {
83  goto clone;
84  } else {
85  AVFrame *second = s->second;
86 
87  extract_field(second, s->nb_planes, !!(second->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST));
88 
89  if (second->pts != AV_NOPTS_VALUE &&
90  inpicref->pts != AV_NOPTS_VALUE)
91  second->pts += inpicref->pts;
92  else
93  second->pts = AV_NOPTS_VALUE;
94 
95  ret = ff_filter_frame(outlink, second);
96  if (ret < 0)
97  return ret;
98 clone:
99  s->second = av_frame_clone(inpicref);
100  if (!s->second)
101  return AVERROR(ENOMEM);
102  }
103 
104  extract_field(inpicref, s->nb_planes, !(inpicref->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST));
105 
106  if (inpicref->pts != AV_NOPTS_VALUE)
107  inpicref->pts *= 2;
108 
109  return ff_filter_frame(outlink, inpicref);
110 }
111 
112 static int flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts)
113 {
114  AVFilterContext *ctx = outlink->src;
115  SeparateFieldsContext *s = ctx->priv;
116  int ret = 0;
117 
118  if (s->second) {
119  *out_pts = s->second->pts += pts;
120  extract_field(s->second, s->nb_planes, !!(s->second->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST));
121  ret = ff_filter_frame(outlink, s->second);
122  s->second = NULL;
123  }
124 
125  return ret;
126 }
127 
129 {
130  AVFilterLink *inlink = ctx->inputs[0];
131  AVFilterLink *outlink = ctx->outputs[0];
132  AVFrame *in;
133  int64_t pts;
134  int ret, status;
135 
137 
139  if (ret < 0)
140  return ret;
141  if (ret > 0)
142  return filter_frame(inlink, in);
143 
145  if (status == AVERROR_EOF) {
146  int64_t out_pts = pts;
147 
148  ret = flush_frame(outlink, pts, &out_pts);
149  ff_outlink_set_status(outlink, status, out_pts);
150  return ret;
151  }
152  }
153 
155 
156  return FFERROR_NOT_READY;
157 }
158 
160 {
161  SeparateFieldsContext *s = ctx->priv;
162 
163  av_frame_free(&s->second);
164 }
165 
167  {
168  .name = "default",
169  .type = AVMEDIA_TYPE_VIDEO,
170  .config_props = config_props_output,
171  },
172 };
173 
175  .name = "separatefields",
176  .description = NULL_IF_CONFIG_SMALL("Split input video frames into fields."),
177  .priv_size = sizeof(SeparateFieldsContext),
178  .activate = activate,
179  .uninit = uninit,
182 };
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
SeparateFieldsContext::second
AVFrame * second
Definition: vf_separatefields.c:28
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1061
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
flush_frame
static int flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts)
Definition: vf_separatefields.c:112
int64_t
long long int64_t
Definition: coverity.c:34
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
pixdesc.h
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:501
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:661
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
video.h
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:434
AV_FRAME_FLAG_TOP_FIELD_FIRST
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition: frame.h:653
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1490
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3210
SeparateFieldsContext::nb_planes
int nb_planes
Definition: vf_separatefields.c:27
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
Definition: vf_separatefields.c:67
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
pts
static int64_t pts
Definition: transcode_aac.c:644
AVRational::num
int num
Numerator.
Definition: rational.h:59
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
AVFrame::interlaced_frame
attribute_deprecated int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:566
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:37
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:424
s
#define s(width, name)
Definition: cbs_vp9.c:198
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
activate
static int activate(AVFilterContext *ctx)
Definition: vf_separatefields.c:128
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:609
separatefields_outputs
static const AVFilterPad separatefields_outputs[]
Definition: vf_separatefields.c:166
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
NULL
#define NULL
Definition: coverity.c:32
ff_inlink_acknowledge_status
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1437
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
SeparateFieldsContext
Definition: vf_separatefields.c:26
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AV_FRAME_FLAG_INTERLACED
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition: frame.h:648
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
frame
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 the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
ff_vf_separatefields
const AVFilter ff_vf_separatefields
Definition: vf_separatefields.c:174
AVFrame::height
int height
Definition: frame.h:461
status
ov_status_e status
Definition: dnn_backend_openvino.c:100
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_separatefields.c:159
AVRational::den
int den
Denominator.
Definition: rational.h:60
avfilter.h
config_props_output
static int config_props_output(AVFilterLink *outlink)
Definition: vf_separatefields.c:31
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
extract_field
static void extract_field(AVFrame *frame, int nb_planes, int type)
Definition: vf_separatefields.c:56
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61