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.

378 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. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
  60. static enum AVPixelFormat full_scale_yuvj_pix_fmts[] = {
  61. FULL_SCALE_YUVJ_FORMATS, AV_PIX_FMT_NONE
  62. };
  63. static int query_formats(AVFilterContext *ctx)
  64. {
  65. static const enum AVPixelFormat pix_fmts[] = {
  66. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  67. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
  68. AV_PIX_FMT_GRAY8, FULL_SCALE_YUVJ_FORMATS,
  69. AV_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_desc_get(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. const uint8_t *src[4], int src_linesize[4],
  155. enum AVPixelFormat format, int w, int src_h,
  156. int src_field, int interleave, int dst_field)
  157. {
  158. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(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. const uint8_t *srcp = src[plane];
  166. if (linesize < 0)
  167. return;
  168. lines /= k;
  169. if (src_field == FIELD_LOWER)
  170. srcp += src_linesize[plane];
  171. if (interleave && dst_field == FIELD_LOWER)
  172. dstp += dst_linesize[plane];
  173. av_image_copy_plane(dstp, dst_linesize[plane] * (interleave ? 2 : 1),
  174. srcp, src_linesize[plane]*k, linesize, lines);
  175. }
  176. }
  177. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  178. {
  179. AVFilterContext *ctx = inlink->dst;
  180. TInterlaceContext *tinterlace = ctx->priv;
  181. avfilter_unref_buffer(tinterlace->cur);
  182. tinterlace->cur = tinterlace->next;
  183. tinterlace->next = picref;
  184. inlink->cur_buf = NULL;
  185. return 0;
  186. }
  187. static int end_frame(AVFilterLink *inlink)
  188. {
  189. AVFilterContext *ctx = inlink->dst;
  190. AVFilterLink *outlink = ctx->outputs[0];
  191. TInterlaceContext *tinterlace = ctx->priv;
  192. AVFilterBufferRef *cur = tinterlace->cur;
  193. AVFilterBufferRef *next = tinterlace->next;
  194. AVFilterBufferRef *out = NULL;
  195. int field, tff;
  196. /* we need at least two frames */
  197. if (!tinterlace->cur)
  198. return 0;
  199. switch (tinterlace->mode) {
  200. case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
  201. * the lower field, generating a double-height video at half framerate */
  202. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  203. avfilter_copy_buffer_ref_props(out, cur);
  204. out->video->h = outlink->h;
  205. out->video->interlaced = 1;
  206. out->video->top_field_first = 1;
  207. /* write odd frame lines into the upper field of the new frame */
  208. copy_picture_field(out->data, out->linesize,
  209. (const uint8_t **)cur->data, cur->linesize,
  210. inlink->format, inlink->w, inlink->h,
  211. FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER);
  212. /* write even frame lines into the lower field of the new frame */
  213. copy_picture_field(out->data, out->linesize,
  214. (const uint8_t **)next->data, next->linesize,
  215. inlink->format, inlink->w, inlink->h,
  216. FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER);
  217. avfilter_unref_bufferp(&tinterlace->next);
  218. break;
  219. case MODE_DROP_ODD: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
  220. case MODE_DROP_EVEN: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
  221. out = avfilter_ref_buffer(tinterlace->mode == MODE_DROP_EVEN ? cur : next, AV_PERM_READ);
  222. avfilter_unref_bufferp(&tinterlace->next);
  223. break;
  224. case MODE_PAD: /* expand each frame to double height, but pad alternate
  225. * lines with black; framerate unchanged */
  226. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  227. avfilter_copy_buffer_ref_props(out, cur);
  228. out->video->h = outlink->h;
  229. field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
  230. /* copy upper and lower fields */
  231. copy_picture_field(out->data, out->linesize,
  232. (const uint8_t **)cur->data, cur->linesize,
  233. inlink->format, inlink->w, inlink->h,
  234. FIELD_UPPER_AND_LOWER, 1, field);
  235. /* pad with black the other field */
  236. copy_picture_field(out->data, out->linesize,
  237. (const uint8_t **)tinterlace->black_data, tinterlace->black_linesize,
  238. inlink->format, inlink->w, inlink->h,
  239. FIELD_UPPER_AND_LOWER, 1, !field);
  240. break;
  241. /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
  242. * halving the frame rate and preserving image height */
  243. case MODE_INTERLEAVE_TOP: /* top field first */
  244. case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
  245. tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
  246. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  247. avfilter_copy_buffer_ref_props(out, cur);
  248. out->video->interlaced = 1;
  249. out->video->top_field_first = tff;
  250. /* copy upper/lower field from cur */
  251. copy_picture_field(out->data, out->linesize,
  252. (const uint8_t **)cur->data, cur->linesize,
  253. inlink->format, inlink->w, inlink->h,
  254. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
  255. /* copy lower/upper field from next */
  256. copy_picture_field(out->data, out->linesize,
  257. (const uint8_t **)next->data, next->linesize,
  258. inlink->format, inlink->w, inlink->h,
  259. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
  260. avfilter_unref_bufferp(&tinterlace->next);
  261. break;
  262. case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
  263. /* output current frame first */
  264. out = avfilter_ref_buffer(cur, ~AV_PERM_WRITE);
  265. out->video->interlaced = 1;
  266. ff_start_frame(outlink, out);
  267. ff_draw_slice(outlink, 0, outlink->h, 1);
  268. ff_end_frame(outlink);
  269. /* output mix of current and next frame */
  270. tff = next->video->top_field_first;
  271. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  272. avfilter_copy_buffer_ref_props(out, next);
  273. out->video->interlaced = 1;
  274. /* write current frame second field lines into the second field of the new frame */
  275. copy_picture_field(out->data, out->linesize,
  276. (const uint8_t **)cur->data, cur->linesize,
  277. inlink->format, inlink->w, inlink->h,
  278. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
  279. /* write next frame first field lines into the first field of the new frame */
  280. copy_picture_field(out->data, out->linesize,
  281. (const uint8_t **)next->data, next->linesize,
  282. inlink->format, inlink->w, inlink->h,
  283. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
  284. break;
  285. }
  286. ff_start_frame(outlink, out);
  287. ff_draw_slice(outlink, 0, outlink->h, 1);
  288. ff_end_frame(outlink);
  289. tinterlace->frame++;
  290. return 0;
  291. }
  292. static int request_frame(AVFilterLink *outlink)
  293. {
  294. TInterlaceContext *tinterlace = outlink->src->priv;
  295. AVFilterLink *inlink = outlink->src->inputs[0];
  296. do {
  297. int ret;
  298. if ((ret = ff_request_frame(inlink)) < 0)
  299. return ret;
  300. } while (!tinterlace->cur);
  301. return 0;
  302. }
  303. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
  304. AVFilter avfilter_vf_tinterlace = {
  305. .name = "tinterlace",
  306. .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
  307. .priv_size = sizeof(TInterlaceContext),
  308. .init = init,
  309. .uninit = uninit,
  310. .query_formats = query_formats,
  311. .inputs = (const AVFilterPad[]) {
  312. { .name = "default",
  313. .type = AVMEDIA_TYPE_VIDEO,
  314. .start_frame = start_frame,
  315. .draw_slice = null_draw_slice,
  316. .end_frame = end_frame, },
  317. { .name = NULL}
  318. },
  319. .outputs = (const AVFilterPad[]) {
  320. { .name = "default",
  321. .type = AVMEDIA_TYPE_VIDEO,
  322. .config_props = config_out_props,
  323. .request_frame = request_frame },
  324. { .name = NULL}
  325. },
  326. };