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.

438 lines
13KB

  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-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. #define FILTER \
  40. for (x = 0; x < w; 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 spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
  50. + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
  51. \
  52. CHECK(-1) CHECK(-2) }} }} \
  53. CHECK( 1) CHECK( 2) }} }} \
  54. \
  55. if (mode < 2) { \
  56. int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
  57. int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
  58. int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
  59. int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
  60. \
  61. diff = FFMAX3(diff, min, -max); \
  62. } \
  63. \
  64. if (spatial_pred > d + diff) \
  65. spatial_pred = d + diff; \
  66. else if (spatial_pred < d - diff) \
  67. spatial_pred = d - diff; \
  68. \
  69. dst[0] = spatial_pred; \
  70. \
  71. dst++; \
  72. cur++; \
  73. prev++; \
  74. next++; \
  75. prev2++; \
  76. next2++; \
  77. }
  78. static void filter_line_c(void *dst1,
  79. void *prev1, void *cur1, void *next1,
  80. int w, int prefs, int mrefs, int parity, int mode)
  81. {
  82. uint8_t *dst = dst1;
  83. uint8_t *prev = prev1;
  84. uint8_t *cur = cur1;
  85. uint8_t *next = next1;
  86. int x;
  87. uint8_t *prev2 = parity ? prev : cur ;
  88. uint8_t *next2 = parity ? cur : next;
  89. FILTER
  90. }
  91. static void filter_line_c_16bit(void *dst1,
  92. void *prev1, void *cur1, void *next1,
  93. int w, int prefs, int mrefs, int parity,
  94. int mode)
  95. {
  96. uint16_t *dst = dst1;
  97. uint16_t *prev = prev1;
  98. uint16_t *cur = cur1;
  99. uint16_t *next = next1;
  100. int x;
  101. uint16_t *prev2 = parity ? prev : cur ;
  102. uint16_t *next2 = parity ? cur : next;
  103. mrefs /= 2;
  104. prefs /= 2;
  105. FILTER
  106. }
  107. static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
  108. int parity, int tff)
  109. {
  110. YADIFContext *yadif = ctx->priv;
  111. int y, i;
  112. for (i = 0; i < yadif->csp->nb_components; i++) {
  113. int w = dstpic->video->w;
  114. int h = dstpic->video->h;
  115. int refs = yadif->cur->linesize[i];
  116. int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
  117. if (i == 1 || i == 2) {
  118. /* Why is this not part of the per-plane description thing? */
  119. w >>= yadif->csp->log2_chroma_w;
  120. h >>= yadif->csp->log2_chroma_h;
  121. }
  122. for (y = 0; y < h; y++) {
  123. if ((y ^ parity) & 1) {
  124. uint8_t *prev = &yadif->prev->data[i][y * refs];
  125. uint8_t *cur = &yadif->cur ->data[i][y * refs];
  126. uint8_t *next = &yadif->next->data[i][y * refs];
  127. uint8_t *dst = &dstpic->data[i][y * dstpic->linesize[i]];
  128. int mode = y == 1 || y + 2 == h ? 2 : yadif->mode;
  129. yadif->filter_line(dst, prev, cur, next, w,
  130. y + 1 < h ? refs : -refs,
  131. y ? -refs : refs,
  132. parity ^ tff, mode);
  133. } else {
  134. memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
  135. &yadif->cur->data[i][y * refs], w * df);
  136. }
  137. }
  138. }
  139. emms_c();
  140. }
  141. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms,
  142. int w, int h)
  143. {
  144. AVFilterBufferRef *picref;
  145. int width = FFALIGN(w, 32);
  146. int height = FFALIGN(h + 2, 32);
  147. int i;
  148. picref = ff_default_get_video_buffer(link, perms, width, height);
  149. picref->video->w = w;
  150. picref->video->h = h;
  151. for (i = 0; i < 3; i++)
  152. picref->data[i] += picref->linesize[i];
  153. return picref;
  154. }
  155. static int return_frame(AVFilterContext *ctx, int is_second)
  156. {
  157. YADIFContext *yadif = ctx->priv;
  158. AVFilterLink *link = ctx->outputs[0];
  159. int tff, ret;
  160. if (yadif->parity == -1) {
  161. tff = yadif->cur->video->interlaced ?
  162. yadif->cur->video->top_field_first : 1;
  163. } else {
  164. tff = yadif->parity ^ 1;
  165. }
  166. if (is_second) {
  167. yadif->out = ff_get_video_buffer(link, PERM_RWP, link->w, link->h);
  168. if (!yadif->out)
  169. return AVERROR(ENOMEM);
  170. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  171. yadif->out->video->interlaced = 0;
  172. }
  173. if (!yadif->csp)
  174. yadif->csp = av_pix_fmt_desc_get(link->format);
  175. if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
  176. yadif->filter_line = filter_line_c_16bit;
  177. filter(ctx, yadif->out, tff ^ !is_second, tff);
  178. if (is_second) {
  179. int64_t cur_pts = yadif->cur->pts;
  180. int64_t next_pts = yadif->next->pts;
  181. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  182. yadif->out->pts = cur_pts + next_pts;
  183. } else {
  184. yadif->out->pts = AV_NOPTS_VALUE;
  185. }
  186. }
  187. ret = ff_filter_frame(ctx->outputs[0], yadif->out);
  188. yadif->frame_pending = (yadif->mode&1) && !is_second;
  189. return ret;
  190. }
  191. static int filter_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  192. {
  193. AVFilterContext *ctx = link->dst;
  194. YADIFContext *yadif = ctx->priv;
  195. if (yadif->frame_pending)
  196. return_frame(ctx, 1);
  197. if (yadif->prev)
  198. avfilter_unref_buffer(yadif->prev);
  199. yadif->prev = yadif->cur;
  200. yadif->cur = yadif->next;
  201. yadif->next = picref;
  202. if (!yadif->cur)
  203. return 0;
  204. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  205. yadif->out = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  206. if (!yadif->out)
  207. return AVERROR(ENOMEM);
  208. avfilter_unref_bufferp(&yadif->prev);
  209. if (yadif->out->pts != AV_NOPTS_VALUE)
  210. yadif->out->pts *= 2;
  211. return ff_filter_frame(ctx->outputs[0], yadif->out);
  212. }
  213. if (!yadif->prev &&
  214. !(yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ)))
  215. return AVERROR(ENOMEM);
  216. yadif->out = ff_get_video_buffer(ctx->outputs[0], PERM_RWP,
  217. link->w, link->h);
  218. if (!yadif->out)
  219. return AVERROR(ENOMEM);
  220. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  221. yadif->out->video->interlaced = 0;
  222. if (yadif->out->pts != AV_NOPTS_VALUE)
  223. yadif->out->pts *= 2;
  224. return return_frame(ctx, 0);
  225. }
  226. static int request_frame(AVFilterLink *link)
  227. {
  228. AVFilterContext *ctx = link->src;
  229. YADIFContext *yadif = ctx->priv;
  230. if (yadif->frame_pending) {
  231. return_frame(ctx, 1);
  232. return 0;
  233. }
  234. do {
  235. int ret;
  236. if (yadif->eof)
  237. return AVERROR_EOF;
  238. ret = ff_request_frame(link->src->inputs[0]);
  239. if (ret == AVERROR_EOF && yadif->next) {
  240. AVFilterBufferRef *next =
  241. avfilter_ref_buffer(yadif->next, AV_PERM_READ);
  242. if (!next)
  243. return AVERROR(ENOMEM);
  244. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  245. filter_frame(link->src->inputs[0], next);
  246. yadif->eof = 1;
  247. } else if (ret < 0) {
  248. return ret;
  249. }
  250. } while (!yadif->cur);
  251. return 0;
  252. }
  253. static int poll_frame(AVFilterLink *link)
  254. {
  255. YADIFContext *yadif = link->src->priv;
  256. int ret, val;
  257. if (yadif->frame_pending)
  258. return 1;
  259. val = ff_poll_frame(link->src->inputs[0]);
  260. if (val <= 0)
  261. return val;
  262. //FIXME change API to not requre this red tape
  263. if (val == 1 && !yadif->next) {
  264. if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
  265. return ret;
  266. val = ff_poll_frame(link->src->inputs[0]);
  267. if (val <= 0)
  268. return val;
  269. }
  270. assert(yadif->next || !val);
  271. if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
  272. return val;
  273. return val * ((yadif->mode&1)+1);
  274. }
  275. static av_cold void uninit(AVFilterContext *ctx)
  276. {
  277. YADIFContext *yadif = ctx->priv;
  278. if (yadif->prev) avfilter_unref_bufferp(&yadif->prev);
  279. if (yadif->cur ) avfilter_unref_bufferp(&yadif->cur );
  280. if (yadif->next) avfilter_unref_bufferp(&yadif->next);
  281. }
  282. static int query_formats(AVFilterContext *ctx)
  283. {
  284. static const enum AVPixelFormat pix_fmts[] = {
  285. AV_PIX_FMT_YUV420P,
  286. AV_PIX_FMT_YUV422P,
  287. AV_PIX_FMT_YUV444P,
  288. AV_PIX_FMT_YUV410P,
  289. AV_PIX_FMT_YUV411P,
  290. AV_PIX_FMT_GRAY8,
  291. AV_PIX_FMT_YUVJ420P,
  292. AV_PIX_FMT_YUVJ422P,
  293. AV_PIX_FMT_YUVJ444P,
  294. AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
  295. AV_PIX_FMT_YUV440P,
  296. AV_PIX_FMT_YUVJ440P,
  297. AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
  298. AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
  299. AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
  300. AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
  301. AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
  302. AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
  303. AV_PIX_FMT_YUVA420P,
  304. AV_PIX_FMT_NONE
  305. };
  306. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  307. return 0;
  308. }
  309. static av_cold int init(AVFilterContext *ctx, const char *args)
  310. {
  311. YADIFContext *yadif = ctx->priv;
  312. yadif->mode = 0;
  313. yadif->parity = -1;
  314. yadif->auto_enable = 0;
  315. yadif->csp = NULL;
  316. if (args)
  317. sscanf(args, "%d:%d:%d",
  318. &yadif->mode, &yadif->parity, &yadif->auto_enable);
  319. yadif->filter_line = filter_line_c;
  320. if (ARCH_X86)
  321. ff_yadif_init_x86(yadif);
  322. av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n",
  323. yadif->mode, yadif->parity, yadif->auto_enable);
  324. return 0;
  325. }
  326. static int config_props(AVFilterLink *link)
  327. {
  328. link->time_base.num = link->src->inputs[0]->time_base.num;
  329. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  330. link->w = link->src->inputs[0]->w;
  331. link->h = link->src->inputs[0]->h;
  332. return 0;
  333. }
  334. static const AVFilterPad avfilter_vf_yadif_inputs[] = {
  335. {
  336. .name = "default",
  337. .type = AVMEDIA_TYPE_VIDEO,
  338. .get_video_buffer = get_video_buffer,
  339. .filter_frame = filter_frame,
  340. },
  341. { NULL }
  342. };
  343. static const AVFilterPad avfilter_vf_yadif_outputs[] = {
  344. {
  345. .name = "default",
  346. .type = AVMEDIA_TYPE_VIDEO,
  347. .poll_frame = poll_frame,
  348. .request_frame = request_frame,
  349. .config_props = config_props,
  350. },
  351. { NULL }
  352. };
  353. AVFilter avfilter_vf_yadif = {
  354. .name = "yadif",
  355. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
  356. .priv_size = sizeof(YADIFContext),
  357. .init = init,
  358. .uninit = uninit,
  359. .query_formats = query_formats,
  360. .inputs = avfilter_vf_yadif_inputs,
  361. .outputs = avfilter_vf_yadif_outputs,
  362. };