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.4KB

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