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.

293 lines
10.0KB

  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,
  59. ptrdiff_t mref, ptrdiff_t pref)
  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_complex_c(uint8_t *dstp, ptrdiff_t linesize,
  72. const uint8_t *srcp,
  73. ptrdiff_t mref, ptrdiff_t pref)
  74. {
  75. const uint8_t *srcp_above = srcp + mref;
  76. const uint8_t *srcp_below = srcp + pref;
  77. const uint8_t *srcp_above2 = srcp + mref * 2;
  78. const uint8_t *srcp_below2 = srcp + pref * 2;
  79. int i, srcp_x, srcp_ab;
  80. for (i = 0; i < linesize; i++) {
  81. // this calculation is an integer representation of
  82. // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
  83. // '4 +' is for rounding.
  84. srcp_x = srcp[i] << 1;
  85. srcp_ab = srcp_above[i] + srcp_below[i];
  86. dstp[i] = av_clip_uint8((4 + ((srcp[i] + srcp_x + srcp_ab) << 1)
  87. - srcp_above2[i] - srcp_below2[i]) >> 3);
  88. // Prevent over-sharpening:
  89. // dst must not exceed src when the average of above and below
  90. // is less than src. And the other way around.
  91. if (srcp_ab > srcp_x) {
  92. if (dstp[i] < srcp[i])
  93. dstp[i] = srcp[i];
  94. } else if (dstp[i] > srcp[i])
  95. dstp[i] = srcp[i];
  96. }
  97. }
  98. static const enum AVPixelFormat formats_supported[] = {
  99. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  100. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
  101. AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  102. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
  103. };
  104. static int query_formats(AVFilterContext *ctx)
  105. {
  106. AVFilterFormats *fmts_list = ff_make_format_list(formats_supported);
  107. if (!fmts_list)
  108. return AVERROR(ENOMEM);
  109. return ff_set_common_formats(ctx, fmts_list);
  110. }
  111. static av_cold void uninit(AVFilterContext *ctx)
  112. {
  113. InterlaceContext *s = ctx->priv;
  114. av_frame_free(&s->cur);
  115. av_frame_free(&s->next);
  116. }
  117. static int config_out_props(AVFilterLink *outlink)
  118. {
  119. AVFilterContext *ctx = outlink->src;
  120. AVFilterLink *inlink = outlink->src->inputs[0];
  121. InterlaceContext *s = ctx->priv;
  122. if (inlink->h < 2) {
  123. av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
  124. return AVERROR_INVALIDDATA;
  125. }
  126. if (!s->lowpass)
  127. av_log(ctx, AV_LOG_WARNING, "Lowpass filter is disabled, "
  128. "the resulting video will be aliased rather than interlaced.\n");
  129. // same input size
  130. outlink->w = inlink->w;
  131. outlink->h = inlink->h;
  132. outlink->time_base = inlink->time_base;
  133. outlink->frame_rate = inlink->frame_rate;
  134. // half framerate
  135. outlink->time_base.num *= 2;
  136. outlink->frame_rate.den *= 2;
  137. if (s->lowpass) {
  138. if (s->lowpass == VLPF_LIN)
  139. s->lowpass_line = lowpass_line_c;
  140. else if (s->lowpass == VLPF_CMP)
  141. s->lowpass_line = lowpass_line_complex_c;
  142. if (ARCH_X86)
  143. ff_interlace_init_x86(s);
  144. }
  145. av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
  146. s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
  147. return 0;
  148. }
  149. static void copy_picture_field(InterlaceContext *s,
  150. AVFrame *src_frame, AVFrame *dst_frame,
  151. AVFilterLink *inlink, enum FieldType field_type,
  152. int lowpass)
  153. {
  154. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  155. int hsub = desc->log2_chroma_w;
  156. int vsub = desc->log2_chroma_h;
  157. int plane, j;
  158. for (plane = 0; plane < desc->nb_components; plane++) {
  159. int cols = (plane == 1 || plane == 2) ? -(-inlink->w) >> hsub : inlink->w;
  160. int lines = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
  161. uint8_t *dstp = dst_frame->data[plane];
  162. const uint8_t *srcp = src_frame->data[plane];
  163. int srcp_linesize = src_frame->linesize[plane] * 2;
  164. int dstp_linesize = dst_frame->linesize[plane] * 2;
  165. av_assert0(cols >= 0 || lines >= 0);
  166. lines = (lines + (field_type == FIELD_UPPER)) / 2;
  167. if (field_type == FIELD_LOWER) {
  168. srcp += src_frame->linesize[plane];
  169. dstp += dst_frame->linesize[plane];
  170. }
  171. if (lowpass) {
  172. int x = 0;
  173. if (lowpass == VLPF_CMP)
  174. x = 1;
  175. for (j = lines; j > 0; j--) {
  176. ptrdiff_t pref = src_frame->linesize[plane];
  177. ptrdiff_t mref = -pref;
  178. if (j >= (lines - x))
  179. mref = 0;
  180. else if (j <= (1 + x))
  181. pref = 0;
  182. s->lowpass_line(dstp, cols, srcp, mref, pref);
  183. dstp += dstp_linesize;
  184. srcp += srcp_linesize;
  185. }
  186. } else {
  187. av_image_copy_plane(dstp, dstp_linesize, srcp, srcp_linesize, cols, lines);
  188. }
  189. }
  190. }
  191. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  192. {
  193. AVFilterContext *ctx = inlink->dst;
  194. AVFilterLink *outlink = ctx->outputs[0];
  195. InterlaceContext *s = ctx->priv;
  196. AVFrame *out;
  197. int tff, ret;
  198. av_frame_free(&s->cur);
  199. s->cur = s->next;
  200. s->next = buf;
  201. /* we need at least two frames */
  202. if (!s->cur || !s->next)
  203. return 0;
  204. if (s->cur->interlaced_frame) {
  205. av_log(ctx, AV_LOG_WARNING,
  206. "video is already interlaced, adjusting framerate only\n");
  207. out = av_frame_clone(s->cur);
  208. if (!out)
  209. return AVERROR(ENOMEM);
  210. out->pts /= 2; // adjust pts to new framerate
  211. ret = ff_filter_frame(outlink, out);
  212. return ret;
  213. }
  214. tff = (s->scan == MODE_TFF);
  215. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  216. if (!out)
  217. return AVERROR(ENOMEM);
  218. av_frame_copy_props(out, s->cur);
  219. out->interlaced_frame = 1;
  220. out->top_field_first = tff;
  221. out->pts /= 2; // adjust pts to new framerate
  222. /* copy upper/lower field from cur */
  223. copy_picture_field(s, s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
  224. av_frame_free(&s->cur);
  225. /* copy lower/upper field from next */
  226. copy_picture_field(s, s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
  227. av_frame_free(&s->next);
  228. ret = ff_filter_frame(outlink, out);
  229. return ret;
  230. }
  231. static const AVFilterPad inputs[] = {
  232. {
  233. .name = "default",
  234. .type = AVMEDIA_TYPE_VIDEO,
  235. .filter_frame = filter_frame,
  236. },
  237. { NULL }
  238. };
  239. static const AVFilterPad outputs[] = {
  240. {
  241. .name = "default",
  242. .type = AVMEDIA_TYPE_VIDEO,
  243. .config_props = config_out_props,
  244. },
  245. { NULL }
  246. };
  247. AVFilter ff_vf_interlace = {
  248. .name = "interlace",
  249. .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
  250. .uninit = uninit,
  251. .priv_class = &interlace_class,
  252. .priv_size = sizeof(InterlaceContext),
  253. .query_formats = query_formats,
  254. .inputs = inputs,
  255. .outputs = outputs,
  256. };