vf_interlace.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with Libav; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. /**
  19. * @file
  20. * progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter
  21. */
  22. #include "libavutil/common.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/avassert.h"
  26. #include "formats.h"
  27. #include "avfilter.h"
  28. #include "interlace.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. #define OFFSET(x) offsetof(InterlaceContext, x)
  32. #define V AV_OPT_FLAG_VIDEO_PARAM
  33. static const AVOption options[] = {
  34. { "scan", "scanning mode", OFFSET(scan),
  35. AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
  36. { "tff", "top field first", 0,
  37. AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
  38. { "bff", "bottom field first", 0,
  39. AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
  40. { "lowpass", "enable vertical low-pass filter", OFFSET(lowpass),
  41. AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, .flags = V },
  42. { NULL }
  43. };
  44. static const AVClass class = {
  45. .class_name = "interlace filter",
  46. .item_name = av_default_item_name,
  47. .option = options,
  48. .version = LIBAVUTIL_VERSION_INT,
  49. };
  50. static void lowpass_line_c(uint8_t *dstp, ptrdiff_t linesize,
  51. const uint8_t *srcp,
  52. const uint8_t *srcp_above,
  53. const uint8_t *srcp_below)
  54. {
  55. int i;
  56. for (i = 0; i < linesize; i++) {
  57. // this calculation is an integer representation of
  58. // '0.5 * current + 0.25 * above + 0.25 * below'
  59. // '1 +' is for rounding.
  60. dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
  61. }
  62. }
  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. av_opt_free(s);
  80. }
  81. static int config_out_props(AVFilterLink *outlink)
  82. {
  83. AVFilterContext *ctx = outlink->src;
  84. AVFilterLink *inlink = outlink->src->inputs[0];
  85. InterlaceContext *s = ctx->priv;
  86. if (inlink->h < 2) {
  87. av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
  88. return AVERROR_INVALIDDATA;
  89. }
  90. if (!s->lowpass)
  91. av_log(ctx, AV_LOG_WARNING, "Lowpass filter is disabled, "
  92. "the resulting video will be aliased rather than interlaced.\n");
  93. // same input size
  94. outlink->w = inlink->w;
  95. outlink->h = inlink->h;
  96. outlink->time_base = inlink->time_base;
  97. outlink->frame_rate = inlink->frame_rate;
  98. // half framerate
  99. outlink->time_base.num *= 2;
  100. outlink->frame_rate.den *= 2;
  101. if (s->lowpass) {
  102. s->lowpass_line = lowpass_line_c;
  103. if (ARCH_X86)
  104. ff_interlace_init_x86(s);
  105. }
  106. av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
  107. s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
  108. return 0;
  109. }
  110. static void copy_picture_field(InterlaceContext *s,
  111. AVFrame *src_frame, AVFrame *dst_frame,
  112. AVFilterLink *inlink, enum FieldType field_type,
  113. int lowpass)
  114. {
  115. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  116. int hsub = desc->log2_chroma_w;
  117. int vsub = desc->log2_chroma_h;
  118. int plane, j;
  119. for (plane = 0; plane < desc->nb_components; plane++) {
  120. int cols = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->w, hsub)
  121. : inlink->w;
  122. int lines = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, vsub)
  123. : inlink->h;
  124. uint8_t *dstp = dst_frame->data[plane];
  125. const uint8_t *srcp = src_frame->data[plane];
  126. av_assert0(cols >= 0 || lines >= 0);
  127. lines = (lines + (field_type == FIELD_UPPER)) / 2;
  128. if (field_type == FIELD_LOWER) {
  129. srcp += src_frame->linesize[plane];
  130. dstp += dst_frame->linesize[plane];
  131. }
  132. if (lowpass) {
  133. int srcp_linesize = src_frame->linesize[plane] * 2;
  134. int dstp_linesize = dst_frame->linesize[plane] * 2;
  135. for (j = lines; j > 0; j--) {
  136. const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
  137. const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
  138. if (j == lines)
  139. srcp_above = srcp; // there is no line above
  140. if (j == 1)
  141. srcp_below = srcp; // there is no line below
  142. s->lowpass_line(dstp, cols, srcp, srcp_above, srcp_below);
  143. dstp += dstp_linesize;
  144. srcp += srcp_linesize;
  145. }
  146. } else {
  147. av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
  148. srcp, src_frame->linesize[plane] * 2,
  149. cols, lines);
  150. }
  151. }
  152. }
  153. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  154. {
  155. AVFilterContext *ctx = inlink->dst;
  156. AVFilterLink *outlink = ctx->outputs[0];
  157. InterlaceContext *s = ctx->priv;
  158. AVFrame *out;
  159. int tff, ret;
  160. av_frame_free(&s->cur);
  161. s->cur = s->next;
  162. s->next = buf;
  163. /* we need at least two frames */
  164. if (!s->cur || !s->next)
  165. return 0;
  166. if (s->cur->interlaced_frame) {
  167. av_log(ctx, AV_LOG_WARNING,
  168. "video is already interlaced, adjusting framerate only\n");
  169. out = av_frame_clone(s->cur);
  170. if (!out)
  171. return AVERROR(ENOMEM);
  172. out->pts /= 2; // adjust pts to new framerate
  173. ret = ff_filter_frame(outlink, out);
  174. s->got_output = 1;
  175. return ret;
  176. }
  177. tff = (s->scan == MODE_TFF);
  178. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  179. if (!out)
  180. return AVERROR(ENOMEM);
  181. av_frame_copy_props(out, s->cur);
  182. out->interlaced_frame = 1;
  183. out->top_field_first = tff;
  184. out->pts /= 2; // adjust pts to new framerate
  185. /* copy upper/lower field from cur */
  186. copy_picture_field(s, s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
  187. av_frame_free(&s->cur);
  188. /* copy lower/upper field from next */
  189. copy_picture_field(s, s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
  190. av_frame_free(&s->next);
  191. ret = ff_filter_frame(outlink, out);
  192. s->got_output = 1;
  193. return ret;
  194. }
  195. static int request_frame(AVFilterLink *outlink)
  196. {
  197. AVFilterContext *ctx = outlink->src;
  198. InterlaceContext *s = ctx->priv;
  199. int ret = 0;
  200. s->got_output = 0;
  201. while (ret >= 0 && !s->got_output)
  202. ret = ff_request_frame(ctx->inputs[0]);
  203. return ret;
  204. }
  205. static const AVFilterPad inputs[] = {
  206. {
  207. .name = "default",
  208. .type = AVMEDIA_TYPE_VIDEO,
  209. .filter_frame = filter_frame,
  210. },
  211. { NULL }
  212. };
  213. static const AVFilterPad outputs[] = {
  214. {
  215. .name = "default",
  216. .type = AVMEDIA_TYPE_VIDEO,
  217. .config_props = config_out_props,
  218. .request_frame = request_frame,
  219. },
  220. { NULL }
  221. };
  222. AVFilter ff_vf_interlace = {
  223. .name = "interlace",
  224. .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
  225. .uninit = uninit,
  226. .priv_class = &class,
  227. .priv_size = sizeof(InterlaceContext),
  228. .query_formats = query_formats,
  229. .inputs = inputs,
  230. .outputs = outputs,
  231. };