FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "yadif.h"
26 
27 static int return_frame(AVFilterContext *ctx, int is_second)
28 {
29  YADIFContext *yadif = ctx->priv;
30  AVFilterLink *link = ctx->outputs[0];
31  int tff, ret;
32 
33  if (yadif->parity == -1) {
34  tff = yadif->cur->interlaced_frame ?
35  yadif->cur->top_field_first : 1;
36  } else {
37  tff = yadif->parity ^ 1;
38  }
39 
40  if (is_second) {
41  yadif->out = ff_get_video_buffer(link, link->w, link->h);
42  if (!yadif->out)
43  return AVERROR(ENOMEM);
44 
45  av_frame_copy_props(yadif->out, yadif->cur);
46  yadif->out->interlaced_frame = 0;
47  }
48 
49  yadif->filter(ctx, yadif->out, tff ^ !is_second, tff);
50 
51  if (is_second) {
52  int64_t cur_pts = yadif->cur->pts;
53  int64_t next_pts = yadif->next->pts;
54 
55  if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
56  yadif->out->pts = cur_pts + next_pts;
57  } else {
58  yadif->out->pts = AV_NOPTS_VALUE;
59  }
60  }
61  ret = ff_filter_frame(ctx->outputs[0], yadif->out);
62 
63  yadif->frame_pending = (yadif->mode&1) && !is_second;
64  return ret;
65 }
66 
67 static int checkstride(YADIFContext *yadif, const AVFrame *a, const AVFrame *b)
68 {
69  int i;
70  for (i = 0; i < yadif->csp->nb_components; i++)
71  if (a->linesize[i] != b->linesize[i])
72  return 1;
73  return 0;
74 }
75 
76 static void fixstride(AVFilterLink *link, AVFrame *f)
77 {
78  AVFrame *dst = ff_default_get_video_buffer(link, f->width, f->height);
79  if(!dst)
80  return;
81  av_frame_copy_props(dst, f);
82  av_image_copy(dst->data, dst->linesize,
83  (const uint8_t **)f->data, f->linesize,
84  dst->format, dst->width, dst->height);
85  av_frame_unref(f);
86  av_frame_move_ref(f, dst);
87  av_frame_free(&dst);
88 }
89 
91 {
92  AVFilterContext *ctx = link->dst;
93  YADIFContext *yadif = ctx->priv;
94 
95  av_assert0(frame);
96 
97  if (yadif->frame_pending)
98  return_frame(ctx, 1);
99 
100  if (yadif->prev)
101  av_frame_free(&yadif->prev);
102  yadif->prev = yadif->cur;
103  yadif->cur = yadif->next;
104  yadif->next = frame;
105 
106  if (!yadif->cur &&
107  !(yadif->cur = av_frame_clone(yadif->next)))
108  return AVERROR(ENOMEM);
109 
110  if (checkstride(yadif, yadif->next, yadif->cur)) {
111  av_log(ctx, AV_LOG_VERBOSE, "Reallocating frame due to differing stride\n");
112  fixstride(link, yadif->next);
113  }
114  if (checkstride(yadif, yadif->next, yadif->cur))
115  fixstride(link, yadif->cur);
116  if (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))
117  fixstride(link, yadif->prev);
118  if (checkstride(yadif, yadif->next, yadif->cur) || (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))) {
119  av_log(ctx, AV_LOG_ERROR, "Failed to reallocate frame\n");
120  return -1;
121  }
122 
123  if (!yadif->prev)
124  return 0;
125 
126  if ((yadif->deint && !yadif->cur->interlaced_frame) ||
127  ctx->is_disabled ||
128  (yadif->deint && !yadif->prev->interlaced_frame && yadif->prev->repeat_pict) ||
129  (yadif->deint && !yadif->next->interlaced_frame && yadif->next->repeat_pict)
130  ) {
131  yadif->out = av_frame_clone(yadif->cur);
132  if (!yadif->out)
133  return AVERROR(ENOMEM);
134 
135  av_frame_free(&yadif->prev);
136  if (yadif->out->pts != AV_NOPTS_VALUE)
137  yadif->out->pts *= 2;
138  return ff_filter_frame(ctx->outputs[0], yadif->out);
139  }
140 
141  yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
142  if (!yadif->out)
143  return AVERROR(ENOMEM);
144 
145  av_frame_copy_props(yadif->out, yadif->cur);
146  yadif->out->interlaced_frame = 0;
147 
148  if (yadif->out->pts != AV_NOPTS_VALUE)
149  yadif->out->pts *= 2;
150 
151  return return_frame(ctx, 0);
152 }
153 
155 {
156  AVFilterContext *ctx = link->src;
157  YADIFContext *yadif = ctx->priv;
158  int ret;
159 
160  if (yadif->frame_pending) {
161  return_frame(ctx, 1);
162  return 0;
163  }
164 
165  if (yadif->eof)
166  return AVERROR_EOF;
167 
168  ret = ff_request_frame(ctx->inputs[0]);
169 
170  if (ret == AVERROR_EOF && yadif->cur) {
171  AVFrame *next = av_frame_clone(yadif->next);
172 
173  if (!next)
174  return AVERROR(ENOMEM);
175 
176  next->pts = yadif->next->pts * 2 - yadif->cur->pts;
177 
178  ff_yadif_filter_frame(ctx->inputs[0], next);
179  yadif->eof = 1;
180  } else if (ret < 0) {
181  return ret;
182  }
183 
184  return 0;
185 }
186 
187 #define OFFSET(x) offsetof(YADIFContext, x)
188 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
189 
190 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
191 
193  { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
194  CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
195  CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
196  CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
197  CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
198 
199  { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
200  CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
201  CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
202  CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
203 
204  { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
205  CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
206  CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
207 
208  { NULL }
209 };
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
AVOption.
Definition: opt.h:246
misc image utilities
int repeat_pict
When decoding, this signals how much the picture must be delayed.
Definition: frame.h:368
const char * b
Definition: vf_curves.c:116
send 1 frame for each frame but skips spatial interlacing check
Definition: yadif.h:29
int frame_pending
Definition: yadif.h:51
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:582
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:99
int is_disabled
the enabled state from the last expression evaluation
Definition: avfilter.h:385
#define FLAGS
Definition: yadif_common.c:188
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
AVFrame * cur
Definition: yadif.h:53
uint8_t
#define f(width, name)
Definition: cbs_vp9.c:255
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:319
AVFrame * next
Definition: yadif.h:54
static AVFrame * frame
bottom field first
Definition: yadif.h:35
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:373
send 1 frame for each field
Definition: yadif.h:28
AVFrame * prev
Definition: yadif.h:55
int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: yadif_common.c:90
#define av_log(a,...)
auto detection
Definition: yadif.h:36
int width
Definition: frame.h:284
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int deint
YADIFDeint.
Definition: yadif.h:49
#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
void * priv
private data for use by the filter
Definition: avfilter.h:353
simple assert() macros that are a bit more flexible than ISO C assert().
static int return_frame(AVFilterContext *ctx, int is_second)
Definition: yadif_common.c:27
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:387
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:83
static void fixstride(AVFilterLink *link, AVFrame *f)
Definition: yadif_common.c:76
AVFormatContext * ctx
Definition: movenc.c:48
mcdeint parity
Definition: vf_mcdeint.c:274
void(* filter)(AVFilterContext *ctx, AVFrame *dstpic, int parity, int tff)
Definition: yadif.h:58
int eof
Definition: yadif.h:70
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:299
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
AVFrame * out
Definition: yadif.h:56
#define OFFSET(x)
Definition: yadif_common.c:187
#define CONST(name, help, val, unit)
Definition: yadif_common.c:190
static int checkstride(YADIFContext *yadif, const AVFrame *a, const AVFrame *b)
Definition: yadif_common.c:67
int parity
YADIFParity.
Definition: yadif.h:48
send 1 frame for each field but skips spatial interlacing check
Definition: yadif.h:30
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
int ff_yadif_request_frame(AVFilterLink *link)
Definition: yadif_common.c:154
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
AVFrame * ff_default_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:44
const AVPixFmtDescriptor * csp
Definition: yadif.h:69
deinterlace all frames
Definition: yadif.h:40
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:378
send 1 frame for each frame
Definition: yadif.h:27
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
only deinterlace frames marked as interlaced
Definition: yadif.h:41
top field first
Definition: yadif.h:34
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
int mode
YADIFMode.
Definition: yadif.h:47
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:654
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
const AVOption ff_yadif_options[]
Definition: yadif_common.c:192