21# include <libavutil/opt.h>
22# include <libavutil/pixfmt.h>
23# include <libswscale/swscale.h>
33static constexpr int64_t swscale_cache_max_entries = 32;
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;
48static Mutex swscale_cache_lock;
49static int64_t swscale_cache_timestamp = 0;
52static SwsContext *sws_create_context(
int src_width,
60# if defined(FFMPEG_SWSCALE_THREADING)
63 SwsContext *c = sws_alloc_context();
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);
76 if (sws_init_context(c,
nullptr,
nullptr) < 0) {
81 SwsContext *c = sws_getContext(src_width,
83 AVPixelFormat(av_src_format),
86 AVPixelFormat(av_dst_format),
96static void init_swscale_cache_if_needed()
98 if (swscale_cache ==
nullptr) {
100 swscale_cache_timestamp = 0;
104static bool remove_oldest_swscale_context()
109 SwscaleContext &ctx = (*swscale_cache)[
index];
113 int64_t time = swscale_cache_timestamp - ctx.last_use_timestamp;
114 if (time > oldest_time) {
116 oldest_index =
index;
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);
129static void maintain_swscale_cache_size()
131 while (swscale_cache->size() > swscale_cache_max_entries) {
132 if (!remove_oldest_swscale_context()) {
140SwsContext *ffmpeg_sws_get_context(
int src_width,
152 std::lock_guard
lock(swscale_cache_lock);
154 init_swscale_cache_if_needed();
156 swscale_cache_timestamp++;
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)
171 c.last_use_timestamp = swscale_cache_timestamp;
175 if (ctx ==
nullptr) {
177 ctx = sws_create_context(
178 src_width, src_height, av_src_format, dst_width, dst_height, av_dst_format, sws_flags);
180 int src_range, dst_range, brightness, contrast, saturation;
181 const int *table, *inv_table;
182 if (sws_getColorspaceDetails(ctx,
191 if (src_full_range) {
194 if (dst_full_range) {
197 if (src_color_space >= 0) {
198 inv_table = sws_getCoefficients(src_color_space);
200 if (dst_color_space >= 0) {
201 table = sws_getCoefficients(dst_color_space);
203 sws_setColorspaceDetails(
204 ctx, (
int *)inv_table, src_range, table, dst_range, brightness, contrast, saturation);
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;
221 c.last_use_timestamp = swscale_cache_timestamp;
222 swscale_cache->append(c);
224 maintain_swscale_cache_size();
229void ffmpeg_sws_release_context(SwsContext *ctx)
231 std::lock_guard
lock(swscale_cache_lock);
232 init_swscale_cache_if_needed();
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");
243 BLI_assert_msg(found,
"Releasing ffmpeg swscale context that is not in cache");
245 maintain_swscale_cache_size();
248void ffmpeg_sws_exit()
250 std::lock_guard
lock(swscale_cache_lock);
251 if (swscale_cache !=
nullptr) {
252 for (SwscaleContext &c : *swscale_cache) {
253 sws_freeContext(c.context);
255 delete swscale_cache;
256 swscale_cache =
nullptr;
260void ffmpeg_sws_scale_frame(SwsContext *ctx, AVFrame *dst,
const AVFrame *src)
262# if defined(FFMPEG_SWSCALE_THREADING)
263 sws_scale_frame(ctx, dst, src);
265 sws_scale(ctx, src->data, src->linesize, 0, src->height, dst->data, dst->linesize);
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert_msg(a, msg)
std::string time(const std::tm &date_time, TimeFormat format=TimeFormat::H24)
int context(const bContext *C, const char *member, bContextDataResult *result)