vf_tinterlace.c 14 KB

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