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.

508 lines
15KB

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