FFmpeg  2.1.1
vf_mcdeint.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * FFmpeg is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 /**
20  * @file
21  * Motion Compensation Deinterlacer
22  * Ported from MPlayer libmpcodecs/vf_mcdeint.c.
23  *
24  * Known Issues:
25  *
26  * The motion estimation is somewhat at the mercy of the input, if the
27  * input frames are created purely based on spatial interpolation then
28  * for example a thin black line or another random and not
29  * interpolateable pattern will cause problems.
30  * Note: completely ignoring the "unavailable" lines during motion
31  * estimation did not look any better, so the most obvious solution
32  * would be to improve tfields or penalize problematic motion vectors.
33  *
34  * If non iterative ME is used then snow currently ignores the OBMC
35  * window and as a result sometimes creates artifacts.
36  *
37  * Only past frames are used, we should ideally use future frames too,
38  * something like filtering the whole movie in forward and then
39  * backward direction seems like a interesting idea but the current
40  * filter framework is FAR from supporting such things.
41  *
42  * Combining the motion compensated image with the input image also is
43  * not as trivial as it seems, simple blindly taking even lines from
44  * one and odd ones from the other does not work at all as ME/MC
45  * sometimes has nothing in the previous frames which matches the
46  * current. The current algorithm has been found by trial and error
47  * and almost certainly can be improved...
48  */
49 
50 #include "libavutil/opt.h"
51 #include "libavutil/pixdesc.h"
52 #include "libavcodec/avcodec.h"
53 #include "avfilter.h"
54 #include "formats.h"
55 #include "internal.h"
56 
58  MODE_FAST = 0,
63 };
64 
66  PARITY_TFF = 0, ///< top field first
67  PARITY_BFF = 1, ///< bottom field first
68 };
69 
70 typedef struct {
71  const AVClass *class;
74  int qp;
77 
78 #define OFFSET(x) offsetof(MCDeintContext, x)
79 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
80 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
81 
82 static const AVOption mcdeint_options[] = {
83  { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_FAST}, 0, MODE_NB-1, FLAGS, .unit="mode" },
84  CONST("fast", NULL, MODE_FAST, "mode"),
85  CONST("medium", NULL, MODE_MEDIUM, "mode"),
86  CONST("slow", NULL, MODE_SLOW, "mode"),
87  CONST("extra_slow", NULL, MODE_EXTRA_SLOW, "mode"),
88 
89  { "parity", "set the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=PARITY_BFF}, -1, 1, FLAGS, "parity" },
90  CONST("tff", "assume top field first", PARITY_TFF, "parity"),
91  CONST("bff", "assume bottom field first", PARITY_BFF, "parity"),
92 
93  { "qp", "set qp", OFFSET(qp), AV_OPT_TYPE_INT, {.i64=1}, INT_MIN, INT_MAX, FLAGS },
94  { NULL }
95 };
96 
97 AVFILTER_DEFINE_CLASS(mcdeint);
98 
99 static int config_props(AVFilterLink *inlink)
100 {
101  AVFilterContext *ctx = inlink->dst;
102  MCDeintContext *mcdeint = ctx->priv;
103  AVCodec *enc;
104  AVCodecContext *enc_ctx;
105  AVDictionary *opts = NULL;
106  int ret;
107 
108  if (!(enc = avcodec_find_encoder(AV_CODEC_ID_SNOW))) {
109  av_log(ctx, AV_LOG_ERROR, "Snow encoder is not enabled in libavcodec\n");
110  return AVERROR(EINVAL);
111  }
112 
113  mcdeint->enc_ctx = avcodec_alloc_context3(enc);
114  if (!mcdeint->enc_ctx)
115  return AVERROR(ENOMEM);
116  enc_ctx = mcdeint->enc_ctx;
117  enc_ctx->width = inlink->w;
118  enc_ctx->height = inlink->h;
119  enc_ctx->time_base = (AVRational){1,25}; // meaningless
120  enc_ctx->gop_size = 300;
121  enc_ctx->max_b_frames = 0;
122  enc_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
125  enc_ctx->global_quality = 1;
126  enc_ctx->me_cmp = enc_ctx->me_sub_cmp = FF_CMP_SAD;
127  enc_ctx->mb_cmp = FF_CMP_SSE;
128  av_dict_set(&opts, "memc_only", "1", 0);
129 
130  switch (mcdeint->mode) {
131  case MODE_EXTRA_SLOW:
132  enc_ctx->refs = 3;
133  case MODE_SLOW:
134  enc_ctx->me_method = ME_ITER;
135  case MODE_MEDIUM:
136  enc_ctx->flags |= CODEC_FLAG_4MV;
137  enc_ctx->dia_size = 2;
138  case MODE_FAST:
139  enc_ctx->flags |= CODEC_FLAG_QPEL;
140  }
141 
142  ret = avcodec_open2(enc_ctx, enc, &opts);
143  av_dict_free(&opts);
144  if (ret < 0)
145  return ret;
146 
147  return 0;
148 }
149 
150 static av_cold void uninit(AVFilterContext *ctx)
151 {
152  MCDeintContext *mcdeint = ctx->priv;
153 
154  if (mcdeint->enc_ctx) {
155  avcodec_close(mcdeint->enc_ctx);
156  av_freep(&mcdeint->enc_ctx);
157  }
158 }
159 
161 {
162  static const enum PixelFormat pix_fmts[] = {
164  };
165 
167 
168  return 0;
169 }
170 
171 static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
172 {
173  MCDeintContext *mcdeint = inlink->dst->priv;
174  AVFilterLink *outlink = inlink->dst->outputs[0];
175  AVFrame *outpic, *frame_dec;
176  AVPacket pkt;
177  int x, y, i, ret, got_frame = 0;
178 
179  outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
180  if (!outpic) {
181  av_frame_free(&inpic);
182  return AVERROR(ENOMEM);
183  }
184  av_frame_copy_props(outpic, inpic);
185  inpic->quality = mcdeint->qp * FF_QP2LAMBDA;
186 
187  av_init_packet(&pkt);
188  pkt.data = NULL; // packet data will be allocated by the encoder
189  pkt.size = 0;
190 
191  ret = avcodec_encode_video2(mcdeint->enc_ctx, &pkt, inpic, &got_frame);
192  if (ret < 0)
193  goto end;
194 
195  frame_dec = mcdeint->enc_ctx->coded_frame;
196 
197  for (i = 0; i < 3; i++) {
198  int is_chroma = !!i;
199  int w = FF_CEIL_RSHIFT(inlink->w, is_chroma);
200  int h = FF_CEIL_RSHIFT(inlink->h, is_chroma);
201  int fils = frame_dec->linesize[i];
202  int srcs = inpic ->linesize[i];
203  int dsts = outpic ->linesize[i];
204 
205  for (y = 0; y < h; y++) {
206  if ((y ^ mcdeint->parity) & 1) {
207  for (x = 0; x < w; x++) {
208  uint8_t *filp = &frame_dec->data[i][x + y*fils];
209  uint8_t *srcp = &inpic ->data[i][x + y*srcs];
210  uint8_t *dstp = &outpic ->data[i][x + y*dsts];
211 
212  if (y > 0 && y < h-1){
213  int is_edge = x < 3 || x > w-4;
214  int diff0 = filp[-fils] - srcp[-srcs];
215  int diff1 = filp[+fils] - srcp[+srcs];
216  int temp = filp[0];
217 
218 #define DELTA(j) av_clip(j, -x, w-1-x)
219 
220 #define GET_SCORE_EDGE(j)\
221  FFABS(srcp[-srcs+DELTA(-1+(j))] - srcp[+srcs+DELTA(-1-(j))])+\
222  FFABS(srcp[-srcs+DELTA(j) ] - srcp[+srcs+DELTA( -(j))])+\
223  FFABS(srcp[-srcs+DELTA(1+(j)) ] - srcp[+srcs+DELTA( 1-(j))])
224 
225 #define GET_SCORE(j)\
226  FFABS(srcp[-srcs-1+(j)] - srcp[+srcs-1-(j)])+\
227  FFABS(srcp[-srcs +(j)] - srcp[+srcs -(j)])+\
228  FFABS(srcp[-srcs+1+(j)] - srcp[+srcs+1-(j)])
229 
230 #define CHECK_EDGE(j)\
231  { int score = GET_SCORE_EDGE(j);\
232  if (score < spatial_score){\
233  spatial_score = score;\
234  diff0 = filp[-fils+DELTA(j)] - srcp[-srcs+DELTA(j)];\
235  diff1 = filp[+fils+DELTA(-(j))] - srcp[+srcs+DELTA(-(j))];\
236 
237 #define CHECK(j)\
238  { int score = GET_SCORE(j);\
239  if (score < spatial_score){\
240  spatial_score= score;\
241  diff0 = filp[-fils+(j)] - srcp[-srcs+(j)];\
242  diff1 = filp[+fils-(j)] - srcp[+srcs-(j)];\
243 
244  if (is_edge) {
245  int spatial_score = GET_SCORE_EDGE(0) - 1;
246  CHECK_EDGE(-1) CHECK_EDGE(-2) }} }}
247  CHECK_EDGE( 1) CHECK_EDGE( 2) }} }}
248  } else {
249  int spatial_score = GET_SCORE(0) - 1;
250  CHECK(-1) CHECK(-2) }} }}
251  CHECK( 1) CHECK( 2) }} }}
252  }
253 
254 
255  if (diff0 + diff1 > 0)
256  temp -= (diff0 + diff1 - FFABS(FFABS(diff0) - FFABS(diff1)) / 2) / 2;
257  else
258  temp -= (diff0 + diff1 + FFABS(FFABS(diff0) - FFABS(diff1)) / 2) / 2;
259  *filp = *dstp = temp > 255U ? ~(temp>>31) : temp;
260  } else {
261  *dstp = *filp;
262  }
263  }
264  }
265  }
266 
267  for (y = 0; y < h; y++) {
268  if (!((y ^ mcdeint->parity) & 1)) {
269  for (x = 0; x < w; x++) {
270  frame_dec->data[i][x + y*fils] =
271  outpic ->data[i][x + y*dsts] = inpic->data[i][x + y*srcs];
272  }
273  }
274  }
275  }
276  mcdeint->parity ^= 1;
277 
278 end:
281  if (ret < 0) {
282  av_frame_free(&outpic);
283  return ret;
284  }
285  return ff_filter_frame(outlink, outpic);
286 }
287 
288 static const AVFilterPad mcdeint_inputs[] = {
289  {
290  .name = "default",
291  .type = AVMEDIA_TYPE_VIDEO,
292  .filter_frame = filter_frame,
293  .config_props = config_props,
294  },
295  { NULL }
296 };
297 
298 static const AVFilterPad mcdeint_outputs[] = {
299  {
300  .name = "default",
301  .type = AVMEDIA_TYPE_VIDEO,
302  },
303  { NULL }
304 };
305 
307  .name = "mcdeint",
308  .description = NULL_IF_CONFIG_SMALL("Apply motion compensating deinterlacing."),
309  .priv_size = sizeof(MCDeintContext),
310  .uninit = uninit,
312  .inputs = mcdeint_inputs,
313  .outputs = mcdeint_outputs,
314  .priv_class = &mcdeint_class,
315 };
static const AVFilterPad mcdeint_inputs[]
Definition: vf_mcdeint.c:288
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:279
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
AVOption.
Definition: opt.h:253
const char * name
Filter name.
Definition: avfilter.h:468
void * priv
private data for use by the filter
Definition: avfilter.h:648
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:111
else temp
Definition: vf_mcdeint.c:258
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:1397
enum MCDeintParity parity
Definition: vf_mcdeint.c:73
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:109
Pixel format.
Definition: avcodec.h:4533
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
enum MCDeintMode mode
Definition: vf_mcdeint.c:72
#define CODEC_FLAG_QPEL
Use qpel MC.
Definition: avcodec.h:694
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1265
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:294
#define OFFSET(x)
Definition: vf_mcdeint.c:78
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
Definition: avfilter.c:965
MCDeintMode
Definition: vf_mcdeint.c:57
BYTE int const BYTE * srcp
Definition: avisynth_c.h:713
const char * name
Pad name.
Definition: internal.h:66
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1118
AVCodecContext * enc_ctx
Definition: vf_mcdeint.c:75
uint8_t
mode
Definition: f_perms.c:27
#define GET_SCORE_EDGE(j)
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only &quot;metadata&quot; fields from src to dst.
Definition: frame.c:446
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
bottom field first
Definition: vf_mcdeint.c:67
av_frame_free & inpic
Definition: vf_mcdeint.c:280
int me_cmp
motion estimation comparison function
Definition: avcodec.h:1524
int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
Definition: utils.c:1834
#define FF_CEIL_RSHIFT(a, b)
Definition: avcodec.h:916
static const AVFilterPad mcdeint_outputs[]
Definition: vf_mcdeint.c:298
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:151
void 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:531
#define CONST(name, help, val, unit)
Definition: vf_mcdeint.c:80
A filter pad used for either input or output.
Definition: internal.h:60
#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:2505
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
#define FLAGS
Definition: vf_mcdeint.c:79
else int spatial_score
Definition: vf_mcdeint.c:249
static int config_props(AVFilterLink *inlink)
Definition: vf_mcdeint.c:99
BYTE * dstp
Definition: avisynth_c.h:713
int me_sub_cmp
subpixel motion estimation comparison function
Definition: avcodec.h:1530
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:2588
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
void av_dict_free(AVDictionary **m)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:162
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1234
#define CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:692
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:123
#define CODEC_FLAG_LOW_DELAY
Force low delay.
Definition: avcodec.h:712
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_mcdeint.c:150
int refs
number of reference frames
Definition: avcodec.h:1791
float y
iterative search
Definition: avcodec.h:604
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
static const AVOption mcdeint_options[]
Definition: vf_mcdeint.c:82
mcdeint parity
Definition: vf_mcdeint.c:276
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:208
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:2426
Main libavfilter public API header.
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:642
planar YUV 4:2:0, 12bpp, (1 Cr &amp; Cb sample per 2x2 Y samples)
Definition: avcodec.h:4534
AVFilter avfilter_vf_mcdeint
Definition: vf_mcdeint.c:306
#define CHECK(j)
main external API structure.
Definition: avcodec.h:1146
static int query_formats(AVFilterContext *ctx)
Definition: vf_mcdeint.c:160
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:62
top field first
Definition: vf_mcdeint.c:66
#define CHECK_EDGE(j)
Describe the class of an AVClass context structure.
Definition: log.h:50
#define GET_SCORE(j)
Filter definition.
Definition: avfilter.h:464
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:102
rational number numerator/denominator
Definition: rational.h:43
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2588
uint8_t * data
Definition: avcodec.h:1063
#define FF_CMP_SSE
Definition: avcodec.h:1544
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:1157
#define FFABS(a)
Definition: avcodec.h:920
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1220
#define PixelFormat
Definition: avcodec.h:4990
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1333
int mb_cmp
macroblock comparison function (not supported yet)
Definition: avcodec.h:1536
* filp
Definition: vf_mcdeint.c:259
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:49
int dia_size
ME diamond size &amp; shape.
Definition: avcodec.h:1565
MCDeintParity
Definition: vf_mcdeint.c:65
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:301
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avcodec.h:2257
#define AVERROR(e)
static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
Definition: vf_mcdeint.c:171
An instance of a filter.
Definition: avfilter.h:627
static AVPacket pkt
Definition: demuxing.c:52
int me_method
Motion estimation algorithm used for video coding.
Definition: avcodec.h:1351
internal API functions
#define CODEC_FLAG_4MV
4 MV per MB allowed / advanced prediction for H.263.
Definition: avcodec.h:693
#define FF_CMP_SAD
Definition: avcodec.h:1543
This structure stores compressed data.
Definition: avcodec.h:1040
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2421
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107