pullup.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /*
  2. * This file is part of MPlayer.
  3. *
  4. * MPlayer is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * MPlayer is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with MPlayer; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "config.h"
  22. #include "pullup.h"
  23. #include "cpudetect.h"
  24. #if ARCH_X86
  25. #if HAVE_MMX
  26. static int diff_y_mmx(unsigned char *a, unsigned char *b, int s)
  27. {
  28. int ret;
  29. __asm__ volatile (
  30. "movl $4, %%ecx \n\t"
  31. "pxor %%mm4, %%mm4 \n\t"
  32. "pxor %%mm7, %%mm7 \n\t"
  33. "1: \n\t"
  34. "movq (%%"REG_S"), %%mm0 \n\t"
  35. "movq (%%"REG_S"), %%mm2 \n\t"
  36. "add %%"REG_a", %%"REG_S" \n\t"
  37. "movq (%%"REG_D"), %%mm1 \n\t"
  38. "add %%"REG_a", %%"REG_D" \n\t"
  39. "psubusb %%mm1, %%mm2 \n\t"
  40. "psubusb %%mm0, %%mm1 \n\t"
  41. "movq %%mm2, %%mm0 \n\t"
  42. "movq %%mm1, %%mm3 \n\t"
  43. "punpcklbw %%mm7, %%mm0 \n\t"
  44. "punpcklbw %%mm7, %%mm1 \n\t"
  45. "punpckhbw %%mm7, %%mm2 \n\t"
  46. "punpckhbw %%mm7, %%mm3 \n\t"
  47. "paddw %%mm0, %%mm4 \n\t"
  48. "paddw %%mm1, %%mm4 \n\t"
  49. "paddw %%mm2, %%mm4 \n\t"
  50. "paddw %%mm3, %%mm4 \n\t"
  51. "decl %%ecx \n\t"
  52. "jnz 1b \n\t"
  53. "movq %%mm4, %%mm3 \n\t"
  54. "punpcklwd %%mm7, %%mm4 \n\t"
  55. "punpckhwd %%mm7, %%mm3 \n\t"
  56. "paddd %%mm4, %%mm3 \n\t"
  57. "movd %%mm3, %%eax \n\t"
  58. "psrlq $32, %%mm3 \n\t"
  59. "movd %%mm3, %%edx \n\t"
  60. "addl %%edx, %%eax \n\t"
  61. "emms \n\t"
  62. : "=a" (ret)
  63. : "S" (a), "D" (b), "a" (s)
  64. : "%ecx", "%edx"
  65. );
  66. return ret;
  67. }
  68. static int licomb_y_mmx(unsigned char *a, unsigned char *b, int s)
  69. {
  70. int ret;
  71. __asm__ volatile (
  72. "movl $4, %%ecx \n\t"
  73. "pxor %%mm6, %%mm6 \n\t"
  74. "pxor %%mm7, %%mm7 \n\t"
  75. "sub %%"REG_a", %%"REG_D" \n\t"
  76. "2: \n\t"
  77. "movq (%%"REG_D"), %%mm0 \n\t"
  78. "movq (%%"REG_D"), %%mm1 \n\t"
  79. "punpcklbw %%mm7, %%mm0 \n\t"
  80. "movq (%%"REG_D",%%"REG_a"), %%mm2 \n\t"
  81. "punpcklbw %%mm7, %%mm1 \n\t"
  82. "punpcklbw %%mm7, %%mm2 \n\t"
  83. "paddw %%mm0, %%mm0 \n\t"
  84. "paddw %%mm2, %%mm1 \n\t"
  85. "movq %%mm0, %%mm2 \n\t"
  86. "psubusw %%mm1, %%mm0 \n\t"
  87. "psubusw %%mm2, %%mm1 \n\t"
  88. "paddw %%mm0, %%mm6 \n\t"
  89. "paddw %%mm1, %%mm6 \n\t"
  90. "movq (%%"REG_S"), %%mm0 \n\t"
  91. "movq (%%"REG_D"), %%mm1 \n\t"
  92. "punpckhbw %%mm7, %%mm0 \n\t"
  93. "movq (%%"REG_D",%%"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. "movq (%%"REG_D",%%"REG_a"), %%mm0 \n\t"
  104. "movq (%%"REG_S"), %%mm1 \n\t"
  105. "punpcklbw %%mm7, %%mm0 \n\t"
  106. "movq (%%"REG_S",%%"REG_a"), %%mm2 \n\t"
  107. "punpcklbw %%mm7, %%mm1 \n\t"
  108. "punpcklbw %%mm7, %%mm2 \n\t"
  109. "paddw %%mm0, %%mm0 \n\t"
  110. "paddw %%mm2, %%mm1 \n\t"
  111. "movq %%mm0, %%mm2 \n\t"
  112. "psubusw %%mm1, %%mm0 \n\t"
  113. "psubusw %%mm2, %%mm1 \n\t"
  114. "paddw %%mm0, %%mm6 \n\t"
  115. "paddw %%mm1, %%mm6 \n\t"
  116. "movq (%%"REG_D",%%"REG_a"), %%mm0 \n\t"
  117. "movq (%%"REG_S"), %%mm1 \n\t"
  118. "punpckhbw %%mm7, %%mm0 \n\t"
  119. "movq (%%"REG_S",%%"REG_a"), %%mm2 \n\t"
  120. "punpckhbw %%mm7, %%mm1 \n\t"
  121. "punpckhbw %%mm7, %%mm2 \n\t"
  122. "paddw %%mm0, %%mm0 \n\t"
  123. "paddw %%mm2, %%mm1 \n\t"
  124. "movq %%mm0, %%mm2 \n\t"
  125. "psubusw %%mm1, %%mm0 \n\t"
  126. "psubusw %%mm2, %%mm1 \n\t"
  127. "paddw %%mm0, %%mm6 \n\t"
  128. "paddw %%mm1, %%mm6 \n\t"
  129. "add %%"REG_a", %%"REG_S" \n\t"
  130. "add %%"REG_a", %%"REG_D" \n\t"
  131. "decl %%ecx \n\t"
  132. "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"
  141. "emms \n\t"
  142. : "=a" (ret)
  143. : "S" (a), "D" (b), "a" (s)
  144. : "%ecx", "%edx"
  145. );
  146. return ret;
  147. }
  148. static int var_y_mmx(unsigned char *a, unsigned char *b, int s)
  149. {
  150. int ret;
  151. __asm__ volatile (
  152. "movl $3, %%ecx \n\t"
  153. "pxor %%mm4, %%mm4 \n\t"
  154. "pxor %%mm7, %%mm7 \n\t"
  155. "1: \n\t"
  156. "movq (%%"REG_S"), %%mm0 \n\t"
  157. "movq (%%"REG_S"), %%mm2 \n\t"
  158. "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
  159. "add %%"REG_a", %%"REG_S" \n\t"
  160. "psubusb %%mm1, %%mm2 \n\t"
  161. "psubusb %%mm0, %%mm1 \n\t"
  162. "movq %%mm2, %%mm0 \n\t"
  163. "movq %%mm1, %%mm3 \n\t"
  164. "punpcklbw %%mm7, %%mm0 \n\t"
  165. "punpcklbw %%mm7, %%mm1 \n\t"
  166. "punpckhbw %%mm7, %%mm2 \n\t"
  167. "punpckhbw %%mm7, %%mm3 \n\t"
  168. "paddw %%mm0, %%mm4 \n\t"
  169. "paddw %%mm1, %%mm4 \n\t"
  170. "paddw %%mm2, %%mm4 \n\t"
  171. "paddw %%mm3, %%mm4 \n\t"
  172. "decl %%ecx \n\t"
  173. "jnz 1b \n\t"
  174. "movq %%mm4, %%mm3 \n\t"
  175. "punpcklwd %%mm7, %%mm4 \n\t"
  176. "punpckhwd %%mm7, %%mm3 \n\t"
  177. "paddd %%mm4, %%mm3 \n\t"
  178. "movd %%mm3, %%eax \n\t"
  179. "psrlq $32, %%mm3 \n\t"
  180. "movd %%mm3, %%edx \n\t"
  181. "addl %%edx, %%eax \n\t"
  182. "emms \n\t"
  183. : "=a" (ret)
  184. : "S" (a), "a" (s)
  185. : "%ecx", "%edx"
  186. );
  187. return 4*ret;
  188. }
  189. #endif
  190. #endif
  191. #define ABS(a) (((a)^((a)>>31))-((a)>>31))
  192. static int diff_y(unsigned char *a, unsigned char *b, int s)
  193. {
  194. int i, j, diff=0;
  195. for (i=4; i; i--) {
  196. for (j=0; j<8; j++) diff += ABS(a[j]-b[j]);
  197. a+=s; b+=s;
  198. }
  199. return diff;
  200. }
  201. static int licomb_y(unsigned char *a, unsigned char *b, int s)
  202. {
  203. int i, j, diff=0;
  204. for (i=4; i; i--) {
  205. for (j=0; j<8; j++)
  206. diff += ABS((a[j]<<1) - b[j-s] - b[j])
  207. + ABS((b[j]<<1) - a[j] - a[j+s]);
  208. a+=s; b+=s;
  209. }
  210. return diff;
  211. }
  212. #if 0
  213. static int qpcomb_y(unsigned char *a, unsigned char *b, int s)
  214. {
  215. int i, j, diff=0;
  216. for (i=4; i; i--) {
  217. for (j=0; j<8; j++)
  218. diff += ABS(a[j] - 3*b[j-s] + 3*a[j+s] - b[j]);
  219. a+=s; b+=s;
  220. }
  221. return diff;
  222. }
  223. static int licomb_y_test(unsigned char *a, unsigned char *b, int s)
  224. {
  225. int c = licomb_y(a,b,s);
  226. int m = licomb_y_mmx(a,b,s);
  227. if (c != m) printf("%d != %d\n", c, m);
  228. return m;
  229. }
  230. #endif
  231. static int var_y(unsigned char *a, unsigned char *b, int s)
  232. {
  233. int i, j, var=0;
  234. for (i=3; i; i--) {
  235. for (j=0; j<8; j++) {
  236. var += ABS(a[j]-a[j+s]);
  237. }
  238. a+=s; b+=s;
  239. }
  240. return 4*var; /* match comb scaling */
  241. }
  242. static void alloc_buffer(struct pullup_context *c, struct pullup_buffer *b)
  243. {
  244. int i;
  245. if (b->planes) return;
  246. b->planes = calloc(c->nplanes, sizeof(unsigned char *));
  247. for (i = 0; i < c->nplanes; i++) {
  248. b->planes[i] = malloc(c->h[i]*c->stride[i]);
  249. /* Deal with idiotic 128=0 for chroma: */
  250. memset(b->planes[i], c->background[i], c->h[i]*c->stride[i]);
  251. }
  252. }
  253. struct pullup_buffer *pullup_lock_buffer(struct pullup_buffer *b, int parity)
  254. {
  255. if (!b) return 0;
  256. if ((parity+1) & 1) b->lock[0]++;
  257. if ((parity+1) & 2) b->lock[1]++;
  258. return b;
  259. }
  260. void pullup_release_buffer(struct pullup_buffer *b, int parity)
  261. {
  262. if (!b) return;
  263. if ((parity+1) & 1) b->lock[0]--;
  264. if ((parity+1) & 2) b->lock[1]--;
  265. }
  266. struct pullup_buffer *pullup_get_buffer(struct pullup_context *c, int parity)
  267. {
  268. int i;
  269. /* Try first to get the sister buffer for the previous field */
  270. if (parity < 2 && c->last && parity != c->last->parity
  271. && !c->last->buffer->lock[parity]) {
  272. alloc_buffer(c, c->last->buffer);
  273. return pullup_lock_buffer(c->last->buffer, parity);
  274. }
  275. /* Prefer a buffer with both fields open */
  276. for (i = 0; i < c->nbuffers; i++) {
  277. if (c->buffers[i].lock[0]) continue;
  278. if (c->buffers[i].lock[1]) continue;
  279. alloc_buffer(c, &c->buffers[i]);
  280. return pullup_lock_buffer(&c->buffers[i], parity);
  281. }
  282. if (parity == 2) return 0;
  283. /* Search for any half-free buffer */
  284. for (i = 0; i < c->nbuffers; i++) {
  285. if (((parity+1) & 1) && c->buffers[i].lock[0]) continue;
  286. if (((parity+1) & 2) && c->buffers[i].lock[1]) continue;
  287. alloc_buffer(c, &c->buffers[i]);
  288. return pullup_lock_buffer(&c->buffers[i], parity);
  289. }
  290. return 0;
  291. }
  292. static void compute_metric(struct pullup_context *c,
  293. struct pullup_field *fa, int pa,
  294. struct pullup_field *fb, int pb,
  295. int (*func)(unsigned char *, unsigned char *, int), int *dest)
  296. {
  297. unsigned char *a, *b;
  298. int x, y;
  299. int mp = c->metric_plane;
  300. int xstep = c->bpp[mp];
  301. int ystep = c->stride[mp]<<3;
  302. int s = c->stride[mp]<<1; /* field stride */
  303. int w = c->metric_w*xstep;
  304. if (!fa->buffer || !fb->buffer) return;
  305. /* Shortcut for duplicate fields (e.g. from RFF flag) */
  306. if (fa->buffer == fb->buffer && pa == pb) {
  307. memset(dest, 0, c->metric_len * sizeof(int));
  308. return;
  309. }
  310. a = fa->buffer->planes[mp] + pa * c->stride[mp] + c->metric_offset;
  311. b = fb->buffer->planes[mp] + pb * c->stride[mp] + c->metric_offset;
  312. for (y = c->metric_h; y; y--) {
  313. for (x = 0; x < w; x += xstep) {
  314. *dest++ = func(a + x, b + x, s);
  315. }
  316. a += ystep; b += ystep;
  317. }
  318. }
  319. static void alloc_metrics(struct pullup_context *c, struct pullup_field *f)
  320. {
  321. f->diffs = calloc(c->metric_len, sizeof(int));
  322. f->comb = calloc(c->metric_len, sizeof(int));
  323. f->var = calloc(c->metric_len, sizeof(int));
  324. /* add more metrics here as needed */
  325. }
  326. static struct pullup_field *make_field_queue(struct pullup_context *c, int len)
  327. {
  328. struct pullup_field *head, *f;
  329. f = head = calloc(1, sizeof(struct pullup_field));
  330. alloc_metrics(c, f);
  331. for (; len > 0; len--) {
  332. f->next = calloc(1, sizeof(struct pullup_field));
  333. f->next->prev = f;
  334. f = f->next;
  335. alloc_metrics(c, f);
  336. }
  337. f->next = head;
  338. head->prev = f;
  339. return head;
  340. }
  341. static void check_field_queue(struct pullup_context *c)
  342. {
  343. if (c->head->next == c->first) {
  344. struct pullup_field *f = calloc(1, sizeof(struct pullup_field));
  345. alloc_metrics(c, f);
  346. f->prev = c->head;
  347. f->next = c->first;
  348. c->head->next = f;
  349. c->first->prev = f;
  350. }
  351. }
  352. void pullup_submit_field(struct pullup_context *c, struct pullup_buffer *b, int parity)
  353. {
  354. struct pullup_field *f;
  355. /* Grow the circular list if needed */
  356. check_field_queue(c);
  357. /* Cannot have two fields of same parity in a row; drop the new one */
  358. if (c->last && c->last->parity == parity) return;
  359. f = c->head;
  360. f->parity = parity;
  361. f->buffer = pullup_lock_buffer(b, parity);
  362. f->flags = 0;
  363. f->breaks = 0;
  364. f->affinity = 0;
  365. compute_metric(c, f, parity, f->prev->prev, parity, c->diff, f->diffs);
  366. compute_metric(c, parity?f->prev:f, 0, parity?f:f->prev, 1, c->comb, f->comb);
  367. compute_metric(c, f, parity, f, -1, c->var, f->var);
  368. /* Advance the circular list */
  369. if (!c->first) c->first = c->head;
  370. c->last = c->head;
  371. c->head = c->head->next;
  372. }
  373. void pullup_flush_fields(struct pullup_context *c)
  374. {
  375. struct pullup_field *f;
  376. for (f = c->first; f && f != c->head; f = f->next) {
  377. pullup_release_buffer(f->buffer, f->parity);
  378. f->buffer = 0;
  379. }
  380. c->first = c->last = 0;
  381. }
  382. #define F_HAVE_BREAKS 1
  383. #define F_HAVE_AFFINITY 2
  384. #define BREAK_LEFT 1
  385. #define BREAK_RIGHT 2
  386. static int queue_length(struct pullup_field *begin, struct pullup_field *end)
  387. {
  388. int count = 1;
  389. struct pullup_field *f;
  390. if (!begin || !end) return 0;
  391. for (f = begin; f != end; f = f->next) count++;
  392. return count;
  393. }
  394. static int find_first_break(struct pullup_field *f, int max)
  395. {
  396. int i;
  397. for (i = 0; i < max; i++) {
  398. if (f->breaks & BREAK_RIGHT || f->next->breaks & BREAK_LEFT)
  399. return i+1;
  400. f = f->next;
  401. }
  402. return 0;
  403. }
  404. static void compute_breaks(struct pullup_context *c, struct pullup_field *f0)
  405. {
  406. int i;
  407. struct pullup_field *f1 = f0->next;
  408. struct pullup_field *f2 = f1->next;
  409. struct pullup_field *f3 = f2->next;
  410. int l, max_l=0, max_r=0;
  411. //struct pullup_field *ff;
  412. //for (i=0, ff=c->first; ff != f0; i++, ff=ff->next);
  413. if (f0->flags & F_HAVE_BREAKS) return;
  414. //printf("\n%d: ", i);
  415. f0->flags |= F_HAVE_BREAKS;
  416. /* Special case when fields are 100% identical */
  417. if (f0->buffer == f2->buffer && f1->buffer != f3->buffer) {
  418. f2->breaks |= BREAK_RIGHT;
  419. return;
  420. }
  421. if (f0->buffer != f2->buffer && f1->buffer == f3->buffer) {
  422. f1->breaks |= BREAK_LEFT;
  423. return;
  424. }
  425. for (i = 0; i < c->metric_len; i++) {
  426. l = f2->diffs[i] - f3->diffs[i];
  427. if (l > max_l) max_l = l;
  428. if (-l > max_r) max_r = -l;
  429. }
  430. /* Don't get tripped up when differences are mostly quant error */
  431. //printf("%d %d\n", max_l, max_r);
  432. if (max_l + max_r < 128) return;
  433. if (max_l > 4*max_r) f1->breaks |= BREAK_LEFT;
  434. if (max_r > 4*max_l) f2->breaks |= BREAK_RIGHT;
  435. }
  436. static void compute_affinity(struct pullup_context *c, struct pullup_field *f)
  437. {
  438. int i;
  439. int max_l=0, max_r=0, l;
  440. if (f->flags & F_HAVE_AFFINITY) return;
  441. f->flags |= F_HAVE_AFFINITY;
  442. if (f->buffer == f->next->next->buffer) {
  443. f->affinity = 1;
  444. f->next->affinity = 0;
  445. f->next->next->affinity = -1;
  446. f->next->flags |= F_HAVE_AFFINITY;
  447. f->next->next->flags |= F_HAVE_AFFINITY;
  448. return;
  449. }
  450. if (1) {
  451. for (i = 0; i < c->metric_len; i++) {
  452. int lv = f->prev->var[i];
  453. int rv = f->next->var[i];
  454. int v = f->var[i];
  455. int lc = f->comb[i] - (v+lv) + ABS(v-lv);
  456. int rc = f->next->comb[i] - (v+rv) + ABS(v-rv);
  457. lc = lc>0 ? lc : 0;
  458. rc = rc>0 ? rc : 0;
  459. l = lc - rc;
  460. if (l > max_l) max_l = l;
  461. if (-l > max_r) max_r = -l;
  462. }
  463. if (max_l + max_r < 64) return;
  464. if (max_r > 6*max_l) f->affinity = -1;
  465. else if (max_l > 6*max_r) f->affinity = 1;
  466. } else {
  467. for (i = 0; i < c->metric_len; i++) {
  468. l = f->comb[i] - f->next->comb[i];
  469. if (l > max_l) max_l = l;
  470. if (-l > max_r) max_r = -l;
  471. }
  472. if (max_l + max_r < 64) return;
  473. if (max_r > 2*max_l) f->affinity = -1;
  474. else if (max_l > 2*max_r) f->affinity = 1;
  475. }
  476. }
  477. static void foo(struct pullup_context *c)
  478. {
  479. struct pullup_field *f = c->first;
  480. int i, n = queue_length(f, c->last);
  481. for (i = 0; i < n-1; i++) {
  482. if (i < n-3) compute_breaks(c, f);
  483. compute_affinity(c, f);
  484. f = f->next;
  485. }
  486. }
  487. static int decide_frame_length(struct pullup_context *c)
  488. {
  489. struct pullup_field *f0 = c->first;
  490. struct pullup_field *f1 = f0->next;
  491. struct pullup_field *f2 = f1->next;
  492. int l;
  493. if (queue_length(c->first, c->last) < 4) return 0;
  494. foo(c);
  495. if (f0->affinity == -1) return 1;
  496. l = find_first_break(f0, 3);
  497. if (l == 1 && c->strict_breaks < 0) l = 0;
  498. switch (l) {
  499. case 1:
  500. if (c->strict_breaks < 1 && f0->affinity == 1 && f1->affinity == -1)
  501. return 2;
  502. else return 1;
  503. case 2:
  504. /* FIXME: strictly speaking, f0->prev is no longer valid... :) */
  505. if (c->strict_pairs
  506. && (f0->prev->breaks & BREAK_RIGHT) && (f2->breaks & BREAK_LEFT)
  507. && (f0->affinity != 1 || f1->affinity != -1) )
  508. return 1;
  509. if (f1->affinity == 1) return 1;
  510. else return 2;
  511. case 3:
  512. if (f2->affinity == 1) return 2;
  513. else return 3;
  514. default:
  515. /* 9 possibilities covered before switch */
  516. if (f1->affinity == 1) return 1; /* covers 6 */
  517. else if (f1->affinity == -1) return 2; /* covers 6 */
  518. else if (f2->affinity == -1) { /* covers 2 */
  519. if (f0->affinity == 1) return 3;
  520. else return 1;
  521. }
  522. else return 2; /* the remaining 6 */
  523. }
  524. }
  525. static void print_aff_and_breaks(struct pullup_context *c, struct pullup_field *f)
  526. {
  527. int i;
  528. struct pullup_field *f0 = f;
  529. const char aff_l[] = "+..", aff_r[] = "..+";
  530. printf("\naffinity: ");
  531. for (i = 0; i < 4; i++) {
  532. printf("%c%d%c", aff_l[1+f->affinity], i, aff_r[1+f->affinity]);
  533. f = f->next;
  534. }
  535. f = f0;
  536. printf("\nbreaks: ");
  537. for (i=0; i<4; i++) {
  538. printf("%c%d%c", f->breaks & BREAK_LEFT ? '|' : '.', i, f->breaks & BREAK_RIGHT ? '|' : '.');
  539. f = f->next;
  540. }
  541. printf("\n");
  542. }
  543. struct pullup_frame *pullup_get_frame(struct pullup_context *c)
  544. {
  545. int i;
  546. struct pullup_frame *fr = c->frame;
  547. int n = decide_frame_length(c);
  548. int aff = c->first->next->affinity;
  549. if (!n) return 0;
  550. if (fr->lock) return 0;
  551. if (c->verbose) {
  552. print_aff_and_breaks(c, c->first);
  553. printf("duration: %d \n", n);
  554. }
  555. fr->lock++;
  556. fr->length = n;
  557. fr->parity = c->first->parity;
  558. fr->buffer = 0;
  559. for (i = 0; i < n; i++) {
  560. /* We cheat and steal the buffer without release+relock */
  561. fr->ifields[i] = c->first->buffer;
  562. c->first->buffer = 0;
  563. c->first = c->first->next;
  564. }
  565. if (n == 1) {
  566. fr->ofields[fr->parity] = fr->ifields[0];
  567. fr->ofields[fr->parity^1] = 0;
  568. } else if (n == 2) {
  569. fr->ofields[fr->parity] = fr->ifields[0];
  570. fr->ofields[fr->parity^1] = fr->ifields[1];
  571. } else if (n == 3) {
  572. if (aff == 0)
  573. aff = (fr->ifields[0] == fr->ifields[1]) ? -1 : 1;
  574. /* else if (c->verbose) printf("forced aff: %d \n", aff); */
  575. fr->ofields[fr->parity] = fr->ifields[1+aff];
  576. fr->ofields[fr->parity^1] = fr->ifields[1];
  577. }
  578. pullup_lock_buffer(fr->ofields[0], 0);
  579. pullup_lock_buffer(fr->ofields[1], 1);
  580. if (fr->ofields[0] == fr->ofields[1]) {
  581. fr->buffer = fr->ofields[0];
  582. pullup_lock_buffer(fr->buffer, 2);
  583. return fr;
  584. }
  585. return fr;
  586. }
  587. static void copy_field(struct pullup_context *c, struct pullup_buffer *dest,
  588. struct pullup_buffer *src, int parity)
  589. {
  590. int i, j;
  591. unsigned char *d, *s;
  592. for (i = 0; i < c->nplanes; i++) {
  593. s = src->planes[i] + parity*c->stride[i];
  594. d = dest->planes[i] + parity*c->stride[i];
  595. for (j = c->h[i]>>1; j; j--) {
  596. memcpy(d, s, c->stride[i]);
  597. s += c->stride[i]<<1;
  598. d += c->stride[i]<<1;
  599. }
  600. }
  601. }
  602. void pullup_pack_frame(struct pullup_context *c, struct pullup_frame *fr)
  603. {
  604. int i;
  605. if (fr->buffer) return;
  606. if (fr->length < 2) return; /* FIXME: deal with this */
  607. for (i = 0; i < 2; i++)
  608. {
  609. if (fr->ofields[i]->lock[i^1]) continue;
  610. fr->buffer = fr->ofields[i];
  611. pullup_lock_buffer(fr->buffer, 2);
  612. copy_field(c, fr->buffer, fr->ofields[i^1], i^1);
  613. return;
  614. }
  615. fr->buffer = pullup_get_buffer(c, 2);
  616. copy_field(c, fr->buffer, fr->ofields[0], 0);
  617. copy_field(c, fr->buffer, fr->ofields[1], 1);
  618. }
  619. void pullup_release_frame(struct pullup_frame *fr)
  620. {
  621. int i;
  622. for (i = 0; i < fr->length; i++)
  623. pullup_release_buffer(fr->ifields[i], fr->parity ^ (i&1));
  624. pullup_release_buffer(fr->ofields[0], 0);
  625. pullup_release_buffer(fr->ofields[1], 1);
  626. if (fr->buffer) pullup_release_buffer(fr->buffer, 2);
  627. fr->lock--;
  628. }
  629. struct pullup_context *pullup_alloc_context(void)
  630. {
  631. struct pullup_context *c;
  632. c = calloc(1, sizeof(struct pullup_context));
  633. return c;
  634. }
  635. void pullup_preinit_context(struct pullup_context *c)
  636. {
  637. c->bpp = calloc(c->nplanes, sizeof(int));
  638. c->w = calloc(c->nplanes, sizeof(int));
  639. c->h = calloc(c->nplanes, sizeof(int));
  640. c->stride = calloc(c->nplanes, sizeof(int));
  641. c->background = calloc(c->nplanes, sizeof(int));
  642. }
  643. void pullup_init_context(struct pullup_context *c)
  644. {
  645. int mp = c->metric_plane;
  646. if (c->nbuffers < 10) c->nbuffers = 10;
  647. c->buffers = calloc(c->nbuffers, sizeof (struct pullup_buffer));
  648. c->metric_w = (c->w[mp] - ((c->junk_left + c->junk_right) << 3)) >> 3;
  649. c->metric_h = (c->h[mp] - ((c->junk_top + c->junk_bottom) << 1)) >> 3;
  650. c->metric_offset = c->junk_left*c->bpp[mp] + (c->junk_top<<1)*c->stride[mp];
  651. c->metric_len = c->metric_w * c->metric_h;
  652. c->head = make_field_queue(c, 8);
  653. c->frame = calloc(1, sizeof (struct pullup_frame));
  654. c->frame->ifields = calloc(3, sizeof (struct pullup_buffer *));
  655. switch(c->format) {
  656. case PULLUP_FMT_Y:
  657. c->diff = diff_y;
  658. c->comb = licomb_y;
  659. c->var = var_y;
  660. #if ARCH_X86
  661. #if HAVE_MMX
  662. if (c->cpu & PULLUP_CPU_MMX) {
  663. c->diff = diff_y_mmx;
  664. c->comb = licomb_y_mmx;
  665. c->var = var_y_mmx;
  666. }
  667. #endif
  668. #endif
  669. /* c->comb = qpcomb_y; */
  670. break;
  671. #if 0
  672. case PULLUP_FMT_YUY2:
  673. c->diff = diff_yuy2;
  674. break;
  675. case PULLUP_FMT_RGB32:
  676. c->diff = diff_rgb32;
  677. break;
  678. #endif
  679. }
  680. }
  681. void pullup_free_context(struct pullup_context *c)
  682. {
  683. struct pullup_field *f;
  684. free(c->buffers);
  685. f = c->head;
  686. do {
  687. if (!f) break;
  688. free(f->diffs);
  689. free(f->comb);
  690. f = f->next;
  691. free(f->prev);
  692. } while (f != c->head);
  693. free(c->frame);
  694. free(c);
  695. }