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
58
65 };
66
70 };
71
80
81 #define OFFSET(x) offsetof(MCDeintContext, x)
82 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
83 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
84
91
95
98 };
99
101
103 {
110
114 }
115
136
137 switch (mcdeint->
mode) {
147 }
148
153
154 return 0;
155 }
156
158 {
160
163 }
164
166 {
171 int x, y,
i,
ret, got_frame = 0;
172
174 if (!outpic) {
177 }
180
183 goto end;
184
185 frame_dec = mcdeint->
enc_ctx->coded_frame;
186
187 for (
i = 0;
i < 3;
i++) {
192 int srcs =
inpic ->linesize[
i];
194
195 for (y = 0; y <
h; y++) {
196 if ((y ^ mcdeint->
parity) & 1) {
197 for (x = 0; x <
w; x++) {
198 uint8_t *
filp = &frame_dec->
data[
i][x + y*fils];
199 uint8_t *srcp = &
inpic ->data[
i][x + y*srcs];
200 uint8_t *dstp = &outpic ->
data[
i][x + y*dsts];
201
202 if (y > 0 && y <
h-1){
203 int is_edge = x < 3 || x >
w-4;
204 int diff0 =
filp[-fils] - srcp[-srcs];
205 int diff1 =
filp[+fils] - srcp[+srcs];
207
208 #define DELTA(j) av_clip(j, -x, w-1-x)
209
210 #define GET_SCORE_EDGE(j)\
211 FFABS(srcp[-srcs+DELTA(-1+(j))] - srcp[+srcs+DELTA(-1-(j))])+\
212 FFABS(srcp[-srcs+DELTA(j) ] - srcp[+srcs+DELTA( -(j))])+\
213 FFABS(srcp[-srcs+DELTA(1+(j)) ] - srcp[+srcs+DELTA( 1-(j))])
214
215 #define GET_SCORE(j)\
216 FFABS(srcp[-srcs-1+(j)] - srcp[+srcs-1-(j)])+\
217 FFABS(srcp[-srcs +(j)] - srcp[+srcs -(j)])+\
218 FFABS(srcp[-srcs+1+(j)] - srcp[+srcs+1-(j)])
219
220 #define CHECK_EDGE(j)\
221 { int score = GET_SCORE_EDGE(j);\
222 if (score < spatial_score){\
223 spatial_score = score;\
224 diff0 = filp[-fils+DELTA(j)] - srcp[-srcs+DELTA(j)];\
225 diff1 = filp[+fils+DELTA(-(j))] - srcp[+srcs+DELTA(-(j))];\
226
227 #define CHECK(j)\
228 { int score = GET_SCORE(j);\
229 if (score < spatial_score){\
230 spatial_score= score;\
231 diff0 = filp[-fils+(j)] - srcp[-srcs+(j)];\
232 diff1 = filp[+fils-(j)] - srcp[+srcs-(j)];\
233
234 if (is_edge) {
242 }
243
244
245 if (diff0 + diff1 > 0)
252 }
253 }
254 }
255 }
256
257 for (y = 0; y <
h; y++) {
258 if (!((y ^ mcdeint->parity) & 1)) {
259 for (x = 0; x <
w; x++) {
260 frame_dec->data[
i][x + y*fils] =
261 outpic ->data[
i][x + y*dsts] =
inpic->data[
i][x + y*srcs];
262 }
263 }
264 }
265 }
266 mcdeint->parity ^= 1;
267
268 end:
274 }
276 }
277
279 {
284 },
285 };
286
288 {
291 },
292 };
293
302 .priv_class = &mcdeint_class,
303 };