aboutsummaryrefslogtreecommitdiffhomepage
path: root/libmpcodecs/vf_ivtc.c
blob: b6db4c18a83e32c088009c38e0c7d4b8ff15da3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "mp_msg.h"
#include "cpudetect.h"

#include "img_format.h"
#include "mp_image.h"
#include "vf.h"

#include "libvo/fastmemcpy.h"


struct metrics {
	/* difference: total, even lines, odd lines */
	int d, e, o;
	/* noise: temporal, spacial (current), spacial (past) */
	int t, s, p;
};

struct frameinfo {
	/* peak, relative, mean */
	struct metrics p, r, m;
};

struct vf_priv_s {
	struct frameinfo fi[2];
	mp_image_t *dmpi;
	int first;
	int drop, lastdrop, dropnext;
	int inframes, outframes;
};

enum {
	F_DROP,
	F_MERGE,
	F_NEXT,
	F_SHOW
};

#ifdef HAVE_MMX
static void block_diffs_MMX(struct metrics *m, unsigned char *old, unsigned char *new, int os, int ns)
{
	int i;
	short out[24]; // output buffer for the partial metrics from the mmx code
	
	asm (
		"movl $4, %%ecx \n\t"
		"pxor %%mm4, %%mm4 \n\t" // 4 even difference sums
		"pxor %%mm5, %%mm5 \n\t" // 4 odd difference sums
		"pxor %%mm7, %%mm7 \n\t" // all zeros
		
		ASMALIGN(4)
		"1: \n\t"
		
		// Even difference
		"movq (%%"REG_S"), %%mm0 \n\t"
		"movq (%%"REG_S"), %%mm2 \n\t"
		"add %%"REG_a", %%"REG_S" \n\t"
		"movq (%%"REG_D"), %%mm1 \n\t"
		"add %%"REG_b", %%"REG_D" \n\t"
		"psubusb %%mm1, %%mm2 \n\t"
		"psubusb %%mm0, %%mm1 \n\t"
		"movq %%mm2, %%mm0 \n\t"
		"movq %%mm1, %%mm3 \n\t"
		"punpcklbw %%mm7, %%mm0 \n\t"
		"punpcklbw %%mm7, %%mm1 \n\t"
		"punpckhbw %%mm7, %%mm2 \n\t"
		"punpckhbw %%mm7, %%mm3 \n\t"
		"paddw %%mm0, %%mm4 \n\t"
		"paddw %%mm1, %%mm4 \n\t"
		"paddw %%mm2, %%mm4 \n\t"
		"paddw %%mm3, %%mm4 \n\t"
		
		// Odd difference
		"movq (%%"REG_S"), %%mm0 \n\t"
		"movq (%%"REG_S"), %%mm2 \n\t"
		"add %%"REG_a", %%"REG_S" \n\t"
		"movq (%%"REG_D"), %%mm1 \n\t"
		"add %%"REG_b", %%"REG_D" \n\t"
		"psubusb %%mm1, %%mm2 \n\t"
		"psubusb %%mm0, %%mm1 \n\t"
		"movq %%mm2, %%mm0 \n\t"
		"movq %%mm1, %%mm3 \n\t"
		"punpcklbw %%mm7, %%mm0 \n\t"
		"punpcklbw %%mm7, %%mm1 \n\t"
		"punpckhbw %%mm7, %%mm2 \n\t"
		"punpckhbw %%mm7, %%mm3 \n\t"
		"paddw %%mm0, %%mm5 \n\t"
		"paddw %%mm1, %%mm5 \n\t"
		"paddw %%mm2, %%mm5 \n\t"
		"paddw %%mm3, %%mm5 \n\t"
			
		"decl %%ecx \n\t"
		"jnz 1b \n\t"
		"movq %%mm4, (%%"REG_d") \n\t"
		"movq %%mm5, 8(%%"REG_d") \n\t"
		: 
		: "S" (old), "D" (new), "a" (os), "b" (ns), "d" (out)
		: "memory"
		);
	m->e = out[0]+out[1]+out[2]+out[3];
	m->o = out[4]+out[5]+out[6]+out[7];
	m->d = m->e + m->o;

	asm (
		// First loop to measure first four columns
		"movl $4, %%ecx \n\t"
		"pxor %%mm4, %%mm4 \n\t" // Past spacial noise
		"pxor %%mm5, %%mm5 \n\t" // Temporal noise
		"pxor %%mm6, %%mm6 \n\t" // Current spacial noise
		
		ASMALIGN(4)
		"2: \n\t"
		
		"movq (%%"REG_S"), %%mm0 \n\t"
		"movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
		"add %%"REG_a", %%"REG_S" \n\t"
		"add %%"REG_a", %%"REG_S" \n\t"
		"movq (%%"REG_D"), %%mm2 \n\t"
		"movq (%%"REG_D",%%"REG_b"), %%mm3 \n\t"
		"add %%"REG_b", %%"REG_D" \n\t"
		"add %%"REG_b", %%"REG_D" \n\t"
		"punpcklbw %%mm7, %%mm0 \n\t"
		"punpcklbw %%mm7, %%mm1 \n\t"
		"punpcklbw %%mm7, %%mm2 \n\t"
		"punpcklbw %%mm7, %%mm3 \n\t"
		"paddw %%mm1, %%mm4 \n\t"
		"paddw %%mm1, %%mm5 \n\t"
		"paddw %%mm3, %%mm6 \n\t"
		"psubw %%mm0, %%mm4 \n\t"
		"psubw %%mm2, %%mm5 \n\t"
		"psubw %%mm2, %%mm6 \n\t"
		
		"decl %%ecx \n\t"
		"jnz 2b \n\t"
		
		"movq %%mm0, %%mm1 \n\t"
		"movq %%mm0, %%mm2 \n\t"
		"movq %%mm0, %%mm3 \n\t"
		"pcmpgtw %%mm4, %%mm1 \n\t"
		"pcmpgtw %%mm5, %%mm2 \n\t"
		"pcmpgtw %%mm6, %%mm3 \n\t"
		"pxor %%mm1, %%mm4 \n\t"
		"pxor %%mm2, %%mm5 \n\t"
		"pxor %%mm3, %%mm6 \n\t"
		"psubw %%mm1, %%mm4 \n\t"
		"psubw %%mm2, %%mm5 \n\t"
		"psubw %%mm3, %%mm6 \n\t"
		"movq %%mm4, (%%"REG_d") \n\t"
		"movq %%mm5, 16(%%"REG_d") \n\t"
		"movq %%mm6, 32(%%"REG_d") \n\t"

		"mov %%"REG_a", %%"REG_c" \n\t"
		"shl $3, %%"REG_c" \n\t"
		"sub %%"REG_c", %%"REG_S" \n\t"
		"mov %%"REG_b", %%"REG_c" \n\t"
		"shl $3, %%"REG_c" \n\t"
		"sub %%"REG_c", %%"REG_D" \n\t"

		// Second loop for the last four columns
		"movl $4, %%ecx \n\t"
		"pxor %%mm4, %%mm4 \n\t"
		"pxor %%mm5, %%mm5 \n\t"
		"pxor %%mm6, %%mm6 \n\t"
		
		ASMALIGN(4)
		"3: \n\t"
		
		"movq (%%"REG_S"), %%mm0 \n\t"
		"movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
		"add %%"REG_a", %%"REG_S" \n\t"
		"add %%"REG_a", %%"REG_S" \n\t"
		"movq (%%"REG_D"), %%mm2 \n\t"
		"movq (%%"REG_D",%%"REG_b"), %%mm3 \n\t"
		"add %%"REG_b", %%"REG_D" \n\t"
		"add %%"REG_b", %%"REG_D" \n\t"
		"punpckhbw %%mm7, %%mm0 \n\t"
		"punpckhbw %%mm7, %%mm1 \n\t"
		"punpckhbw %%mm7, %%mm2 \n\t"
		"punpckhbw %%mm7, %%mm3 \n\t"
		"paddw %%mm1, %%mm4 \n\t"
		"paddw %%mm1, %%mm5 \n\t"
		"paddw %%mm3, %%mm6 \n\t"
		"psubw %%mm0, %%mm4 \n\t"
		"psubw %%mm2, %%mm5 \n\t"
		"psubw %%mm2, %%mm6 \n\t"
		
		"decl %%ecx \n\t"
		"jnz 3b \n\t"
		
		"movq %%mm0, %%mm1 \n\t"
		"movq %%mm0, %%mm2 \n\t"
		"movq %%mm0, %%mm3 \n\t"
		"pcmpgtw %%mm4, %%mm1 \n\t"
		"pcmpgtw %%mm5, %%mm2 \n\t"
		"pcmpgtw %%mm6, %%mm3 \n\t"
		"pxor %%mm1, %%mm4 \n\t"
		"pxor %%mm2, %%mm5 \n\t"
		"pxor %%mm3, %%mm6 \n\t"
		"psubw %%mm1, %%mm4 \n\t"
		"psubw %%mm2, %%mm5 \n\t"
		"psubw %%mm3, %%mm6 \n\t"
		"movq %%mm4, 8(%%"REG_d") \n\t"
		"movq %%mm5, 24(%%"REG_d") \n\t"
		"movq %%mm6, 40(%%"REG_d") \n\t"

		"emms \n\t"
		: 
		: "S" (old), "D" (new), "a" ((long)os), "b" ((long)ns), "d" (out)
		: "memory"
		);
	m->p = m->t = m->s = 0;
	for (i=0; i<8; i++) {
		m->p += out[i];
		m->t += out[8+i];
		m->s += out[16+i];
	}
	//printf("e=%d o=%d d=%d p=%d t=%d s=%d\n", m->e, m->o, m->d, m->p, m->t, m->s);
}
#endif

//#define MAG(a) ((a)*(a))
//#define MAG(a) (abs(a))
#define MAG(a) (((a)^((a)>>31))-((a)>>31))

//#define LOWPASS(s) (((s)[-2] + 4*(s)[-1] + 6*(s)[0] + 4*(s)[1] + (s)[2])>>4)
//#define LOWPASS(s) (((s)[-1] + 2*(s)[0] + (s)[1])>>2)
#define LOWPASS(s) ((s)[0])


static void block_diffs_C(struct metrics *m, unsigned char *old, unsigned char *new, int os, int ns)
{
	int x, y, e=0, o=0, s=0, p=0, t=0;
	unsigned char *oldp, *newp;
	m->s = m->p = m->t = 0;
	for (x = 8; x; x--) {
		oldp = old++;
		newp = new++;
		s = p = t = 0;
		for (y = 4; y; y--) {
			e += MAG(newp[0]-oldp[0]);
			o += MAG(newp[ns]-oldp[os]);
			s += newp[ns]-newp[0];
			p += oldp[os]-oldp[0];
			t += oldp[os]-newp[0];
			oldp += os<<1;
			newp += ns<<1;
		}
		m->s += MAG(s);
		m->p += MAG(p);
		m->t += MAG(t);
	}
	m->e = e;
	m->o = o;
	m->d = e+o;
}

static void (*block_diffs)(struct metrics *, unsigned char *, unsigned char *, int, int);

#define MAXUP(a,b) ((a) = ((a)>(b)) ? (a) : (b))

static void diff_planes(struct frameinfo *fi,
	unsigned char *old, unsigned char *new, int w, int h, int os, int ns)
{
	int x, y;
	struct metrics l;
	struct metrics *peak=&fi->p, *rel=&fi->r, *mean=&fi->m;
	memset(peak, 0, sizeof(struct metrics));
	memset(rel, 0, sizeof(struct metrics));
	memset(mean, 0, sizeof(struct metrics));
	for (y = 0; y < h-7; y += 8) {
		for (x = 8; x < w-8-7; x += 8) {
			block_diffs(&l, old+x+y*os, new+x+y*ns, os, ns);
			mean->d += l.d;
			mean->e += l.e;
			mean->o += l.o;
			mean->s += l.s;
			mean->p += l.p;
			mean->t += l.t;
			MAXUP(peak->d, l.d);
			MAXUP(peak->e, l.e);
			MAXUP(peak->o, l.o);
			MAXUP(peak->s, l.s);
			MAXUP(peak->p, l.p);
			MAXUP(peak->t, l.t);
			MAXUP(rel->e, l.e-l.o);
			MAXUP(rel->o, l.o-l.e);
			MAXUP(rel->s, l.s-l.t);
			MAXUP(rel->p, l.p-l.t);
			MAXUP(rel->t, l.t-l.p);
			MAXUP(rel->d, l.t-l.s); /* hack */
		}
	}
	x = (w/8-2)*(h/8);
	mean->d /= x;
	mean->e /= x;
	mean->o /= x;
	mean->s /= x;
	mean->p /= x;
	mean->t /= x;
}

static void diff_fields(struct frameinfo *fi, mp_image_t *old, mp_image_t *new)
{
	diff_planes(fi, old->planes[0], new->planes[0],
		new->w, new->h, old->stride[0], new->stride[0]);
}

static void stats(struct frameinfo *f)
{
	mp_msg(MSGT_VFILTER, MSGL_V, "       pd=%d re=%d ro=%d rp=%d rt=%d rs=%d rd=%d pp=%d pt=%d ps=%d\r",
		f->p.d, f->r.e, f->r.o, f->r.p, f->r.t, f->r.s, f->r.d, f->p.p, f->p.t, f->p.s);
}

static int foo(struct vf_priv_s *p, mp_image_t *new, mp_image_t *cur)
{
	struct frameinfo *f = p->fi;

	f[0] = f[1];
	diff_fields(&f[1], cur, new);
	stats(&f[1]);

	// Immediately drop this frame if it's already been used.
	if (p->dropnext) {
		p->dropnext = 0;
		return F_DROP;
	}
	
	// Sometimes a pulldown frame comes all by itself, so both
	// its top and bottom field are duplicates from the adjacent
	// two frames. We can just drop such a frame, but we
	// immediately show the next frame instead to keep the frame
	// drops evenly spaced during normal 3:2 pulldown sequences.
	if ((3*f[1].r.o < f[1].r.e) && (f[1].r.s < f[1].r.d)) {
		p->dropnext = 1;
		return F_NEXT;
	}
	
	// If none of these conditions hold, we will consider the frame
	// progressive and just show it as-is.
	if (!(  (3*f[0].r.e < f[0].r.o) ||
		((2*f[0].r.d < f[0].r.s) && (f[0].r.s > 1200)) ||
		((2*f[1].r.t < f[1].r.p) && (f[1].r.p > 1200))  ))
		return F_SHOW;

	// Otherwise, we have to decide whether to merge or drop.
	// If the noise metric only increases minimally, we're off
	// to a good start...
	if (((2*f[1].r.t < 3*f[1].r.p) && (f[1].r.t < 3600)) ||
		(f[1].r.t < 900) || (f[1].r.d < 900)) {
		// ...and if noise decreases or the duplicate even field
		// is detected, we go ahead with the merge.
		if ((3*f[0].r.e < f[0].r.o) || (2*f[1].r.t < f[1].r.p)) {
			p->dropnext = 1;
			return F_MERGE;
		}
	}
	return F_DROP;
}



static void copy_image(mp_image_t *dmpi, mp_image_t *mpi, int field)
{
	switch (field) {
	case 0:
		my_memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h/2,
			dmpi->stride[0]*2, mpi->stride[0]*2);
		if (mpi->flags & MP_IMGFLAG_PLANAR) {
			my_memcpy_pic(dmpi->planes[1], mpi->planes[1],
				mpi->chroma_width, mpi->chroma_height/2,
				dmpi->stride[1]*2, mpi->stride[1]*2);
			my_memcpy_pic(dmpi->planes[2], mpi->planes[2],
				mpi->chroma_width, mpi->chroma_height/2,
				dmpi->stride[2]*2, mpi->stride[2]*2);
		}
		break;
	case 1:
		my_memcpy_pic(dmpi->planes[0]+dmpi->stride[0],
			mpi->planes[0]+mpi->stride[0], mpi->w, mpi->h/2,
			dmpi->stride[0]*2, mpi->stride[0]*2);
		if (mpi->flags & MP_IMGFLAG_PLANAR) {
			my_memcpy_pic(dmpi->planes[1]+dmpi->stride[1],
				mpi->planes[1]+mpi->stride[1],
				mpi->chroma_width, mpi->chroma_height/2,
				dmpi->stride[1]*2, mpi->stride[1]*2);
			my_memcpy_pic(dmpi->planes[2]+dmpi->stride[2],
				mpi->planes[2]+mpi->stride[2],
				mpi->chroma_width, mpi->chroma_height/2,
				dmpi->stride[2]*2, mpi->stride[2]*2);
		}
		break;
	case 2:
		memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
			dmpi->stride[0], mpi->stride[0]);
		if (mpi->flags & MP_IMGFLAG_PLANAR) {
			memcpy_pic(dmpi->planes[1], mpi->planes[1],
				mpi->chroma_width, mpi->chroma_height,
				dmpi->stride[1], mpi->stride[1]);
			memcpy_pic(dmpi->planes[2], mpi->planes[2],
				mpi->chroma_width, mpi->chroma_height,
				dmpi->stride[2], mpi->stride[2]);
		}
		break;
	}
}

static int do_put_image(struct vf_instance_s* vf, mp_image_t *dmpi)
{
	struct vf_priv_s *p = vf->priv;
	int dropflag=0;

	if (!p->dropnext) switch (p->drop) {
	case 0:
		dropflag = 0;
		break;
	case 1:
		dropflag = (++p->lastdrop >= 5);
		break;
	case 2:
		dropflag = (++p->lastdrop >= 5) && (4*p->inframes <= 5*p->outframes);
		break;
	}
	
	if (dropflag) {
		//mp_msg(MSGT_VFILTER, MSGL_V, "drop! [%d/%d=%g]\n",
		//	p->outframes, p->inframes, (float)p->outframes/p->inframes);
		mp_msg(MSGT_VFILTER, MSGL_V, "!");
		p->lastdrop = 0;
		return 0;
	}

	p->outframes++;
	return vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
}

static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts)
{
	int ret=0;
	struct vf_priv_s *p = vf->priv;

	p->inframes++;

	if (p->first) { /* hack */
		p->first = 0;
		return 1;
	}

	if (!p->dmpi) p->dmpi = vf_get_image(vf->next, mpi->imgfmt,
		MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
		MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
		mpi->width, mpi->height);
	/* FIXME -- not correct, off by one frame! */
	p->dmpi->qscale = mpi->qscale;
	p->dmpi->qstride = mpi->qstride;
	p->dmpi->qscale_type = mpi->qscale_type;
		
	switch (foo(p, mpi, p->dmpi)) {
	case F_DROP:
		copy_image(p->dmpi, mpi, 2);
		ret = 0;
		p->lastdrop = 0;
		mp_msg(MSGT_VFILTER, MSGL_V, "DROP\n");
		break;
	case F_MERGE:
		copy_image(p->dmpi, mpi, 0);
		ret = do_put_image(vf, p->dmpi);
		copy_image(p->dmpi, mpi, 1);
		mp_msg(MSGT_VFILTER, MSGL_V, "MERGE\n");
		p->dmpi = NULL;
		break;
	case F_NEXT:
		copy_image(p->dmpi, mpi, 2);
		ret = do_put_image(vf, p->dmpi);
		mp_msg(MSGT_VFILTER, MSGL_V, "NEXT\n");
		p->dmpi = NULL;
		break;
	case F_SHOW:
		ret = do_put_image(vf, p->dmpi);
		copy_image(p->dmpi, mpi, 2);
		mp_msg(MSGT_VFILTER, MSGL_V, "OK\n");
		p->dmpi = NULL;
		break;
	}
	return ret;
}

static int query_format(struct vf_instance_s* vf, unsigned int fmt)
{
	switch (fmt) {
	case IMGFMT_YV12:
	case IMGFMT_IYUV:
	case IMGFMT_I420:
		return vf_next_query_format(vf, fmt);
	}
	return 0;
}

static void uninit(struct vf_instance_s* vf)
{
	free(vf->priv);
}

static int open(vf_instance_t *vf, char* args)
{
	struct vf_priv_s *p;
	vf->put_image = put_image;
	vf->query_format = query_format;
	vf->uninit = uninit;
	vf->default_reqs = VFCAP_ACCEPT_STRIDE;
	vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
	p->drop = 0;
	p->first = 1;
	if (args) sscanf(args, "%d", &p->drop);
	block_diffs = block_diffs_C;
#ifdef HAVE_MMX
	if(gCpuCaps.hasMMX) block_diffs = block_diffs_MMX;
#endif
	return 1;
}

const vf_info_t vf_info_ivtc = {
    "inverse telecine, take 2",
    "ivtc",
    "Rich Felker",
    "",
    open,
    NULL
};