comparison libmpcodecs/pullup.c @ 10664:d47ca466c97b

pullup -- third generation inverse telecine engine. the backend (pullup.[ch]) is not mplayer-specific and is designed to work well with g2; vf_pullup.c is the g1 wrapper. see man page for details, and keep in mind, this is a work in progress.
author rfelker
date Mon, 18 Aug 2003 15:24:08 +0000
parents
children 67449e5936f3
comparison
equal deleted inserted replaced
10663:711159267b2d 10664:d47ca466c97b
1
2
3 #include <stdlib.h>
4 #include "pullup.h"
5
6
7
8
9 #ifdef HAVE_MMX
10 static int diff_y_mmx(unsigned char *a, unsigned char *b, int s)
11 {
12 int ret;
13 asm (
14 "movl $4, %%ecx \n\t"
15 "pxor %%mm4, %%mm4 \n\t"
16 "pxor %%mm7, %%mm7 \n\t"
17
18 ".balign 16 \n\t"
19 "1: \n\t"
20
21 "movq (%%esi), %%mm0 \n\t"
22 "movq (%%esi), %%mm2 \n\t"
23 "addl %%eax, %%esi \n\t"
24 "movq (%%edi), %%mm1 \n\t"
25 "addl %%eax, %%edi \n\t"
26 "psubusb %%mm1, %%mm2 \n\t"
27 "psubusb %%mm0, %%mm1 \n\t"
28 "movq %%mm2, %%mm0 \n\t"
29 "movq %%mm1, %%mm3 \n\t"
30 "punpcklbw %%mm7, %%mm0 \n\t"
31 "punpcklbw %%mm7, %%mm1 \n\t"
32 "punpckhbw %%mm7, %%mm2 \n\t"
33 "punpckhbw %%mm7, %%mm3 \n\t"
34 "paddw %%mm0, %%mm4 \n\t"
35 "paddw %%mm1, %%mm4 \n\t"
36 "paddw %%mm2, %%mm4 \n\t"
37 "paddw %%mm3, %%mm4 \n\t"
38
39 "decl %%ecx \n\t"
40 "jnz fb \n\t"
41
42 "movq %%mm4, %%mm3 \n\t"
43 "punpcklwl %%mm7, %%mm4 \n\t"
44 "punpckhwl %%mm7, %%mm3 \n\t"
45 "paddl %%mm4, %%mm3 \n\t"
46 "movq %%mm3, %%mm2 \n\t"
47 "punpckllq %%mm7, %%mm3 \n\t"
48 "punpckhlq %%mm7, %%mm2 \n\t"
49 "paddl %%mm3, %%mm2 \n\t"
50 "movl %%mm2, %eax"
51
52 "emms \n\t"
53 : "=a" (ret)
54 : "S" (a), "D" (b), "a" (s)
55 :
56 );
57 return ret;
58 }
59 #endif
60
61 #define ABS(a) (((a)^((a)>>31))-((a)>>31))
62
63 static int diff_y(unsigned char *a, unsigned char *b, int s)
64 {
65 int i, j, diff=0;
66 for (i=4; i; i--) {
67 for (j=0; j<8; j++) diff += ABS(a[j]-b[j]);
68 a+=s; b+=s;
69 }
70 return diff;
71 }
72
73 static int licomb_y(unsigned char *a, unsigned char *b, int s)
74 {
75 int i, j, diff=0;
76 for (i=8; i; i--) {
77 for (j=0; j<8; j++)
78 diff += ABS((a[j]<<1) - b[j-s] - b[j])
79 + ABS((b[j]<<1) - a[j] - a[j+s]);
80 a+=s; b+=s;
81 }
82 return diff;
83 }
84
85
86
87
88
89
90
91
92
93 static void alloc_buffer(struct pullup_context *c, struct pullup_buffer *b)
94 {
95 int i;
96 if (b->planes) return;
97 b->planes = calloc(c->nplanes, sizeof(unsigned char *));
98 for (i = 0; i < c->nplanes; i++) {
99 b->planes[i] = malloc(c->h[i]*c->stride[i]);
100 /* Deal with idiotic 128=0 for chroma: */
101 memset(b->planes[i], c->background[i], c->h[i]*c->stride[i]);
102 }
103 }
104
105 struct pullup_buffer *pullup_lock_buffer(struct pullup_buffer *b, int parity)
106 {
107 if (parity+1 & 1) b->lock[0]++;
108 if (parity+1 & 2) b->lock[1]++;
109 return b;
110 }
111
112 void pullup_release_buffer(struct pullup_buffer *b, int parity)
113 {
114 if (parity+1 & 1) b->lock[0]--;
115 if (parity+1 & 2) b->lock[1]--;
116 }
117
118 struct pullup_buffer *pullup_get_buffer(struct pullup_context *c, int parity)
119 {
120 int i;
121
122 /* Try first to get the sister buffer for the previous field */
123 if (parity < 2 && c->last && parity != c->last->parity
124 && !c->last->buffer->lock[parity]) {
125 alloc_buffer(c, c->last->buffer);
126 return pullup_lock_buffer(c->last->buffer, parity);
127 }
128
129 /* Prefer a buffer with both fields open */
130 for (i = 0; i < c->nbuffers; i++) {
131 if (c->buffers[i].lock[0]) continue;
132 if (c->buffers[i].lock[1]) continue;
133 alloc_buffer(c, &c->buffers[i]);
134 return pullup_lock_buffer(&c->buffers[i], parity);
135 }
136
137 if (parity == 2) return 0;
138
139 /* Search for any half-free buffer */
140 for (i = 0; i < c->nbuffers; i++) {
141 if (parity+1 & 1 && c->buffers[i].lock[0]) continue;
142 if (parity+1 & 2 && c->buffers[i].lock[1]) continue;
143 alloc_buffer(c, &c->buffers[i]);
144 return pullup_lock_buffer(&c->buffers[i], parity);
145 }
146
147 return 0;
148 }
149
150
151
152
153
154
155 static void compute_metric(struct pullup_context *c,
156 struct pullup_field *fa, int pa,
157 struct pullup_field *fb, int pb,
158 int (*func)(unsigned char *, unsigned char *, int), int *dest)
159 {
160 unsigned char *a, *b;
161 int x, y;
162 int xstep = c->bpp[0];
163 int ystep = c->stride[0]<<3;
164 int s = c->stride[0]<<1; /* field stride */
165 int w = c->metric_w*xstep;
166
167 if (!fa->buffer || !fb->buffer) return;
168
169 /* Shortcut for duplicate fields (e.g. from RFF flag) */
170 if (fa->buffer == fb->buffer && pa == pb) {
171 memset(dest, 0, c->metric_len * sizeof(int));
172 return;
173 }
174
175 a = fa->buffer->planes[0] + pa * c->stride[0] + c->metric_offset;
176 b = fb->buffer->planes[0] + pb * c->stride[0] + c->metric_offset;
177
178 for (y = c->metric_h; y; y--) {
179 for (x = 0; x < w; x += xstep) {
180 *dest++ = func(a + x, b + x, s);
181 }
182 a += ystep; b += ystep;
183 }
184 }
185
186
187
188
189
190 static void alloc_metrics(struct pullup_context *c, struct pullup_field *f)
191 {
192 f->diffs = calloc(c->metric_len, sizeof(int));
193 f->licomb = calloc(c->metric_len, sizeof(int));
194 /* add more metrics here as needed */
195 }
196
197 static struct pullup_field *make_field_queue(struct pullup_context *c, int len)
198 {
199 struct pullup_field *head, *f;
200 f = head = calloc(1, sizeof(struct pullup_field));
201 alloc_metrics(c, f);
202 for (; len > 0; len--) {
203 f->next = calloc(1, sizeof(struct pullup_field));
204 f->next->prev = f;
205 f = f->next;
206 alloc_metrics(c, f);
207 }
208 f->next = head;
209 head->prev = f;
210 return head;
211 }
212
213 static void check_field_queue(struct pullup_context *c)
214 {
215 if (c->head->next == c->first) {
216 struct pullup_field *f = calloc(1, sizeof(struct pullup_field));
217 alloc_metrics(c, f);
218 f->prev = c->head;
219 f->next = c->first;
220 c->head->next = f;
221 c->first->prev = f;
222 }
223 }
224
225 int pullup_submit_field(struct pullup_context *c, struct pullup_buffer *b, int parity)
226 {
227 struct pullup_field *f;
228
229 /* Grow the circular list if needed */
230 check_field_queue(c);
231
232 /* Cannot have two fields of same parity in a row; drop the new one */
233 if (c->last && c->last->parity == parity) return 0;
234
235 f = c->head;
236 f->parity = parity;
237 f->buffer = pullup_lock_buffer(b, parity);
238 f->flags = 0;
239 f->breaks = 0;
240 f->affinity = 0;
241
242 compute_metric(c, f, parity, f->prev->prev, parity, c->diff, f->diffs);
243 compute_metric(c, parity?f->prev:f, 0, parity?f:f->prev, 1, c->licomb, f->licomb);
244
245 /* Advance the circular list */
246 if (!c->first) c->first = c->head;
247 c->last = c->head;
248 c->head = c->head->next;
249 }
250
251 void pullup_flush_fields(struct pullup_context *c)
252 {
253 struct pullup_field *f;
254
255 for (f = c->first; f && f != c->head; f = f->next) {
256 pullup_release_buffer(f->buffer, f->parity);
257 f->buffer = 0;
258 }
259 c->first = c->last = 0;
260 }
261
262
263
264
265
266
267
268
269 #define F_HAVE_BREAKS 1
270 #define F_HAVE_AFFINITY 2
271
272
273 #define BREAK_LEFT 1
274 #define BREAK_RIGHT 2
275
276
277
278
279 static int queue_length(struct pullup_field *begin, struct pullup_field *end)
280 {
281 int count = 1;
282 struct pullup_field *f;
283
284 if (!begin || !end) return 0;
285 for (f = begin; f != end; f = f->next) count++;
286 return count;
287 }
288
289 static int find_first_break(struct pullup_field *f, int max)
290 {
291 int i;
292 for (i = 0; i < max; i++) {
293 if (f->breaks & BREAK_RIGHT || f->next->breaks & BREAK_LEFT)
294 return i+1;
295 f = f->next;
296 }
297 return 0;
298 }
299
300 static void compute_breaks(struct pullup_context *c, struct pullup_field *f0)
301 {
302 int i;
303 struct pullup_field *f1 = f0->next;
304 struct pullup_field *f2 = f1->next;
305 struct pullup_field *f3 = f2->next;
306 int l, max_l=0, max_r=0;
307
308 if (f0->flags & F_HAVE_BREAKS) return;
309 f0->flags |= F_HAVE_BREAKS;
310
311 /* Special case when fields are 100% identical */
312 if (f0->buffer == f2->buffer && f1->buffer != f3->buffer) {
313 f0->breaks |= BREAK_LEFT;
314 f2->breaks |= BREAK_RIGHT;
315 return;
316 }
317
318 for (i = 0; i < c->metric_len; i++) {
319 l = f2->diffs[i] - f3->diffs[i];
320 if (l > max_l) max_l = l;
321 if (-l > max_r) max_r = -l;
322 }
323 /* Don't get tripped up when differences are mostly quant error */
324 if (max_l + max_r < 64) return;
325 if (max_l > 4*max_r) f1->breaks |= BREAK_LEFT;
326 if (max_r > 4*max_l) f2->breaks |= BREAK_RIGHT;
327 //printf("max_l=%d max_r=%d\n", max_l, max_r);
328 }
329
330 static void compute_affinity(struct pullup_context *c, struct pullup_field *f)
331 {
332 int i;
333 int max_l=0, max_r=0, l;
334 if (f->flags & F_HAVE_AFFINITY) return;
335 f->flags |= F_HAVE_AFFINITY;
336 for (i = 0; i < c->metric_len; i++) {
337 l = f->licomb[i] - f->next->licomb[i];
338 if (l > max_l) max_l = l;
339 if (-l > max_r) max_r = -l;
340 }
341 if (max_l + max_r < 64) return;
342 if (max_r > 3*max_l) f->affinity = -1;
343 else if (max_l > 3*max_r) f->affinity = 1;
344 }
345
346 static void foo(struct pullup_context *c)
347 {
348 struct pullup_field *f = c->first;
349 int i, n = queue_length(f, c->last);
350 for (i = 0; i < n; i++) {
351 if (i < n-3) compute_breaks(c, f);
352 compute_affinity(c, f);
353 f = f->next;
354 }
355 }
356
357 static int decide_frame_length(struct pullup_context *c)
358 {
359 int n;
360 struct pullup_field *f0 = c->first;
361 struct pullup_field *f1 = f0->next;
362 struct pullup_field *f2 = f1->next;
363 struct pullup_field *f3 = f2->next;
364 struct pullup_field *f4 = f3->next;
365 struct pullup_field *f5 = f4->next;
366
367 if (queue_length(c->first, c->last) < 6) return 0;
368 foo(c);
369
370 n = find_first_break(f0, 3);
371
372 switch (n) {
373 case 1:
374 return 1;
375 case 2:
376 if (f0->affinity == -1 || f1->affinity == 1) return 1;
377 else return 2;
378 case 3:
379 if (f1->affinity == -1 && f2->affinity != -1) return 2;
380 else if (f1->affinity == 1 && f0->affinity != 1) return 1;
381 else return 3;
382 default:
383 if (f0->affinity == -1 && f1->affinity != -1) return 1;
384 else if (f1->affinity == 1 && f2->affinity == -1) return 1;
385 else return 2;
386 }
387 }
388
389
390 static void print_aff_and_breaks(struct pullup_context *c, struct pullup_field *f)
391 {
392 int i;
393 int max_l, max_r, l;
394 struct pullup_field *f0 = f;
395 const char aff_l[] = "+..", aff_r[] = "..+";
396 printf("\naffinity: ");
397 for (i = 0; i < 6; i++) {
398 printf("%c%d%c", aff_l[1+f->affinity], i, aff_r[1+f->affinity]);
399 f = f->next;
400 }
401 f = f0;
402 printf("\nbreaks: ");
403 for (i=0; i<6; i++) {
404 printf("%c%d%c", f->breaks & BREAK_LEFT ? '|' : '.', i, f->breaks & BREAK_RIGHT ? '|' : '.');
405 f = f->next;
406 }
407 printf("\n");
408 }
409
410
411
412
413
414 struct pullup_frame *pullup_get_frame(struct pullup_context *c)
415 {
416 int i;
417 struct pullup_frame *fr = c->frame;
418 int n = decide_frame_length(c);
419
420 if (!n) return 0;
421 if (fr->lock) return 0;
422
423 print_aff_and_breaks(c, c->first);
424 printf("duration: %d \n", n);
425
426 fr->lock++;
427 fr->length = n;
428 fr->parity = c->first->parity;
429 fr->buffer = 0;
430 for (i = 0; i < n; i++) {
431 /* We cheat and steal the buffer without release+relock */
432 fr->fields[i] = c->first->buffer;
433 c->first->buffer = 0;
434 c->first = c->first->next;
435 }
436 /* Export the entire frame as one buffer, if possible! */
437 if (n == 2 && fr->fields[0] == fr->fields[1]) {
438 fr->buffer = fr->fields[0];
439 pullup_lock_buffer(fr->buffer, 2);
440 return fr;
441 }
442 /* (loop is in case we ever support frames longer than 3 fields) */
443 for (i = 1; i < n-1; i++) {
444 if (fr->fields[i] == fr->fields[i-1]
445 || fr->fields[i] == fr->fields[i+1]) {
446 fr->buffer = fr->fields[i];
447 pullup_lock_buffer(fr->buffer, 2);
448 break;
449 }
450 }
451 return fr;
452 }
453
454 static void copy_field(struct pullup_context *c, struct pullup_buffer *dest,
455 struct pullup_buffer *src, int parity)
456 {
457 int i, j;
458 unsigned char *d, *s;
459 for (i = 0; i < c->nplanes; i++) {
460 s = src->planes[i] + parity*c->stride[i];
461 d = dest->planes[i] + parity*c->stride[i];
462 for (j = c->h[i]>>1; j; j--) {
463 memcpy(d, s, c->stride[i]);
464 s += c->stride[i]<<1;
465 d += c->stride[i]<<1;
466 }
467 }
468 }
469
470 void pullup_pack_frame(struct pullup_context *c, struct pullup_frame *fr)
471 {
472 int i;
473 int par = fr->parity;
474 if (fr->buffer) return;
475 if (fr->length < 2) return; /* FIXME: deal with this */
476 for (i = 0; i < fr->length; i++)
477 {
478 if (fr->fields[i]->lock[par ^ (i&1) ^ 1]) continue;
479 fr->buffer = fr->fields[i];
480 pullup_lock_buffer(fr->buffer, 2);
481 copy_field(c, fr->buffer, fr->fields[i+(i>0?-1:1)], par^(i&1)^1);
482 return;
483 }
484 fr->buffer = pullup_get_buffer(c, 2);
485 copy_field(c, fr->buffer, fr->fields[0], par);
486 copy_field(c, fr->buffer, fr->fields[1], par^1);
487 }
488
489 void pullup_release_frame(struct pullup_frame *fr)
490 {
491 int i;
492 for (i = 0; i < fr->length; i++)
493 pullup_release_buffer(fr->fields[i], fr->parity ^ (i&1));
494 if (fr->buffer) pullup_release_buffer(fr->buffer, 2);
495 fr->lock--;
496 }
497
498
499
500
501
502
503 struct pullup_context *pullup_alloc_context()
504 {
505 struct pullup_context *c;
506
507 c = calloc(1, sizeof(struct pullup_context));
508
509 return c;
510 }
511
512 void pullup_preinit_context(struct pullup_context *c)
513 {
514 c->bpp = calloc(c->nplanes, sizeof(int));
515 c->w = calloc(c->nplanes, sizeof(int));
516 c->h = calloc(c->nplanes, sizeof(int));
517 c->stride = calloc(c->nplanes, sizeof(int));
518 c->background = calloc(c->nplanes, sizeof(int));
519 }
520
521 void pullup_init_context(struct pullup_context *c)
522 {
523 if (c->nbuffers < 10) c->nbuffers = 10;
524 c->buffers = calloc(c->nbuffers, sizeof (struct pullup_buffer));
525
526 c->metric_w = (c->w[0] - (c->junk_left + c->junk_right << 3)) >> 3;
527 c->metric_h = (c->h[0] - (c->junk_top + c->junk_bottom << 1)) >> 3;
528 c->metric_offset = c->junk_left*c->bpp[0] + (c->junk_top<<1)*c->stride[0];
529 c->metric_len = c->metric_w * c->metric_h;
530
531 c->head = make_field_queue(c, 8);
532
533 c->frame = calloc(1, sizeof (struct pullup_frame));
534 c->frame->fields = calloc(3, sizeof (struct pullup_buffer *));
535
536 switch(c->format) {
537 case PULLUP_FMT_Y:
538 c->diff = diff_y;
539 c->licomb = licomb_y;
540 #ifdef HAVE_MMX
541 if (c->cpu & PULLUP_CPU_MMX) c->diff = diff_y_mmx;
542 #endif
543 break;
544 #if 0
545 case PULLUP_FMT_YUY2:
546 c->diff = diff_yuy2;
547 break;
548 case PULLUP_FMT_RGB32:
549 c->diff = diff_rgb32;
550 break;
551 #endif
552 }
553 }
554
555 void pullup_free_context(struct pullup_context *c)
556 {
557 /* FIXME: free! */
558 }
559
560
561
562
563
564
565
566
567