vf_tinterlace.c 25 KB

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