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.

419 lines
13KB

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