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.

581 lines
26KB

  1. /*
  2. * Copyright (c) 2017 Thomas Mundt <tmundt75@gmail.com>
  3. * Copyright (c) 2011 Stefano Sabatini
  4. * Copyright (c) 2010 Baptiste Coudurier
  5. * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with FFmpeg if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. */
  23. /**
  24. * @file
  25. * temporal field interlace filter, ported from MPlayer/libmpcodecs
  26. */
  27. #include "libavutil/opt.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/avassert.h"
  30. #include "avfilter.h"
  31. #include "internal.h"
  32. #include "tinterlace.h"
  33. #define OFFSET(x) offsetof(TInterlaceContext, x)
  34. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  35. static const AVOption tinterlace_options[] = {
  36. {"mode", "select interlace mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_MERGE}, 0, MODE_NB-1, FLAGS, "mode"},
  37. {"merge", "merge fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGE}, INT_MIN, INT_MAX, FLAGS, "mode"},
  38. {"drop_even", "drop even fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_EVEN}, INT_MIN, INT_MAX, FLAGS, "mode"},
  39. {"drop_odd", "drop odd fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_DROP_ODD}, INT_MIN, INT_MAX, FLAGS, "mode"},
  40. {"pad", "pad alternate lines with black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PAD}, INT_MIN, INT_MAX, FLAGS, "mode"},
  41. {"interleave_top", "interleave top and bottom fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_TOP}, INT_MIN, INT_MAX, FLAGS, "mode"},
  42. {"interleave_bottom", "interleave bottom and top fields", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "mode"},
  43. {"interlacex2", "interlace fields from two consecutive frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLACEX2}, INT_MIN, INT_MAX, FLAGS, "mode"},
  44. {"mergex2", "merge fields keeping same frame rate", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MERGEX2}, 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. {"complex_filter", "enable complex vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_CVLPF},INT_MIN, INT_MAX, FLAGS, "flags" },
  49. {"cvlpf", "enable complex vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_CVLPF},INT_MIN, INT_MAX, FLAGS, "flags" },
  50. {"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" },
  51. {"bypass_il", "bypass already interlaced frames", 0, AV_OPT_TYPE_CONST, {.i64 = TINTERLACE_FLAG_BYPASS_IL}, INT_MIN, INT_MAX, FLAGS, "flags" },
  52. {NULL}
  53. };
  54. AVFILTER_DEFINE_CLASS(tinterlace);
  55. static const AVOption interlace_options[] = {
  56. { "scan", "scanning mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_TFF}, 0, 1, FLAGS, "mode"},
  57. { "tff", "top field first", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_TFF}, INT_MIN, INT_MAX, FLAGS, .unit = "mode"},
  58. { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_BFF}, INT_MIN, INT_MAX, FLAGS, .unit = "mode"},
  59. { "lowpass", "set vertical low-pass filter", OFFSET(lowpass), AV_OPT_TYPE_INT, {.i64 = VLPF_LIN}, 0, 2, FLAGS, "lowpass" },
  60. { "off", "disable vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = VLPF_OFF}, INT_MIN, INT_MAX, FLAGS, "lowpass" },
  61. { "linear", "linear vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = VLPF_LIN}, INT_MIN, INT_MAX, FLAGS, "lowpass" },
  62. { "complex", "complex vertical low-pass filter", 0, AV_OPT_TYPE_CONST, {.i64 = VLPF_CMP}, INT_MIN, INT_MAX, FLAGS, "lowpass" },
  63. { NULL }
  64. };
  65. AVFILTER_DEFINE_CLASS(interlace);
  66. #define FULL_SCALE_YUVJ_FORMATS \
  67. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
  68. static const enum AVPixelFormat full_scale_yuvj_pix_fmts[] = {
  69. FULL_SCALE_YUVJ_FORMATS, AV_PIX_FMT_NONE
  70. };
  71. static const AVRational standard_tbs[] = {
  72. {1, 25},
  73. {1, 30},
  74. {1001, 30000},
  75. };
  76. static int query_formats(AVFilterContext *ctx)
  77. {
  78. static const enum AVPixelFormat pix_fmts[] = {
  79. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  80. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  81. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  82. AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUV422P10LE,
  83. AV_PIX_FMT_YUV440P10LE, AV_PIX_FMT_YUV444P10LE,
  84. AV_PIX_FMT_YUV420P12LE, AV_PIX_FMT_YUV422P12LE,
  85. AV_PIX_FMT_YUV440P12LE, AV_PIX_FMT_YUV444P12LE,
  86. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  87. AV_PIX_FMT_YUVA420P10LE, AV_PIX_FMT_YUVA422P10LE, AV_PIX_FMT_YUVA444P10LE,
  88. AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
  89. AV_PIX_FMT_NONE
  90. };
  91. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  92. if (!fmts_list)
  93. return AVERROR(ENOMEM);
  94. return ff_set_common_formats(ctx, fmts_list);
  95. }
  96. static void lowpass_line_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp,
  97. ptrdiff_t mref, ptrdiff_t pref, int clip_max)
  98. {
  99. const uint8_t *srcp_above = srcp + mref;
  100. const uint8_t *srcp_below = srcp + pref;
  101. int i;
  102. for (i = 0; i < width; i++) {
  103. // this calculation is an integer representation of
  104. // '0.5 * current + 0.25 * above + 0.25 * below'
  105. // '1 +' is for rounding.
  106. dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
  107. }
  108. }
  109. static void lowpass_line_c_16(uint8_t *dst8, ptrdiff_t width, const uint8_t *src8,
  110. ptrdiff_t mref, ptrdiff_t pref, int clip_max)
  111. {
  112. uint16_t *dstp = (uint16_t *)dst8;
  113. const uint16_t *srcp = (const uint16_t *)src8;
  114. const uint16_t *srcp_above = srcp + mref / 2;
  115. const uint16_t *srcp_below = srcp + pref / 2;
  116. int i, src_x;
  117. for (i = 0; i < width; i++) {
  118. // this calculation is an integer representation of
  119. // '0.5 * current + 0.25 * above + 0.25 * below'
  120. // '1 +' is for rounding.
  121. src_x = av_le2ne16(srcp[i]) << 1;
  122. dstp[i] = av_le2ne16((1 + src_x + av_le2ne16(srcp_above[i])
  123. + av_le2ne16(srcp_below[i])) >> 2);
  124. }
  125. }
  126. static void lowpass_line_complex_c(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp,
  127. ptrdiff_t mref, ptrdiff_t pref, int clip_max)
  128. {
  129. const uint8_t *srcp_above = srcp + mref;
  130. const uint8_t *srcp_below = srcp + pref;
  131. const uint8_t *srcp_above2 = srcp + mref * 2;
  132. const uint8_t *srcp_below2 = srcp + pref * 2;
  133. int i, src_x, src_ab;
  134. for (i = 0; i < width; i++) {
  135. // this calculation is an integer representation of
  136. // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
  137. // '4 +' is for rounding.
  138. src_x = srcp[i] << 1;
  139. src_ab = srcp_above[i] + srcp_below[i];
  140. dstp[i] = av_clip_uint8((4 + ((srcp[i] + src_x + src_ab) << 1)
  141. - srcp_above2[i] - srcp_below2[i]) >> 3);
  142. // Prevent over-sharpening:
  143. // dst must not exceed src when the average of above and below
  144. // is less than src. And the other way around.
  145. if (src_ab > src_x) {
  146. if (dstp[i] < srcp[i])
  147. dstp[i] = srcp[i];
  148. } else if (dstp[i] > srcp[i])
  149. dstp[i] = srcp[i];
  150. }
  151. }
  152. static void lowpass_line_complex_c_16(uint8_t *dst8, ptrdiff_t width, const uint8_t *src8,
  153. ptrdiff_t mref, ptrdiff_t pref, int clip_max)
  154. {
  155. uint16_t *dstp = (uint16_t *)dst8;
  156. const uint16_t *srcp = (const uint16_t *)src8;
  157. const uint16_t *srcp_above = srcp + mref / 2;
  158. const uint16_t *srcp_below = srcp + pref / 2;
  159. const uint16_t *srcp_above2 = srcp + mref;
  160. const uint16_t *srcp_below2 = srcp + pref;
  161. int i, dst_le, src_le, src_x, src_ab;
  162. for (i = 0; i < width; i++) {
  163. // this calculation is an integer representation of
  164. // '0.75 * current + 0.25 * above + 0.25 * below - 0.125 * above2 - 0.125 * below2'
  165. // '4 +' is for rounding.
  166. src_le = av_le2ne16(srcp[i]);
  167. src_x = src_le << 1;
  168. src_ab = av_le2ne16(srcp_above[i]) + av_le2ne16(srcp_below[i]);
  169. dst_le = av_clip((4 + ((src_le + src_x + src_ab) << 1)
  170. - av_le2ne16(srcp_above2[i])
  171. - av_le2ne16(srcp_below2[i])) >> 3, 0, clip_max);
  172. // Prevent over-sharpening:
  173. // dst must not exceed src when the average of above and below
  174. // is less than src. And the other way around.
  175. if (src_ab > src_x) {
  176. if (dst_le < src_le)
  177. dstp[i] = av_le2ne16(src_le);
  178. else
  179. dstp[i] = av_le2ne16(dst_le);
  180. } else if (dst_le > src_le) {
  181. dstp[i] = av_le2ne16(src_le);
  182. } else
  183. dstp[i] = av_le2ne16(dst_le);
  184. }
  185. }
  186. static av_cold void uninit(AVFilterContext *ctx)
  187. {
  188. TInterlaceContext *tinterlace = ctx->priv;
  189. av_frame_free(&tinterlace->cur );
  190. av_frame_free(&tinterlace->next);
  191. av_freep(&tinterlace->black_data[0]);
  192. }
  193. static int config_out_props(AVFilterLink *outlink)
  194. {
  195. AVFilterContext *ctx = outlink->src;
  196. AVFilterLink *inlink = outlink->src->inputs[0];
  197. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
  198. TInterlaceContext *tinterlace = ctx->priv;
  199. int i;
  200. tinterlace->vsub = desc->log2_chroma_h;
  201. outlink->w = inlink->w;
  202. outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD || tinterlace->mode == MODE_MERGEX2?
  203. inlink->h*2 : inlink->h;
  204. if (tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD || tinterlace->mode == MODE_MERGEX2)
  205. outlink->sample_aspect_ratio = av_mul_q(inlink->sample_aspect_ratio,
  206. av_make_q(2, 1));
  207. if (tinterlace->mode == MODE_PAD) {
  208. uint8_t black[4] = { 0, 0, 0, 16 };
  209. int ret;
  210. ff_draw_init(&tinterlace->draw, outlink->format, 0);
  211. ff_draw_color(&tinterlace->draw, &tinterlace->color, black);
  212. if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
  213. tinterlace->color.comp[0].u8[0] = 0;
  214. ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
  215. outlink->w, outlink->h, outlink->format, 16);
  216. if (ret < 0)
  217. return ret;
  218. ff_fill_rectangle(&tinterlace->draw, &tinterlace->color, tinterlace->black_data,
  219. tinterlace->black_linesize, 0, 0, outlink->w, outlink->h);
  220. }
  221. if (tinterlace->flags & (TINTERLACE_FLAG_VLPF | TINTERLACE_FLAG_CVLPF)
  222. && !(tinterlace->mode == MODE_INTERLEAVE_TOP
  223. || tinterlace->mode == MODE_INTERLEAVE_BOTTOM)) {
  224. av_log(ctx, AV_LOG_WARNING, "low_pass_filter flags ignored with mode %d\n",
  225. tinterlace->mode);
  226. tinterlace->flags &= ~(TINTERLACE_FLAG_VLPF | TINTERLACE_FLAG_CVLPF);
  227. }
  228. tinterlace->preout_time_base = inlink->time_base;
  229. if (tinterlace->mode == MODE_INTERLACEX2) {
  230. tinterlace->preout_time_base.den *= 2;
  231. outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){2,1});
  232. outlink->time_base = av_mul_q(inlink->time_base , (AVRational){1,2});
  233. } else if (tinterlace->mode == MODE_MERGEX2) {
  234. outlink->frame_rate = inlink->frame_rate;
  235. outlink->time_base = inlink->time_base;
  236. } else if (tinterlace->mode != MODE_PAD) {
  237. outlink->frame_rate = av_mul_q(inlink->frame_rate, (AVRational){1,2});
  238. outlink->time_base = av_mul_q(inlink->time_base , (AVRational){2,1});
  239. }
  240. for (i = 0; i<FF_ARRAY_ELEMS(standard_tbs); i++){
  241. if (!av_cmp_q(standard_tbs[i], outlink->time_base))
  242. break;
  243. }
  244. if (i == FF_ARRAY_ELEMS(standard_tbs) ||
  245. (tinterlace->flags & TINTERLACE_FLAG_EXACT_TB))
  246. outlink->time_base = tinterlace->preout_time_base;
  247. tinterlace->csp = av_pix_fmt_desc_get(outlink->format);
  248. if (tinterlace->flags & TINTERLACE_FLAG_CVLPF) {
  249. if (tinterlace->csp->comp[0].depth > 8)
  250. tinterlace->lowpass_line = lowpass_line_complex_c_16;
  251. else
  252. tinterlace->lowpass_line = lowpass_line_complex_c;
  253. if (ARCH_X86)
  254. ff_tinterlace_init_x86(tinterlace);
  255. } else if (tinterlace->flags & TINTERLACE_FLAG_VLPF) {
  256. if (tinterlace->csp->comp[0].depth > 8)
  257. tinterlace->lowpass_line = lowpass_line_c_16;
  258. else
  259. tinterlace->lowpass_line = lowpass_line_c;
  260. if (ARCH_X86)
  261. ff_tinterlace_init_x86(tinterlace);
  262. }
  263. av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n", tinterlace->mode,
  264. (tinterlace->flags & TINTERLACE_FLAG_CVLPF) ? "complex" :
  265. (tinterlace->flags & TINTERLACE_FLAG_VLPF) ? "linear" : "off",
  266. inlink->h, outlink->h);
  267. return 0;
  268. }
  269. #define FIELD_UPPER 0
  270. #define FIELD_LOWER 1
  271. #define FIELD_UPPER_AND_LOWER 2
  272. /**
  273. * Copy picture field from src to dst.
  274. *
  275. * @param src_field copy from upper, lower field or both
  276. * @param interleave leave a padding line between each copied line
  277. * @param dst_field copy to upper or lower field,
  278. * only meaningful when interleave is selected
  279. * @param flags context flags
  280. */
  281. static inline
  282. void copy_picture_field(TInterlaceContext *tinterlace,
  283. uint8_t *dst[4], int dst_linesize[4],
  284. const uint8_t *src[4], int src_linesize[4],
  285. enum AVPixelFormat format, int w, int src_h,
  286. int src_field, int interleave, int dst_field,
  287. int flags)
  288. {
  289. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
  290. int hsub = desc->log2_chroma_w;
  291. int plane, vsub = desc->log2_chroma_h;
  292. int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
  293. int h;
  294. for (plane = 0; plane < desc->nb_components; plane++) {
  295. int lines = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(src_h, vsub) : src_h;
  296. int cols = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT( w, hsub) : w;
  297. uint8_t *dstp = dst[plane];
  298. const uint8_t *srcp = src[plane];
  299. int srcp_linesize = src_linesize[plane] * k;
  300. int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);
  301. int clip_max = (1 << tinterlace->csp->comp[plane].depth) - 1;
  302. lines = (lines + (src_field == FIELD_UPPER)) / k;
  303. if (src_field == FIELD_LOWER)
  304. srcp += src_linesize[plane];
  305. if (interleave && dst_field == FIELD_LOWER)
  306. dstp += dst_linesize[plane];
  307. // Low-pass filtering is required when creating an interlaced destination from
  308. // a progressive source which contains high-frequency vertical detail.
  309. // Filtering will reduce interlace 'twitter' and Moire patterning.
  310. if (flags & (TINTERLACE_FLAG_VLPF | TINTERLACE_FLAG_CVLPF)) {
  311. int x = !!(flags & TINTERLACE_FLAG_CVLPF);
  312. for (h = lines; h > 0; h--) {
  313. ptrdiff_t pref = src_linesize[plane];
  314. ptrdiff_t mref = -pref;
  315. if (h >= (lines - x)) mref = 0; // there is no line above
  316. else if (h <= (1 + x)) pref = 0; // there is no line below
  317. tinterlace->lowpass_line(dstp, cols, srcp, mref, pref, clip_max);
  318. dstp += dstp_linesize;
  319. srcp += srcp_linesize;
  320. }
  321. } else {
  322. if (tinterlace->csp->comp[plane].depth > 8)
  323. cols *= 2;
  324. av_image_copy_plane(dstp, dstp_linesize, srcp, srcp_linesize, cols, lines);
  325. }
  326. }
  327. }
  328. static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
  329. {
  330. AVFilterContext *ctx = inlink->dst;
  331. AVFilterLink *outlink = ctx->outputs[0];
  332. TInterlaceContext *tinterlace = ctx->priv;
  333. AVFrame *cur, *next, *out;
  334. int field, tff, ret;
  335. av_frame_free(&tinterlace->cur);
  336. tinterlace->cur = tinterlace->next;
  337. tinterlace->next = picref;
  338. cur = tinterlace->cur;
  339. next = tinterlace->next;
  340. /* we need at least two frames */
  341. if (!tinterlace->cur)
  342. return 0;
  343. switch (tinterlace->mode) {
  344. case MODE_MERGEX2: /* move the odd frame into the upper field of the new image, even into
  345. * the lower field, generating a double-height video at same framerate */
  346. case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
  347. * the lower field, generating a double-height video at half framerate */
  348. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  349. if (!out)
  350. return AVERROR(ENOMEM);
  351. av_frame_copy_props(out, cur);
  352. out->height = outlink->h;
  353. out->interlaced_frame = 1;
  354. out->top_field_first = 1;
  355. out->sample_aspect_ratio = av_mul_q(cur->sample_aspect_ratio, av_make_q(2, 1));
  356. /* write odd frame lines into the upper field of the new frame */
  357. copy_picture_field(tinterlace, out->data, out->linesize,
  358. (const uint8_t **)cur->data, cur->linesize,
  359. inlink->format, inlink->w, inlink->h,
  360. FIELD_UPPER_AND_LOWER, 1, tinterlace->mode == MODE_MERGEX2 ? (1 + inlink->frame_count_out) & 1 ? FIELD_LOWER : FIELD_UPPER : FIELD_UPPER, tinterlace->flags);
  361. /* write even frame lines into the lower field of the new frame */
  362. copy_picture_field(tinterlace, out->data, out->linesize,
  363. (const uint8_t **)next->data, next->linesize,
  364. inlink->format, inlink->w, inlink->h,
  365. FIELD_UPPER_AND_LOWER, 1, tinterlace->mode == MODE_MERGEX2 ? (1 + inlink->frame_count_out) & 1 ? FIELD_UPPER : FIELD_LOWER : FIELD_LOWER, tinterlace->flags);
  366. if (tinterlace->mode != MODE_MERGEX2)
  367. av_frame_free(&tinterlace->next);
  368. break;
  369. case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
  370. case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
  371. out = av_frame_clone(tinterlace->mode == MODE_DROP_EVEN ? cur : next);
  372. if (!out)
  373. return AVERROR(ENOMEM);
  374. av_frame_free(&tinterlace->next);
  375. break;
  376. case MODE_PAD: /* expand each frame to double height, but pad alternate
  377. * lines with black; framerate unchanged */
  378. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  379. if (!out)
  380. return AVERROR(ENOMEM);
  381. av_frame_copy_props(out, cur);
  382. out->height = outlink->h;
  383. out->sample_aspect_ratio = av_mul_q(cur->sample_aspect_ratio, av_make_q(2, 1));
  384. field = (1 + outlink->frame_count_in) & 1 ? FIELD_UPPER : FIELD_LOWER;
  385. /* copy upper and lower fields */
  386. copy_picture_field(tinterlace, out->data, out->linesize,
  387. (const uint8_t **)cur->data, cur->linesize,
  388. inlink->format, inlink->w, inlink->h,
  389. FIELD_UPPER_AND_LOWER, 1, field, tinterlace->flags);
  390. /* pad with black the other field */
  391. copy_picture_field(tinterlace, out->data, out->linesize,
  392. (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
  393. inlink->format, inlink->w, inlink->h,
  394. FIELD_UPPER_AND_LOWER, 1, !field, tinterlace->flags);
  395. break;
  396. /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
  397. * halving the frame rate and preserving image height */
  398. case MODE_INTERLEAVE_TOP: /* top field first */
  399. case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
  400. if ((tinterlace->flags & TINTERLACE_FLAG_BYPASS_IL) && cur->interlaced_frame) {
  401. av_log(ctx, AV_LOG_WARNING,
  402. "video is already interlaced, adjusting framerate only\n");
  403. out = av_frame_clone(cur);
  404. if (!out)
  405. return AVERROR(ENOMEM);
  406. out->pts /= 2; // adjust pts to new framerate
  407. ret = ff_filter_frame(outlink, out);
  408. return ret;
  409. }
  410. tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
  411. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  412. if (!out)
  413. return AVERROR(ENOMEM);
  414. av_frame_copy_props(out, cur);
  415. out->interlaced_frame = 1;
  416. out->top_field_first = tff;
  417. /* copy upper/lower field from cur */
  418. copy_picture_field(tinterlace, out->data, out->linesize,
  419. (const uint8_t **)cur->data, cur->linesize,
  420. inlink->format, inlink->w, inlink->h,
  421. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
  422. tinterlace->flags);
  423. /* copy lower/upper field from next */
  424. copy_picture_field(tinterlace, out->data, out->linesize,
  425. (const uint8_t **)next->data, next->linesize,
  426. inlink->format, inlink->w, inlink->h,
  427. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
  428. tinterlace->flags);
  429. av_frame_free(&tinterlace->next);
  430. break;
  431. case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
  432. /* output current frame first */
  433. out = av_frame_clone(cur);
  434. if (!out)
  435. return AVERROR(ENOMEM);
  436. out->interlaced_frame = 1;
  437. if (cur->pts != AV_NOPTS_VALUE)
  438. out->pts = cur->pts*2;
  439. out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
  440. if ((ret = ff_filter_frame(outlink, out)) < 0)
  441. return ret;
  442. /* output mix of current and next frame */
  443. tff = next->top_field_first;
  444. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  445. if (!out)
  446. return AVERROR(ENOMEM);
  447. av_frame_copy_props(out, next);
  448. out->interlaced_frame = 1;
  449. out->top_field_first = !tff;
  450. if (next->pts != AV_NOPTS_VALUE && cur->pts != AV_NOPTS_VALUE)
  451. out->pts = cur->pts + next->pts;
  452. else
  453. out->pts = AV_NOPTS_VALUE;
  454. /* write current frame second field lines into the second field of the new frame */
  455. copy_picture_field(tinterlace, out->data, out->linesize,
  456. (const uint8_t **)cur->data, cur->linesize,
  457. inlink->format, inlink->w, inlink->h,
  458. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
  459. tinterlace->flags);
  460. /* write next frame first field lines into the first field of the new frame */
  461. copy_picture_field(tinterlace, out->data, out->linesize,
  462. (const uint8_t **)next->data, next->linesize,
  463. inlink->format, inlink->w, inlink->h,
  464. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER,
  465. tinterlace->flags);
  466. break;
  467. default:
  468. av_assert0(0);
  469. }
  470. out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
  471. ret = ff_filter_frame(outlink, out);
  472. return ret;
  473. }
  474. static int init_interlace(AVFilterContext *ctx)
  475. {
  476. TInterlaceContext *tinterlace = ctx->priv;
  477. if (tinterlace->mode <= MODE_BFF)
  478. tinterlace->mode += MODE_INTERLEAVE_TOP;
  479. tinterlace->flags |= TINTERLACE_FLAG_BYPASS_IL;
  480. if (tinterlace->lowpass == VLPF_LIN)
  481. tinterlace->flags |= TINTERLACE_FLAG_VLPF;
  482. if (tinterlace->lowpass == VLPF_CMP)
  483. tinterlace->flags |= TINTERLACE_FLAG_CVLPF;
  484. return 0;
  485. }
  486. static const AVFilterPad tinterlace_inputs[] = {
  487. {
  488. .name = "default",
  489. .type = AVMEDIA_TYPE_VIDEO,
  490. .filter_frame = filter_frame,
  491. },
  492. { NULL }
  493. };
  494. static const AVFilterPad tinterlace_outputs[] = {
  495. {
  496. .name = "default",
  497. .type = AVMEDIA_TYPE_VIDEO,
  498. .config_props = config_out_props,
  499. },
  500. { NULL }
  501. };
  502. AVFilter ff_vf_tinterlace = {
  503. .name = "tinterlace",
  504. .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
  505. .priv_size = sizeof(TInterlaceContext),
  506. .uninit = uninit,
  507. .query_formats = query_formats,
  508. .inputs = tinterlace_inputs,
  509. .outputs = tinterlace_outputs,
  510. .priv_class = &tinterlace_class,
  511. };
  512. AVFilter ff_vf_interlace = {
  513. .name = "interlace",
  514. .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
  515. .priv_size = sizeof(TInterlaceContext),
  516. .init = init_interlace,
  517. .uninit = uninit,
  518. .query_formats = query_formats,
  519. .inputs = tinterlace_inputs,
  520. .outputs = tinterlace_outputs,
  521. .priv_class = &interlace_class,
  522. };