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.

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