FFmpeg
yadif_common.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
3  * 2010 James Darnley <james.darnley@gmail.com>
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 #include "libavutil/avassert.h"
23 #include "libavutil/imgutils.h"
24 #include "internal.h"
25 #include "video.h"
26 #include "yadif.h"
27 
28 static int return_frame(AVFilterContext *ctx, int is_second)
29 {
30  YADIFContext *yadif = ctx->priv;
31  AVFilterLink *link = ctx->outputs[0];
32  int tff, ret;
33 
34  if (yadif->parity == -1) {
35  tff = (yadif->cur->flags & AV_FRAME_FLAG_INTERLACED) ?
36  !!(yadif->cur->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) : 1;
37  } else {
38  tff = yadif->parity ^ 1;
39  }
40 
41  if (is_second) {
42  yadif->out = ff_get_video_buffer(link, link->w, link->h);
43  if (!yadif->out)
44  return AVERROR(ENOMEM);
45 
46  av_frame_copy_props(yadif->out, yadif->cur);
47 #if FF_API_INTERLACED_FRAME
49  yadif->out->interlaced_frame = 0;
51 #endif
53  if (yadif->current_field == YADIF_FIELD_BACK_END)
55  }
56 
57  yadif->filter(ctx, yadif->out, tff ^ !is_second, tff);
58 
59  if (is_second) {
60  int64_t cur_pts = yadif->cur->pts;
61  int64_t next_pts = yadif->next->pts;
62 
63  if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
64  yadif->out->pts = cur_pts + next_pts;
65  if (yadif->pts_multiplier == 1) {
66  yadif->out->pts >>= 1;
67  yadif->out->duration >>= 1;
68  }
69  } else {
70  yadif->out->pts = AV_NOPTS_VALUE;
71  }
72  }
73 
74  ff_ccfifo_inject(&yadif->cc_fifo, yadif->out);
75  ret = ff_filter_frame(ctx->outputs[0], yadif->out);
76 
77  yadif->frame_pending = (yadif->mode&1) && !is_second;
78  return ret;
79 }
80 
81 static int checkstride(YADIFContext *yadif, const AVFrame *a, const AVFrame *b)
82 {
83  int i;
84  for (i = 0; i < yadif->csp->nb_components; i++)
85  if (a->linesize[i] != b->linesize[i])
86  return 1;
87  return 0;
88 }
89 
91 {
92  AVFrame *dst = ff_default_get_video_buffer(link, f->width, f->height);
93  if(!dst)
94  return;
95  av_frame_copy_props(dst, f);
96  av_image_copy2(dst->data, dst->linesize,
97  f->data, f->linesize,
98  dst->format, dst->width, dst->height);
100  av_frame_move_ref(f, dst);
101  av_frame_free(&dst);
102 }
103 
105 {
106  AVFilterContext *ctx = link->dst;
107  YADIFContext *yadif = ctx->priv;
108 
109  av_assert0(frame);
110 
111  ff_ccfifo_extract(&yadif->cc_fifo, frame);
112 
113  if (yadif->frame_pending)
114  return_frame(ctx, 1);
115 
116  if (yadif->prev)
117  av_frame_free(&yadif->prev);
118  yadif->prev = yadif->cur;
119  yadif->cur = yadif->next;
120  yadif->next = frame;
121 
122  if (!yadif->cur) {
123  yadif->cur = av_frame_clone(yadif->next);
124  if (!yadif->cur)
125  return AVERROR(ENOMEM);
127  }
128 
129  if (checkstride(yadif, yadif->next, yadif->cur)) {
130  av_log(ctx, AV_LOG_VERBOSE, "Reallocating frame due to differing stride\n");
131  fixstride(link, yadif->next);
132  }
133  if (checkstride(yadif, yadif->next, yadif->cur))
134  fixstride(link, yadif->cur);
135  if (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))
136  fixstride(link, yadif->prev);
137  if (checkstride(yadif, yadif->next, yadif->cur) || (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))) {
138  av_log(ctx, AV_LOG_ERROR, "Failed to reallocate frame\n");
139  return -1;
140  }
141 
142  if (!yadif->prev)
143  return 0;
144 
145  if ((yadif->deint && !(yadif->cur->flags & AV_FRAME_FLAG_INTERLACED)) ||
146  ctx->is_disabled ||
147  (yadif->deint && !(yadif->prev->flags & AV_FRAME_FLAG_INTERLACED) && yadif->prev->repeat_pict) ||
148  (yadif->deint && !(yadif->next->flags & AV_FRAME_FLAG_INTERLACED) && yadif->next->repeat_pict)
149  ) {
150  yadif->out = av_frame_clone(yadif->cur);
151  if (!yadif->out)
152  return AVERROR(ENOMEM);
153 
154  ff_ccfifo_inject(&yadif->cc_fifo, yadif->out);
155  av_frame_free(&yadif->prev);
156  if (yadif->out->pts != AV_NOPTS_VALUE)
157  yadif->out->pts *= yadif->pts_multiplier;
158  yadif->out->duration *= yadif->pts_multiplier;
159  return ff_filter_frame(ctx->outputs[0], yadif->out);
160  }
161 
162  yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
163  if (!yadif->out)
164  return AVERROR(ENOMEM);
165 
166  av_frame_copy_props(yadif->out, yadif->cur);
167 #if FF_API_INTERLACED_FRAME
169  yadif->out->interlaced_frame = 0;
171 #endif
172  yadif->out->flags &= ~AV_FRAME_FLAG_INTERLACED;
173 
174  if (yadif->out->pts != AV_NOPTS_VALUE)
175  yadif->out->pts *= yadif->pts_multiplier;
176  if (!(yadif->mode & 1))
177  yadif->out->duration *= yadif->pts_multiplier;
178  else if (yadif->pts_multiplier == 1)
179  yadif->out->duration >>= 1;
180 
181  return return_frame(ctx, 0);
182 }
183 
185 {
186  AVFilterContext *ctx = link->src;
187  YADIFContext *yadif = ctx->priv;
188  int ret;
189 
190  if (yadif->frame_pending) {
191  return_frame(ctx, 1);
192  return 0;
193  }
194 
195  if (yadif->eof)
196  return AVERROR_EOF;
197 
198  ret = ff_request_frame(ctx->inputs[0]);
199 
200  if (ret == AVERROR_EOF && yadif->cur) {
201  AVFrame *next = av_frame_clone(yadif->next);
202 
203  if (!next)
204  return AVERROR(ENOMEM);
205 
207  next->pts = yadif->next->pts * 2 - yadif->cur->pts;
208 
209  ff_yadif_filter_frame(ctx->inputs[0], next);
210  yadif->eof = 1;
211  } else if (ret < 0) {
212  return ret;
213  }
214 
215  return 0;
216 }
217 
219 {
220  AVFilterContext *ctx = outlink->src;
221  YADIFContext *yadif = ctx->priv;
222  AVRational tb = ctx->inputs[0]->time_base;
223  int ret;
224 
225  if (av_reduce(&outlink->time_base.num, &outlink->time_base.den, tb.num, tb.den * 2LL, INT_MAX)) {
226  yadif->pts_multiplier = 2;
227  } else {
228  av_log(ctx, AV_LOG_WARNING, "Cannot use exact output timebase\n");
229  outlink->time_base = tb;
230  yadif->pts_multiplier = 1;
231  }
232 
233  outlink->w = ctx->inputs[0]->w;
234  outlink->h = ctx->inputs[0]->h;
235 
236  if (outlink->w < 3 || outlink->h < 3) {
237  av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
238  return AVERROR(EINVAL);
239  }
240 
241  if(yadif->mode & 1)
242  outlink->frame_rate = av_mul_q(ctx->inputs[0]->frame_rate,
243  (AVRational){2, 1});
244  else
245  outlink->frame_rate = ctx->inputs[0]->frame_rate;
246 
247  ret = ff_ccfifo_init(&yadif->cc_fifo, outlink->frame_rate, ctx);
248  if (ret < 0) {
249  av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
250  return ret;
251  }
252 
253  return 0;
254 }
255 
257 {
258  YADIFContext *yadif = ctx->priv;
259 
260  av_frame_free(&yadif->prev);
261  av_frame_free(&yadif->cur );
262  av_frame_free(&yadif->next);
263  ff_ccfifo_uninit(&yadif->cc_fifo);
264 }
265 
266 #define OFFSET(x) offsetof(YADIFContext, x)
267 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
268 
269 #define CONST(name, help, val, u) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, .unit = u }
270 
272  { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, .unit = "mode"},
273  CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
274  CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
275  CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
276  CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
277 
278  { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, .unit = "parity" },
279  CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
280  CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
281  CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
282 
283  { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, .unit = "deint" },
284  CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
285  CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
286 
287  { NULL }
288 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:112
ff_yadif_request_frame
int ff_yadif_request_frame(AVFilterLink *link)
Definition: yadif_common.c:184
YADIF_MODE_SEND_FIELD
@ YADIF_MODE_SEND_FIELD
send 1 frame for each field
Definition: yadif.h:29
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
YADIFContext::parity
int parity
YADIFParity.
Definition: yadif.h:55
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
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
YADIFContext::frame_pending
int frame_pending
Definition: yadif.h:58
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
AVFrame::duration
int64_t duration
Duration of the frame, in the same units as pts.
Definition: frame.h:781
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
YADIFContext::csp
const AVPixFmtDescriptor * csp
Definition: yadif.h:76
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:160
YADIFContext::mode
int mode
YADIFMode.
Definition: yadif.h:54
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:375
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:487
AVFrame::width
int width
Definition: frame.h:447
FLAGS
#define FLAGS
Definition: yadif_common.c:267
AVOption
AVOption.
Definition: opt.h:346
b
#define b
Definition: input.c:41
ff_request_frame
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:462
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:647
video.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:396
YADIF_MODE_SEND_FRAME
@ YADIF_MODE_SEND_FRAME
send 1 frame for each frame
Definition: yadif.h:28
YADIF_MODE_SEND_FIELD_NOSPATIAL
@ YADIF_MODE_SEND_FIELD_NOSPATIAL
send 1 frame for each field but skips spatial interlacing check
Definition: yadif.h:31
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:639
ff_default_get_video_buffer
AVFrame * ff_default_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:107
YADIF_PARITY_AUTO
@ YADIF_PARITY_AUTO
auto detection
Definition: yadif.h:37
YADIFContext::deint
int deint
YADIFDeint.
Definition: yadif.h:56
ff_ccfifo_uninit
void ff_ccfifo_uninit(CCFifo *ccf)
Free all memory allocated in a CCFifo and clear the context.
Definition: ccfifo.c:46
YADIF_MODE_SEND_FRAME_NOSPATIAL
@ YADIF_MODE_SEND_FRAME_NOSPATIAL
send 1 frame for each frame but skips spatial interlacing check
Definition: yadif.h:30
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
AVRational::num
int num
Numerator.
Definition: rational.h:59
AVFrame::interlaced_frame
attribute_deprecated int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:552
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
ff_ccfifo_inject
int ff_ccfifo_inject(CCFifo *ccf, AVFrame *frame)
Insert CC data from the FIFO into an AVFrame (as side data)
Definition: ccfifo.c:133
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:593
link
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 link
Definition: filter_design.txt:23
frame
static AVFrame * frame
Definition: demux_decode.c:54
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:709
AVPixFmtDescriptor::nb_components
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
YADIFContext::eof
int eof
Definition: yadif.h:77
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
ff_ccfifo_extract
int ff_ccfifo_extract(CCFifo *ccf, AVFrame *frame)
Extract CC data from an AVFrame.
Definition: ccfifo.c:183
yadif.h
f
f
Definition: af_crystalizer.c:121
checkstride
static int checkstride(YADIFContext *yadif, const AVFrame *a, const AVFrame *b)
Definition: yadif_common.c:81
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
YADIFContext::out
AVFrame * out
Definition: yadif.h:63
YADIFContext::filter
void(* filter)(AVFilterContext *ctx, AVFrame *dstpic, int parity, int tff)
Definition: yadif.h:65
ff_yadif_config_output_common
int ff_yadif_config_output_common(AVFilterLink *outlink)
Definition: yadif_common.c:218
parity
mcdeint parity
Definition: vf_mcdeint.c:281
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:462
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
YADIFContext::prev
AVFrame * prev
Definition: yadif.h:62
OFFSET
#define OFFSET(x)
Definition: yadif_common.c:266
YADIFContext::pts_multiplier
int pts_multiplier
Definition: yadif.h:90
internal.h
fixstride
static void fixstride(AVFilterLink *link, AVFrame *f)
Definition: yadif_common.c:90
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
ff_yadif_uninit
void ff_yadif_uninit(AVFilterContext *ctx)
Definition: yadif_common.c:256
YADIFContext
Definition: yadif.h:51
av_frame_move_ref
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:633
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:606
tb
#define tb
Definition: regdef.h:68
AV_FRAME_FLAG_INTERLACED
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition: frame.h:634
ff_yadif_options
const AVOption ff_yadif_options[]
Definition: yadif_common.c:271
ff_ccfifo_init
int ff_ccfifo_init(CCFifo *ccf, AVRational framerate, void *log_ctx)
Initialize a CCFifo.
Definition: ccfifo.c:53
ret
ret
Definition: filter_design.txt:187
YADIF_DEINT_ALL
@ YADIF_DEINT_ALL
deinterlace all frames
Definition: yadif.h:41
AVFrame::height
int height
Definition: frame.h:447
YADIFContext::next
AVFrame * next
Definition: yadif.h:61
YADIF_FIELD_END
@ YADIF_FIELD_END
The first or last field in a sequence.
Definition: yadif.h:47
av_image_copy2
static void av_image_copy2(uint8_t *const dst_data[4], const int dst_linesizes[4], uint8_t *const src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Wrapper around av_image_copy() to workaround the limitation that the conversion from uint8_t * const ...
Definition: imgutils.h:184
AVRational::den
int den
Denominator.
Definition: rational.h:60
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
YADIF_DEINT_INTERLACED
@ YADIF_DEINT_INTERLACED
only deinterlace frames marked as interlaced
Definition: yadif.h:42
YADIF_FIELD_BACK_END
@ YADIF_FIELD_BACK_END
The last frame in a sequence.
Definition: yadif.h:46
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
YADIF_PARITY_TFF
@ YADIF_PARITY_TFF
top field first
Definition: yadif.h:35
YADIFContext::current_field
int current_field
YADIFCurrentField.
Definition: yadif.h:88
YADIFContext::cc_fifo
CCFifo cc_fifo
Definition: yadif.h:80
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:420
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
YADIFContext::cur
AVFrame * cur
Definition: yadif.h:60
CONST
#define CONST(name, help, val, u)
Definition: yadif_common.c:269
return_frame
static int return_frame(AVFilterContext *ctx, int is_second)
Definition: yadif_common.c:28
AVFrame::repeat_pict
int repeat_pict
Number of fields in this frame which should be repeated, i.e.
Definition: frame.h:543
ff_yadif_filter_frame
int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: yadif_common.c:104
YADIF_PARITY_BFF
@ YADIF_PARITY_BFF
bottom field first
Definition: yadif.h:36