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.

367 lines
14KB

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