vf_tinterlace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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/imgutils.h"
  27. #include "libavutil/avassert.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30. enum TInterlaceMode {
  31. MODE_MERGE = 0,
  32. MODE_DROP_EVEN,
  33. MODE_DROP_ODD,
  34. MODE_PAD,
  35. MODE_INTERLEAVE_TOP,
  36. MODE_INTERLEAVE_BOTTOM,
  37. MODE_INTERLACEX2,
  38. };
  39. static const char *tinterlace_mode_str[] = {
  40. "merge",
  41. "drop_even",
  42. "drop_odd",
  43. "pad",
  44. "interleave_top",
  45. "interleave_bottom",
  46. "interlacex2",
  47. NULL
  48. };
  49. typedef struct {
  50. enum TInterlaceMode mode; ///< interlace mode selected
  51. int frame; ///< number of the output frame
  52. int vsub; ///< chroma vertical subsampling
  53. AVFilterBufferRef *cur;
  54. AVFilterBufferRef *next;
  55. uint8_t *black_data[4]; ///< buffer used to fill padded lines
  56. int black_linesize[4];
  57. } TInterlaceContext;
  58. #define FULL_SCALE_YUVJ_FORMATS \
  59. PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUVJ440P
  60. static enum PixelFormat full_scale_yuvj_pix_fmts[] = {
  61. FULL_SCALE_YUVJ_FORMATS, PIX_FMT_NONE
  62. };
  63. static int query_formats(AVFilterContext *ctx)
  64. {
  65. static const enum PixelFormat pix_fmts[] = {
  66. PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P,
  67. PIX_FMT_YUV444P, PIX_FMT_YUV410P, PIX_FMT_YUVA420P,
  68. PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
  69. PIX_FMT_NONE
  70. };
  71. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  72. return 0;
  73. }
  74. static av_cold int init(AVFilterContext *ctx, const char *args)
  75. {
  76. TInterlaceContext *tinterlace = ctx->priv;
  77. int i;
  78. char c;
  79. tinterlace->mode = MODE_MERGE;
  80. if (args) {
  81. if (sscanf(args, "%d%c", (int *)&tinterlace->mode, &c) == 1) {
  82. if (tinterlace->mode > 6) {
  83. av_log(ctx, AV_LOG_ERROR,
  84. "Invalid mode '%s', use an integer between 0 and 6\n", args);
  85. return AVERROR(EINVAL);
  86. }
  87. av_log(ctx, AV_LOG_WARNING,
  88. "Using numeric constant is deprecated, use symbolic values\n");
  89. } else {
  90. for (i = 0; tinterlace_mode_str[i]; i++) {
  91. if (!strcmp(tinterlace_mode_str[i], args)) {
  92. tinterlace->mode = i;
  93. break;
  94. }
  95. }
  96. if (!tinterlace_mode_str[i]) {
  97. av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s'\n", args);
  98. return AVERROR(EINVAL);
  99. }
  100. }
  101. }
  102. return 0;
  103. }
  104. static av_cold void uninit(AVFilterContext *ctx)
  105. {
  106. TInterlaceContext *tinterlace = ctx->priv;
  107. if (tinterlace->cur ) avfilter_unref_bufferp(&tinterlace->cur );
  108. if (tinterlace->next) avfilter_unref_bufferp(&tinterlace->next);
  109. av_freep(&tinterlace->black_data[0]);
  110. }
  111. static int config_out_props(AVFilterLink *outlink)
  112. {
  113. AVFilterContext *ctx = outlink->src;
  114. AVFilterLink *inlink = outlink->src->inputs[0];
  115. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[outlink->format];
  116. TInterlaceContext *tinterlace = ctx->priv;
  117. tinterlace->vsub = desc->log2_chroma_h;
  118. outlink->w = inlink->w;
  119. outlink->h = tinterlace->mode == MODE_MERGE || tinterlace->mode == MODE_PAD ?
  120. inlink->h*2 : inlink->h;
  121. if (tinterlace->mode == MODE_PAD) {
  122. uint8_t black[4] = { 16, 128, 128, 16 };
  123. int i, ret;
  124. if (ff_fmt_is_in(outlink->format, full_scale_yuvj_pix_fmts))
  125. black[0] = black[3] = 0;
  126. ret = av_image_alloc(tinterlace->black_data, tinterlace->black_linesize,
  127. outlink->w, outlink->h, outlink->format, 1);
  128. if (ret < 0)
  129. return ret;
  130. /* fill black picture with black */
  131. for (i = 0; i < 4 && tinterlace->black_data[i]; i++) {
  132. int h = i == 1 || i == 2 ? outlink->h >> desc->log2_chroma_h : outlink->h;
  133. memset(tinterlace->black_data[i], black[i],
  134. tinterlace->black_linesize[i] * h);
  135. }
  136. }
  137. av_log(ctx, AV_LOG_VERBOSE, "mode:%s h:%d -> h:%d\n",
  138. tinterlace_mode_str[tinterlace->mode], inlink->h, outlink->h);
  139. return 0;
  140. }
  141. #define FIELD_UPPER 0
  142. #define FIELD_LOWER 1
  143. #define FIELD_UPPER_AND_LOWER 2
  144. /**
  145. * Copy picture field from src to dst.
  146. *
  147. * @param src_field copy from upper, lower field or both
  148. * @param interleave leave a padding line between each copied line
  149. * @param dst_field copy to upper or lower field,
  150. * only meaningful when interleave is selected
  151. */
  152. static inline
  153. void copy_picture_field(uint8_t *dst[4], int dst_linesize[4],
  154. const uint8_t *src[4], int src_linesize[4],
  155. enum PixelFormat format, int w, int src_h,
  156. int src_field, int interleave, int dst_field)
  157. {
  158. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[format];
  159. int plane, vsub = desc->log2_chroma_h;
  160. int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;
  161. for (plane = 0; plane < desc->nb_components; plane++) {
  162. int lines = plane == 1 || plane == 2 ? src_h >> vsub : src_h;
  163. int linesize = av_image_get_linesize(format, w, plane);
  164. uint8_t *dstp = dst[plane];
  165. const uint8_t *srcp = src[plane];
  166. lines /= k;
  167. if (src_field == FIELD_LOWER)
  168. srcp += src_linesize[plane];
  169. if (interleave && dst_field == FIELD_LOWER)
  170. dstp += dst_linesize[plane];
  171. av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
  172. srcp, src_linesize[plane]*k, linesize, lines);
  173. }
  174. }
  175. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  176. {
  177. AVFilterContext *ctx = inlink->dst;
  178. TInterlaceContext *tinterlace = ctx->priv;
  179. avfilter_unref_buffer(tinterlace->cur);
  180. tinterlace->cur = tinterlace->next;
  181. tinterlace->next = picref;
  182. inlink->cur_buf = NULL;
  183. return 0;
  184. }
  185. static int end_frame(AVFilterLink *inlink)
  186. {
  187. AVFilterContext *ctx = inlink->dst;
  188. AVFilterLink *outlink = ctx->outputs[0];
  189. TInterlaceContext *tinterlace = ctx->priv;
  190. AVFilterBufferRef *cur = tinterlace->cur;
  191. AVFilterBufferRef *next = tinterlace->next;
  192. AVFilterBufferRef *out = NULL;
  193. int field, tff;
  194. /* we need at least two frames */
  195. if (!tinterlace->cur)
  196. return 0;
  197. switch (tinterlace->mode) {
  198. case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
  199. * the lower field, generating a double-height video at half framerate */
  200. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  201. avfilter_copy_buffer_ref_props(out, cur);
  202. out->video->h = outlink->h;
  203. out->video->interlaced = 1;
  204. out->video->top_field_first = 1;
  205. /* write odd frame lines into the upper field of the new frame */
  206. copy_picture_field(out->data, out->linesize,
  207. (const uint8_t **)cur->data, cur->linesize,
  208. inlink->format, inlink->w, inlink->h,
  209. FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER);
  210. /* write even frame lines into the lower field of the new frame */
  211. copy_picture_field(out->data, out->linesize,
  212. (const uint8_t **)next->data, next->linesize,
  213. inlink->format, inlink->w, inlink->h,
  214. FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER);
  215. avfilter_unref_bufferp(&tinterlace->next);
  216. break;
  217. case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
  218. case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
  219. out = avfilter_ref_buffer(tinterlace->mode == MODE_DROP_EVEN ? cur : next, AV_PERM_READ);
  220. avfilter_unref_bufferp(&tinterlace->next);
  221. break;
  222. case MODE_PAD: /* expand each frame to double height, but pad alternate
  223. * lines with black; framerate unchanged */
  224. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  225. avfilter_copy_buffer_ref_props(out, cur);
  226. out->video->h = outlink->h;
  227. field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
  228. /* copy upper and lower fields */
  229. copy_picture_field(out->data, out->linesize,
  230. (const uint8_t **)cur->data, cur->linesize,
  231. inlink->format, inlink->w, inlink->h,
  232. FIELD_UPPER_AND_LOWER, 1, field);
  233. /* pad with black the other field */
  234. copy_picture_field(out->data, out->linesize,
  235. (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
  236. inlink->format, inlink->w, inlink->h,
  237. FIELD_UPPER_AND_LOWER, 1, !field);
  238. break;
  239. /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
  240. * halving the frame rate and preserving image height */
  241. case MODE_INTERLEAVE_TOP: /* top field first */
  242. case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
  243. tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
  244. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  245. avfilter_copy_buffer_ref_props(out, cur);
  246. out->video->interlaced = 1;
  247. out->video->top_field_first = tff;
  248. /* copy upper/lower field from cur */
  249. copy_picture_field(out->data, out->linesize,
  250. (const uint8_t **)cur->data, cur->linesize,
  251. inlink->format, inlink->w, inlink->h,
  252. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
  253. /* copy lower/upper field from next */
  254. copy_picture_field(out->data, out->linesize,
  255. (const uint8_t **)next->data, next->linesize,
  256. inlink->format, inlink->w, inlink->h,
  257. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
  258. avfilter_unref_bufferp(&tinterlace->next);
  259. break;
  260. case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
  261. /* output current frame first */
  262. out = avfilter_ref_buffer(cur, ~AV_PERM_WRITE);
  263. out->video->interlaced = 1;
  264. ff_start_frame(outlink, out);
  265. ff_draw_slice(outlink, 0, outlink->h, 1);
  266. ff_end_frame(outlink);
  267. /* output mix of current and next frame */
  268. tff = next->video->top_field_first;
  269. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  270. avfilter_copy_buffer_ref_props(out, next);
  271. out->video->interlaced = 1;
  272. /* write current frame second field lines into the second field of the new frame */
  273. copy_picture_field(out->data, out->linesize,
  274. (const uint8_t **)cur->data, cur->linesize,
  275. inlink->format, inlink->w, inlink->h,
  276. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
  277. /* write next frame first field lines into the first field of the new frame */
  278. copy_picture_field(out->data, out->linesize,
  279. (const uint8_t **)next->data, next->linesize,
  280. inlink->format, inlink->w, inlink->h,
  281. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
  282. break;
  283. }
  284. ff_start_frame(outlink, out);
  285. ff_draw_slice(outlink, 0, outlink->h, 1);
  286. ff_end_frame(outlink);
  287. tinterlace->frame++;
  288. return 0;
  289. }
  290. static int poll_frame(AVFilterLink *outlink)
  291. {
  292. TInterlaceContext *tinterlace = outlink->src->priv;
  293. AVFilterLink *inlink = outlink->src->inputs[0];
  294. int ret, val;
  295. val = ff_poll_frame(inlink);
  296. if (val == 1 && !tinterlace->next) {
  297. if ((ret = ff_request_frame(inlink)) < 0)
  298. return ret;
  299. val = ff_poll_frame(inlink);
  300. }
  301. av_assert0(tinterlace->next);
  302. return val;
  303. }
  304. static int request_frame(AVFilterLink *outlink)
  305. {
  306. TInterlaceContext *tinterlace = outlink->src->priv;
  307. AVFilterLink *inlink = outlink->src->inputs[0];
  308. do {
  309. int ret;
  310. if ((ret = ff_request_frame(inlink)) < 0)
  311. return ret;
  312. } while (!tinterlace->cur);
  313. return 0;
  314. }
  315. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
  316. AVFilter avfilter_vf_tinterlace = {
  317. .name = "tinterlace",
  318. .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
  319. .priv_size = sizeof(TInterlaceContext),
  320. .init = init,
  321. .uninit = uninit,
  322. .query_formats = query_formats,
  323. .inputs = (const AVFilterPad[]) {
  324. { .name = "default",
  325. .type = AVMEDIA_TYPE_VIDEO,
  326. .start_frame = start_frame,
  327. .draw_slice = null_draw_slice,
  328. .end_frame = end_frame, },
  329. { .name = NULL}
  330. },
  331. .outputs = (const AVFilterPad[]) {
  332. { .name = "default",
  333. .type = AVMEDIA_TYPE_VIDEO,
  334. .config_props = config_out_props,
  335. .poll_frame = poll_frame,
  336. .request_frame = request_frame },
  337. { .name = NULL}
  338. },
  339. };