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.

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