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.

396 lines
17KB

  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. #include "tinterlace.h"
  32. #define OFFSET(x) offsetof(TInterlaceContext, x)
  33. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  34. #define TINTERLACE_FLAG_VLPF 01
  35. static const AVOption tinterlace_options[] = {
  36. {"mode", "select interlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_MERGE}, 0, MODE_NB-1, FLAGS, "mode"},
  37. {"merge", "merge fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGE}, INT_MIN, INT_MAX, FLAGS, "mode"},
  38. {"drop_even", "drop even fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_EVEN}, INT_MIN, INT_MAX, FLAGS, "mode"},
  39. {"drop_odd", "drop odd fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_ODD}, INT_MIN, INT_MAX, FLAGS, "mode"},
  40. {"pad", "pad alternate lines with black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PAD}, INT_MIN, INT_MAX, FLAGS, "mode"},
  41. {"interleave_top", "interleave top and bottom fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_TOP}, INT_MIN, INT_MAX, FLAGS, "mode"},
  42. {"interleave_bottom", "interleave bottom and top fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "mode"},
  43. {"interlacex2", "interlace fields from two consecutive frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLACEX2}, INT_MIN, INT_MAX, FLAGS, "mode"},
  44. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
  45. {"low_pass_filter", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
  46. {"vlpf", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
  47. {NULL}
  48. };
  49. AVFILTER_DEFINE_CLASS(tinterlace);
  50. #define FULL_SCALE_YUVJ_FORMATS \
  51. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
  52. static const enum AVPixelFormat full_scale_yuvj_pix_fmts[] = {
  53. FULL_SCALE_YUVJ_FORMATS, AV_PIX_FMT_NONE
  54. };
  55. static int query_formats(AVFilterContext *ctx)
  56. {
  57. static const enum AVPixelFormat pix_fmts[] = {
  58. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  59. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  60. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  61. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  62. AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
  63. AV_PIX_FMT_NONE
  64. };
  65. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  66. return 0;
  67. }
  68. static void lowpass_line_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp,
  69. const uint8_t *srcp_above, const uint8_t *srcp_below)
  70. {
  71. int i;
  72. for (i = 0; i < width; i++) {
  73. // this calculation is an integer representation of
  74. // '0.5 * current + 0.25 * above + 0.25 * below'
  75. // '1 +' is for rounding.
  76. dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
  77. }
  78. }
  79. static av_cold void uninit(AVFilterContext *ctx)
  80. {
  81. TInterlaceContext *tinterlace = ctx->priv;
  82. av_frame_free(&tinterlace->cur );
  83. av_frame_free(&tinterlace->next);
  84. av_freep(&tinterlace->black_data[0]);
  85. }
  86. static int config_out_props(AVFilterLink *outlink)
  87. {
  88. AVFilterContext *ctx = outlink->src;
  89. AVFilterLink *inlink = outlink->src->inputs[0];
  90. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
  91. TInterlaceContext *tinterlace = ctx->priv;
  92. tinterlace->vsub = desc->log2_chroma_h;
  93. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  94. outlink->w = inlink->w;
  95. outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD ?
  96. inlink->h*2 : inlink->h;
  97. if (tinterlace->mode == MODE_PAD) {
  98. uint8_t black[4] = { 16, 128, 128, 16 };
  99. int i, ret;
  100. if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
  101. black[0] = black[3] = 0;
  102. ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
  103. outlink->w, outlink->h, outlink->format, 1);
  104. if (ret < 0)
  105. return ret;
  106. /* fill black picture with black */
  107. for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
  108. int h = i == 1 || i == 2 ? FF_CEIL_RSHIFT(outlink->h, desc->log2_chroma_h) : outlink->h;
  109. memset(tinterlace->black_data[i], black[i],
  110. tinterlace->black_linesize[i] * h);
  111. }
  112. }
  113. if ((tinterlace->flags & TINTERLACE_FLAG_VLPF)
  114. && !(tinterlace->mode == MODE_INTERLEAVE_TOP
  115. || tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) {
  116. av_log(ctx, AV_LOG_WARNING, "low_pass_filter flag ignored with mode %d\n",
  117. tinterlace->mode);
  118. tinterlace->flags &= ~TINTERLACE_FLAG_VLPF;
  119. }
  120. if (tinterlace->mode == MODE_INTERLACEX2) {
  121. outlink->time_base.num = inlink->time_base.num;
  122. outlink->time_base.den = inlink->time_base.den * 2;
  123. outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){2,1});
  124. } else if (tinterlace->mode != MODE_PAD) {
  125. outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){1,2});
  126. }
  127. if (tinterlace->flags & TINTERLACE_FLAG_VLPF) {
  128. tinterlace->lowpass_line = lowpass_line_c;
  129. if (ARCH_X86)
  130. ff_tinterlace_init_x86(tinterlace);
  131. }
  132. av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n",
  133. tinterlace->mode, (tinterlace->flags & TINTERLACE_FLAG_VLPF) ? "on" : "off",
  134. inlink->h, outlink->h);
  135. return 0;
  136. }
  137. #define FIELD_UPPER 0
  138. #define FIELD_LOWER 1
  139. #define FIELD_UPPER_AND_LOWER 2
  140. /**
  141. * Copy picture field from src to dst.
  142. *
  143. * @param src_field copy from upper, lower field or both
  144. * @param interleave leave a padding line between each copied line
  145. * @param dst_field copy to upper or lower field,
  146. * only meaningful when interleave is selected
  147. * @param flags context flags
  148. */
  149. static inline
  150. void copy_picture_field(TInterlaceContext *tinterlace,
  151. uint8_t *dst[4], int dst_linesize[4],
  152. const uint8_t *src[4], int src_linesize[4],
  153. enum AVPixelFormat format, int w, int src_h,
  154. int src_field, int interleave, int dst_field,
  155. int flags)
  156. {
  157. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
  158. int plane, vsub = desc->log2_chroma_h;
  159. int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
  160. int h;
  161. for (plane = 0; plane < desc->nb_components; plane++) {
  162. int lines = plane == 1 || plane == 2 ? FF_CEIL_RSHIFT(src_h, vsub) : src_h;
  163. int cols = plane == 1 || plane == 2 ? FF_CEIL_RSHIFT( w, desc->log2_chroma_w) : w;
  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. tinterlace->lowpass_line(dstp, cols, srcp, srcp_above, srcp_below);
  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(tinterlace, 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(tinterlace, 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(tinterlace, 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(tinterlace, 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(tinterlace, 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(tinterlace, 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 (cur->pts != AV_NOPTS_VALUE)
  290. out->pts = cur->pts*2;
  291. if ((ret = ff_filter_frame(outlink, out)) < 0)
  292. return ret;
  293. /* output mix of current and next frame */
  294. tff = next->top_field_first;
  295. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  296. if (!out)
  297. return AVERROR(ENOMEM);
  298. av_frame_copy_props(out, next);
  299. out->interlaced_frame = 1;
  300. out->top_field_first = !tff;
  301. if (next->pts != AV_NOPTS_VALUE && cur->pts != AV_NOPTS_VALUE)
  302. out->pts = cur->pts + next->pts;
  303. else
  304. out->pts = AV_NOPTS_VALUE;
  305. /* write current frame second field lines into the second field of the new frame */
  306. copy_picture_field(tinterlace, out->data, out->linesize,
  307. (const uint8_t **)cur->data, cur->linesize,
  308. inlink->format, inlink->w, inlink->h,
  309. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
  310. tinterlace->flags);
  311. /* write next frame first field lines into the first field of the new frame */
  312. copy_picture_field(tinterlace, out->data, out->linesize,
  313. (const uint8_t **)next->data, next->linesize,
  314. inlink->format, inlink->w, inlink->h,
  315. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
  316. tinterlace->flags);
  317. break;
  318. default:
  319. av_assert0(0);
  320. }
  321. ret = ff_filter_frame(outlink, out);
  322. tinterlace->frame++;
  323. return ret;
  324. }
  325. static const AVFilterPad tinterlace_inputs[] = {
  326. {
  327. .name = "default",
  328. .type = AVMEDIA_TYPE_VIDEO,
  329. .filter_frame = filter_frame,
  330. },
  331. { NULL }
  332. };
  333. static const AVFilterPad tinterlace_outputs[] = {
  334. {
  335. .name = "default",
  336. .type = AVMEDIA_TYPE_VIDEO,
  337. .config_props = config_out_props,
  338. },
  339. { NULL }
  340. };
  341. AVFilter ff_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. };