2 * Copyright (c) 2012 Rudolf Polzer
3 * Copyright (c) 2013 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * @file telecine filter, heavily based from mpv-player:TOOLS/vf_dlopen/telecine.c by
27 #include "libavutil/avstring.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
40 unsigned int pattern_pos;
56 #define OFFSET(x) offsetof(TelecineContext, x)
57 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
59 static const AVOption telecine_options[] = {
60 {"first_field", "select first field", OFFSET(first_field), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "field"},
61 {"top", "select top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
62 {"t", "select top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
63 {"bottom", "select bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
64 {"b", "select bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
65 {"pattern", "pattern that describe for how many fields a frame is to be displayed", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str="23"}, 0, 0, FLAGS},
69 AVFILTER_DEFINE_CLASS(telecine);
71 static av_cold int init(AVFilterContext *ctx)
73 TelecineContext *tc = ctx->priv;
77 if (!strlen(tc->pattern)) {
78 av_log(ctx, AV_LOG_ERROR, "No pattern provided.\n");
79 return AVERROR_INVALIDDATA;
82 for (p = tc->pattern; *p; p++) {
83 if (!av_isdigit(*p)) {
84 av_log(ctx, AV_LOG_ERROR, "Provided pattern includes non-numeric characters.\n");
85 return AVERROR_INVALIDDATA;
88 max = FFMAX(*p - '0', max);
90 tc->pts.den += *p - '0';
93 tc->out_cnt = (max + 1) / 2;
94 av_log(ctx, AV_LOG_INFO, "Telecine pattern %s yields up to %d frames per frame, pts advance factor: %d/%d\n",
95 tc->pattern, tc->out_cnt, tc->pts.num, tc->pts.den);
100 static int query_formats(AVFilterContext *ctx)
102 AVFilterFormats *pix_fmts = NULL;
105 for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
106 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
107 if (!(desc->flags & PIX_FMT_HWACCEL))
108 ff_add_format(&pix_fmts, fmt);
111 ff_set_common_formats(ctx, pix_fmts);
115 static int config_input(AVFilterLink *inlink)
117 TelecineContext *tc = inlink->dst->priv;
118 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
121 tc->temp = ff_get_video_buffer(inlink, inlink->w, inlink->h);
123 return AVERROR(ENOMEM);
124 for (i = 0; i < tc->out_cnt; i++) {
125 tc->frame[i] = ff_get_video_buffer(inlink, inlink->w, inlink->h);
127 return AVERROR(ENOMEM);
130 if ((ret = av_image_fill_linesizes(tc->stride, inlink->format, inlink->w)) < 0)
133 tc->planeheight[1] = tc->planeheight[2] = inlink->h >> desc->log2_chroma_h;
134 tc->planeheight[0] = tc->planeheight[3] = inlink->h;
136 tc->nb_planes = av_pix_fmt_count_planes(inlink->format);
141 static int config_output(AVFilterLink *outlink)
143 AVFilterContext *ctx = outlink->src;
144 TelecineContext *tc = ctx->priv;
145 const AVFilterLink *inlink = ctx->inputs[0];
146 AVRational fps = inlink->frame_rate;
148 if (!fps.num || !fps.den) {
149 av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; "
150 "current rate of %d/%d is invalid\n", fps.num, fps.den);
151 return AVERROR(EINVAL);
153 fps = av_mul_q(fps, av_inv_q(tc->pts));
154 av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n",
155 inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den);
157 outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
158 outlink->frame_rate = fps;
159 outlink->time_base = av_mul_q(inlink->time_base, tc->pts);
161 tc->ts_unit = av_q2d(av_inv_q(av_mul_q(fps, outlink->time_base)));
166 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
168 AVFilterContext *ctx = inlink->dst;
169 AVFilterLink *outlink = ctx->outputs[0];
170 TelecineContext *tc = ctx->priv;
171 int i, len, ret = 0, nout = 0;
173 len = tc->pattern[tc->pattern_pos] - '0';
176 if (!tc->pattern[tc->pattern_pos])
179 if (!len) { // do not output any field from this frame
180 av_frame_free(&inpicref);
185 for (i = 0; i < tc->nb_planes; i++) {
186 // fill in the EARLIER field from the buffered pic
187 av_image_copy_plane(tc->frame[nout]->data[i] + tc->frame[nout]->linesize[i] * tc->first_field,
188 tc->frame[nout]->linesize[i] * 2,
189 tc->temp->data[i] + tc->temp->linesize[i] * tc->first_field,
190 tc->temp->linesize[i] * 2,
192 (tc->planeheight[i] - tc->first_field + 1) / 2);
193 // fill in the LATER field from the new pic
194 av_image_copy_plane(tc->frame[nout]->data[i] + tc->frame[nout]->linesize[i] * !tc->first_field,
195 tc->frame[nout]->linesize[i] * 2,
196 inpicref->data[i] + inpicref->linesize[i] * !tc->first_field,
197 inpicref->linesize[i] * 2,
199 (tc->planeheight[i] - !tc->first_field + 1) / 2);
207 // output THIS image as-is
208 for (i = 0; i < tc->nb_planes; i++)
209 av_image_copy_plane(tc->frame[nout]->data[i], tc->frame[nout]->linesize[i],
210 inpicref->data[i], inpicref->linesize[i],
218 // copy THIS image to the buffer, we need it later
219 for (i = 0; i < tc->nb_planes; i++)
220 av_image_copy_plane(tc->temp->data[i], tc->temp->linesize[i],
221 inpicref->data[i], inpicref->linesize[i],
227 for (i = 0; i < nout; i++) {
228 AVFrame *frame = av_frame_clone(tc->frame[i]);
231 av_frame_free(&inpicref);
232 return AVERROR(ENOMEM);
235 av_frame_copy_props(frame, inpicref);
236 frame->pts = tc->frame_count++ * tc->ts_unit;
237 ret = ff_filter_frame(outlink, frame);
239 av_frame_free(&inpicref);
244 static av_cold void uninit(AVFilterContext *ctx)
246 TelecineContext *tc = ctx->priv;
249 av_frame_free(&tc->temp);
250 for (i = 0; i < tc->out_cnt; i++)
251 av_frame_free(&tc->frame[i]);
254 static const AVFilterPad telecine_inputs[] = {
257 .type = AVMEDIA_TYPE_VIDEO,
258 .filter_frame = filter_frame,
259 .config_props = config_input,
264 static const AVFilterPad telecine_outputs[] = {
267 .type = AVMEDIA_TYPE_VIDEO,
268 .config_props = config_output,
273 AVFilter avfilter_vf_telecine = {
275 .description = NULL_IF_CONFIG_SMALL("Apply a telecine pattern."),
276 .priv_size = sizeof(TelecineContext),
277 .priv_class = &telecine_class,
280 .query_formats = query_formats,
281 .inputs = telecine_inputs,
282 .outputs = telecine_outputs,