vf_tinterlace.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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_YUV410P, AV_PIX_FMT_YUV411P,
  79. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  80. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  81. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  82. AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
  83. AV_PIX_FMT_NONE
  84. };
  85. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  86. return 0;
  87. }
  88. static av_cold void uninit(AVFilterContext *ctx)
  89. {
  90. TInterlaceContext *tinterlace = ctx->priv;
  91. av_frame_free(&tinterlace->cur );
  92. av_frame_free(&tinterlace->next);
  93. av_freep(&tinterlace->black_data[0]);
  94. }
  95. static int config_out_props(AVFilterLink *outlink)
  96. {
  97. AVFilterContext *ctx = outlink->src;
  98. AVFilterLink *inlink = outlink->src->inputs[0];
  99. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
  100. TInterlaceContext *tinterlace = ctx->priv;
  101. tinterlace->vsub = desc->log2_chroma_h;
  102. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  103. outlink->w = inlink->w;
  104. outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD ?
  105. inlink->h*2 : inlink->h;
  106. if (tinterlace->mode == MODE_PAD) {
  107. uint8_t black[4] = { 16, 128, 128, 16 };
  108. int i, ret;
  109. if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
  110. black[0] = black[3] = 0;
  111. ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
  112. outlink->w, outlink->h, outlink->format, 1);
  113. if (ret < 0)
  114. return ret;
  115. /* fill black picture with black */
  116. for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
  117. int h = i == 1 || i == 2 ? FF_CEIL_RSHIFT(outlink->h, desc->log2_chroma_h) : outlink->h;
  118. memset(tinterlace->black_data[i], black[i],
  119. tinterlace->black_linesize[i] * h);
  120. }
  121. }
  122. if ((tinterlace->flags & TINTERLACE_FLAG_VLPF)
  123. && !(tinterlace->mode == MODE_INTERLEAVE_TOP
  124. || tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) {
  125. av_log(ctx, AV_LOG_WARNING, "low_pass_filter flag ignored with mode %d\n",
  126. tinterlace->mode);
  127. tinterlace->flags &= ~TINTERLACE_FLAG_VLPF;
  128. }
  129. av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n",
  130. tinterlace->mode, (tinterlace->flags & TINTERLACE_FLAG_VLPF) ? "on" : "off",
  131. inlink->h, outlink->h);
  132. return 0;
  133. }
  134. #define FIELD_UPPER 0
  135. #define FIELD_LOWER 1
  136. #define FIELD_UPPER_AND_LOWER 2
  137. /**
  138. * Copy picture field from src to dst.
  139. *
  140. * @param src_field copy from upper, lower field or both
  141. * @param interleave leave a padding line between each copied line
  142. * @param dst_field copy to upper or lower field,
  143. * only meaningful when interleave is selected
  144. * @param flags context flags
  145. */
  146. static inline
  147. void copy_picture_field(uint8_t *dst[4], int dst_linesize[4],
  148. const uint8_t *src[4], int src_linesize[4],
  149. enum AVPixelFormat format, int w, int src_h,
  150. int src_field, int interleave, int dst_field,
  151. int flags)
  152. {
  153. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
  154. int plane, vsub = desc->log2_chroma_h;
  155. int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
  156. int h, i;
  157. for (plane = 0; plane < desc->nb_components; plane++) {
  158. int lines = plane == 1 || plane == 2 ? FF_CEIL_RSHIFT(src_h, vsub) : src_h;
  159. int linesize = av_image_get_linesize(format, w, plane);
  160. uint8_t *dstp = dst[plane];
  161. const uint8_t *srcp = src[plane];
  162. if (linesize < 0)
  163. return;
  164. lines = (lines + (src_field == FIELD_UPPER)) / k;
  165. if (src_field == FIELD_LOWER)
  166. srcp += src_linesize[plane];
  167. if (interleave && dst_field == FIELD_LOWER)
  168. dstp += dst_linesize[plane];
  169. if (flags & TINTERLACE_FLAG_VLPF) {
  170. // Low-pass filtering is required when creating an interlaced destination from
  171. // a progressive source which contains high-frequency vertical detail.
  172. // Filtering will reduce interlace 'twitter' and Moire patterning.
  173. int srcp_linesize = src_linesize[plane] * k;
  174. int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
  175. for (h = lines; h > 0; h--) {
  176. const uint8_t *srcp_above = srcp - src_linesize[plane];
  177. const uint8_t *srcp_below = srcp + src_linesize[plane];
  178. if (h == lines) srcp_above = srcp; // there is no line above
  179. if (h == 1) srcp_below = srcp; // there is no line below
  180. for (i = 0; i < linesize; i++) {
  181. // this calculation is an integer representation of
  182. // '0.5 * current + 0.25 * above + 0.25 * below'
  183. // '1 +' is for rounding. */
  184. dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
  185. }
  186. dstp += dstp_linesize;
  187. srcp += srcp_linesize;
  188. }
  189. } else {
  190. av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
  191. srcp, src_linesize[plane]*k, linesize, lines);
  192. }
  193. }
  194. }
  195. static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
  196. {
  197. AVFilterContext *ctx = inlink->dst;
  198. AVFilterLink *outlink = ctx->outputs[0];
  199. TInterlaceContext *tinterlace = ctx->priv;
  200. AVFrame *cur, *next, *out;
  201. int field, tff, ret;
  202. av_frame_free(&tinterlace->cur);
  203. tinterlace->cur = tinterlace->next;
  204. tinterlace->next = picref;
  205. cur = tinterlace->cur;
  206. next = tinterlace->next;
  207. /* we need at least two frames */
  208. if (!tinterlace->cur)
  209. return 0;
  210. switch (tinterlace->mode) {
  211. case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
  212. * the lower field, generating a double-height video at half framerate */
  213. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  214. if (!out)
  215. return AVERROR(ENOMEM);
  216. av_frame_copy_props(out, cur);
  217. out->height = outlink->h;
  218. out->interlaced_frame = 1;
  219. out->top_field_first = 1;
  220. /* write odd frame lines into the upper field of the new frame */
  221. copy_picture_field(out->data, out->linesize,
  222. (const uint8_t **)cur->data, cur->linesize,
  223. inlink->format, inlink->w, inlink->h,
  224. FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER, tinterlace->flags);
  225. /* write even frame lines into the lower field of the new frame */
  226. copy_picture_field(out->data, out->linesize,
  227. (const uint8_t **)next->data, next->linesize,
  228. inlink->format, inlink->w, inlink->h,
  229. FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER, tinterlace->flags);
  230. av_frame_free(&tinterlace->next);
  231. break;
  232. case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
  233. case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
  234. out = av_frame_clone(tinterlace->mode == MODE_DROP_EVEN ? cur : next);
  235. if (!out)
  236. return AVERROR(ENOMEM);
  237. av_frame_free(&tinterlace->next);
  238. break;
  239. case MODE_PAD: /* expand each frame to double height, but pad alternate
  240. * lines with black; framerate unchanged */
  241. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  242. if (!out)
  243. return AVERROR(ENOMEM);
  244. av_frame_copy_props(out, cur);
  245. out->height = outlink->h;
  246. field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
  247. /* copy upper and lower fields */
  248. copy_picture_field(out->data, out->linesize,
  249. (const uint8_t **)cur->data, cur->linesize,
  250. inlink->format, inlink->w, inlink->h,
  251. FIELD_UPPER_AND_LOWER, 1, field, tinterlace->flags);
  252. /* pad with black the other field */
  253. copy_picture_field(out->data, out->linesize,
  254. (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
  255. inlink->format, inlink->w, inlink->h,
  256. FIELD_UPPER_AND_LOWER, 1, !field, tinterlace->flags);
  257. break;
  258. /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
  259. * halving the frame rate and preserving image height */
  260. case MODE_INTERLEAVE_TOP: /* top field first */
  261. case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
  262. tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
  263. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  264. if (!out)
  265. return AVERROR(ENOMEM);
  266. av_frame_copy_props(out, cur);
  267. out->interlaced_frame = 1;
  268. out->top_field_first = tff;
  269. /* copy upper/lower field from cur */
  270. copy_picture_field(out->data, out->linesize,
  271. (const uint8_t **)cur->data, cur->linesize,
  272. inlink->format, inlink->w, inlink->h,
  273. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
  274. tinterlace->flags);
  275. /* copy lower/upper field from next */
  276. copy_picture_field(out->data, out->linesize,
  277. (const uint8_t **)next->data, next->linesize,
  278. inlink->format, inlink->w, inlink->h,
  279. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
  280. tinterlace->flags);
  281. av_frame_free(&tinterlace->next);
  282. break;
  283. case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
  284. /* output current frame first */
  285. out = av_frame_clone(cur);
  286. if (!out)
  287. return AVERROR(ENOMEM);
  288. out->interlaced_frame = 1;
  289. if ((ret = ff_filter_frame(outlink, out)) < 0)
  290. return ret;
  291. /* output mix of current and next frame */
  292. tff = next->top_field_first;
  293. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  294. if (!out)
  295. return AVERROR(ENOMEM);
  296. av_frame_copy_props(out, next);
  297. out->interlaced_frame = 1;
  298. /* write current frame second field lines into the second field of the new frame */
  299. copy_picture_field(out->data, out->linesize,
  300. (const uint8_t **)cur->data, cur->linesize,
  301. inlink->format, inlink->w, inlink->h,
  302. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
  303. tinterlace->flags);
  304. /* write next frame first field lines into the first field of the new frame */
  305. copy_picture_field(out->data, out->linesize,
  306. (const uint8_t **)next->data, next->linesize,
  307. inlink->format, inlink->w, inlink->h,
  308. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
  309. tinterlace->flags);
  310. break;
  311. default:
  312. av_assert0(0);
  313. }
  314. ret = ff_filter_frame(outlink, out);
  315. tinterlace->frame++;
  316. return ret;
  317. }
  318. static const AVFilterPad tinterlace_inputs[] = {
  319. {
  320. .name = "default",
  321. .type = AVMEDIA_TYPE_VIDEO,
  322. .filter_frame = filter_frame,
  323. },
  324. { NULL }
  325. };
  326. static const AVFilterPad tinterlace_outputs[] = {
  327. {
  328. .name = "default",
  329. .type = AVMEDIA_TYPE_VIDEO,
  330. .config_props = config_out_props,
  331. },
  332. { NULL }
  333. };
  334. AVFilter ff_vf_tinterlace = {
  335. .name = "tinterlace",
  336. .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
  337. .priv_size = sizeof(TInterlaceContext),
  338. .uninit = uninit,
  339. .query_formats = query_formats,
  340. .inputs = tinterlace_inputs,
  341. .outputs = tinterlace_outputs,
  342. .priv_class = &tinterlace_class,
  343. };