vf_yadif.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
  3. * 2010 James Darnley <james.darnley@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/cpu.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. #include "yadif.h"
  30. #undef NDEBUG
  31. #include <assert.h>
  32. typedef struct ThreadData {
  33. AVFrame *frame;
  34. int plane;
  35. int w, h;
  36. int parity;
  37. int tff;
  38. } ThreadData;
  39. #define CHECK(j)\
  40. { int score = FFABS(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\
  41. + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
  42. + FFABS(cur[mrefs + 1 + (j)] - cur[prefs + 1 - (j)]);\
  43. if (score < spatial_score) {\
  44. spatial_score= score;\
  45. spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
  46. /* The is_not_edge argument here controls when the code will enter a branch
  47. * which reads up to and including x-3 and x+3. */
  48. #define FILTER(start, end, is_not_edge) \
  49. for (x = start; x < end; x++) { \
  50. int c = cur[mrefs]; \
  51. int d = (prev2[0] + next2[0])>>1; \
  52. int e = cur[prefs]; \
  53. int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
  54. int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
  55. int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
  56. int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
  57. int spatial_pred = (c+e) >> 1; \
  58. \
  59. if (is_not_edge) {\
  60. int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
  61. + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
  62. CHECK(-1) CHECK(-2) }} }} \
  63. CHECK( 1) CHECK( 2) }} }} \
  64. }\
  65. \
  66. if (mode < 2) { \
  67. int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
  68. int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
  69. int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
  70. int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
  71. \
  72. diff = FFMAX3(diff, min, -max); \
  73. } \
  74. \
  75. if (spatial_pred > d + diff) \
  76. spatial_pred = d + diff; \
  77. else if (spatial_pred < d - diff) \
  78. spatial_pred = d - diff; \
  79. \
  80. dst[0] = spatial_pred; \
  81. \
  82. dst++; \
  83. cur++; \
  84. prev++; \
  85. next++; \
  86. prev2++; \
  87. next2++; \
  88. }
  89. static void filter_line_c(void *dst1,
  90. void *prev1, void *cur1, void *next1,
  91. int w, int prefs, int mrefs, int parity, int mode)
  92. {
  93. uint8_t *dst = dst1;
  94. uint8_t *prev = prev1;
  95. uint8_t *cur = cur1;
  96. uint8_t *next = next1;
  97. int x;
  98. uint8_t *prev2 = parity ? prev : cur ;
  99. uint8_t *next2 = parity ? cur : next;
  100. /* The function is called with the pointers already pointing to data[3] and
  101. * with 6 subtracted from the width. This allows the FILTER macro to be
  102. * called so that it processes all the pixels normally. A constant value of
  103. * true for is_not_edge lets the compiler ignore the if statement. */
  104. FILTER(0, w, 1)
  105. }
  106. static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
  107. int w, int prefs, int mrefs, int parity, int mode)
  108. {
  109. uint8_t *dst = dst1;
  110. uint8_t *prev = prev1;
  111. uint8_t *cur = cur1;
  112. uint8_t *next = next1;
  113. int x;
  114. uint8_t *prev2 = parity ? prev : cur ;
  115. uint8_t *next2 = parity ? cur : next;
  116. /* Only edge pixels need to be processed here. A constant value of false
  117. * for is_not_edge should let the compiler ignore the whole branch. */
  118. FILTER(0, 3, 0)
  119. dst = (uint8_t*)dst1 + w - 3;
  120. prev = (uint8_t*)prev1 + w - 3;
  121. cur = (uint8_t*)cur1 + w - 3;
  122. next = (uint8_t*)next1 + w - 3;
  123. prev2 = (uint8_t*)(parity ? prev : cur);
  124. next2 = (uint8_t*)(parity ? cur : next);
  125. FILTER(w - 3, w, 0)
  126. }
  127. static void filter_line_c_16bit(void *dst1,
  128. void *prev1, void *cur1, void *next1,
  129. int w, int prefs, int mrefs, int parity,
  130. int mode)
  131. {
  132. uint16_t *dst = dst1;
  133. uint16_t *prev = prev1;
  134. uint16_t *cur = cur1;
  135. uint16_t *next = next1;
  136. int x;
  137. uint16_t *prev2 = parity ? prev : cur ;
  138. uint16_t *next2 = parity ? cur : next;
  139. mrefs /= 2;
  140. prefs /= 2;
  141. FILTER(0, w, 1)
  142. }
  143. static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
  144. int w, int prefs, int mrefs, int parity, int mode)
  145. {
  146. uint16_t *dst = dst1;
  147. uint16_t *prev = prev1;
  148. uint16_t *cur = cur1;
  149. uint16_t *next = next1;
  150. int x;
  151. uint16_t *prev2 = parity ? prev : cur ;
  152. uint16_t *next2 = parity ? cur : next;
  153. mrefs /= 2;
  154. prefs /= 2;
  155. FILTER(0, 3, 0)
  156. dst = (uint16_t*)dst1 + w - 3;
  157. prev = (uint16_t*)prev1 + w - 3;
  158. cur = (uint16_t*)cur1 + w - 3;
  159. next = (uint16_t*)next1 + w - 3;
  160. prev2 = (uint16_t*)(parity ? prev : cur);
  161. next2 = (uint16_t*)(parity ? cur : next);
  162. FILTER(w - 3, w, 0)
  163. }
  164. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  165. {
  166. YADIFContext *s = ctx->priv;
  167. ThreadData *td = arg;
  168. int refs = s->cur->linesize[td->plane];
  169. int df = (s->csp->comp[td->plane].depth + 7) / 8;
  170. int pix_3 = 3 * df;
  171. int slice_h = td->h / nb_jobs;
  172. int slice_start = jobnr * slice_h;
  173. int slice_end = (jobnr == nb_jobs - 1) ? td->h : (jobnr + 1) * slice_h;
  174. int y;
  175. /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
  176. * we need to call the c variant which avoids this for border pixels
  177. */
  178. for (y = slice_start; y < slice_end; y++) {
  179. if ((y ^ td->parity) & 1) {
  180. uint8_t *prev = &s->prev->data[td->plane][y * refs];
  181. uint8_t *cur = &s->cur ->data[td->plane][y * refs];
  182. uint8_t *next = &s->next->data[td->plane][y * refs];
  183. uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
  184. int mode = y == 1 || y + 2 == td->h ? 2 : s->mode;
  185. s->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
  186. next + pix_3, td->w - 6,
  187. y + 1 < td->h ? refs : -refs,
  188. y ? -refs : refs,
  189. td->parity ^ td->tff, mode);
  190. s->filter_edges(dst, prev, cur, next, td->w,
  191. y + 1 < td->h ? refs : -refs,
  192. y ? -refs : refs,
  193. td->parity ^ td->tff, mode);
  194. } else {
  195. memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
  196. &s->cur->data[td->plane][y * refs], td->w * df);
  197. }
  198. }
  199. return 0;
  200. }
  201. static void filter(AVFilterContext *ctx, AVFrame *dstpic,
  202. int parity, int tff)
  203. {
  204. YADIFContext *yadif = ctx->priv;
  205. ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
  206. int i;
  207. for (i = 0; i < yadif->csp->nb_components; i++) {
  208. int w = dstpic->width;
  209. int h = dstpic->height;
  210. if (i == 1 || i == 2) {
  211. w >>= yadif->csp->log2_chroma_w;
  212. h >>= yadif->csp->log2_chroma_h;
  213. }
  214. td.w = w;
  215. td.h = h;
  216. td.plane = i;
  217. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ctx->graph->nb_threads));
  218. }
  219. emms_c();
  220. }
  221. static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
  222. {
  223. AVFrame *frame;
  224. int width = FFALIGN(w, 32);
  225. int height = FFALIGN(h + 2, 32);
  226. int i;
  227. frame = ff_default_get_video_buffer(link, width, height);
  228. frame->width = w;
  229. frame->height = h;
  230. for (i = 0; i < 3; i++)
  231. frame->data[i] += frame->linesize[i];
  232. return frame;
  233. }
  234. static int return_frame(AVFilterContext *ctx, int is_second)
  235. {
  236. YADIFContext *yadif = ctx->priv;
  237. AVFilterLink *link = ctx->outputs[0];
  238. int tff, ret;
  239. if (yadif->parity == -1) {
  240. tff = yadif->cur->interlaced_frame ?
  241. yadif->cur->top_field_first : 1;
  242. } else {
  243. tff = yadif->parity ^ 1;
  244. }
  245. if (is_second) {
  246. yadif->out = ff_get_video_buffer(link, link->w, link->h);
  247. if (!yadif->out)
  248. return AVERROR(ENOMEM);
  249. av_frame_copy_props(yadif->out, yadif->cur);
  250. yadif->out->interlaced_frame = 0;
  251. }
  252. filter(ctx, yadif->out, tff ^ !is_second, tff);
  253. if (is_second) {
  254. int64_t cur_pts = yadif->cur->pts;
  255. int64_t next_pts = yadif->next->pts;
  256. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  257. yadif->out->pts = cur_pts + next_pts;
  258. } else {
  259. yadif->out->pts = AV_NOPTS_VALUE;
  260. }
  261. }
  262. ret = ff_filter_frame(ctx->outputs[0], yadif->out);
  263. yadif->frame_pending = (yadif->mode&1) && !is_second;
  264. return ret;
  265. }
  266. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  267. {
  268. AVFilterContext *ctx = link->dst;
  269. YADIFContext *yadif = ctx->priv;
  270. if (yadif->frame_pending)
  271. return_frame(ctx, 1);
  272. if (yadif->prev)
  273. av_frame_free(&yadif->prev);
  274. yadif->prev = yadif->cur;
  275. yadif->cur = yadif->next;
  276. yadif->next = frame;
  277. if (!yadif->cur)
  278. return 0;
  279. if (yadif->auto_enable && !yadif->cur->interlaced_frame) {
  280. yadif->out = av_frame_clone(yadif->cur);
  281. if (!yadif->out)
  282. return AVERROR(ENOMEM);
  283. av_frame_free(&yadif->prev);
  284. if (yadif->out->pts != AV_NOPTS_VALUE)
  285. yadif->out->pts *= 2;
  286. return ff_filter_frame(ctx->outputs[0], yadif->out);
  287. }
  288. if (!yadif->prev &&
  289. !(yadif->prev = av_frame_clone(yadif->cur)))
  290. return AVERROR(ENOMEM);
  291. yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
  292. if (!yadif->out)
  293. return AVERROR(ENOMEM);
  294. av_frame_copy_props(yadif->out, yadif->cur);
  295. yadif->out->interlaced_frame = 0;
  296. if (yadif->out->pts != AV_NOPTS_VALUE)
  297. yadif->out->pts *= 2;
  298. return return_frame(ctx, 0);
  299. }
  300. static int request_frame(AVFilterLink *link)
  301. {
  302. AVFilterContext *ctx = link->src;
  303. YADIFContext *yadif = ctx->priv;
  304. if (yadif->frame_pending) {
  305. return_frame(ctx, 1);
  306. return 0;
  307. }
  308. do {
  309. int ret;
  310. if (yadif->eof)
  311. return AVERROR_EOF;
  312. ret = ff_request_frame(link->src->inputs[0]);
  313. if (ret == AVERROR_EOF && yadif->next) {
  314. AVFrame *next = av_frame_clone(yadif->next);
  315. if (!next)
  316. return AVERROR(ENOMEM);
  317. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  318. filter_frame(link->src->inputs[0], next);
  319. yadif->eof = 1;
  320. } else if (ret < 0) {
  321. return ret;
  322. }
  323. } while (!yadif->cur);
  324. return 0;
  325. }
  326. static int poll_frame(AVFilterLink *link)
  327. {
  328. YADIFContext *yadif = link->src->priv;
  329. int ret, val;
  330. if (yadif->frame_pending)
  331. return 1;
  332. val = ff_poll_frame(link->src->inputs[0]);
  333. if (val <= 0)
  334. return val;
  335. //FIXME change API to not require this red tape
  336. if (val == 1 && !yadif->next) {
  337. if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
  338. return ret;
  339. val = ff_poll_frame(link->src->inputs[0]);
  340. if (val <= 0)
  341. return val;
  342. }
  343. assert(yadif->next || !val);
  344. if (yadif->auto_enable && yadif->next && !yadif->next->interlaced_frame)
  345. return val;
  346. return val * ((yadif->mode&1)+1);
  347. }
  348. static av_cold void uninit(AVFilterContext *ctx)
  349. {
  350. YADIFContext *yadif = ctx->priv;
  351. if (yadif->prev) av_frame_free(&yadif->prev);
  352. if (yadif->cur ) av_frame_free(&yadif->cur );
  353. if (yadif->next) av_frame_free(&yadif->next);
  354. }
  355. static int query_formats(AVFilterContext *ctx)
  356. {
  357. static const enum AVPixelFormat pix_fmts[] = {
  358. AV_PIX_FMT_YUV420P,
  359. AV_PIX_FMT_YUV422P,
  360. AV_PIX_FMT_YUV444P,
  361. AV_PIX_FMT_YUV410P,
  362. AV_PIX_FMT_YUV411P,
  363. AV_PIX_FMT_GRAY8,
  364. AV_PIX_FMT_YUVJ420P,
  365. AV_PIX_FMT_YUVJ422P,
  366. AV_PIX_FMT_YUVJ444P,
  367. AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
  368. AV_PIX_FMT_YUV440P,
  369. AV_PIX_FMT_YUVJ440P,
  370. AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
  371. AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
  372. AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
  373. AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
  374. AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
  375. AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
  376. AV_PIX_FMT_YUVA420P,
  377. AV_PIX_FMT_NONE
  378. };
  379. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  380. return 0;
  381. }
  382. static int config_props(AVFilterLink *link)
  383. {
  384. YADIFContext *s = link->src->priv;
  385. link->time_base.num = link->src->inputs[0]->time_base.num;
  386. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  387. link->w = link->src->inputs[0]->w;
  388. link->h = link->src->inputs[0]->h;
  389. if (s->mode & 1)
  390. link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate,
  391. (AVRational){2, 1});
  392. s->csp = av_pix_fmt_desc_get(link->format);
  393. if (s->csp->comp[0].depth > 8) {
  394. s->filter_line = filter_line_c_16bit;
  395. s->filter_edges = filter_edges_16bit;
  396. } else {
  397. s->filter_line = filter_line_c;
  398. s->filter_edges = filter_edges;
  399. if (ARCH_X86)
  400. ff_yadif_init_x86(s);
  401. }
  402. return 0;
  403. }
  404. #define OFFSET(x) offsetof(YADIFContext, x)
  405. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  406. static const AVOption options[] = {
  407. { "mode", NULL, OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 3, FLAGS },
  408. { "parity", NULL, OFFSET(parity), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, FLAGS, "parity" },
  409. { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, .unit = "parity" },
  410. { "tff", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .unit = "parity" },
  411. { "bff", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .unit = "parity" },
  412. { "auto", NULL, OFFSET(auto_enable), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  413. { NULL },
  414. };
  415. static const AVClass yadif_class = {
  416. .class_name = "yadif",
  417. .item_name = av_default_item_name,
  418. .option = options,
  419. .version = LIBAVUTIL_VERSION_INT,
  420. };
  421. static const AVFilterPad avfilter_vf_yadif_inputs[] = {
  422. {
  423. .name = "default",
  424. .type = AVMEDIA_TYPE_VIDEO,
  425. .get_video_buffer = get_video_buffer,
  426. .filter_frame = filter_frame,
  427. },
  428. { NULL }
  429. };
  430. static const AVFilterPad avfilter_vf_yadif_outputs[] = {
  431. {
  432. .name = "default",
  433. .type = AVMEDIA_TYPE_VIDEO,
  434. .poll_frame = poll_frame,
  435. .request_frame = request_frame,
  436. .config_props = config_props,
  437. },
  438. { NULL }
  439. };
  440. AVFilter ff_vf_yadif = {
  441. .name = "yadif",
  442. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
  443. .priv_size = sizeof(YADIFContext),
  444. .priv_class = &yadif_class,
  445. .uninit = uninit,
  446. .query_formats = query_formats,
  447. .inputs = avfilter_vf_yadif_inputs,
  448. .outputs = avfilter_vf_yadif_outputs,
  449. .flags = AVFILTER_FLAG_SLICE_THREADS,
  450. };