vf_yadif.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
  3. * 2010 James Darnley <james.darnley@gmail.com>
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include "libavutil/cpu.h"
  20. #include "libavutil/common.h"
  21. #include "libavutil/pixdesc.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: top field first
  36. * 1: bottom field first
  37. * -1: auto-detection
  38. */
  39. int parity;
  40. int frame_pending;
  41. /**
  42. * 0: deinterlace all frames
  43. * 1: only deinterlace frames marked as interlaced
  44. */
  45. int auto_enable;
  46. AVFilterBufferRef *cur;
  47. AVFilterBufferRef *next;
  48. AVFilterBufferRef *prev;
  49. AVFilterBufferRef *out;
  50. void (*filter_line)(uint8_t *dst,
  51. uint8_t *prev, uint8_t *cur, uint8_t *next,
  52. int w, int prefs, int mrefs, int parity, int mode);
  53. const AVPixFmtDescriptor *csp;
  54. } YADIFContext;
  55. #define CHECK(j)\
  56. { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
  57. + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
  58. + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
  59. if (score < spatial_score) {\
  60. spatial_score= score;\
  61. spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
  62. #define FILTER \
  63. for (x = 0; x < w; x++) { \
  64. int c = cur[mrefs]; \
  65. int d = (prev2[0] + next2[0])>>1; \
  66. int e = cur[prefs]; \
  67. int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
  68. int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
  69. int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
  70. int diff = FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2); \
  71. int spatial_pred = (c+e)>>1; \
  72. int spatial_score = FFABS(cur[mrefs-1] - cur[prefs-1]) + FFABS(c-e) \
  73. + FFABS(cur[mrefs+1] - cur[prefs+1]) - 1; \
  74. \
  75. CHECK(-1) CHECK(-2) }} }} \
  76. CHECK( 1) CHECK( 2) }} }} \
  77. \
  78. if (mode < 2) { \
  79. int b = (prev2[2*mrefs] + next2[2*mrefs])>>1; \
  80. int f = (prev2[2*prefs] + next2[2*prefs])>>1; \
  81. int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e)); \
  82. int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e)); \
  83. \
  84. diff = FFMAX3(diff, min, -max); \
  85. } \
  86. \
  87. if (spatial_pred > d + diff) \
  88. spatial_pred = d + diff; \
  89. else if (spatial_pred < d - diff) \
  90. spatial_pred = d - diff; \
  91. \
  92. dst[0] = spatial_pred; \
  93. \
  94. dst++; \
  95. cur++; \
  96. prev++; \
  97. next++; \
  98. prev2++; \
  99. next2++; \
  100. }
  101. static void filter_line_c(uint8_t *dst,
  102. uint8_t *prev, uint8_t *cur, uint8_t *next,
  103. int w, int prefs, int mrefs, int parity, int mode)
  104. {
  105. int x;
  106. uint8_t *prev2 = parity ? prev : cur ;
  107. uint8_t *next2 = parity ? cur : next;
  108. FILTER
  109. }
  110. static void filter_line_c_16bit(uint16_t *dst,
  111. uint16_t *prev, uint16_t *cur, uint16_t *next,
  112. int w, int prefs, int mrefs, int parity, int mode)
  113. {
  114. int x;
  115. uint16_t *prev2 = parity ? prev : cur ;
  116. uint16_t *next2 = parity ? cur : next;
  117. mrefs /= 2;
  118. prefs /= 2;
  119. FILTER
  120. }
  121. static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
  122. int parity, int tff)
  123. {
  124. YADIFContext *yadif = ctx->priv;
  125. int y, i;
  126. for (i = 0; i < yadif->csp->nb_components; i++) {
  127. int w = dstpic->video->w;
  128. int h = dstpic->video->h;
  129. int refs = yadif->cur->linesize[i];
  130. int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
  131. if (i == 1 || i == 2) {
  132. /* Why is this not part of the per-plane description thing? */
  133. w >>= yadif->csp->log2_chroma_w;
  134. h >>= yadif->csp->log2_chroma_h;
  135. }
  136. for (y = 0; y < h; y++) {
  137. if ((y ^ parity) & 1) {
  138. uint8_t *prev = &yadif->prev->data[i][y*refs];
  139. uint8_t *cur = &yadif->cur ->data[i][y*refs];
  140. uint8_t *next = &yadif->next->data[i][y*refs];
  141. uint8_t *dst = &dstpic->data[i][y*dstpic->linesize[i]];
  142. int mode = y==1 || y+2==h ? 2 : yadif->mode;
  143. yadif->filter_line(dst, prev, cur, next, w, y+1<h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
  144. } else {
  145. memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
  146. &yadif->cur->data[i][y*refs], w*df);
  147. }
  148. }
  149. }
  150. #if HAVE_MMX
  151. __asm__ volatile("emms \n\t" : : : "memory");
  152. #endif
  153. }
  154. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  155. {
  156. AVFilterBufferRef *picref;
  157. int width = FFALIGN(w, 32);
  158. int height= FFALIGN(h+2, 32);
  159. int i;
  160. picref = avfilter_default_get_video_buffer(link, perms, width, height);
  161. picref->video->w = w;
  162. picref->video->h = h;
  163. for (i = 0; i < 3; i++)
  164. picref->data[i] += picref->linesize[i];
  165. return picref;
  166. }
  167. static void return_frame(AVFilterContext *ctx, int is_second)
  168. {
  169. YADIFContext *yadif = ctx->priv;
  170. AVFilterLink *link= ctx->outputs[0];
  171. int tff;
  172. if (yadif->parity == -1) {
  173. tff = yadif->cur->video->interlaced ?
  174. yadif->cur->video->top_field_first : 1;
  175. } else {
  176. tff = yadif->parity^1;
  177. }
  178. if (is_second) {
  179. yadif->out = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
  180. AV_PERM_REUSE, link->w, link->h);
  181. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  182. yadif->out->video->interlaced = 0;
  183. }
  184. if (!yadif->csp)
  185. yadif->csp = &av_pix_fmt_descriptors[link->format];
  186. if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
  187. yadif->filter_line = (void*)filter_line_c_16bit;
  188. filter(ctx, yadif->out, tff ^ !is_second, tff);
  189. if (is_second) {
  190. if (yadif->next->pts != AV_NOPTS_VALUE &&
  191. yadif->cur->pts != AV_NOPTS_VALUE) {
  192. yadif->out->pts =
  193. (yadif->next->pts&yadif->cur->pts) +
  194. ((yadif->next->pts^yadif->cur->pts)>>1);
  195. } else {
  196. yadif->out->pts = AV_NOPTS_VALUE;
  197. }
  198. avfilter_start_frame(ctx->outputs[0], yadif->out);
  199. }
  200. avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
  201. avfilter_end_frame(ctx->outputs[0]);
  202. yadif->frame_pending = (yadif->mode&1) && !is_second;
  203. }
  204. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  205. {
  206. AVFilterContext *ctx = link->dst;
  207. YADIFContext *yadif = ctx->priv;
  208. if (yadif->frame_pending)
  209. return_frame(ctx, 1);
  210. if (yadif->prev)
  211. avfilter_unref_buffer(yadif->prev);
  212. yadif->prev = yadif->cur;
  213. yadif->cur = yadif->next;
  214. yadif->next = picref;
  215. if (!yadif->cur)
  216. return;
  217. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  218. yadif->out = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  219. avfilter_unref_buffer(yadif->prev);
  220. yadif->prev = NULL;
  221. avfilter_start_frame(ctx->outputs[0], yadif->out);
  222. return;
  223. }
  224. if (!yadif->prev)
  225. yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  226. yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
  227. AV_PERM_REUSE, link->w, link->h);
  228. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  229. yadif->out->video->interlaced = 0;
  230. avfilter_start_frame(ctx->outputs[0], yadif->out);
  231. }
  232. static void end_frame(AVFilterLink *link)
  233. {
  234. AVFilterContext *ctx = link->dst;
  235. YADIFContext *yadif = ctx->priv;
  236. if (!yadif->out)
  237. return;
  238. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  239. avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
  240. avfilter_end_frame(ctx->outputs[0]);
  241. return;
  242. }
  243. return_frame(ctx, 0);
  244. }
  245. static int request_frame(AVFilterLink *link)
  246. {
  247. AVFilterContext *ctx = link->src;
  248. YADIFContext *yadif = ctx->priv;
  249. if (yadif->frame_pending) {
  250. return_frame(ctx, 1);
  251. return 0;
  252. }
  253. do {
  254. int ret;
  255. if ((ret = avfilter_request_frame(link->src->inputs[0])))
  256. return ret;
  257. } while (!yadif->cur);
  258. return 0;
  259. }
  260. static int poll_frame(AVFilterLink *link)
  261. {
  262. YADIFContext *yadif = link->src->priv;
  263. int ret, val;
  264. if (yadif->frame_pending)
  265. return 1;
  266. val = avfilter_poll_frame(link->src->inputs[0]);
  267. if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
  268. if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
  269. return ret;
  270. val = avfilter_poll_frame(link->src->inputs[0]);
  271. }
  272. assert(yadif->next || !val);
  273. if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
  274. return val;
  275. return val * ((yadif->mode&1)+1);
  276. }
  277. static av_cold void uninit(AVFilterContext *ctx)
  278. {
  279. YADIFContext *yadif = ctx->priv;
  280. if (yadif->prev) avfilter_unref_buffer(yadif->prev);
  281. if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
  282. if (yadif->next) avfilter_unref_buffer(yadif->next);
  283. }
  284. static int query_formats(AVFilterContext *ctx)
  285. {
  286. static const enum PixelFormat pix_fmts[] = {
  287. PIX_FMT_YUV420P,
  288. PIX_FMT_YUV422P,
  289. PIX_FMT_YUV444P,
  290. PIX_FMT_YUV410P,
  291. PIX_FMT_YUV411P,
  292. PIX_FMT_GRAY8,
  293. PIX_FMT_YUVJ420P,
  294. PIX_FMT_YUVJ422P,
  295. PIX_FMT_YUVJ444P,
  296. AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
  297. PIX_FMT_YUV440P,
  298. PIX_FMT_YUVJ440P,
  299. AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
  300. AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
  301. AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
  302. AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
  303. AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
  304. AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
  305. PIX_FMT_YUVA420P,
  306. PIX_FMT_NONE
  307. };
  308. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  309. return 0;
  310. }
  311. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  312. {
  313. YADIFContext *yadif = ctx->priv;
  314. av_unused int cpu_flags = av_get_cpu_flags();
  315. yadif->mode = 0;
  316. yadif->parity = -1;
  317. yadif->auto_enable = 0;
  318. yadif->csp = NULL;
  319. if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
  320. yadif->filter_line = filter_line_c;
  321. if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
  322. yadif->filter_line = ff_yadif_filter_line_ssse3;
  323. else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
  324. yadif->filter_line = ff_yadif_filter_line_sse2;
  325. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
  326. yadif->filter_line = ff_yadif_filter_line_mmx;
  327. av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
  328. return 0;
  329. }
  330. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  331. AVFilter avfilter_vf_yadif = {
  332. .name = "yadif",
  333. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
  334. .priv_size = sizeof(YADIFContext),
  335. .init = init,
  336. .uninit = uninit,
  337. .query_formats = query_formats,
  338. .inputs = (const AVFilterPad[]) {{ .name = "default",
  339. .type = AVMEDIA_TYPE_VIDEO,
  340. .start_frame = start_frame,
  341. .get_video_buffer = get_video_buffer,
  342. .draw_slice = null_draw_slice,
  343. .end_frame = end_frame,
  344. .rej_perms = AV_PERM_REUSE2, },
  345. { .name = NULL}},
  346. .outputs = (const AVFilterPad[]) {{ .name = "default",
  347. .type = AVMEDIA_TYPE_VIDEO,
  348. .poll_frame = poll_frame,
  349. .request_frame = request_frame, },
  350. { .name = NULL}},
  351. };