vf_interlace.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. */
  23. /**
  24. * @file
  25. * progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter
  26. */
  27. #include "libavutil/common.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/avassert.h"
  31. #include "formats.h"
  32. #include "avfilter.h"
  33. #include "interlace.h"
  34. #include "internal.h"
  35. #include "video.h"
  36. #define OFFSET(x) offsetof(InterlaceContext, x)
  37. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  38. static const AVOption interlace_options[] = {
  39. { "scan", "scanning mode", OFFSET(scan),
  40. AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = FLAGS, .unit = "scan" },
  41. { "tff", "top field first", 0,
  42. AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "scan" },
  43. { "bff", "bottom field first", 0,
  44. AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = FLAGS, .unit = "scan" },
  45. { "lowpass", "set vertical low-pass filter", OFFSET(lowpass),
  46. AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, .flags = FLAGS },
  47. { NULL }
  48. };
  49. AVFILTER_DEFINE_CLASS(interlace);
  50. static void lowpass_line_c(uint8_t *dstp, ptrdiff_t linesize,
  51. const uint8_t *srcp,
  52. const uint8_t *srcp_above,
  53. const uint8_t *srcp_below)
  54. {
  55. int i;
  56. for (i = 0; i < linesize; i++) {
  57. // this calculation is an integer representation of
  58. // '0.5 * current + 0.25 * above + 0.25 * below'
  59. // '1 +' is for rounding.
  60. dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
  61. }
  62. }
  63. static const enum AVPixelFormat formats_supported[] = {
  64. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  65. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
  66. AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  67. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
  68. };
  69. static int query_formats(AVFilterContext *ctx)
  70. {
  71. AVFilterFormats *fmts_list = ff_make_format_list(formats_supported);
  72. if (!fmts_list)
  73. return AVERROR(ENOMEM);
  74. return ff_set_common_formats(ctx, fmts_list);
  75. }
  76. static av_cold void uninit(AVFilterContext *ctx)
  77. {
  78. InterlaceContext *s = ctx->priv;
  79. av_frame_free(&s->cur);
  80. av_frame_free(&s->next);
  81. }
  82. static int config_out_props(AVFilterLink *outlink)
  83. {
  84. AVFilterContext *ctx = outlink->src;
  85. AVFilterLink *inlink = outlink->src->inputs[0];
  86. InterlaceContext *s = ctx->priv;
  87. if (inlink->h < 2) {
  88. av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
  89. return AVERROR_INVALIDDATA;
  90. }
  91. if (!s->lowpass)
  92. av_log(ctx, AV_LOG_WARNING, "Lowpass filter is disabled, "
  93. "the resulting video will be aliased rather than interlaced.\n");
  94. // same input size
  95. outlink->w = inlink->w;
  96. outlink->h = inlink->h;
  97. outlink->time_base = inlink->time_base;
  98. outlink->frame_rate = inlink->frame_rate;
  99. // half framerate
  100. outlink->time_base.num *= 2;
  101. outlink->frame_rate.den *= 2;
  102. if (s->lowpass) {
  103. s->lowpass_line = lowpass_line_c;
  104. if (ARCH_X86)
  105. ff_interlace_init_x86(s);
  106. }
  107. av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
  108. s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
  109. return 0;
  110. }
  111. static void copy_picture_field(InterlaceContext *s,
  112. AVFrame *src_frame, AVFrame *dst_frame,
  113. AVFilterLink *inlink, enum FieldType field_type,
  114. int lowpass)
  115. {
  116. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  117. int hsub = desc->log2_chroma_w;
  118. int vsub = desc->log2_chroma_h;
  119. int plane, j;
  120. for (plane = 0; plane < desc->nb_components; plane++) {
  121. int cols = (plane == 1 || plane == 2) ? -(-inlink->w) >> hsub : inlink->w;
  122. int lines = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
  123. uint8_t *dstp = dst_frame->data[plane];
  124. const uint8_t *srcp = src_frame->data[plane];
  125. av_assert0(cols >= 0 || lines >= 0);
  126. lines = (lines + (field_type == FIELD_UPPER)) / 2;
  127. if (field_type == FIELD_LOWER) {
  128. srcp += src_frame->linesize[plane];
  129. dstp += dst_frame->linesize[plane];
  130. }
  131. if (lowpass) {
  132. int srcp_linesize = src_frame->linesize[plane] * 2;
  133. int dstp_linesize = dst_frame->linesize[plane] * 2;
  134. for (j = lines; j > 0; j--) {
  135. const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
  136. const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
  137. if (j == lines)
  138. srcp_above = srcp; // there is no line above
  139. if (j == 1)
  140. srcp_below = srcp; // there is no line below
  141. s->lowpass_line(dstp, cols, srcp, srcp_above, srcp_below);
  142. dstp += dstp_linesize;
  143. srcp += srcp_linesize;
  144. }
  145. } else {
  146. av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
  147. srcp, src_frame->linesize[plane] * 2,
  148. cols, lines);
  149. }
  150. }
  151. }
  152. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  153. {
  154. AVFilterContext *ctx = inlink->dst;
  155. AVFilterLink *outlink = ctx->outputs[0];
  156. InterlaceContext *s = ctx->priv;
  157. AVFrame *out;
  158. int tff, ret;
  159. av_frame_free(&s->cur);
  160. s->cur = s->next;
  161. s->next = buf;
  162. /* we need at least two frames */
  163. if (!s->cur || !s->next)
  164. return 0;
  165. if (s->cur->interlaced_frame) {
  166. av_log(ctx, AV_LOG_WARNING,
  167. "video is already interlaced, adjusting framerate only\n");
  168. out = av_frame_clone(s->cur);
  169. if (!out)
  170. return AVERROR(ENOMEM);
  171. out->pts /= 2; // adjust pts to new framerate
  172. ret = ff_filter_frame(outlink, out);
  173. return ret;
  174. }
  175. tff = (s->scan == MODE_TFF);
  176. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  177. if (!out)
  178. return AVERROR(ENOMEM);
  179. av_frame_copy_props(out, s->cur);
  180. out->interlaced_frame = 1;
  181. out->top_field_first = tff;
  182. out->pts /= 2; // adjust pts to new framerate
  183. /* copy upper/lower field from cur */
  184. copy_picture_field(s, s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
  185. av_frame_free(&s->cur);
  186. /* copy lower/upper field from next */
  187. copy_picture_field(s, s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
  188. av_frame_free(&s->next);
  189. ret = ff_filter_frame(outlink, out);
  190. return ret;
  191. }
  192. static const AVFilterPad inputs[] = {
  193. {
  194. .name = "default",
  195. .type = AVMEDIA_TYPE_VIDEO,
  196. .filter_frame = filter_frame,
  197. },
  198. { NULL }
  199. };
  200. static const AVFilterPad outputs[] = {
  201. {
  202. .name = "default",
  203. .type = AVMEDIA_TYPE_VIDEO,
  204. .config_props = config_out_props,
  205. },
  206. { NULL }
  207. };
  208. AVFilter ff_vf_interlace = {
  209. .name = "interlace",
  210. .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
  211. .uninit = uninit,
  212. .priv_class = &interlace_class,
  213. .priv_size = sizeof(InterlaceContext),
  214. .query_formats = query_formats,
  215. .inputs = inputs,
  216. .outputs = outputs,
  217. };