vf_yadif.c 14 KB

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