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.

421 lines
18KB

  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. #define TINTERLACE_FLAG_EXACT_TB 2
  36. static const AVOption tinterlace_options[] = {
  37. {"mode", "select interlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_MERGE}, 0, MODE_NB-1, FLAGS, "mode"},
  38. {"merge", "merge fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGE}, INT_MIN, INT_MAX, FLAGS, "mode"},
  39. {"drop_even", "drop even fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_EVEN}, INT_MIN, INT_MAX, FLAGS, "mode"},
  40. {"drop_odd", "drop odd fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_ODD}, INT_MIN, INT_MAX, FLAGS, "mode"},
  41. {"pad", "pad alternate lines with black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PAD}, INT_MIN, INT_MAX, FLAGS, "mode"},
  42. {"interleave_top", "interleave top and bottom fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_TOP}, INT_MIN, INT_MAX, FLAGS, "mode"},
  43. {"interleave_bottom", "interleave bottom and top fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "mode"},
  44. {"interlacex2", "interlace fields from two consecutive frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLACEX2}, INT_MIN, INT_MAX, FLAGS, "mode"},
  45. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
  46. {"low_pass_filter", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
  47. {"vlpf", "enable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_VLPF}, INT_MIN, INT_MAX, FLAGS, "flags" },
  48. {"exact_tb", "force a timebase which can represent timestamps exactly", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_EXACT_TB}, INT_MIN, INT_MAX, FLAGS, "flags" },
  49. {NULL}
  50. };
  51. AVFILTER_DEFINE_CLASS(tinterlace);
  52. #define FULL_SCALE_YUVJ_FORMATS \
  53. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
  54. static const enum AVPixelFormat full_scale_yuvj_pix_fmts[] = {
  55. FULL_SCALE_YUVJ_FORMATS, AV_PIX_FMT_NONE
  56. };
  57. static const AVRational standard_tbs[] = {
  58. {1, 25},
  59. {1, 30},
  60. {1001, 30000},
  61. };
  62. static int query_formats(AVFilterContext *ctx)
  63. {
  64. static const enum AVPixelFormat pix_fmts[] = {
  65. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  66. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  67. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  68. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  69. AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
  70. AV_PIX_FMT_NONE
  71. };
  72. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  73. if (!fmts_list)
  74. return AVERROR(ENOMEM);
  75. return ff_set_common_formats(ctx, fmts_list);
  76. }
  77. static void lowpass_line_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp,
  78. const uint8_t *srcp_above, const uint8_t *srcp_below)
  79. {
  80. int i;
  81. for (i = 0; i < width; i++) {
  82. // this calculation is an integer representation of
  83. // '0.5 * current + 0.25 * above + 0.25 * below'
  84. // '1 +' is for rounding.
  85. dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
  86. }
  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. int i;
  102. tinterlace->vsub = desc->log2_chroma_h;
  103. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  104. outlink->w = inlink->w;
  105. outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD ?
  106. inlink->h*2 : inlink->h;
  107. if (tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD)
  108. outlink->sample_aspect_ratio = av_mul_q(inlink->sample_aspect_ratio,
  109. av_make_q(2, 1));
  110. if (tinterlace->mode == MODE_PAD) {
  111. uint8_t black[4] = { 16, 128, 128, 16 };
  112. int i, ret;
  113. if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
  114. black[0] = black[3] = 0;
  115. ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
  116. outlink->w, outlink->h, outlink->format, 1);
  117. if (ret < 0)
  118. return ret;
  119. /* fill black picture with black */
  120. for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
  121. int h = i == 1 || i == 2 ? FF_CEIL_RSHIFT(outlink->h, desc->log2_chroma_h) : outlink->h;
  122. memset(tinterlace->black_data[i], black[i],
  123. tinterlace->black_linesize[i] * h);
  124. }
  125. }
  126. if ((tinterlace->flags & TINTERLACE_FLAG_VLPF)
  127. && !(tinterlace->mode == MODE_INTERLEAVE_TOP
  128. || tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) {
  129. av_log(ctx, AV_LOG_WARNING, "low_pass_filter flag ignored with mode %d\n",
  130. tinterlace->mode);
  131. tinterlace->flags &= ~TINTERLACE_FLAG_VLPF;
  132. }
  133. tinterlace->preout_time_base = inlink->time_base;
  134. if (tinterlace->mode == MODE_INTERLACEX2) {
  135. tinterlace->preout_time_base.den *= 2;
  136. outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){2,1});
  137. outlink->time_base = av_mul_q(inlink->time_base , (AVRational){1,2});
  138. } else if (tinterlace->mode != MODE_PAD) {
  139. outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){1,2});
  140. outlink->time_base = av_mul_q(inlink->time_base , (AVRational){2,1});
  141. }
  142. for (i = 0; i<FF_ARRAY_ELEMS(standard_tbs); i++){
  143. if (!av_cmp_q(standard_tbs[i], outlink->time_base))
  144. break;
  145. }
  146. if (i == FF_ARRAY_ELEMS(standard_tbs) ||
  147. (tinterlace->flags & TINTERLACE_FLAG_EXACT_TB))
  148. outlink->time_base = tinterlace->preout_time_base;
  149. if (tinterlace->flags & TINTERLACE_FLAG_VLPF) {
  150. tinterlace->lowpass_line = lowpass_line_c;
  151. if (ARCH_X86)
  152. ff_tinterlace_init_x86(tinterlace);
  153. }
  154. av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n",
  155. tinterlace->mode, (tinterlace->flags & TINTERLACE_FLAG_VLPF) ? "on" : "off",
  156. inlink->h, outlink->h);
  157. return 0;
  158. }
  159. #define FIELD_UPPER 0
  160. #define FIELD_LOWER 1
  161. #define FIELD_UPPER_AND_LOWER 2
  162. /**
  163. * Copy picture field from src to dst.
  164. *
  165. * @param src_field copy from upper, lower field or both
  166. * @param interleave leave a padding line between each copied line
  167. * @param dst_field copy to upper or lower field,
  168. * only meaningful when interleave is selected
  169. * @param flags context flags
  170. */
  171. static inline
  172. void copy_picture_field(TInterlaceContext *tinterlace,
  173. uint8_t *dst[4], int dst_linesize[4],
  174. const uint8_t *src[4], int src_linesize[4],
  175. enum AVPixelFormat format, int w, int src_h,
  176. int src_field, int interleave, int dst_field,
  177. int flags)
  178. {
  179. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
  180. int hsub = desc->log2_chroma_w;
  181. int plane, vsub = desc->log2_chroma_h;
  182. int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
  183. int h;
  184. for (plane = 0; plane < desc->nb_components; plane++) {
  185. int lines = plane == 1 || plane == 2 ? FF_CEIL_RSHIFT(src_h, vsub) : src_h;
  186. int cols = plane == 1 || plane == 2 ? FF_CEIL_RSHIFT( w, hsub) : w;
  187. uint8_t *dstp = dst[plane];
  188. const uint8_t *srcp = src[plane];
  189. lines = (lines + (src_field == FIELD_UPPER)) / k;
  190. if (src_field == FIELD_LOWER)
  191. srcp += src_linesize[plane];
  192. if (interleave && dst_field == FIELD_LOWER)
  193. dstp += dst_linesize[plane];
  194. if (flags & TINTERLACE_FLAG_VLPF) {
  195. // Low-pass filtering is required when creating an interlaced destination from
  196. // a progressive source which contains high-frequency vertical detail.
  197. // Filtering will reduce interlace 'twitter' and Moire patterning.
  198. int srcp_linesize = src_linesize[plane] * k;
  199. int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
  200. for (h = lines; h > 0; h--) {
  201. const uint8_t *srcp_above = srcp - src_linesize[plane];
  202. const uint8_t *srcp_below = srcp + src_linesize[plane];
  203. if (h == lines) srcp_above = srcp; // there is no line above
  204. if (h == 1) srcp_below = srcp; // there is no line below
  205. tinterlace->lowpass_line(dstp, cols, srcp, srcp_above, srcp_below);
  206. dstp += dstp_linesize;
  207. srcp += srcp_linesize;
  208. }
  209. } else {
  210. av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
  211. srcp, src_linesize[plane]*k, cols, lines);
  212. }
  213. }
  214. }
  215. static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
  216. {
  217. AVFilterContext *ctx = inlink->dst;
  218. AVFilterLink *outlink = ctx->outputs[0];
  219. TInterlaceContext *tinterlace = ctx->priv;
  220. AVFrame *cur, *next, *out;
  221. int field, tff, ret;
  222. av_frame_free(&tinterlace->cur);
  223. tinterlace->cur = tinterlace->next;
  224. tinterlace->next = picref;
  225. cur = tinterlace->cur;
  226. next = tinterlace->next;
  227. /* we need at least two frames */
  228. if (!tinterlace->cur)
  229. return 0;
  230. switch (tinterlace->mode) {
  231. case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
  232. * the lower field, generating a double-height video at half framerate */
  233. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  234. if (!out)
  235. return AVERROR(ENOMEM);
  236. av_frame_copy_props(out, cur);
  237. out->height = outlink->h;
  238. out->interlaced_frame = 1;
  239. out->top_field_first = 1;
  240. out->sample_aspect_ratio = av_mul_q(cur->sample_aspect_ratio, av_make_q(2, 1));
  241. /* write odd frame lines into the upper field of the new frame */
  242. copy_picture_field(tinterlace, out->data, out->linesize,
  243. (const uint8_t **)cur->data, cur->linesize,
  244. inlink->format, inlink->w, inlink->h,
  245. FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER, tinterlace->flags);
  246. /* write even frame lines into the lower field of the new frame */
  247. copy_picture_field(tinterlace, out->data, out->linesize,
  248. (const uint8_t **)next->data, next->linesize,
  249. inlink->format, inlink->w, inlink->h,
  250. FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER, tinterlace->flags);
  251. av_frame_free(&tinterlace->next);
  252. break;
  253. case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
  254. case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
  255. out = av_frame_clone(tinterlace->mode == MODE_DROP_EVEN ? cur : next);
  256. if (!out)
  257. return AVERROR(ENOMEM);
  258. av_frame_free(&tinterlace->next);
  259. break;
  260. case MODE_PAD: /* expand each frame to double height, but pad alternate
  261. * lines with black; framerate unchanged */
  262. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  263. if (!out)
  264. return AVERROR(ENOMEM);
  265. av_frame_copy_props(out, cur);
  266. out->height = outlink->h;
  267. out->sample_aspect_ratio = av_mul_q(cur->sample_aspect_ratio, av_make_q(2, 1));
  268. field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
  269. /* copy upper and lower fields */
  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. FIELD_UPPER_AND_LOWER, 1, field, tinterlace->flags);
  274. /* pad with black the other field */
  275. copy_picture_field(tinterlace, out->data, out->linesize,
  276. (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
  277. inlink->format, inlink->w, inlink->h,
  278. FIELD_UPPER_AND_LOWER, 1, !field, tinterlace->flags);
  279. break;
  280. /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
  281. * halving the frame rate and preserving image height */
  282. case MODE_INTERLEAVE_TOP: /* top field first */
  283. case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
  284. tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
  285. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  286. if (!out)
  287. return AVERROR(ENOMEM);
  288. av_frame_copy_props(out, cur);
  289. out->interlaced_frame = 1;
  290. out->top_field_first = tff;
  291. /* copy upper/lower field from cur */
  292. copy_picture_field(tinterlace, out->data, out->linesize,
  293. (const uint8_t **)cur->data, cur->linesize,
  294. inlink->format, inlink->w, inlink->h,
  295. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
  296. tinterlace->flags);
  297. /* copy lower/upper field from next */
  298. copy_picture_field(tinterlace, out->data, out->linesize,
  299. (const uint8_t **)next->data, next->linesize,
  300. inlink->format, inlink->w, inlink->h,
  301. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
  302. tinterlace->flags);
  303. av_frame_free(&tinterlace->next);
  304. break;
  305. case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
  306. /* output current frame first */
  307. out = av_frame_clone(cur);
  308. if (!out)
  309. return AVERROR(ENOMEM);
  310. out->interlaced_frame = 1;
  311. if (cur->pts != AV_NOPTS_VALUE)
  312. out->pts = cur->pts*2;
  313. out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
  314. if ((ret = ff_filter_frame(outlink, out)) < 0)
  315. return ret;
  316. /* output mix of current and next frame */
  317. tff = next->top_field_first;
  318. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  319. if (!out)
  320. return AVERROR(ENOMEM);
  321. av_frame_copy_props(out, next);
  322. out->interlaced_frame = 1;
  323. out->top_field_first = !tff;
  324. if (next->pts != AV_NOPTS_VALUE && cur->pts != AV_NOPTS_VALUE)
  325. out->pts = cur->pts + next->pts;
  326. else
  327. out->pts = AV_NOPTS_VALUE;
  328. /* write current frame second field lines into the second field of the new frame */
  329. copy_picture_field(tinterlace, out->data, out->linesize,
  330. (const uint8_t **)cur->data, cur->linesize,
  331. inlink->format, inlink->w, inlink->h,
  332. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
  333. tinterlace->flags);
  334. /* write next frame first field lines into the first field of the new frame */
  335. copy_picture_field(tinterlace, out->data, out->linesize,
  336. (const uint8_t **)next->data, next->linesize,
  337. inlink->format, inlink->w, inlink->h,
  338. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
  339. tinterlace->flags);
  340. break;
  341. default:
  342. av_assert0(0);
  343. }
  344. out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
  345. ret = ff_filter_frame(outlink, out);
  346. tinterlace->frame++;
  347. return ret;
  348. }
  349. static const AVFilterPad tinterlace_inputs[] = {
  350. {
  351. .name = "default",
  352. .type = AVMEDIA_TYPE_VIDEO,
  353. .filter_frame = filter_frame,
  354. },
  355. { NULL }
  356. };
  357. static const AVFilterPad tinterlace_outputs[] = {
  358. {
  359. .name = "default",
  360. .type = AVMEDIA_TYPE_VIDEO,
  361. .config_props = config_out_props,
  362. },
  363. { NULL }
  364. };
  365. AVFilter ff_vf_tinterlace = {
  366. .name = "tinterlace",
  367. .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
  368. .priv_size = sizeof(TInterlaceContext),
  369. .uninit = uninit,
  370. .query_formats = query_formats,
  371. .inputs = tinterlace_inputs,
  372. .outputs = tinterlace_outputs,
  373. .priv_class = &tinterlace_class,
  374. };