vf_yadif.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
  3. * 2010 James Darnley <james.darnley@gmail.com>
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/cpu.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "libavutil/imgutils.h"
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. #include "yadif.h"
  30. typedef struct ThreadData {
  31. AVFrame *frame;
  32. int plane;
  33. int w, h;
  34. int parity;
  35. int tff;
  36. } ThreadData;
  37. #define CHECK(j)\
  38. { int score = FFABS(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\
  39. + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
  40. + FFABS(cur[mrefs + 1 + (j)] - cur[prefs + 1 - (j)]);\
  41. if (score < spatial_score) {\
  42. spatial_score= score;\
  43. spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
  44. /* The is_not_edge argument here controls when the code will enter a branch
  45. * which reads up to and including x-3 and x+3. */
  46. #define FILTER(start, end, is_not_edge) \
  47. for (x = start; x < end; x++) { \
  48. int c = cur[mrefs]; \
  49. int d = (prev2[0] + next2[0])>>1; \
  50. int e = cur[prefs]; \
  51. int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
  52. int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
  53. int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
  54. int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
  55. int spatial_pred = (c+e) >> 1; \
  56. \
  57. if (is_not_edge) {\
  58. int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
  59. + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
  60. CHECK(-1) CHECK(-2) }} }} \
  61. CHECK( 1) CHECK( 2) }} }} \
  62. }\
  63. \
  64. if (!(mode&2)) { \
  65. int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
  66. int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
  67. int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
  68. int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
  69. \
  70. diff = FFMAX3(diff, min, -max); \
  71. } \
  72. \
  73. if (spatial_pred > d + diff) \
  74. spatial_pred = d + diff; \
  75. else if (spatial_pred < d - diff) \
  76. spatial_pred = d - diff; \
  77. \
  78. dst[0] = spatial_pred; \
  79. \
  80. dst++; \
  81. cur++; \
  82. prev++; \
  83. next++; \
  84. prev2++; \
  85. next2++; \
  86. }
  87. static void filter_line_c(void *dst1,
  88. void *prev1, void *cur1, void *next1,
  89. int w, int prefs, int mrefs, int parity, int mode)
  90. {
  91. uint8_t *dst = dst1;
  92. uint8_t *prev = prev1;
  93. uint8_t *cur = cur1;
  94. uint8_t *next = next1;
  95. int x;
  96. uint8_t *prev2 = parity ? prev : cur ;
  97. uint8_t *next2 = parity ? cur : next;
  98. /* The function is called with the pointers already pointing to data[3] and
  99. * with 6 subtracted from the width. This allows the FILTER macro to be
  100. * called so that it processes all the pixels normally. A constant value of
  101. * true for is_not_edge lets the compiler ignore the if statement. */
  102. FILTER(0, w, 1)
  103. }
  104. #define MAX_ALIGN 8
  105. static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
  106. int w, int prefs, int mrefs, int parity, int mode)
  107. {
  108. uint8_t *dst = dst1;
  109. uint8_t *prev = prev1;
  110. uint8_t *cur = cur1;
  111. uint8_t *next = next1;
  112. int x;
  113. uint8_t *prev2 = parity ? prev : cur ;
  114. uint8_t *next2 = parity ? cur : next;
  115. const int edge = MAX_ALIGN - 1;
  116. /* Only edge pixels need to be processed here. A constant value of false
  117. * for is_not_edge should let the compiler ignore the whole branch. */
  118. FILTER(0, 3, 0)
  119. dst = (uint8_t*)dst1 + w - edge;
  120. prev = (uint8_t*)prev1 + w - edge;
  121. cur = (uint8_t*)cur1 + w - edge;
  122. next = (uint8_t*)next1 + w - edge;
  123. prev2 = (uint8_t*)(parity ? prev : cur);
  124. next2 = (uint8_t*)(parity ? cur : next);
  125. FILTER(w - edge, w - 3, 1)
  126. FILTER(w - 3, w, 0)
  127. }
  128. static void filter_line_c_16bit(void *dst1,
  129. void *prev1, void *cur1, void *next1,
  130. int w, int prefs, int mrefs, int parity,
  131. int mode)
  132. {
  133. uint16_t *dst = dst1;
  134. uint16_t *prev = prev1;
  135. uint16_t *cur = cur1;
  136. uint16_t *next = next1;
  137. int x;
  138. uint16_t *prev2 = parity ? prev : cur ;
  139. uint16_t *next2 = parity ? cur : next;
  140. mrefs /= 2;
  141. prefs /= 2;
  142. FILTER(0, w, 1)
  143. }
  144. static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
  145. int w, int prefs, int mrefs, int parity, int mode)
  146. {
  147. uint16_t *dst = dst1;
  148. uint16_t *prev = prev1;
  149. uint16_t *cur = cur1;
  150. uint16_t *next = next1;
  151. int x;
  152. uint16_t *prev2 = parity ? prev : cur ;
  153. uint16_t *next2 = parity ? cur : next;
  154. const int edge = MAX_ALIGN / 2 - 1;
  155. mrefs /= 2;
  156. prefs /= 2;
  157. FILTER(0, 3, 0)
  158. dst = (uint16_t*)dst1 + w - edge;
  159. prev = (uint16_t*)prev1 + w - edge;
  160. cur = (uint16_t*)cur1 + w - edge;
  161. next = (uint16_t*)next1 + w - edge;
  162. prev2 = (uint16_t*)(parity ? prev : cur);
  163. next2 = (uint16_t*)(parity ? cur : next);
  164. FILTER(w - edge, w - 3, 1)
  165. FILTER(w - 3, w, 0)
  166. }
  167. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  168. {
  169. YADIFContext *s = ctx->priv;
  170. ThreadData *td = arg;
  171. int refs = s->cur->linesize[td->plane];
  172. int df = (s->csp->comp[td->plane].depth + 7) / 8;
  173. int pix_3 = 3 * df;
  174. int slice_start = (td->h * jobnr ) / nb_jobs;
  175. int slice_end = (td->h * (jobnr+1)) / nb_jobs;
  176. int y;
  177. int edge = 3 + MAX_ALIGN / df - 1;
  178. /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
  179. * we need to call the c variant which avoids this for border pixels
  180. */
  181. for (y = slice_start; y < slice_end; y++) {
  182. if ((y ^ td->parity) & 1) {
  183. uint8_t *prev = &s->prev->data[td->plane][y * refs];
  184. uint8_t *cur = &s->cur ->data[td->plane][y * refs];
  185. uint8_t *next = &s->next->data[td->plane][y * refs];
  186. uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
  187. int mode = y == 1 || y + 2 == td->h ? 2 : s->mode;
  188. s->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
  189. next + pix_3, td->w - edge,
  190. y + 1 < td->h ? refs : -refs,
  191. y ? -refs : refs,
  192. td->parity ^ td->tff, mode);
  193. s->filter_edges(dst, prev, cur, next, td->w,
  194. y + 1 < td->h ? refs : -refs,
  195. y ? -refs : refs,
  196. td->parity ^ td->tff, mode);
  197. } else {
  198. memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
  199. &s->cur->data[td->plane][y * refs], td->w * df);
  200. }
  201. }
  202. return 0;
  203. }
  204. static void filter(AVFilterContext *ctx, AVFrame *dstpic,
  205. int parity, int tff)
  206. {
  207. YADIFContext *yadif = ctx->priv;
  208. ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
  209. int i;
  210. for (i = 0; i < yadif->csp->nb_components; i++) {
  211. int w = dstpic->width;
  212. int h = dstpic->height;
  213. if (i == 1 || i == 2) {
  214. w = AV_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w);
  215. h = AV_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h);
  216. }
  217. td.w = w;
  218. td.h = h;
  219. td.plane = i;
  220. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ff_filter_get_nb_threads(ctx)));
  221. }
  222. emms_c();
  223. }
  224. static av_cold void uninit(AVFilterContext *ctx)
  225. {
  226. YADIFContext *yadif = ctx->priv;
  227. av_frame_free(&yadif->prev);
  228. av_frame_free(&yadif->cur );
  229. av_frame_free(&yadif->next);
  230. }
  231. static int query_formats(AVFilterContext *ctx)
  232. {
  233. static const enum AVPixelFormat pix_fmts[] = {
  234. AV_PIX_FMT_YUV420P,
  235. AV_PIX_FMT_YUV422P,
  236. AV_PIX_FMT_YUV444P,
  237. AV_PIX_FMT_YUV410P,
  238. AV_PIX_FMT_YUV411P,
  239. AV_PIX_FMT_GRAY8,
  240. AV_PIX_FMT_YUVJ420P,
  241. AV_PIX_FMT_YUVJ422P,
  242. AV_PIX_FMT_YUVJ444P,
  243. AV_PIX_FMT_GRAY16,
  244. AV_PIX_FMT_YUV440P,
  245. AV_PIX_FMT_YUVJ440P,
  246. AV_PIX_FMT_YUV420P9,
  247. AV_PIX_FMT_YUV422P9,
  248. AV_PIX_FMT_YUV444P9,
  249. AV_PIX_FMT_YUV420P10,
  250. AV_PIX_FMT_YUV422P10,
  251. AV_PIX_FMT_YUV444P10,
  252. AV_PIX_FMT_YUV420P12,
  253. AV_PIX_FMT_YUV422P12,
  254. AV_PIX_FMT_YUV444P12,
  255. AV_PIX_FMT_YUV420P14,
  256. AV_PIX_FMT_YUV422P14,
  257. AV_PIX_FMT_YUV444P14,
  258. AV_PIX_FMT_YUV420P16,
  259. AV_PIX_FMT_YUV422P16,
  260. AV_PIX_FMT_YUV444P16,
  261. AV_PIX_FMT_YUVA420P,
  262. AV_PIX_FMT_YUVA422P,
  263. AV_PIX_FMT_YUVA444P,
  264. AV_PIX_FMT_GBRP,
  265. AV_PIX_FMT_GBRP9,
  266. AV_PIX_FMT_GBRP10,
  267. AV_PIX_FMT_GBRP12,
  268. AV_PIX_FMT_GBRP14,
  269. AV_PIX_FMT_GBRP16,
  270. AV_PIX_FMT_GBRAP,
  271. AV_PIX_FMT_NONE
  272. };
  273. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  274. if (!fmts_list)
  275. return AVERROR(ENOMEM);
  276. return ff_set_common_formats(ctx, fmts_list);
  277. }
  278. static int config_props(AVFilterLink *link)
  279. {
  280. AVFilterContext *ctx = link->src;
  281. YADIFContext *s = ctx->priv;
  282. link->time_base.num = ctx->inputs[0]->time_base.num;
  283. link->time_base.den = ctx->inputs[0]->time_base.den * 2;
  284. link->w = ctx->inputs[0]->w;
  285. link->h = ctx->inputs[0]->h;
  286. if(s->mode & 1)
  287. link->frame_rate = av_mul_q(ctx->inputs[0]->frame_rate,
  288. (AVRational){2, 1});
  289. if (link->w < 3 || link->h < 3) {
  290. av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
  291. return AVERROR(EINVAL);
  292. }
  293. s->csp = av_pix_fmt_desc_get(link->format);
  294. s->filter = filter;
  295. if (s->csp->comp[0].depth > 8) {
  296. s->filter_line = filter_line_c_16bit;
  297. s->filter_edges = filter_edges_16bit;
  298. } else {
  299. s->filter_line = filter_line_c;
  300. s->filter_edges = filter_edges;
  301. }
  302. if (ARCH_X86)
  303. ff_yadif_init_x86(s);
  304. return 0;
  305. }
  306. static const AVClass yadif_class = {
  307. .class_name = "yadif",
  308. .item_name = av_default_item_name,
  309. .option = ff_yadif_options,
  310. .version = LIBAVUTIL_VERSION_INT,
  311. .category = AV_CLASS_CATEGORY_FILTER,
  312. };
  313. static const AVFilterPad avfilter_vf_yadif_inputs[] = {
  314. {
  315. .name = "default",
  316. .type = AVMEDIA_TYPE_VIDEO,
  317. .filter_frame = ff_yadif_filter_frame,
  318. },
  319. { NULL }
  320. };
  321. static const AVFilterPad avfilter_vf_yadif_outputs[] = {
  322. {
  323. .name = "default",
  324. .type = AVMEDIA_TYPE_VIDEO,
  325. .request_frame = ff_yadif_request_frame,
  326. .config_props = config_props,
  327. },
  328. { NULL }
  329. };
  330. AVFilter ff_vf_yadif = {
  331. .name = "yadif",
  332. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
  333. .priv_size = sizeof(YADIFContext),
  334. .priv_class = &yadif_class,
  335. .uninit = uninit,
  336. .query_formats = query_formats,
  337. .inputs = avfilter_vf_yadif_inputs,
  338. .outputs = avfilter_vf_yadif_outputs,
  339. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  340. };