vf_interlace.c 7.8 KB

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