vf_yadif.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. if (yadif->next->pts != AV_NOPTS_VALUE &&
  154. yadif->cur->pts != AV_NOPTS_VALUE) {
  155. yadif->out->pts =
  156. (yadif->next->pts&yadif->cur->pts) +
  157. ((yadif->next->pts^yadif->cur->pts)>>1);
  158. } else {
  159. yadif->out->pts = AV_NOPTS_VALUE;
  160. }
  161. avfilter_start_frame(ctx->outputs[0], yadif->out);
  162. }
  163. avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
  164. avfilter_end_frame(ctx->outputs[0]);
  165. yadif->frame_pending = (yadif->mode&1) && !is_second;
  166. }
  167. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  168. {
  169. AVFilterContext *ctx = link->dst;
  170. YADIFContext *yadif = ctx->priv;
  171. if (yadif->frame_pending)
  172. return_frame(ctx, 1);
  173. if (yadif->prev)
  174. avfilter_unref_buffer(yadif->prev);
  175. yadif->prev = yadif->cur;
  176. yadif->cur = yadif->next;
  177. yadif->next = picref;
  178. if (!yadif->cur)
  179. return;
  180. if (!yadif->prev)
  181. yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  182. yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
  183. AV_PERM_REUSE, link->w, link->h);
  184. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  185. yadif->out->video->interlaced = 0;
  186. avfilter_start_frame(ctx->outputs[0], yadif->out);
  187. }
  188. static void end_frame(AVFilterLink *link)
  189. {
  190. AVFilterContext *ctx = link->dst;
  191. YADIFContext *yadif = ctx->priv;
  192. if (!yadif->out)
  193. return;
  194. return_frame(ctx, 0);
  195. }
  196. static int request_frame(AVFilterLink *link)
  197. {
  198. AVFilterContext *ctx = link->src;
  199. YADIFContext *yadif = ctx->priv;
  200. if (yadif->frame_pending) {
  201. return_frame(ctx, 1);
  202. return 0;
  203. }
  204. do {
  205. int ret;
  206. if ((ret = avfilter_request_frame(link->src->inputs[0])))
  207. return ret;
  208. } while (!yadif->cur);
  209. return 0;
  210. }
  211. static int poll_frame(AVFilterLink *link)
  212. {
  213. YADIFContext *yadif = link->src->priv;
  214. int ret, val;
  215. if (yadif->frame_pending)
  216. return 1;
  217. val = avfilter_poll_frame(link->src->inputs[0]);
  218. if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
  219. if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
  220. return ret;
  221. val = avfilter_poll_frame(link->src->inputs[0]);
  222. }
  223. assert(yadif->next);
  224. return val * ((yadif->mode&1)+1);
  225. }
  226. static av_cold void uninit(AVFilterContext *ctx)
  227. {
  228. YADIFContext *yadif = ctx->priv;
  229. if (yadif->prev) avfilter_unref_buffer(yadif->prev);
  230. if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
  231. if (yadif->next) avfilter_unref_buffer(yadif->next);
  232. }
  233. static int query_formats(AVFilterContext *ctx)
  234. {
  235. static const enum PixelFormat pix_fmts[] = {
  236. PIX_FMT_YUV420P,
  237. PIX_FMT_GRAY8,
  238. PIX_FMT_NONE
  239. };
  240. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  241. return 0;
  242. }
  243. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  244. {
  245. YADIFContext *yadif = ctx->priv;
  246. av_unused int cpu_flags = av_get_cpu_flags();
  247. yadif->mode = 0;
  248. yadif->parity = -1;
  249. if (args) sscanf(args, "%d:%d", &yadif->mode, &yadif->parity);
  250. yadif->filter_line = filter_line_c;
  251. if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
  252. yadif->filter_line = ff_yadif_filter_line_ssse3;
  253. else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
  254. yadif->filter_line = ff_yadif_filter_line_sse2;
  255. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
  256. yadif->filter_line = ff_yadif_filter_line_mmx;
  257. av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d\n", yadif->mode, yadif->parity);
  258. return 0;
  259. }
  260. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  261. AVFilter avfilter_vf_yadif = {
  262. .name = "yadif",
  263. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
  264. .priv_size = sizeof(YADIFContext),
  265. .init = init,
  266. .uninit = uninit,
  267. .query_formats = query_formats,
  268. .inputs = (AVFilterPad[]) {{ .name = "default",
  269. .type = AVMEDIA_TYPE_VIDEO,
  270. .start_frame = start_frame,
  271. .get_video_buffer = get_video_buffer,
  272. .draw_slice = null_draw_slice,
  273. .end_frame = end_frame, },
  274. { .name = NULL}},
  275. .outputs = (AVFilterPad[]) {{ .name = "default",
  276. .type = AVMEDIA_TYPE_VIDEO,
  277. .poll_frame = poll_frame,
  278. .request_frame = request_frame, },
  279. { .name = NULL}},
  280. };