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.

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