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.

390 lines
12KB

  1. /*
  2. * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
  3. * 2010 James Darnley <james.darnley@gmail.com>
  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 "libavutil/pixdesc.h"
  23. #include "avfilter.h"
  24. #include "yadif.h"
  25. #undef NDEBUG
  26. #include <assert.h>
  27. typedef struct {
  28. /**
  29. * 0: send 1 frame for each frame
  30. * 1: send 1 frame for each field
  31. * 2: like 0 but skips spatial interlacing check
  32. * 3: like 1 but skips spatial interlacing check
  33. */
  34. int mode;
  35. /**
  36. * 0: bottom field first
  37. * 1: top field first
  38. * -1: auto-detection
  39. */
  40. int parity;
  41. int frame_pending;
  42. AVFilterBufferRef *cur;
  43. AVFilterBufferRef *next;
  44. AVFilterBufferRef *prev;
  45. AVFilterBufferRef *out;
  46. void (*filter_line)(uint8_t *dst,
  47. uint8_t *prev, uint8_t *cur, uint8_t *next,
  48. int w, int prefs, int mrefs, int parity, int mode);
  49. const AVPixFmtDescriptor *csp;
  50. } YADIFContext;
  51. #define CHECK(j)\
  52. { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
  53. + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
  54. + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
  55. if (score < spatial_score) {\
  56. spatial_score= score;\
  57. spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
  58. #define FILTER \
  59. for (x = 0; x < w; x++) { \
  60. int c = cur[mrefs]; \
  61. int d = (prev2[0] + next2[0])>>1; \
  62. int e = cur[prefs]; \
  63. int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
  64. int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
  65. int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
  66. int diff = FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2); \
  67. int spatial_pred = (c+e)>>1; \
  68. int spatial_score = FFABS(cur[mrefs-1] - cur[prefs-1]) + FFABS(c-e) \
  69. + FFABS(cur[mrefs+1] - cur[prefs+1]) - 1; \
  70. \
  71. CHECK(-1) CHECK(-2) }} }} \
  72. CHECK( 1) CHECK( 2) }} }} \
  73. \
  74. if (mode < 2) { \
  75. int b = (prev2[2*mrefs] + next2[2*mrefs])>>1; \
  76. int f = (prev2[2*prefs] + next2[2*prefs])>>1; \
  77. int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e)); \
  78. int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e)); \
  79. \
  80. diff = FFMAX3(diff, min, -max); \
  81. } \
  82. \
  83. if (spatial_pred > d + diff) \
  84. spatial_pred = d + diff; \
  85. else if (spatial_pred < d - diff) \
  86. spatial_pred = d - diff; \
  87. \
  88. dst[0] = spatial_pred; \
  89. \
  90. dst++; \
  91. cur++; \
  92. prev++; \
  93. next++; \
  94. prev2++; \
  95. next2++; \
  96. }
  97. static void filter_line_c(uint8_t *dst,
  98. uint8_t *prev, uint8_t *cur, uint8_t *next,
  99. int w, int prefs, int mrefs, int parity, int mode)
  100. {
  101. int x;
  102. uint8_t *prev2 = parity ? prev : cur ;
  103. uint8_t *next2 = parity ? cur : next;
  104. FILTER
  105. }
  106. static void filter_line_c_16bit(uint16_t *dst,
  107. uint16_t *prev, uint16_t *cur, uint16_t *next,
  108. int w, int prefs, int mrefs, int parity, int mode)
  109. {
  110. int x;
  111. uint16_t *prev2 = parity ? prev : cur ;
  112. uint16_t *next2 = parity ? cur : next;
  113. mrefs /= 2;
  114. prefs /= 2;
  115. FILTER
  116. }
  117. static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
  118. int parity, int tff)
  119. {
  120. YADIFContext *yadif = ctx->priv;
  121. int y, i;
  122. for (i = 0; i < yadif->csp->nb_components; i++) {
  123. int w = dstpic->video->w;
  124. int h = dstpic->video->h;
  125. int refs = yadif->cur->linesize[i];
  126. int df = (yadif->csp->comp[i].depth_minus1+1) / 8;
  127. if (i) {
  128. /* Why is this not part of the per-plane description thing? */
  129. w >>= yadif->csp->log2_chroma_w;
  130. h >>= yadif->csp->log2_chroma_h;
  131. }
  132. for (y = 0; y < h; y++) {
  133. if ((y ^ parity) & 1) {
  134. uint8_t *prev = &yadif->prev->data[i][y*refs];
  135. uint8_t *cur = &yadif->cur ->data[i][y*refs];
  136. uint8_t *next = &yadif->next->data[i][y*refs];
  137. uint8_t *dst = &dstpic->data[i][y*dstpic->linesize[i]];
  138. int mode = y==1 || y+2==h ? 2 : yadif->mode;
  139. yadif->filter_line(dst, prev, cur, next, w, y+1<h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
  140. } else {
  141. memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
  142. &yadif->cur->data[i][y*refs], w*df);
  143. }
  144. }
  145. }
  146. #if HAVE_MMX
  147. __asm__ volatile("emms \n\t" : : : "memory");
  148. #endif
  149. }
  150. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  151. {
  152. AVFilterBufferRef *picref;
  153. int width = FFALIGN(w, 32);
  154. int height= FFALIGN(h+2, 32);
  155. int i;
  156. picref = avfilter_default_get_video_buffer(link, perms, width, height);
  157. picref->video->w = w;
  158. picref->video->h = h;
  159. for (i = 0; i < 3; i++)
  160. picref->data[i] += picref->linesize[i];
  161. return picref;
  162. }
  163. static void return_frame(AVFilterContext *ctx, int is_second)
  164. {
  165. YADIFContext *yadif = ctx->priv;
  166. AVFilterLink *link= ctx->outputs[0];
  167. int tff;
  168. if (yadif->parity == -1) {
  169. tff = yadif->cur->video->interlaced ?
  170. yadif->cur->video->top_field_first : 1;
  171. } else {
  172. tff = yadif->parity^1;
  173. }
  174. if (is_second)
  175. yadif->out = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
  176. AV_PERM_REUSE, link->w, link->h);
  177. if (!yadif->csp)
  178. yadif->csp = &av_pix_fmt_descriptors[link->format];
  179. if (yadif->csp->comp[0].depth_minus1 == 15)
  180. yadif->filter_line = filter_line_c_16bit;
  181. filter(ctx, yadif->out, tff ^ !is_second, tff);
  182. if (is_second) {
  183. if (yadif->next->pts != AV_NOPTS_VALUE &&
  184. yadif->cur->pts != AV_NOPTS_VALUE) {
  185. yadif->out->pts =
  186. (yadif->next->pts&yadif->cur->pts) +
  187. ((yadif->next->pts^yadif->cur->pts)>>1);
  188. } else {
  189. yadif->out->pts = AV_NOPTS_VALUE;
  190. }
  191. avfilter_start_frame(ctx->outputs[0], yadif->out);
  192. }
  193. avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
  194. avfilter_end_frame(ctx->outputs[0]);
  195. yadif->frame_pending = (yadif->mode&1) && !is_second;
  196. }
  197. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  198. {
  199. AVFilterContext *ctx = link->dst;
  200. YADIFContext *yadif = ctx->priv;
  201. if (yadif->frame_pending)
  202. return_frame(ctx, 1);
  203. if (yadif->prev)
  204. avfilter_unref_buffer(yadif->prev);
  205. yadif->prev = yadif->cur;
  206. yadif->cur = yadif->next;
  207. yadif->next = picref;
  208. if (!yadif->cur)
  209. return;
  210. if (!yadif->prev)
  211. yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  212. yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
  213. AV_PERM_REUSE, link->w, link->h);
  214. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  215. yadif->out->video->interlaced = 0;
  216. avfilter_start_frame(ctx->outputs[0], yadif->out);
  217. }
  218. static void end_frame(AVFilterLink *link)
  219. {
  220. AVFilterContext *ctx = link->dst;
  221. YADIFContext *yadif = ctx->priv;
  222. if (!yadif->out)
  223. return;
  224. return_frame(ctx, 0);
  225. }
  226. static int request_frame(AVFilterLink *link)
  227. {
  228. AVFilterContext *ctx = link->src;
  229. YADIFContext *yadif = ctx->priv;
  230. if (yadif->frame_pending) {
  231. return_frame(ctx, 1);
  232. return 0;
  233. }
  234. do {
  235. int ret;
  236. if ((ret = avfilter_request_frame(link->src->inputs[0])))
  237. return ret;
  238. } while (!yadif->cur);
  239. return 0;
  240. }
  241. static int poll_frame(AVFilterLink *link)
  242. {
  243. YADIFContext *yadif = link->src->priv;
  244. int ret, val;
  245. if (yadif->frame_pending)
  246. return 1;
  247. val = avfilter_poll_frame(link->src->inputs[0]);
  248. if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
  249. if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
  250. return ret;
  251. val = avfilter_poll_frame(link->src->inputs[0]);
  252. }
  253. assert(yadif->next || !val);
  254. return val * ((yadif->mode&1)+1);
  255. }
  256. static av_cold void uninit(AVFilterContext *ctx)
  257. {
  258. YADIFContext *yadif = ctx->priv;
  259. if (yadif->prev) avfilter_unref_buffer(yadif->prev);
  260. if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
  261. if (yadif->next) avfilter_unref_buffer(yadif->next);
  262. }
  263. static int query_formats(AVFilterContext *ctx)
  264. {
  265. static const enum PixelFormat pix_fmts[] = {
  266. PIX_FMT_YUV420P,
  267. PIX_FMT_YUV422P,
  268. PIX_FMT_YUV444P,
  269. PIX_FMT_YUV410P,
  270. PIX_FMT_YUV411P,
  271. PIX_FMT_GRAY8,
  272. PIX_FMT_YUVJ420P,
  273. PIX_FMT_YUVJ422P,
  274. PIX_FMT_YUVJ444P,
  275. AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
  276. PIX_FMT_YUV440P,
  277. PIX_FMT_YUVJ440P,
  278. AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
  279. AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
  280. AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
  281. PIX_FMT_NONE
  282. };
  283. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  284. return 0;
  285. }
  286. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  287. {
  288. YADIFContext *yadif = ctx->priv;
  289. av_unused int cpu_flags = av_get_cpu_flags();
  290. yadif->mode = 0;
  291. yadif->parity = -1;
  292. yadif->csp = NULL;
  293. if (args) sscanf(args, "%d:%d", &yadif->mode, &yadif->parity);
  294. yadif->filter_line = filter_line_c;
  295. if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
  296. yadif->filter_line = ff_yadif_filter_line_ssse3;
  297. else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
  298. yadif->filter_line = ff_yadif_filter_line_sse2;
  299. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
  300. yadif->filter_line = ff_yadif_filter_line_mmx;
  301. av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d\n", yadif->mode, yadif->parity);
  302. return 0;
  303. }
  304. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  305. AVFilter avfilter_vf_yadif = {
  306. .name = "yadif",
  307. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
  308. .priv_size = sizeof(YADIFContext),
  309. .init = init,
  310. .uninit = uninit,
  311. .query_formats = query_formats,
  312. .inputs = (AVFilterPad[]) {{ .name = "default",
  313. .type = AVMEDIA_TYPE_VIDEO,
  314. .start_frame = start_frame,
  315. .get_video_buffer = get_video_buffer,
  316. .draw_slice = null_draw_slice,
  317. .end_frame = end_frame, },
  318. { .name = NULL}},
  319. .outputs = (AVFilterPad[]) {{ .name = "default",
  320. .type = AVMEDIA_TYPE_VIDEO,
  321. .poll_frame = poll_frame,
  322. .request_frame = request_frame, },
  323. { .name = NULL}},
  324. };