vf_yadif.c 12 KB

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