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.

367 lines
13KB

  1. /*
  2. * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
  3. * Copyright (c) 2010 Baptiste Coudurier
  4. * Copyright (c) 2011 Stefano Sabatini
  5. * Copyright (c) 2013 Vittorio Giovara <vittorio.giovara@gmail.com>
  6. * Copyright (c) 2017 Thomas Mundt <tmundt75@gmail.com>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. /**
  25. * @file
  26. * progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter
  27. */
  28. #include "libavutil/common.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/avassert.h"
  32. #include "formats.h"
  33. #include "avfilter.h"
  34. #include "interlace.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. #define OFFSET(x) offsetof(InterlaceContext, x)
  38. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  39. static const AVOption interlace_options[] = {
  40. { "scan", "scanning mode", OFFSET(scan),
  41. AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = FLAGS, .unit = "scan" },
  42. { "tff", "top field first", 0,
  43. AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "scan" },
  44. { "bff", "bottom field first", 0,
  45. AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "scan" },
  46. { "lowpass", "set vertical low-pass filter", OFFSET(lowpass),
  47. AV_OPT_TYPE_INT, {.i64 = VLPF_LIN }, 0, 2, .flags = FLAGS, .unit = "lowpass" },
  48. { "off", "disable vertical low-pass filter", 0,
  49. AV_OPT_TYPE_CONST, {.i64 = VLPF_OFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "lowpass" },
  50. { "linear", "linear vertical low-pass filter", 0,
  51. AV_OPT_TYPE_CONST, {.i64 = VLPF_LIN }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "lowpass" },
  52. { "complex", "complex vertical low-pass filter", 0,
  53. AV_OPT_TYPE_CONST, {.i64 = VLPF_CMP }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "lowpass" },
  54. { NULL }
  55. };
  56. AVFILTER_DEFINE_CLASS(interlace);
  57. static void lowpass_line_c(uint8_t *dstp, ptrdiff_t linesize,
  58. const uint8_t *srcp, ptrdiff_t mref,
  59. ptrdiff_t pref, int clip_max)
  60. {
  61. const uint8_t *srcp_above = srcp + mref;
  62. const uint8_t *srcp_below = srcp + pref;
  63. int i;
  64. for (i = 0; i < linesize; i++) {
  65. // this calculation is an integer representation of
  66. // '0.5 * current + 0.25 * above + 0.25 * below'
  67. // '1 +' is for rounding.
  68. dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
  69. }
  70. }
  71. static void lowpass_line_c_16(uint8_t *dst8, ptrdiff_t linesize,
  72. const uint8_t *src8, ptrdiff_t mref,
  73. ptrdiff_t pref, int clip_max)
  74. {
  75. uint16_t *dstp = (uint16_t *)dst8;
  76. const uint16_t *srcp = (const uint16_t *)src8;
  77. const uint16_t *srcp_above = srcp + mref / 2;
  78. const uint16_t *srcp_below = srcp + pref / 2;
  79. int i, src_x;
  80. for (i = 0; i < linesize; i++) {
  81. // this calculation is an integer representation of
  82. // '0.5 * current + 0.25 * above + 0.25 * below'
  83. // '1 +' is for rounding.
  84. src_x = av_le2ne16(srcp[i]) << 1;
  85. dstp[i] = av_le2ne16((1 + src_x + av_le2ne16(srcp_above[i])
  86. + av_le2ne16(srcp_below[i])) >> 2);
  87. }
  88. }
  89. static void lowpass_line_complex_c(uint8_t *dstp, ptrdiff_t linesize,
  90. const uint8_t *srcp, ptrdiff_t mref,
  91. ptrdiff_t pref, int clip_max)
  92. {
  93. const uint8_t *srcp_above = srcp + mref;
  94. const uint8_t *srcp_below = srcp + pref;
  95. const uint8_t *srcp_above2 = srcp + mref * 2;
  96. const uint8_t *srcp_below2 = srcp + pref * 2;
  97. int i, src_x, src_ab;
  98. for (i = 0; i < linesize; i++) {
  99. // this calculation is an integer representation of
  100. // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
  101. // '4 +' is for rounding.
  102. src_x = srcp[i] << 1;
  103. src_ab = srcp_above[i] + srcp_below[i];
  104. dstp[i] = av_clip_uint8((4 + ((srcp[i] + src_x + src_ab) << 1)
  105. - srcp_above2[i] - srcp_below2[i]) >> 3);
  106. // Prevent over-sharpening:
  107. // dst must not exceed src when the average of above and below
  108. // is less than src. And the other way around.
  109. if (src_ab > src_x) {
  110. if (dstp[i] < srcp[i])
  111. dstp[i] = srcp[i];
  112. } else if (dstp[i] > srcp[i])
  113. dstp[i] = srcp[i];
  114. }
  115. }
  116. static void lowpass_line_complex_c_16(uint8_t *dst8, ptrdiff_t linesize,
  117. const uint8_t *src8, ptrdiff_t mref,
  118. ptrdiff_t pref, int clip_max)
  119. {
  120. uint16_t *dstp = (uint16_t *)dst8;
  121. const uint16_t *srcp = (const uint16_t *)src8;
  122. const uint16_t *srcp_above = srcp + mref / 2;
  123. const uint16_t *srcp_below = srcp + pref / 2;
  124. const uint16_t *srcp_above2 = srcp + mref;
  125. const uint16_t *srcp_below2 = srcp + pref;
  126. int i, dst_le, src_le, src_x, src_ab;
  127. for (i = 0; i < linesize; i++) {
  128. // this calculation is an integer representation of
  129. // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
  130. // '4 +' is for rounding.
  131. src_le = av_le2ne16(srcp[i]);
  132. src_x = src_le << 1;
  133. src_ab = av_le2ne16(srcp_above[i]) + av_le2ne16(srcp_below[i]);
  134. dst_le = av_clip((4 + ((src_le + src_x + src_ab) << 1)
  135. - av_le2ne16(srcp_above2[i])
  136. - av_le2ne16(srcp_below2[i])) >> 3, 0, clip_max);
  137. // Prevent over-sharpening:
  138. // dst must not exceed src when the average of above and below
  139. // is less than src. And the other way around.
  140. if (src_ab > src_x) {
  141. if (dst_le < src_le)
  142. dstp[i] = av_le2ne16(src_le);
  143. else
  144. dstp[i] = av_le2ne16(dst_le);
  145. } else if (dst_le > src_le) {
  146. dstp[i] = av_le2ne16(src_le);
  147. } else
  148. dstp[i] = av_le2ne16(dst_le);
  149. }
  150. }
  151. static const enum AVPixelFormat formats_supported[] = {
  152. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  153. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  154. AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUV444P10LE,
  155. AV_PIX_FMT_YUV420P12LE, AV_PIX_FMT_YUV422P12LE, AV_PIX_FMT_YUV444P12LE,
  156. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  157. AV_PIX_FMT_YUVA420P10LE, AV_PIX_FMT_YUVA422P10LE, AV_PIX_FMT_YUVA444P10LE,
  158. AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  159. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
  160. };
  161. static int query_formats(AVFilterContext *ctx)
  162. {
  163. AVFilterFormats *fmts_list = ff_make_format_list(formats_supported);
  164. if (!fmts_list)
  165. return AVERROR(ENOMEM);
  166. return ff_set_common_formats(ctx, fmts_list);
  167. }
  168. static av_cold void uninit(AVFilterContext *ctx)
  169. {
  170. InterlaceContext *s = ctx->priv;
  171. av_frame_free(&s->cur);
  172. av_frame_free(&s->next);
  173. }
  174. void ff_interlace_init(InterlaceContext *s, int depth)
  175. {
  176. if (s->lowpass) {
  177. if (s->lowpass == VLPF_LIN) {
  178. if (depth > 8)
  179. s->lowpass_line = lowpass_line_c_16;
  180. else
  181. s->lowpass_line = lowpass_line_c;
  182. } else if (s->lowpass == VLPF_CMP) {
  183. if (depth > 8)
  184. s->lowpass_line = lowpass_line_complex_c_16;
  185. else
  186. s->lowpass_line = lowpass_line_complex_c;
  187. }
  188. if (ARCH_X86)
  189. ff_interlace_init_x86(s, depth);
  190. }
  191. }
  192. static int config_out_props(AVFilterLink *outlink)
  193. {
  194. AVFilterContext *ctx = outlink->src;
  195. AVFilterLink *inlink = outlink->src->inputs[0];
  196. InterlaceContext *s = ctx->priv;
  197. if (inlink->h < 2) {
  198. av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
  199. return AVERROR_INVALIDDATA;
  200. }
  201. if (!s->lowpass)
  202. av_log(ctx, AV_LOG_WARNING, "Lowpass filter is disabled, "
  203. "the resulting video will be aliased rather than interlaced.\n");
  204. // same input size
  205. outlink->w = inlink->w;
  206. outlink->h = inlink->h;
  207. outlink->time_base = inlink->time_base;
  208. outlink->frame_rate = inlink->frame_rate;
  209. // half framerate
  210. outlink->time_base.num *= 2;
  211. outlink->frame_rate.den *= 2;
  212. s->csp = av_pix_fmt_desc_get(outlink->format);
  213. ff_interlace_init(s, s->csp->comp[0].depth);
  214. av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
  215. s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
  216. return 0;
  217. }
  218. static void copy_picture_field(InterlaceContext *s,
  219. AVFrame *src_frame, AVFrame *dst_frame,
  220. AVFilterLink *inlink, enum FieldType field_type,
  221. int lowpass)
  222. {
  223. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  224. int hsub = desc->log2_chroma_w;
  225. int vsub = desc->log2_chroma_h;
  226. int plane, j;
  227. for (plane = 0; plane < desc->nb_components; plane++) {
  228. int cols = (plane == 1 || plane == 2) ? -(-inlink->w) >> hsub : inlink->w;
  229. int lines = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
  230. uint8_t *dstp = dst_frame->data[plane];
  231. const uint8_t *srcp = src_frame->data[plane];
  232. int srcp_linesize = src_frame->linesize[plane] * 2;
  233. int dstp_linesize = dst_frame->linesize[plane] * 2;
  234. int clip_max = (1 << s->csp->comp[plane].depth) - 1;
  235. av_assert0(cols >= 0 || lines >= 0);
  236. lines = (lines + (field_type == FIELD_UPPER)) / 2;
  237. if (field_type == FIELD_LOWER) {
  238. srcp += src_frame->linesize[plane];
  239. dstp += dst_frame->linesize[plane];
  240. }
  241. if (lowpass) {
  242. int x = 0;
  243. if (lowpass == VLPF_CMP)
  244. x = 1;
  245. for (j = lines; j > 0; j--) {
  246. ptrdiff_t pref = src_frame->linesize[plane];
  247. ptrdiff_t mref = -pref;
  248. if (j >= (lines - x))
  249. mref = 0;
  250. else if (j <= (1 + x))
  251. pref = 0;
  252. s->lowpass_line(dstp, cols, srcp, mref, pref, clip_max);
  253. dstp += dstp_linesize;
  254. srcp += srcp_linesize;
  255. }
  256. } else {
  257. if (s->csp->comp[plane].depth > 8)
  258. cols *= 2;
  259. av_image_copy_plane(dstp, dstp_linesize, srcp, srcp_linesize, cols, lines);
  260. }
  261. }
  262. }
  263. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  264. {
  265. AVFilterContext *ctx = inlink->dst;
  266. AVFilterLink *outlink = ctx->outputs[0];
  267. InterlaceContext *s = ctx->priv;
  268. AVFrame *out;
  269. int tff, ret;
  270. av_frame_free(&s->cur);
  271. s->cur = s->next;
  272. s->next = buf;
  273. /* we need at least two frames */
  274. if (!s->cur || !s->next)
  275. return 0;
  276. if (s->cur->interlaced_frame) {
  277. av_log(ctx, AV_LOG_WARNING,
  278. "video is already interlaced, adjusting framerate only\n");
  279. out = av_frame_clone(s->cur);
  280. if (!out)
  281. return AVERROR(ENOMEM);
  282. out->pts /= 2; // adjust pts to new framerate
  283. ret = ff_filter_frame(outlink, out);
  284. return ret;
  285. }
  286. tff = (s->scan == MODE_TFF);
  287. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  288. if (!out)
  289. return AVERROR(ENOMEM);
  290. av_frame_copy_props(out, s->cur);
  291. out->interlaced_frame = 1;
  292. out->top_field_first = tff;
  293. out->pts /= 2; // adjust pts to new framerate
  294. /* copy upper/lower field from cur */
  295. copy_picture_field(s, s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
  296. av_frame_free(&s->cur);
  297. /* copy lower/upper field from next */
  298. copy_picture_field(s, s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
  299. av_frame_free(&s->next);
  300. ret = ff_filter_frame(outlink, out);
  301. return ret;
  302. }
  303. static const AVFilterPad inputs[] = {
  304. {
  305. .name = "default",
  306. .type = AVMEDIA_TYPE_VIDEO,
  307. .filter_frame = filter_frame,
  308. },
  309. { NULL }
  310. };
  311. static const AVFilterPad outputs[] = {
  312. {
  313. .name = "default",
  314. .type = AVMEDIA_TYPE_VIDEO,
  315. .config_props = config_out_props,
  316. },
  317. { NULL }
  318. };
  319. AVFilter ff_vf_interlace = {
  320. .name = "interlace",
  321. .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
  322. .uninit = uninit,
  323. .priv_class = &interlace_class,
  324. .priv_size = sizeof(InterlaceContext),
  325. .query_formats = query_formats,
  326. .inputs = inputs,
  327. .outputs = outputs,
  328. };