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.

258 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 {
  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. int got_output; // signal an output frame is reday to request_frame()
  49. } InterlaceContext;
  50. #define OFFSET(x) offsetof(InterlaceContext, x)
  51. #define V AV_OPT_FLAG_VIDEO_PARAM
  52. static const AVOption options[] = {
  53. { "scan", "scanning mode", OFFSET(scan),
  54. AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
  55. { "tff", "top field first", 0,
  56. AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
  57. { "bff", "bottom field first", 0,
  58. AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
  59. { "lowpass", "enable vertical low-pass filter", OFFSET(lowpass),
  60. AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, .flags = V },
  61. { NULL }
  62. };
  63. static const AVClass class = {
  64. .class_name = "interlace filter",
  65. .item_name = av_default_item_name,
  66. .option = options,
  67. .version = LIBAVUTIL_VERSION_INT,
  68. };
  69. static const enum AVPixelFormat formats_supported[] = {
  70. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  71. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
  72. AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  73. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
  74. };
  75. static int query_formats(AVFilterContext *ctx)
  76. {
  77. ff_set_common_formats(ctx, ff_make_format_list(formats_supported));
  78. return 0;
  79. }
  80. static av_cold void uninit(AVFilterContext *ctx)
  81. {
  82. InterlaceContext *s = ctx->priv;
  83. av_frame_free(&s->cur);
  84. av_frame_free(&s->next);
  85. }
  86. static int config_out_props(AVFilterLink *outlink)
  87. {
  88. AVFilterContext *ctx = outlink->src;
  89. AVFilterLink *inlink = outlink->src->inputs[0];
  90. InterlaceContext *s = ctx->priv;
  91. if (inlink->h < 2) {
  92. av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
  93. return AVERROR_INVALIDDATA;
  94. }
  95. // same input size
  96. outlink->w = inlink->w;
  97. outlink->h = inlink->h;
  98. outlink->time_base = inlink->time_base;
  99. outlink->frame_rate = inlink->frame_rate;
  100. // half framerate
  101. outlink->time_base.num *= 2;
  102. outlink->frame_rate.den *= 2;
  103. av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
  104. s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
  105. return 0;
  106. }
  107. static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame,
  108. AVFilterLink *inlink, enum FieldType field_type,
  109. int lowpass)
  110. {
  111. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  112. int vsub = desc->log2_chroma_h;
  113. int plane, i, j;
  114. for (plane = 0; plane < desc->nb_components; plane++) {
  115. int lines = (plane == 1 || plane == 2) ? FF_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
  116. int linesize = av_image_get_linesize(inlink->format, inlink->w, plane);
  117. uint8_t *dstp = dst_frame->data[plane];
  118. const uint8_t *srcp = src_frame->data[plane];
  119. av_assert0(linesize >= 0);
  120. lines /= 2;
  121. if (field_type == FIELD_LOWER)
  122. srcp += src_frame->linesize[plane];
  123. if (field_type == FIELD_LOWER)
  124. dstp += dst_frame->linesize[plane];
  125. if (lowpass) {
  126. int srcp_linesize = src_frame->linesize[plane] * 2;
  127. int dstp_linesize = dst_frame->linesize[plane] * 2;
  128. for (j = lines; j > 0; j--) {
  129. const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
  130. const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
  131. if (j == lines)
  132. srcp_above = srcp; // there is no line above
  133. if (j == 1)
  134. srcp_below = srcp; // there is no line below
  135. for (i = 0; i < linesize; i++) {
  136. // this calculation is an integer representation of
  137. // '0.5 * current + 0.25 * above + 0.25 + below'
  138. // '1 +' is for rounding.
  139. dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
  140. }
  141. dstp += dstp_linesize;
  142. srcp += srcp_linesize;
  143. }
  144. } else {
  145. av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
  146. srcp, src_frame->linesize[plane] * 2,
  147. linesize, lines);
  148. }
  149. }
  150. }
  151. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  152. {
  153. AVFilterContext *ctx = inlink->dst;
  154. AVFilterLink *outlink = ctx->outputs[0];
  155. InterlaceContext *s = ctx->priv;
  156. AVFrame *out;
  157. int tff, ret;
  158. av_frame_free(&s->cur);
  159. s->cur = s->next;
  160. s->next = buf;
  161. /* we need at least two frames */
  162. if (!s->cur || !s->next)
  163. return 0;
  164. tff = (s->scan == MODE_TFF);
  165. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  166. if (!out)
  167. return AVERROR(ENOMEM);
  168. av_frame_copy_props(out, s->cur);
  169. out->interlaced_frame = 1;
  170. out->top_field_first = tff;
  171. out->pts /= 2; // adjust pts to new framerate
  172. /* copy upper/lower field from cur */
  173. copy_picture_field(s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
  174. av_frame_free(&s->cur);
  175. /* copy lower/upper field from next */
  176. copy_picture_field(s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
  177. av_frame_free(&s->next);
  178. ret = ff_filter_frame(outlink, out);
  179. s->got_output = 1;
  180. return ret;
  181. }
  182. static int request_frame(AVFilterLink *outlink)
  183. {
  184. AVFilterContext *ctx = outlink->src;
  185. InterlaceContext *s = ctx->priv;
  186. int ret = 0;
  187. s->got_output = 0;
  188. while (ret >= 0 && !s->got_output)
  189. ret = ff_request_frame(ctx->inputs[0]);
  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. .request_frame = request_frame,
  206. },
  207. { NULL }
  208. };
  209. AVFilter avfilter_vf_interlace = {
  210. .name = "interlace",
  211. .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
  212. .uninit = uninit,
  213. .priv_class = &class,
  214. .priv_size = sizeof(InterlaceContext),
  215. .query_formats = query_formats,
  216. .inputs = inputs,
  217. .outputs = outputs,
  218. };