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.

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