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.

392 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/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. 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. 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. return 0;
  183. }
  184. static int end_frame(AVFilterLink *inlink)
  185. {
  186. AVFilterContext *ctx = inlink->dst;
  187. AVFilterLink *outlink = ctx->outputs[0];
  188. TInterlaceContext *tinterlace = ctx->priv;
  189. AVFilterBufferRef *cur = tinterlace->cur;
  190. AVFilterBufferRef *next = tinterlace->next;
  191. AVFilterBufferRef *out = NULL;
  192. int field, tff;
  193. /* we need at least two frames */
  194. if (!tinterlace->cur)
  195. return 0;
  196. switch (tinterlace->mode) {
  197. case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
  198. * the lower field, generating a double-height video at half framerate */
  199. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  200. avfilter_copy_buffer_ref_props(out, cur);
  201. out->video->h = outlink->h;
  202. out->video->interlaced = 1;
  203. out->video->top_field_first = 1;
  204. /* write odd frame lines into the upper field of the new frame */
  205. copy_picture_field(out->data, out->linesize,
  206. cur->data, cur->linesize,
  207. inlink->format, inlink->w, inlink->h,
  208. FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER);
  209. /* write even frame lines into the lower field of the new frame */
  210. copy_picture_field(out->data, out->linesize,
  211. next->data, next->linesize,
  212. inlink->format, inlink->w, inlink->h,
  213. FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER);
  214. avfilter_unref_bufferp(&tinterlace->next);
  215. break;
  216. case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
  217. case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
  218. out = avfilter_ref_buffer(tinterlace->mode == MODE_DROP_EVEN ? cur : next, AV_PERM_READ);
  219. avfilter_unref_bufferp(&tinterlace->next);
  220. break;
  221. case MODE_PAD: /* expand each frame to double height, but pad alternate
  222. * lines with black; framerate unchanged */
  223. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  224. avfilter_copy_buffer_ref_props(out, cur);
  225. out->video->h = outlink->h;
  226. field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
  227. /* copy upper and lower fields */
  228. copy_picture_field(out->data, out->linesize,
  229. cur->data, cur->linesize,
  230. inlink->format, inlink->w, inlink->h,
  231. FIELD_UPPER_AND_LOWER, 1, field);
  232. /* pad with black the other field */
  233. copy_picture_field(out->data, out->linesize,
  234. tinterlace->black_data, tinterlace->black_linesize,
  235. inlink->format, inlink->w, inlink->h,
  236. FIELD_UPPER_AND_LOWER, 1, !field);
  237. break;
  238. /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
  239. * halving the frame rate and preserving image height */
  240. case MODE_INTERLEAVE_TOP: /* top field first */
  241. case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
  242. tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
  243. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  244. avfilter_copy_buffer_ref_props(out, cur);
  245. out->video->interlaced = 1;
  246. out->video->top_field_first = tff;
  247. /* copy upper/lower field from cur */
  248. copy_picture_field(out->data, out->linesize,
  249. cur->data, cur->linesize,
  250. inlink->format, inlink->w, inlink->h,
  251. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
  252. /* copy lower/upper field from next */
  253. copy_picture_field(out->data, out->linesize,
  254. next->data, next->linesize,
  255. inlink->format, inlink->w, inlink->h,
  256. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
  257. avfilter_unref_bufferp(&tinterlace->next);
  258. break;
  259. case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
  260. /* output current frame first */
  261. out = avfilter_ref_buffer(cur, ~AV_PERM_WRITE);
  262. out->video->interlaced = 1;
  263. ff_start_frame(outlink, out);
  264. ff_draw_slice(outlink, 0, outlink->h, 1);
  265. ff_end_frame(outlink);
  266. /* output mix of current and next frame */
  267. tff = next->video->top_field_first;
  268. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  269. avfilter_copy_buffer_ref_props(out, next);
  270. out->video->interlaced = 1;
  271. /* write current frame second field lines into the second field of the new frame */
  272. copy_picture_field(out->data, out->linesize,
  273. cur->data, cur->linesize,
  274. inlink->format, inlink->w, inlink->h,
  275. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
  276. /* write next frame first field lines into the first field of the new frame */
  277. copy_picture_field(out->data, out->linesize,
  278. next->data, next->linesize,
  279. inlink->format, inlink->w, inlink->h,
  280. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
  281. break;
  282. }
  283. ff_start_frame(outlink, out);
  284. ff_draw_slice(outlink, 0, outlink->h, 1);
  285. ff_end_frame(outlink);
  286. tinterlace->frame++;
  287. return 0;
  288. }
  289. static int poll_frame(AVFilterLink *outlink)
  290. {
  291. TInterlaceContext *tinterlace = outlink->src->priv;
  292. AVFilterLink *inlink = outlink->src->inputs[0];
  293. int ret, val;
  294. val = ff_poll_frame(inlink);
  295. if (val == 1 && !tinterlace->next) {
  296. if ((ret = ff_request_frame(inlink)) < 0)
  297. return ret;
  298. val = ff_poll_frame(inlink);
  299. }
  300. av_assert0(tinterlace->next);
  301. return val;
  302. }
  303. static int request_frame(AVFilterLink *outlink)
  304. {
  305. TInterlaceContext *tinterlace = outlink->src->priv;
  306. AVFilterLink *inlink = outlink->src->inputs[0];
  307. do {
  308. int ret;
  309. if ((ret = ff_request_frame(inlink)) < 0)
  310. return ret;
  311. } while (!tinterlace->cur);
  312. return 0;
  313. }
  314. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
  315. AVFilter avfilter_vf_tinterlace = {
  316. .name = "tinterlace",
  317. .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
  318. .priv_size = sizeof(TInterlaceContext),
  319. .init = init,
  320. .uninit = uninit,
  321. .query_formats = query_formats,
  322. .inputs = (const AVFilterPad[]) {
  323. { .name = "default",
  324. .type = AVMEDIA_TYPE_VIDEO,
  325. .start_frame = start_frame,
  326. .draw_slice = null_draw_slice,
  327. .end_frame = end_frame, },
  328. { .name = NULL}
  329. },
  330. .outputs = (const AVFilterPad[]) {
  331. { .name = "default",
  332. .type = AVMEDIA_TYPE_VIDEO,
  333. .config_props = config_out_props,
  334. .poll_frame = poll_frame,
  335. .request_frame = request_frame },
  336. { .name = NULL}
  337. },
  338. };