vf_yadif.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "libavutil/cpu.h"
  21. #include "libavutil/common.h"
  22. #include "avfilter.h"
  23. #include "yadif.h"
  24. #undef NDEBUG
  25. #include <assert.h>
  26. typedef struct {
  27. /**
  28. * 0: send 1 frame for each frame
  29. * 1: send 1 frame for each field
  30. * 2: like 0 but skips spatial interlacing check
  31. * 3: like 1 but skips spatial interlacing check
  32. */
  33. int mode;
  34. /**
  35. * 0: bottom field first
  36. * 1: top field first
  37. * -1: auto-detection
  38. */
  39. int parity;
  40. int frame_pending;
  41. AVFilterBufferRef *cur;
  42. AVFilterBufferRef *next;
  43. AVFilterBufferRef *prev;
  44. AVFilterBufferRef *out;
  45. void (*filter_line)(uint8_t *dst,
  46. uint8_t *prev, uint8_t *cur, uint8_t *next,
  47. int w, int refs, int parity, int mode);
  48. } YADIFContext;
  49. static void filter_line_c(uint8_t *dst,
  50. uint8_t *prev, uint8_t *cur, uint8_t *next,
  51. int w, int refs, int parity, int mode)
  52. {
  53. int x;
  54. uint8_t *prev2 = parity ? prev : cur ;
  55. uint8_t *next2 = parity ? cur : next;
  56. for (x = 0; x < w; x++) {
  57. int c = cur[-refs];
  58. int d = (prev2[0] + next2[0])>>1;
  59. int e = cur[+refs];
  60. int temporal_diff0 = FFABS(prev2[0] - next2[0]);
  61. int temporal_diff1 =(FFABS(prev[-refs] - c) + FFABS(prev[+refs] - e) )>>1;
  62. int temporal_diff2 =(FFABS(next[-refs] - c) + FFABS(next[+refs] - e) )>>1;
  63. int diff = FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2);
  64. int spatial_pred = (c+e)>>1;
  65. int spatial_score = FFABS(cur[-refs-1] - cur[+refs-1]) + FFABS(c-e)
  66. + FFABS(cur[-refs+1] - cur[+refs+1]) - 1;
  67. #define CHECK(j)\
  68. { int score = FFABS(cur[-refs-1+j] - cur[+refs-1-j])\
  69. + FFABS(cur[-refs +j] - cur[+refs -j])\
  70. + FFABS(cur[-refs+1+j] - cur[+refs+1-j]);\
  71. if (score < spatial_score) {\
  72. spatial_score= score;\
  73. spatial_pred= (cur[-refs +j] + cur[+refs -j])>>1;\
  74. CHECK(-1) CHECK(-2) }} }}
  75. CHECK( 1) CHECK( 2) }} }}
  76. if (mode < 2) {
  77. int b = (prev2[-2*refs] + next2[-2*refs])>>1;
  78. int f = (prev2[+2*refs] + next2[+2*refs])>>1;
  79. #if 0
  80. int a = cur[-3*refs];
  81. int g = cur[+3*refs];
  82. int max = FFMAX3(d-e, d-c, FFMIN3(FFMAX(b-c,f-e),FFMAX(b-c,b-a),FFMAX(f-g,f-e)) );
  83. int min = FFMIN3(d-e, d-c, FFMAX3(FFMIN(b-c,f-e),FFMIN(b-c,b-a),FFMIN(f-g,f-e)) );
  84. #else
  85. int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e));
  86. int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e));
  87. #endif
  88. diff = FFMAX3(diff, min, -max);
  89. }
  90. if (spatial_pred > d + diff)
  91. spatial_pred = d + diff;
  92. else if (spatial_pred < d - diff)
  93. spatial_pred = d - diff;
  94. dst[0] = spatial_pred;
  95. dst++;
  96. cur++;
  97. prev++;
  98. next++;
  99. prev2++;
  100. next2++;
  101. }
  102. }
  103. static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
  104. int parity, int tff)
  105. {
  106. YADIFContext *yadif = ctx->priv;
  107. int y, i;
  108. for (i = 0; i < 3; i++) {
  109. int is_chroma = !!i;
  110. int w = dstpic->video->w >> is_chroma;
  111. int h = dstpic->video->h >> is_chroma;
  112. int refs = yadif->cur->linesize[i];
  113. for (y = 0; y < h; y++) {
  114. if ((y ^ parity) & 1) {
  115. uint8_t *prev = &yadif->prev->data[i][y*refs];
  116. uint8_t *cur = &yadif->cur ->data[i][y*refs];
  117. uint8_t *next = &yadif->next->data[i][y*refs];
  118. uint8_t *dst = &dstpic->data[i][y*dstpic->linesize[i]];
  119. yadif->filter_line(dst, prev, cur, next, w, refs, parity ^ tff, yadif->mode);
  120. } else {
  121. memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
  122. &yadif->cur->data[i][y*refs], w);
  123. }
  124. }
  125. }
  126. #if HAVE_MMX
  127. __asm__ volatile("emms \n\t" : : : "memory");
  128. #endif
  129. }
  130. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  131. {
  132. AVFilterBufferRef *picref;
  133. int width = FFALIGN(w, 32);
  134. int height= FFALIGN(h+6, 32);
  135. int i;
  136. picref = avfilter_default_get_video_buffer(link, perms, width, height);
  137. picref->video->w = w;
  138. picref->video->h = h;
  139. for (i = 0; i < 3; i++)
  140. picref->data[i] += 3 * picref->linesize[i];
  141. return picref;
  142. }
  143. static void return_frame(AVFilterContext *ctx, int is_second)
  144. {
  145. YADIFContext *yadif = ctx->priv;
  146. AVFilterLink *link= ctx->outputs[0];
  147. int tff = yadif->parity == -1 ? yadif->cur->video->top_field_first : (yadif->parity^1);
  148. if (is_second)
  149. yadif->out = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
  150. AV_PERM_REUSE, link->w, link->h);
  151. filter(ctx, yadif->out, tff ^ !is_second, tff);
  152. if (is_second)
  153. avfilter_start_frame(ctx->outputs[0], yadif->out);
  154. avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
  155. avfilter_end_frame(ctx->outputs[0]);
  156. yadif->frame_pending = (yadif->mode&1) && !is_second;
  157. }
  158. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  159. {
  160. AVFilterContext *ctx = link->dst;
  161. YADIFContext *yadif = ctx->priv;
  162. if (yadif->frame_pending)
  163. return_frame(ctx, 1);
  164. if (yadif->prev)
  165. avfilter_unref_buffer(yadif->prev);
  166. yadif->prev = yadif->cur;
  167. yadif->cur = yadif->next;
  168. yadif->next = picref;
  169. if (!yadif->cur)
  170. return;
  171. if (!yadif->prev)
  172. yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  173. yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
  174. AV_PERM_REUSE, link->w, link->h);
  175. yadif->out->pts = yadif->cur->pts;
  176. avfilter_start_frame(ctx->outputs[0], yadif->out);
  177. }
  178. static void end_frame(AVFilterLink *link)
  179. {
  180. AVFilterContext *ctx = link->dst;
  181. YADIFContext *yadif = ctx->priv;
  182. if (!yadif->out)
  183. return;
  184. return_frame(ctx, 0);
  185. }
  186. static int request_frame(AVFilterLink *link)
  187. {
  188. AVFilterContext *ctx = link->src;
  189. YADIFContext *yadif = ctx->priv;
  190. if (yadif->frame_pending) {
  191. return_frame(ctx, 1);
  192. return 0;
  193. }
  194. do {
  195. int ret;
  196. if ((ret = avfilter_request_frame(link->src->inputs[0])))
  197. return ret;
  198. } while (!yadif->cur);
  199. return 0;
  200. }
  201. static int poll_frame(AVFilterLink *link)
  202. {
  203. YADIFContext *yadif = link->src->priv;
  204. int ret, val;
  205. if (yadif->frame_pending)
  206. return 1;
  207. val = avfilter_poll_frame(link->src->inputs[0]);
  208. if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
  209. if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
  210. return ret;
  211. val = avfilter_poll_frame(link->src->inputs[0]);
  212. }
  213. assert(yadif->next);
  214. return val * ((yadif->mode&1)+1);
  215. }
  216. static av_cold void uninit(AVFilterContext *ctx)
  217. {
  218. YADIFContext *yadif = ctx->priv;
  219. if (yadif->prev) avfilter_unref_buffer(yadif->prev);
  220. if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
  221. if (yadif->next) avfilter_unref_buffer(yadif->next);
  222. }
  223. static int query_formats(AVFilterContext *ctx)
  224. {
  225. static const enum PixelFormat pix_fmts[] = {
  226. PIX_FMT_YUV420P,
  227. PIX_FMT_GRAY8,
  228. PIX_FMT_NONE
  229. };
  230. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  231. return 0;
  232. }
  233. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  234. {
  235. YADIFContext *yadif = ctx->priv;
  236. av_unused int cpu_flags = av_get_cpu_flags();
  237. yadif->mode = 0;
  238. yadif->parity = -1;
  239. if (args) sscanf(args, "%d:%d", &yadif->mode, &yadif->parity);
  240. yadif->filter_line = filter_line_c;
  241. if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
  242. yadif->filter_line = ff_yadif_filter_line_mmx;
  243. av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d\n", yadif->mode, yadif->parity);
  244. return 0;
  245. }
  246. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  247. AVFilter avfilter_vf_yadif = {
  248. .name = "yadif",
  249. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
  250. .priv_size = sizeof(YADIFContext),
  251. .init = init,
  252. .uninit = uninit,
  253. .query_formats = query_formats,
  254. .inputs = (AVFilterPad[]) {{ .name = "default",
  255. .type = AVMEDIA_TYPE_VIDEO,
  256. .start_frame = start_frame,
  257. .get_video_buffer = get_video_buffer,
  258. .draw_slice = null_draw_slice,
  259. .end_frame = end_frame, },
  260. { .name = NULL}},
  261. .outputs = (AVFilterPad[]) {{ .name = "default",
  262. .type = AVMEDIA_TYPE_VIDEO,
  263. .poll_frame = poll_frame,
  264. .request_frame = request_frame, },
  265. { .name = NULL}},
  266. };