vf_tinterlace.c 19 KB

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