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.

352 lines
11KB

  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 prefs, int mrefs, 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 prefs, int mrefs, 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[mrefs];
  58. int d = (prev2[0] + next2[0])>>1;
  59. int e = cur[prefs];
  60. int temporal_diff0 = FFABS(prev2[0] - next2[0]);
  61. int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1;
  62. int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - 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[mrefs-1] - cur[prefs-1]) + FFABS(c-e)
  66. + FFABS(cur[mrefs+1] - cur[prefs+1]) - 1;
  67. #define CHECK(j)\
  68. { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
  69. + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
  70. + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
  71. if (score < spatial_score) {\
  72. spatial_score= score;\
  73. spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
  74. CHECK(-1) CHECK(-2) }} }}
  75. CHECK( 1) CHECK( 2) }} }}
  76. if (mode < 2) {
  77. int b = (prev2[2*mrefs] + next2[2*mrefs])>>1;
  78. int f = (prev2[2*prefs] + next2[2*prefs])>>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. int mode = y==1 || y+2==h ? 2 : yadif->mode;
  120. yadif->filter_line(dst, prev, cur, next, w, y+1<h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
  121. } else {
  122. memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
  123. &yadif->cur->data[i][y*refs], w);
  124. }
  125. }
  126. }
  127. #if HAVE_MMX
  128. __asm__ volatile("emms \n\t" : : : "memory");
  129. #endif
  130. }
  131. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  132. {
  133. AVFilterBufferRef *picref;
  134. int width = FFALIGN(w, 32);
  135. int height= FFALIGN(h+2, 32);
  136. int i;
  137. picref = avfilter_default_get_video_buffer(link, perms, width, height);
  138. picref->video->w = w;
  139. picref->video->h = h;
  140. for (i = 0; i < 3; i++)
  141. picref->data[i] += picref->linesize[i];
  142. return picref;
  143. }
  144. static void return_frame(AVFilterContext *ctx, int is_second)
  145. {
  146. YADIFContext *yadif = ctx->priv;
  147. AVFilterLink *link= ctx->outputs[0];
  148. int tff;
  149. if (yadif->parity == -1) {
  150. tff = yadif->cur->video->interlaced ?
  151. yadif->cur->video->top_field_first : 1;
  152. } else {
  153. tff = yadif->parity^1;
  154. }
  155. if (is_second)
  156. yadif->out = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
  157. AV_PERM_REUSE, link->w, link->h);
  158. filter(ctx, yadif->out, tff ^ !is_second, tff);
  159. if (is_second) {
  160. if (yadif->next->pts != AV_NOPTS_VALUE &&
  161. yadif->cur->pts != AV_NOPTS_VALUE) {
  162. yadif->out->pts =
  163. (yadif->next->pts&yadif->cur->pts) +
  164. ((yadif->next->pts^yadif->cur->pts)>>1);
  165. } else {
  166. yadif->out->pts = AV_NOPTS_VALUE;
  167. }
  168. avfilter_start_frame(ctx->outputs[0], yadif->out);
  169. }
  170. avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
  171. avfilter_end_frame(ctx->outputs[0]);
  172. yadif->frame_pending = (yadif->mode&1) && !is_second;
  173. }
  174. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  175. {
  176. AVFilterContext *ctx = link->dst;
  177. YADIFContext *yadif = ctx->priv;
  178. if (yadif->frame_pending)
  179. return_frame(ctx, 1);
  180. if (yadif->prev)
  181. avfilter_unref_buffer(yadif->prev);
  182. yadif->prev = yadif->cur;
  183. yadif->cur = yadif->next;
  184. yadif->next = picref;
  185. if (!yadif->cur)
  186. return;
  187. if (!yadif->prev)
  188. yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  189. yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
  190. AV_PERM_REUSE, link->w, link->h);
  191. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  192. yadif->out->video->interlaced = 0;
  193. avfilter_start_frame(ctx->outputs[0], yadif->out);
  194. }
  195. static void end_frame(AVFilterLink *link)
  196. {
  197. AVFilterContext *ctx = link->dst;
  198. YADIFContext *yadif = ctx->priv;
  199. if (!yadif->out)
  200. return;
  201. return_frame(ctx, 0);
  202. }
  203. static int request_frame(AVFilterLink *link)
  204. {
  205. AVFilterContext *ctx = link->src;
  206. YADIFContext *yadif = ctx->priv;
  207. if (yadif->frame_pending) {
  208. return_frame(ctx, 1);
  209. return 0;
  210. }
  211. do {
  212. int ret;
  213. if ((ret = avfilter_request_frame(link->src->inputs[0])))
  214. return ret;
  215. } while (!yadif->cur);
  216. return 0;
  217. }
  218. static int poll_frame(AVFilterLink *link)
  219. {
  220. YADIFContext *yadif = link->src->priv;
  221. int ret, val;
  222. if (yadif->frame_pending)
  223. return 1;
  224. val = avfilter_poll_frame(link->src->inputs[0]);
  225. if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
  226. if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
  227. return ret;
  228. val = avfilter_poll_frame(link->src->inputs[0]);
  229. }
  230. assert(yadif->next);
  231. return val * ((yadif->mode&1)+1);
  232. }
  233. static av_cold void uninit(AVFilterContext *ctx)
  234. {
  235. YADIFContext *yadif = ctx->priv;
  236. if (yadif->prev) avfilter_unref_buffer(yadif->prev);
  237. if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
  238. if (yadif->next) avfilter_unref_buffer(yadif->next);
  239. }
  240. static int query_formats(AVFilterContext *ctx)
  241. {
  242. static const enum PixelFormat pix_fmts[] = {
  243. PIX_FMT_YUV420P,
  244. PIX_FMT_GRAY8,
  245. PIX_FMT_NONE
  246. };
  247. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  248. return 0;
  249. }
  250. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  251. {
  252. YADIFContext *yadif = ctx->priv;
  253. av_unused int cpu_flags = av_get_cpu_flags();
  254. yadif->mode = 0;
  255. yadif->parity = -1;
  256. if (args) sscanf(args, "%d:%d", &yadif->mode, &yadif->parity);
  257. yadif->filter_line = filter_line_c;
  258. if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
  259. yadif->filter_line = ff_yadif_filter_line_ssse3;
  260. else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
  261. yadif->filter_line = ff_yadif_filter_line_sse2;
  262. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
  263. yadif->filter_line = ff_yadif_filter_line_mmx;
  264. av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d\n", yadif->mode, yadif->parity);
  265. return 0;
  266. }
  267. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  268. AVFilter avfilter_vf_yadif = {
  269. .name = "yadif",
  270. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
  271. .priv_size = sizeof(YADIFContext),
  272. .init = init,
  273. .uninit = uninit,
  274. .query_formats = query_formats,
  275. .inputs = (AVFilterPad[]) {{ .name = "default",
  276. .type = AVMEDIA_TYPE_VIDEO,
  277. .start_frame = start_frame,
  278. .get_video_buffer = get_video_buffer,
  279. .draw_slice = null_draw_slice,
  280. .end_frame = end_frame, },
  281. { .name = NULL}},
  282. .outputs = (AVFilterPad[]) {{ .name = "default",
  283. .type = AVMEDIA_TYPE_VIDEO,
  284. .poll_frame = poll_frame,
  285. .request_frame = request_frame, },
  286. { .name = NULL}},
  287. };