vf_tinterlace.c 12 KB

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