FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_freezeframes.c
Date: 2024-07-26 21:54:09
Exec Total Coverage
Lines: 0 57 0.0%
Functions: 0 3 0.0%
Branches: 0 52 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2019 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/avstring.h"
22 #include "libavutil/common.h"
23 #include "libavutil/internal.h"
24 #include "libavutil/opt.h"
25
26 #include "avfilter.h"
27 #include "filters.h"
28 #include "internal.h"
29 #include "video.h"
30
31 typedef struct FreezeFramesContext {
32 const AVClass *class;
33 int64_t first, last, replace;
34
35 AVFrame *replace_frame;
36 } FreezeFramesContext;
37
38 #define OFFSET(x) offsetof(FreezeFramesContext, x)
39 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
40
41 static const AVOption freezeframes_options[] = {
42 { "first", "set first frame to freeze", OFFSET(first), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
43 { "last", "set last frame to freeze", OFFSET(last), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
44 { "replace", "set frame to replace", OFFSET(replace), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
45 { NULL },
46 };
47
48 AVFILTER_DEFINE_CLASS(freezeframes);
49
50 static int config_output(AVFilterLink *outlink)
51 {
52 AVFilterContext *ctx = outlink->src;
53 AVFilterLink *sourcelink = ctx->inputs[0];
54 AVFilterLink *replacelink = ctx->inputs[1];
55
56 if (sourcelink->w != replacelink->w || sourcelink->h != replacelink->h) {
57 av_log(ctx, AV_LOG_ERROR,
58 "Input frame sizes do not match (%dx%d vs %dx%d).\n",
59 sourcelink->w, sourcelink->h,
60 replacelink->w, replacelink->h);
61 return AVERROR(EINVAL);
62 }
63
64 outlink->w = sourcelink->w;
65 outlink->h = sourcelink->h;
66 outlink->time_base = sourcelink->time_base;
67 outlink->sample_aspect_ratio = sourcelink->sample_aspect_ratio;
68 outlink->frame_rate = sourcelink->frame_rate;
69
70 return 0;
71 }
72
73 static int activate(AVFilterContext *ctx)
74 {
75 AVFilterLink *outlink = ctx->outputs[0];
76 FreezeFramesContext *s = ctx->priv;
77 AVFrame *frame = NULL;
78 int drop = ctx->inputs[0]->frame_count_out >= s->first &&
79 ctx->inputs[0]->frame_count_out <= s->last;
80 int replace = ctx->inputs[1]->frame_count_out == s->replace;
81 int ret;
82
83 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
84
85 if (drop && s->replace_frame) {
86 ret = ff_inlink_consume_frame(ctx->inputs[0], &frame);
87 if (ret < 0)
88 return ret;
89
90 if (frame) {
91 int64_t dropped_pts = frame->pts;
92
93 av_frame_free(&frame);
94 frame = av_frame_clone(s->replace_frame);
95 if (!frame)
96 return AVERROR(ENOMEM);
97 frame->pts = dropped_pts;
98 return ff_filter_frame(outlink, frame);
99 }
100 } else if (!drop) {
101 ret = ff_inlink_consume_frame(ctx->inputs[0], &frame);
102 if (ret < 0)
103 return ret;
104
105 if (frame)
106 return ff_filter_frame(outlink, frame);
107 }
108
109 ret = ff_inlink_consume_frame(ctx->inputs[1], &frame);
110 if (ret < 0)
111 return ret;
112 if (replace && frame) {
113 s->replace_frame = frame;
114 } else if (frame) {
115 av_frame_free(&frame);
116 }
117
118 FF_FILTER_FORWARD_STATUS(ctx->inputs[0], outlink);
119 FF_FILTER_FORWARD_STATUS(ctx->inputs[1], outlink);
120
121 if (!drop || (drop && s->replace_frame))
122 FF_FILTER_FORWARD_WANTED(outlink, ctx->inputs[0]);
123 if (!s->replace_frame)
124 FF_FILTER_FORWARD_WANTED(outlink, ctx->inputs[1]);
125
126 return FFERROR_NOT_READY;
127 }
128
129 static av_cold void uninit(AVFilterContext *ctx)
130 {
131 FreezeFramesContext *s = ctx->priv;
132
133 av_frame_free(&s->replace_frame);
134 }
135
136 static const AVFilterPad freezeframes_inputs[] = {
137 {
138 .name = "source",
139 .type = AVMEDIA_TYPE_VIDEO,
140 },
141 {
142 .name = "replace",
143 .type = AVMEDIA_TYPE_VIDEO,
144 },
145 };
146
147 static const AVFilterPad freezeframes_outputs[] = {
148 {
149 .name = "default",
150 .type = AVMEDIA_TYPE_VIDEO,
151 .config_props = config_output,
152 },
153 };
154
155 const AVFilter ff_vf_freezeframes = {
156 .name = "freezeframes",
157 .description = NULL_IF_CONFIG_SMALL("Freeze video frames."),
158 .priv_size = sizeof(FreezeFramesContext),
159 .priv_class = &freezeframes_class,
160 FILTER_INPUTS(freezeframes_inputs),
161 FILTER_OUTPUTS(freezeframes_outputs),
162 .activate = activate,
163 .uninit = uninit,
164 };
165