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
7.9KB

  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. av_opt_free(s);
  86. }
  87. static int config_out_props(AVFilterLink *outlink)
  88. {
  89. AVFilterContext *ctx = outlink->src;
  90. AVFilterLink *inlink = outlink->src->inputs[0];
  91. InterlaceContext *s = ctx->priv;
  92. if (inlink->h < 2) {
  93. av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
  94. return AVERROR_INVALIDDATA;
  95. }
  96. // same input size
  97. outlink->w = inlink->w;
  98. outlink->h = inlink->h;
  99. outlink->time_base = inlink->time_base;
  100. // half framerate
  101. outlink->time_base.num *= 2;
  102. av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
  103. s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
  104. return 0;
  105. }
  106. static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame,
  107. AVFilterLink *inlink, enum FieldType field_type,
  108. int lowpass)
  109. {
  110. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  111. int vsub = desc->log2_chroma_h;
  112. int plane, i, j;
  113. for (plane = 0; plane < desc->nb_components; plane++) {
  114. int lines = (plane == 1 || plane == 2) ? inlink->h >> vsub : inlink->h;
  115. int linesize = av_image_get_linesize(inlink->format, inlink->w, plane);
  116. uint8_t *dstp = dst_frame->data[plane];
  117. const uint8_t *srcp = src_frame->data[plane];
  118. av_assert0(linesize >= 0);
  119. lines /= 2;
  120. if (field_type == FIELD_LOWER)
  121. srcp += src_frame->linesize[plane];
  122. if (field_type == FIELD_LOWER)
  123. dstp += dst_frame->linesize[plane];
  124. if (lowpass) {
  125. int srcp_linesize = src_frame->linesize[plane] * 2;
  126. int dstp_linesize = dst_frame->linesize[plane] * 2;
  127. for (j = lines; j > 0; j--) {
  128. const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
  129. const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
  130. if (j == lines)
  131. srcp_above = srcp; // there is no line above
  132. if (j == 1)
  133. srcp_below = srcp; // there is no line below
  134. for (i = 0; i < linesize; i++) {
  135. // this calculation is an integer representation of
  136. // '0.5 * current + 0.25 * above + 0.25 + below'
  137. // '1 +' is for rounding.
  138. dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
  139. }
  140. dstp += dstp_linesize;
  141. srcp += srcp_linesize;
  142. }
  143. } else {
  144. av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
  145. srcp, src_frame->linesize[plane] * 2,
  146. linesize, lines);
  147. }
  148. }
  149. }
  150. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  151. {
  152. AVFilterContext *ctx = inlink->dst;
  153. AVFilterLink *outlink = ctx->outputs[0];
  154. InterlaceContext *s = ctx->priv;
  155. AVFrame *out;
  156. int tff, ret;
  157. av_frame_free(&s->cur);
  158. s->cur = s->next;
  159. s->next = buf;
  160. /* we need at least two frames */
  161. if (!s->cur || !s->next)
  162. return 0;
  163. tff = (s->scan == MODE_TFF);
  164. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  165. if (!out)
  166. return AVERROR(ENOMEM);
  167. av_frame_copy_props(out, s->cur);
  168. out->interlaced_frame = 1;
  169. out->top_field_first = tff;
  170. out->pts /= 2; // adjust pts to new framerate
  171. /* copy upper/lower field from cur */
  172. copy_picture_field(s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
  173. av_frame_free(&s->cur);
  174. /* copy lower/upper field from next */
  175. copy_picture_field(s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
  176. av_frame_free(&s->next);
  177. ret = ff_filter_frame(outlink, out);
  178. s->got_output = 1;
  179. return ret;
  180. }
  181. static int request_frame(AVFilterLink *outlink)
  182. {
  183. AVFilterContext *ctx = outlink->src;
  184. InterlaceContext *s = ctx->priv;
  185. int ret = 0;
  186. s->got_output = 0;
  187. while (ret >= 0 && !s->got_output)
  188. ret = ff_request_frame(ctx->inputs[0]);
  189. return ret;
  190. }
  191. static const AVFilterPad inputs[] = {
  192. {
  193. .name = "default",
  194. .type = AVMEDIA_TYPE_VIDEO,
  195. .filter_frame = filter_frame,
  196. },
  197. { NULL }
  198. };
  199. static const AVFilterPad outputs[] = {
  200. {
  201. .name = "default",
  202. .type = AVMEDIA_TYPE_VIDEO,
  203. .config_props = config_out_props,
  204. .request_frame = request_frame,
  205. },
  206. { NULL }
  207. };
  208. AVFilter avfilter_vf_interlace = {
  209. .name = "interlace",
  210. .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
  211. .uninit = uninit,
  212. .priv_class = &class,
  213. .priv_size = sizeof(InterlaceContext),
  214. .query_formats = query_formats,
  215. .inputs = inputs,
  216. .outputs = outputs,
  217. };