vf_pullup.c 20 KB

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