FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_shuffleframes.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 49 57 86.0%
Functions: 3 3 100.0%
Branches: 28 40 70.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2015 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/mem.h"
25 #include "libavutil/opt.h"
26
27 #include "avfilter.h"
28 #include "filters.h"
29 #include "video.h"
30
31 typedef struct ShuffleFramesContext {
32 const AVClass *class;
33 char *mapping;
34 AVFrame **frames;
35 int *map;
36 int64_t *pts;
37 int in_frames;
38 int nb_frames;
39 } ShuffleFramesContext;
40
41 2 static av_cold int init(AVFilterContext *ctx)
42 {
43 2 ShuffleFramesContext *s = ctx->priv;
44 2 char *mapping, *saveptr = NULL, *p;
45 int n, nb_items;
46
47 2 nb_items = 1;
48
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
12 for (p = s->mapping; *p; p++) {
49
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
10 if (*p == '|' || *p == ' ')
50 4 nb_items++;
51 }
52
53 2 s->frames = av_calloc(nb_items, sizeof(*s->frames));
54 2 s->map = av_calloc(nb_items, sizeof(*s->map));
55 2 s->pts = av_calloc(nb_items, sizeof(*s->pts));
56
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 if (!s->map || !s->frames || !s->pts) {
57 return AVERROR(ENOMEM);
58 }
59
60 2 mapping = av_strdup(s->mapping);
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!mapping)
62 return AVERROR(ENOMEM);
63
64
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (n = 0; n < nb_items; n++) {
65
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 char *map = av_strtok(n == 0 ? mapping : NULL, " |", &saveptr);
66
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (!map || sscanf(map, "%d", &s->map[n]) != 1) {
67 av_free(mapping);
68 return AVERROR(EINVAL);
69 }
70
71
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (s->map[n] < -1 || s->map[n] >= nb_items) {
72 av_log(ctx, AV_LOG_ERROR, "Index %d out of range: [-1, %d].\n", s->map[n], nb_items - 1);
73 av_free(mapping);
74 return AVERROR(EINVAL);
75 }
76 }
77
78 2 s->nb_frames = nb_items;
79 2 av_free(mapping);
80 2 return 0;
81 }
82
83 50 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
84 {
85 50 AVFilterContext *ctx = inlink->dst;
86 50 ShuffleFramesContext *s = ctx->priv;
87 50 int ret = 0;
88
89
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (s->in_frames < s->nb_frames) {
90 50 s->frames[s->in_frames] = frame;
91 50 s->pts[s->in_frames] = frame->pts;
92 50 s->in_frames++;
93 }
94
95
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 34 times.
50 if (s->in_frames == s->nb_frames) {
96 int n, x;
97
98
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 16 times.
64 for (n = 0; n < s->nb_frames; n++) {
99 AVFrame *out;
100
101 48 x = s->map[n];
102
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 if (x >= 0) {
103 48 out = av_frame_clone(s->frames[x]);
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (!out)
105 return AVERROR(ENOMEM);
106 48 out->pts = s->pts[n];
107 48 ret = ff_filter_frame(ctx->outputs[0], out);
108 }
109 48 s->in_frames--;
110 }
111
112
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 16 times.
64 for (n = 0; n < s->nb_frames; n++)
113 48 av_frame_free(&s->frames[n]);
114 }
115
116 50 return ret;
117 }
118
119 2 static av_cold void uninit(AVFilterContext *ctx)
120 {
121 2 ShuffleFramesContext *s = ctx->priv;
122
123
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 while (s->in_frames > 0) {
124 2 s->in_frames--;
125 2 av_frame_free(&s->frames[s->in_frames]);
126 }
127
128 2 av_freep(&s->frames);
129 2 av_freep(&s->map);
130 2 av_freep(&s->pts);
131 2 }
132
133 #define OFFSET(x) offsetof(ShuffleFramesContext, x)
134 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
135 static const AVOption shuffleframes_options[] = {
136 { "mapping", "set destination indexes of input frames", OFFSET(mapping), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
137 { NULL },
138 };
139
140 AVFILTER_DEFINE_CLASS(shuffleframes);
141
142 static const AVFilterPad shuffleframes_inputs[] = {
143 {
144 .name = "default",
145 .type = AVMEDIA_TYPE_VIDEO,
146 .filter_frame = filter_frame,
147 },
148 };
149
150 const AVFilter ff_vf_shuffleframes = {
151 .name = "shuffleframes",
152 .description = NULL_IF_CONFIG_SMALL("Shuffle video frames."),
153 .priv_size = sizeof(ShuffleFramesContext),
154 .priv_class = &shuffleframes_class,
155 .init = init,
156 .uninit = uninit,
157 FILTER_INPUTS(shuffleframes_inputs),
158 FILTER_OUTPUTS(ff_video_default_filterpad),
159 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
160 };
161