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.

271 lines
8.0KB

  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 "version.h"
  30. #include "video.h"
  31. enum ScanMode {
  32. MODE_TFF = 0,
  33. MODE_BFF = 1,
  34. };
  35. enum FieldType {
  36. FIELD_UPPER = 0,
  37. FIELD_LOWER = 1,
  38. };
  39. typedef struct InterlaceContext {
  40. const AVClass *class;
  41. enum ScanMode scan; // top or bottom field first scanning
  42. #if FF_API_INTERLACE_LOWPASS_SET
  43. int lowpass; // enable or disable low pass filterning
  44. #endif
  45. AVFrame *cur, *next; // the two frames from which the new one is obtained
  46. int got_output; // signal an output frame is reday to request_frame()
  47. } InterlaceContext;
  48. #define OFFSET(x) offsetof(InterlaceContext, x)
  49. #define V AV_OPT_FLAG_VIDEO_PARAM
  50. static const AVOption options[] = {
  51. { "scan", "scanning mode", OFFSET(scan),
  52. AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
  53. { "tff", "top field first", 0,
  54. AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
  55. { "bff", "bottom field first", 0,
  56. AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
  57. #if FF_API_INTERLACE_LOWPASS_SET
  58. { "lowpass", "(deprecated, this option is always set)", OFFSET(lowpass),
  59. AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, .flags = V },
  60. #endif
  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 FF_API_INTERLACE_LOWPASS_SET
  93. if (!s->lowpass)
  94. av_log(ctx, AV_LOG_WARNING, "This option is deprecated and always set.\n");
  95. #endif
  96. if (inlink->h < 2) {
  97. av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
  98. return AVERROR_INVALIDDATA;
  99. }
  100. // same input size
  101. outlink->w = inlink->w;
  102. outlink->h = inlink->h;
  103. outlink->time_base = inlink->time_base;
  104. // half framerate
  105. outlink->time_base.num *= 2;
  106. av_log(ctx, AV_LOG_VERBOSE, "%s interlacing\n",
  107. s->scan == MODE_TFF ? "tff" : "bff");
  108. return 0;
  109. }
  110. static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame,
  111. AVFilterLink *inlink, enum FieldType field_type)
  112. {
  113. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  114. int vsub = desc->log2_chroma_h;
  115. int plane, i, j;
  116. for (plane = 0; plane < desc->nb_components; plane++) {
  117. int lines = (plane == 1 || plane == 2) ? -(-inlink->h) >> vsub : inlink->h;
  118. int linesize = av_image_get_linesize(inlink->format, inlink->w, plane);
  119. uint8_t *dstp = dst_frame->data[plane];
  120. const uint8_t *srcp = src_frame->data[plane];
  121. int srcp_linesize;
  122. int dstp_linesize;
  123. av_assert0(linesize >= 0);
  124. lines = (lines + (field_type == FIELD_UPPER)) / 2;
  125. if (field_type == FIELD_LOWER)
  126. srcp += src_frame->linesize[plane];
  127. if (field_type == FIELD_LOWER)
  128. dstp += dst_frame->linesize[plane];
  129. srcp_linesize = src_frame->linesize[plane] * 2;
  130. 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. for (i = 0; i < linesize; i++) {
  139. // this calculation is an integer representation of
  140. // '0.5 * current + 0.25 * above + 0.25 * below'
  141. // '1 +' is for rounding.
  142. dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
  143. }
  144. dstp += dstp_linesize;
  145. srcp += srcp_linesize;
  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->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. 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. };