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.

383 lines
16KB

  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  3. * Copyright (c) 2010 Baptiste Coudurier
  4. * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with FFmpeg if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. /**
  23. * @file
  24. * temporal field interlace filter, ported from MPlayer/libmpcodecs
  25. */
  26. #include "libavutil/opt.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/avassert.h"
  29. #include "avfilter.h"
  30. #include "internal.h"
  31. enum TInterlaceMode {
  32. MODE_MERGE = 0,
  33. MODE_DROP_EVEN,
  34. MODE_DROP_ODD,
  35. MODE_PAD,
  36. MODE_INTERLEAVE_TOP,
  37. MODE_INTERLEAVE_BOTTOM,
  38. MODE_INTERLACEX2,
  39. MODE_NB,
  40. };
  41. typedef struct {
  42. const AVClass *class;
  43. enum TInterlaceMode mode; ///< interlace mode selected
  44. int flags; ///< flags affecting interlacing algorithm
  45. int frame; ///< number of the output frame
  46. int vsub; ///< chroma vertical subsampling
  47. AVFrame *cur;
  48. AVFrame *next;
  49. uint8_t *black_data[4]; ///< buffer used to fill padded lines
  50. int black_linesize[4];
  51. } TInterlaceContext;
  52. #define OFFSET(x) offsetof(TInterlaceContext, x)
  53. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  54. #define TINTERLACE_FLAG_VLPF 01
  55. static const AVOption tinterlace_options[] = {
  56. {"mode", "select interlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_MERGE}, 0, MODE_NB-1, FLAGS, "mode"},
  57. {"merge", "merge fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGE}, INT_MIN, INT_MAX, FLAGS, "mode"},
  58. {"drop_even", "drop even fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_EVEN}, INT_MIN, INT_MAX, FLAGS, "mode"},
  59. {"drop_odd", "drop odd fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_ODD}, INT_MIN, INT_MAX, FLAGS, "mode"},
  60. {"pad", "pad alternate lines with black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PAD}, INT_MIN, INT_MAX, FLAGS, "mode"},
  61. {"interleave_top", "interleave top and bottom fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_TOP}, INT_MIN, INT_MAX, FLAGS, "mode"},
  62. {"interleave_bottom", "interleave bottom and top fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "mode"},
  63. {"interlacex2", "interlace fields from two consecutive frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLACEX2}, INT_MIN, INT_MAX, FLAGS, "mode"},
  64. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
  65. {"low_pass_filter", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
  66. {"vlpf", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
  67. {NULL}
  68. };
  69. AVFILTER_DEFINE_CLASS(tinterlace);
  70. #define FULL_SCALE_YUVJ_FORMATS \
  71. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
  72. static enum AVPixelFormat full_scale_yuvj_pix_fmts[] = {
  73. FULL_SCALE_YUVJ_FORMATS, AV_PIX_FMT_NONE
  74. };
  75. static int query_formats(AVFilterContext *ctx)
  76. {
  77. static const enum AVPixelFormat pix_fmts[] = {
  78. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  79. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
  80. AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
  81. AV_PIX_FMT_NONE
  82. };
  83. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  84. return 0;
  85. }
  86. static av_cold void uninit(AVFilterContext *ctx)
  87. {
  88. TInterlaceContext *tinterlace = ctx->priv;
  89. av_frame_free(&tinterlace->cur );
  90. av_frame_free(&tinterlace->next);
  91. av_freep(&tinterlace->black_data[0]);
  92. }
  93. static int config_out_props(AVFilterLink *outlink)
  94. {
  95. AVFilterContext *ctx = outlink->src;
  96. AVFilterLink *inlink = outlink->src->inputs[0];
  97. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
  98. TInterlaceContext *tinterlace = ctx->priv;
  99. tinterlace->vsub = desc->log2_chroma_h;
  100. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  101. outlink->w = inlink->w;
  102. outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD ?
  103. inlink->h*2 : inlink->h;
  104. if (tinterlace->mode == MODE_PAD) {
  105. uint8_t black[4] = { 16, 128, 128, 16 };
  106. int i, ret;
  107. if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
  108. black[0] = black[3] = 0;
  109. ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
  110. outlink->w, outlink->h, outlink->format, 1);
  111. if (ret < 0)
  112. return ret;
  113. /* fill black picture with black */
  114. for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
  115. int h = i == 1 || i == 2 ? FF_CEIL_RSHIFT(outlink->h, desc->log2_chroma_h) : outlink->h;
  116. memset(tinterlace->black_data[i], black[i],
  117. tinterlace->black_linesize[i] * h);
  118. }
  119. }
  120. if ((tinterlace->flags & TINTERLACE_FLAG_VLPF)
  121. && !(tinterlace->mode == MODE_INTERLEAVE_TOP
  122. || tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) {
  123. av_log(ctx, AV_LOG_WARNING, "low_pass_filter flag ignored with mode %d\n",
  124. tinterlace->mode);
  125. tinterlace->flags &= ~TINTERLACE_FLAG_VLPF;
  126. }
  127. av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n",
  128. tinterlace->mode, (tinterlace->flags & TINTERLACE_FLAG_VLPF) ? "on" : "off",
  129. inlink->h, outlink->h);
  130. return 0;
  131. }
  132. #define FIELD_UPPER 0
  133. #define FIELD_LOWER 1
  134. #define FIELD_UPPER_AND_LOWER 2
  135. /**
  136. * Copy picture field from src to dst.
  137. *
  138. * @param src_field copy from upper, lower field or both
  139. * @param interleave leave a padding line between each copied line
  140. * @param dst_field copy to upper or lower field,
  141. * only meaningful when interleave is selected
  142. * @param flags context flags
  143. */
  144. static inline
  145. void copy_picture_field(uint8_t *dst[4], int dst_linesize[4],
  146. const uint8_t *src[4], int src_linesize[4],
  147. enum AVPixelFormat format, int w, int src_h,
  148. int src_field, int interleave, int dst_field,
  149. int flags)
  150. {
  151. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
  152. int plane, vsub = desc->log2_chroma_h;
  153. int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
  154. int h, i;
  155. for (plane = 0; plane < desc->nb_components; plane++) {
  156. int lines = plane == 1 || plane == 2 ? FF_CEIL_RSHIFT(src_h, vsub) : src_h;
  157. int linesize = av_image_get_linesize(format, w, plane);
  158. uint8_t *dstp = dst[plane];
  159. const uint8_t *srcp = src[plane];
  160. if (linesize < 0)
  161. return;
  162. lines = (lines + (src_field == FIELD_UPPER)) / k;
  163. if (src_field == FIELD_LOWER)
  164. srcp += src_linesize[plane];
  165. if (interleave && dst_field == FIELD_LOWER)
  166. dstp += dst_linesize[plane];
  167. if (flags & TINTERLACE_FLAG_VLPF) {
  168. // Low-pass filtering is required when creating an interlaced destination from
  169. // a progressive source which contains high-frequency vertical detail.
  170. // Filtering will reduce interlace 'twitter' and Moire patterning.
  171. int srcp_linesize = src_linesize[plane] * k;
  172. int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
  173. for (h = lines; h > 0; h--) {
  174. const uint8_t *srcp_above = srcp - src_linesize[plane];
  175. const uint8_t *srcp_below = srcp + src_linesize[plane];
  176. if (h == lines) srcp_above = srcp; // there is no line above
  177. if (h == 1) srcp_below = srcp; // there is no line below
  178. for (i = 0; i < linesize; i++) {
  179. // this calculation is an integer representation of
  180. // '0.5 * current + 0.25 * above + 0.25 * below'
  181. // '1 +' is for rounding. */
  182. dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
  183. }
  184. dstp += dstp_linesize;
  185. srcp += srcp_linesize;
  186. }
  187. } else {
  188. av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
  189. srcp, src_linesize[plane]*k, linesize, lines);
  190. }
  191. }
  192. }
  193. static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
  194. {
  195. AVFilterContext *ctx = inlink->dst;
  196. AVFilterLink *outlink = ctx->outputs[0];
  197. TInterlaceContext *tinterlace = ctx->priv;
  198. AVFrame *cur, *next, *out;
  199. int field, tff, ret;
  200. av_frame_free(&tinterlace->cur);
  201. tinterlace->cur = tinterlace->next;
  202. tinterlace->next = picref;
  203. cur = tinterlace->cur;
  204. next = tinterlace->next;
  205. /* we need at least two frames */
  206. if (!tinterlace->cur)
  207. return 0;
  208. switch (tinterlace->mode) {
  209. case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
  210. * the lower field, generating a double-height video at half framerate */
  211. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  212. if (!out)
  213. return AVERROR(ENOMEM);
  214. av_frame_copy_props(out, cur);
  215. out->height = outlink->h;
  216. out->interlaced_frame = 1;
  217. out->top_field_first = 1;
  218. /* write odd frame lines into the upper field of the new frame */
  219. copy_picture_field(out->data, out->linesize,
  220. (const uint8_t **)cur->data, cur->linesize,
  221. inlink->format, inlink->w, inlink->h,
  222. FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER, tinterlace->flags);
  223. /* write even frame lines into the lower field of the new frame */
  224. copy_picture_field(out->data, out->linesize,
  225. (const uint8_t **)next->data, next->linesize,
  226. inlink->format, inlink->w, inlink->h,
  227. FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER, tinterlace->flags);
  228. av_frame_free(&tinterlace->next);
  229. break;
  230. case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
  231. case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
  232. out = av_frame_clone(tinterlace->mode == MODE_DROP_EVEN ? cur : next);
  233. av_frame_free(&tinterlace->next);
  234. break;
  235. case MODE_PAD: /* expand each frame to double height, but pad alternate
  236. * lines with black; framerate unchanged */
  237. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  238. if (!out)
  239. return AVERROR(ENOMEM);
  240. av_frame_copy_props(out, cur);
  241. out->height = outlink->h;
  242. field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
  243. /* copy upper and lower fields */
  244. copy_picture_field(out->data, out->linesize,
  245. (const uint8_t **)cur->data, cur->linesize,
  246. inlink->format, inlink->w, inlink->h,
  247. FIELD_UPPER_AND_LOWER, 1, field, tinterlace->flags);
  248. /* pad with black the other field */
  249. copy_picture_field(out->data, out->linesize,
  250. (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
  251. inlink->format, inlink->w, inlink->h,
  252. FIELD_UPPER_AND_LOWER, 1, !field, tinterlace->flags);
  253. break;
  254. /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
  255. * halving the frame rate and preserving image height */
  256. case MODE_INTERLEAVE_TOP: /* top field first */
  257. case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
  258. tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
  259. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  260. if (!out)
  261. return AVERROR(ENOMEM);
  262. av_frame_copy_props(out, cur);
  263. out->interlaced_frame = 1;
  264. out->top_field_first = tff;
  265. /* copy upper/lower field from cur */
  266. copy_picture_field(out->data, out->linesize,
  267. (const uint8_t **)cur->data, cur->linesize,
  268. inlink->format, inlink->w, inlink->h,
  269. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
  270. tinterlace->flags);
  271. /* copy lower/upper field from next */
  272. copy_picture_field(out->data, out->linesize,
  273. (const uint8_t **)next->data, next->linesize,
  274. inlink->format, inlink->w, inlink->h,
  275. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
  276. tinterlace->flags);
  277. av_frame_free(&tinterlace->next);
  278. break;
  279. case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
  280. /* output current frame first */
  281. out = av_frame_clone(cur);
  282. if (!out)
  283. return AVERROR(ENOMEM);
  284. out->interlaced_frame = 1;
  285. if ((ret = ff_filter_frame(outlink, out)) < 0)
  286. return ret;
  287. /* output mix of current and next frame */
  288. tff = next->top_field_first;
  289. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  290. if (!out)
  291. return AVERROR(ENOMEM);
  292. av_frame_copy_props(out, next);
  293. out->interlaced_frame = 1;
  294. /* write current frame second field lines into the second field of the new frame */
  295. copy_picture_field(out->data, out->linesize,
  296. (const uint8_t **)cur->data, cur->linesize,
  297. inlink->format, inlink->w, inlink->h,
  298. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
  299. tinterlace->flags);
  300. /* write next frame first field lines into the first field of the new frame */
  301. copy_picture_field(out->data, out->linesize,
  302. (const uint8_t **)next->data, next->linesize,
  303. inlink->format, inlink->w, inlink->h,
  304. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
  305. tinterlace->flags);
  306. break;
  307. default:
  308. av_assert0(0);
  309. }
  310. ret = ff_filter_frame(outlink, out);
  311. tinterlace->frame++;
  312. return ret;
  313. }
  314. static const AVFilterPad tinterlace_inputs[] = {
  315. {
  316. .name = "default",
  317. .type = AVMEDIA_TYPE_VIDEO,
  318. .filter_frame = filter_frame,
  319. },
  320. { NULL }
  321. };
  322. static const AVFilterPad tinterlace_outputs[] = {
  323. {
  324. .name = "default",
  325. .type = AVMEDIA_TYPE_VIDEO,
  326. .config_props = config_out_props,
  327. },
  328. { NULL }
  329. };
  330. AVFilter avfilter_vf_tinterlace = {
  331. .name = "tinterlace",
  332. .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
  333. .priv_size = sizeof(TInterlaceContext),
  334. .uninit = uninit,
  335. .query_formats = query_formats,
  336. .inputs = tinterlace_inputs,
  337. .outputs = tinterlace_outputs,
  338. .priv_class = &tinterlace_class,
  339. };