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.

430 lines
12KB

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