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.

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