vf_tinterlace.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 plane, vsub = desc->log2_chroma_h;
  176. int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
  177. int h;
  178. for (plane = 0; plane < desc->nb_components; plane++) {
  179. int lines = plane == 1 || plane == 2 ? FF_CEIL_RSHIFT(src_h, vsub) : src_h;
  180. int cols = plane == 1 || plane == 2 ? FF_CEIL_RSHIFT( w, desc->log2_chroma_w) : w;
  181. int linesize = av_image_get_linesize(format, w, plane);
  182. uint8_t *dstp = dst[plane];
  183. const uint8_t *srcp = src[plane];
  184. if (linesize < 0)
  185. return;
  186. lines = (lines + (src_field == FIELD_UPPER)) / k;
  187. if (src_field == FIELD_LOWER)
  188. srcp += src_linesize[plane];
  189. if (interleave && dst_field == FIELD_LOWER)
  190. dstp += dst_linesize[plane];
  191. if (flags & TINTERLACE_FLAG_VLPF) {
  192. // Low-pass filtering is required when creating an interlaced destination from
  193. // a progressive source which contains high-frequency vertical detail.
  194. // Filtering will reduce interlace 'twitter' and Moire patterning.
  195. int srcp_linesize = src_linesize[plane] * k;
  196. int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
  197. for (h = lines; h > 0; h--) {
  198. const uint8_t *srcp_above = srcp - src_linesize[plane];
  199. const uint8_t *srcp_below = srcp + src_linesize[plane];
  200. if (h == lines) srcp_above = srcp; // there is no line above
  201. if (h == 1) srcp_below = srcp; // there is no line below
  202. tinterlace->lowpass_line(dstp, cols, srcp, srcp_above, srcp_below);
  203. dstp += dstp_linesize;
  204. srcp += srcp_linesize;
  205. }
  206. } else {
  207. av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
  208. srcp, src_linesize[plane]*k, linesize, lines);
  209. }
  210. }
  211. }
  212. static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
  213. {
  214. AVFilterContext *ctx = inlink->dst;
  215. AVFilterLink *outlink = ctx->outputs[0];
  216. TInterlaceContext *tinterlace = ctx->priv;
  217. AVFrame *cur, *next, *out;
  218. int field, tff, ret;
  219. av_frame_free(&tinterlace->cur);
  220. tinterlace->cur = tinterlace->next;
  221. tinterlace->next = picref;
  222. cur = tinterlace->cur;
  223. next = tinterlace->next;
  224. /* we need at least two frames */
  225. if (!tinterlace->cur)
  226. return 0;
  227. switch (tinterlace->mode) {
  228. case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
  229. * the lower field, generating a double-height video at half framerate */
  230. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  231. if (!out)
  232. return AVERROR(ENOMEM);
  233. av_frame_copy_props(out, cur);
  234. out->height = outlink->h;
  235. out->interlaced_frame = 1;
  236. out->top_field_first = 1;
  237. /* write odd frame lines into the upper field of the new frame */
  238. copy_picture_field(tinterlace, out->data, out->linesize,
  239. (const uint8_t **)cur->data, cur->linesize,
  240. inlink->format, inlink->w, inlink->h,
  241. FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER, tinterlace->flags);
  242. /* write even frame lines into the lower field of the new frame */
  243. copy_picture_field(tinterlace, out->data, out->linesize,
  244. (const uint8_t **)next->data, next->linesize,
  245. inlink->format, inlink->w, inlink->h,
  246. FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER, tinterlace->flags);
  247. av_frame_free(&tinterlace->next);
  248. break;
  249. case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
  250. case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
  251. out = av_frame_clone(tinterlace->mode == MODE_DROP_EVEN ? cur : next);
  252. if (!out)
  253. return AVERROR(ENOMEM);
  254. av_frame_free(&tinterlace->next);
  255. break;
  256. case MODE_PAD: /* expand each frame to double height, but pad alternate
  257. * lines with black; framerate unchanged */
  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->height = outlink->h;
  263. field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
  264. /* copy upper and lower fields */
  265. copy_picture_field(tinterlace, out->data, out->linesize,
  266. (const uint8_t **)cur->data, cur->linesize,
  267. inlink->format, inlink->w, inlink->h,
  268. FIELD_UPPER_AND_LOWER, 1, field, tinterlace->flags);
  269. /* pad with black the other field */
  270. copy_picture_field(tinterlace, out->data, out->linesize,
  271. (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
  272. inlink->format, inlink->w, inlink->h,
  273. FIELD_UPPER_AND_LOWER, 1, !field, tinterlace->flags);
  274. break;
  275. /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
  276. * halving the frame rate and preserving image height */
  277. case MODE_INTERLEAVE_TOP: /* top field first */
  278. case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
  279. tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
  280. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  281. if (!out)
  282. return AVERROR(ENOMEM);
  283. av_frame_copy_props(out, cur);
  284. out->interlaced_frame = 1;
  285. out->top_field_first = tff;
  286. /* copy upper/lower field from cur */
  287. copy_picture_field(tinterlace, out->data, out->linesize,
  288. (const uint8_t **)cur->data, cur->linesize,
  289. inlink->format, inlink->w, inlink->h,
  290. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
  291. tinterlace->flags);
  292. /* copy lower/upper field from next */
  293. copy_picture_field(tinterlace, out->data, out->linesize,
  294. (const uint8_t **)next->data, next->linesize,
  295. inlink->format, inlink->w, inlink->h,
  296. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
  297. tinterlace->flags);
  298. av_frame_free(&tinterlace->next);
  299. break;
  300. case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
  301. /* output current frame first */
  302. out = av_frame_clone(cur);
  303. if (!out)
  304. return AVERROR(ENOMEM);
  305. out->interlaced_frame = 1;
  306. if (cur->pts != AV_NOPTS_VALUE)
  307. out->pts = cur->pts*2;
  308. out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
  309. if ((ret = ff_filter_frame(outlink, out)) < 0)
  310. return ret;
  311. /* output mix of current and next frame */
  312. tff = next->top_field_first;
  313. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  314. if (!out)
  315. return AVERROR(ENOMEM);
  316. av_frame_copy_props(out, next);
  317. out->interlaced_frame = 1;
  318. out->top_field_first = !tff;
  319. if (next->pts != AV_NOPTS_VALUE && cur->pts != AV_NOPTS_VALUE)
  320. out->pts = cur->pts + next->pts;
  321. else
  322. out->pts = AV_NOPTS_VALUE;
  323. /* write current frame second field lines into the second field of the new frame */
  324. copy_picture_field(tinterlace, out->data, out->linesize,
  325. (const uint8_t **)cur->data, cur->linesize,
  326. inlink->format, inlink->w, inlink->h,
  327. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
  328. tinterlace->flags);
  329. /* write next frame first field lines into the first field of the new frame */
  330. copy_picture_field(tinterlace, out->data, out->linesize,
  331. (const uint8_t **)next->data, next->linesize,
  332. inlink->format, inlink->w, inlink->h,
  333. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
  334. tinterlace->flags);
  335. break;
  336. default:
  337. av_assert0(0);
  338. }
  339. out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
  340. ret = ff_filter_frame(outlink, out);
  341. tinterlace->frame++;
  342. return ret;
  343. }
  344. static const AVFilterPad tinterlace_inputs[] = {
  345. {
  346. .name = "default",
  347. .type = AVMEDIA_TYPE_VIDEO,
  348. .filter_frame = filter_frame,
  349. },
  350. { NULL }
  351. };
  352. static const AVFilterPad tinterlace_outputs[] = {
  353. {
  354. .name = "default",
  355. .type = AVMEDIA_TYPE_VIDEO,
  356. .config_props = config_out_props,
  357. },
  358. { NULL }
  359. };
  360. AVFilter ff_vf_tinterlace = {
  361. .name = "tinterlace",
  362. .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
  363. .priv_size = sizeof(TInterlaceContext),
  364. .uninit = uninit,
  365. .query_formats = query_formats,
  366. .inputs = tinterlace_inputs,
  367. .outputs = tinterlace_outputs,
  368. .priv_class = &tinterlace_class,
  369. };