Configure needs AS to be set for the Makefiles.
[mplayer/glamo.git] / libmpcodecs / pullup.c
bloba74cb83b20ef47dc781cd0b9cecc421602a6c1df
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "config.h"
7 #include "pullup.h"
8 #include "cpudetect.h"
12 #if ARCH_X86
13 #if HAVE_MMX
14 static int diff_y_mmx(unsigned char *a, unsigned char *b, int s)
16 int ret;
17 __asm__ volatile (
18 "movl $4, %%ecx \n\t"
19 "pxor %%mm4, %%mm4 \n\t"
20 "pxor %%mm7, %%mm7 \n\t"
22 "1: \n\t"
24 "movq (%%"REG_S"), %%mm0 \n\t"
25 "movq (%%"REG_S"), %%mm2 \n\t"
26 "add %%"REG_a", %%"REG_S" \n\t"
27 "movq (%%"REG_D"), %%mm1 \n\t"
28 "add %%"REG_a", %%"REG_D" \n\t"
29 "psubusb %%mm1, %%mm2 \n\t"
30 "psubusb %%mm0, %%mm1 \n\t"
31 "movq %%mm2, %%mm0 \n\t"
32 "movq %%mm1, %%mm3 \n\t"
33 "punpcklbw %%mm7, %%mm0 \n\t"
34 "punpcklbw %%mm7, %%mm1 \n\t"
35 "punpckhbw %%mm7, %%mm2 \n\t"
36 "punpckhbw %%mm7, %%mm3 \n\t"
37 "paddw %%mm0, %%mm4 \n\t"
38 "paddw %%mm1, %%mm4 \n\t"
39 "paddw %%mm2, %%mm4 \n\t"
40 "paddw %%mm3, %%mm4 \n\t"
42 "decl %%ecx \n\t"
43 "jnz 1b \n\t"
45 "movq %%mm4, %%mm3 \n\t"
46 "punpcklwd %%mm7, %%mm4 \n\t"
47 "punpckhwd %%mm7, %%mm3 \n\t"
48 "paddd %%mm4, %%mm3 \n\t"
49 "movd %%mm3, %%eax \n\t"
50 "psrlq $32, %%mm3 \n\t"
51 "movd %%mm3, %%edx \n\t"
52 "addl %%edx, %%eax \n\t"
53 "emms \n\t"
54 : "=a" (ret)
55 : "S" (a), "D" (b), "a" (s)
56 : "%ecx", "%edx"
58 return ret;
61 static int licomb_y_mmx(unsigned char *a, unsigned char *b, int s)
63 int ret;
64 __asm__ volatile (
65 "movl $4, %%ecx \n\t"
66 "pxor %%mm6, %%mm6 \n\t"
67 "pxor %%mm7, %%mm7 \n\t"
68 "sub %%"REG_a", %%"REG_D" \n\t"
70 "2: \n\t"
72 "movq (%%"REG_D"), %%mm0 \n\t"
73 "movq (%%"REG_D"), %%mm1 \n\t"
74 "punpcklbw %%mm7, %%mm0 \n\t"
75 "movq (%%"REG_D",%%"REG_a"), %%mm2 \n\t"
76 "punpcklbw %%mm7, %%mm1 \n\t"
77 "punpcklbw %%mm7, %%mm2 \n\t"
78 "paddw %%mm0, %%mm0 \n\t"
79 "paddw %%mm2, %%mm1 \n\t"
80 "movq %%mm0, %%mm2 \n\t"
81 "psubusw %%mm1, %%mm0 \n\t"
82 "psubusw %%mm2, %%mm1 \n\t"
83 "paddw %%mm0, %%mm6 \n\t"
84 "paddw %%mm1, %%mm6 \n\t"
86 "movq (%%"REG_S"), %%mm0 \n\t"
87 "movq (%%"REG_D"), %%mm1 \n\t"
88 "punpckhbw %%mm7, %%mm0 \n\t"
89 "movq (%%"REG_D",%%"REG_a"), %%mm2 \n\t"
90 "punpckhbw %%mm7, %%mm1 \n\t"
91 "punpckhbw %%mm7, %%mm2 \n\t"
92 "paddw %%mm0, %%mm0 \n\t"
93 "paddw %%mm2, %%mm1 \n\t"
94 "movq %%mm0, %%mm2 \n\t"
95 "psubusw %%mm1, %%mm0 \n\t"
96 "psubusw %%mm2, %%mm1 \n\t"
97 "paddw %%mm0, %%mm6 \n\t"
98 "paddw %%mm1, %%mm6 \n\t"
100 "movq (%%"REG_D",%%"REG_a"), %%mm0 \n\t"
101 "movq (%%"REG_S"), %%mm1 \n\t"
102 "punpcklbw %%mm7, %%mm0 \n\t"
103 "movq (%%"REG_S",%%"REG_a"), %%mm2 \n\t"
104 "punpcklbw %%mm7, %%mm1 \n\t"
105 "punpcklbw %%mm7, %%mm2 \n\t"
106 "paddw %%mm0, %%mm0 \n\t"
107 "paddw %%mm2, %%mm1 \n\t"
108 "movq %%mm0, %%mm2 \n\t"
109 "psubusw %%mm1, %%mm0 \n\t"
110 "psubusw %%mm2, %%mm1 \n\t"
111 "paddw %%mm0, %%mm6 \n\t"
112 "paddw %%mm1, %%mm6 \n\t"
114 "movq (%%"REG_D",%%"REG_a"), %%mm0 \n\t"
115 "movq (%%"REG_S"), %%mm1 \n\t"
116 "punpckhbw %%mm7, %%mm0 \n\t"
117 "movq (%%"REG_S",%%"REG_a"), %%mm2 \n\t"
118 "punpckhbw %%mm7, %%mm1 \n\t"
119 "punpckhbw %%mm7, %%mm2 \n\t"
120 "paddw %%mm0, %%mm0 \n\t"
121 "paddw %%mm2, %%mm1 \n\t"
122 "movq %%mm0, %%mm2 \n\t"
123 "psubusw %%mm1, %%mm0 \n\t"
124 "psubusw %%mm2, %%mm1 \n\t"
125 "paddw %%mm0, %%mm6 \n\t"
126 "paddw %%mm1, %%mm6 \n\t"
128 "add %%"REG_a", %%"REG_S" \n\t"
129 "add %%"REG_a", %%"REG_D" \n\t"
130 "decl %%ecx \n\t"
131 "jnz 2b \n\t"
133 "movq %%mm6, %%mm5 \n\t"
134 "punpcklwd %%mm7, %%mm6 \n\t"
135 "punpckhwd %%mm7, %%mm5 \n\t"
136 "paddd %%mm6, %%mm5 \n\t"
137 "movd %%mm5, %%eax \n\t"
138 "psrlq $32, %%mm5 \n\t"
139 "movd %%mm5, %%edx \n\t"
140 "addl %%edx, %%eax \n\t"
142 "emms \n\t"
143 : "=a" (ret)
144 : "S" (a), "D" (b), "a" (s)
145 : "%ecx", "%edx"
147 return ret;
150 static int var_y_mmx(unsigned char *a, unsigned char *b, int s)
152 int ret;
153 __asm__ volatile (
154 "movl $3, %%ecx \n\t"
155 "pxor %%mm4, %%mm4 \n\t"
156 "pxor %%mm7, %%mm7 \n\t"
158 "1: \n\t"
160 "movq (%%"REG_S"), %%mm0 \n\t"
161 "movq (%%"REG_S"), %%mm2 \n\t"
162 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
163 "add %%"REG_a", %%"REG_S" \n\t"
164 "psubusb %%mm1, %%mm2 \n\t"
165 "psubusb %%mm0, %%mm1 \n\t"
166 "movq %%mm2, %%mm0 \n\t"
167 "movq %%mm1, %%mm3 \n\t"
168 "punpcklbw %%mm7, %%mm0 \n\t"
169 "punpcklbw %%mm7, %%mm1 \n\t"
170 "punpckhbw %%mm7, %%mm2 \n\t"
171 "punpckhbw %%mm7, %%mm3 \n\t"
172 "paddw %%mm0, %%mm4 \n\t"
173 "paddw %%mm1, %%mm4 \n\t"
174 "paddw %%mm2, %%mm4 \n\t"
175 "paddw %%mm3, %%mm4 \n\t"
177 "decl %%ecx \n\t"
178 "jnz 1b \n\t"
180 "movq %%mm4, %%mm3 \n\t"
181 "punpcklwd %%mm7, %%mm4 \n\t"
182 "punpckhwd %%mm7, %%mm3 \n\t"
183 "paddd %%mm4, %%mm3 \n\t"
184 "movd %%mm3, %%eax \n\t"
185 "psrlq $32, %%mm3 \n\t"
186 "movd %%mm3, %%edx \n\t"
187 "addl %%edx, %%eax \n\t"
188 "emms \n\t"
189 : "=a" (ret)
190 : "S" (a), "a" (s)
191 : "%ecx", "%edx"
193 return 4*ret;
195 #endif
196 #endif
198 #define ABS(a) (((a)^((a)>>31))-((a)>>31))
200 static int diff_y(unsigned char *a, unsigned char *b, int s)
202 int i, j, diff=0;
203 for (i=4; i; i--) {
204 for (j=0; j<8; j++) diff += ABS(a[j]-b[j]);
205 a+=s; b+=s;
207 return diff;
210 static int licomb_y(unsigned char *a, unsigned char *b, int s)
212 int i, j, diff=0;
213 for (i=4; i; i--) {
214 for (j=0; j<8; j++)
215 diff += ABS((a[j]<<1) - b[j-s] - b[j])
216 + ABS((b[j]<<1) - a[j] - a[j+s]);
217 a+=s; b+=s;
219 return diff;
222 #if 0
223 static int qpcomb_y(unsigned char *a, unsigned char *b, int s)
225 int i, j, diff=0;
226 for (i=4; i; i--) {
227 for (j=0; j<8; j++)
228 diff += ABS(a[j] - 3*b[j-s] + 3*a[j+s] - b[j]);
229 a+=s; b+=s;
231 return diff;
234 static int licomb_y_test(unsigned char *a, unsigned char *b, int s)
236 int c = licomb_y(a,b,s);
237 int m = licomb_y_mmx(a,b,s);
238 if (c != m) printf("%d != %d\n", c, m);
239 return m;
241 #endif
243 static int var_y(unsigned char *a, unsigned char *b, int s)
245 int i, j, var=0;
246 for (i=3; i; i--) {
247 for (j=0; j<8; j++) {
248 var += ABS(a[j]-a[j+s]);
250 a+=s; b+=s;
252 return 4*var; /* match comb scaling */
263 static void alloc_buffer(struct pullup_context *c, struct pullup_buffer *b)
265 int i;
266 if (b->planes) return;
267 b->planes = calloc(c->nplanes, sizeof(unsigned char *));
268 for (i = 0; i < c->nplanes; i++) {
269 b->planes[i] = malloc(c->h[i]*c->stride[i]);
270 /* Deal with idiotic 128=0 for chroma: */
271 memset(b->planes[i], c->background[i], c->h[i]*c->stride[i]);
275 struct pullup_buffer *pullup_lock_buffer(struct pullup_buffer *b, int parity)
277 if (!b) return 0;
278 if ((parity+1) & 1) b->lock[0]++;
279 if ((parity+1) & 2) b->lock[1]++;
280 return b;
283 void pullup_release_buffer(struct pullup_buffer *b, int parity)
285 if (!b) return;
286 if ((parity+1) & 1) b->lock[0]--;
287 if ((parity+1) & 2) b->lock[1]--;
290 struct pullup_buffer *pullup_get_buffer(struct pullup_context *c, int parity)
292 int i;
294 /* Try first to get the sister buffer for the previous field */
295 if (parity < 2 && c->last && parity != c->last->parity
296 && !c->last->buffer->lock[parity]) {
297 alloc_buffer(c, c->last->buffer);
298 return pullup_lock_buffer(c->last->buffer, parity);
301 /* Prefer a buffer with both fields open */
302 for (i = 0; i < c->nbuffers; i++) {
303 if (c->buffers[i].lock[0]) continue;
304 if (c->buffers[i].lock[1]) continue;
305 alloc_buffer(c, &c->buffers[i]);
306 return pullup_lock_buffer(&c->buffers[i], parity);
309 if (parity == 2) return 0;
311 /* Search for any half-free buffer */
312 for (i = 0; i < c->nbuffers; i++) {
313 if (((parity+1) & 1) && c->buffers[i].lock[0]) continue;
314 if (((parity+1) & 2) && c->buffers[i].lock[1]) continue;
315 alloc_buffer(c, &c->buffers[i]);
316 return pullup_lock_buffer(&c->buffers[i], parity);
319 return 0;
327 static void compute_metric(struct pullup_context *c,
328 struct pullup_field *fa, int pa,
329 struct pullup_field *fb, int pb,
330 int (*func)(unsigned char *, unsigned char *, int), int *dest)
332 unsigned char *a, *b;
333 int x, y;
334 int mp = c->metric_plane;
335 int xstep = c->bpp[mp];
336 int ystep = c->stride[mp]<<3;
337 int s = c->stride[mp]<<1; /* field stride */
338 int w = c->metric_w*xstep;
340 if (!fa->buffer || !fb->buffer) return;
342 /* Shortcut for duplicate fields (e.g. from RFF flag) */
343 if (fa->buffer == fb->buffer && pa == pb) {
344 memset(dest, 0, c->metric_len * sizeof(int));
345 return;
348 a = fa->buffer->planes[mp] + pa * c->stride[mp] + c->metric_offset;
349 b = fb->buffer->planes[mp] + pb * c->stride[mp] + c->metric_offset;
351 for (y = c->metric_h; y; y--) {
352 for (x = 0; x < w; x += xstep) {
353 *dest++ = func(a + x, b + x, s);
355 a += ystep; b += ystep;
363 static void alloc_metrics(struct pullup_context *c, struct pullup_field *f)
365 f->diffs = calloc(c->metric_len, sizeof(int));
366 f->comb = calloc(c->metric_len, sizeof(int));
367 f->var = calloc(c->metric_len, sizeof(int));
368 /* add more metrics here as needed */
371 static struct pullup_field *make_field_queue(struct pullup_context *c, int len)
373 struct pullup_field *head, *f;
374 f = head = calloc(1, sizeof(struct pullup_field));
375 alloc_metrics(c, f);
376 for (; len > 0; len--) {
377 f->next = calloc(1, sizeof(struct pullup_field));
378 f->next->prev = f;
379 f = f->next;
380 alloc_metrics(c, f);
382 f->next = head;
383 head->prev = f;
384 return head;
387 static void check_field_queue(struct pullup_context *c)
389 if (c->head->next == c->first) {
390 struct pullup_field *f = calloc(1, sizeof(struct pullup_field));
391 alloc_metrics(c, f);
392 f->prev = c->head;
393 f->next = c->first;
394 c->head->next = f;
395 c->first->prev = f;
399 void pullup_submit_field(struct pullup_context *c, struct pullup_buffer *b, int parity)
401 struct pullup_field *f;
403 /* Grow the circular list if needed */
404 check_field_queue(c);
406 /* Cannot have two fields of same parity in a row; drop the new one */
407 if (c->last && c->last->parity == parity) return;
409 f = c->head;
410 f->parity = parity;
411 f->buffer = pullup_lock_buffer(b, parity);
412 f->flags = 0;
413 f->breaks = 0;
414 f->affinity = 0;
416 compute_metric(c, f, parity, f->prev->prev, parity, c->diff, f->diffs);
417 compute_metric(c, parity?f->prev:f, 0, parity?f:f->prev, 1, c->comb, f->comb);
418 compute_metric(c, f, parity, f, -1, c->var, f->var);
420 /* Advance the circular list */
421 if (!c->first) c->first = c->head;
422 c->last = c->head;
423 c->head = c->head->next;
426 void pullup_flush_fields(struct pullup_context *c)
428 struct pullup_field *f;
430 for (f = c->first; f && f != c->head; f = f->next) {
431 pullup_release_buffer(f->buffer, f->parity);
432 f->buffer = 0;
434 c->first = c->last = 0;
444 #define F_HAVE_BREAKS 1
445 #define F_HAVE_AFFINITY 2
448 #define BREAK_LEFT 1
449 #define BREAK_RIGHT 2
454 static int queue_length(struct pullup_field *begin, struct pullup_field *end)
456 int count = 1;
457 struct pullup_field *f;
459 if (!begin || !end) return 0;
460 for (f = begin; f != end; f = f->next) count++;
461 return count;
464 static int find_first_break(struct pullup_field *f, int max)
466 int i;
467 for (i = 0; i < max; i++) {
468 if (f->breaks & BREAK_RIGHT || f->next->breaks & BREAK_LEFT)
469 return i+1;
470 f = f->next;
472 return 0;
475 static void compute_breaks(struct pullup_context *c, struct pullup_field *f0)
477 int i;
478 struct pullup_field *f1 = f0->next;
479 struct pullup_field *f2 = f1->next;
480 struct pullup_field *f3 = f2->next;
481 int l, max_l=0, max_r=0;
482 //struct pullup_field *ff;
483 //for (i=0, ff=c->first; ff != f0; i++, ff=ff->next);
485 if (f0->flags & F_HAVE_BREAKS) return;
486 //printf("\n%d: ", i);
487 f0->flags |= F_HAVE_BREAKS;
489 /* Special case when fields are 100% identical */
490 if (f0->buffer == f2->buffer && f1->buffer != f3->buffer) {
491 f2->breaks |= BREAK_RIGHT;
492 return;
494 if (f0->buffer != f2->buffer && f1->buffer == f3->buffer) {
495 f1->breaks |= BREAK_LEFT;
496 return;
499 for (i = 0; i < c->metric_len; i++) {
500 l = f2->diffs[i] - f3->diffs[i];
501 if (l > max_l) max_l = l;
502 if (-l > max_r) max_r = -l;
504 /* Don't get tripped up when differences are mostly quant error */
505 //printf("%d %d\n", max_l, max_r);
506 if (max_l + max_r < 128) return;
507 if (max_l > 4*max_r) f1->breaks |= BREAK_LEFT;
508 if (max_r > 4*max_l) f2->breaks |= BREAK_RIGHT;
511 static void compute_affinity(struct pullup_context *c, struct pullup_field *f)
513 int i;
514 int max_l=0, max_r=0, l;
515 if (f->flags & F_HAVE_AFFINITY) return;
516 f->flags |= F_HAVE_AFFINITY;
517 if (f->buffer == f->next->next->buffer) {
518 f->affinity = 1;
519 f->next->affinity = 0;
520 f->next->next->affinity = -1;
521 f->next->flags |= F_HAVE_AFFINITY;
522 f->next->next->flags |= F_HAVE_AFFINITY;
523 return;
525 if (1) {
526 for (i = 0; i < c->metric_len; i++) {
527 int lv = f->prev->var[i];
528 int rv = f->next->var[i];
529 int v = f->var[i];
530 int lc = f->comb[i] - (v+lv) + ABS(v-lv);
531 int rc = f->next->comb[i] - (v+rv) + ABS(v-rv);
532 lc = lc>0 ? lc : 0;
533 rc = rc>0 ? rc : 0;
534 l = lc - rc;
535 if (l > max_l) max_l = l;
536 if (-l > max_r) max_r = -l;
538 if (max_l + max_r < 64) return;
539 if (max_r > 6*max_l) f->affinity = -1;
540 else if (max_l > 6*max_r) f->affinity = 1;
541 } else {
542 for (i = 0; i < c->metric_len; i++) {
543 l = f->comb[i] - f->next->comb[i];
544 if (l > max_l) max_l = l;
545 if (-l > max_r) max_r = -l;
547 if (max_l + max_r < 64) return;
548 if (max_r > 2*max_l) f->affinity = -1;
549 else if (max_l > 2*max_r) f->affinity = 1;
553 static void foo(struct pullup_context *c)
555 struct pullup_field *f = c->first;
556 int i, n = queue_length(f, c->last);
557 for (i = 0; i < n-1; i++) {
558 if (i < n-3) compute_breaks(c, f);
559 compute_affinity(c, f);
560 f = f->next;
564 static int decide_frame_length(struct pullup_context *c)
566 struct pullup_field *f0 = c->first;
567 struct pullup_field *f1 = f0->next;
568 struct pullup_field *f2 = f1->next;
569 int l;
571 if (queue_length(c->first, c->last) < 4) return 0;
572 foo(c);
574 if (f0->affinity == -1) return 1;
576 l = find_first_break(f0, 3);
577 if (l == 1 && c->strict_breaks < 0) l = 0;
579 switch (l) {
580 case 1:
581 if (c->strict_breaks < 1 && f0->affinity == 1 && f1->affinity == -1)
582 return 2;
583 else return 1;
584 case 2:
585 /* FIXME: strictly speaking, f0->prev is no longer valid... :) */
586 if (c->strict_pairs
587 && (f0->prev->breaks & BREAK_RIGHT) && (f2->breaks & BREAK_LEFT)
588 && (f0->affinity != 1 || f1->affinity != -1) )
589 return 1;
590 if (f1->affinity == 1) return 1;
591 else return 2;
592 case 3:
593 if (f2->affinity == 1) return 2;
594 else return 3;
595 default:
596 /* 9 possibilities covered before switch */
597 if (f1->affinity == 1) return 1; /* covers 6 */
598 else if (f1->affinity == -1) return 2; /* covers 6 */
599 else if (f2->affinity == -1) { /* covers 2 */
600 if (f0->affinity == 1) return 3;
601 else return 1;
603 else return 2; /* the remaining 6 */
608 static void print_aff_and_breaks(struct pullup_context *c, struct pullup_field *f)
610 int i;
611 struct pullup_field *f0 = f;
612 const char aff_l[] = "+..", aff_r[] = "..+";
613 printf("\naffinity: ");
614 for (i = 0; i < 4; i++) {
615 printf("%c%d%c", aff_l[1+f->affinity], i, aff_r[1+f->affinity]);
616 f = f->next;
618 f = f0;
619 printf("\nbreaks: ");
620 for (i=0; i<4; i++) {
621 printf("%c%d%c", f->breaks & BREAK_LEFT ? '|' : '.', i, f->breaks & BREAK_RIGHT ? '|' : '.');
622 f = f->next;
624 printf("\n");
631 struct pullup_frame *pullup_get_frame(struct pullup_context *c)
633 int i;
634 struct pullup_frame *fr = c->frame;
635 int n = decide_frame_length(c);
636 int aff = c->first->next->affinity;
638 if (!n) return 0;
639 if (fr->lock) return 0;
641 if (c->verbose) {
642 print_aff_and_breaks(c, c->first);
643 printf("duration: %d \n", n);
646 fr->lock++;
647 fr->length = n;
648 fr->parity = c->first->parity;
649 fr->buffer = 0;
650 for (i = 0; i < n; i++) {
651 /* We cheat and steal the buffer without release+relock */
652 fr->ifields[i] = c->first->buffer;
653 c->first->buffer = 0;
654 c->first = c->first->next;
657 if (n == 1) {
658 fr->ofields[fr->parity] = fr->ifields[0];
659 fr->ofields[fr->parity^1] = 0;
660 } else if (n == 2) {
661 fr->ofields[fr->parity] = fr->ifields[0];
662 fr->ofields[fr->parity^1] = fr->ifields[1];
663 } else if (n == 3) {
664 if (aff == 0)
665 aff = (fr->ifields[0] == fr->ifields[1]) ? -1 : 1;
666 /* else if (c->verbose) printf("forced aff: %d \n", aff); */
667 fr->ofields[fr->parity] = fr->ifields[1+aff];
668 fr->ofields[fr->parity^1] = fr->ifields[1];
670 pullup_lock_buffer(fr->ofields[0], 0);
671 pullup_lock_buffer(fr->ofields[1], 1);
673 if (fr->ofields[0] == fr->ofields[1]) {
674 fr->buffer = fr->ofields[0];
675 pullup_lock_buffer(fr->buffer, 2);
676 return fr;
678 return fr;
681 static void copy_field(struct pullup_context *c, struct pullup_buffer *dest,
682 struct pullup_buffer *src, int parity)
684 int i, j;
685 unsigned char *d, *s;
686 for (i = 0; i < c->nplanes; i++) {
687 s = src->planes[i] + parity*c->stride[i];
688 d = dest->planes[i] + parity*c->stride[i];
689 for (j = c->h[i]>>1; j; j--) {
690 memcpy(d, s, c->stride[i]);
691 s += c->stride[i]<<1;
692 d += c->stride[i]<<1;
697 void pullup_pack_frame(struct pullup_context *c, struct pullup_frame *fr)
699 int i;
700 if (fr->buffer) return;
701 if (fr->length < 2) return; /* FIXME: deal with this */
702 for (i = 0; i < 2; i++)
704 if (fr->ofields[i]->lock[i^1]) continue;
705 fr->buffer = fr->ofields[i];
706 pullup_lock_buffer(fr->buffer, 2);
707 copy_field(c, fr->buffer, fr->ofields[i^1], i^1);
708 return;
710 fr->buffer = pullup_get_buffer(c, 2);
711 copy_field(c, fr->buffer, fr->ofields[0], 0);
712 copy_field(c, fr->buffer, fr->ofields[1], 1);
715 void pullup_release_frame(struct pullup_frame *fr)
717 int i;
718 for (i = 0; i < fr->length; i++)
719 pullup_release_buffer(fr->ifields[i], fr->parity ^ (i&1));
720 pullup_release_buffer(fr->ofields[0], 0);
721 pullup_release_buffer(fr->ofields[1], 1);
722 if (fr->buffer) pullup_release_buffer(fr->buffer, 2);
723 fr->lock--;
731 struct pullup_context *pullup_alloc_context(void)
733 struct pullup_context *c;
735 c = calloc(1, sizeof(struct pullup_context));
737 return c;
740 void pullup_preinit_context(struct pullup_context *c)
742 c->bpp = calloc(c->nplanes, sizeof(int));
743 c->w = calloc(c->nplanes, sizeof(int));
744 c->h = calloc(c->nplanes, sizeof(int));
745 c->stride = calloc(c->nplanes, sizeof(int));
746 c->background = calloc(c->nplanes, sizeof(int));
749 void pullup_init_context(struct pullup_context *c)
751 int mp = c->metric_plane;
752 if (c->nbuffers < 10) c->nbuffers = 10;
753 c->buffers = calloc(c->nbuffers, sizeof (struct pullup_buffer));
755 c->metric_w = (c->w[mp] - ((c->junk_left + c->junk_right) << 3)) >> 3;
756 c->metric_h = (c->h[mp] - ((c->junk_top + c->junk_bottom) << 1)) >> 3;
757 c->metric_offset = c->junk_left*c->bpp[mp] + (c->junk_top<<1)*c->stride[mp];
758 c->metric_len = c->metric_w * c->metric_h;
760 c->head = make_field_queue(c, 8);
762 c->frame = calloc(1, sizeof (struct pullup_frame));
763 c->frame->ifields = calloc(3, sizeof (struct pullup_buffer *));
765 switch(c->format) {
766 case PULLUP_FMT_Y:
767 c->diff = diff_y;
768 c->comb = licomb_y;
769 c->var = var_y;
770 #if ARCH_X86
771 #if HAVE_MMX
772 if (c->cpu & PULLUP_CPU_MMX) {
773 c->diff = diff_y_mmx;
774 c->comb = licomb_y_mmx;
775 c->var = var_y_mmx;
777 #endif
778 #endif
779 /* c->comb = qpcomb_y; */
780 break;
781 #if 0
782 case PULLUP_FMT_YUY2:
783 c->diff = diff_yuy2;
784 break;
785 case PULLUP_FMT_RGB32:
786 c->diff = diff_rgb32;
787 break;
788 #endif
792 void pullup_free_context(struct pullup_context *c)
794 struct pullup_field *f;
795 free(c->buffers);
796 f = c->head;
797 do {
798 if (!f) break;
799 free(f->diffs);
800 free(f->comb);
801 f = f->next;
802 free(f->prev);
803 } while (f != c->head);
804 free(c->frame);
805 free(c);