1/* 2 * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com> 3 * Copyright (c) 2010 Baptiste Coudurier 4 * Copyright (c) 2011 Stefano Sabatini 5 * Copyright (c) 2013 Vittorio Giovara <vittorio.giovara@gmail.com> 6 * 7 * This file is part of FFmpeg. 8 * 9 * FFmpeg is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * FFmpeg is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License along 20 * with FFmpeg; if not, write to the Free Software Foundation, Inc., 21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 22 */ 23 24/** 25 * @file 26 * progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter 27 */ 28 29#include "libavutil/common.h" 30#include "libavutil/opt.h" 31#include "libavutil/imgutils.h" 32#include "libavutil/avassert.h" 33 34#include "formats.h" 35#include "avfilter.h" 36#include "internal.h" 37#include "version.h" 38#include "video.h" 39 40enum ScanMode { 41 MODE_TFF = 0, 42 MODE_BFF = 1, 43}; 44 45enum FieldType { 46 FIELD_UPPER = 0, 47 FIELD_LOWER = 1, 48}; 49 50typedef struct InterlaceContext { 51 const AVClass *class; 52 enum ScanMode scan; // top or bottom field first scanning 53#if FF_API_INTERLACE_LOWPASS_SET 54 int lowpass; // enable or disable low pass filterning 55#endif 56 AVFrame *cur, *next; // the two frames from which the new one is obtained 57} InterlaceContext; 58 59#define OFFSET(x) offsetof(InterlaceContext, x) 60#define V AV_OPT_FLAG_VIDEO_PARAM 61static const AVOption interlace_options[] = { 62 { "scan", "scanning mode", OFFSET(scan), 63 AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" }, 64 { "tff", "top field first", 0, 65 AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" }, 66 { "bff", "bottom field first", 0, 67 AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" }, 68#if FF_API_INTERLACE_LOWPASS_SET 69 { "lowpass", "(deprecated, this option is always set)", OFFSET(lowpass), 70 AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, .flags = V }, 71#endif 72 { NULL } 73}; 74 75AVFILTER_DEFINE_CLASS(interlace); 76 77static const enum AVPixelFormat formats_supported[] = { 78 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, 79 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P, 80 AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, 81 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE 82}; 83 84static int query_formats(AVFilterContext *ctx) 85{ 86 ff_set_common_formats(ctx, ff_make_format_list(formats_supported)); 87 return 0; 88} 89 90static av_cold void uninit(AVFilterContext *ctx) 91{ 92 InterlaceContext *s = ctx->priv; 93 94 av_frame_free(&s->cur); 95 av_frame_free(&s->next); 96} 97 98static int config_out_props(AVFilterLink *outlink) 99{ 100 AVFilterContext *ctx = outlink->src; 101 AVFilterLink *inlink = outlink->src->inputs[0]; 102 InterlaceContext *s = ctx->priv; 103 104#if FF_API_INTERLACE_LOWPASS_SET 105 if (!s->lowpass) 106 av_log(ctx, AV_LOG_WARNING, "This option is deprecated and always set.\n"); 107#endif 108 109 if (inlink->h < 2) { 110 av_log(ctx, AV_LOG_ERROR, "input video height is too small\n"); 111 return AVERROR_INVALIDDATA; 112 } 113 // same input size 114 outlink->w = inlink->w; 115 outlink->h = inlink->h; 116 outlink->time_base = inlink->time_base; 117 outlink->frame_rate = inlink->frame_rate; 118 // half framerate 119 outlink->time_base.num *= 2; 120 outlink->frame_rate.den *= 2; 121 outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP; 122 123 av_log(ctx, AV_LOG_VERBOSE, "%s interlacing\n", 124 s->scan == MODE_TFF ? "tff" : "bff"); 125 126 return 0; 127} 128 129static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame, 130 AVFilterLink *inlink, enum FieldType field_type) 131{ 132 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); 133 int vsub = desc->log2_chroma_h; 134 int plane, i, j; 135 136 for (plane = 0; plane < desc->nb_components; plane++) { 137 int lines = (plane == 1 || plane == 2) ? FF_CEIL_RSHIFT(inlink->h, vsub) : inlink->h; 138 int linesize = av_image_get_linesize(inlink->format, inlink->w, plane); 139 uint8_t *dstp = dst_frame->data[plane]; 140 const uint8_t *srcp = src_frame->data[plane]; 141 int srcp_linesize; 142 int dstp_linesize; 143 144 av_assert0(linesize >= 0); 145 146 lines = (lines + (field_type == FIELD_UPPER)) / 2; 147 if (field_type == FIELD_LOWER) 148 srcp += src_frame->linesize[plane]; 149 if (field_type == FIELD_LOWER) 150 dstp += dst_frame->linesize[plane]; 151 152 srcp_linesize = src_frame->linesize[plane] * 2; 153 dstp_linesize = dst_frame->linesize[plane] * 2; 154 for (j = lines; j > 0; j--) { 155 const uint8_t *srcp_above = srcp - src_frame->linesize[plane]; 156 const uint8_t *srcp_below = srcp + src_frame->linesize[plane]; 157 if (j == lines) 158 srcp_above = srcp; // there is no line above 159 if (j == 1) 160 srcp_below = srcp; // there is no line below 161 for (i = 0; i < linesize; i++) { 162 // this calculation is an integer representation of 163 // '0.5 * current + 0.25 * above + 0.25 * below' 164 // '1 +' is for rounding. 165 dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2; 166 } 167 dstp += dstp_linesize; 168 srcp += srcp_linesize; 169 } 170 } 171} 172 173static int filter_frame(AVFilterLink *inlink, AVFrame *buf) 174{ 175 AVFilterContext *ctx = inlink->dst; 176 AVFilterLink *outlink = ctx->outputs[0]; 177 InterlaceContext *s = ctx->priv; 178 AVFrame *out; 179 int tff, ret; 180 181 av_frame_free(&s->cur); 182 s->cur = s->next; 183 s->next = buf; 184 185 /* we need at least two frames */ 186 if (!s->cur || !s->next) 187 return 0; 188 189 if (s->cur->interlaced_frame) { 190 av_log(ctx, AV_LOG_WARNING, 191 "video is already interlaced, adjusting framerate only\n"); 192 out = av_frame_clone(s->cur); 193 if (!out) 194 return AVERROR(ENOMEM); 195 out->pts /= 2; // adjust pts to new framerate 196 ret = ff_filter_frame(outlink, out); 197 return ret; 198 } 199 200 tff = (s->scan == MODE_TFF); 201 out = ff_get_video_buffer(outlink, outlink->w, outlink->h); 202 if (!out) 203 return AVERROR(ENOMEM); 204 205 av_frame_copy_props(out, s->cur); 206 out->interlaced_frame = 1; 207 out->top_field_first = tff; 208 out->pts /= 2; // adjust pts to new framerate 209 210 /* copy upper/lower field from cur */ 211 copy_picture_field(s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER); 212 av_frame_free(&s->cur); 213 214 /* copy lower/upper field from next */ 215 copy_picture_field(s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER); 216 av_frame_free(&s->next); 217 218 ret = ff_filter_frame(outlink, out); 219 220 return ret; 221} 222 223static const AVFilterPad inputs[] = { 224 { 225 .name = "default", 226 .type = AVMEDIA_TYPE_VIDEO, 227 .filter_frame = filter_frame, 228 }, 229 { NULL } 230}; 231 232static const AVFilterPad outputs[] = { 233 { 234 .name = "default", 235 .type = AVMEDIA_TYPE_VIDEO, 236 .config_props = config_out_props, 237 }, 238 { NULL } 239}; 240 241AVFilter ff_vf_interlace = { 242 .name = "interlace", 243 .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."), 244 .uninit = uninit, 245 .priv_class = &interlace_class, 246 .priv_size = sizeof(InterlaceContext), 247 .query_formats = query_formats, 248 .inputs = inputs, 249 .outputs = outputs, 250}; 251