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.

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