40# include <libavutil/imgutils.h>
50static const float proxy_fac[] = {0.25, 0.50, 0.75, 1.00};
95 char stream_suffix[20];
96 const char *name = (temp) ?
"proxy_%d%s_part.avi" :
"proxy_%d%s.avi";
122struct proxy_output_ctx {
126 const AVCodec *codec;
130 AVRational output_timebase;
131 IMB_Proxy_Size proxy_size;
136static proxy_output_ctx *alloc_proxy_output_ffmpeg(
MovieReader *anim,
137 AVCodecContext *codec_ctx,
148 rv->proxy_size = proxy_size;
157 rv->of = avformat_alloc_context();
161 rv->of->oformat = av_guess_format(
"mp4",
nullptr,
nullptr);
163 rv->of->url = av_strdup(filepath);
165 rv->st = avformat_new_stream(rv->of,
nullptr);
168 rv->codec = avcodec_find_encoder(AV_CODEC_ID_H264);
170 rv->c = avcodec_alloc_context3(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);
180 rv->c->width = width;
181 rv->c->height = height;
182 rv->c->gop_size = 10;
183 rv->c->max_b_frames = 0;
187 rv->c->pix_fmt = pix_fmts[0];
190 rv->c->pix_fmt = AV_PIX_FMT_YUVJ420P;
193 rv->c->sample_aspect_ratio = rv->st->sample_aspect_ratio = st->sample_aspect_ratio;
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;
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);
209 AVDictionary *codec_opts =
nullptr;
211 av_dict_set_int(&codec_opts,
"crf", crf, 0);
215 av_dict_set(&codec_opts,
"preset",
"veryfast", 0);
216 av_dict_set(&codec_opts,
"tune",
"fastdecode", 0);
218 if (rv->codec->capabilities & AV_CODEC_CAP_OTHER_THREADS) {
219 rv->c->thread_count = 0;
222 rv->c->thread_count = MOV_thread_count();
225 if (rv->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) {
226 rv->c->thread_type = FF_THREAD_FRAME;
228 else if (rv->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) {
229 rv->c->thread_type = FF_THREAD_SLICE;
232 if (rv->of->oformat->flags & AVFMT_GLOBALHEADER) {
233 rv->c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
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;
241 int ret = avio_open(&rv->of->pb, filepath, AVIO_FLAG_WRITE);
244 char error_str[AV_ERROR_MAX_STRING_SIZE];
245 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE,
ret);
248 "Could not build proxy '%s': failed to create output file (%s)",
251 avcodec_free_context(&rv->c);
252 avformat_free_context(rv->of);
257 ret = avcodec_open2(rv->c, rv->codec, &codec_opts);
259 char error_str[AV_ERROR_MAX_STRING_SIZE];
260 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE,
ret);
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);
270 avcodec_parameters_from_context(rv->st->codecpar, rv->c);
273 rv->orig_height = st->codecpar->height;
275 if (st->codecpar->width != width || st->codecpar->height != height ||
276 st->codecpar->format != rv->c->pix_fmt)
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);
285 rv->sws_ctx = ffmpeg_sws_get_context(st->codecpar->width,
287 AVPixelFormat(st->codecpar->format),
288 codec_ctx->color_range == AVCOL_RANGE_JPEG,
293 codec_ctx->color_range == AVCOL_RANGE_JPEG,
298 ret = avformat_write_header(rv->of,
nullptr);
300 char error_str[AV_ERROR_MAX_STRING_SIZE];
301 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE,
ret);
304 &
LOG,
"Could not build proxy '%s': failed to write header (%s)", filepath, error_str);
307 av_frame_free(&rv->frame);
310 avcodec_free_context(&rv->c);
311 avformat_free_context(rv->of);
319static void add_to_proxy_output_ffmpeg(proxy_output_ctx *ctx,
321 AVRational input_timebase)
327 const int64_t src_pts = frame ? frame->pts : AV_NOPTS_VALUE;
329 if (ctx->sws_ctx && frame &&
330 (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]))
332 ffmpeg_sws_scale_frame(ctx->sws_ctx, ctx->frame, frame);
335 frame = ctx->sws_ctx ? (frame ? ctx->frame :
nullptr) : frame;
338 if (src_pts != AV_NOPTS_VALUE) {
339 frame->pts = av_rescale_q(src_pts, input_timebase, ctx->output_timebase);
342 frame->pts = ctx->cfra;
347 int ret = avcodec_send_frame(ctx->c, frame);
350 char error_str[AV_ERROR_MAX_STRING_SIZE];
351 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE,
ret);
354 &
LOG,
"Building proxy '%s': failed to send video frame (%s)", ctx->of->url, error_str);
357 AVPacket *packet = av_packet_alloc();
360 ret = avcodec_receive_packet(ctx->c, packet);
362 if (
ret == AVERROR(EAGAIN) ||
ret == AVERROR_EOF) {
367 char error_str[AV_ERROR_MAX_STRING_SIZE];
368 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE,
ret);
371 "Building proxy '%s': error encoding frame #%i (%s)",
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
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);
390 "Building proxy '%s': error writing frame #%i (%s)",
398 av_packet_free(&packet);
401static void free_proxy_output_ffmpeg(proxy_output_ctx *ctx,
int rollback)
412 add_to_proxy_output_ffmpeg(ctx,
nullptr, {1, 1});
415 av_write_trailer(ctx->of);
417 if (ctx->of->oformat) {
418 if (!(ctx->of->oformat->flags & AVFMT_NOFILE)) {
419 avio_close(ctx->of->pb);
422 avcodec_free_context(&ctx->c);
423 avformat_free_context(ctx->of);
426 ffmpeg_sws_release_context(ctx->sws_ctx);
427 ctx->sws_ctx =
nullptr;
430 av_frame_free(&ctx->frame);
446struct MovieProxyBuilder {
448 AVFormatContext *iFormatCtx;
449 AVCodecContext *iCodecCtx;
450 const AVCodec *iCodec;
458 int proxy_sizes_in_use;
460 bool build_only_on_bad_performance;
461 bool building_cancelled;
464static MovieProxyBuilder *proxy_builder_create(
MovieReader *anim,
465 int proxy_sizes_in_use,
467 bool build_only_on_bad_performance)
470 if (anim->never_seek_decode_one_frame) {
478 context->proxy_sizes_in_use = proxy_sizes_in_use;
480 context->build_only_on_bad_performance = build_only_on_bad_performance;
484 if (avformat_open_input(&
context->iFormatCtx, anim->filepath,
nullptr,
nullptr) != 0) {
489 if (avformat_find_stream_info(
context->iFormatCtx,
nullptr) < 0) {
490 avformat_close_input(&
context->iFormatCtx);
495 streamcount = anim->streamindex;
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) {
510 if (
context->videoStream == -1) {
511 avformat_close_input(&
context->iFormatCtx);
518 context->iCodec = avcodec_find_decoder(
context->iStream->codecpar->codec_id);
520 if (
context->iCodec ==
nullptr) {
521 avformat_close_input(&
context->iFormatCtx);
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;
530 if (
context->iCodec->capabilities & AV_CODEC_CAP_OTHER_THREADS) {
531 context->iCodecCtx->thread_count = 0;
534 context->iCodecCtx->thread_count = MOV_thread_count();
537 if (
context->iCodec->capabilities & AV_CODEC_CAP_FRAME_THREADS) {
538 context->iCodecCtx->thread_type = FF_THREAD_FRAME;
540 else if (
context->iCodec->capabilities & AV_CODEC_CAP_SLICE_THREADS) {
541 context->iCodecCtx->thread_type = FF_THREAD_SLICE;
544 if (avcodec_open2(
context->iCodecCtx,
context->iCodec,
nullptr) < 0) {
545 avformat_close_input(&
context->iFormatCtx);
546 avcodec_free_context(&
context->iCodecCtx);
551 for (i = 0; i < num_proxy_sizes; i++) {
556 height += height % 2;
557 context->proxy_ctx[i] = alloc_proxy_output_ffmpeg(
565 if (
context->proxy_ctx[0] ==
nullptr &&
context->proxy_ctx[1] ==
nullptr &&
566 context->proxy_ctx[2] ==
nullptr &&
context->proxy_ctx[3] ==
nullptr)
568 avformat_close_input(&
context->iFormatCtx);
569 avcodec_free_context(&
context->iCodecCtx);
577static void proxy_builder_finish(MovieProxyBuilder *context,
const bool stop)
579 const bool do_rollback = stop ||
context->building_cancelled;
581 for (
int i = 0; i <
context->num_proxy_sizes; i++) {
583 free_proxy_output_ffmpeg(
context->proxy_ctx[i], do_rollback);
587 avcodec_free_context(&
context->iCodecCtx);
588 avformat_close_input(&
context->iFormatCtx);
593static void proxy_builder_proc_decoded_frame(MovieProxyBuilder *context, AVFrame *in_frame)
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);
600static int proxy_builder_process(MovieProxyBuilder *context,
603 const FunctionRef<
void(
float progress)> set_progress_fn)
605 AVFrame *in_frame = av_frame_alloc();
606 AVPacket *next_packet = av_packet_alloc();
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;
614 if (progress != next_progress) {
615 progress = next_progress;
617 if (set_progress_fn) {
618 set_progress_fn(progress);
626 if (next_packet->stream_index ==
context->videoStream) {
627 int ret = avcodec_send_packet(
context->iCodecCtx, next_packet);
629 ret = avcodec_receive_frame(
context->iCodecCtx, in_frame);
631 if (
ret == AVERROR(EAGAIN) ||
ret == AVERROR_EOF) {
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);
642 proxy_builder_proc_decoded_frame(context, in_frame);
645 av_packet_unref(next_packet);
654 int ret = avcodec_send_packet(
context->iCodecCtx,
nullptr);
657 ret = avcodec_receive_frame(
context->iCodecCtx, in_frame);
659 if (
ret == AVERROR(EAGAIN) ||
ret == AVERROR_EOF) {
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);
669 proxy_builder_proc_decoded_frame(context, in_frame);
673 av_packet_free(&next_packet);
680static int performance_get_decode_rate(MovieProxyBuilder *context,
const double time_period)
682 AVFrame *in_frame = av_frame_alloc();
683 AVPacket *packet = av_packet_alloc();
686 int frames_decoded = 0;
688 while (av_read_frame(
context->iFormatCtx, packet) >= 0) {
689 if (packet->stream_index !=
context->videoStream) {
690 av_packet_unref(packet);
694 int ret = avcodec_send_packet(
context->iCodecCtx, packet);
696 ret = avcodec_receive_frame(
context->iCodecCtx, in_frame);
698 if (
ret == AVERROR(EAGAIN) ||
ret == AVERROR_EOF) {
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);
713 if (end > start + time_period) {
716 av_packet_unref(packet);
719 av_packet_free(&packet);
720 av_frame_free(&in_frame);
722 avcodec_flush_buffers(
context->iCodecCtx);
723 av_seek_frame(
context->iFormatCtx, -1, 0, AVSEEK_FLAG_BACKWARD);
724 return frames_decoded;
730static int performance_get_max_gop_size(MovieProxyBuilder *context)
732 AVPacket *packet = av_packet_alloc();
734 const int packets_max = 10000;
735 int packet_index = 0;
739 while (av_read_frame(
context->iFormatCtx, packet) >= 0) {
740 if (packet->stream_index !=
context->videoStream) {
741 av_packet_unref(packet);
747 if (packet->flags & AV_PKT_FLAG_KEY) {
748 max_gop =
max_ii(max_gop, cur_gop);
752 if (packet_index > packets_max) {
755 av_packet_unref(packet);
758 av_packet_free(&packet);
760 av_seek_frame(
context->iFormatCtx, -1, 0, AVSEEK_FLAG_BACKWARD);
771static bool need_to_build_proxy(MovieProxyBuilder *context)
773 if (!
context->build_only_on_bad_performance) {
778 performance_get_decode_rate(context, 0.1);
781 const int decode_rate = performance_get_decode_rate(context, 0.1);
782 const int max_gop_size = performance_get_max_gop_size(context);
784 if (max_gop_size <= 10 || max_gop_size < decode_rate) {
786 "Skipping proxy building for %s: Decoding performance is already good.",
788 context->building_cancelled =
true;
802 int proxy_sizes_in_use,
804 const bool overwrite,
806 bool build_only_on_bad_performance)
808 int proxy_sizes_to_build = proxy_sizes_in_use;
811 if (processed_paths !=
nullptr) {
814 if ((proxy_size & proxy_sizes_to_build) == 0) {
821 if (!processed_paths->
add(filepath)) {
822 proxy_sizes_to_build &= ~int(proxy_size);
830 if (built_proxies != 0) {
833 if (proxy_size & built_proxies) {
842 proxy_sizes_to_build &= ~built_proxies;
845 if (proxy_sizes_to_build == 0) {
849 MovieProxyBuilder *context =
nullptr;
852 context = proxy_builder_create(
853 anim, proxy_sizes_to_build, quality, build_only_on_bad_performance);
869 const FunctionRef<
void(
float progress)> set_progress_fn)
872 if (context !=
nullptr) {
873 if (need_to_build_proxy(context)) {
874 proxy_builder_process(context, stop, do_update, set_progress_fn);
878 UNUSED_VARS(context, stop, do_update, set_progress_fn);
884 if (context !=
nullptr) {
885 proxy_builder_finish(context, stop);
894 if (anim ==
nullptr) {
954 for (
int i = 0; i < num_proxy_sizes; i++) {
959 existing |= int(proxy_size);
File and directory operations.
#define BLI_path_join(...)
#define SNPRINTF(dst, format,...)
Platform independent time functions.
const char * dirname(char *path)
#define CLOG_ERROR(clg_ref,...)
#define CLOG_INFO_NOCHECK(clg_ref, format,...)
Read Blender memory allocation and freeing.
#define BLI_assert_msg(a, msg)
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)
int context(const bContext *C, const char *member, bContextDataResult *result)
std::string filename(const std::string_view &str, size_t pos)
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
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])
MINLINE int round_fl_to_int(float a)
static const float proxy_fac[]
void MOV_close(MovieReader *anim)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
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()
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)
double BLI_time_now_seconds()
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)
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]