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.

436 lines
13KB

  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. uint64_t next_pts = yadif->next->pts;
  193. uint64_t cur_pts = yadif->cur->pts;
  194. uint64_t prev_pts = yadif->prev->pts;
  195. uint64_t ft = FFMIN3( cur_pts-prev_pts,
  196. next_pts-cur_pts,
  197. (next_pts-prev_pts)/2);
  198. if(next_pts - cur_pts < 2*ft)
  199. yadif->out->pts =
  200. (next_pts&cur_pts) +
  201. ((next_pts^cur_pts)>>1);
  202. else
  203. yadif->out->pts = cur_pts + ft/2;
  204. } else {
  205. yadif->out->pts = AV_NOPTS_VALUE;
  206. }
  207. avfilter_start_frame(ctx->outputs[0], yadif->out);
  208. }
  209. avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
  210. avfilter_end_frame(ctx->outputs[0]);
  211. yadif->frame_pending = (yadif->mode&1) && !is_second;
  212. }
  213. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  214. {
  215. AVFilterContext *ctx = link->dst;
  216. YADIFContext *yadif = ctx->priv;
  217. if (yadif->frame_pending)
  218. return_frame(ctx, 1);
  219. if (yadif->prev)
  220. avfilter_unref_buffer(yadif->prev);
  221. yadif->prev = yadif->cur;
  222. yadif->cur = yadif->next;
  223. yadif->next = picref;
  224. if (!yadif->cur)
  225. return;
  226. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  227. yadif->out = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  228. avfilter_unref_buffer(yadif->prev);
  229. yadif->prev = NULL;
  230. avfilter_start_frame(ctx->outputs[0], yadif->out);
  231. return;
  232. }
  233. if (!yadif->prev)
  234. yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  235. yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
  236. AV_PERM_REUSE, link->w, link->h);
  237. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  238. yadif->out->video->interlaced = 0;
  239. avfilter_start_frame(ctx->outputs[0], yadif->out);
  240. }
  241. static void end_frame(AVFilterLink *link)
  242. {
  243. AVFilterContext *ctx = link->dst;
  244. YADIFContext *yadif = ctx->priv;
  245. if (!yadif->out)
  246. return;
  247. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  248. avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
  249. avfilter_end_frame(ctx->outputs[0]);
  250. return;
  251. }
  252. return_frame(ctx, 0);
  253. }
  254. static int request_frame(AVFilterLink *link)
  255. {
  256. AVFilterContext *ctx = link->src;
  257. YADIFContext *yadif = ctx->priv;
  258. if (yadif->frame_pending) {
  259. return_frame(ctx, 1);
  260. return 0;
  261. }
  262. do {
  263. int ret;
  264. if ((ret = avfilter_request_frame(link->src->inputs[0])))
  265. return ret;
  266. } while (!yadif->cur);
  267. return 0;
  268. }
  269. static int poll_frame(AVFilterLink *link)
  270. {
  271. YADIFContext *yadif = link->src->priv;
  272. int ret, val;
  273. if (yadif->frame_pending)
  274. return 1;
  275. val = avfilter_poll_frame(link->src->inputs[0]);
  276. if (val <= 0)
  277. return val;
  278. if (val >= 1 && !yadif->next) { //FIXME change API to not requre this red tape
  279. if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
  280. return ret;
  281. val = avfilter_poll_frame(link->src->inputs[0]);
  282. if (val <= 0)
  283. return val;
  284. }
  285. assert(yadif->next || !val);
  286. if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
  287. return val;
  288. return val * ((yadif->mode&1)+1);
  289. }
  290. static av_cold void uninit(AVFilterContext *ctx)
  291. {
  292. YADIFContext *yadif = ctx->priv;
  293. if (yadif->prev) avfilter_unref_buffer(yadif->prev);
  294. if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
  295. if (yadif->next) avfilter_unref_buffer(yadif->next);
  296. }
  297. static int query_formats(AVFilterContext *ctx)
  298. {
  299. static const enum PixelFormat pix_fmts[] = {
  300. PIX_FMT_YUV420P,
  301. PIX_FMT_YUV422P,
  302. PIX_FMT_YUV444P,
  303. PIX_FMT_YUV410P,
  304. PIX_FMT_YUV411P,
  305. PIX_FMT_GRAY8,
  306. PIX_FMT_YUVJ420P,
  307. PIX_FMT_YUVJ422P,
  308. PIX_FMT_YUVJ444P,
  309. AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
  310. PIX_FMT_YUV440P,
  311. PIX_FMT_YUVJ440P,
  312. AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
  313. AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
  314. AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
  315. AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
  316. AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
  317. AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
  318. PIX_FMT_YUVA420P,
  319. PIX_FMT_NONE
  320. };
  321. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  322. return 0;
  323. }
  324. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  325. {
  326. YADIFContext *yadif = ctx->priv;
  327. int cpu_flags = av_get_cpu_flags();
  328. yadif->mode = 0;
  329. yadif->parity = -1;
  330. yadif->auto_enable = 0;
  331. yadif->csp = NULL;
  332. if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
  333. yadif->filter_line = filter_line_c;
  334. if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
  335. yadif->filter_line = ff_yadif_filter_line_ssse3;
  336. else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
  337. yadif->filter_line = ff_yadif_filter_line_sse2;
  338. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
  339. yadif->filter_line = ff_yadif_filter_line_mmx;
  340. av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
  341. return 0;
  342. }
  343. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  344. AVFilter avfilter_vf_yadif = {
  345. .name = "yadif",
  346. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
  347. .priv_size = sizeof(YADIFContext),
  348. .init = init,
  349. .uninit = uninit,
  350. .query_formats = query_formats,
  351. .inputs = (const AVFilterPad[]) {{ .name = "default",
  352. .type = AVMEDIA_TYPE_VIDEO,
  353. .start_frame = start_frame,
  354. .get_video_buffer = get_video_buffer,
  355. .draw_slice = null_draw_slice,
  356. .end_frame = end_frame,
  357. .rej_perms = AV_PERM_REUSE2, },
  358. { .name = NULL}},
  359. .outputs = (const AVFilterPad[]) {{ .name = "default",
  360. .type = AVMEDIA_TYPE_VIDEO,
  361. .poll_frame = poll_frame,
  362. .request_frame = request_frame, },
  363. { .name = NULL}},
  364. };