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
;
55 #define OFFSET(x) offsetof(TelecineContext, x)
56 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
58 static const AVOption telecine_options
[] = {
59 {"first_field", "select first field", OFFSET(first_field
), AV_OPT_TYPE_INT
, {.i64
=0}, 0, 1, FLAGS
, "field"},
60 {"top", "select top field first", 0, AV_OPT_TYPE_CONST
, {.i64
=0}, 0, 0, FLAGS
, "field"},
61 {"t", "select top field first", 0, AV_OPT_TYPE_CONST
, {.i64
=0}, 0, 0, FLAGS
, "field"},
62 {"bottom", "select bottom field first", 0, AV_OPT_TYPE_CONST
, {.i64
=1}, 0, 0, FLAGS
, "field"},
63 {"b", "select bottom field first", 0, AV_OPT_TYPE_CONST
, {.i64
=1}, 0, 0, FLAGS
, "field"},
64 {"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
},
68 AVFILTER_DEFINE_CLASS(telecine
);
70 static av_cold
int init(AVFilterContext
*ctx
)
72 TelecineContext
*tc
= ctx
->priv
;
76 if (!strlen(tc
->pattern
)) {
77 av_log(ctx
, AV_LOG_ERROR
, "No pattern provided.\n");
78 return AVERROR_INVALIDDATA
;
81 for (p
= tc
->pattern
; *p
; p
++) {
82 if (!av_isdigit(*p
)) {
83 av_log(ctx
, AV_LOG_ERROR
, "Provided pattern includes non-numeric characters.\n");
84 return AVERROR_INVALIDDATA
;
87 max
= FFMAX(*p
- '0', max
);
89 tc
->pts
.den
+= *p
- '0';
92 tc
->out_cnt
= (max
+ 1) / 2;
93 av_log(ctx
, AV_LOG_INFO
, "Telecine pattern %s yields up to %d frames per frame, pts advance factor: %d/%d\n",
94 tc
->pattern
, tc
->out_cnt
, tc
->pts
.num
, tc
->pts
.den
);
99 static int query_formats(AVFilterContext
*ctx
)
101 AVFilterFormats
*pix_fmts
= NULL
;
104 for (fmt
= 0; av_pix_fmt_desc_get(fmt
); fmt
++) {
105 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(fmt
);
106 if (!(desc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
||
107 desc
->flags
& AV_PIX_FMT_FLAG_PAL
||
108 desc
->flags
& AV_PIX_FMT_FLAG_BITSTREAM
))
109 ff_add_format(&pix_fmts
, fmt
);
112 ff_set_common_formats(ctx
, pix_fmts
);
116 static int config_input(AVFilterLink
*inlink
)
118 TelecineContext
*tc
= inlink
->dst
->priv
;
119 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(inlink
->format
);
122 tc
->temp
= ff_get_video_buffer(inlink
, inlink
->w
, inlink
->h
);
124 return AVERROR(ENOMEM
);
125 for (i
= 0; i
< tc
->out_cnt
; i
++) {
126 tc
->frame
[i
] = ff_get_video_buffer(inlink
, inlink
->w
, inlink
->h
);
128 return AVERROR(ENOMEM
);
131 if ((ret
= av_image_fill_linesizes(tc
->stride
, inlink
->format
, inlink
->w
)) < 0)
134 tc
->planeheight
[1] = tc
->planeheight
[2] = FF_CEIL_RSHIFT(inlink
->h
, desc
->log2_chroma_h
);
135 tc
->planeheight
[0] = tc
->planeheight
[3] = inlink
->h
;
137 tc
->nb_planes
= av_pix_fmt_count_planes(inlink
->format
);
142 static int config_output(AVFilterLink
*outlink
)
144 AVFilterContext
*ctx
= outlink
->src
;
145 TelecineContext
*tc
= ctx
->priv
;
146 const AVFilterLink
*inlink
= ctx
->inputs
[0];
147 AVRational fps
= inlink
->frame_rate
;
149 if (!fps
.num
|| !fps
.den
) {
150 av_log(ctx
, AV_LOG_ERROR
, "The input needs a constant frame rate; "
151 "current rate of %d/%d is invalid\n", fps
.num
, fps
.den
);
152 return AVERROR(EINVAL
);
154 fps
= av_mul_q(fps
, av_inv_q(tc
->pts
));
155 av_log(ctx
, AV_LOG_VERBOSE
, "FPS: %d/%d -> %d/%d\n",
156 inlink
->frame_rate
.num
, inlink
->frame_rate
.den
, fps
.num
, fps
.den
);
158 outlink
->flags
|= FF_LINK_FLAG_REQUEST_LOOP
;
159 outlink
->frame_rate
= fps
;
160 outlink
->time_base
= av_mul_q(inlink
->time_base
, tc
->pts
);
161 av_log(ctx
, AV_LOG_VERBOSE
, "TB: %d/%d -> %d/%d\n",
162 inlink
->time_base
.num
, inlink
->time_base
.den
, outlink
->time_base
.num
, outlink
->time_base
.den
);
164 tc
->ts_unit
= av_q2d(av_inv_q(av_mul_q(fps
, outlink
->time_base
)));
169 static int filter_frame(AVFilterLink
*inlink
, AVFrame
*inpicref
)
171 AVFilterContext
*ctx
= inlink
->dst
;
172 AVFilterLink
*outlink
= ctx
->outputs
[0];
173 TelecineContext
*tc
= ctx
->priv
;
174 int i
, len
, ret
= 0, nout
= 0;
176 len
= tc
->pattern
[tc
->pattern_pos
] - '0';
179 if (!tc
->pattern
[tc
->pattern_pos
])
182 if (!len
) { // do not output any field from this frame
183 av_frame_free(&inpicref
);
188 for (i
= 0; i
< tc
->nb_planes
; i
++) {
189 // fill in the EARLIER field from the buffered pic
190 av_image_copy_plane(tc
->frame
[nout
]->data
[i
] + tc
->frame
[nout
]->linesize
[i
] * tc
->first_field
,
191 tc
->frame
[nout
]->linesize
[i
] * 2,
192 tc
->temp
->data
[i
] + tc
->temp
->linesize
[i
] * tc
->first_field
,
193 tc
->temp
->linesize
[i
] * 2,
195 (tc
->planeheight
[i
] - tc
->first_field
+ 1) / 2);
196 // fill in the LATER field from the new pic
197 av_image_copy_plane(tc
->frame
[nout
]->data
[i
] + tc
->frame
[nout
]->linesize
[i
] * !tc
->first_field
,
198 tc
->frame
[nout
]->linesize
[i
] * 2,
199 inpicref
->data
[i
] + inpicref
->linesize
[i
] * !tc
->first_field
,
200 inpicref
->linesize
[i
] * 2,
202 (tc
->planeheight
[i
] - !tc
->first_field
+ 1) / 2);
210 // output THIS image as-is
211 for (i
= 0; i
< tc
->nb_planes
; i
++)
212 av_image_copy_plane(tc
->frame
[nout
]->data
[i
], tc
->frame
[nout
]->linesize
[i
],
213 inpicref
->data
[i
], inpicref
->linesize
[i
],
221 // copy THIS image to the buffer, we need it later
222 for (i
= 0; i
< tc
->nb_planes
; i
++)
223 av_image_copy_plane(tc
->temp
->data
[i
], tc
->temp
->linesize
[i
],
224 inpicref
->data
[i
], inpicref
->linesize
[i
],
230 for (i
= 0; i
< nout
; i
++) {
231 AVFrame
*frame
= av_frame_clone(tc
->frame
[i
]);
234 av_frame_free(&inpicref
);
235 return AVERROR(ENOMEM
);
238 frame
->pts
= outlink
->frame_count
* tc
->ts_unit
;
239 ret
= ff_filter_frame(outlink
, frame
);
241 av_frame_free(&inpicref
);
246 static av_cold
void uninit(AVFilterContext
*ctx
)
248 TelecineContext
*tc
= ctx
->priv
;
251 av_frame_free(&tc
->temp
);
252 for (i
= 0; i
< tc
->out_cnt
; i
++)
253 av_frame_free(&tc
->frame
[i
]);
256 static const AVFilterPad telecine_inputs
[] = {
259 .type
= AVMEDIA_TYPE_VIDEO
,
260 .filter_frame
= filter_frame
,
261 .config_props
= config_input
,
266 static const AVFilterPad telecine_outputs
[] = {
269 .type
= AVMEDIA_TYPE_VIDEO
,
270 .config_props
= config_output
,
275 AVFilter ff_vf_telecine
= {
277 .description
= NULL_IF_CONFIG_SMALL("Apply a telecine pattern."),
278 .priv_size
= sizeof(TelecineContext
),
279 .priv_class
= &telecine_class
,
282 .query_formats
= query_formats
,
283 .inputs
= telecine_inputs
,
284 .outputs
= telecine_outputs
,