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