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.

451 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(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_descriptors[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. ret = ff_start_frame(ctx->outputs[0], yadif->out);
  179. if (ret < 0)
  180. return ret;
  181. }
  182. if ((ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1)) < 0 ||
  183. (ret = ff_end_frame(ctx->outputs[0])) < 0)
  184. return ret;
  185. yadif->frame_pending = (yadif->mode&1) && !is_second;
  186. return 0;
  187. }
  188. static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  189. {
  190. AVFilterContext *ctx = link->dst;
  191. YADIFContext *yadif = ctx->priv;
  192. if (yadif->frame_pending)
  193. return_frame(ctx, 1);
  194. if (yadif->prev)
  195. avfilter_unref_buffer(yadif->prev);
  196. yadif->prev = yadif->cur;
  197. yadif->cur = yadif->next;
  198. yadif->next = picref;
  199. link->cur_buf = NULL;
  200. if (!yadif->cur)
  201. return 0;
  202. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  203. yadif->out = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  204. if (!yadif->out)
  205. return AVERROR(ENOMEM);
  206. avfilter_unref_bufferp(&yadif->prev);
  207. if (yadif->out->pts != AV_NOPTS_VALUE)
  208. yadif->out->pts *= 2;
  209. return ff_start_frame(ctx->outputs[0], yadif->out);
  210. }
  211. if (!yadif->prev &&
  212. !(yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ)))
  213. return AVERROR(ENOMEM);
  214. yadif->out = ff_get_video_buffer(ctx->outputs[0], PERM_RWP,
  215. link->w, link->h);
  216. if (!yadif->out)
  217. return AVERROR(ENOMEM);
  218. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  219. yadif->out->video->interlaced = 0;
  220. if (yadif->out->pts != AV_NOPTS_VALUE)
  221. yadif->out->pts *= 2;
  222. return ff_start_frame(ctx->outputs[0], yadif->out);
  223. }
  224. static int end_frame(AVFilterLink *link)
  225. {
  226. AVFilterContext *ctx = link->dst;
  227. YADIFContext *yadif = ctx->priv;
  228. if (!yadif->out)
  229. return 0;
  230. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  231. int ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
  232. if (ret >= 0)
  233. ret = ff_end_frame(ctx->outputs[0]);
  234. return ret;
  235. }
  236. return_frame(ctx, 0);
  237. return 0;
  238. }
  239. static int request_frame(AVFilterLink *link)
  240. {
  241. AVFilterContext *ctx = link->src;
  242. YADIFContext *yadif = ctx->priv;
  243. if (yadif->frame_pending) {
  244. return_frame(ctx, 1);
  245. return 0;
  246. }
  247. do {
  248. int ret;
  249. if (yadif->eof)
  250. return AVERROR_EOF;
  251. ret = ff_request_frame(link->src->inputs[0]);
  252. if (ret == AVERROR_EOF && yadif->next) {
  253. AVFilterBufferRef *next =
  254. avfilter_ref_buffer(yadif->next, AV_PERM_READ);
  255. if (!next)
  256. return AVERROR(ENOMEM);
  257. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  258. start_frame(link->src->inputs[0], next);
  259. end_frame(link->src->inputs[0]);
  260. yadif->eof = 1;
  261. } else if (ret < 0) {
  262. return ret;
  263. }
  264. } while (!yadif->cur);
  265. return 0;
  266. }
  267. static int poll_frame(AVFilterLink *link)
  268. {
  269. YADIFContext *yadif = link->src->priv;
  270. int ret, val;
  271. if (yadif->frame_pending)
  272. return 1;
  273. val = ff_poll_frame(link->src->inputs[0]);
  274. if (val <= 0)
  275. return val;
  276. //FIXME change API to not requre this red tape
  277. if (val == 1 && !yadif->next) {
  278. if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
  279. return ret;
  280. val = ff_poll_frame(link->src->inputs[0]);
  281. if (val <= 0)
  282. return val;
  283. }
  284. assert(yadif->next || !val);
  285. if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
  286. return val;
  287. return val * ((yadif->mode&1)+1);
  288. }
  289. static av_cold void uninit(AVFilterContext *ctx)
  290. {
  291. YADIFContext *yadif = ctx->priv;
  292. if (yadif->prev) avfilter_unref_bufferp(&yadif->prev);
  293. if (yadif->cur ) avfilter_unref_bufferp(&yadif->cur );
  294. if (yadif->next) avfilter_unref_bufferp(&yadif->next);
  295. }
  296. static int query_formats(AVFilterContext *ctx)
  297. {
  298. static const enum PixelFormat pix_fmts[] = {
  299. PIX_FMT_YUV420P,
  300. PIX_FMT_YUV422P,
  301. PIX_FMT_YUV444P,
  302. PIX_FMT_YUV410P,
  303. PIX_FMT_YUV411P,
  304. PIX_FMT_GRAY8,
  305. PIX_FMT_YUVJ420P,
  306. PIX_FMT_YUVJ422P,
  307. PIX_FMT_YUVJ444P,
  308. AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
  309. PIX_FMT_YUV440P,
  310. PIX_FMT_YUVJ440P,
  311. AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
  312. AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
  313. AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
  314. AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
  315. AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
  316. AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
  317. PIX_FMT_YUVA420P,
  318. PIX_FMT_NONE
  319. };
  320. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  321. return 0;
  322. }
  323. static av_cold int init(AVFilterContext *ctx, const char *args)
  324. {
  325. YADIFContext *yadif = ctx->priv;
  326. yadif->mode = 0;
  327. yadif->parity = -1;
  328. yadif->auto_enable = 0;
  329. yadif->csp = NULL;
  330. if (args)
  331. sscanf(args, "%d:%d:%d",
  332. &yadif->mode, &yadif->parity, &yadif->auto_enable);
  333. yadif->filter_line = filter_line_c;
  334. if (HAVE_MMX)
  335. ff_yadif_init_x86(yadif);
  336. av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n",
  337. yadif->mode, yadif->parity, yadif->auto_enable);
  338. return 0;
  339. }
  340. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  341. {
  342. return 0;
  343. }
  344. static int config_props(AVFilterLink *link)
  345. {
  346. link->time_base.num = link->src->inputs[0]->time_base.num;
  347. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  348. link->w = link->src->inputs[0]->w;
  349. link->h = link->src->inputs[0]->h;
  350. return 0;
  351. }
  352. AVFilter avfilter_vf_yadif = {
  353. .name = "yadif",
  354. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
  355. .priv_size = sizeof(YADIFContext),
  356. .init = init,
  357. .uninit = uninit,
  358. .query_formats = query_formats,
  359. .inputs = (const AVFilterPad[]) {{ .name = "default",
  360. .type = AVMEDIA_TYPE_VIDEO,
  361. .start_frame = start_frame,
  362. .get_video_buffer = get_video_buffer,
  363. .draw_slice = null_draw_slice,
  364. .end_frame = end_frame, },
  365. { .name = NULL}},
  366. .outputs = (const AVFilterPad[]) {{ .name = "default",
  367. .type = AVMEDIA_TYPE_VIDEO,
  368. .poll_frame = poll_frame,
  369. .request_frame = request_frame,
  370. .config_props = config_props, },
  371. { .name = NULL}},
  372. };