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.

297 lines
10KB

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