FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_telecine.c
Date: 2024-04-20 14:10:07
Exec Total Coverage
Lines: 120 138 87.0%
Functions: 6 6 100.0%
Branches: 47 62 75.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2012 Rudolf Polzer
3 * Copyright (c) 2013 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file telecine filter, heavily based from mpv-player:TOOLS/vf_dlopen/telecine.c by
24 * Rudolf Polzer.
25 */
26
27 #include "libavutil/avstring.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "avfilter.h"
32 #include "filters.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "video.h"
36
37 typedef struct TelecineContext {
38 const AVClass *class;
39 int first_field;
40 char *pattern;
41 unsigned int pattern_pos;
42 int64_t start_time;
43
44 AVRational pts;
45 AVRational ts_unit;
46 int out_cnt;
47 int occupied;
48
49 int nb_planes;
50 int planeheight[4];
51 int stride[4];
52
53 AVFrame *frame[5];
54 AVFrame *temp;
55 } TelecineContext;
56
57 #define OFFSET(x) offsetof(TelecineContext, x)
58 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
59
60 static const AVOption telecine_options[] = {
61 {"first_field", "select first field", OFFSET(first_field), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, .unit = "field"},
62 {"top", "select top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "field"},
63 {"t", "select top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "field"},
64 {"bottom", "select bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "field"},
65 {"b", "select bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "field"},
66 {"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},
67 {NULL}
68 };
69
70 AVFILTER_DEFINE_CLASS(telecine);
71
72 36 static av_cold int init(AVFilterContext *ctx)
73 {
74 36 TelecineContext *s = ctx->priv;
75 const char *p;
76 36 int max = 0;
77
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (!strlen(s->pattern)) {
79 av_log(ctx, AV_LOG_ERROR, "No pattern provided.\n");
80 return AVERROR_INVALIDDATA;
81 }
82
83
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 36 times.
108 for (p = s->pattern; *p; p++) {
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
72 if (!av_isdigit(*p)) {
85 av_log(ctx, AV_LOG_ERROR, "Provided pattern includes non-numeric characters.\n");
86 return AVERROR_INVALIDDATA;
87 }
88
89 72 max = FFMAX(*p - '0', max);
90 72 s->pts.num += 2;
91 72 s->pts.den += *p - '0';
92 }
93
94 36 s->start_time = AV_NOPTS_VALUE;
95
96 36 s->out_cnt = (max + 1) / 2;
97 36 av_log(ctx, AV_LOG_INFO, "Telecine pattern %s yields up to %d frames per frame, pts advance factor: %d/%d\n",
98 s->pattern, s->out_cnt, s->pts.num, s->pts.den);
99
100 36 return 0;
101 }
102
103 18 static int query_formats(AVFilterContext *ctx)
104 {
105 18 int reject_flags = AV_PIX_FMT_FLAG_BITSTREAM |
106 AV_PIX_FMT_FLAG_HWACCEL |
107 AV_PIX_FMT_FLAG_PAL;
108
109 18 return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, reject_flags));
110 }
111
112 18 static int config_input(AVFilterLink *inlink)
113 {
114 18 TelecineContext *s = inlink->dst->priv;
115 18 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
116 int i, ret;
117
118 18 s->temp = ff_get_video_buffer(inlink, inlink->w, inlink->h);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!s->temp)
120 return AVERROR(ENOMEM);
121
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 18 times.
54 for (i = 0; i < s->out_cnt; i++) {
122 36 s->frame[i] = ff_get_video_buffer(inlink, inlink->w, inlink->h);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (!s->frame[i])
124 return AVERROR(ENOMEM);
125 }
126
127
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if ((ret = av_image_fill_linesizes(s->stride, inlink->format, inlink->w)) < 0)
128 return ret;
129
130 18 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
131 18 s->planeheight[0] = s->planeheight[3] = inlink->h;
132
133 18 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
134
135 18 return 0;
136 }
137
138 18 static int config_output(AVFilterLink *outlink)
139 {
140 18 AVFilterContext *ctx = outlink->src;
141 18 TelecineContext *s = ctx->priv;
142 18 const AVFilterLink *inlink = ctx->inputs[0];
143 18 AVRational fps = inlink->frame_rate;
144
145
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 if (!fps.num || !fps.den) {
146 av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; "
147 "current rate of %d/%d is invalid\n", fps.num, fps.den);
148 return AVERROR(EINVAL);
149 }
150 18 fps = av_mul_q(fps, av_inv_q(s->pts));
151 18 av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n",
152 18 inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den);
153
154 18 outlink->frame_rate = fps;
155 18 outlink->time_base = av_mul_q(inlink->time_base, s->pts);
156 18 av_log(ctx, AV_LOG_VERBOSE, "TB: %d/%d -> %d/%d\n",
157 18 inlink->time_base.num, inlink->time_base.den, outlink->time_base.num, outlink->time_base.den);
158
159 18 s->ts_unit = av_inv_q(av_mul_q(fps, outlink->time_base));
160
161 18 return 0;
162 }
163
164 479 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
165 {
166 479 AVFilterContext *ctx = inlink->dst;
167 479 AVFilterLink *outlink = ctx->outputs[0];
168 479 TelecineContext *s = ctx->priv;
169 479 int i, len, ret = 0, nout = 0;
170
171
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 461 times.
479 if (s->start_time == AV_NOPTS_VALUE)
172 18 s->start_time = inpicref->pts;
173
174 479 len = s->pattern[s->pattern_pos] - '0';
175
176 479 s->pattern_pos++;
177
2/2
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 248 times.
479 if (!s->pattern[s->pattern_pos])
178 231 s->pattern_pos = 0;
179
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 479 times.
479 if (!len) { // do not output any field from this frame
181 av_frame_free(&inpicref);
182 return 0;
183 }
184
185
2/2
✓ Branch 0 taken 230 times.
✓ Branch 1 taken 249 times.
479 if (s->occupied) {
186 230 ret = ff_inlink_make_frame_writable(inlink, &s->frame[nout]);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 230 times.
230 if (ret < 0) {
188 av_frame_free(&inpicref);
189 return ret;
190 }
191
2/2
✓ Branch 0 taken 690 times.
✓ Branch 1 taken 230 times.
920 for (i = 0; i < s->nb_planes; i++) {
192 // fill in the EARLIER field from the buffered pic
193 690 av_image_copy_plane(s->frame[nout]->data[i] + s->frame[nout]->linesize[i] * s->first_field,
194 690 s->frame[nout]->linesize[i] * 2,
195 690 s->temp->data[i] + s->temp->linesize[i] * s->first_field,
196 690 s->temp->linesize[i] * 2,
197 s->stride[i],
198 690 (s->planeheight[i] - s->first_field + 1) / 2);
199 // fill in the LATER field from the new pic
200 690 av_image_copy_plane(s->frame[nout]->data[i] + s->frame[nout]->linesize[i] * !s->first_field,
201 690 s->frame[nout]->linesize[i] * 2,
202 690 inpicref->data[i] + inpicref->linesize[i] * !s->first_field,
203 690 inpicref->linesize[i] * 2,
204 s->stride[i],
205 690 (s->planeheight[i] - !s->first_field + 1) / 2);
206 }
207 #if FF_API_INTERLACED_FRAME
208 FF_DISABLE_DEPRECATION_WARNINGS
209 230 s->frame[nout]->interlaced_frame = 1;
210 230 s->frame[nout]->top_field_first = !s->first_field;
211 FF_ENABLE_DEPRECATION_WARNINGS
212 #endif
213 230 s->frame[nout]->flags |= AV_FRAME_FLAG_INTERLACED;
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 230 times.
230 if (s->first_field)
215 s->frame[nout]->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
216 else
217 230 s->frame[nout]->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
218 230 nout++;
219 230 len--;
220 230 s->occupied = 0;
221 }
222
223
2/2
✓ Branch 0 taken 358 times.
✓ Branch 1 taken 479 times.
837 while (len >= 2) {
224 // output THIS image as-is
225 358 ret = ff_inlink_make_frame_writable(inlink, &s->frame[nout]);
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 358 times.
358 if (ret < 0) {
227 av_frame_free(&inpicref);
228 return ret;
229 }
230
2/2
✓ Branch 0 taken 1074 times.
✓ Branch 1 taken 358 times.
1432 for (i = 0; i < s->nb_planes; i++)
231 1074 av_image_copy_plane(s->frame[nout]->data[i], s->frame[nout]->linesize[i],
232 1074 inpicref->data[i], inpicref->linesize[i],
233 s->stride[i],
234 s->planeheight[i]);
235 #if FF_API_INTERLACED_FRAME
236 FF_DISABLE_DEPRECATION_WARNINGS
237 358 s->frame[nout]->interlaced_frame = inpicref->interlaced_frame;
238 358 s->frame[nout]->top_field_first = inpicref->top_field_first;
239 FF_ENABLE_DEPRECATION_WARNINGS
240 #endif
241 358 s->frame[nout]->flags |= (inpicref->flags & (AV_FRAME_FLAG_INTERLACED | AV_FRAME_FLAG_TOP_FIELD_FIRST));
242 358 nout++;
243 358 len -= 2;
244 }
245
246
2/2
✓ Branch 0 taken 243 times.
✓ Branch 1 taken 236 times.
479 if (len >= 1) {
247 // copy THIS image to the buffer, we need it later
248
2/2
✓ Branch 0 taken 729 times.
✓ Branch 1 taken 243 times.
972 for (i = 0; i < s->nb_planes; i++)
249 729 av_image_copy_plane(s->temp->data[i], s->temp->linesize[i],
250 729 inpicref->data[i], inpicref->linesize[i],
251 s->stride[i],
252 s->planeheight[i]);
253 243 s->occupied = 1;
254 }
255
256
2/2
✓ Branch 0 taken 588 times.
✓ Branch 1 taken 479 times.
1067 for (i = 0; i < nout; i++) {
257 588 AVFrame *frame = av_frame_clone(s->frame[i]);
258
3/4
✓ Branch 0 taken 588 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 443 times.
✓ Branch 3 taken 145 times.
588 int interlaced = frame ? !!(frame->flags & AV_FRAME_FLAG_INTERLACED) : 0;
259
3/4
✓ Branch 0 taken 588 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 443 times.
✓ Branch 3 taken 145 times.
588 int tff = frame ? !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) : 0;
260
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 588 times.
588 if (!frame) {
262 av_frame_free(&inpicref);
263 return AVERROR(ENOMEM);
264 }
265
266 588 av_frame_copy_props(frame, inpicref);
267 #if FF_API_INTERLACED_FRAME
268 FF_DISABLE_DEPRECATION_WARNINGS
269 588 frame->interlaced_frame = interlaced;
270 588 frame->top_field_first = tff;
271 FF_ENABLE_DEPRECATION_WARNINGS
272 #endif
273
2/2
✓ Branch 0 taken 443 times.
✓ Branch 1 taken 145 times.
588 if (interlaced)
274 443 frame->flags |= AV_FRAME_FLAG_INTERLACED;
275 else
276 145 frame->flags &= ~AV_FRAME_FLAG_INTERLACED;
277
2/2
✓ Branch 0 taken 443 times.
✓ Branch 1 taken 145 times.
588 if (tff)
278 443 frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
279 else
280 145 frame->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
281
1/2
✓ Branch 0 taken 588 times.
✗ Branch 1 not taken.
588 frame->pts = ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time) +
282 588 av_rescale(outlink->frame_count_in, s->ts_unit.num,
283 588 s->ts_unit.den);
284 588 ret = ff_filter_frame(outlink, frame);
285 }
286 479 av_frame_free(&inpicref);
287
288 479 return ret;
289 }
290
291 36 static av_cold void uninit(AVFilterContext *ctx)
292 {
293 36 TelecineContext *s = ctx->priv;
294 int i;
295
296 36 av_frame_free(&s->temp);
297
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 36 times.
108 for (i = 0; i < s->out_cnt; i++)
298 72 av_frame_free(&s->frame[i]);
299 36 }
300
301 static const AVFilterPad telecine_inputs[] = {
302 {
303 .name = "default",
304 .type = AVMEDIA_TYPE_VIDEO,
305 .filter_frame = filter_frame,
306 .config_props = config_input,
307 },
308 };
309
310 static const AVFilterPad telecine_outputs[] = {
311 {
312 .name = "default",
313 .type = AVMEDIA_TYPE_VIDEO,
314 .config_props = config_output,
315 },
316 };
317
318 const AVFilter ff_vf_telecine = {
319 .name = "telecine",
320 .description = NULL_IF_CONFIG_SMALL("Apply a telecine pattern."),
321 .priv_size = sizeof(TelecineContext),
322 .priv_class = &telecine_class,
323 .init = init,
324 .uninit = uninit,
325 FILTER_INPUTS(telecine_inputs),
326 FILTER_OUTPUTS(telecine_outputs),
327 FILTER_QUERY_FUNC(query_formats),
328 };
329