vf_yadif.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with FFmpeg; if not, write to the Free Software
  17. * Foundation, Inc., 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/opt.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. /* Only edge pixels need to be processed here. A constant value of false
  116. * for is_not_edge should let the compiler ignore the whole branch. */
  117. FILTER(0, 3, 0)
  118. dst = (uint8_t*)dst1 + w - (MAX_ALIGN-1);
  119. prev = (uint8_t*)prev1 + w - (MAX_ALIGN-1);
  120. cur = (uint8_t*)cur1 + w - (MAX_ALIGN-1);
  121. next = (uint8_t*)next1 + w - (MAX_ALIGN-1);
  122. prev2 = (uint8_t*)(parity ? prev : cur);
  123. next2 = (uint8_t*)(parity ? cur : next);
  124. FILTER(w - (MAX_ALIGN-1), w - 3, 1)
  125. FILTER(w - 3, w, 0)
  126. }
  127. static void filter_line_c_16bit(void *dst1,
  128. void *prev1, void *cur1, void *next1,
  129. int w, int prefs, int mrefs, int parity,
  130. int mode)
  131. {
  132. uint16_t *dst = dst1;
  133. uint16_t *prev = prev1;
  134. uint16_t *cur = cur1;
  135. uint16_t *next = next1;
  136. int x;
  137. uint16_t *prev2 = parity ? prev : cur ;
  138. uint16_t *next2 = parity ? cur : next;
  139. mrefs /= 2;
  140. prefs /= 2;
  141. FILTER(0, w, 1)
  142. }
  143. static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
  144. int w, int prefs, int mrefs, int parity, int mode)
  145. {
  146. uint16_t *dst = dst1;
  147. uint16_t *prev = prev1;
  148. uint16_t *cur = cur1;
  149. uint16_t *next = next1;
  150. int x;
  151. uint16_t *prev2 = parity ? prev : cur ;
  152. uint16_t *next2 = parity ? cur : next;
  153. mrefs /= 2;
  154. prefs /= 2;
  155. FILTER(0, 3, 0)
  156. dst = (uint16_t*)dst1 + w - (MAX_ALIGN/2-1);
  157. prev = (uint16_t*)prev1 + w - (MAX_ALIGN/2-1);
  158. cur = (uint16_t*)cur1 + w - (MAX_ALIGN/2-1);
  159. next = (uint16_t*)next1 + w - (MAX_ALIGN/2-1);
  160. prev2 = (uint16_t*)(parity ? prev : cur);
  161. next2 = (uint16_t*)(parity ? cur : next);
  162. FILTER(w - (MAX_ALIGN/2-1), w - 3, 1)
  163. FILTER(w - 3, w, 0)
  164. }
  165. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  166. {
  167. YADIFContext *s = ctx->priv;
  168. ThreadData *td = arg;
  169. int refs = s->cur->linesize[td->plane];
  170. int df = (s->csp->comp[td->plane].depth_minus1 + 8) / 8;
  171. int pix_3 = 3 * df;
  172. int slice_start = (td->h * jobnr ) / nb_jobs;
  173. int slice_end = (td->h * (jobnr+1)) / nb_jobs;
  174. int y;
  175. /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
  176. * we need to call the c variant which avoids this for border pixels
  177. */
  178. for (y = slice_start; y < slice_end; y++) {
  179. if ((y ^ td->parity) & 1) {
  180. uint8_t *prev = &s->prev->data[td->plane][y * refs];
  181. uint8_t *cur = &s->cur ->data[td->plane][y * refs];
  182. uint8_t *next = &s->next->data[td->plane][y * refs];
  183. uint8_t *dst = &td->frame->data[td->plane][y * td->frame->linesize[td->plane]];
  184. int mode = y == 1 || y + 2 == td->h ? 2 : s->mode;
  185. s->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
  186. next + pix_3, td->w - (3 + MAX_ALIGN/df-1),
  187. y + 1 < td->h ? refs : -refs,
  188. y ? -refs : refs,
  189. td->parity ^ td->tff, mode);
  190. s->filter_edges(dst, prev, cur, next, td->w,
  191. y + 1 < td->h ? refs : -refs,
  192. y ? -refs : refs,
  193. td->parity ^ td->tff, mode);
  194. } else {
  195. memcpy(&td->frame->data[td->plane][y * td->frame->linesize[td->plane]],
  196. &s->cur->data[td->plane][y * refs], td->w * df);
  197. }
  198. }
  199. return 0;
  200. }
  201. static void filter(AVFilterContext *ctx, AVFrame *dstpic,
  202. int parity, int tff)
  203. {
  204. YADIFContext *yadif = ctx->priv;
  205. ThreadData td = { .frame = dstpic, .parity = parity, .tff = tff };
  206. int i;
  207. for (i = 0; i < yadif->csp->nb_components; i++) {
  208. int w = dstpic->width;
  209. int h = dstpic->height;
  210. if (i == 1 || i == 2) {
  211. w = FF_CEIL_RSHIFT(w, yadif->csp->log2_chroma_w);
  212. h = FF_CEIL_RSHIFT(h, yadif->csp->log2_chroma_h);
  213. }
  214. td.w = w;
  215. td.h = h;
  216. td.plane = i;
  217. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ctx->graph->nb_threads));
  218. }
  219. emms_c();
  220. }
  221. static int return_frame(AVFilterContext *ctx, int is_second)
  222. {
  223. YADIFContext *yadif = ctx->priv;
  224. AVFilterLink *link = ctx->outputs[0];
  225. int tff, ret;
  226. if (yadif->parity == -1) {
  227. tff = yadif->cur->interlaced_frame ?
  228. yadif->cur->top_field_first : 1;
  229. } else {
  230. tff = yadif->parity ^ 1;
  231. }
  232. if (is_second) {
  233. yadif->out = ff_get_video_buffer(link, link->w, link->h);
  234. if (!yadif->out)
  235. return AVERROR(ENOMEM);
  236. av_frame_copy_props(yadif->out, yadif->cur);
  237. yadif->out->interlaced_frame = 0;
  238. }
  239. filter(ctx, yadif->out, tff ^ !is_second, tff);
  240. if (is_second) {
  241. int64_t cur_pts = yadif->cur->pts;
  242. int64_t next_pts = yadif->next->pts;
  243. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  244. yadif->out->pts = cur_pts + next_pts;
  245. } else {
  246. yadif->out->pts = AV_NOPTS_VALUE;
  247. }
  248. }
  249. ret = ff_filter_frame(ctx->outputs[0], yadif->out);
  250. yadif->frame_pending = (yadif->mode&1) && !is_second;
  251. return ret;
  252. }
  253. static int checkstride(YADIFContext *yadif, const AVFrame *a, const AVFrame *b)
  254. {
  255. int i;
  256. for (i = 0; i < yadif->csp->nb_components; i++)
  257. if (a->linesize[i] != b->linesize[i])
  258. return 1;
  259. return 0;
  260. }
  261. static void fixstride(AVFilterLink *link, AVFrame *f)
  262. {
  263. AVFrame *dst = ff_default_get_video_buffer(link, f->width, f->height);
  264. if(!dst)
  265. return;
  266. av_frame_copy_props(dst, f);
  267. av_image_copy(dst->data, dst->linesize,
  268. (const uint8_t **)f->data, f->linesize,
  269. dst->format, dst->width, dst->height);
  270. av_frame_unref(f);
  271. av_frame_move_ref(f, dst);
  272. av_frame_free(&dst);
  273. }
  274. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  275. {
  276. AVFilterContext *ctx = link->dst;
  277. YADIFContext *yadif = ctx->priv;
  278. av_assert0(frame);
  279. if (yadif->frame_pending)
  280. return_frame(ctx, 1);
  281. if (yadif->prev)
  282. av_frame_free(&yadif->prev);
  283. yadif->prev = yadif->cur;
  284. yadif->cur = yadif->next;
  285. yadif->next = frame;
  286. if (!yadif->cur)
  287. return 0;
  288. if (checkstride(yadif, yadif->next, yadif->cur)) {
  289. av_log(ctx, AV_LOG_VERBOSE, "Reallocating frame due to differing stride\n");
  290. fixstride(link, yadif->next);
  291. }
  292. if (checkstride(yadif, yadif->next, yadif->cur))
  293. fixstride(link, yadif->cur);
  294. if (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))
  295. fixstride(link, yadif->prev);
  296. if (checkstride(yadif, yadif->next, yadif->cur) || (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))) {
  297. av_log(ctx, AV_LOG_ERROR, "Failed to reallocate frame\n");
  298. return -1;
  299. }
  300. if ((yadif->deint && !yadif->cur->interlaced_frame) || ctx->is_disabled) {
  301. yadif->out = av_frame_clone(yadif->cur);
  302. if (!yadif->out)
  303. return AVERROR(ENOMEM);
  304. av_frame_free(&yadif->prev);
  305. if (yadif->out->pts != AV_NOPTS_VALUE)
  306. yadif->out->pts *= 2;
  307. return ff_filter_frame(ctx->outputs[0], yadif->out);
  308. }
  309. if (!yadif->prev &&
  310. !(yadif->prev = av_frame_clone(yadif->cur)))
  311. return AVERROR(ENOMEM);
  312. yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
  313. if (!yadif->out)
  314. return AVERROR(ENOMEM);
  315. av_frame_copy_props(yadif->out, yadif->cur);
  316. yadif->out->interlaced_frame = 0;
  317. if (yadif->out->pts != AV_NOPTS_VALUE)
  318. yadif->out->pts *= 2;
  319. return return_frame(ctx, 0);
  320. }
  321. static int request_frame(AVFilterLink *link)
  322. {
  323. AVFilterContext *ctx = link->src;
  324. YADIFContext *yadif = ctx->priv;
  325. if (yadif->frame_pending) {
  326. return_frame(ctx, 1);
  327. return 0;
  328. }
  329. do {
  330. int ret;
  331. if (yadif->eof)
  332. return AVERROR_EOF;
  333. ret = ff_request_frame(link->src->inputs[0]);
  334. if (ret == AVERROR_EOF && yadif->cur) {
  335. AVFrame *next = av_frame_clone(yadif->next);
  336. if (!next)
  337. return AVERROR(ENOMEM);
  338. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  339. filter_frame(link->src->inputs[0], next);
  340. yadif->eof = 1;
  341. } else if (ret < 0) {
  342. return ret;
  343. }
  344. } while (!yadif->cur);
  345. return 0;
  346. }
  347. static av_cold void uninit(AVFilterContext *ctx)
  348. {
  349. YADIFContext *yadif = ctx->priv;
  350. av_frame_free(&yadif->prev);
  351. av_frame_free(&yadif->cur );
  352. av_frame_free(&yadif->next);
  353. }
  354. static int query_formats(AVFilterContext *ctx)
  355. {
  356. static const enum AVPixelFormat pix_fmts[] = {
  357. AV_PIX_FMT_YUV420P,
  358. AV_PIX_FMT_YUV422P,
  359. AV_PIX_FMT_YUV444P,
  360. AV_PIX_FMT_YUV410P,
  361. AV_PIX_FMT_YUV411P,
  362. AV_PIX_FMT_GRAY8,
  363. AV_PIX_FMT_YUVJ420P,
  364. AV_PIX_FMT_YUVJ422P,
  365. AV_PIX_FMT_YUVJ444P,
  366. AV_PIX_FMT_GRAY16,
  367. AV_PIX_FMT_YUV440P,
  368. AV_PIX_FMT_YUVJ440P,
  369. AV_PIX_FMT_YUV420P9,
  370. AV_PIX_FMT_YUV422P9,
  371. AV_PIX_FMT_YUV444P9,
  372. AV_PIX_FMT_YUV420P10,
  373. AV_PIX_FMT_YUV422P10,
  374. AV_PIX_FMT_YUV444P10,
  375. AV_PIX_FMT_YUV420P12,
  376. AV_PIX_FMT_YUV422P12,
  377. AV_PIX_FMT_YUV444P12,
  378. AV_PIX_FMT_YUV420P14,
  379. AV_PIX_FMT_YUV422P14,
  380. AV_PIX_FMT_YUV444P14,
  381. AV_PIX_FMT_YUV420P16,
  382. AV_PIX_FMT_YUV422P16,
  383. AV_PIX_FMT_YUV444P16,
  384. AV_PIX_FMT_YUVA420P,
  385. AV_PIX_FMT_YUVA422P,
  386. AV_PIX_FMT_YUVA444P,
  387. AV_PIX_FMT_GBRP,
  388. AV_PIX_FMT_GBRAP,
  389. AV_PIX_FMT_NONE
  390. };
  391. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  392. return 0;
  393. }
  394. static int config_props(AVFilterLink *link)
  395. {
  396. AVFilterContext *ctx = link->src;
  397. YADIFContext *s = link->src->priv;
  398. link->time_base.num = link->src->inputs[0]->time_base.num;
  399. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  400. link->w = link->src->inputs[0]->w;
  401. link->h = link->src->inputs[0]->h;
  402. if(s->mode&1)
  403. link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
  404. if (link->w < 3 || link->h < 3) {
  405. av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
  406. return AVERROR(EINVAL);
  407. }
  408. s->csp = av_pix_fmt_desc_get(link->format);
  409. if (s->csp->comp[0].depth_minus1 / 8 == 1) {
  410. s->filter_line = filter_line_c_16bit;
  411. s->filter_edges = filter_edges_16bit;
  412. } else {
  413. s->filter_line = filter_line_c;
  414. s->filter_edges = filter_edges;
  415. }
  416. if (ARCH_X86)
  417. ff_yadif_init_x86(s);
  418. return 0;
  419. }
  420. #define OFFSET(x) offsetof(YADIFContext, x)
  421. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  422. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
  423. static const AVOption yadif_options[] = {
  424. { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
  425. CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
  426. CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
  427. CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
  428. CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
  429. { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
  430. CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
  431. CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
  432. CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
  433. { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
  434. CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
  435. CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
  436. { NULL }
  437. };
  438. AVFILTER_DEFINE_CLASS(yadif);
  439. static const AVFilterPad avfilter_vf_yadif_inputs[] = {
  440. {
  441. .name = "default",
  442. .type = AVMEDIA_TYPE_VIDEO,
  443. .filter_frame = filter_frame,
  444. },
  445. { NULL }
  446. };
  447. static const AVFilterPad avfilter_vf_yadif_outputs[] = {
  448. {
  449. .name = "default",
  450. .type = AVMEDIA_TYPE_VIDEO,
  451. .request_frame = request_frame,
  452. .config_props = config_props,
  453. },
  454. { NULL }
  455. };
  456. AVFilter ff_vf_yadif = {
  457. .name = "yadif",
  458. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
  459. .priv_size = sizeof(YADIFContext),
  460. .priv_class = &yadif_class,
  461. .uninit = uninit,
  462. .query_formats = query_formats,
  463. .inputs = avfilter_vf_yadif_inputs,
  464. .outputs = avfilter_vf_yadif_outputs,
  465. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  466. };