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 "internal.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];
36 
37  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
38 
39  if (inlink->h & 1) {
40  av_log(ctx, AV_LOG_ERROR, "height must be even\n");
41  return AVERROR_INVALIDDATA;
42  }
43 
44  outlink->time_base.num = inlink->time_base.num;
45  outlink->time_base.den = inlink->time_base.den * 2;
46  outlink->frame_rate.num = inlink->frame_rate.num * 2;
47  outlink->frame_rate.den = inlink->frame_rate.den;
48  outlink->w = inlink->w;
49  outlink->h = inlink->h / 2;
50 
51  return 0;
52 }
53 
54 static void extract_field(AVFrame *frame, int nb_planes, int type)
55 {
56  int i;
57 
58  for (i = 0; i < nb_planes; i++) {
59  if (type)
60  frame->data[i] = frame->data[i] + frame->linesize[i];
61  frame->linesize[i] *= 2;
62  }
63 }
64 
65 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
66 {
67  AVFilterContext *ctx = inlink->dst;
68  SeparateFieldsContext *s = ctx->priv;
69  AVFilterLink *outlink = ctx->outputs[0];
70  int ret;
71 
72  inpicref->height = outlink->h;
73  inpicref->interlaced_frame = 0;
74 
75  if (!s->second) {
76  goto clone;
77  } else {
78  AVFrame *second = s->second;
79 
80  extract_field(second, s->nb_planes, second->top_field_first);
81 
82  if (second->pts != AV_NOPTS_VALUE &&
83  inpicref->pts != AV_NOPTS_VALUE)
84  second->pts += inpicref->pts;
85  else
86  second->pts = AV_NOPTS_VALUE;
87 
88  ret = ff_filter_frame(outlink, second);
89  if (ret < 0)
90  return ret;
91 clone:
92  s->second = av_frame_clone(inpicref);
93  if (!s->second)
94  return AVERROR(ENOMEM);
95  }
96 
97  extract_field(inpicref, s->nb_planes, !inpicref->top_field_first);
98 
99  if (inpicref->pts != AV_NOPTS_VALUE)
100  inpicref->pts *= 2;
101 
102  return ff_filter_frame(outlink, inpicref);
103 }
104 
105 static int flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts)
106 {
107  AVFilterContext *ctx = outlink->src;
108  SeparateFieldsContext *s = ctx->priv;
109  int ret = 0;
110 
111  if (s->second) {
112  *out_pts = s->second->pts += pts;
113  extract_field(s->second, s->nb_planes, s->second->top_field_first);
114  ret = ff_filter_frame(outlink, s->second);
115  s->second = NULL;
116  }
117 
118  return ret;
119 }
120 
122 {
123  AVFilterLink *inlink = ctx->inputs[0];
124  AVFilterLink *outlink = ctx->outputs[0];
125  AVFrame *in;
126  int64_t pts;
127  int ret, status;
128 
130 
132  if (ret < 0)
133  return ret;
134  if (ret > 0)
135  return filter_frame(inlink, in);
136 
138  if (status == AVERROR_EOF) {
139  int64_t out_pts = pts;
140 
141  ret = flush_frame(outlink, pts, &out_pts);
142  ff_outlink_set_status(outlink, status, out_pts);
143  return ret;
144  }
145  }
146 
148 
149  return FFERROR_NOT_READY;
150 }
151 
153 {
154  SeparateFieldsContext *s = ctx->priv;
155 
156  av_frame_free(&s->second);
157 }
158 
160  {
161  .name = "default",
162  .type = AVMEDIA_TYPE_VIDEO,
163  },
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 };
status
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
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:1018
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:105
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:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
pixdesc.h
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:424
AVFrame::top_field_first
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:474
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
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:199
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:1417
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2700
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:65
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:653
AVRational::num
int num
Numerator.
Definition: rational.h:59
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
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:189
s
#define s(width, name)
Definition: cbs_vp9.c:257
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
activate
static int activate(AVFilterContext *ctx)
Definition: vf_separatefields.c:121
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:422
separatefields_outputs
static const AVFilterPad separatefields_outputs[]
Definition: vf_separatefields.c:166
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
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:1371
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:117
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
internal.h
AVFrame::interlaced_frame
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:469
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
separatefields_inputs
static const AVFilterPad separatefields_inputs[]
Definition: vf_separatefields.c:159
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AVFilter
Filter definition.
Definition: avfilter.h:165
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:389
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_separatefields.c:152
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:402
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
extract_field
static void extract_field(AVFrame *frame, int nb_planes, int type)
Definition: vf_separatefields.c:54
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61