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.

459 lines
14KB

  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/avassert.h"
  20. #include "libavutil/cpu.h"
  21. #include "libavutil/common.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "avfilter.h"
  24. #include "yadif.h"
  25. #undef NDEBUG
  26. #include <assert.h>
  27. typedef struct {
  28. /**
  29. * 0: send 1 frame for each frame
  30. * 1: send 1 frame for each field
  31. * 2: like 0 but skips spatial interlacing check
  32. * 3: like 1 but skips spatial interlacing check
  33. */
  34. int mode;
  35. /**
  36. * 0: top field first
  37. * 1: bottom field first
  38. * -1: auto-detection
  39. */
  40. int parity;
  41. int frame_pending;
  42. /**
  43. * 0: deinterlace all frames
  44. * 1: only deinterlace frames marked as interlaced
  45. */
  46. int auto_enable;
  47. AVFilterBufferRef *cur;
  48. AVFilterBufferRef *next;
  49. AVFilterBufferRef *prev;
  50. AVFilterBufferRef *out;
  51. void (*filter_line)(uint8_t *dst,
  52. uint8_t *prev, uint8_t *cur, uint8_t *next,
  53. int w, int prefs, int mrefs, int parity, int mode);
  54. const AVPixFmtDescriptor *csp;
  55. int eof;
  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 + 8) / 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 / 8 == 1)
  189. yadif->filter_line = (void*)filter_line_c_16bit;
  190. filter(ctx, yadif->out, tff ^ !is_second, tff);
  191. if (is_second) {
  192. int64_t cur_pts = yadif->cur->pts;
  193. int64_t next_pts = yadif->next->pts;
  194. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  195. yadif->out->pts = cur_pts + next_pts;
  196. } else {
  197. yadif->out->pts = AV_NOPTS_VALUE;
  198. }
  199. avfilter_start_frame(ctx->outputs[0], yadif->out);
  200. }
  201. avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
  202. avfilter_end_frame(ctx->outputs[0]);
  203. yadif->frame_pending = (yadif->mode&1) && !is_second;
  204. }
  205. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  206. {
  207. AVFilterContext *ctx = link->dst;
  208. YADIFContext *yadif = ctx->priv;
  209. av_assert0(picref);
  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. if (yadif->out->pts != AV_NOPTS_VALUE)
  224. yadif->out->pts *= 2;
  225. avfilter_start_frame(ctx->outputs[0], yadif->out);
  226. return;
  227. }
  228. if (!yadif->prev)
  229. yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  230. yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
  231. AV_PERM_REUSE, link->w, link->h);
  232. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  233. yadif->out->video->interlaced = 0;
  234. if (yadif->out->pts != AV_NOPTS_VALUE)
  235. yadif->out->pts *= 2;
  236. avfilter_start_frame(ctx->outputs[0], yadif->out);
  237. }
  238. static void end_frame(AVFilterLink *link)
  239. {
  240. AVFilterContext *ctx = link->dst;
  241. YADIFContext *yadif = ctx->priv;
  242. if (!yadif->out)
  243. return;
  244. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  245. avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
  246. avfilter_end_frame(ctx->outputs[0]);
  247. return;
  248. }
  249. return_frame(ctx, 0);
  250. }
  251. static int request_frame(AVFilterLink *link)
  252. {
  253. AVFilterContext *ctx = link->src;
  254. YADIFContext *yadif = ctx->priv;
  255. if (yadif->frame_pending) {
  256. return_frame(ctx, 1);
  257. return 0;
  258. }
  259. do {
  260. int ret;
  261. if (yadif->eof)
  262. return AVERROR_EOF;
  263. ret = avfilter_request_frame(link->src->inputs[0]);
  264. if (ret == AVERROR_EOF && yadif->cur) {
  265. AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, AV_PERM_READ);
  266. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  267. start_frame(link->src->inputs[0], next);
  268. end_frame(link->src->inputs[0]);
  269. yadif->eof = 1;
  270. } else if (ret < 0) {
  271. return ret;
  272. }
  273. } while (!yadif->cur);
  274. return 0;
  275. }
  276. static int poll_frame(AVFilterLink *link)
  277. {
  278. YADIFContext *yadif = link->src->priv;
  279. int ret, val;
  280. if (yadif->frame_pending)
  281. return 1;
  282. val = avfilter_poll_frame(link->src->inputs[0]);
  283. if (val <= 0)
  284. return val;
  285. if (val >= 1 && !yadif->next) { //FIXME change API to not requre this red tape
  286. if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
  287. return ret;
  288. val = avfilter_poll_frame(link->src->inputs[0]);
  289. if (val <= 0)
  290. return val;
  291. }
  292. assert(yadif->next || !val);
  293. if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
  294. return val;
  295. return val * ((yadif->mode&1)+1);
  296. }
  297. static av_cold void uninit(AVFilterContext *ctx)
  298. {
  299. YADIFContext *yadif = ctx->priv;
  300. if (yadif->prev) avfilter_unref_buffer(yadif->prev);
  301. if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
  302. if (yadif->next) avfilter_unref_buffer(yadif->next);
  303. }
  304. static int query_formats(AVFilterContext *ctx)
  305. {
  306. static const enum PixelFormat pix_fmts[] = {
  307. PIX_FMT_YUV420P,
  308. PIX_FMT_YUV422P,
  309. PIX_FMT_YUV444P,
  310. PIX_FMT_YUV410P,
  311. PIX_FMT_YUV411P,
  312. PIX_FMT_GRAY8,
  313. PIX_FMT_YUVJ420P,
  314. PIX_FMT_YUVJ422P,
  315. PIX_FMT_YUVJ444P,
  316. AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
  317. PIX_FMT_YUV440P,
  318. PIX_FMT_YUVJ440P,
  319. AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
  320. AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
  321. AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
  322. AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
  323. AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
  324. AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
  325. PIX_FMT_YUVA420P,
  326. PIX_FMT_YUVA422P,
  327. PIX_FMT_YUVA444P,
  328. PIX_FMT_NONE
  329. };
  330. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  331. return 0;
  332. }
  333. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  334. {
  335. YADIFContext *yadif = ctx->priv;
  336. int cpu_flags = av_get_cpu_flags();
  337. yadif->mode = 0;
  338. yadif->parity = -1;
  339. yadif->auto_enable = 0;
  340. yadif->csp = NULL;
  341. if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
  342. yadif->filter_line = filter_line_c;
  343. if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
  344. yadif->filter_line = ff_yadif_filter_line_ssse3;
  345. else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
  346. yadif->filter_line = ff_yadif_filter_line_sse2;
  347. else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
  348. yadif->filter_line = ff_yadif_filter_line_mmx;
  349. av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
  350. return 0;
  351. }
  352. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  353. static int config_props(AVFilterLink *link)
  354. {
  355. link->time_base.num = link->src->inputs[0]->time_base.num;
  356. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  357. link->w = link->src->inputs[0]->w;
  358. link->h = link->src->inputs[0]->h;
  359. return 0;
  360. }
  361. AVFilter avfilter_vf_yadif = {
  362. .name = "yadif",
  363. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
  364. .priv_size = sizeof(YADIFContext),
  365. .init = init,
  366. .uninit = uninit,
  367. .query_formats = query_formats,
  368. .inputs = (const AVFilterPad[]) {{ .name = "default",
  369. .type = AVMEDIA_TYPE_VIDEO,
  370. .start_frame = start_frame,
  371. .get_video_buffer = get_video_buffer,
  372. .draw_slice = null_draw_slice,
  373. .end_frame = end_frame,
  374. .rej_perms = AV_PERM_REUSE2, },
  375. { .name = NULL}},
  376. .outputs = (const AVFilterPad[]) {{ .name = "default",
  377. .type = AVMEDIA_TYPE_VIDEO,
  378. .poll_frame = poll_frame,
  379. .request_frame = request_frame,
  380. .config_props = config_props, },
  381. { .name = NULL}},
  382. };