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.

419 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 CHECK(j)\
  32. { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
  33. + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
  34. + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
  35. if (score < spatial_score) {\
  36. spatial_score= score;\
  37. spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
  38. #define FILTER \
  39. for (x = 0; x < w; 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 spatial_score = FFABS(cur[mrefs-1] - cur[prefs-1]) + FFABS(c-e) \
  49. + FFABS(cur[mrefs+1] - cur[prefs+1]) - 1; \
  50. \
  51. CHECK(-1) CHECK(-2) }} }} \
  52. CHECK( 1) CHECK( 2) }} }} \
  53. \
  54. if (mode < 2) { \
  55. int b = (prev2[2*mrefs] + next2[2*mrefs])>>1; \
  56. int f = (prev2[2*prefs] + next2[2*prefs])>>1; \
  57. int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e)); \
  58. int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e)); \
  59. \
  60. diff = FFMAX3(diff, min, -max); \
  61. } \
  62. \
  63. if (spatial_pred > d + diff) \
  64. spatial_pred = d + diff; \
  65. else if (spatial_pred < d - diff) \
  66. spatial_pred = d - diff; \
  67. \
  68. dst[0] = spatial_pred; \
  69. \
  70. dst++; \
  71. cur++; \
  72. prev++; \
  73. next++; \
  74. prev2++; \
  75. next2++; \
  76. }
  77. static void filter_line_c(uint8_t *dst,
  78. uint8_t *prev, uint8_t *cur, uint8_t *next,
  79. int w, int prefs, int mrefs, int parity, int mode)
  80. {
  81. int x;
  82. uint8_t *prev2 = parity ? prev : cur ;
  83. uint8_t *next2 = parity ? cur : next;
  84. FILTER
  85. }
  86. static void filter_line_c_16bit(uint16_t *dst,
  87. uint16_t *prev, uint16_t *cur, uint16_t *next,
  88. int w, int prefs, int mrefs, int parity, int mode)
  89. {
  90. int x;
  91. uint16_t *prev2 = parity ? prev : cur ;
  92. uint16_t *next2 = parity ? cur : next;
  93. mrefs /= 2;
  94. prefs /= 2;
  95. FILTER
  96. }
  97. static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
  98. int parity, int tff)
  99. {
  100. YADIFContext *yadif = ctx->priv;
  101. int y, i;
  102. for (i = 0; i < yadif->csp->nb_components; i++) {
  103. int w = dstpic->video->w;
  104. int h = dstpic->video->h;
  105. int refs = yadif->cur->linesize[i];
  106. int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
  107. if (i == 1 || i == 2) {
  108. /* Why is this not part of the per-plane description thing? */
  109. w >>= yadif->csp->log2_chroma_w;
  110. h >>= yadif->csp->log2_chroma_h;
  111. }
  112. for (y = 0; y < h; y++) {
  113. if ((y ^ parity) & 1) {
  114. uint8_t *prev = &yadif->prev->data[i][y*refs];
  115. uint8_t *cur = &yadif->cur ->data[i][y*refs];
  116. uint8_t *next = &yadif->next->data[i][y*refs];
  117. uint8_t *dst = &dstpic->data[i][y*dstpic->linesize[i]];
  118. int mode = y==1 || y+2==h ? 2 : yadif->mode;
  119. yadif->filter_line(dst, prev, cur, next, w, y+1<h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
  120. } else {
  121. memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
  122. &yadif->cur->data[i][y*refs], w*df);
  123. }
  124. }
  125. }
  126. emms_c();
  127. }
  128. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  129. {
  130. AVFilterBufferRef *picref;
  131. int width = FFALIGN(w, 32);
  132. int height= FFALIGN(h+2, 32);
  133. int i;
  134. picref = ff_default_get_video_buffer(link, perms, width, height);
  135. picref->video->w = w;
  136. picref->video->h = h;
  137. for (i = 0; i < 3; i++)
  138. picref->data[i] += picref->linesize[i];
  139. return picref;
  140. }
  141. static void return_frame(AVFilterContext *ctx, int is_second)
  142. {
  143. YADIFContext *yadif = ctx->priv;
  144. AVFilterLink *link= ctx->outputs[0];
  145. int tff;
  146. if (yadif->parity == -1) {
  147. tff = yadif->cur->video->interlaced ?
  148. yadif->cur->video->top_field_first : 1;
  149. } else {
  150. tff = yadif->parity^1;
  151. }
  152. if (is_second) {
  153. yadif->out = ff_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
  154. AV_PERM_REUSE, link->w, link->h);
  155. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  156. yadif->out->video->interlaced = 0;
  157. }
  158. if (!yadif->csp)
  159. yadif->csp = &av_pix_fmt_descriptors[link->format];
  160. if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
  161. yadif->filter_line = filter_line_c_16bit;
  162. filter(ctx, yadif->out, tff ^ !is_second, tff);
  163. if (is_second) {
  164. int64_t cur_pts = yadif->cur->pts;
  165. int64_t next_pts = yadif->next->pts;
  166. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  167. yadif->out->pts = cur_pts + next_pts;
  168. } else {
  169. yadif->out->pts = AV_NOPTS_VALUE;
  170. }
  171. ff_start_frame(ctx->outputs[0], yadif->out);
  172. }
  173. ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
  174. ff_end_frame(ctx->outputs[0]);
  175. yadif->frame_pending = (yadif->mode&1) && !is_second;
  176. }
  177. static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  178. {
  179. AVFilterContext *ctx = link->dst;
  180. YADIFContext *yadif = ctx->priv;
  181. if (yadif->frame_pending)
  182. return_frame(ctx, 1);
  183. if (yadif->prev)
  184. avfilter_unref_buffer(yadif->prev);
  185. yadif->prev = yadif->cur;
  186. yadif->cur = yadif->next;
  187. yadif->next = picref;
  188. if (!yadif->cur)
  189. return 0;
  190. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  191. yadif->out = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  192. avfilter_unref_buffer(yadif->prev);
  193. yadif->prev = NULL;
  194. if (yadif->out->pts != AV_NOPTS_VALUE)
  195. yadif->out->pts *= 2;
  196. return ff_start_frame(ctx->outputs[0], yadif->out);
  197. }
  198. if (!yadif->prev)
  199. yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  200. yadif->out = ff_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
  201. AV_PERM_REUSE, link->w, link->h);
  202. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  203. yadif->out->video->interlaced = 0;
  204. if (yadif->out->pts != AV_NOPTS_VALUE)
  205. yadif->out->pts *= 2;
  206. return ff_start_frame(ctx->outputs[0], yadif->out);
  207. }
  208. static void end_frame(AVFilterLink *link)
  209. {
  210. AVFilterContext *ctx = link->dst;
  211. YADIFContext *yadif = ctx->priv;
  212. if (!yadif->out)
  213. return;
  214. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  215. ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
  216. ff_end_frame(ctx->outputs[0]);
  217. return;
  218. }
  219. return_frame(ctx, 0);
  220. }
  221. static int request_frame(AVFilterLink *link)
  222. {
  223. AVFilterContext *ctx = link->src;
  224. YADIFContext *yadif = ctx->priv;
  225. if (yadif->frame_pending) {
  226. return_frame(ctx, 1);
  227. return 0;
  228. }
  229. do {
  230. int ret;
  231. if (yadif->eof)
  232. return AVERROR_EOF;
  233. ret = ff_request_frame(link->src->inputs[0]);
  234. if (ret == AVERROR_EOF && yadif->next) {
  235. AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, AV_PERM_READ);
  236. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  237. start_frame(link->src->inputs[0], next);
  238. end_frame(link->src->inputs[0]);
  239. yadif->eof = 1;
  240. } else if (ret < 0) {
  241. return ret;
  242. }
  243. } while (!yadif->cur);
  244. return 0;
  245. }
  246. static int poll_frame(AVFilterLink *link)
  247. {
  248. YADIFContext *yadif = link->src->priv;
  249. int ret, val;
  250. if (yadif->frame_pending)
  251. return 1;
  252. val = ff_poll_frame(link->src->inputs[0]);
  253. if (val <= 0)
  254. return val;
  255. if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
  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_buffer(yadif->prev);
  271. if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
  272. if (yadif->next) avfilter_unref_buffer(yadif->next);
  273. }
  274. static int query_formats(AVFilterContext *ctx)
  275. {
  276. static const enum PixelFormat pix_fmts[] = {
  277. PIX_FMT_YUV420P,
  278. PIX_FMT_YUV422P,
  279. PIX_FMT_YUV444P,
  280. PIX_FMT_YUV410P,
  281. PIX_FMT_YUV411P,
  282. PIX_FMT_GRAY8,
  283. PIX_FMT_YUVJ420P,
  284. PIX_FMT_YUVJ422P,
  285. PIX_FMT_YUVJ444P,
  286. AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
  287. PIX_FMT_YUV440P,
  288. PIX_FMT_YUVJ440P,
  289. AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
  290. AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
  291. AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
  292. AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
  293. AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
  294. AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
  295. PIX_FMT_YUVA420P,
  296. 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) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
  309. yadif->filter_line = filter_line_c;
  310. if (HAVE_MMX)
  311. ff_yadif_init_x86(yadif);
  312. av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
  313. return 0;
  314. }
  315. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  316. {
  317. return 0;
  318. }
  319. static int config_props(AVFilterLink *link)
  320. {
  321. link->time_base.num = link->src->inputs[0]->time_base.num;
  322. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  323. link->w = link->src->inputs[0]->w;
  324. link->h = link->src->inputs[0]->h;
  325. return 0;
  326. }
  327. AVFilter avfilter_vf_yadif = {
  328. .name = "yadif",
  329. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
  330. .priv_size = sizeof(YADIFContext),
  331. .init = init,
  332. .uninit = uninit,
  333. .query_formats = query_formats,
  334. .inputs = (const AVFilterPad[]) {{ .name = "default",
  335. .type = AVMEDIA_TYPE_VIDEO,
  336. .start_frame = start_frame,
  337. .get_video_buffer = get_video_buffer,
  338. .draw_slice = null_draw_slice,
  339. .end_frame = end_frame, },
  340. { .name = NULL}},
  341. .outputs = (const AVFilterPad[]) {{ .name = "default",
  342. .type = AVMEDIA_TYPE_VIDEO,
  343. .poll_frame = poll_frame,
  344. .request_frame = request_frame,
  345. .config_props = config_props, },
  346. { .name = NULL}},
  347. };