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.

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