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.

325 lines
11KB

  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_bufferp(&tinterlace->cur );
  73. if (tinterlace->next) avfilter_unref_bufferp(&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 line
  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. avfilter_unref_buffer(tinterlace->cur);
  145. tinterlace->cur = tinterlace->next;
  146. tinterlace->next = picref;
  147. }
  148. static void end_frame(AVFilterLink *inlink)
  149. {
  150. AVFilterContext *ctx = inlink->dst;
  151. AVFilterLink *outlink = ctx->outputs[0];
  152. TInterlaceContext *tinterlace = ctx->priv;
  153. AVFilterBufferRef *cur = tinterlace->cur;
  154. AVFilterBufferRef *next = tinterlace->next;
  155. AVFilterBufferRef *out = NULL;
  156. int field, tff;
  157. /* we need at least two frames */
  158. if (!tinterlace->cur)
  159. return;
  160. switch (tinterlace->mode) {
  161. case 0: /* move the odd frame into the upper field of the new image, even into
  162. * the lower field, generating a double-height video at half framerate */
  163. out = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  164. avfilter_copy_buffer_ref_props(out, cur);
  165. out->video->h = outlink->h;
  166. out->video->interlaced = 1;
  167. out->video->top_field_first = 1;
  168. /* write odd frame lines into the upper field of the new frame */
  169. copy_picture_field(out->data, out->linesize,
  170. cur->data, cur->linesize,
  171. inlink->format, inlink->w, inlink->h,
  172. FIELD_UPPER_AND_LOWER, 1, FIELD_UPPER);
  173. /* write even frame lines into the lower field of the new frame */
  174. copy_picture_field(out->data, out->linesize,
  175. next->data, next->linesize,
  176. inlink->format, inlink->w, inlink->h,
  177. FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER);
  178. avfilter_unref_bufferp(&tinterlace->next);
  179. break;
  180. case 1: /* only output even frames, odd frames are dropped; height unchanged, half framerate */
  181. case 2: /* only output odd frames, even frames are dropped; height unchanged, half framerate */
  182. out = avfilter_ref_buffer(tinterlace->mode == 2 ? cur : next, AV_PERM_READ);
  183. avfilter_unref_bufferp(&tinterlace->next);
  184. break;
  185. case 3: /* expand each frame to double height, but pad alternate
  186. * lines with black; framerate unchanged */
  187. out = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  188. avfilter_copy_buffer_ref_props(out, cur);
  189. out->video->h = outlink->h;
  190. field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
  191. /* copy upper and lower fields */
  192. copy_picture_field(out->data, out->linesize,
  193. cur->data, cur->linesize,
  194. inlink->format, inlink->w, inlink->h,
  195. FIELD_UPPER_AND_LOWER, 1, field);
  196. /* pad with black the other field */
  197. copy_picture_field(out->data, out->linesize,
  198. tinterlace->black_data, tinterlace->black_linesize,
  199. inlink->format, inlink->w, inlink->h,
  200. FIELD_UPPER_AND_LOWER, 1, !field);
  201. break;
  202. /* interleave upper/lower lines from odd frames with lower/upper lines from even frames,
  203. * halving the frame rate and preserving image height */
  204. case 4: /* top field first */
  205. case 5: /* bottom field first */
  206. tff = tinterlace->mode == 4;
  207. out = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  208. avfilter_copy_buffer_ref_props(out, cur);
  209. out->video->interlaced = 1;
  210. out->video->top_field_first = tff;
  211. /* copy upper/lower field from cur */
  212. copy_picture_field(out->data, out->linesize,
  213. cur->data, cur->linesize,
  214. inlink->format, inlink->w, inlink->h,
  215. tff ? FIELD_UPPER : FIELD_LOWER, 1, tff ? FIELD_UPPER : FIELD_LOWER);
  216. /* copy lower/upper field from next */
  217. copy_picture_field(out->data, out->linesize,
  218. next->data, next->linesize,
  219. inlink->format, inlink->w, inlink->h,
  220. tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER);
  221. avfilter_unref_bufferp(&tinterlace->next);
  222. break;
  223. }
  224. avfilter_start_frame(outlink, out);
  225. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  226. avfilter_end_frame(outlink);
  227. tinterlace->frame++;
  228. }
  229. static int poll_frame(AVFilterLink *outlink)
  230. {
  231. TInterlaceContext *tinterlace = outlink->src->priv;
  232. AVFilterLink *inlink = outlink->src->inputs[0];
  233. int ret, val;
  234. val = avfilter_poll_frame(inlink);
  235. if (val == 1 && !tinterlace->next) {
  236. if ((ret = avfilter_request_frame(inlink)) < 0)
  237. return ret;
  238. val = avfilter_poll_frame(inlink);
  239. }
  240. assert(tinterlace->next);
  241. return val;
  242. }
  243. static int request_frame(AVFilterLink *outlink)
  244. {
  245. TInterlaceContext *tinterlace = outlink->src->priv;
  246. AVFilterLink *inlink = outlink->src->inputs[0];
  247. do {
  248. int ret;
  249. if ((ret = avfilter_request_frame(inlink)) < 0)
  250. return ret;
  251. } while (!tinterlace->cur);
  252. return 0;
  253. }
  254. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  255. AVFilter avfilter_vf_tinterlace = {
  256. .name = "tinterlace",
  257. .description = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
  258. .priv_size = sizeof(TInterlaceContext),
  259. .init = init,
  260. .uninit = uninit,
  261. .query_formats = query_formats,
  262. .inputs = (const AVFilterPad[]) {
  263. { .name = "default",
  264. .type = AVMEDIA_TYPE_VIDEO,
  265. .start_frame = start_frame,
  266. .draw_slice = null_draw_slice,
  267. .end_frame = end_frame, },
  268. { .name = NULL}
  269. },
  270. .outputs = (const AVFilterPad[]) {
  271. { .name = "default",
  272. .type = AVMEDIA_TYPE_VIDEO,
  273. .config_props = config_out_props,
  274. .poll_frame = poll_frame,
  275. .request_frame = request_frame },
  276. { .name = NULL}
  277. },
  278. };