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.

270 lines
8.3KB

  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. // half framerate
  98. outlink->time_base.num *= 2;
  99. if (s->lowpass) {
  100. s->lowpass_line = lowpass_line_c;
  101. if (ARCH_X86)
  102. ff_interlace_init_x86(s);
  103. }
  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(InterlaceContext *s,
  109. AVFrame *src_frame, AVFrame *dst_frame,
  110. AVFilterLink *inlink, enum FieldType field_type,
  111. int lowpass)
  112. {
  113. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  114. int hsub = desc->log2_chroma_w;
  115. int vsub = desc->log2_chroma_h;
  116. int plane, j;
  117. for (plane = 0; plane < desc->nb_components; plane++) {
  118. int cols = (plane == 1 || plane == 2) ? -(-inlink->w) >> hsub : inlink->w;
  119. int lines = (plane == 1 || plane == 2) ? -(-inlink->h) >> vsub : inlink->h;
  120. uint8_t *dstp = dst_frame->data[plane];
  121. const uint8_t *srcp = src_frame->data[plane];
  122. av_assert0(cols >= 0 || lines >= 0);
  123. lines = (lines + (field_type == FIELD_UPPER)) / 2;
  124. if (field_type == FIELD_LOWER) {
  125. srcp += src_frame->linesize[plane];
  126. dstp += dst_frame->linesize[plane];
  127. }
  128. if (lowpass) {
  129. int srcp_linesize = src_frame->linesize[plane] * 2;
  130. int dstp_linesize = dst_frame->linesize[plane] * 2;
  131. for (j = lines; j > 0; j--) {
  132. const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
  133. const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
  134. if (j == lines)
  135. srcp_above = srcp; // there is no line above
  136. if (j == 1)
  137. srcp_below = srcp; // there is no line below
  138. s->lowpass_line(dstp, cols, srcp, srcp_above, srcp_below);
  139. dstp += dstp_linesize;
  140. srcp += srcp_linesize;
  141. }
  142. } else {
  143. av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
  144. srcp, src_frame->linesize[plane] * 2,
  145. cols, lines);
  146. }
  147. }
  148. }
  149. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  150. {
  151. AVFilterContext *ctx = inlink->dst;
  152. AVFilterLink *outlink = ctx->outputs[0];
  153. InterlaceContext *s = ctx->priv;
  154. AVFrame *out;
  155. int tff, ret;
  156. av_frame_free(&s->cur);
  157. s->cur = s->next;
  158. s->next = buf;
  159. /* we need at least two frames */
  160. if (!s->cur || !s->next)
  161. return 0;
  162. if (s->cur->interlaced_frame) {
  163. av_log(ctx, AV_LOG_WARNING,
  164. "video is already interlaced, adjusting framerate only\n");
  165. out = av_frame_clone(s->cur);
  166. if (!out)
  167. return AVERROR(ENOMEM);
  168. out->pts /= 2; // adjust pts to new framerate
  169. ret = ff_filter_frame(outlink, out);
  170. s->got_output = 1;
  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, s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
  183. av_frame_free(&s->cur);
  184. /* copy lower/upper field from next */
  185. copy_picture_field(s, s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
  186. av_frame_free(&s->next);
  187. ret = ff_filter_frame(outlink, out);
  188. s->got_output = 1;
  189. return ret;
  190. }
  191. static int request_frame(AVFilterLink *outlink)
  192. {
  193. AVFilterContext *ctx = outlink->src;
  194. InterlaceContext *s = ctx->priv;
  195. int ret = 0;
  196. s->got_output = 0;
  197. while (ret >= 0 && !s->got_output)
  198. ret = ff_request_frame(ctx->inputs[0]);
  199. return ret;
  200. }
  201. static const AVFilterPad inputs[] = {
  202. {
  203. .name = "default",
  204. .type = AVMEDIA_TYPE_VIDEO,
  205. .filter_frame = filter_frame,
  206. },
  207. { NULL }
  208. };
  209. static const AVFilterPad outputs[] = {
  210. {
  211. .name = "default",
  212. .type = AVMEDIA_TYPE_VIDEO,
  213. .config_props = config_out_props,
  214. .request_frame = request_frame,
  215. },
  216. { NULL }
  217. };
  218. AVFilter ff_vf_interlace = {
  219. .name = "interlace",
  220. .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
  221. .uninit = uninit,
  222. .priv_class = &class,
  223. .priv_size = sizeof(InterlaceContext),
  224. .query_formats = query_formats,
  225. .inputs = inputs,
  226. .outputs = outputs,
  227. };