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/lfg.h" |
22 |
|
|
#include "libavutil/opt.h" |
23 |
|
|
#include "libavutil/random_seed.h" |
24 |
|
|
#include "avfilter.h" |
25 |
|
|
#include "filters.h" |
26 |
|
|
|
27 |
|
|
#define MAX_FRAMES 512 |
28 |
|
|
|
29 |
|
|
typedef struct RandomContext { |
30 |
|
|
const AVClass *class; |
31 |
|
|
|
32 |
|
|
AVLFG lfg; |
33 |
|
|
int nb_frames; |
34 |
|
|
int64_t random_seed; |
35 |
|
|
int nb_frames_filled; |
36 |
|
|
AVFrame *frames[MAX_FRAMES]; |
37 |
|
|
int64_t pts[MAX_FRAMES]; |
38 |
|
|
int64_t duration[MAX_FRAMES]; |
39 |
|
|
int flush_idx; |
40 |
|
|
} RandomContext; |
41 |
|
|
|
42 |
|
|
#define OFFSET(x) offsetof(RandomContext, x) |
43 |
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
44 |
|
|
|
45 |
|
|
static const AVOption random_options[] = { |
46 |
|
|
{ "frames", "set number of frames in cache", OFFSET(nb_frames), AV_OPT_TYPE_INT, {.i64=30}, 2, MAX_FRAMES, FLAGS }, |
47 |
|
|
{ "seed", "set the seed", OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX, FLAGS }, |
48 |
|
|
{ NULL } |
49 |
|
|
}; |
50 |
|
|
|
51 |
|
|
AVFILTER_DEFINE_CLASS(random); |
52 |
|
|
|
53 |
|
✗ |
static av_cold int init(AVFilterContext *ctx) |
54 |
|
|
{ |
55 |
|
✗ |
RandomContext *s = ctx->priv; |
56 |
|
|
uint32_t seed; |
57 |
|
|
|
58 |
|
✗ |
if (s->random_seed < 0) |
59 |
|
✗ |
s->random_seed = av_get_random_seed(); |
60 |
|
✗ |
seed = s->random_seed; |
61 |
|
✗ |
av_lfg_init(&s->lfg, seed); |
62 |
|
|
|
63 |
|
✗ |
return 0; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
✗ |
static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
67 |
|
|
{ |
68 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
69 |
|
✗ |
RandomContext *s = ctx->priv; |
70 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
71 |
|
|
AVFrame *out; |
72 |
|
|
int idx; |
73 |
|
|
|
74 |
|
✗ |
if (s->nb_frames_filled < s->nb_frames) { |
75 |
|
✗ |
s->frames[s->nb_frames_filled] = in; |
76 |
|
✗ |
s->duration[s->nb_frames_filled] = in->duration; |
77 |
|
✗ |
s->pts[s->nb_frames_filled++] = in->pts; |
78 |
|
✗ |
return 0; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
✗ |
idx = av_lfg_get(&s->lfg) % s->nb_frames; |
82 |
|
|
|
83 |
|
✗ |
out = s->frames[idx]; |
84 |
|
✗ |
out->pts = s->pts[0]; |
85 |
|
✗ |
out->duration = s->duration[0]; |
86 |
|
✗ |
memmove(&s->pts[0], &s->pts[1], (s->nb_frames - 1) * sizeof(s->pts[0])); |
87 |
|
✗ |
memmove(&s->duration[0], &s->duration[1], (s->nb_frames - 1) * sizeof(s->duration[0])); |
88 |
|
✗ |
s->frames[idx] = in; |
89 |
|
✗ |
s->pts[s->nb_frames - 1] = in->pts; |
90 |
|
✗ |
s->duration[s->nb_frames - 1] = in->duration; |
91 |
|
|
|
92 |
|
✗ |
return ff_filter_frame(outlink, out); |
93 |
|
|
} |
94 |
|
|
|
95 |
|
✗ |
static int request_frame(AVFilterLink *outlink) |
96 |
|
|
{ |
97 |
|
✗ |
AVFilterContext *ctx = outlink->src; |
98 |
|
✗ |
RandomContext *s = ctx->priv; |
99 |
|
|
int ret; |
100 |
|
|
|
101 |
|
✗ |
ret = ff_request_frame(ctx->inputs[0]); |
102 |
|
|
|
103 |
|
✗ |
next: |
104 |
|
✗ |
if (ret == AVERROR_EOF && !ctx->is_disabled && s->nb_frames > 0) { |
105 |
|
✗ |
AVFrame *out = s->frames[s->nb_frames - 1]; |
106 |
|
✗ |
if (!out) { |
107 |
|
✗ |
s->nb_frames--; |
108 |
|
✗ |
goto next; |
109 |
|
|
} |
110 |
|
✗ |
out->duration = s->duration[s->flush_idx]; |
111 |
|
✗ |
out->pts = s->pts[s->flush_idx++]; |
112 |
|
✗ |
ret = ff_filter_frame(outlink, out); |
113 |
|
✗ |
s->frames[s->nb_frames - 1] = NULL; |
114 |
|
✗ |
s->nb_frames--; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
✗ |
return ret; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
✗ |
static av_cold void uninit(AVFilterContext *ctx) |
121 |
|
|
{ |
122 |
|
✗ |
RandomContext *s = ctx->priv; |
123 |
|
|
|
124 |
|
✗ |
for (int i = 0; i < s->nb_frames; i++) |
125 |
|
✗ |
av_frame_free(&s->frames[i]); |
126 |
|
✗ |
} |
127 |
|
|
|
128 |
|
|
static const AVFilterPad random_inputs[] = { |
129 |
|
|
{ |
130 |
|
|
.name = "default", |
131 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
132 |
|
|
.filter_frame = filter_frame, |
133 |
|
|
}, |
134 |
|
|
}; |
135 |
|
|
|
136 |
|
|
static const AVFilterPad random_outputs[] = { |
137 |
|
|
{ |
138 |
|
|
.name = "default", |
139 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
140 |
|
|
.request_frame = request_frame, |
141 |
|
|
}, |
142 |
|
|
}; |
143 |
|
|
|
144 |
|
|
const AVFilter ff_vf_random = { |
145 |
|
|
.name = "random", |
146 |
|
|
.description = NULL_IF_CONFIG_SMALL("Return random frames."), |
147 |
|
|
.priv_size = sizeof(RandomContext), |
148 |
|
|
.priv_class = &random_class, |
149 |
|
|
.init = init, |
150 |
|
|
.uninit = uninit, |
151 |
|
|
FILTER_INPUTS(random_inputs), |
152 |
|
|
FILTER_OUTPUTS(random_outputs), |
153 |
|
|
}; |
154 |
|
|
|