You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

779 lines
20KB

  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, int 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, int 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, int 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)); // clear all pointers to avoid stale ones
  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. return 0;
  216. }
  217. static PullupBuffer *pullup_get_buffer(PullupContext *s, int parity)
  218. {
  219. int i;
  220. /* Try first to get the sister buffer for the previous field */
  221. if (parity < 2 && s->last && parity != s->last->parity
  222. && !s->last->buffer->lock[parity]) {
  223. alloc_buffer(s, s->last->buffer);
  224. return pullup_lock_buffer(s->last->buffer, parity);
  225. }
  226. /* Prefer a buffer with both fields open */
  227. for (i = 0; i < FF_ARRAY_ELEMS(s->buffers); i++) {
  228. if (s->buffers[i].lock[0])
  229. continue;
  230. if (s->buffers[i].lock[1])
  231. continue;
  232. alloc_buffer(s, &s->buffers[i]);
  233. return pullup_lock_buffer(&s->buffers[i], parity);
  234. }
  235. if (parity == 2)
  236. return 0;
  237. /* Search for any half-free buffer */
  238. for (i = 0; i < FF_ARRAY_ELEMS(s->buffers); i++) {
  239. if (((parity + 1) & 1) && s->buffers[i].lock[0])
  240. continue;
  241. if (((parity + 1) & 2) && s->buffers[i].lock[1])
  242. continue;
  243. alloc_buffer(s, &s->buffers[i]);
  244. return pullup_lock_buffer(&s->buffers[i], parity);
  245. }
  246. return NULL;
  247. }
  248. static int queue_length(PullupField *begin, PullupField *end)
  249. {
  250. PullupField *f;
  251. int count = 1;
  252. if (!begin || !end)
  253. return 0;
  254. for (f = begin; f != end; f = f->next)
  255. count++;
  256. return count;
  257. }
  258. static int find_first_break(PullupField *f, int max)
  259. {
  260. int i;
  261. for (i = 0; i < max; i++) {
  262. if (f->breaks & BREAK_RIGHT || f->next->breaks & BREAK_LEFT)
  263. return i + 1;
  264. f = f->next;
  265. }
  266. return 0;
  267. }
  268. static void compute_breaks(PullupContext *s, PullupField *f0)
  269. {
  270. PullupField *f1 = f0->next;
  271. PullupField *f2 = f1->next;
  272. PullupField *f3 = f2->next;
  273. int i, l, max_l = 0, max_r = 0;
  274. if (f0->flags & F_HAVE_BREAKS)
  275. return;
  276. f0->flags |= F_HAVE_BREAKS;
  277. /* Special case when fields are 100% identical */
  278. if (f0->buffer == f2->buffer && f1->buffer != f3->buffer) {
  279. f2->breaks |= BREAK_RIGHT;
  280. return;
  281. }
  282. if (f0->buffer != f2->buffer && f1->buffer == f3->buffer) {
  283. f1->breaks |= BREAK_LEFT;
  284. return;
  285. }
  286. for (i = 0; i < s->metric_length; i++) {
  287. l = f2->diffs[i] - f3->diffs[i];
  288. if ( l > max_l)
  289. max_l = l;
  290. if (-l > max_r)
  291. max_r = -l;
  292. }
  293. /* Don't get tripped up when differences are mostly quant error */
  294. if (max_l + max_r < 128)
  295. return;
  296. if (max_l > 4 * max_r)
  297. f1->breaks |= BREAK_LEFT;
  298. if (max_r > 4 * max_l)
  299. f2->breaks |= BREAK_RIGHT;
  300. }
  301. static void compute_affinity(PullupContext *s, PullupField *f)
  302. {
  303. int i, max_l = 0, max_r = 0, l;
  304. if (f->flags & F_HAVE_AFFINITY)
  305. return;
  306. f->flags |= F_HAVE_AFFINITY;
  307. if (f->buffer == f->next->next->buffer) {
  308. f->affinity = 1;
  309. f->next->affinity = 0;
  310. f->next->next->affinity = -1;
  311. f->next->flags |= F_HAVE_AFFINITY;
  312. f->next->next->flags |= F_HAVE_AFFINITY;
  313. return;
  314. }
  315. for (i = 0; i < s->metric_length; i++) {
  316. int v = f->vars[i];
  317. int lv = f->prev->vars[i];
  318. int rv = f->next->vars[i];
  319. int lc = f->combs[i] - (v + lv) + ABS(v - lv);
  320. int rc = f->next->combs[i] - (v + rv) + ABS(v - rv);
  321. lc = FFMAX(lc, 0);
  322. rc = FFMAX(rc, 0);
  323. l = lc - rc;
  324. if ( l > max_l)
  325. max_l = l;
  326. if (-l > max_r)
  327. max_r = -l;
  328. }
  329. if (max_l + max_r < 64)
  330. return;
  331. if (max_r > 6 * max_l)
  332. f->affinity = -1;
  333. else if (max_l > 6 * max_r)
  334. f->affinity = 1;
  335. }
  336. static int decide_frame_length(PullupContext *s)
  337. {
  338. PullupField *f0 = s->first;
  339. PullupField *f1 = f0->next;
  340. PullupField *f2 = f1->next;
  341. PullupField *f;
  342. int i, l, n;
  343. if (queue_length(s->first, s->last) < 4)
  344. return 0;
  345. f = s->first;
  346. n = queue_length(f, s->last);
  347. for (i = 0; i < n - 1; i++) {
  348. if (i < n - 3)
  349. compute_breaks(s, f);
  350. compute_affinity(s, f);
  351. f = f->next;
  352. }
  353. if (f0->affinity == -1)
  354. return 1;
  355. l = find_first_break(f0, 3);
  356. if (l == 1 && s->strict_breaks < 0)
  357. l = 0;
  358. switch (l) {
  359. case 1:
  360. return 1 + (s->strict_breaks < 1 && f0->affinity == 1 && f1->affinity == -1);
  361. case 2:
  362. /* FIXME: strictly speaking, f0->prev is no longer valid... :) */
  363. if (s->strict_pairs
  364. && (f0->prev->breaks & BREAK_RIGHT) && (f2->breaks & BREAK_LEFT)
  365. && (f0->affinity != 1 || f1->affinity != -1) )
  366. return 1;
  367. return 1 + (f1->affinity != 1);
  368. case 3:
  369. return 2 + (f2->affinity != 1);
  370. default:
  371. /* 9 possibilities covered before switch */
  372. if (f1->affinity == 1)
  373. return 1; /* covers 6 */
  374. else if (f1->affinity == -1)
  375. return 2; /* covers 6 */
  376. else if (f2->affinity == -1) { /* covers 2 */
  377. return (f0->affinity == 1) ? 3 : 1;
  378. } else {
  379. return 2; /* the remaining 6 */
  380. }
  381. }
  382. }
  383. static PullupFrame *pullup_get_frame(PullupContext *s)
  384. {
  385. PullupFrame *fr = &s->frame;
  386. int i, n = decide_frame_length(s);
  387. int aff = s->first->next->affinity;
  388. av_assert1(n < FF_ARRAY_ELEMS(fr->ifields));
  389. if (!n || fr->lock)
  390. return NULL;
  391. fr->lock++;
  392. fr->length = n;
  393. fr->parity = s->first->parity;
  394. fr->buffer = 0;
  395. for (i = 0; i < n; i++) {
  396. /* We cheat and steal the buffer without release+relock */
  397. fr->ifields[i] = s->first->buffer;
  398. s->first->buffer = 0;
  399. s->first = s->first->next;
  400. }
  401. if (n == 1) {
  402. fr->ofields[fr->parity ] = fr->ifields[0];
  403. fr->ofields[fr->parity ^ 1] = 0;
  404. } else if (n == 2) {
  405. fr->ofields[fr->parity ] = fr->ifields[0];
  406. fr->ofields[fr->parity ^ 1] = fr->ifields[1];
  407. } else if (n == 3) {
  408. if (!aff)
  409. aff = (fr->ifields[0] == fr->ifields[1]) ? -1 : 1;
  410. fr->ofields[fr->parity ] = fr->ifields[1 + aff];
  411. fr->ofields[fr->parity ^ 1] = fr->ifields[1 ];
  412. }
  413. pullup_lock_buffer(fr->ofields[0], 0);
  414. pullup_lock_buffer(fr->ofields[1], 1);
  415. if (fr->ofields[0] == fr->ofields[1]) {
  416. fr->buffer = fr->ofields[0];
  417. pullup_lock_buffer(fr->buffer, 2);
  418. return fr;
  419. }
  420. return fr;
  421. }
  422. static void pullup_release_frame(PullupFrame *f)
  423. {
  424. int i;
  425. for (i = 0; i < f->length; i++)
  426. pullup_release_buffer(f->ifields[i], f->parity ^ (i & 1));
  427. pullup_release_buffer(f->ofields[0], 0);
  428. pullup_release_buffer(f->ofields[1], 1);
  429. if (f->buffer)
  430. pullup_release_buffer(f->buffer, 2);
  431. f->lock--;
  432. }
  433. static void compute_metric(PullupContext *s, int *dest,
  434. PullupField *fa, int pa, PullupField *fb, int pb,
  435. int (*func)(const uint8_t *, const uint8_t *, int))
  436. {
  437. int mp = s->metric_plane;
  438. int xstep = 8;
  439. int ystep = s->planewidth[mp] << 3;
  440. int stride = s->planewidth[mp] << 1; /* field stride */
  441. int w = s->metric_w * xstep;
  442. uint8_t *a, *b;
  443. int x, y;
  444. if (!fa->buffer || !fb->buffer)
  445. return;
  446. /* Shortcut for duplicate fields (e.g. from RFF flag) */
  447. if (fa->buffer == fb->buffer && pa == pb) {
  448. memset(dest, 0, s->metric_length * sizeof(*dest));
  449. return;
  450. }
  451. a = fa->buffer->planes[mp] + pa * s->planewidth[mp] + s->metric_offset;
  452. b = fb->buffer->planes[mp] + pb * s->planewidth[mp] + s->metric_offset;
  453. for (y = 0; y < s->metric_h; y++) {
  454. for (x = 0; x < w; x += xstep)
  455. *dest++ = func(a + x, b + x, stride);
  456. a += ystep; b += ystep;
  457. }
  458. }
  459. static int check_field_queue(PullupContext *s)
  460. {
  461. int ret;
  462. if (s->head->next == s->first) {
  463. PullupField *f = av_mallocz(sizeof(*f));
  464. if (!f)
  465. return AVERROR(ENOMEM);
  466. if ((ret = alloc_metrics(s, f)) < 0) {
  467. av_free(f);
  468. return ret;
  469. }
  470. f->prev = s->head;
  471. f->next = s->first;
  472. s->head->next = f;
  473. s->first->prev = f;
  474. }
  475. return 0;
  476. }
  477. static void pullup_submit_field(PullupContext *s, PullupBuffer *b, int parity)
  478. {
  479. PullupField *f;
  480. /* Grow the circular list if needed */
  481. if (check_field_queue(s) < 0)
  482. return;
  483. /* Cannot have two fields of same parity in a row; drop the new one */
  484. if (s->last && s->last->parity == parity)
  485. return;
  486. f = s->head;
  487. f->parity = parity;
  488. f->buffer = pullup_lock_buffer(b, parity);
  489. f->flags = 0;
  490. f->breaks = 0;
  491. f->affinity = 0;
  492. compute_metric(s, f->diffs, f, parity, f->prev->prev, parity, s->diff);
  493. compute_metric(s, f->combs, parity ? f->prev : f, 0, parity ? f : f->prev, 1, s->comb);
  494. compute_metric(s, f->vars, f, parity, f, -1, s->var);
  495. emms_c();
  496. /* Advance the circular list */
  497. if (!s->first)
  498. s->first = s->head;
  499. s->last = s->head;
  500. s->head = s->head->next;
  501. }
  502. static void copy_field(PullupContext *s,
  503. PullupBuffer *dst, PullupBuffer *src, int parity)
  504. {
  505. uint8_t *dd, *ss;
  506. int i;
  507. for (i = 0; i < s->nb_planes; i++) {
  508. ss = src->planes[i] + parity * s->planewidth[i];
  509. dd = dst->planes[i] + parity * s->planewidth[i];
  510. av_image_copy_plane(dd, s->planewidth[i] << 1,
  511. ss, s->planewidth[i] << 1,
  512. s->planewidth[i], s->planeheight[i] >> 1);
  513. }
  514. }
  515. static void pullup_pack_frame(PullupContext *s, PullupFrame *fr)
  516. {
  517. int i;
  518. if (fr->buffer)
  519. return;
  520. if (fr->length < 2)
  521. return; /* FIXME: deal with this */
  522. for (i = 0; i < 2; i++) {
  523. if (fr->ofields[i]->lock[i^1])
  524. continue;
  525. fr->buffer = fr->ofields[i];
  526. pullup_lock_buffer(fr->buffer, 2);
  527. copy_field(s, fr->buffer, fr->ofields[i^1], i^1);
  528. return;
  529. }
  530. fr->buffer = pullup_get_buffer(s, 2);
  531. copy_field(s, fr->buffer, fr->ofields[0], 0);
  532. copy_field(s, fr->buffer, fr->ofields[1], 1);
  533. }
  534. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  535. {
  536. AVFilterContext *ctx = inlink->dst;
  537. AVFilterLink *outlink = ctx->outputs[0];
  538. PullupContext *s = ctx->priv;
  539. PullupBuffer *b;
  540. PullupFrame *f;
  541. AVFrame *out;
  542. int p, ret = 0;
  543. b = pullup_get_buffer(s, 2);
  544. if (!b) {
  545. av_log(ctx, AV_LOG_WARNING, "Could not get buffer!\n");
  546. f = pullup_get_frame(s);
  547. pullup_release_frame(f);
  548. goto end;
  549. }
  550. av_image_copy(b->planes, s->planewidth,
  551. (const uint8_t**)in->data, in->linesize,
  552. inlink->format, inlink->w, inlink->h);
  553. p = in->interlaced_frame ? !in->top_field_first : 0;
  554. pullup_submit_field(s, b, p );
  555. pullup_submit_field(s, b, p^1);
  556. if (in->repeat_pict)
  557. pullup_submit_field(s, b, p);
  558. pullup_release_buffer(b, 2);
  559. f = pullup_get_frame(s);
  560. if (!f)
  561. goto end;
  562. if (f->length < 2) {
  563. pullup_release_frame(f);
  564. f = pullup_get_frame(s);
  565. if (!f)
  566. goto end;
  567. if (f->length < 2) {
  568. pullup_release_frame(f);
  569. if (!in->repeat_pict)
  570. goto end;
  571. f = pullup_get_frame(s);
  572. if (!f)
  573. goto end;
  574. if (f->length < 2) {
  575. pullup_release_frame(f);
  576. goto end;
  577. }
  578. }
  579. }
  580. /* If the frame isn't already exportable... */
  581. if (!f->buffer)
  582. pullup_pack_frame(s, f);
  583. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  584. if (!out) {
  585. ret = AVERROR(ENOMEM);
  586. goto end;
  587. }
  588. av_frame_copy_props(out, in);
  589. av_image_copy(out->data, out->linesize,
  590. (const uint8_t**)f->buffer->planes, s->planewidth,
  591. inlink->format, inlink->w, inlink->h);
  592. ret = ff_filter_frame(outlink, out);
  593. pullup_release_frame(f);
  594. end:
  595. av_frame_free(&in);
  596. return ret;
  597. }
  598. static av_cold void uninit(AVFilterContext *ctx)
  599. {
  600. PullupContext *s = ctx->priv;
  601. int i;
  602. free_field_queue(s->head);
  603. s->last = NULL;
  604. for (i = 0; i < FF_ARRAY_ELEMS(s->buffers); i++) {
  605. av_freep(&s->buffers[i].planes[0]);
  606. av_freep(&s->buffers[i].planes[1]);
  607. av_freep(&s->buffers[i].planes[2]);
  608. }
  609. }
  610. static const AVFilterPad pullup_inputs[] = {
  611. {
  612. .name = "default",
  613. .type = AVMEDIA_TYPE_VIDEO,
  614. .filter_frame = filter_frame,
  615. .config_props = config_input,
  616. },
  617. { NULL }
  618. };
  619. static const AVFilterPad pullup_outputs[] = {
  620. {
  621. .name = "default",
  622. .type = AVMEDIA_TYPE_VIDEO,
  623. .config_props = config_output,
  624. },
  625. { NULL }
  626. };
  627. AVFilter ff_vf_pullup = {
  628. .name = "pullup",
  629. .description = NULL_IF_CONFIG_SMALL("Pullup from field sequence to frames."),
  630. .priv_size = sizeof(PullupContext),
  631. .priv_class = &pullup_class,
  632. .uninit = uninit,
  633. .query_formats = query_formats,
  634. .inputs = pullup_inputs,
  635. .outputs = pullup_outputs,
  636. };