vf_tinterlace.c 16 KB

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