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.

394 lines
12KB

  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. 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. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  179. yadif->out->video->interlaced = 0;
  180. }
  181. if (!yadif->csp)
  182. yadif->csp = &av_pix_fmt_descriptors[link->format];
  183. if (yadif->csp->comp[0].depth_minus1 == 15)
  184. yadif->filter_line = filter_line_c_16bit;
  185. filter(ctx, yadif->out, tff ^ !is_second, tff);
  186. if (is_second) {
  187. if (yadif->next->pts != AV_NOPTS_VALUE &&
  188. yadif->cur->pts != AV_NOPTS_VALUE) {
  189. yadif->out->pts =
  190. (yadif->next->pts&yadif->cur->pts) +
  191. ((yadif->next->pts^yadif->cur->pts)>>1);
  192. } else {
  193. yadif->out->pts = AV_NOPTS_VALUE;
  194. }
  195. avfilter_start_frame(ctx->outputs[0], yadif->out);
  196. }
  197. avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
  198. avfilter_end_frame(ctx->outputs[0]);
  199. yadif->frame_pending = (yadif->mode&1) && !is_second;
  200. }
  201. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  202. {
  203. AVFilterContext *ctx = link->dst;
  204. YADIFContext *yadif = ctx->priv;
  205. if (yadif->frame_pending)
  206. return_frame(ctx, 1);
  207. if (yadif->prev)
  208. avfilter_unref_buffer(yadif->prev);
  209. yadif->prev = yadif->cur;
  210. yadif->cur = yadif->next;
  211. yadif->next = picref;
  212. if (!yadif->cur)
  213. return;
  214. if (!yadif->prev)
  215. yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  216. yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
  217. AV_PERM_REUSE, link->w, link->h);
  218. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  219. yadif->out->video->interlaced = 0;
  220. avfilter_start_frame(ctx->outputs[0], yadif->out);
  221. }
  222. static void end_frame(AVFilterLink *link)
  223. {
  224. AVFilterContext *ctx = link->dst;
  225. YADIFContext *yadif = ctx->priv;
  226. if (!yadif->out)
  227. return;
  228. return_frame(ctx, 0);
  229. }
  230. static int request_frame(AVFilterLink *link)
  231. {
  232. AVFilterContext *ctx = link->src;
  233. YADIFContext *yadif = ctx->priv;
  234. if (yadif->frame_pending) {
  235. return_frame(ctx, 1);
  236. return 0;
  237. }
  238. do {
  239. int ret;
  240. if ((ret = avfilter_request_frame(link->src->inputs[0])))
  241. return ret;
  242. } while (!yadif->cur);
  243. return 0;
  244. }
  245. static int poll_frame(AVFilterLink *link)
  246. {
  247. YADIFContext *yadif = link->src->priv;
  248. int ret, val;
  249. if (yadif->frame_pending)
  250. return 1;
  251. val = avfilter_poll_frame(link->src->inputs[0]);
  252. if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
  253. if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
  254. return ret;
  255. val = avfilter_poll_frame(link->src->inputs[0]);
  256. }
  257. assert(yadif->next || !val);
  258. return val * ((yadif->mode&1)+1);
  259. }
  260. static av_cold void uninit(AVFilterContext *ctx)
  261. {
  262. YADIFContext *yadif = ctx->priv;
  263. if (yadif->prev) avfilter_unref_buffer(yadif->prev);
  264. if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
  265. if (yadif->next) avfilter_unref_buffer(yadif->next);
  266. }
  267. static int query_formats(AVFilterContext *ctx)
  268. {
  269. static const enum PixelFormat pix_fmts[] = {
  270. PIX_FMT_YUV420P,
  271. PIX_FMT_YUV422P,
  272. PIX_FMT_YUV444P,
  273. PIX_FMT_YUV410P,
  274. PIX_FMT_YUV411P,
  275. PIX_FMT_GRAY8,
  276. PIX_FMT_YUVJ420P,
  277. PIX_FMT_YUVJ422P,
  278. PIX_FMT_YUVJ444P,
  279. AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
  280. PIX_FMT_YUV440P,
  281. PIX_FMT_YUVJ440P,
  282. AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
  283. AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
  284. AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
  285. PIX_FMT_NONE
  286. };
  287. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  288. return 0;
  289. }
  290. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  291. {
  292. YADIFContext *yadif = ctx->priv;
  293. av_unused int cpu_flags = av_get_cpu_flags();
  294. yadif->mode = 0;
  295. yadif->parity = -1;
  296. yadif->csp = NULL;
  297. if (args) sscanf(args, "%d:%d", &yadif->mode, &yadif->parity);
  298. yadif->filter_line = filter_line_c;
  299. if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
  300. yadif->filter_line = ff_yadif_filter_line_ssse3;
  301. else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
  302. yadif->filter_line = ff_yadif_filter_line_sse2;
  303. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
  304. yadif->filter_line = ff_yadif_filter_line_mmx;
  305. av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d\n", yadif->mode, yadif->parity);
  306. return 0;
  307. }
  308. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  309. AVFilter avfilter_vf_yadif = {
  310. .name = "yadif",
  311. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
  312. .priv_size = sizeof(YADIFContext),
  313. .init = init,
  314. .uninit = uninit,
  315. .query_formats = query_formats,
  316. .inputs = (AVFilterPad[]) {{ .name = "default",
  317. .type = AVMEDIA_TYPE_VIDEO,
  318. .start_frame = start_frame,
  319. .get_video_buffer = get_video_buffer,
  320. .draw_slice = null_draw_slice,
  321. .end_frame = end_frame, },
  322. { .name = NULL}},
  323. .outputs = (AVFilterPad[]) {{ .name = "default",
  324. .type = AVMEDIA_TYPE_VIDEO,
  325. .poll_frame = poll_frame,
  326. .request_frame = request_frame, },
  327. { .name = NULL}},
  328. };