vf_tinterlace.c 17 KB

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