Blender V5.3
movie_proxy_indexer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Peter Schlaile <peter [at] schlaile [dot] de>.
2 * SPDX-FileCopyrightText: 2024-2026 Blender Authors
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later */
5
9
10#include <cstdlib>
11
12#include "MEM_guardedalloc.h"
13
14#include "DNA_color_types.h"
15
16#include "BLI_endian_switch.hh"
17#include "BLI_fileops.hh"
18#include "BLI_math_base.hh"
19#include "BLI_math_base_c.hh"
20#include "BLI_path_utils.hh"
21#include "BLI_string.hh"
22#include "BLI_string_utils.hh"
23#include "BLI_time.hh"
24#include "BLI_utildefines.hh"
25
26#include "CLG_log.h"
27
29
30#include "MOV_read.hh"
31
32#include "ffmpeg_swscale.hh"
34#include "movie_read.hh"
35#include "movie_util.hh"
36
37#ifdef WITH_FFMPEG
38extern "C" {
39# include "ffmpeg_compat.h"
40# include <libavutil/imgutils.h>
41}
42#endif
43
44namespace blender {
45
46static CLG_LogRef LOG = {"video.proxy"};
47
50static const float proxy_fac[] = {0.25, 0.50, 0.75, 1.00};
51
53{
54 switch (pr_size) {
55 case IMB_PROXY_NONE:
56 return -1;
57 case IMB_PROXY_25:
58 return 0;
59 case IMB_PROXY_50:
60 return 1;
61 case IMB_PROXY_75:
62 return 2;
63 case IMB_PROXY_100:
64 return 3;
65 default:
66 BLI_assert_msg(0, "Unhandled proxy size enum!");
67 return -1;
68 }
69}
70
71static void get_proxy_dir(const MovieReader *anim, char *proxy_dir, size_t proxy_dir_maxncpy)
72{
73 if (!anim->proxy_dir[0]) {
75 char dirname[FILE_MAXDIR];
77 BLI_path_join(proxy_dir, proxy_dir_maxncpy, dirname, "BL_proxy", filename);
78 }
79 else {
80 BLI_strncpy(proxy_dir, anim->proxy_dir, proxy_dir_maxncpy);
81 }
82}
83
84static bool get_proxy_filepath(const MovieReader *anim,
85 IMB_Proxy_Size preview_size,
86 char *filepath,
87 bool temp)
88{
89 char proxy_dir[FILE_MAXDIR];
90 int i = proxy_size_to_array_index(preview_size);
91
92 BLI_assert(i >= 0);
93
94 char proxy_name[FILE_MAXFILE];
95 char stream_suffix[20];
96 const char *name = (temp) ? "proxy_%d%s_part.avi" : "proxy_%d%s.avi";
97
98 stream_suffix[0] = 0;
99
100 if (anim->streamindex > 0) {
101 SNPRINTF(stream_suffix, "_st%d", anim->streamindex);
102 }
103
104 SNPRINTF(proxy_name, name, int(proxy_fac[i] * 100), stream_suffix, anim->suffix);
105
106 get_proxy_dir(anim, proxy_dir, sizeof(proxy_dir));
107
108 if (BLI_path_ncmp(anim->filepath, proxy_dir, FILE_MAXDIR) == 0) {
109 return false;
110 }
111
112 BLI_path_join(filepath, FILE_MAXFILE + FILE_MAXDIR, proxy_dir, proxy_name);
113 return true;
114}
115
116/* ----------------------------------------------------------------------
117 * - ffmpeg rebuilder
118 * ---------------------------------------------------------------------- */
119
120#ifdef WITH_FFMPEG
121
122struct proxy_output_ctx {
123 AVFormatContext *of;
124 AVStream *st;
125 AVCodecContext *c;
126 const AVCodec *codec;
127 SwsContext *sws_ctx;
128 AVFrame *frame;
129 int cfra;
130 AVRational output_timebase;
131 IMB_Proxy_Size proxy_size;
132 int orig_height;
133 MovieReader *anim;
134};
135
136static proxy_output_ctx *alloc_proxy_output_ffmpeg(MovieReader *anim,
137 AVCodecContext *codec_ctx,
138 AVStream *st,
139 IMB_Proxy_Size proxy_size,
140 int width,
141 int height,
142 int quality)
143{
144 proxy_output_ctx *rv = MEM_new_zeroed<proxy_output_ctx>("alloc_proxy_output");
145
146 char filepath[FILE_MAX];
147
148 rv->proxy_size = proxy_size;
149 rv->anim = anim;
150
151 get_proxy_filepath(rv->anim, rv->proxy_size, filepath, true);
152 if (!BLI_file_ensure_parent_dir_exists(filepath)) {
153 MEM_delete(rv);
154 return nullptr;
155 }
156
157 rv->of = avformat_alloc_context();
158 /* Note: we keep on using .avi extension for proxies,
159 * but actual container can not be AVI, since it does not support
160 * video rotation metadata. */
161 rv->of->oformat = av_guess_format("mp4", nullptr, nullptr);
162
163 rv->of->url = av_strdup(filepath);
164
165 rv->st = avformat_new_stream(rv->of, nullptr);
166 rv->st->id = 0;
167
168 rv->codec = avcodec_find_encoder(AV_CODEC_ID_H264);
169
170 rv->c = avcodec_alloc_context3(rv->codec);
171
172 if (!rv->codec) {
173 CLOG_ERROR(&LOG, "Could not build proxy '%s': failed to create video encoder", filepath);
174 avcodec_free_context(&rv->c);
175 avformat_free_context(rv->of);
176 MEM_delete(rv);
177 return nullptr;
178 }
179
180 rv->c->width = width;
181 rv->c->height = height;
182 rv->c->gop_size = 10;
183 rv->c->max_b_frames = 0;
184
185 const enum AVPixelFormat *pix_fmts = ffmpeg_get_pix_fmts(rv->c, rv->codec);
186 if (pix_fmts) {
187 rv->c->pix_fmt = pix_fmts[0];
188 }
189 else {
190 rv->c->pix_fmt = AV_PIX_FMT_YUVJ420P;
191 }
192
193 rv->c->sample_aspect_ratio = rv->st->sample_aspect_ratio = st->sample_aspect_ratio;
194
195 /* Use same output timebase as input: we seek within the proxy file
196 * using exact same frame numbers as if it was original file. So we want to
197 * match original frame-rate, plus any variable frames in the source file. */
198 rv->output_timebase = st->time_base;
199 rv->c->time_base = st->time_base;
200 rv->st->time_base = st->time_base;
201 rv->st->avg_frame_rate = st->avg_frame_rate;
202
203 /* This range matches #eFFMpegCrf. `crf_range_min` corresponds to lowest quality,
204 * `crf_range_max` to highest quality. */
205 const int crf_range_min = 32;
206 const int crf_range_max = 17;
207 int crf = round_fl_to_int((quality / 100.0f) * (crf_range_max - crf_range_min) + crf_range_min);
208
209 AVDictionary *codec_opts = nullptr;
210 /* High quality preset value. */
211 av_dict_set_int(&codec_opts, "crf", crf, 0);
212 /* Prefer smaller file-size. Presets from `veryslow` to `veryfast` produce output with very
213 * similar file-size, but there is big difference in performance.
214 * In some cases `veryfast` preset will produce smallest file-size. */
215 av_dict_set(&codec_opts, "preset", "veryfast", 0);
216 av_dict_set(&codec_opts, "tune", "fastdecode", 0);
217
218 if (rv->codec->capabilities & AV_CODEC_CAP_OTHER_THREADS) {
219 rv->c->thread_count = 0;
220 }
221 else {
222 rv->c->thread_count = MOV_thread_count();
223 }
224
225 if (rv->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) {
226 rv->c->thread_type = FF_THREAD_FRAME;
227 }
228 else if (rv->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) {
229 rv->c->thread_type = FF_THREAD_SLICE;
230 }
231
232 if (rv->of->oformat->flags & AVFMT_GLOBALHEADER) {
233 rv->c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
234 }
235
236 rv->c->color_range = codec_ctx->color_range;
237 rv->c->color_primaries = codec_ctx->color_primaries;
238 rv->c->color_trc = codec_ctx->color_trc;
239 rv->c->colorspace = codec_ctx->colorspace;
240
241 int ret = avio_open(&rv->of->pb, filepath, AVIO_FLAG_WRITE);
242
243 if (ret < 0) {
244 char error_str[AV_ERROR_MAX_STRING_SIZE];
245 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
246
248 "Could not build proxy '%s': failed to create output file (%s)",
249 filepath,
250 error_str);
251 avcodec_free_context(&rv->c);
252 avformat_free_context(rv->of);
253 MEM_delete(rv);
254 return nullptr;
255 }
256
257 ret = avcodec_open2(rv->c, rv->codec, &codec_opts);
258 if (ret < 0) {
259 char error_str[AV_ERROR_MAX_STRING_SIZE];
260 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
261
263 &LOG, "Could not build proxy '%s': failed to open video codec (%s)", filepath, error_str);
264 avcodec_free_context(&rv->c);
265 avformat_free_context(rv->of);
266 MEM_delete(rv);
267 return nullptr;
268 }
269
270 avcodec_parameters_from_context(rv->st->codecpar, rv->c);
271 ffmpeg_copy_display_matrix(st, rv->st);
272
273 rv->orig_height = st->codecpar->height;
274
275 if (st->codecpar->width != width || st->codecpar->height != height ||
276 st->codecpar->format != rv->c->pix_fmt)
277 {
278 const size_t align = ffmpeg_get_buffer_alignment();
279 rv->frame = av_frame_alloc();
280 rv->frame->format = rv->c->pix_fmt;
281 rv->frame->width = width;
282 rv->frame->height = height;
283 av_frame_get_buffer(rv->frame, align);
284
285 rv->sws_ctx = ffmpeg_sws_get_context(st->codecpar->width,
286 rv->orig_height,
287 AVPixelFormat(st->codecpar->format),
288 codec_ctx->color_range == AVCOL_RANGE_JPEG,
289 -1,
290 width,
291 height,
292 rv->c->pix_fmt,
293 codec_ctx->color_range == AVCOL_RANGE_JPEG,
294 -1,
295 SWS_FAST_BILINEAR);
296 }
297
298 ret = avformat_write_header(rv->of, nullptr);
299 if (ret < 0) {
300 char error_str[AV_ERROR_MAX_STRING_SIZE];
301 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
302
304 &LOG, "Could not build proxy '%s': failed to write header (%s)", filepath, error_str);
305
306 if (rv->frame) {
307 av_frame_free(&rv->frame);
308 }
309
310 avcodec_free_context(&rv->c);
311 avformat_free_context(rv->of);
312 MEM_delete(rv);
313 return nullptr;
314 }
315
316 return rv;
317}
318
319static void add_to_proxy_output_ffmpeg(proxy_output_ctx *ctx,
320 AVFrame *frame,
321 AVRational input_timebase)
322{
323 if (!ctx) {
324 return;
325 }
326
327 const int64_t src_pts = frame ? frame->pts : AV_NOPTS_VALUE;
328
329 if (ctx->sws_ctx && frame &&
330 (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]))
331 {
332 ffmpeg_sws_scale_frame(ctx->sws_ctx, ctx->frame, frame);
333 }
334
335 frame = ctx->sws_ctx ? (frame ? ctx->frame : nullptr) : frame;
336
337 if (frame) {
338 if (src_pts != AV_NOPTS_VALUE) {
339 frame->pts = av_rescale_q(src_pts, input_timebase, ctx->output_timebase);
340 }
341 else {
342 frame->pts = ctx->cfra;
343 }
344 ctx->cfra++;
345 }
346
347 int ret = avcodec_send_frame(ctx->c, frame);
348 if (ret < 0) {
349 /* Can't send frame to encoder. This shouldn't happen. */
350 char error_str[AV_ERROR_MAX_STRING_SIZE];
351 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
352
354 &LOG, "Building proxy '%s': failed to send video frame (%s)", ctx->of->url, error_str);
355 return;
356 }
357 AVPacket *packet = av_packet_alloc();
358
359 while (ret >= 0) {
360 ret = avcodec_receive_packet(ctx->c, packet);
361
362 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
363 /* No more packets to flush. */
364 break;
365 }
366 if (ret < 0) {
367 char error_str[AV_ERROR_MAX_STRING_SIZE];
368 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
369
371 "Building proxy '%s': error encoding frame #%i (%s)",
372 ctx->of->url,
373 ctx->cfra - 1,
374 error_str);
375 break;
376 }
377
378 packet->stream_index = ctx->st->index;
379 av_packet_rescale_ts(packet, ctx->c->time_base, ctx->st->time_base);
380# ifdef FFMPEG_USE_DURATION_WORKAROUND
381 my_guess_pkt_duration(ctx->of, ctx->st, packet);
382# endif
383
384 int write_ret = av_interleaved_write_frame(ctx->of, packet);
385 if (write_ret != 0) {
386 char error_str[AV_ERROR_MAX_STRING_SIZE];
387 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, write_ret);
388
390 "Building proxy '%s': error writing frame #%i (%s)",
391 ctx->of->url,
392 ctx->cfra - 1,
393 error_str);
394 break;
395 }
396 }
397
398 av_packet_free(&packet);
399}
400
401static void free_proxy_output_ffmpeg(proxy_output_ctx *ctx, int rollback)
402{
403 char filepath[FILE_MAX];
404 char filepath_tmp[FILE_MAX];
405
406 if (!ctx) {
407 return;
408 }
409
410 if (!rollback) {
411 /* Flush the remaining packets. */
412 add_to_proxy_output_ffmpeg(ctx, nullptr, {1, 1});
413 }
414
415 av_write_trailer(ctx->of);
416
417 if (ctx->of->oformat) {
418 if (!(ctx->of->oformat->flags & AVFMT_NOFILE)) {
419 avio_close(ctx->of->pb);
420 }
421 }
422 avcodec_free_context(&ctx->c);
423 avformat_free_context(ctx->of);
424
425 if (ctx->sws_ctx) {
426 ffmpeg_sws_release_context(ctx->sws_ctx);
427 ctx->sws_ctx = nullptr;
428 }
429 if (ctx->frame) {
430 av_frame_free(&ctx->frame);
431 }
432
433 get_proxy_filepath(ctx->anim, ctx->proxy_size, filepath_tmp, true);
434
435 if (rollback) {
436 BLI_delete(filepath_tmp, false, false);
437 }
438 else {
439 get_proxy_filepath(ctx->anim, ctx->proxy_size, filepath, false);
440 BLI_rename_overwrite(filepath_tmp, filepath);
441 }
442
443 MEM_delete(ctx);
444}
445
446struct MovieProxyBuilder {
447
448 AVFormatContext *iFormatCtx;
449 AVCodecContext *iCodecCtx;
450 const AVCodec *iCodec;
451 AVStream *iStream;
452 int videoStream;
453
454 int num_proxy_sizes;
455
456 proxy_output_ctx *proxy_ctx[IMB_PROXY_MAX_SLOT];
457
458 int proxy_sizes_in_use;
459
460 bool build_only_on_bad_performance;
461 bool building_cancelled;
462};
463
464static MovieProxyBuilder *proxy_builder_create(MovieReader *anim,
465 int proxy_sizes_in_use,
466 int quality,
467 bool build_only_on_bad_performance)
468{
469 /* Never build proxies for un-seekable single frame files. */
470 if (anim->never_seek_decode_one_frame) {
471 return nullptr;
472 }
473
474 MovieProxyBuilder *context = MEM_new_zeroed<MovieProxyBuilder>(__func__);
475 int num_proxy_sizes = IMB_PROXY_MAX_SLOT;
476 int i, streamcount;
477
478 context->proxy_sizes_in_use = proxy_sizes_in_use;
479 context->num_proxy_sizes = IMB_PROXY_MAX_SLOT;
480 context->build_only_on_bad_performance = build_only_on_bad_performance;
481
482 memset(context->proxy_ctx, 0, sizeof(context->proxy_ctx));
483
484 if (avformat_open_input(&context->iFormatCtx, anim->filepath, nullptr, nullptr) != 0) {
485 MEM_delete(context);
486 return nullptr;
487 }
488
489 if (avformat_find_stream_info(context->iFormatCtx, nullptr) < 0) {
490 avformat_close_input(&context->iFormatCtx);
491 MEM_delete(context);
492 return nullptr;
493 }
494
495 streamcount = anim->streamindex;
496
497 /* Find the video stream. */
498 context->videoStream = -1;
499 for (i = 0; i < context->iFormatCtx->nb_streams; i++) {
500 if (ffmpeg_stream_counts_as_video(context->iFormatCtx, context->iFormatCtx->streams[i])) {
501 if (streamcount > 0) {
502 streamcount--;
503 continue;
504 }
505 context->videoStream = i;
506 break;
507 }
508 }
509
510 if (context->videoStream == -1) {
511 avformat_close_input(&context->iFormatCtx);
512 MEM_delete(context);
513 return nullptr;
514 }
515
516 context->iStream = context->iFormatCtx->streams[context->videoStream];
517
518 context->iCodec = avcodec_find_decoder(context->iStream->codecpar->codec_id);
519
520 if (context->iCodec == nullptr) {
521 avformat_close_input(&context->iFormatCtx);
522 MEM_delete(context);
523 return nullptr;
524 }
525
526 context->iCodecCtx = avcodec_alloc_context3(nullptr);
527 avcodec_parameters_to_context(context->iCodecCtx, context->iStream->codecpar);
528 context->iCodecCtx->workaround_bugs = FF_BUG_AUTODETECT;
529
530 if (context->iCodec->capabilities & AV_CODEC_CAP_OTHER_THREADS) {
531 context->iCodecCtx->thread_count = 0;
532 }
533 else {
534 context->iCodecCtx->thread_count = MOV_thread_count();
535 }
536
537 if (context->iCodec->capabilities & AV_CODEC_CAP_FRAME_THREADS) {
538 context->iCodecCtx->thread_type = FF_THREAD_FRAME;
539 }
540 else if (context->iCodec->capabilities & AV_CODEC_CAP_SLICE_THREADS) {
541 context->iCodecCtx->thread_type = FF_THREAD_SLICE;
542 }
543
544 if (avcodec_open2(context->iCodecCtx, context->iCodec, nullptr) < 0) {
545 avformat_close_input(&context->iFormatCtx);
546 avcodec_free_context(&context->iCodecCtx);
547 MEM_delete(context);
548 return nullptr;
549 }
550
551 for (i = 0; i < num_proxy_sizes; i++) {
552 if (proxy_sizes_in_use & proxy_sizes[i]) {
553 int width = context->iCodecCtx->width * proxy_fac[i];
554 int height = context->iCodecCtx->height * proxy_fac[i];
555 width += width % 2;
556 height += height % 2;
557 context->proxy_ctx[i] = alloc_proxy_output_ffmpeg(
558 anim, context->iCodecCtx, context->iStream, proxy_sizes[i], width, height, quality);
559 if (!context->proxy_ctx[i]) {
560 proxy_sizes_in_use &= ~int(proxy_sizes[i]);
561 }
562 }
563 }
564
565 if (context->proxy_ctx[0] == nullptr && context->proxy_ctx[1] == nullptr &&
566 context->proxy_ctx[2] == nullptr && context->proxy_ctx[3] == nullptr)
567 {
568 avformat_close_input(&context->iFormatCtx);
569 avcodec_free_context(&context->iCodecCtx);
570 MEM_delete(context);
571 return nullptr; /* Nothing to transcode. */
572 }
573
574 return context;
575}
576
577static void proxy_builder_finish(MovieProxyBuilder *context, const bool stop)
578{
579 const bool do_rollback = stop || context->building_cancelled;
580
581 for (int i = 0; i < context->num_proxy_sizes; i++) {
582 if (context->proxy_sizes_in_use & proxy_sizes[i]) {
583 free_proxy_output_ffmpeg(context->proxy_ctx[i], do_rollback);
584 }
585 }
586
587 avcodec_free_context(&context->iCodecCtx);
588 avformat_close_input(&context->iFormatCtx);
589
590 MEM_delete(context);
591}
592
593static void proxy_builder_proc_decoded_frame(MovieProxyBuilder *context, AVFrame *in_frame)
594{
595 for (int i = 0; i < context->num_proxy_sizes; i++) {
596 add_to_proxy_output_ffmpeg(context->proxy_ctx[i], in_frame, context->iStream->time_base);
597 }
598}
599
600static int proxy_builder_process(MovieProxyBuilder *context,
601 const bool *stop,
602 bool *do_update,
603 const FunctionRef<void(float progress)> set_progress_fn)
604{
605 AVFrame *in_frame = av_frame_alloc();
606 AVPacket *next_packet = av_packet_alloc();
607 uint64_t stream_size = avio_size(context->iFormatCtx->pb);
608
609 float progress = 0.0f;
610 while (av_read_frame(context->iFormatCtx, next_packet) >= 0) {
611 float next_progress =
612 float(int(floor(double(next_packet->pos) * 100 / double(stream_size) + 0.5))) / 100;
613
614 if (progress != next_progress) {
615 progress = next_progress;
616 *do_update = true;
617 if (set_progress_fn) {
618 set_progress_fn(progress);
619 }
620 }
621
622 if (*stop) {
623 break;
624 }
625
626 if (next_packet->stream_index == context->videoStream) {
627 int ret = avcodec_send_packet(context->iCodecCtx, next_packet);
628 while (ret >= 0) {
629 ret = avcodec_receive_frame(context->iCodecCtx, in_frame);
630
631 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
632 /* No more frames to flush. */
633 break;
634 }
635 if (ret < 0) {
636 char error_str[AV_ERROR_MAX_STRING_SIZE];
637 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
638 CLOG_ERROR(&LOG, "Error decoding proxy frame: %s", error_str);
639 break;
640 }
641
642 proxy_builder_proc_decoded_frame(context, in_frame);
643 }
644 }
645 av_packet_unref(next_packet);
646 }
647
648 /* process pictures still stuck in decoder engine after EOF
649 * according to ffmpeg docs using nullptr packets.
650 *
651 * At least, if we haven't already stopped... */
652
653 if (!*stop) {
654 int ret = avcodec_send_packet(context->iCodecCtx, nullptr);
655
656 while (ret >= 0) {
657 ret = avcodec_receive_frame(context->iCodecCtx, in_frame);
658
659 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
660 /* No more frames to flush. */
661 break;
662 }
663 if (ret < 0) {
664 char error_str[AV_ERROR_MAX_STRING_SIZE];
665 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
666 CLOG_ERROR(&LOG, "Error flushing proxy frame: %s", error_str);
667 break;
668 }
669 proxy_builder_proc_decoded_frame(context, in_frame);
670 }
671 }
672
673 av_packet_free(&next_packet);
674 av_free(in_frame);
675
676 return 1;
677}
678
679/* Get number of frames, that can be decoded in specified time period. */
680static int performance_get_decode_rate(MovieProxyBuilder *context, const double time_period)
681{
682 AVFrame *in_frame = av_frame_alloc();
683 AVPacket *packet = av_packet_alloc();
684
685 const double start = BLI_time_now_seconds();
686 int frames_decoded = 0;
687
688 while (av_read_frame(context->iFormatCtx, packet) >= 0) {
689 if (packet->stream_index != context->videoStream) {
690 av_packet_unref(packet);
691 continue;
692 }
693
694 int ret = avcodec_send_packet(context->iCodecCtx, packet);
695 while (ret >= 0) {
696 ret = avcodec_receive_frame(context->iCodecCtx, in_frame);
697
698 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
699 break;
700 }
701
702 if (ret < 0) {
703 char error_str[AV_ERROR_MAX_STRING_SIZE];
704 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
705 CLOG_ERROR(&LOG, "Error decoding proxy frame: %s", error_str);
706 break;
707 }
708 frames_decoded++;
709 }
710
711 const double end = BLI_time_now_seconds();
712
713 if (end > start + time_period) {
714 break;
715 }
716 av_packet_unref(packet);
717 }
718
719 av_packet_free(&packet);
720 av_frame_free(&in_frame);
721
722 avcodec_flush_buffers(context->iCodecCtx);
723 av_seek_frame(context->iFormatCtx, -1, 0, AVSEEK_FLAG_BACKWARD);
724 return frames_decoded;
725}
726
727/* Read up to 10k movie packets and return max GOP size detected.
728 * Number of packets is arbitrary. It should be as large as possible, but processed within
729 * reasonable time period, so detected GOP size is as close to real as possible. */
730static int performance_get_max_gop_size(MovieProxyBuilder *context)
731{
732 AVPacket *packet = av_packet_alloc();
733
734 const int packets_max = 10000;
735 int packet_index = 0;
736 int max_gop = 0;
737 int cur_gop = 0;
738
739 while (av_read_frame(context->iFormatCtx, packet) >= 0) {
740 if (packet->stream_index != context->videoStream) {
741 av_packet_unref(packet);
742 continue;
743 }
744 packet_index++;
745 cur_gop++;
746
747 if (packet->flags & AV_PKT_FLAG_KEY) {
748 max_gop = max_ii(max_gop, cur_gop);
749 cur_gop = 0;
750 }
751
752 if (packet_index > packets_max) {
753 break;
754 }
755 av_packet_unref(packet);
756 }
757
758 av_packet_free(&packet);
759
760 av_seek_frame(context->iFormatCtx, -1, 0, AVSEEK_FLAG_BACKWARD);
761 return max_gop;
762}
763
764/* Assess scrubbing performance of provided file. This function is not meant to be very exact.
765 * It compares number of frames decoded in reasonable time with largest detected GOP size.
766 * Because seeking happens in single GOP, it means, that maximum seek time can be detected this
767 * way.
768 * Since proxies use GOP size of 10 frames, skip building if detected GOP size is less or
769 * equal.
770 */
771static bool need_to_build_proxy(MovieProxyBuilder *context)
772{
773 if (!context->build_only_on_bad_performance) {
774 return true;
775 }
776
777 /* Make sure, that file is not cold read. */
778 performance_get_decode_rate(context, 0.1);
779 /* Get decode rate per 100ms. This is arbitrary, but seems to be good baseline cadence of
780 * seeking. */
781 const int decode_rate = performance_get_decode_rate(context, 0.1);
782 const int max_gop_size = performance_get_max_gop_size(context);
783
784 if (max_gop_size <= 10 || max_gop_size < decode_rate) {
786 "Skipping proxy building for %s: Decoding performance is already good.",
787 context->iFormatCtx->url);
788 context->building_cancelled = true;
789 return false;
790 }
791
792 return true;
793}
794
795#endif
796
797/* ----------------------------------------------------------------------
798 * - public API
799 * ---------------------------------------------------------------------- */
800
801MovieProxyBuilder *MOV_proxy_builder_start(MovieReader *anim,
802 int proxy_sizes_in_use,
803 int quality,
804 const bool overwrite,
805 Set<std::string> *processed_paths,
806 bool build_only_on_bad_performance)
807{
808 int proxy_sizes_to_build = proxy_sizes_in_use;
809
810 /* Check which proxies are going to be generated in this session already. */
811 if (processed_paths != nullptr) {
812 for (int i = 0; i < IMB_PROXY_MAX_SLOT; i++) {
813 IMB_Proxy_Size proxy_size = proxy_sizes[i];
814 if ((proxy_size & proxy_sizes_to_build) == 0) {
815 continue;
816 }
817 char filepath[FILE_MAX];
818 if (!get_proxy_filepath(anim, proxy_size, filepath, false)) {
819 return nullptr;
820 }
821 if (!processed_paths->add(filepath)) {
822 proxy_sizes_to_build &= ~int(proxy_size);
823 }
824 }
825 }
826
827 /* When not overwriting existing proxies, skip the ones that already exist. */
828 if (!overwrite) {
829 int built_proxies = MOV_get_existing_proxies(anim);
830 if (built_proxies != 0) {
831 for (int i = 0; i < IMB_PROXY_MAX_SLOT; i++) {
832 IMB_Proxy_Size proxy_size = proxy_sizes[i];
833 if (proxy_size & built_proxies) {
834 char filepath[FILE_MAX];
835 if (!get_proxy_filepath(anim, proxy_size, filepath, false)) {
836 return nullptr;
837 }
838 CLOG_INFO_NOCHECK(&LOG, "Skipping proxy: %s", filepath);
839 }
840 }
841 }
842 proxy_sizes_to_build &= ~built_proxies;
843 }
844
845 if (proxy_sizes_to_build == 0) {
846 return nullptr;
847 }
848
849 MovieProxyBuilder *context = nullptr;
850#ifdef WITH_FFMPEG
851 if (anim->state == MovieReader::State::Valid) {
852 context = proxy_builder_create(
853 anim, proxy_sizes_to_build, quality, build_only_on_bad_performance);
854 }
855#else
856 UNUSED_VARS(build_only_on_bad_performance);
857#endif
858
859 return context;
860
861 UNUSED_VARS(proxy_sizes_in_use, quality);
862}
863
864void MOV_proxy_builder_process(MovieProxyBuilder *context,
865 /* NOLINTNEXTLINE: readability-non-const-parameter. */
866 const bool *stop,
867 /* NOLINTNEXTLINE: readability-non-const-parameter. */
868 bool *do_update,
869 const FunctionRef<void(float progress)> set_progress_fn)
870{
871#ifdef WITH_FFMPEG
872 if (context != nullptr) {
873 if (need_to_build_proxy(context)) {
874 proxy_builder_process(context, stop, do_update, set_progress_fn);
875 }
876 }
877#endif
878 UNUSED_VARS(context, stop, do_update, set_progress_fn);
879}
880
881void MOV_proxy_builder_finish(MovieProxyBuilder *context, const bool stop)
882{
883#ifdef WITH_FFMPEG
884 if (context != nullptr) {
885 proxy_builder_finish(context, stop);
886 }
887#endif
888 /* static defined at top of the file */
889 UNUSED_VARS(context, stop, proxy_sizes);
890}
891
893{
894 if (anim == nullptr) {
895 return;
896 }
897
898 for (int i = 0; i < IMB_PROXY_MAX_SLOT; i++) {
899 if (anim->proxy_anim[i]) {
900 MOV_close(anim->proxy_anim[i]);
901 anim->proxy_anim[i] = nullptr;
902 }
903 }
904
905 anim->proxies_tried = 0;
906}
907
908void MOV_set_custom_proxy_dir(MovieReader *anim, const char *dir)
909{
910 if (STREQ(anim->proxy_dir, dir)) {
911 return;
912 }
913 STRNCPY(anim->proxy_dir, dir);
914
915 MOV_close_proxies(anim);
916}
917
919{
920 char filepath[FILE_MAX];
921 int i = proxy_size_to_array_index(preview_size);
922
923 if (i < 0) {
924 return nullptr;
925 }
926
927 if (anim->proxy_anim[i]) {
928 return anim->proxy_anim[i];
929 }
930
931 if (anim->proxies_tried & preview_size) {
932 return nullptr;
933 }
934
935 get_proxy_filepath(anim, preview_size, filepath, false);
936
937 /* Proxies are generated in the same color space as animation itself.
938 *
939 * Also skip any colorspace conversion to the color pipeline design as it helps performance and
940 * the image buffers from the proxy builder are not used anywhere else in Blender. */
941 ColorManagedColorspaceSettings colorspace_settings;
942 IMB_colormanagement_colorspace_settings_set(&colorspace_settings, anim->colorspace);
943 anim->proxy_anim[i] = MOV_open_file(filepath, ImBufFlags::Zero, 0, true, &colorspace_settings);
944
945 anim->proxies_tried |= preview_size;
946
947 return anim->proxy_anim[i];
948}
949
951{
952 const int num_proxy_sizes = IMB_PROXY_MAX_SLOT;
953 int existing = IMB_PROXY_NONE;
954 for (int i = 0; i < num_proxy_sizes; i++) {
955 IMB_Proxy_Size proxy_size = proxy_sizes[i];
956 char filepath[FILE_MAX];
957 get_proxy_filepath(anim, proxy_size, filepath, false);
958 if (BLI_exists(filepath)) {
959 existing |= int(proxy_size);
960 }
961 }
962 return existing;
963}
964
965} // namespace blender
File and directory operations.
#define BLI_path_ncmp
#define FILE_MAXFILE
#define FILE_MAX
#define BLI_path_join(...)
#define FILE_MAXDIR
#define SNPRINTF(dst, format,...)
Platform independent time functions.
const char * dirname(char *path)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_INFO_NOCHECK(clg_ref, format,...)
Definition CLG_log.h:204
Read Blender memory allocation and freeing.
#define UNUSED_VARS(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
bool add(const Key &key)
Definition BLI_set.hh:248
FFMPEG_INLINE size_t ffmpeg_get_buffer_alignment()
FFMPEG_INLINE enum AVPixelFormat * ffmpeg_get_pix_fmts(struct AVCodecContext *context, const AVCodec *codec)
FFMPEG_INLINE void my_guess_pkt_duration(AVFormatContext *s, AVStream *st, AVPacket *pkt)
FFMPEG_INLINE void ffmpeg_copy_display_matrix(const AVStream *src, AVStream *dst)
#define floor
#define LOG(level)
Definition log.h:97
int context(const bContext *C, const char *member, bContextDataResult *result)
std::string filename(const std::string_view &str, size_t pos)
BMesh Iterators.
static void get_proxy_dir(const MovieReader *anim, char *proxy_dir, size_t proxy_dir_maxncpy)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
char * STRNCPY(char(&dst)[N], const char *src)
MovieProxyBuilder * MOV_proxy_builder_start(MovieReader *anim, int proxy_sizes_in_use, int quality, const bool overwrite, Set< std::string > *processed_paths, bool build_only_on_bad_performance)
static void get_proxy_filepath(const MovieClip *clip, int proxy_render_size, bool undistorted, int framenr, char filepath[FILE_MAX])
Definition movieclip.cc:416
MINLINE int round_fl_to_int(float a)
static const float proxy_fac[]
void MOV_close(MovieReader *anim)
Definition movie_read.cc:70
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
MINLINE int max_ii(int a, int b)
void MOV_proxy_builder_finish(MovieProxyBuilder *context, const bool stop)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
MovieReader * MOV_open_file(const char *filepath, const ImBufFlags ib_flags, const int streamindex, const bool keep_original_colorspace, ColorManagedColorspaceSettings *colorspace_settings)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
void IMB_colormanagement_colorspace_settings_set(ColorManagedColorspaceSettings *settings, const char *name)
static const IMB_Proxy_Size proxy_sizes[]
MovieReader * movie_open_proxy(MovieReader *anim, IMB_Proxy_Size preview_size)
int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL()
Definition fileops_c.cc:541
static bool get_proxy_filepath(const MovieReader *anim, IMB_Proxy_Size preview_size, char *filepath, bool temp)
static int proxy_size_to_array_index(IMB_Proxy_Size pr_size)
void BLI_path_split_dir_file(const char *filepath, char *dir, size_t dir_maxncpy, char *file, size_t file_maxncpy) ATTR_NONNULL(1
int MOV_get_existing_proxies(const MovieReader *anim)
static CLG_LogRef LOG
Definition logging.cpp:12
double BLI_time_now_seconds()
Definition time.cc:30
void MOV_set_custom_proxy_dir(MovieReader *anim, const char *dir)
void MOV_close_proxies(MovieReader *anim)
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:454
void MOV_proxy_builder_process(MovieProxyBuilder *context, const bool *stop, bool *do_update, const FunctionRef< void(float progress)> set_progress_fn)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
MovieReader * proxy_anim[IMB_PROXY_MAX_SLOT]
Definition movie_read.hh:93