vf_pullup.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. * Copyright (c) 2003 Rich Felker
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/imgutils.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. #include "vf_pullup.h"
  29. #define F_HAVE_BREAKS 1
  30. #define F_HAVE_AFFINITY 2
  31. #define BREAK_LEFT 1
  32. #define BREAK_RIGHT 2
  33. #define OFFSET(x) offsetof(PullupContext, x)
  34. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  35. static const AVOption pullup_options[] = {
  36. { "jl", "set left junk size", OFFSET(junk_left), AV_OPT_TYPE_INT, {.i64=1}, 0, INT_MAX, FLAGS },
  37. { "jr", "set right junk size", OFFSET(junk_right), AV_OPT_TYPE_INT, {.i64=1}, 0, INT_MAX, FLAGS },
  38. { "jt", "set top junk size", OFFSET(junk_top), AV_OPT_TYPE_INT, {.i64=4}, 1, INT_MAX, FLAGS },
  39. { "jb", "set bottom junk size", OFFSET(junk_bottom), AV_OPT_TYPE_INT, {.i64=4}, 1, INT_MAX, FLAGS },
  40. { "sb", "set strict breaks", OFFSET(strict_breaks), AV_OPT_TYPE_INT, {.i64=0},-1, 1, FLAGS },
  41. { "mp", "set metric plane", OFFSET(metric_plane), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "mp" },
  42. { "y", "luma", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mp" },
  43. { "u", "chroma blue", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mp" },
  44. { "v", "chroma red", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mp" },
  45. { NULL }
  46. };
  47. AVFILTER_DEFINE_CLASS(pullup);
  48. static int query_formats(AVFilterContext *ctx)
  49. {
  50. static const enum AVPixelFormat pix_fmts[] = {
  51. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  52. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  53. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  54. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  55. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  56. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_GRAY8,
  57. AV_PIX_FMT_NONE
  58. };
  59. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  60. return 0;
  61. }
  62. #define ABS(a) (((a) ^ ((a) >> 31)) - ((a) >> 31))
  63. static int diff_c(const uint8_t *a, const uint8_t *b, ptrdiff_t s)
  64. {
  65. int i, j, diff = 0;
  66. for (i = 0; i < 4; i++) {
  67. for (j = 0; j < 8; j++)
  68. diff += ABS(a[j] - b[j]);
  69. a += s;
  70. b += s;
  71. }
  72. return diff;
  73. }
  74. static int comb_c(const uint8_t *a, const uint8_t *b, ptrdiff_t s)
  75. {
  76. int i, j, comb = 0;
  77. for (i = 0; i < 4; i++) {
  78. for (j = 0; j < 8; j++)
  79. comb += ABS((a[j] << 1) - b[j - s] - b[j ]) +
  80. ABS((b[j] << 1) - a[j ] - a[j + s]);
  81. a += s;
  82. b += s;
  83. }
  84. return comb;
  85. }
  86. static int var_c(const uint8_t *a, const uint8_t *b, ptrdiff_t s)
  87. {
  88. int i, j, var = 0;
  89. for (i = 0; i < 3; i++) {
  90. for (j = 0; j < 8; j++)
  91. var += ABS(a[j] - a[j + s]);
  92. a += s;
  93. }
  94. return 4 * var; /* match comb scaling */
  95. }
  96. static int alloc_metrics(PullupContext *s, PullupField *f)
  97. {
  98. f->diffs = av_calloc(FFALIGN(s->metric_length, 16), sizeof(*f->diffs));
  99. f->combs = av_calloc(FFALIGN(s->metric_length, 16), sizeof(*f->combs));
  100. f->vars = av_calloc(FFALIGN(s->metric_length, 16), sizeof(*f->vars));
  101. if (!f->diffs || !f->combs || !f->vars) {
  102. av_freep(&f->diffs);
  103. av_freep(&f->combs);
  104. av_freep(&f->vars);
  105. return AVERROR(ENOMEM);
  106. }
  107. return 0;
  108. }
  109. static void free_field_queue(PullupField *head)
  110. {
  111. PullupField *f = head;
  112. do {
  113. PullupField *next;
  114. if (!f)
  115. break;
  116. av_free(f->diffs);
  117. av_free(f->combs);
  118. av_free(f->vars);
  119. next = f->next;
  120. memset(f, 0, sizeof(*f));
  121. av_free(f);
  122. f = next;
  123. } while (f != head);
  124. }
  125. static PullupField *make_field_queue(PullupContext *s, int len)
  126. {
  127. PullupField *head, *f;
  128. f = head = av_mallocz(sizeof(*head));
  129. if (!f)
  130. return NULL;
  131. if (alloc_metrics(s, f) < 0) {
  132. av_free(f);
  133. return NULL;
  134. }
  135. for (; len > 0; len--) {
  136. f->next = av_mallocz(sizeof(*f->next));
  137. if (!f->next) {
  138. free_field_queue(head);
  139. return NULL;
  140. }
  141. f->next->prev = f;
  142. f = f->next;
  143. if (alloc_metrics(s, f) < 0) {
  144. free_field_queue(head);
  145. return NULL;
  146. }
  147. }
  148. f->next = head;
  149. head->prev = f;
  150. return head;
  151. }
  152. static int config_input(AVFilterLink *inlink)
  153. {
  154. AVFilterContext *ctx = inlink->dst;
  155. PullupContext *s = ctx->priv;
  156. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  157. int mp = s->metric_plane;
  158. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  159. if (mp + 1 > s->nb_planes) {
  160. av_log(ctx, AV_LOG_ERROR, "input format does not have such plane\n");
  161. return AVERROR(EINVAL);
  162. }
  163. s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  164. s->planeheight[0] = s->planeheight[3] = inlink->h;
  165. s->planewidth[1] = s->planewidth[2] = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  166. s->planewidth[0] = s->planewidth[3] = inlink->w;
  167. s->metric_w = (s->planewidth[mp] - ((s->junk_left + s->junk_right) << 3)) >> 3;
  168. s->metric_h = (s->planeheight[mp] - ((s->junk_top + s->junk_bottom) << 1)) >> 3;
  169. s->metric_offset = (s->junk_left << 3) + (s->junk_top << 1) * s->planewidth[mp];
  170. s->metric_length = s->metric_w * s->metric_h;
  171. av_log(ctx, AV_LOG_DEBUG, "w: %d h: %d\n", s->metric_w, s->metric_h);
  172. av_log(ctx, AV_LOG_DEBUG, "offset: %d length: %d\n", s->metric_offset, s->metric_length);
  173. s->head = make_field_queue(s, 8);
  174. if (!s->head)
  175. return AVERROR(ENOMEM);
  176. s->diff = diff_c;
  177. s->comb = comb_c;
  178. s->var = var_c;
  179. if (ARCH_X86)
  180. ff_pullup_init_x86(s);
  181. return 0;
  182. }
  183. static int config_output(AVFilterLink *outlink)
  184. {
  185. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  186. return 0;
  187. }
  188. static PullupBuffer *pullup_lock_buffer(PullupBuffer *b, int parity)
  189. {
  190. if (!b)
  191. return NULL;
  192. if ((parity + 1) & 1)
  193. b->lock[0]++;
  194. if ((parity + 1) & 2)
  195. b->lock[1]++;
  196. return b;
  197. }
  198. static void pullup_release_buffer(PullupBuffer *b, int parity)
  199. {
  200. if (!b)
  201. return;
  202. if ((parity + 1) & 1)
  203. b->lock[0]--;
  204. if ((parity + 1) & 2)
  205. b->lock[1]--;
  206. }
  207. static int alloc_buffer(PullupContext *s, PullupBuffer *b)
  208. {
  209. int i;
  210. if (b->planes[0])
  211. return 0;
  212. for (i = 0; i < s->nb_planes; i++) {
  213. b->planes[i] = av_malloc(s->planeheight[i] * s->planewidth[i]);
  214. }
  215. if (s->nb_planes == 1)
  216. b->planes[1] = av_malloc(4*256);
  217. return 0;
  218. }
  219. static PullupBuffer *pullup_get_buffer(PullupContext *s, int parity)
  220. {
  221. int i;
  222. /* Try first to get the sister buffer for the previous field */
  223. if (parity < 2 && s->last && parity != s->last->parity
  224. && !s->last->buffer->lock[parity]) {
  225. alloc_buffer(s, s->last->buffer);
  226. return pullup_lock_buffer(s->last->buffer, parity);
  227. }
  228. /* Prefer a buffer with both fields open */
  229. for (i = 0; i < FF_ARRAY_ELEMS(s->buffers); i++) {
  230. if (s->buffers[i].lock[0])
  231. continue;
  232. if (s->buffers[i].lock[1])
  233. continue;
  234. alloc_buffer(s, &s->buffers[i]);
  235. return pullup_lock_buffer(&s->buffers[i], parity);
  236. }
  237. if (parity == 2)
  238. return 0;
  239. /* Search for any half-free buffer */
  240. for (i = 0; i < FF_ARRAY_ELEMS(s->buffers); i++) {
  241. if (((parity + 1) & 1) && s->buffers[i].lock[0])
  242. continue;
  243. if (((parity + 1) & 2) && s->buffers[i].lock[1])
  244. continue;
  245. alloc_buffer(s, &s->buffers[i]);
  246. return pullup_lock_buffer(&s->buffers[i], parity);
  247. }
  248. return NULL;
  249. }
  250. static int queue_length(PullupField *begin, PullupField *end)
  251. {
  252. PullupField *f;
  253. int count = 1;
  254. if (!begin || !end)
  255. return 0;
  256. for (f = begin; f != end; f = f->next)
  257. count++;
  258. return count;
  259. }
  260. static int find_first_break(PullupField *f, int max)
  261. {
  262. int i;
  263. for (i = 0; i < max; i++) {
  264. if (f->breaks & BREAK_RIGHT || f->next->breaks & BREAK_LEFT)
  265. return i + 1;
  266. f = f->next;
  267. }
  268. return 0;
  269. }
  270. static void compute_breaks(PullupContext *s, PullupField *f0)
  271. {
  272. PullupField *f1 = f0->next;
  273. PullupField *f2 = f1->next;
  274. PullupField *f3 = f2->next;
  275. int i, l, max_l = 0, max_r = 0;
  276. if (f0->flags & F_HAVE_BREAKS)
  277. return;
  278. f0->flags |= F_HAVE_BREAKS;
  279. /* Special case when fields are 100% identical */
  280. if (f0->buffer == f2->buffer && f1->buffer != f3->buffer) {
  281. f2->breaks |= BREAK_RIGHT;
  282. return;
  283. }
  284. if (f0->buffer != f2->buffer && f1->buffer == f3->buffer) {
  285. f1->breaks |= BREAK_LEFT;
  286. return;
  287. }
  288. for (i = 0; i < s->metric_length; i++) {
  289. l = f2->diffs[i] - f3->diffs[i];
  290. if ( l > max_l)
  291. max_l = l;
  292. if (-l > max_r)
  293. max_r = -l;
  294. }
  295. /* Don't get tripped up when differences are mostly quant error */
  296. if (max_l + max_r < 128)
  297. return;
  298. if (max_l > 4 * max_r)
  299. f1->breaks |= BREAK_LEFT;
  300. if (max_r > 4 * max_l)
  301. f2->breaks |= BREAK_RIGHT;
  302. }
  303. static void compute_affinity(PullupContext *s, PullupField *f)
  304. {
  305. int i, max_l = 0, max_r = 0, l;
  306. if (f->flags & F_HAVE_AFFINITY)
  307. return;
  308. f->flags |= F_HAVE_AFFINITY;
  309. if (f->buffer == f->next->next->buffer) {
  310. f->affinity = 1;
  311. f->next->affinity = 0;
  312. f->next->next->affinity = -1;
  313. f->next->flags |= F_HAVE_AFFINITY;
  314. f->next->next->flags |= F_HAVE_AFFINITY;
  315. return;
  316. }
  317. for (i = 0; i < s->metric_length; i++) {
  318. int v = f->vars[i];
  319. int lv = f->prev->vars[i];
  320. int rv = f->next->vars[i];
  321. int lc = f->combs[i] - (v + lv) + ABS(v - lv);
  322. int rc = f->next->combs[i] - (v + rv) + ABS(v - rv);
  323. lc = FFMAX(lc, 0);
  324. rc = FFMAX(rc, 0);
  325. l = lc - rc;
  326. if ( l > max_l)
  327. max_l = l;
  328. if (-l > max_r)
  329. max_r = -l;
  330. }
  331. if (max_l + max_r < 64)
  332. return;
  333. if (max_r > 6 * max_l)
  334. f->affinity = -1;
  335. else if (max_l > 6 * max_r)
  336. f->affinity = 1;
  337. }
  338. static int decide_frame_length(PullupContext *s)
  339. {
  340. PullupField *f0 = s->first;
  341. PullupField *f1 = f0->next;
  342. PullupField *f2 = f1->next;
  343. PullupField *f;
  344. int i, l, n;
  345. if (queue_length(s->first, s->last) < 4)
  346. return 0;
  347. f = s->first;
  348. n = queue_length(f, s->last);
  349. for (i = 0; i < n - 1; i++) {
  350. if (i < n - 3)
  351. compute_breaks(s, f);
  352. compute_affinity(s, f);
  353. f = f->next;
  354. }
  355. if (f0->affinity == -1)
  356. return 1;
  357. l = find_first_break(f0, 3);
  358. if (l == 1 && s->strict_breaks < 0)
  359. l = 0;
  360. switch (l) {
  361. case 1:
  362. return 1 + (s->strict_breaks < 1 && f0->affinity == 1 && f1->affinity == -1);
  363. case 2:
  364. /* FIXME: strictly speaking, f0->prev is no longer valid... :) */
  365. if (s->strict_pairs
  366. && (f0->prev->breaks & BREAK_RIGHT) && (f2->breaks & BREAK_LEFT)
  367. && (f0->affinity != 1 || f1->affinity != -1) )
  368. return 1;
  369. return 1 + (f1->affinity != 1);
  370. case 3:
  371. return 2 + (f2->affinity != 1);
  372. default:
  373. /* 9 possibilities covered before switch */
  374. if (f1->affinity == 1)
  375. return 1; /* covers 6 */
  376. else if (f1->affinity == -1)
  377. return 2; /* covers 6 */
  378. else if (f2->affinity == -1) { /* covers 2 */
  379. return (f0->affinity == 1) ? 3 : 1;
  380. } else {
  381. return 2; /* the remaining 6 */
  382. }
  383. }
  384. }
  385. static PullupFrame *pullup_get_frame(PullupContext *s)
  386. {
  387. PullupFrame *fr = &s->frame;
  388. int i, n = decide_frame_length(s);
  389. int aff = s->first->next->affinity;
  390. av_assert1(n < FF_ARRAY_ELEMS(fr->ifields));
  391. if (!n || fr->lock)
  392. return NULL;
  393. fr->lock++;
  394. fr->length = n;
  395. fr->parity = s->first->parity;
  396. fr->buffer = 0;
  397. for (i = 0; i < n; i++) {
  398. /* We cheat and steal the buffer without release+relock */
  399. fr->ifields[i] = s->first->buffer;
  400. s->first->buffer = 0;
  401. s->first = s->first->next;
  402. }
  403. if (n == 1) {
  404. fr->ofields[fr->parity ] = fr->ifields[0];
  405. fr->ofields[fr->parity ^ 1] = 0;
  406. } else if (n == 2) {
  407. fr->ofields[fr->parity ] = fr->ifields[0];
  408. fr->ofields[fr->parity ^ 1] = fr->ifields[1];
  409. } else if (n == 3) {
  410. if (!aff)
  411. aff = (fr->ifields[0] == fr->ifields[1]) ? -1 : 1;
  412. fr->ofields[fr->parity ] = fr->ifields[1 + aff];
  413. fr->ofields[fr->parity ^ 1] = fr->ifields[1 ];
  414. }
  415. pullup_lock_buffer(fr->ofields[0], 0);
  416. pullup_lock_buffer(fr->ofields[1], 1);
  417. if (fr->ofields[0] == fr->ofields[1]) {
  418. fr->buffer = fr->ofields[0];
  419. pullup_lock_buffer(fr->buffer, 2);
  420. return fr;
  421. }
  422. return fr;
  423. }
  424. static void pullup_release_frame(PullupFrame *f)
  425. {
  426. int i;
  427. for (i = 0; i < f->length; i++)
  428. pullup_release_buffer(f->ifields[i], f->parity ^ (i & 1));
  429. pullup_release_buffer(f->ofields[0], 0);
  430. pullup_release_buffer(f->ofields[1], 1);
  431. if (f->buffer)
  432. pullup_release_buffer(f->buffer, 2);
  433. f->lock--;
  434. }
  435. static void compute_metric(PullupContext *s, int *dest,
  436. PullupField *fa, int pa, PullupField *fb, int pb,
  437. int (*func)(const uint8_t *, const uint8_t *, ptrdiff_t))
  438. {
  439. int mp = s->metric_plane;
  440. int xstep = 8;
  441. int ystep = s->planewidth[mp] << 3;
  442. int stride = s->planewidth[mp] << 1; /* field stride */
  443. int w = s->metric_w * xstep;
  444. uint8_t *a, *b;
  445. int x, y;
  446. if (!fa->buffer || !fb->buffer)
  447. return;
  448. /* Shortcut for duplicate fields (e.g. from RFF flag) */
  449. if (fa->buffer == fb->buffer && pa == pb) {
  450. memset(dest, 0, s->metric_length * sizeof(*dest));
  451. return;
  452. }
  453. a = fa->buffer->planes[mp] + pa * s->planewidth[mp] + s->metric_offset;
  454. b = fb->buffer->planes[mp] + pb * s->planewidth[mp] + s->metric_offset;
  455. for (y = 0; y < s->metric_h; y++) {
  456. for (x = 0; x < w; x += xstep)
  457. *dest++ = func(a + x, b + x, stride);
  458. a += ystep; b += ystep;
  459. }
  460. }
  461. static int check_field_queue(PullupContext *s)
  462. {
  463. int ret;
  464. if (s->head->next == s->first) {
  465. PullupField *f = av_mallocz(sizeof(*f));
  466. if (!f)
  467. return AVERROR(ENOMEM);
  468. if ((ret = alloc_metrics(s, f)) < 0) {
  469. av_free(f);
  470. return ret;
  471. }
  472. f->prev = s->head;
  473. f->next = s->first;
  474. s->head->next = f;
  475. s->first->prev = f;
  476. }
  477. return 0;
  478. }
  479. static void pullup_submit_field(PullupContext *s, PullupBuffer *b, int parity)
  480. {
  481. PullupField *f;
  482. /* Grow the circular list if needed */
  483. if (check_field_queue(s) < 0)
  484. return;
  485. /* Cannot have two fields of same parity in a row; drop the new one */
  486. if (s->last && s->last->parity == parity)
  487. return;
  488. f = s->head;
  489. f->parity = parity;
  490. f->buffer = pullup_lock_buffer(b, parity);
  491. f->flags = 0;
  492. f->breaks = 0;
  493. f->affinity = 0;
  494. compute_metric(s, f->diffs, f, parity, f->prev->prev, parity, s->diff);
  495. compute_metric(s, f->combs, parity ? f->prev : f, 0, parity ? f : f->prev, 1, s->comb);
  496. compute_metric(s, f->vars, f, parity, f, -1, s->var);
  497. emms_c();
  498. /* Advance the circular list */
  499. if (!s->first)
  500. s->first = s->head;
  501. s->last = s->head;
  502. s->head = s->head->next;
  503. }
  504. static void copy_field(PullupContext *s,
  505. PullupBuffer *dst, PullupBuffer *src, int parity)
  506. {
  507. uint8_t *dd, *ss;
  508. int i;
  509. for (i = 0; i < s->nb_planes; i++) {
  510. ss = src->planes[i] + parity * s->planewidth[i];
  511. dd = dst->planes[i] + parity * s->planewidth[i];
  512. av_image_copy_plane(dd, s->planewidth[i] << 1,
  513. ss, s->planewidth[i] << 1,
  514. s->planewidth[i], s->planeheight[i] >> 1);
  515. }
  516. }
  517. static void pullup_pack_frame(PullupContext *s, PullupFrame *fr)
  518. {
  519. int i;
  520. if (fr->buffer)
  521. return;
  522. if (fr->length < 2)
  523. return; /* FIXME: deal with this */
  524. for (i = 0; i < 2; i++) {
  525. if (fr->ofields[i]->lock[i^1])
  526. continue;
  527. fr->buffer = fr->ofields[i];
  528. pullup_lock_buffer(fr->buffer, 2);
  529. copy_field(s, fr->buffer, fr->ofields[i^1], i^1);
  530. return;
  531. }
  532. fr->buffer = pullup_get_buffer(s, 2);
  533. copy_field(s, fr->buffer, fr->ofields[0], 0);
  534. copy_field(s, fr->buffer, fr->ofields[1], 1);
  535. }
  536. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  537. {
  538. AVFilterContext *ctx = inlink->dst;
  539. AVFilterLink *outlink = ctx->outputs[0];
  540. PullupContext *s = ctx->priv;
  541. PullupBuffer *b;
  542. PullupFrame *f;
  543. AVFrame *out;
  544. int p, ret = 0;
  545. b = pullup_get_buffer(s, 2);
  546. if (!b) {
  547. av_log(ctx, AV_LOG_WARNING, "Could not get buffer!\n");
  548. f = pullup_get_frame(s);
  549. pullup_release_frame(f);
  550. goto end;
  551. }
  552. av_image_copy(b->planes, s->planewidth,
  553. (const uint8_t**)in->data, in->linesize,
  554. inlink->format, inlink->w, inlink->h);
  555. p = in->interlaced_frame ? !in->top_field_first : 0;
  556. pullup_submit_field(s, b, p );
  557. pullup_submit_field(s, b, p^1);
  558. if (in->repeat_pict)
  559. pullup_submit_field(s, b, p);
  560. pullup_release_buffer(b, 2);
  561. f = pullup_get_frame(s);
  562. if (!f)
  563. goto end;
  564. if (f->length < 2) {
  565. pullup_release_frame(f);
  566. f = pullup_get_frame(s);
  567. if (!f)
  568. goto end;
  569. if (f->length < 2) {
  570. pullup_release_frame(f);
  571. if (!in->repeat_pict)
  572. goto end;
  573. f = pullup_get_frame(s);
  574. if (!f)
  575. goto end;
  576. if (f->length < 2) {
  577. pullup_release_frame(f);
  578. goto end;
  579. }
  580. }
  581. }
  582. /* If the frame isn't already exportable... */
  583. if (!f->buffer)
  584. pullup_pack_frame(s, f);
  585. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  586. if (!out) {
  587. ret = AVERROR(ENOMEM);
  588. goto end;
  589. }
  590. av_frame_copy_props(out, in);
  591. av_image_copy(out->data, out->linesize,
  592. (const uint8_t**)f->buffer->planes, s->planewidth,
  593. inlink->format, inlink->w, inlink->h);
  594. ret = ff_filter_frame(outlink, out);
  595. pullup_release_frame(f);
  596. end:
  597. av_frame_free(&in);
  598. return ret;
  599. }
  600. static av_cold void uninit(AVFilterContext *ctx)
  601. {
  602. PullupContext *s = ctx->priv;
  603. int i;
  604. free_field_queue(s->head);
  605. s->last = NULL;
  606. for (i = 0; i < FF_ARRAY_ELEMS(s->buffers); i++) {
  607. av_freep(&s->buffers[i].planes[0]);
  608. av_freep(&s->buffers[i].planes[1]);
  609. av_freep(&s->buffers[i].planes[2]);
  610. }
  611. }
  612. static const AVFilterPad pullup_inputs[] = {
  613. {
  614. .name = "default",
  615. .type = AVMEDIA_TYPE_VIDEO,
  616. .filter_frame = filter_frame,
  617. .config_props = config_input,
  618. },
  619. { NULL }
  620. };
  621. static const AVFilterPad pullup_outputs[] = {
  622. {
  623. .name = "default",
  624. .type = AVMEDIA_TYPE_VIDEO,
  625. .config_props = config_output,
  626. },
  627. { NULL }
  628. };
  629. AVFilter ff_vf_pullup = {
  630. .name = "pullup",
  631. .description = NULL_IF_CONFIG_SMALL("Pullup from field sequence to frames."),
  632. .priv_size = sizeof(PullupContext),
  633. .priv_class = &pullup_class,
  634. .uninit = uninit,
  635. .query_formats = query_formats,
  636. .inputs = pullup_inputs,
  637. .outputs = pullup_outputs,
  638. };