MythTV  master
pullup.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) MPlayer Project license GPL v2
3 */
4 
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "pullup.h"
10 #include "config.h"
11 #include "../mm_arch.h"
12 
13 
14 
15 #if HAVE_MMX
16 static int diff_y_mmx(const unsigned char *a, const unsigned char *b, int s)
17 {
18  int ret;
19  __asm__ volatile (
20  "movl $4, %%ecx \n\t"
21  "pxor %%mm4, %%mm4 \n\t"
22  "pxor %%mm7, %%mm7 \n\t"
23 
24  ASMALIGN(4)
25  "1: \n\t"
26 
27  "movq (%%"FF_REG_S"), %%mm0 \n\t"
28  "movq (%%"FF_REG_S"), %%mm2 \n\t"
29  "add %%"FF_REG_a", %%"FF_REG_S" \n\t"
30  "movq (%%"FF_REG_D"), %%mm1 \n\t"
31  "add %%"FF_REG_a", %%"FF_REG_D" \n\t"
32  "psubusb %%mm1, %%mm2 \n\t"
33  "psubusb %%mm0, %%mm1 \n\t"
34  "movq %%mm2, %%mm0 \n\t"
35  "movq %%mm1, %%mm3 \n\t"
36  "punpcklbw %%mm7, %%mm0 \n\t"
37  "punpcklbw %%mm7, %%mm1 \n\t"
38  "punpckhbw %%mm7, %%mm2 \n\t"
39  "punpckhbw %%mm7, %%mm3 \n\t"
40  "paddw %%mm0, %%mm4 \n\t"
41  "paddw %%mm1, %%mm4 \n\t"
42  "paddw %%mm2, %%mm4 \n\t"
43  "paddw %%mm3, %%mm4 \n\t"
44 
45  "decl %%ecx \n\t"
46  "jnz 1b \n\t"
47 
48  "movq %%mm4, %%mm3 \n\t"
49  "punpcklwd %%mm7, %%mm4 \n\t"
50  "punpckhwd %%mm7, %%mm3 \n\t"
51  "paddd %%mm4, %%mm3 \n\t"
52  "movd %%mm3, %%eax \n\t"
53  "psrlq $32, %%mm3 \n\t"
54  "movd %%mm3, %%edx \n\t"
55  "addl %%edx, %%eax \n\t"
56  "emms \n\t"
57  : "=a" (ret)
58  : "S" (a), "D" (b), "a" (s)
59  : "%ecx", "%edx"
60  );
61  return ret;
62 }
63 
64 static int licomb_y_mmx(const unsigned char *a, const unsigned char *b, int s)
65 {
66  int ret;
67  __asm__ volatile (
68  "movl $4, %%ecx \n\t"
69  "pxor %%mm6, %%mm6 \n\t"
70  "pxor %%mm7, %%mm7 \n\t"
71  "sub %%"FF_REG_a", %%"FF_REG_D" \n\t"
72 
73  ASMALIGN(4)
74  "2: \n\t"
75 
76  "movq (%%"FF_REG_D"), %%mm0 \n\t"
77  "movq (%%"FF_REG_D"), %%mm1 \n\t"
78  "punpcklbw %%mm7, %%mm0 \n\t"
79  "movq (%%"FF_REG_D",%%"FF_REG_a"), %%mm2 \n\t"
80  "punpcklbw %%mm7, %%mm1 \n\t"
81  "punpcklbw %%mm7, %%mm2 \n\t"
82  "paddw %%mm0, %%mm0 \n\t"
83  "paddw %%mm2, %%mm1 \n\t"
84  "movq %%mm0, %%mm2 \n\t"
85  "psubusw %%mm1, %%mm0 \n\t"
86  "psubusw %%mm2, %%mm1 \n\t"
87  "paddw %%mm0, %%mm6 \n\t"
88  "paddw %%mm1, %%mm6 \n\t"
89 
90  "movq (%%"FF_REG_S"), %%mm0 \n\t"
91  "movq (%%"FF_REG_D"), %%mm1 \n\t"
92  "punpckhbw %%mm7, %%mm0 \n\t"
93  "movq (%%"FF_REG_D",%%"FF_REG_a"), %%mm2 \n\t"
94  "punpckhbw %%mm7, %%mm1 \n\t"
95  "punpckhbw %%mm7, %%mm2 \n\t"
96  "paddw %%mm0, %%mm0 \n\t"
97  "paddw %%mm2, %%mm1 \n\t"
98  "movq %%mm0, %%mm2 \n\t"
99  "psubusw %%mm1, %%mm0 \n\t"
100  "psubusw %%mm2, %%mm1 \n\t"
101  "paddw %%mm0, %%mm6 \n\t"
102  "paddw %%mm1, %%mm6 \n\t"
103 
104  "movq (%%"FF_REG_D",%%"FF_REG_a"), %%mm0 \n\t"
105  "movq (%%"FF_REG_S"), %%mm1 \n\t"
106  "punpcklbw %%mm7, %%mm0 \n\t"
107  "movq (%%"FF_REG_S",%%"FF_REG_a"), %%mm2 \n\t"
108  "punpcklbw %%mm7, %%mm1 \n\t"
109  "punpcklbw %%mm7, %%mm2 \n\t"
110  "paddw %%mm0, %%mm0 \n\t"
111  "paddw %%mm2, %%mm1 \n\t"
112  "movq %%mm0, %%mm2 \n\t"
113  "psubusw %%mm1, %%mm0 \n\t"
114  "psubusw %%mm2, %%mm1 \n\t"
115  "paddw %%mm0, %%mm6 \n\t"
116  "paddw %%mm1, %%mm6 \n\t"
117 
118  "movq (%%"FF_REG_D",%%"FF_REG_a"), %%mm0 \n\t"
119  "movq (%%"FF_REG_S"), %%mm1 \n\t"
120  "punpckhbw %%mm7, %%mm0 \n\t"
121  "movq (%%"FF_REG_S",%%"FF_REG_a"), %%mm2 \n\t"
122  "punpckhbw %%mm7, %%mm1 \n\t"
123  "punpckhbw %%mm7, %%mm2 \n\t"
124  "paddw %%mm0, %%mm0 \n\t"
125  "paddw %%mm2, %%mm1 \n\t"
126  "movq %%mm0, %%mm2 \n\t"
127  "psubusw %%mm1, %%mm0 \n\t"
128  "psubusw %%mm2, %%mm1 \n\t"
129  "paddw %%mm0, %%mm6 \n\t"
130  "paddw %%mm1, %%mm6 \n\t"
131 
132  "add %%"FF_REG_a", %%"FF_REG_S" \n\t"
133  "add %%"FF_REG_a", %%"FF_REG_D" \n\t"
134  "decl %%ecx \n\t"
135  "jnz 2b \n\t"
136 
137  "movq %%mm6, %%mm5 \n\t"
138  "punpcklwd %%mm7, %%mm6 \n\t"
139  "punpckhwd %%mm7, %%mm5 \n\t"
140  "paddd %%mm6, %%mm5 \n\t"
141  "movd %%mm5, %%eax \n\t"
142  "psrlq $32, %%mm5 \n\t"
143  "movd %%mm5, %%edx \n\t"
144  "addl %%edx, %%eax \n\t"
145 
146  "emms \n\t"
147  : "=a" (ret)
148  : "S" (a), "D" (b), "a" (s)
149  : "%ecx", "%edx"
150  );
151  return ret;
152 }
153 
154 static int var_y_mmx(const unsigned char *a, const unsigned char *b, int s)
155 {
156  (void) b;
157  int ret;
158  __asm__ volatile (
159  "movl $3, %%ecx \n\t"
160  "pxor %%mm4, %%mm4 \n\t"
161  "pxor %%mm7, %%mm7 \n\t"
162 
163  ASMALIGN(4)
164  "1: \n\t"
165 
166  "movq (%%"FF_REG_S"), %%mm0 \n\t"
167  "movq (%%"FF_REG_S"), %%mm2 \n\t"
168  "movq (%%"FF_REG_S",%%"FF_REG_a"), %%mm1 \n\t"
169  "add %%"FF_REG_a", %%"FF_REG_S" \n\t"
170  "psubusb %%mm1, %%mm2 \n\t"
171  "psubusb %%mm0, %%mm1 \n\t"
172  "movq %%mm2, %%mm0 \n\t"
173  "movq %%mm1, %%mm3 \n\t"
174  "punpcklbw %%mm7, %%mm0 \n\t"
175  "punpcklbw %%mm7, %%mm1 \n\t"
176  "punpckhbw %%mm7, %%mm2 \n\t"
177  "punpckhbw %%mm7, %%mm3 \n\t"
178  "paddw %%mm0, %%mm4 \n\t"
179  "paddw %%mm1, %%mm4 \n\t"
180  "paddw %%mm2, %%mm4 \n\t"
181  "paddw %%mm3, %%mm4 \n\t"
182 
183  "decl %%ecx \n\t"
184  "jnz 1b \n\t"
185 
186  "movq %%mm4, %%mm3 \n\t"
187  "punpcklwd %%mm7, %%mm4 \n\t"
188  "punpckhwd %%mm7, %%mm3 \n\t"
189  "paddd %%mm4, %%mm3 \n\t"
190  "movd %%mm3, %%eax \n\t"
191  "psrlq $32, %%mm3 \n\t"
192  "movd %%mm3, %%edx \n\t"
193  "addl %%edx, %%eax \n\t"
194  "emms \n\t"
195  : "=a" (ret)
196  : "S" (a), "a" (s)
197  : "%ecx", "%edx"
198  );
199  return 4*ret;
200 }
201 #endif
202 
203 #define ABS(a) (((a)^((a)>>31))-((a)>>31))
204 
205 static int diff_y(const unsigned char *a, const unsigned char *b, int s)
206 {
207  int i, j, diff=0;
208  for (i=4; i; i--) {
209  for (j=0; j<8; j++)
210  diff += ABS(a[j]-b[j]);
211  a+=s; b+=s;
212  }
213  return diff;
214 }
215 
216 static int licomb_y(const unsigned char *a, const unsigned char *b, int s)
217 {
218  int i, j, diff=0;
219  for (i=4; i; i--) {
220  for (j=0; j<8; j++)
221  diff += ABS((a[j]<<1) - b[j-s] - b[j])
222  + ABS((b[j]<<1) - a[j] - a[j+s]);
223  a+=s; b+=s;
224  }
225  return diff;
226 }
227 
228 static int qpcomb_y(const unsigned char *a, const unsigned char *b, int s)
229  __attribute__ ((unused)); /* <- to suppress compiler warning */
230 
231 static int qpcomb_y(const unsigned char *a, const unsigned char *b, int s)
232 {
233  int i, j, diff=0;
234  for (i=4; i; i--) {
235  for (j=0; j<8; j++)
236  diff += ABS(a[j] - 3*b[j-s] + 3*a[j+s] - b[j]);
237  a+=s; b+=s;
238  }
239  return diff;
240 }
241 
242 #if 0
243 static int licomb_y_test(const unsigned char *a, const unsigned char *b, int s)
244 {
245  int c = licomb_y(a,b,s);
246  int m = licomb_y_mmx(a,b,s);
247  if (c != m) printf("%d != %d\n", c, m);
248  return m;
249 }
250 #endif
251 
252 static int var_y(const unsigned char *a, const unsigned char *b, int s)
253 {
254  int i, j, var=0;
255  for (i=3; i; i--) {
256  for (j=0; j<8; j++) {
257  var += ABS(a[j]-a[j+s]);
258  }
259  a+=s; b+=s;
260  }
261  return 4*var; /* match comb scaling */
262 }
263 
264 
265 
266 
267 
268 
269 
270 
271 
272 static void alloc_buffer(struct pullup_context *c, struct pullup_buffer *b)
273 {
274  int i;
275  if (b->planes) return;
276  b->planes = calloc(c->nplanes, sizeof(unsigned char *));
277  for (i = 0; i < c->nplanes; i++) {
278  b->planes[i] = malloc(c->h[i]*c->stride[i]);
279  /* Deal with idiotic 128=0 for chroma: */
280  memset(b->planes[i], c->background[i], c->h[i]*c->stride[i]);
281  }
282 }
283 
284 struct pullup_buffer *pullup_lock_buffer(struct pullup_buffer *b, int parity)
285 {
286  if (!b) return 0;
287  if ((parity+1) & 1) b->lock[0]++;
288  if ((parity+1) & 2) b->lock[1]++;
289  return b;
290 }
291 
292 void pullup_release_buffer(struct pullup_buffer *b, int parity)
293 {
294  if (!b) return;
295  if ((parity+1) & 1) b->lock[0]--;
296  if ((parity+1) & 2) b->lock[1]--;
297 }
298 
299 struct pullup_buffer *pullup_get_buffer(struct pullup_context *c, int parity)
300 {
301  int i;
302 
303  /* Try first to get the sister buffer for the previous field */
304  if (parity < 2 && c->last && parity != c->last->parity
305  && !c->last->buffer->lock[parity]) {
306  alloc_buffer(c, c->last->buffer);
307  return pullup_lock_buffer(c->last->buffer, parity);
308  }
309 
310  /* Prefer a buffer with both fields open */
311  for (i = 0; i < c->nbuffers; i++) {
312  if (c->buffers[i].lock[0]) continue;
313  if (c->buffers[i].lock[1]) continue;
314  alloc_buffer(c, &c->buffers[i]);
315  return pullup_lock_buffer(&c->buffers[i], parity);
316  }
317 
318  if (parity == 2) return 0;
319 
320  /* Search for any half-free buffer */
321  for (i = 0; i < c->nbuffers; i++) {
322  if (((parity+1) & 1) && c->buffers[i].lock[0]) continue;
323  if (((parity+1) & 2) && c->buffers[i].lock[1]) continue;
324  alloc_buffer(c, &c->buffers[i]);
325  return pullup_lock_buffer(&c->buffers[i], parity);
326  }
327 
328  return 0;
329 }
330 
331 
332 
333 
334 
335 
336 static void compute_metric(struct pullup_context *c,
337  struct pullup_field *fa, int pa,
338  struct pullup_field *fb, int pb,
339  int (*func)(const unsigned char *, const unsigned char *, int), int *dest)
340 {
341  unsigned char *a, *b;
342  int x, y;
343  int mp = c->metric_plane;
344  int xstep = c->bpp[mp];
345  int ystep = c->stride[mp]<<3;
346  int s = c->stride[mp]<<1; /* field stride */
347  int w = c->metric_w*xstep;
348 
349  if (!fa->buffer || !fb->buffer) return;
350 
351  /* Shortcut for duplicate fields (e.g. from RFF flag) */
352  if (fa->buffer == fb->buffer && pa == pb) {
353  memset(dest, 0, c->metric_len * sizeof(int));
354  return;
355  }
356 
357  a = fa->buffer->planes[mp] + pa * c->stride[mp] + c->metric_offset;
358  b = fb->buffer->planes[mp] + pb * c->stride[mp] + c->metric_offset;
359 
360  for (y = c->metric_h; y; y--) {
361  for (x = 0; x < w; x += xstep) {
362  *dest++ = func(a + x, b + x, s);
363  }
364  a += ystep; b += ystep;
365  }
366 }
367 
368 
369 
370 
371 
372 static void alloc_metrics(struct pullup_context *c, struct pullup_field *f)
373 {
374  f->diffs = calloc(c->metric_len, sizeof(int));
375  f->comb = calloc(c->metric_len, sizeof(int));
376  f->var = calloc(c->metric_len, sizeof(int));
377  /* add more metrics here as needed */
378 }
379 
380 static struct pullup_field *make_field_queue(struct pullup_context *c, int len)
381 {
382  struct pullup_field *head, *f;
383  f = head = calloc(1, sizeof(struct pullup_field));
384  alloc_metrics(c, f);
385  for (; len > 0; len--) {
386  f->next = calloc(1, sizeof(struct pullup_field));
387  f->next->prev = f;
388  f = f->next;
389  alloc_metrics(c, f);
390  }
391  f->next = head;
392  head->prev = f;
393  return head;
394 }
395 
396 static void check_field_queue(struct pullup_context *c)
397 {
398  if (c->head->next == c->first) {
399  struct pullup_field *f = calloc(1, sizeof(struct pullup_field));
400  alloc_metrics(c, f);
401  f->prev = c->head;
402  f->next = c->first;
403  c->head->next = f;
404  c->first->prev = f;
405  }
406 }
407 
409 {
410  struct pullup_field *f;
411 
412  /* Grow the circular list if needed */
414 
415  /* Cannot have two fields of same parity in a row; drop the new one */
416  if (c->last && c->last->parity == parity) return;
417 
418  f = c->head;
419  f->parity = parity;
421  f->flags = 0;
422  f->breaks = 0;
423  f->affinity = 0;
424 
425  compute_metric(c, f, parity, f->prev->prev, parity, c->diff, f->diffs);
426  compute_metric(c, parity?f->prev:f, 0, parity?f:f->prev, 1, c->comb, f->comb);
427  compute_metric(c, f, parity, f, -1, c->var, f->var);
428 
429  /* Advance the circular list */
430  if (!c->first) c->first = c->head;
431  c->last = c->head;
432  c->head = c->head->next;
433 }
434 
436 {
437  struct pullup_field *f;
438 
439  for (f = c->first; f && f != c->head; f = f->next) {
441  f->buffer = 0;
442  }
443  c->first = c->last = 0;
444 }
445 
446 
447 
448 
449 
450 
451 
452 
453 #define F_HAVE_BREAKS 1
454 #define F_HAVE_AFFINITY 2
455 
456 
457 #define BREAK_LEFT 1
458 #define BREAK_RIGHT 2
459 
460 
461 
462 
463 static int queue_length(struct pullup_field *begin, struct pullup_field *end)
464 {
465  int count = 1;
466  struct pullup_field *f;
467 
468  if (!begin || !end) return 0;
469  for (f = begin; f != end; f = f->next) count++;
470  return count;
471 }
472 
473 static int find_first_break(struct pullup_field *f, int max)
474 {
475  int i;
476  for (i = 0; i < max; i++) {
477  if (f->breaks & BREAK_RIGHT || f->next->breaks & BREAK_LEFT)
478  return i+1;
479  f = f->next;
480  }
481  return 0;
482 }
483 
484 static void compute_breaks(struct pullup_context *c, struct pullup_field *f0)
485 {
486  struct pullup_field *f1 = f0->next;
487  struct pullup_field *f2 = f1->next;
488  struct pullup_field *f3 = f2->next;
489  int max_l=0, max_r=0;
490  //struct pullup_field *ff;
491  //for (i=0, ff=c->first; ff != f0; i++, ff=ff->next);
492 
493  if (f0->flags & F_HAVE_BREAKS) return;
494  //printf("\n%d: ", i);
495  f0->flags |= F_HAVE_BREAKS;
496 
497  /* Special case when fields are 100% identical */
498  if (f0->buffer == f2->buffer && f1->buffer != f3->buffer) {
499  f2->breaks |= BREAK_RIGHT;
500  return;
501  }
502  if (f0->buffer != f2->buffer && f1->buffer == f3->buffer) {
503  f1->breaks |= BREAK_LEFT;
504  return;
505  }
506 
507  for (int i = 0; i < c->metric_len; i++) {
508  int l = f2->diffs[i] - f3->diffs[i];
509  if (l > max_l) max_l = l;
510  if (-l > max_r) max_r = -l;
511  }
512  /* Don't get tripped up when differences are mostly quant error */
513  //printf("%d %d\n", max_l, max_r);
514  if (max_l + max_r < 128) return;
515  if (max_l > 4*max_r) f1->breaks |= BREAK_LEFT;
516  if (max_r > 4*max_l) f2->breaks |= BREAK_RIGHT;
517 }
518 
519 static void compute_affinity(struct pullup_context *c, struct pullup_field *f)
520 {
521  int i;
522  int max_l=0, max_r=0, l;
523  if (f->flags & F_HAVE_AFFINITY) return;
524  f->flags |= F_HAVE_AFFINITY;
525  if (f->buffer == f->next->next->buffer) {
526  f->affinity = 1;
527  f->next->affinity = 0;
528  f->next->next->affinity = -1;
529  f->next->flags |= F_HAVE_AFFINITY;
530  f->next->next->flags |= F_HAVE_AFFINITY;
531  return;
532  }
533  if (1) {
534  for (i = 0; i < c->metric_len; i++) {
535  int lv = f->prev->var[i];
536  int rv = f->next->var[i];
537  int v = f->var[i];
538  int lc = f->comb[i] - (v+lv) + ABS(v-lv);
539  int rc = f->next->comb[i] - (v+rv) + ABS(v-rv);
540  lc = lc>0 ? lc : 0;
541  rc = rc>0 ? rc : 0;
542  l = lc - rc;
543  if (l > max_l) max_l = l;
544  if (-l > max_r) max_r = -l;
545  }
546  if (max_l + max_r < 64) return;
547  if (max_r > 6*max_l) f->affinity = -1;
548  else if (max_l > 6*max_r) f->affinity = 1;
549  } else {
550  for (i = 0; i < c->metric_len; i++) {
551  l = f->comb[i] - f->next->comb[i];
552  if (l > max_l) max_l = l;
553  if (-l > max_r) max_r = -l;
554  }
555  if (max_l + max_r < 64) return;
556  if (max_r > 2*max_l) f->affinity = -1;
557  else if (max_l > 2*max_r) f->affinity = 1;
558  }
559 }
560 
561 static void foo(struct pullup_context *c)
562 {
563  struct pullup_field *f = c->first;
564  int i, n = queue_length(f, c->last);
565  for (i = 0; i < n-1; i++) {
566  if (i < n-3) compute_breaks(c, f);
567  compute_affinity(c, f);
568  f = f->next;
569  }
570 }
571 
572 static int decide_frame_length(struct pullup_context *c)
573 {
574  struct pullup_field *f0 = c->first;
575  struct pullup_field *f1 = f0->next;
576  struct pullup_field *f2 = f1->next;
577  /*struct pullup_field *f3 = f2->next;*/
578  int l;
579 
580  if (queue_length(c->first, c->last) < 4) return 0;
581  foo(c);
582 
583  if (f0->affinity == -1) return 1;
584 
585  l = find_first_break(f0, 3);
586  if (l == 1 && c->strict_breaks < 0) l = 0;
587 
588  switch (l) {
589  case 1:
590  if (c->strict_breaks < 1 && f0->affinity == 1 && f1->affinity == -1)
591  return 2;
592  else return 1;
593  case 2:
594  /* FIXME: strictly speaking, f0->prev is no longer valid... :) */
595  if (c->strict_pairs
596  && (f0->prev->breaks & BREAK_RIGHT) && (f2->breaks & BREAK_LEFT)
597  && (f0->affinity != 1 || f1->affinity != -1) )
598  return 1;
599  if (f1->affinity == 1) return 1;
600  return 2;
601  case 3:
602  if (f2->affinity == 1) return 2;
603  else return 3;
604  default:
605  /* 9 possibilities covered before switch */
606  if (f1->affinity == 1) return 1; /* covers 6 */
607  else if (f1->affinity == -1) return 2; /* covers 6 */
608  else if (f2->affinity == -1) { /* covers 2 */
609  if (f0->affinity == 1) return 3;
610  return 1;
611  }
612  else return 2; /* the remaining 6 */
613  }
614 }
615 
616 
617 static void print_aff_and_breaks(struct pullup_context *c, struct pullup_field *f)
618 {
619  int i;
620  struct pullup_field *f0 = f;
621  const char aff_l[] = "+..", aff_r[] = "..+";
622 
623  (void) c;
624 
625  printf("\naffinity: ");
626  for (i = 0; i < 4; i++) {
627  printf("%c%d%c", aff_l[1+f->affinity], i, aff_r[1+f->affinity]);
628  f = f->next;
629  }
630  f = f0;
631  printf("\nbreaks: ");
632  for (i=0; i<4; i++) {
633  printf("%c%d%c",
634  (f->breaks & BREAK_LEFT) ? '|' : '.',
635  i,
636  (f->breaks & BREAK_RIGHT) ? '|' : '.');
637  f = f->next;
638  }
639  printf("\n");
640 }
641 
642 
643 
644 
645 
647 {
648  int i;
649  struct pullup_frame *fr = c->frame;
650  int n = decide_frame_length(c);
651  int aff = c->first->next->affinity;
652 
653  if (!n) return 0;
654  if (fr->lock) return 0;
655 
656  if (c->verbose) {
658  printf("duration: %d \n", n);
659  }
660 
661  fr->lock++;
662  fr->length = n;
663  fr->parity = c->first->parity;
664  fr->buffer = 0;
665  for (i = 0; i < n; i++) {
666  /* We cheat and steal the buffer without release+relock */
667  fr->ifields[i] = c->first->buffer;
668  c->first->buffer = 0;
669  c->first = c->first->next;
670  }
671 
672  if (n == 1) {
673  fr->ofields[fr->parity] = fr->ifields[0];
674  fr->ofields[fr->parity^1] = 0;
675  } else if (n == 2) {
676  fr->ofields[fr->parity] = fr->ifields[0];
677  fr->ofields[fr->parity^1] = fr->ifields[1];
678  } else if (n == 3) {
679  if (aff == 0)
680  aff = (fr->ifields[0] == fr->ifields[1]) ? -1 : 1;
681  /* else if (c->verbose) printf("forced aff: %d \n", aff); */
682  fr->ofields[fr->parity] = fr->ifields[1+aff];
683  fr->ofields[fr->parity^1] = fr->ifields[1];
684  }
685  pullup_lock_buffer(fr->ofields[0], 0);
686  pullup_lock_buffer(fr->ofields[1], 1);
687 
688  if (fr->ofields[0] == fr->ofields[1]) {
689  fr->buffer = fr->ofields[0];
690  pullup_lock_buffer(fr->buffer, 2);
691  return fr;
692  }
693  return fr;
694 }
695 
696 static void copy_field(struct pullup_context *c, struct pullup_buffer *dest,
697  struct pullup_buffer *src, int parity)
698 {
699  for (int i = 0; i < c->nplanes; i++) {
700  unsigned char *s = src->planes[i] + parity*c->stride[i];
701  unsigned char *d = dest->planes[i] + parity*c->stride[i];
702  for (int j = c->h[i]>>1; j; j--) {
703  memcpy(d, s, c->stride[i]);
704  s += c->stride[i]<<1;
705  d += c->stride[i]<<1;
706  }
707  }
708 }
709 
710 void pullup_pack_frame(struct pullup_context *c, struct pullup_frame *fr)
711 {
712  int i;
713  /*int par = fr->parity;*/
714  if (fr->buffer) return;
715  if (fr->length < 2) return; /* FIXME: deal with this */
716  for (i = 0; i < 2; i++)
717  {
718  if (fr->ofields[i]->lock[i^1]) continue;
719  fr->buffer = fr->ofields[i];
720  pullup_lock_buffer(fr->buffer, 2);
721  copy_field(c, fr->buffer, fr->ofields[i^1], i^1);
722  return;
723  }
724  fr->buffer = pullup_get_buffer(c, 2);
725  if (!fr->buffer) return;
726  copy_field(c, fr->buffer, fr->ofields[0], 0);
727  copy_field(c, fr->buffer, fr->ofields[1], 1);
728 }
729 
731 {
732  int i;
733  for (i = 0; i < fr->length; i++)
734  pullup_release_buffer(fr->ifields[i], fr->parity ^ (i&1));
735  pullup_release_buffer(fr->ofields[0], 0);
736  pullup_release_buffer(fr->ofields[1], 1);
737  if (fr->buffer) pullup_release_buffer(fr->buffer, 2);
738  fr->lock--;
739 }
740 
741 
742 
743 
744 
745 
747 {
748  struct pullup_context *c;
749 
750  c = calloc(1, sizeof(struct pullup_context));
751 
752  return c;
753 }
754 
756 {
757  c->bpp = calloc(c->nplanes, sizeof(int));
758  c->w = calloc(c->nplanes, sizeof(int));
759  c->h = calloc(c->nplanes, sizeof(int));
760  c->stride = calloc(c->nplanes, sizeof(int));
761  c->background = calloc(c->nplanes, sizeof(int));
762 }
763 
765 {
766  int mp = c->metric_plane;
767  if (c->nbuffers < 10) c->nbuffers = 10;
768  c->buffers = calloc(c->nbuffers, sizeof (struct pullup_buffer));
769 
770  c->metric_w = (c->w[mp] - ((c->junk_left + c->junk_right) << 3)) >> 3;
771  c->metric_h = (c->h[mp] - ((c->junk_top + c->junk_bottom) << 1)) >> 3;
772  c->metric_offset = c->junk_left*c->bpp[mp] + (c->junk_top<<1)*c->stride[mp];
773  c->metric_len = c->metric_w * c->metric_h;
774 
775  c->head = make_field_queue(c, 8);
776 
777  c->frame = calloc(1, sizeof (struct pullup_frame));
778  c->frame->ifields = calloc(3, sizeof (struct pullup_buffer *));
779 
780  switch(c->format) {
781  case PULLUP_FMT_Y:
782  c->diff = diff_y;
783  c->comb = licomb_y;
784  c->var = var_y;
785 #if HAVE_MMX
786  if (c->cpu & PULLUP_CPU_MMX) {
787  c->diff = diff_y_mmx;
788  c->comb = licomb_y_mmx;
789  c->var = var_y_mmx;
790  }
791 #endif
792  /* c->comb = qpcomb_y; */
793  break;
794 #if 0
795  case PULLUP_FMT_YUY2:
796  c->diff = diff_yuy2;
797  break;
798  case PULLUP_FMT_RGB32:
799  c->diff = diff_rgb32;
800  break;
801 #endif
802  }
803 }
804 
806 {
807  struct pullup_field *f;
808  free(c->buffers);
809  f = c->head;
810  do {
811  free(f->diffs);
812  free(f->comb);
813  free(f->var);
814  f = f->next;
815  free(f->prev);
816  } while (f != c->head);
817  free(c->frame->ifields);
818  free(c->frame);
819  free(c->stride);
820  free(c->bpp);
821  free(c->w);
822  free(c->h);
823  free(c->background);
824  free(c);
825 }
826 
827 
828 
829 
830 
831 
832 
833 
834 
static void check_field_queue(struct pullup_context *c)
Definition: pullup.c:396
int breaks
Definition: pullup.h:29
struct pullup_field * head
Definition: pullup.h:59
int metric_offset
Definition: pullup.h:65
int * background
Definition: pullup.h:51
static void compute_metric(struct pullup_context *c, struct pullup_field *fa, int pa, struct pullup_field *fb, int pb, int(*func)(const unsigned char *, const unsigned char *, int), int *dest)
Definition: pullup.c:336
int junk_bottom
Definition: pullup.h:53
struct exc__state * last
Definition: pxsup2dast.c:98
static int qpcomb_y(const unsigned char *a, const unsigned char *b, int s) __attribute__((unused))
Definition: pullup.c:231
int format
Definition: pullup.h:49
static void compute_breaks(struct pullup_context *c, struct pullup_field *f0)
Definition: pullup.c:484
#define PULLUP_CPU_MMX
Definition: pullup.h:6
int metric_h
Definition: pullup.h:65
struct pullup_buffer * buffer
Definition: pullup.h:43
void pullup_pack_frame(struct pullup_context *c, struct pullup_frame *fr)
Definition: pullup.c:710
struct pullup_buffer ** ifields
Definition: pullup.h:42
struct pullup_field * first
Definition: pullup.h:59
int * h
Definition: pullup.h:51
struct pullup_frame * frame
Definition: pullup.h:66
int nbuffers
Definition: pullup.h:61
static void foo(struct pullup_context *c)
Definition: pullup.c:561
int metric_plane
Definition: pullup.h:55
struct pullup_field * prev
Definition: pullup.h:34
static int queue_length(struct pullup_field *begin, struct pullup_field *end)
Definition: pullup.c:463
int * var
Definition: pullup.h:33
static int diff_y(const unsigned char *a, const unsigned char *b, int s)
Definition: pullup.c:205
int parity
Definition: pullup.h:26
void pullup_release_buffer(struct pullup_buffer *b, int parity)
Definition: pullup.c:292
static void alloc_buffer(struct pullup_context *c, struct pullup_buffer *b)
Definition: pullup.c:272
unsigned char b
Definition: ParseText.cpp:329
int metric_w
Definition: pullup.h:65
#define BREAK_LEFT
Definition: pullup.c:457
static int decide_frame_length(struct pullup_context *c)
Definition: pullup.c:572
int parity
Definition: pullup.h:41
unsigned char ** planes
Definition: pullup.h:21
int(* var)(const unsigned char *, const unsigned char *, int)
Definition: pullup.h:64
int metric_len
Definition: pullup.h:65
struct pullup_buffer * pullup_get_buffer(struct pullup_context *c, int parity)
Definition: pullup.c:299
struct pullup_context * pullup_alloc_context(void)
Definition: pullup.c:746
struct pullup_buffer * ofields[2]
Definition: pullup.h:42
static const uint16_t * d
int lock[2]
Definition: pullup.h:20
#define F_HAVE_BREAKS
Definition: pullup.c:453
int length
Definition: pullup.h:40
void pullup_free_context(struct pullup_context *c)
Definition: pullup.c:805
struct pullup_frame * pullup_get_frame(struct pullup_context *c)
Definition: pullup.c:646
int junk_top
Definition: pullup.h:53
int verbose
Definition: pullup.h:54
int * bpp
Definition: pullup.h:51
static void print_aff_and_breaks(struct pullup_context *c, struct pullup_field *f)
Definition: pullup.c:617
int strict_breaks
Definition: pullup.h:56
struct pullup_buffer * buffer
Definition: pullup.h:27
unsigned int cpu
Definition: pullup.h:52
static void alloc_metrics(struct pullup_context *c, struct pullup_field *f)
Definition: pullup.c:372
int junk_left
Definition: pullup.h:53
static int find_first_break(struct pullup_field *f, int max)
Definition: pullup.c:473
#define F_HAVE_AFFINITY
Definition: pullup.c:454
static struct pullup_field * make_field_queue(struct pullup_context *c, int len)
Definition: pullup.c:380
#define ABS(a)
Definition: pullup.c:203
int * diffs
Definition: pullup.h:31
void pullup_init_context(struct pullup_context *c)
Definition: pullup.c:764
void pullup_submit_field(struct pullup_context *c, struct pullup_buffer *b, int parity)
Definition: pullup.c:408
void pullup_release_frame(struct pullup_frame *fr)
Definition: pullup.c:730
int * w
Definition: pullup.h:51
int(* comb)(const unsigned char *, const unsigned char *, int)
Definition: pullup.h:63
int lock
Definition: pullup.h:39
void pullup_flush_fields(struct pullup_context *c)
Definition: pullup.c:435
struct pullup_buffer * pullup_lock_buffer(struct pullup_buffer *b, int parity)
Definition: pullup.c:284
#define PULLUP_FMT_Y
Definition: pullup.h:13
unsigned int flags
Definition: pullup.h:28
#define PULLUP_FMT_RGB32
Definition: pullup.h:16
int * comb
Definition: pullup.h:32
static void copy_field(struct pullup_context *c, struct pullup_buffer *dest, struct pullup_buffer *src, int parity)
Definition: pullup.c:696
#define BREAK_RIGHT
Definition: pullup.c:458
struct pullup_field * last
Definition: pullup.h:59
int nplanes
Definition: pullup.h:50
static int var_y(const unsigned char *a, const unsigned char *b, int s)
Definition: pullup.c:252
static int licomb_y(const unsigned char *a, const unsigned char *b, int s)
Definition: pullup.c:216
int(* diff)(const unsigned char *, const unsigned char *, int)
Definition: pullup.h:62
static void compute_affinity(struct pullup_context *c, struct pullup_field *f)
Definition: pullup.c:519
int * stride
Definition: pullup.h:51
void pullup_preinit_context(struct pullup_context *c)
Definition: pullup.c:755
struct pullup_buffer * buffers
Definition: pullup.h:60
int strict_pairs
Definition: pullup.h:57
#define PULLUP_FMT_YUY2
Definition: pullup.h:14
int junk_right
Definition: pullup.h:53
int affinity
Definition: pullup.h:30
struct pullup_field * next
Definition: pullup.h:34