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.

504 lines
21KB

  1. /*
  2. * Copyright (c) 2021 Paul B Mahol
  3. *
  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/common.h"
  21. #include "libavutil/imgutils.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. typedef struct ESTDIFContext {
  29. const AVClass *class;
  30. int mode; ///< 0 is frame, 1 is field
  31. int parity; ///< frame field parity
  32. int deint; ///< which frames to deinterlace
  33. int rslope; ///< best edge slope search radius
  34. int redge; ///< best edge match search radius
  35. int linesize[4]; ///< bytes of pixel data per line for each plane
  36. int planewidth[4]; ///< width of each plane
  37. int planeheight[4]; ///< height of each plane
  38. int field; ///< which field are we on, 0 or 1
  39. int eof;
  40. int depth;
  41. int half;
  42. int nb_planes;
  43. int nb_threads;
  44. int64_t pts;
  45. AVFrame *prev;
  46. void (*interpolate)(uint8_t *dst,
  47. const uint8_t *prev_line, const uint8_t *next_line,
  48. const uint8_t *prev2_line, const uint8_t *next2_line,
  49. int x, int width, int rslope, int redge, unsigned half,
  50. int depth, int *K);
  51. } ESTDIFContext;
  52. #define MAX_R 15
  53. #define S (MAX_R * 2 + 1)
  54. #define OFFSET(x) offsetof(ESTDIFContext, x)
  55. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  56. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
  57. static const AVOption estdif_options[] = {
  58. { "mode", "specify the mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "mode" },
  59. CONST("frame", "send one frame for each frame", 0, "mode"),
  60. CONST("field", "send one frame for each field", 1, "mode"),
  61. { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, FLAGS, "parity" },
  62. CONST("tff", "assume top field first", 0, "parity"),
  63. CONST("bff", "assume bottom field first", 1, "parity"),
  64. CONST("auto", "auto detect parity", -1, "parity"),
  65. { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "deint" },
  66. CONST("all", "deinterlace all frames", 0, "deint"),
  67. CONST("interlaced", "only deinterlace frames marked as interlaced", 1, "deint"),
  68. { "rslope", "specify the search radius for edge slope tracing", OFFSET(rslope), AV_OPT_TYPE_INT, {.i64=1}, 1, MAX_R, FLAGS, },
  69. { "redge", "specify the search radius for best edge matching", OFFSET(redge), AV_OPT_TYPE_INT, {.i64=2}, 0, MAX_R, FLAGS, },
  70. { NULL }
  71. };
  72. AVFILTER_DEFINE_CLASS(estdif);
  73. static int query_formats(AVFilterContext *ctx)
  74. {
  75. static const enum AVPixelFormat pix_fmts[] = {
  76. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  77. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  78. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  79. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  80. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  81. AV_PIX_FMT_YUVJ411P,
  82. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  83. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  84. AV_PIX_FMT_GRAY8,
  85. AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  86. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  87. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  88. AV_PIX_FMT_YUV440P10,
  89. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
  90. AV_PIX_FMT_YUV440P12,
  91. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  92. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  93. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  94. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
  95. AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
  96. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
  97. AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  98. AV_PIX_FMT_NONE
  99. };
  100. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  101. if (!fmts_list)
  102. return AVERROR(ENOMEM);
  103. return ff_set_common_formats(ctx, fmts_list);
  104. }
  105. static int config_output(AVFilterLink *outlink)
  106. {
  107. AVFilterContext *ctx = outlink->src;
  108. AVFilterLink *inlink = ctx->inputs[0];
  109. outlink->time_base.num = inlink->time_base.num;
  110. outlink->time_base.den = inlink->time_base.den * 2;
  111. outlink->frame_rate.num = inlink->frame_rate.num * 2;
  112. outlink->frame_rate.den = inlink->frame_rate.den;
  113. return 0;
  114. }
  115. typedef struct ThreadData {
  116. AVFrame *out, *in;
  117. } ThreadData;
  118. #define MIDL(type, ss) \
  119. static unsigned midl_##ss(const type *const prev, \
  120. const type *const next, \
  121. int end, int x, int k) \
  122. { \
  123. return (prev[av_clip(x + k, 0, end)] + \
  124. next[av_clip(x - k, 0, end)] + 1) >> 1; \
  125. }
  126. MIDL(uint8_t, 8)
  127. MIDL(uint16_t, 16)
  128. #define MID4(type, ss) \
  129. static unsigned mid4_##ss(const type *const prev, \
  130. const type *const next, \
  131. const type *const prev2, \
  132. const type *const next2, \
  133. int end, int x, int k, int depth) \
  134. { \
  135. return av_clip_uintp2_c(( \
  136. 9 * (prev[av_clip(x + k, 0, end)] + \
  137. next[av_clip(x - k, 0, end)]) - \
  138. 1 * (prev2[av_clip(x + k*3, 0, end)] + \
  139. next2[av_clip(x - k*3, 0, end)]) + 8) >> 4, \
  140. depth); \
  141. }
  142. MID4(uint8_t, 8)
  143. MID4(uint16_t, 16)
  144. #define DIFF(type, ss) \
  145. static unsigned diff_##ss(const type *const prev, \
  146. const type *const next, \
  147. int end, int x, int k, int j) \
  148. { \
  149. return FFABS(prev[av_clip(x + k + j, 0, end)] - \
  150. next[av_clip(x - k + j, 0, end)]); \
  151. }
  152. DIFF(uint8_t, 8)
  153. DIFF(uint16_t, 16)
  154. #define COST(type, ss) \
  155. static unsigned cost_##ss(const type *const prev, \
  156. const type *const next, \
  157. int end, int x, int k) \
  158. { \
  159. const int m = midl_##ss(prev, next, end, x, k); \
  160. const int p = prev[x]; \
  161. const int n = next[x]; \
  162. \
  163. return FFABS(p - m) + FFABS(n - m); \
  164. }
  165. COST(uint8_t, 8)
  166. COST(uint16_t, 16)
  167. #define INTERPOLATE(type, atype, max, ss) \
  168. static void interpolate_##ss(uint8_t *ddst, \
  169. const uint8_t *const pprev_line, \
  170. const uint8_t *const nnext_line, \
  171. const uint8_t *const pprev2_line, \
  172. const uint8_t *const nnext2_line, \
  173. int x, int width, int rslope, \
  174. int redge, unsigned h, int depth, \
  175. int *K) \
  176. { \
  177. type *dst = (type *)ddst; \
  178. const type *const prev_line = (const type *const)pprev_line; \
  179. const type *const prev2_line = (const type *const)pprev2_line; \
  180. const type *const next_line = (const type *const)nnext_line; \
  181. const type *const next2_line = (const type *const)nnext2_line; \
  182. const int end = width - 1; \
  183. const atype f = redge + 2; \
  184. atype sd[S], sD[S], di = 0; \
  185. atype dmin = max; \
  186. int k = *K; \
  187. \
  188. for (int i = -rslope; i <= rslope && abs(k) > rslope; i++) { \
  189. atype sum = 0; \
  190. \
  191. for (int j = -redge; j <= redge; j++) { \
  192. sum += diff_##ss(prev_line, next_line, end, x, i, j); \
  193. sum += diff_##ss(prev2_line, prev_line, end, x, i, j); \
  194. sum += diff_##ss(next_line, next2_line, end, x, i, j); \
  195. } \
  196. \
  197. sD[i + rslope] = sum; \
  198. sD[i + rslope] += f * cost_##ss(prev_line, next_line, end, x, i); \
  199. sD[i + rslope] += h * abs(i); \
  200. \
  201. dmin = FFMIN(sD[i + rslope], dmin); \
  202. } \
  203. \
  204. for (int i = -rslope; i <= rslope; i++) { \
  205. atype sum = 0; \
  206. \
  207. for (int j = -redge; j <= redge; j++) { \
  208. sum += diff_##ss(prev_line, next_line, end, x, k + i, j); \
  209. sum += diff_##ss(prev2_line, prev_line, end, x, k + i, j); \
  210. sum += diff_##ss(next_line, next2_line, end, x, k + i, j); \
  211. } \
  212. \
  213. sd[i + rslope] = sum; \
  214. sd[i + rslope] += f * cost_##ss(prev_line, next_line, end, x, k + i); \
  215. sd[i + rslope] += h * abs(k + i); \
  216. \
  217. dmin = FFMIN(sd[i + rslope], dmin); \
  218. } \
  219. \
  220. for (int i = -rslope; i <= rslope && abs(k) > rslope; i++) { \
  221. if (dmin == sD[i + rslope]) { \
  222. di = 1; \
  223. k = i; \
  224. break; \
  225. } \
  226. } \
  227. \
  228. for (int i = -rslope; i <= rslope && !di; i++) { \
  229. if (dmin == sd[i + rslope]) { \
  230. k += i; \
  231. break; \
  232. } \
  233. } \
  234. \
  235. dst[x] = mid4_##ss(prev_line, next_line, prev2_line, next2_line, \
  236. end, x, k, depth); \
  237. \
  238. *K = k; \
  239. }
  240. INTERPOLATE(uint8_t, unsigned, UINT_MAX, 8)
  241. INTERPOLATE(uint16_t, uint64_t, UINT64_MAX, 16)
  242. static int deinterlace_slice(AVFilterContext *ctx, void *arg,
  243. int jobnr, int nb_jobs)
  244. {
  245. ESTDIFContext *s = ctx->priv;
  246. ThreadData *td = arg;
  247. AVFrame *out = td->out;
  248. AVFrame *in = td->in;
  249. const int rslope = s->rslope;
  250. const int redge = s->redge;
  251. const int half = s->half;
  252. const int depth = s->depth;
  253. const int interlaced = in->interlaced_frame;
  254. const int tff = (s->field == (s->parity == -1 ? interlaced ? in->top_field_first : 1 :
  255. s->parity ^ 1));
  256. for (int plane = 0; plane < s->nb_planes; plane++) {
  257. const uint8_t *src_data = in->data[plane];
  258. uint8_t *dst_data = out->data[plane];
  259. const int linesize = s->linesize[plane];
  260. const int width = s->planewidth[plane];
  261. const int height = s->planeheight[plane];
  262. const int src_linesize = in->linesize[plane];
  263. const int dst_linesize = out->linesize[plane];
  264. const int start = (height * jobnr) / nb_jobs;
  265. const int end = (height * (jobnr+1)) / nb_jobs;
  266. const uint8_t *prev_line, *prev2_line, *next_line, *next2_line, *in_line;
  267. uint8_t *out_line;
  268. int y_out;
  269. y_out = start + (tff ^ (start & 1));
  270. in_line = src_data + (y_out * src_linesize);
  271. out_line = dst_data + (y_out * dst_linesize);
  272. while (y_out < end) {
  273. memcpy(out_line, in_line, linesize);
  274. y_out += 2;
  275. in_line += src_linesize * 2;
  276. out_line += dst_linesize * 2;
  277. }
  278. y_out = start + ((!tff) ^ (start & 1));
  279. out_line = dst_data + (y_out * dst_linesize);
  280. for (int y = y_out; y < end; y += 2) {
  281. int y_prev2_in = y - 3;
  282. int y_next2_in = y + 3;
  283. int y_prev_in = y - 1;
  284. int y_next_in = y + 1;
  285. int k;
  286. while (y_prev2_in < 0)
  287. y_prev2_in += 2;
  288. while (y_next2_in >= height)
  289. y_next2_in -= 2;
  290. while (y_prev_in < 0)
  291. y_prev_in += 2;
  292. while (y_next_in >= height)
  293. y_next_in -= 2;
  294. prev2_line = src_data + (y_prev2_in * src_linesize);
  295. next2_line = src_data + (y_next2_in * src_linesize);
  296. prev_line = src_data + (y_prev_in * src_linesize);
  297. next_line = src_data + (y_next_in * src_linesize);
  298. k = 0;
  299. for (int x = 0; x < width; x++) {
  300. s->interpolate(out_line,
  301. prev_line, next_line,
  302. prev2_line, next2_line,
  303. x, width, rslope, redge, half, depth, &k);
  304. }
  305. out_line += 2 * dst_linesize;
  306. }
  307. }
  308. return 0;
  309. }
  310. static int filter(AVFilterContext *ctx, int is_second, AVFrame *in)
  311. {
  312. ESTDIFContext *s = ctx->priv;
  313. AVFilterLink *outlink = ctx->outputs[0];
  314. AVFrame *out;
  315. ThreadData td;
  316. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  317. if (!out)
  318. return AVERROR(ENOMEM);
  319. av_frame_copy_props(out, in);
  320. out->interlaced_frame = 0;
  321. out->pts = s->pts;
  322. td.out = out; td.in = in;
  323. ctx->internal->execute(ctx, deinterlace_slice, &td, NULL,
  324. FFMIN(s->planeheight[1] / 2, s->nb_threads));
  325. if (s->mode)
  326. s->field = !s->field;
  327. return ff_filter_frame(outlink, out);
  328. }
  329. static int config_input(AVFilterLink *inlink)
  330. {
  331. AVFilterContext *ctx = inlink->dst;
  332. ESTDIFContext *s = ctx->priv;
  333. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  334. int ret;
  335. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  336. return ret;
  337. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  338. s->planeheight[0] = s->planeheight[3] = inlink->h;
  339. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  340. s->planewidth[0] = s->planewidth[3] = inlink->w;
  341. if (inlink->h < 3) {
  342. av_log(ctx, AV_LOG_ERROR, "Video of less than 3 lines is not supported\n");
  343. return AVERROR(EINVAL);
  344. }
  345. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  346. s->nb_threads = ff_filter_get_nb_threads(ctx);
  347. s->depth = desc->comp[0].depth;
  348. s->interpolate = s->depth <= 8 ? interpolate_8 : interpolate_16;
  349. s->half = 1 << (s->depth - 1);
  350. return 0;
  351. }
  352. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  353. {
  354. AVFilterContext *ctx = inlink->dst;
  355. ESTDIFContext *s = ctx->priv;
  356. int ret;
  357. if (!s->prev) {
  358. s->prev = in;
  359. return 0;
  360. }
  361. if ((s->deint && !in->interlaced_frame) || ctx->is_disabled) {
  362. s->prev->pts *= 2;
  363. ret = ff_filter_frame(ctx->outputs[0], s->prev);
  364. s->prev = in;
  365. return ret;
  366. }
  367. s->pts = s->prev->pts * 2;
  368. ret = filter(ctx, 0, s->prev);
  369. if (ret < 0 || s->mode == 0) {
  370. av_frame_free(&s->prev);
  371. s->prev = in;
  372. return ret;
  373. }
  374. s->pts = s->prev->pts + in->pts;
  375. ret = filter(ctx, 1, s->prev);
  376. av_frame_free(&s->prev);
  377. s->prev = in;
  378. return ret;
  379. }
  380. static int request_frame(AVFilterLink *link)
  381. {
  382. AVFilterContext *ctx = link->src;
  383. ESTDIFContext *s = ctx->priv;
  384. int ret;
  385. if (s->eof)
  386. return AVERROR_EOF;
  387. ret = ff_request_frame(ctx->inputs[0]);
  388. if (ret == AVERROR_EOF && s->prev) {
  389. AVFrame *next = av_frame_clone(s->prev);
  390. if (!next)
  391. return AVERROR(ENOMEM);
  392. next->pts = s->prev->pts + av_rescale_q(1, av_inv_q(ctx->outputs[0]->frame_rate),
  393. ctx->outputs[0]->time_base);
  394. s->eof = 1;
  395. ret = filter_frame(ctx->inputs[0], next);
  396. } else if (ret < 0) {
  397. return ret;
  398. }
  399. return ret;
  400. }
  401. static av_cold void uninit(AVFilterContext *ctx)
  402. {
  403. ESTDIFContext *s = ctx->priv;
  404. av_frame_free(&s->prev);
  405. }
  406. static const AVFilterPad estdif_inputs[] = {
  407. {
  408. .name = "default",
  409. .type = AVMEDIA_TYPE_VIDEO,
  410. .filter_frame = filter_frame,
  411. .config_props = config_input,
  412. },
  413. { NULL }
  414. };
  415. static const AVFilterPad estdif_outputs[] = {
  416. {
  417. .name = "default",
  418. .type = AVMEDIA_TYPE_VIDEO,
  419. .config_props = config_output,
  420. .request_frame = request_frame,
  421. },
  422. { NULL }
  423. };
  424. AVFilter ff_vf_estdif = {
  425. .name = "estdif",
  426. .description = NULL_IF_CONFIG_SMALL("Apply Edge Slope Tracing deinterlace."),
  427. .priv_size = sizeof(ESTDIFContext),
  428. .priv_class = &estdif_class,
  429. .uninit = uninit,
  430. .query_formats = query_formats,
  431. .inputs = estdif_inputs,
  432. .outputs = estdif_outputs,
  433. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  434. .process_command = ff_filter_process_command,
  435. };