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.

248 lines
8.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. *
  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 "internal.h"
  34. #include "video.h"
  35. enum ScanMode {
  36. MODE_TFF = 0,
  37. MODE_BFF = 1,
  38. };
  39. enum FieldType {
  40. FIELD_UPPER = 0,
  41. FIELD_LOWER = 1,
  42. };
  43. typedef struct InterlaceContext {
  44. const AVClass *class;
  45. enum ScanMode scan; // top or bottom field first scanning
  46. int lowpass; // enable or disable low pass filterning
  47. AVFrame *cur, *next; // the two frames from which the new one is obtained
  48. } InterlaceContext;
  49. #define OFFSET(x) offsetof(InterlaceContext, x)
  50. #define V AV_OPT_FLAG_VIDEO_PARAM
  51. static const AVOption interlace_options[] = {
  52. { "scan", "scanning mode", OFFSET(scan),
  53. AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
  54. { "tff", "top field first", 0,
  55. AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
  56. { "bff", "bottom field first", 0,
  57. AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
  58. { "lowpass", "enable vertical low-pass filter", OFFSET(lowpass),
  59. AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, .flags = V },
  60. { NULL }
  61. };
  62. AVFILTER_DEFINE_CLASS(interlace);
  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. ff_set_common_formats(ctx, ff_make_format_list(formats_supported));
  72. return 0;
  73. }
  74. static av_cold void uninit(AVFilterContext *ctx)
  75. {
  76. InterlaceContext *s = ctx->priv;
  77. av_frame_free(&s->cur);
  78. av_frame_free(&s->next);
  79. }
  80. static int config_out_props(AVFilterLink *outlink)
  81. {
  82. AVFilterContext *ctx = outlink->src;
  83. AVFilterLink *inlink = outlink->src->inputs[0];
  84. InterlaceContext *s = ctx->priv;
  85. if (inlink->h < 2) {
  86. av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
  87. return AVERROR_INVALIDDATA;
  88. }
  89. if (!s->lowpass)
  90. av_log(ctx, AV_LOG_WARNING, "***warning*** Lowpass filter is disabled, "
  91. "the resulting video will be aliased rather than interlaced.\n");
  92. // same input size
  93. outlink->w = inlink->w;
  94. outlink->h = inlink->h;
  95. outlink->time_base = inlink->time_base;
  96. outlink->frame_rate = inlink->frame_rate;
  97. // half framerate
  98. outlink->frame_rate.den *= 2;
  99. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  100. av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
  101. s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
  102. return 0;
  103. }
  104. static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame,
  105. AVFilterLink *inlink, enum FieldType field_type,
  106. int lowpass)
  107. {
  108. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  109. int vsub = desc->log2_chroma_h;
  110. int plane, i, j;
  111. for (plane = 0; plane < desc->nb_components; plane++) {
  112. int lines = (plane == 1 || plane == 2) ? FF_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
  113. int linesize = av_image_get_linesize(inlink->format, inlink->w, plane);
  114. uint8_t *dstp = dst_frame->data[plane];
  115. const uint8_t *srcp = src_frame->data[plane];
  116. av_assert0(linesize >= 0);
  117. lines = (lines + (field_type == FIELD_UPPER)) / 2;
  118. if (field_type == FIELD_LOWER)
  119. srcp += src_frame->linesize[plane];
  120. if (field_type == FIELD_LOWER)
  121. dstp += dst_frame->linesize[plane];
  122. if (lowpass) {
  123. int srcp_linesize = src_frame->linesize[plane] * 2;
  124. int dstp_linesize = dst_frame->linesize[plane] * 2;
  125. for (j = lines; j > 0; j--) {
  126. const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
  127. const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
  128. if (j == lines)
  129. srcp_above = srcp; // there is no line above
  130. if (j == 1)
  131. srcp_below = srcp; // there is no line below
  132. for (i = 0; i < linesize; i++) {
  133. // this calculation is an integer representation of
  134. // '0.5 * current + 0.25 * above + 0.25 * below'
  135. // '1 +' is for rounding.
  136. dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
  137. }
  138. dstp += dstp_linesize;
  139. srcp += srcp_linesize;
  140. }
  141. } else {
  142. av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
  143. srcp, src_frame->linesize[plane] * 2,
  144. linesize, lines);
  145. }
  146. }
  147. }
  148. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  149. {
  150. AVFilterContext *ctx = inlink->dst;
  151. AVFilterLink *outlink = ctx->outputs[0];
  152. InterlaceContext *s = ctx->priv;
  153. AVFrame *out;
  154. int tff, ret;
  155. av_frame_free(&s->cur);
  156. s->cur = s->next;
  157. s->next = buf;
  158. /* we need at least two frames */
  159. if (!s->cur || !s->next)
  160. return 0;
  161. if (s->cur->interlaced_frame) {
  162. av_log(ctx, AV_LOG_WARNING,
  163. "video is already interlaced, adjusting framerate only\n");
  164. out = av_frame_clone(s->cur);
  165. if (!out)
  166. return AVERROR(ENOMEM);
  167. out->pts /= 2; // adjust pts to new framerate
  168. ret = ff_filter_frame(outlink, out);
  169. return ret;
  170. }
  171. tff = (s->scan == MODE_TFF);
  172. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  173. if (!out)
  174. return AVERROR(ENOMEM);
  175. av_frame_copy_props(out, s->cur);
  176. out->interlaced_frame = 1;
  177. out->top_field_first = tff;
  178. /* copy upper/lower field from cur */
  179. copy_picture_field(s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
  180. av_frame_free(&s->cur);
  181. /* copy lower/upper field from next */
  182. copy_picture_field(s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
  183. av_frame_free(&s->next);
  184. ret = ff_filter_frame(outlink, out);
  185. return ret;
  186. }
  187. static const AVFilterPad inputs[] = {
  188. {
  189. .name = "default",
  190. .type = AVMEDIA_TYPE_VIDEO,
  191. .filter_frame = filter_frame,
  192. },
  193. { NULL }
  194. };
  195. static const AVFilterPad outputs[] = {
  196. {
  197. .name = "default",
  198. .type = AVMEDIA_TYPE_VIDEO,
  199. .config_props = config_out_props,
  200. },
  201. { NULL }
  202. };
  203. AVFilter ff_vf_interlace = {
  204. .name = "interlace",
  205. .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
  206. .uninit = uninit,
  207. .priv_class = &interlace_class,
  208. .priv_size = sizeof(InterlaceContext),
  209. .query_formats = query_formats,
  210. .inputs = inputs,
  211. .outputs = outputs,
  212. };