FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_mcdeint.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 /**
22  * @file
23  * Motion Compensation Deinterlacer
24  * Ported from MPlayer libmpcodecs/vf_mcdeint.c.
25  *
26  * Known Issues:
27  *
28  * The motion estimation is somewhat at the mercy of the input, if the
29  * input frames are created purely based on spatial interpolation then
30  * for example a thin black line or another random and not
31  * interpolateable pattern will cause problems.
32  * Note: completely ignoring the "unavailable" lines during motion
33  * estimation did not look any better, so the most obvious solution
34  * would be to improve tfields or penalize problematic motion vectors.
35  *
36  * If non iterative ME is used then snow currently ignores the OBMC
37  * window and as a result sometimes creates artifacts.
38  *
39  * Only past frames are used, we should ideally use future frames too,
40  * something like filtering the whole movie in forward and then
41  * backward direction seems like an interesting idea but the current
42  * filter framework is FAR from supporting such things.
43  *
44  * Combining the motion compensated image with the input image also is
45  * not as trivial as it seems, simple blindly taking even lines from
46  * one and odd ones from the other does not work at all as ME/MC
47  * sometimes has nothing in the previous frames which matches the
48  * current. The current algorithm has been found by trial and error
49  * and almost certainly can be improved...
50  */
51 
52 #include "libavutil/opt.h"
53 #include "libavutil/pixdesc.h"
54 #include "libavcodec/avcodec.h"
55 #include "avfilter.h"
56 #include "formats.h"
57 #include "internal.h"
58 
60  MODE_FAST = 0,
65 };
66 
68  PARITY_TFF = 0, ///< top field first
69  PARITY_BFF = 1, ///< bottom field first
70 };
71 
72 typedef struct {
73  const AVClass *class;
74  int mode; ///< MCDeintMode
75  int parity; ///< MCDeintParity
76  int qp;
79 
80 #define OFFSET(x) offsetof(MCDeintContext, x)
81 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
82 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
83 
84 static const AVOption mcdeint_options[] = {
85  { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_FAST}, 0, MODE_NB-1, FLAGS, .unit="mode" },
86  CONST("fast", NULL, MODE_FAST, "mode"),
87  CONST("medium", NULL, MODE_MEDIUM, "mode"),
88  CONST("slow", NULL, MODE_SLOW, "mode"),
89  CONST("extra_slow", NULL, MODE_EXTRA_SLOW, "mode"),
90 
91  { "parity", "set the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=PARITY_BFF}, -1, 1, FLAGS, "parity" },
92  CONST("tff", "assume top field first", PARITY_TFF, "parity"),
93  CONST("bff", "assume bottom field first", PARITY_BFF, "parity"),
94 
95  { "qp", "set qp", OFFSET(qp), AV_OPT_TYPE_INT, {.i64=1}, INT_MIN, INT_MAX, FLAGS },
96  { NULL }
97 };
98 
99 AVFILTER_DEFINE_CLASS(mcdeint);
100 
101 static int config_props(AVFilterLink *inlink)
102 {
103  AVFilterContext *ctx = inlink->dst;
104  MCDeintContext *mcdeint = ctx->priv;
105  AVCodec *enc;
106  AVCodecContext *enc_ctx;
108  int ret;
109 
110  if (!(enc = avcodec_find_encoder(AV_CODEC_ID_SNOW))) {
111  av_log(ctx, AV_LOG_ERROR, "Snow encoder is not enabled in libavcodec\n");
112  return AVERROR(EINVAL);
113  }
114 
115  mcdeint->enc_ctx = avcodec_alloc_context3(enc);
116  if (!mcdeint->enc_ctx)
117  return AVERROR(ENOMEM);
118  enc_ctx = mcdeint->enc_ctx;
119  enc_ctx->width = inlink->w;
120  enc_ctx->height = inlink->h;
121  enc_ctx->time_base = (AVRational){1,25}; // meaningless
122  enc_ctx->gop_size = INT_MAX;
123  enc_ctx->max_b_frames = 0;
124  enc_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
127  enc_ctx->global_quality = 1;
128  enc_ctx->me_cmp = enc_ctx->me_sub_cmp = FF_CMP_SAD;
129  enc_ctx->mb_cmp = FF_CMP_SSE;
130  av_dict_set(&opts, "memc_only", "1", 0);
131  av_dict_set(&opts, "no_bitstream", "1", 0);
132 
133  switch (mcdeint->mode) {
134  case MODE_EXTRA_SLOW:
135  enc_ctx->refs = 3;
136  case MODE_SLOW:
137  enc_ctx->me_method = ME_ITER;
138  case MODE_MEDIUM:
139  enc_ctx->flags |= AV_CODEC_FLAG_4MV;
140  enc_ctx->dia_size = 2;
141  case MODE_FAST:
142  enc_ctx->flags |= AV_CODEC_FLAG_QPEL;
143  }
144 
145  ret = avcodec_open2(enc_ctx, enc, &opts);
146  av_dict_free(&opts);
147  if (ret < 0)
148  return ret;
149 
150  return 0;
151 }
152 
154 {
155  MCDeintContext *mcdeint = ctx->priv;
156 
157  if (mcdeint->enc_ctx) {
158  avcodec_close(mcdeint->enc_ctx);
159  av_freep(&mcdeint->enc_ctx);
160  }
161 }
162 
164 {
165  static const enum AVPixelFormat pix_fmts[] = {
167  };
168  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
169  if (!fmts_list)
170  return AVERROR(ENOMEM);
171  return ff_set_common_formats(ctx, fmts_list);
172 }
173 
174 static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
175 {
176  MCDeintContext *mcdeint = inlink->dst->priv;
177  AVFilterLink *outlink = inlink->dst->outputs[0];
178  AVFrame *outpic, *frame_dec;
179  AVPacket pkt = {0};
180  int x, y, i, ret, got_frame = 0;
181 
182  outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
183  if (!outpic) {
184  av_frame_free(&inpic);
185  return AVERROR(ENOMEM);
186  }
187  av_frame_copy_props(outpic, inpic);
188  inpic->quality = mcdeint->qp * FF_QP2LAMBDA;
189 
190  av_init_packet(&pkt);
191 
192  ret = avcodec_encode_video2(mcdeint->enc_ctx, &pkt, inpic, &got_frame);
193  if (ret < 0)
194  goto end;
195 
196  frame_dec = mcdeint->enc_ctx->coded_frame;
197 
198  for (i = 0; i < 3; i++) {
199  int is_chroma = !!i;
200  int w = AV_CEIL_RSHIFT(inlink->w, is_chroma);
201  int h = AV_CEIL_RSHIFT(inlink->h, is_chroma);
202  int fils = frame_dec->linesize[i];
203  int srcs = inpic ->linesize[i];
204  int dsts = outpic ->linesize[i];
205 
206  for (y = 0; y < h; y++) {
207  if ((y ^ mcdeint->parity) & 1) {
208  for (x = 0; x < w; x++) {
209  uint8_t *filp = &frame_dec->data[i][x + y*fils];
210  uint8_t *srcp = &inpic ->data[i][x + y*srcs];
211  uint8_t *dstp = &outpic ->data[i][x + y*dsts];
212 
213  if (y > 0 && y < h-1){
214  int is_edge = x < 3 || x > w-4;
215  int diff0 = filp[-fils] - srcp[-srcs];
216  int diff1 = filp[+fils] - srcp[+srcs];
217  int temp = filp[0];
218 
219 #define DELTA(j) av_clip(j, -x, w-1-x)
220 
221 #define GET_SCORE_EDGE(j)\
222  FFABS(srcp[-srcs+DELTA(-1+(j))] - srcp[+srcs+DELTA(-1-(j))])+\
223  FFABS(srcp[-srcs+DELTA(j) ] - srcp[+srcs+DELTA( -(j))])+\
224  FFABS(srcp[-srcs+DELTA(1+(j)) ] - srcp[+srcs+DELTA( 1-(j))])
225 
226 #define GET_SCORE(j)\
227  FFABS(srcp[-srcs-1+(j)] - srcp[+srcs-1-(j)])+\
228  FFABS(srcp[-srcs +(j)] - srcp[+srcs -(j)])+\
229  FFABS(srcp[-srcs+1+(j)] - srcp[+srcs+1-(j)])
230 
231 #define CHECK_EDGE(j)\
232  { int score = GET_SCORE_EDGE(j);\
233  if (score < spatial_score){\
234  spatial_score = score;\
235  diff0 = filp[-fils+DELTA(j)] - srcp[-srcs+DELTA(j)];\
236  diff1 = filp[+fils+DELTA(-(j))] - srcp[+srcs+DELTA(-(j))];\
237 
238 #define CHECK(j)\
239  { int score = GET_SCORE(j);\
240  if (score < spatial_score){\
241  spatial_score= score;\
242  diff0 = filp[-fils+(j)] - srcp[-srcs+(j)];\
243  diff1 = filp[+fils-(j)] - srcp[+srcs-(j)];\
244 
245  if (is_edge) {
246  int spatial_score = GET_SCORE_EDGE(0) - 1;
247  CHECK_EDGE(-1) CHECK_EDGE(-2) }} }}
248  CHECK_EDGE( 1) CHECK_EDGE( 2) }} }}
249  } else {
250  int spatial_score = GET_SCORE(0) - 1;
251  CHECK(-1) CHECK(-2) }} }}
252  CHECK( 1) CHECK( 2) }} }}
253  }
254 
255 
256  if (diff0 + diff1 > 0)
257  temp -= (diff0 + diff1 - FFABS(FFABS(diff0) - FFABS(diff1)) / 2) / 2;
258  else
259  temp -= (diff0 + diff1 + FFABS(FFABS(diff0) - FFABS(diff1)) / 2) / 2;
260  *filp = *dstp = temp > 255U ? ~(temp>>31) : temp;
261  } else {
262  *dstp = *filp;
263  }
264  }
265  }
266  }
267 
268  for (y = 0; y < h; y++) {
269  if (!((y ^ mcdeint->parity) & 1)) {
270  for (x = 0; x < w; x++) {
271  frame_dec->data[i][x + y*fils] =
272  outpic ->data[i][x + y*dsts] = inpic->data[i][x + y*srcs];
273  }
274  }
275  }
276  }
277  mcdeint->parity ^= 1;
278 
279 end:
282  if (ret < 0) {
283  av_frame_free(&outpic);
284  return ret;
285  }
286  return ff_filter_frame(outlink, outpic);
287 }
288 
289 static const AVFilterPad mcdeint_inputs[] = {
290  {
291  .name = "default",
292  .type = AVMEDIA_TYPE_VIDEO,
293  .filter_frame = filter_frame,
294  .config_props = config_props,
295  },
296  { NULL }
297 };
298 
299 static const AVFilterPad mcdeint_outputs[] = {
300  {
301  .name = "default",
302  .type = AVMEDIA_TYPE_VIDEO,
303  },
304  { NULL }
305 };
306 
308  .name = "mcdeint",
309  .description = NULL_IF_CONFIG_SMALL("Apply motion compensating deinterlacing."),
310  .priv_size = sizeof(MCDeintContext),
311  .uninit = uninit,
313  .inputs = mcdeint_inputs,
314  .outputs = mcdeint_outputs,
315  .priv_class = &mcdeint_class,
316 };
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:2882
#define NULL
Definition: coverity.c:32
static const AVFilterPad mcdeint_inputs[]
Definition: vf_mcdeint.c:289
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:3043
AVOption.
Definition: opt.h:245
Main libavfilter public API header.
else temp
Definition: vf_mcdeint.c:259
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
Definition: avcodec.h:1945
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1887
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:76
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3573
attribute_deprecated int me_method
This option does nothing.
Definition: avcodec.h:1894
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1796
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
#define OFFSET(x)
Definition: vf_mcdeint.c:80
MCDeintMode
Definition: vf_mcdeint.c:59
BYTE int const BYTE * srcp
Definition: avisynth_c.h:813
const char * name
Pad name.
Definition: internal.h:59
AVCodecContext * enc_ctx
Definition: vf_mcdeint.c:77
uint8_t
#define av_cold
Definition: attributes.h:82
mode
Definition: f_perms.c:27
AVOptions.
#define GET_SCORE_EDGE(j)
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
bottom field first
Definition: vf_mcdeint.c:69
av_frame_free & inpic
Definition: vf_mcdeint.c:281
int me_cmp
motion estimation comparison function
Definition: avcodec.h:2077
else
Definition: vf_mcdeint.c:261
static const AVFilterPad mcdeint_outputs[]
Definition: vf_mcdeint.c:299
AVFILTER_DEFINE_CLASS(mcdeint)
#define FF_CMP_SSE
Definition: avcodec.h:2097
#define av_log(a,...)
#define CONST(name, help, val, unit)
Definition: vf_mcdeint.c:82
A filter pad used for either input or output.
Definition: internal.h:53
#define U(x)
Definition: vp56_arith.h:37
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
Definition: utils.c:2958
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
#define FLAGS
Definition: vf_mcdeint.c:81
else int spatial_score
Definition: vf_mcdeint.c:250
#define AV_CODEC_FLAG_4MV
4 MV per MB allowed / advanced prediction for H.263.
Definition: avcodec.h:836
return ff_filter_frame(outlink, outpic)
static int config_props(AVFilterLink *inlink)
Definition: vf_mcdeint.c:101
BYTE * dstp
Definition: avisynth_c.h:813
#define AVERROR(e)
Definition: error.h:43
int me_sub_cmp
subpixel motion estimation comparison function
Definition: avcodec.h:2083
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:153
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void * priv
private data for use by the filter
Definition: avfilter.h:322
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1754
int mode
MCDeintMode.
Definition: vf_mcdeint.c:74
#define CODEC_FLAG_LOW_DELAY
Definition: avcodec.h:1099
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_mcdeint.c:153
int refs
number of reference frames
Definition: avcodec.h:2340
AVDictionary * opts
Definition: movenc.c:50
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:832
iterative search
Definition: avcodec.h:770
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:156
int width
picture width / height.
Definition: avcodec.h:1846
static const AVOption mcdeint_options[]
Definition: vf_mcdeint.c:84
AVFormatContext * ctx
Definition: movenc.c:48
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
mcdeint parity
Definition: vf_mcdeint.c:277
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:294
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
#define FF_CMP_SAD
Definition: avcodec.h:2096
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
#define CHECK(j)
main external API structure.
Definition: avcodec.h:1659
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:563
static int query_formats(AVFilterContext *ctx)
Definition: vf_mcdeint.c:163
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
top field first
Definition: vf_mcdeint.c:68
#define CHECK_EDGE(j)
Describe the class of an AVClass context structure.
Definition: log.h:67
#define GET_SCORE(j)
Filter definition.
Definition: avfilter.h:144
Rational number (pair of numerator and denominator).
Definition: rational.h:58
attribute_deprecated int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
Definition: utils.c:1921
const char * name
Filter name.
Definition: avfilter.h:148
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:1208
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:319
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1740
#define AV_CODEC_FLAG_QPEL
Use qpel MC.
Definition: avcodec.h:844
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1872
int mb_cmp
macroblock comparison function (not supported yet)
Definition: avcodec.h:2089
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
* filp
Definition: vf_mcdeint.c:260
if(ret< 0)
Definition: vf_mcdeint.c:282
int parity
MCDeintParity.
Definition: vf_mcdeint.c:75
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:3081
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
int dia_size
ME diamond size & shape.
Definition: avcodec.h:2119
MCDeintParity
Definition: vf_mcdeint.c:67
A list of supported formats for one end of a filter link.
Definition: formats.h:64
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:221
static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
Definition: vf_mcdeint.c:174
An instance of a filter.
Definition: avfilter.h:307
#define av_freep(p)
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1567
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:2877
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:580
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
AVFilter ff_vf_mcdeint
Definition: vf_mcdeint.c:307