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.

389 lines
12KB

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