Blender V5.3
ffmpeg_swscale.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#ifdef WITH_FFMPEG
10# include "ffmpeg_swscale.hh"
11
12# include <cstdint>
13# include <mutex>
14
15# include "BLI_mutex.hh"
16# include "BLI_vector.hh"
17
18# include "movie_util.hh"
19
20extern "C" {
21# include <libavutil/opt.h>
22# include <libavutil/pixfmt.h>
23# include <libswscale/swscale.h>
24
25# include "ffmpeg_compat.h"
26}
27
28namespace blender {
29
30/* libswscale context creation and destruction is expensive.
31 * Maintain a cache of already created contexts. */
32
33static constexpr int64_t swscale_cache_max_entries = 32;
34
35struct SwscaleContext {
36 int src_width = 0, src_height = 0;
37 int dst_width = 0, dst_height = 0;
38 AVPixelFormat src_format = AV_PIX_FMT_NONE, dst_format = AV_PIX_FMT_NONE;
39 bool src_full_range = false, dst_full_range = false;
40 int src_colorspace = -1, dst_colorspace = -1;
41 int flags = 0;
42
43 SwsContext *context = nullptr;
44 int64_t last_use_timestamp = 0;
45 bool is_used = false;
46};
47
48static Mutex swscale_cache_lock;
49static int64_t swscale_cache_timestamp = 0;
50static Vector<SwscaleContext> *swscale_cache = nullptr;
51
52static SwsContext *sws_create_context(int src_width,
53 int src_height,
54 int av_src_format,
55 int dst_width,
56 int dst_height,
57 int av_dst_format,
58 int sws_flags)
59{
60# if defined(FFMPEG_SWSCALE_THREADING)
61 /* sws_getContext does not allow passing flags that ask for multi-threaded
62 * scaling context, so do it the hard way. */
63 SwsContext *c = sws_alloc_context();
64 if (c == nullptr) {
65 return nullptr;
66 }
67 av_opt_set_int(c, "srcw", src_width, 0);
68 av_opt_set_int(c, "srch", src_height, 0);
69 av_opt_set_int(c, "src_format", av_src_format, 0);
70 av_opt_set_int(c, "dstw", dst_width, 0);
71 av_opt_set_int(c, "dsth", dst_height, 0);
72 av_opt_set_int(c, "dst_format", av_dst_format, 0);
73 av_opt_set_int(c, "sws_flags", sws_flags, 0);
74 av_opt_set_int(c, "threads", MOV_thread_count(), 0);
75
76 if (sws_init_context(c, nullptr, nullptr) < 0) {
77 sws_freeContext(c);
78 return nullptr;
79 }
80# else
81 SwsContext *c = sws_getContext(src_width,
82 src_height,
83 AVPixelFormat(av_src_format),
84 dst_width,
85 dst_height,
86 AVPixelFormat(av_dst_format),
87 sws_flags,
88 nullptr,
89 nullptr,
90 nullptr);
91# endif
92
93 return c;
94}
95
96static void init_swscale_cache_if_needed()
97{
98 if (swscale_cache == nullptr) {
99 swscale_cache = new Vector<SwscaleContext>();
100 swscale_cache_timestamp = 0;
101 }
102}
103
104static bool remove_oldest_swscale_context()
105{
106 int64_t oldest_index = -1;
107 int64_t oldest_time = 0;
108 for (int64_t index = 0; index < swscale_cache->size(); index++) {
109 SwscaleContext &ctx = (*swscale_cache)[index];
110 if (ctx.is_used) {
111 continue;
112 }
113 int64_t time = swscale_cache_timestamp - ctx.last_use_timestamp;
114 if (time > oldest_time) {
115 oldest_time = time;
116 oldest_index = index;
117 }
118 }
119
120 if (oldest_index >= 0) {
121 SwscaleContext &ctx = (*swscale_cache)[oldest_index];
122 sws_freeContext(ctx.context);
123 swscale_cache->remove_and_reorder(oldest_index);
124 return true;
125 }
126 return false;
127}
128
129static void maintain_swscale_cache_size()
130{
131 while (swscale_cache->size() > swscale_cache_max_entries) {
132 if (!remove_oldest_swscale_context()) {
133 /* Could not remove anything (all contexts are actively used),
134 * stop trying. */
135 break;
136 }
137 }
138}
139
140SwsContext *ffmpeg_sws_get_context(int src_width,
141 int src_height,
142 int av_src_format,
143 bool src_full_range,
144 int src_color_space,
145 int dst_width,
146 int dst_height,
147 int av_dst_format,
148 bool dst_full_range,
149 int dst_color_space,
150 int sws_flags)
151{
152 std::lock_guard lock(swscale_cache_lock);
153
154 init_swscale_cache_if_needed();
155
156 swscale_cache_timestamp++;
157
158 /* Search for unused context that has suitable parameters. */
159 SwsContext *ctx = nullptr;
160 for (SwscaleContext &c : *swscale_cache) {
161 if (!c.is_used && c.src_width == src_width && c.src_height == src_height &&
162 c.src_format == av_src_format && c.src_full_range == src_full_range &&
163 c.src_colorspace == src_color_space && c.dst_width == dst_width &&
164 c.dst_height == dst_height && c.dst_format == av_dst_format &&
165 c.dst_full_range == dst_full_range && c.dst_colorspace == dst_color_space &&
166 c.flags == sws_flags)
167 {
168 ctx = c.context;
169 /* Mark as used. */
170 c.is_used = true;
171 c.last_use_timestamp = swscale_cache_timestamp;
172 break;
173 }
174 }
175 if (ctx == nullptr) {
176 /* No free matching context in cache: create a new one. */
177 ctx = sws_create_context(
178 src_width, src_height, av_src_format, dst_width, dst_height, av_dst_format, sws_flags);
179
180 int src_range, dst_range, brightness, contrast, saturation;
181 const int *table, *inv_table;
182 if (sws_getColorspaceDetails(ctx,
183 (int **)&inv_table,
184 &src_range,
185 (int **)&table,
186 &dst_range,
187 &brightness,
188 &contrast,
189 &saturation) >= 0)
190 {
191 if (src_full_range) {
192 src_range = 1;
193 }
194 if (dst_full_range) {
195 dst_range = 1;
196 }
197 if (src_color_space >= 0) {
198 inv_table = sws_getCoefficients(src_color_space);
199 }
200 if (dst_color_space >= 0) {
201 table = sws_getCoefficients(dst_color_space);
202 }
203 sws_setColorspaceDetails(
204 ctx, (int *)inv_table, src_range, table, dst_range, brightness, contrast, saturation);
205 }
206
207 SwscaleContext c;
208 c.src_width = src_width;
209 c.src_height = src_height;
210 c.dst_width = dst_width;
211 c.dst_height = dst_height;
212 c.src_format = AVPixelFormat(av_src_format);
213 c.dst_format = AVPixelFormat(av_dst_format);
214 c.src_full_range = src_full_range;
215 c.dst_full_range = dst_full_range;
216 c.src_colorspace = src_color_space;
217 c.dst_colorspace = dst_color_space;
218 c.flags = sws_flags;
219 c.context = ctx;
220 c.is_used = true;
221 c.last_use_timestamp = swscale_cache_timestamp;
222 swscale_cache->append(c);
223
224 maintain_swscale_cache_size();
225 }
226 return ctx;
227}
228
229void ffmpeg_sws_release_context(SwsContext *ctx)
230{
231 std::lock_guard lock(swscale_cache_lock);
232 init_swscale_cache_if_needed();
233
234 bool found = false;
235 for (SwscaleContext &c : *swscale_cache) {
236 if (c.context == ctx) {
237 BLI_assert_msg(c.is_used, "Releasing ffmpeg swscale context that is not in use");
238 c.is_used = false;
239 found = true;
240 break;
241 }
242 }
243 BLI_assert_msg(found, "Releasing ffmpeg swscale context that is not in cache");
244 UNUSED_VARS_NDEBUG(found);
245 maintain_swscale_cache_size();
246}
247
248void ffmpeg_sws_exit()
249{
250 std::lock_guard lock(swscale_cache_lock);
251 if (swscale_cache != nullptr) {
252 for (SwscaleContext &c : *swscale_cache) {
253 sws_freeContext(c.context);
254 }
255 delete swscale_cache;
256 swscale_cache = nullptr;
257 }
258}
259
260void ffmpeg_sws_scale_frame(SwsContext *ctx, AVFrame *dst, const AVFrame *src)
261{
262# if defined(FFMPEG_SWSCALE_THREADING)
263 sws_scale_frame(ctx, dst, src);
264# else
265 sws_scale(ctx, src->data, src->linesize, 0, src->height, dst->data, dst->linesize);
266# endif
267}
268
269#endif /* WITH_FFMPEG */
270
271} // namespace blender
volatile int lock
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert_msg(a, msg)
long long int int64_t
friend class Vector
std::string time(const std::tm &date_time, TimeFormat format=TimeFormat::H24)
int context(const bContext *C, const char *member, bContextDataResult *result)
BMesh Iterators.
__int64 int64_t
Definition BKE_text.h:90
std::mutex Mutex
Definition BLI_mutex.hh:51