46# include <libavcodec/avcodec.h>
47# include <libavformat/avformat.h>
48# include <libavutil/hwcontext.h>
49# include <libavutil/imgutils.h>
50# include <libavutil/rational.h>
51# include <libswscale/swscale.h>
61static CLG_LogRef
LOG = {
"video.read"};
72 if (anim ==
nullptr) {
77 free_anim_ffmpeg(anim);
95 av_log(anim->format_ctx, AV_LOG_DEBUG,
"METADATA FETCH\n");
97 AVDictionaryEntry *entry =
nullptr;
99 entry = av_dict_get(anim->format_ctx->
metadata,
"", entry, AV_DICT_IGNORE_SUFFIX);
100 if (entry ==
nullptr) {
129 const int cicp[4] = {anim->codec_ctx->color_primaries,
130 anim->codec_ctx->color_trc,
132 anim->codec_ctx->color_range};
147 const int streamindex,
148 const bool keep_original_colorspace,
155 anim = MEM_new<MovieReader>(
"anim struct");
156 if (anim !=
nullptr) {
163 if (colorspace_settings && colorspace_settings->
name[0] !=
'\0') {
173 if (colorspace_settings) {
184#if !defined(WITH_FFMPEG)
189 if (anim->codec_ctx !=
nullptr) {
203static double ffmpeg_stream_start_time_get(
const AVStream *stream)
205 if (stream->start_time == AV_NOPTS_VALUE) {
209 return stream->start_time * av_q2d(stream->time_base);
212static int ffmpeg_container_frame_count_get(
const AVFormatContext *format_ctx,
213 const AVStream *video_stream,
214 const double frame_rate)
221 const double video_start = ffmpeg_stream_start_time_get(video_stream);
222 double audio_start = 0;
224 for (
int i = 0; i < format_ctx->nb_streams; i++) {
225 if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
226 AVStream *audio_stream = format_ctx->streams[i];
227 audio_start = ffmpeg_stream_start_time_get(audio_stream);
234 if (video_start > audio_start) {
235 stream_dur = double(format_ctx->duration) / AV_TIME_BASE - (video_start - audio_start);
241 stream_dur = double(format_ctx->duration) / AV_TIME_BASE;
244 return lround(stream_dur * frame_rate);
247static int ffmpeg_frame_count_get(
const AVFormatContext *format_ctx,
248 const AVStream *video_stream,
249 const double frame_rate)
252 if (video_stream->duration != AV_NOPTS_VALUE) {
253 const double stream_dur = video_stream->duration * av_q2d(video_stream->time_base);
254 return lround(stream_dur * frame_rate);
260 if (format_ctx->duration != AV_NOPTS_VALUE) {
261 return ffmpeg_container_frame_count_get(format_ctx, video_stream, frame_rate);
265 if (video_stream->nb_frames != 0) {
266 return video_stream->nb_frames;
273 BLI_assert(format_ctx->duration == AV_NOPTS_VALUE);
277static int calc_pix_fmt_max_component_bits(AVPixelFormat fmt)
279 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
280 if (desc ==
nullptr) {
284 for (
int i = 0; i < desc->nb_components; i++) {
290static AVFormatContext *init_format_context(
const char *filepath,
291 int video_stream_index,
293 const AVCodec *forced_video_decoder)
295 AVFormatContext *format_ctx =
nullptr;
296 if (forced_video_decoder !=
nullptr) {
297 format_ctx = avformat_alloc_context();
298 format_ctx->video_codec_id = forced_video_decoder->id;
299 format_ctx->video_codec = forced_video_decoder;
302 if (avformat_open_input(&format_ctx, filepath,
nullptr,
nullptr) != 0) {
306 if (avformat_find_stream_info(format_ctx,
nullptr) < 0) {
307 avformat_close_input(&format_ctx);
311 av_dump_format(format_ctx, 0, filepath, 0);
315 for (
int i = 0; i < format_ctx->nb_streams; i++) {
316 if (ffmpeg_stream_counts_as_video(format_ctx, format_ctx->streams[i])) {
317 if (video_stream_index > 0) {
318 video_stream_index--;
326 if (r_stream_index == -1) {
327 avformat_close_input(&format_ctx);
334static AVFormatContext *init_format_context_vpx_workarounds(
const char *filepath,
335 int video_stream_index,
337 const AVCodec *&r_codec)
339 AVFormatContext *format_ctx = init_format_context(
340 filepath, video_stream_index, r_stream_index,
nullptr);
341 if (format_ctx ==
nullptr) {
352 const AVStream *video_stream = format_ctx->streams[r_stream_index];
353 if (
ELEM(video_stream->codecpar->codec_id, AV_CODEC_ID_VP8, AV_CODEC_ID_VP9)) {
354 AVDictionaryEntry *tag =
nullptr;
355 tag = av_dict_get(video_stream->metadata,
"alpha_mode", tag, AV_DICT_IGNORE_SUFFIX);
356 if (tag &&
STREQ(tag->value,
"1")) {
357 r_codec = avcodec_find_decoder_by_name(
358 video_stream->codecpar->codec_id == AV_CODEC_ID_VP8 ?
"libvpx" :
"libvpx-vp9");
359 if (r_codec !=
nullptr) {
360 avformat_close_input(&format_ctx);
361 format_ctx = avformat_alloc_context();
362 format_ctx = init_format_context(filepath, video_stream_index, r_stream_index, r_codec);
363 if (format_ctx ==
nullptr) {
370 if (r_codec ==
nullptr) {
372 r_codec = avcodec_find_decoder(video_stream->codecpar->codec_id);
380static constexpr AVHWDeviceType ffmpeg_hw_device_types[] = {AV_HWDEVICE_TYPE_VIDEOTOOLBOX};
382static constexpr AVHWDeviceType ffmpeg_hw_device_types[] = {
383 AV_HWDEVICE_TYPE_D3D11VA, AV_HWDEVICE_TYPE_CUDA, AV_HWDEVICE_TYPE_VULKAN};
385static constexpr AVHWDeviceType ffmpeg_hw_device_types[] = {AV_HWDEVICE_TYPE_CUDA,
386 AV_HWDEVICE_TYPE_VULKAN};
389static bool ffmpeg_hw_codec_supports_device(
const AVCodec *codec,
const AVHWDeviceType device_type)
391 for (
int i = 0;
const AVCodecHWConfig *config = avcodec_get_hw_config(codec, i); i++) {
392 if ((config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) &&
393 config->device_type == device_type)
401static AVHWDeviceType ffmpeg_hw_device_type_forced()
404 return AV_HWDEVICE_TYPE_NONE;
406 switch (
U.video_decoding_device) {
408 return AV_HWDEVICE_TYPE_NONE;
410 return AV_HWDEVICE_TYPE_VIDEOTOOLBOX;
412 return AV_HWDEVICE_TYPE_D3D11VA;
414 return AV_HWDEVICE_TYPE_CUDA;
416 return AV_HWDEVICE_TYPE_VULKAN;
418 return AV_HWDEVICE_TYPE_NONE;
421static void ffmpeg_hw_setup_decode(
const MovieReader *anim,
422 AVCodecContext *codec_ctx,
423 const AVCodec *codec)
430 const char *pix_fmt_name = av_get_pix_fmt_name(codec_ctx->pix_fmt);
431 if (pix_fmt_name ==
nullptr) {
432 pix_fmt_name =
"unknown";
434 const AVHWDeviceType forced_device_type = ffmpeg_hw_device_type_forced();
436 Span(&forced_device_type, 1) :
437 Span(ffmpeg_hw_device_types,
439 for (
const AVHWDeviceType device_type : device_types) {
440 if (!ffmpeg_hw_codec_supports_device(codec, device_type)) {
443 if (AVBufferRef *hw_device_ctx = ffmpeg_hw_device_get(device_type)) {
444 codec_ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
446 "Using %s hardware decoding for %s %s %dx%d (%s)",
447 av_hwdevice_get_type_name(device_type),
457 "Using software decoding for %s %s %dx%d (%s)",
465static AVPixelFormat ffmpeg_codec_pix_fmt_get(
const AVCodecContext *codec_ctx)
470 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(codec_ctx->pix_fmt);
471 return (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) ? codec_ctx->sw_pix_fmt : codec_ctx->pix_fmt;
476 avcodec_free_context(&anim->codec_ctx);
477 avformat_close_input(&anim->format_ctx);
478 av_packet_free(&anim->cur_packet);
480 av_frame_free(&anim->frame);
481 av_frame_free(&anim->frame_sw);
482 av_frame_free(&anim->frame_backup);
483 av_frame_free(&anim->frame_rgb);
484 if (anim->frame_deinterlaced->data[0] !=
nullptr) {
485 MEM_delete(anim->frame_deinterlaced->data[0]);
487 av_frame_free(&anim->frame_deinterlaced);
488 if (anim->sws_ctx !=
nullptr) {
489 ffmpeg_sws_release_context(anim->sws_ctx);
490 anim->sws_ctx =
nullptr;
496 if (anim ==
nullptr) {
500 int video_stream_index;
501 const AVCodec *codec =
nullptr;
502 AVFormatContext *format_ctx = init_format_context_vpx_workarounds(
503 anim->filepath, anim->streamindex, video_stream_index, codec);
504 if (format_ctx ==
nullptr || codec ==
nullptr) {
505 avformat_close_input(&format_ctx);
509 AVCodecContext *codec_ctx = avcodec_alloc_context3(
nullptr);
510 AVStream *video_stream = format_ctx->streams[video_stream_index];
511 avcodec_parameters_to_context(codec_ctx, video_stream->codecpar);
512 codec_ctx->workaround_bugs = FF_BUG_AUTODETECT;
514 if (codec->capabilities & AV_CODEC_CAP_OTHER_THREADS) {
515 codec_ctx->thread_count = 0;
518 codec_ctx->thread_count = MOV_thread_count();
521 if (codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) {
522 codec_ctx->thread_type = FF_THREAD_FRAME;
524 else if (codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) {
525 codec_ctx->thread_type = FF_THREAD_SLICE;
528 ffmpeg_hw_setup_decode(anim, codec_ctx, codec);
530 if (avcodec_open2(codec_ctx, codec,
nullptr) < 0) {
531 avcodec_free_context(&codec_ctx);
532 avformat_close_input(&format_ctx);
535 if (codec_ctx->pix_fmt == AV_PIX_FMT_NONE) {
536 avcodec_free_context(&codec_ctx);
537 avformat_close_input(&format_ctx);
542 const bool is_ogg_container =
STREQ(format_ctx->iformat->name,
"ogg");
543 const bool is_non_ogg_video = video_stream->codecpar->codec_id != AV_CODEC_ID_THEORA;
544 const bool is_video_thumbnail = (video_stream->disposition & AV_DISPOSITION_ATTACHED_PIC) != 0;
545 anim->never_seek_decode_one_frame = is_ogg_container && is_non_ogg_video && is_video_thumbnail;
547 anim->frame_rate = av_guess_frame_rate(format_ctx, video_stream,
nullptr);
548 if (anim->never_seek_decode_one_frame) {
551 anim->frame_rate = {24, 1};
553 int frs_num = anim->frame_rate.num;
554 double frs_den = anim->frame_rate.den;
556 frs_den *= AV_TIME_BASE;
558 while (frs_num % 10 == 0 && frs_den >= 2.0 && frs_num > 10) {
563 anim->frs_sec = frs_num;
564 anim->frs_sec_base = frs_den / AV_TIME_BASE;
567 anim->start_offset = ffmpeg_stream_start_time_get(video_stream);
568 anim->duration_in_frames = ffmpeg_frame_count_get(
569 format_ctx, video_stream, av_q2d(anim->frame_rate));
571 anim->x = codec_ctx->width;
572 anim->y = codec_ctx->height;
576 anim->is_float = calc_pix_fmt_max_component_bits(ffmpeg_codec_pix_fmt_get(codec_ctx)) > 8;
578 anim->format_ctx = format_ctx;
579 anim->codec_ctx = codec_ctx;
581 anim->video_stream_index = video_stream_index;
583 anim->cur_frame_index = 0;
585 anim->cur_key_frame_pts = -1;
586 anim->cur_packet = av_packet_alloc();
587 anim->cur_packet->stream_index = -1;
589 anim->frame = av_frame_alloc();
590 anim->frame_sw = av_frame_alloc();
591 anim->frame_backup = av_frame_alloc();
592 anim->frame_backup_complete =
false;
593 anim->frame_complete =
false;
594 anim->frame_deinterlaced = av_frame_alloc();
595 anim->frame_rgb = av_frame_alloc();
599 anim->frame_rgb->format = anim->is_float ? AV_PIX_FMT_GBRAPF32LE : AV_PIX_FMT_RGBA;
600 anim->frame_rgb->width = anim->x;
601 anim->frame_rgb->height = anim->y;
604 if (av_frame_get_buffer(anim->frame_rgb, align) < 0) {
606 ffmpeg_free_decoder(anim);
611 anim->frame_deinterlaced->format = anim->codec_ctx->pix_fmt;
612 anim->frame_deinterlaced->width = anim->codec_ctx->width;
613 anim->frame_deinterlaced->height = anim->codec_ctx->height;
614 av_image_fill_arrays(
615 anim->frame_deinterlaced->data,
616 anim->frame_deinterlaced->linesize,
618 av_image_get_buffer_size(
619 anim->codec_ctx->pix_fmt, anim->codec_ctx->width, anim->codec_ctx->height, 1),
620 "ffmpeg deinterlace"),
621 anim->codec_ctx->pix_fmt,
622 anim->codec_ctx->width,
623 anim->codec_ctx->height,
627 anim->src_pix_fmt = ffmpeg_codec_pix_fmt_get(anim->codec_ctx);
633 anim->sws_ctx = ffmpeg_sws_get_context(anim->x,
636 anim->codec_ctx->color_range == AVCOL_RANGE_JPEG,
637 anim->codec_ctx->colorspace,
640 anim->frame_rgb->format,
643 SWS_POINT | SWS_FULL_CHR_H_INT | SWS_ACCURATE_RND);
645 if (!anim->sws_ctx) {
647 "ffmpeg: swscale can't transform from pixel format %s to %s (%s)",
648 av_get_pix_fmt_name(anim->src_pix_fmt),
649 av_get_pix_fmt_name((AVPixelFormat)anim->frame_rgb->format),
651 ffmpeg_free_decoder(anim);
658static double ffmpeg_pts_per_frame_get(
const MovieReader *anim)
660 const AVStream *v_st = anim->format_ctx->streams[anim->video_stream_index];
661 const AVRational time_base = v_st->time_base;
662 return av_q2d(av_inv_q(av_mul_q(anim->frame_rate, time_base)));
670static void ffmpeg_double_buffer_backup_frame_store(
MovieReader *anim,
int64_t target_pts)
673 if (anim->frame_backup_complete && anim->cur_pts >= target_pts) {
676 if (!anim->frame_complete) {
680 if (anim->frame_backup_complete) {
681 av_frame_unref(anim->frame_backup);
684 av_frame_move_ref(anim->frame_backup, anim->frame);
685 anim->frame_backup_complete =
true;
689static void ffmpeg_double_buffer_backup_frame_clear(
MovieReader *anim)
691 if (anim->frame_backup_complete) {
692 av_frame_unref(anim->frame_backup);
694 anim->frame_backup_complete =
false;
698static AVFrame *ffmpeg_double_buffer_frame_fallback_get(
MovieReader *anim)
700 av_log(anim->format_ctx, AV_LOG_ERROR,
"DECODE UNHAPPY: PTS not matched!\n");
702 if (anim->frame_complete) {
705 if (anim->frame_backup_complete) {
706 return anim->frame_backup;
713static void float_planar_to_interleaved(
const AVFrame *frame,
const int rotation,
ImBuf *ibuf)
715 const size_t src_linesize = frame->linesize[0];
716 BLI_assert_msg(frame->linesize[1] == src_linesize && frame->linesize[2] == src_linesize &&
717 frame->linesize[3] == src_linesize,
718 "ffmpeg frame should be 4 same size planes for a floating point image case");
719 float *float_data = ibuf->float_data_for_write();
721 const int size_x = ibuf->x;
722 const int size_y = ibuf->y;
723 if (rotation == 90) {
725 for (const int64_t y : y_range) {
726 int64_t src_offset = src_linesize * (size_y - y - 1);
727 const float *src_g = reinterpret_cast<const float *>(frame->data[0] + src_offset);
728 const float *src_b = reinterpret_cast<const float *>(frame->data[1] + src_offset);
729 const float *src_r = reinterpret_cast<const float *>(frame->data[2] + src_offset);
730 const float *src_a = reinterpret_cast<const float *>(frame->data[3] + src_offset);
731 float *dst = float_data + (y + (size_x - 1) * size_y) * 4;
732 for (int x = 0; x < size_x; x++) {
741 else if (rotation == 180) {
744 int64_t src_offset = src_linesize * (size_y -
y - 1);
745 const float *src_g =
reinterpret_cast<const float *
>(frame->data[0] + src_offset);
746 const float *src_b =
reinterpret_cast<const float *
>(frame->data[1] + src_offset);
747 const float *src_r =
reinterpret_cast<const float *
>(frame->data[2] + src_offset);
748 const float *src_a =
reinterpret_cast<const float *
>(frame->data[3] + src_offset);
749 float *dst = float_data + ((size_y -
y - 1) * size_x + size_x - 1) * 4;
750 for (
int x = 0;
x < size_x;
x++) {
759 else if (rotation == 270) {
762 int64_t src_offset = src_linesize * (size_y -
y - 1);
763 const float *src_g =
reinterpret_cast<const float *
>(frame->data[0] + src_offset);
764 const float *src_b =
reinterpret_cast<const float *
>(frame->data[1] + src_offset);
765 const float *src_r =
reinterpret_cast<const float *
>(frame->data[2] + src_offset);
766 const float *src_a =
reinterpret_cast<const float *
>(frame->data[3] + src_offset);
767 float *dst = float_data + (size_y -
y - 1) * 4;
768 for (
int x = 0;
x < size_x;
x++) {
777 else if (rotation == 0) {
780 int64_t src_offset = src_linesize * (size_y -
y - 1);
781 const float *src_g =
reinterpret_cast<const float *
>(frame->data[0] + src_offset);
782 const float *src_b =
reinterpret_cast<const float *
>(frame->data[1] + src_offset);
783 const float *src_r =
reinterpret_cast<const float *
>(frame->data[2] + src_offset);
784 const float *src_a =
reinterpret_cast<const float *
>(frame->data[3] + src_offset);
785 float *dst = float_data + size_x *
y * 4;
786 for (
int x = 0;
x < size_x;
x++) {
796 if (
ELEM(rotation, 90, 270)) {
797 std::swap(ibuf->x, ibuf->y);
806static void ffmpeg_postprocess(MovieReader *anim, AVFrame *input, ImBuf *ibuf)
812 if (input->data[0] ==
nullptr && input->data[1] ==
nullptr && input->data[2] ==
nullptr &&
813 input->data[3] ==
nullptr)
817 "data not read properly...");
821 av_log(anim->format_ctx,
823 " POSTPROC: AVFrame planes: %p %p %p %p\n",
829 if (flag_is_set(anim->ib_flags, ImBufFlags::Deinterlace)) {
830 if (ffmpeg_deinterlace(anim->frame_deinterlaced,
832 anim->codec_ctx->pix_fmt,
833 anim->codec_ctx->width,
834 anim->codec_ctx->height) < 0)
839 input = anim->frame_deinterlaced;
846 if (input->format != anim->src_pix_fmt && anim->sws_ctx !=
nullptr) {
847 ffmpeg_sws_release_context(anim->sws_ctx);
848 anim->src_pix_fmt = AVPixelFormat(input->format);
849 anim->sws_ctx = ffmpeg_sws_get_context(anim->x,
852 anim->codec_ctx->color_range == AVCOL_RANGE_JPEG,
853 anim->codec_ctx->colorspace,
856 anim->frame_rgb->format,
859 SWS_POINT | SWS_FULL_CHR_H_INT | SWS_ACCURATE_RND);
862 if (anim->sws_ctx ==
nullptr) {
864 "ffmpeg: swscale can't transform from pixel format %s to %s (%s)",
865 av_get_pix_fmt_name(anim->src_pix_fmt),
866 av_get_pix_fmt_name(AVPixelFormat(anim->frame_rgb->format)),
871 bool already_rotated =
false;
872 if (anim->is_float) {
877 ffmpeg_sws_scale_frame(anim->sws_ctx, anim->frame_rgb, input);
879 float_planar_to_interleaved(anim->frame_rgb, anim->video_rotation, ibuf);
880 already_rotated =
true;
887 const int ibuf_linesize = ibuf->x * 4;
888 const int rgb_linesize = anim->frame_rgb->linesize[0];
889 bool scale_to_ibuf = (rgb_linesize == ibuf_linesize);
893# if (defined(__aarch64__) || defined(_M_ARM64)) && (LIBSWSCALE_VERSION_MAJOR < 7)
894 scale_to_ibuf =
false;
896 uint8_t *rgb_data = anim->frame_rgb->data[0];
901 anim->frame_rgb->linesize[0] = -ibuf_linesize;
902 anim->frame_rgb->data[0] = ibuf->byte_data_for_write() + (ibuf->y - 1) * ibuf_linesize;
904 ffmpeg_sws_scale_frame(anim->sws_ctx, anim->frame_rgb, input);
906 anim->frame_rgb->linesize[0] = rgb_linesize;
907 anim->frame_rgb->data[0] = rgb_data;
911 ffmpeg_sws_scale_frame(anim->sws_ctx, anim->frame_rgb, input);
914 const int src_linesize[4] = {-rgb_linesize, 0, 0, 0};
915 const uint8_t *
const src[4] = {
916 rgb_data + (anim->y - 1) * rgb_linesize,
nullptr,
nullptr,
nullptr};
917 int dst_size = av_image_get_buffer_size(AVPixelFormat(anim->frame_rgb->format),
918 anim->frame_rgb->width,
919 anim->frame_rgb->height,
921 av_image_copy_to_buffer(ibuf->byte_data_for_write(),
925 AVPixelFormat(anim->frame_rgb->format),
937 if (!already_rotated &&
ELEM(anim->video_rotation, 90, 180, 270)) {
942static void final_frame_log(MovieReader *anim,
947 av_log(anim->format_ctx,
949 "DECODE HAPPY: %s frame PTS range %" PRId64 " - %" PRId64 ".\n",
957 return pts_start <= target_pts && target_pts < pts_end;
961static AVFrame *ffmpeg_frame_by_pts_get(MovieReader *anim,
int64_t target_pts)
967 if (!anim->frame_complete) {
971 if (anim->never_seek_decode_one_frame) {
976 const bool backup_frame_ready = anim->frame_backup_complete;
981 AVFrame *best_frame =
nullptr;
982 if (ffmpeg_pts_isect(recent_start, recent_end, target_pts)) {
983 final_frame_log(anim, recent_start, recent_end,
"Recent");
984 best_frame = anim->frame;
986 else if (backup_frame_ready && ffmpeg_pts_isect(backup_start, recent_start, target_pts)) {
987 final_frame_log(anim, backup_start, recent_start,
"Backup");
988 best_frame = anim->frame_backup;
993static void ffmpeg_decode_store_frame_pts(MovieReader *anim)
997# ifdef FFMPEG_OLD_KEY_FRAME_QUERY_METHOD
998 if (anim->frame->key_frame)
1000 if (anim->frame->flags & AV_FRAME_FLAG_KEY)
1003 anim->cur_key_frame_pts = anim->cur_pts;
1006 av_log(anim->format_ctx,
1008 " FRAME DONE: cur_pts=%" PRId64 ", guessed_pts=%" PRId64 "\n",
1013static int ffmpeg_read_video_frame(MovieReader *anim, AVPacket *packet)
1016 while ((ret = av_read_frame(anim->format_ctx, packet)) >= 0) {
1017 if (packet->stream_index == anim->video_stream_index) {
1020 av_packet_unref(packet);
1021 packet->stream_index = -1;
1027static void ffmpeg_cur_packet_clear(MovieReader *anim)
1029 av_packet_unref(anim->cur_packet);
1030 anim->cur_packet->stream_index = -1;
1034static bool ffmpeg_decode_video_frame(MovieReader *anim)
1036 av_log(anim->format_ctx, AV_LOG_DEBUG,
" DECODE VIDEO FRAME\n");
1040 anim->frame_complete = avcodec_receive_frame(anim->codec_ctx, anim->frame) == 0;
1041 if (anim->frame_complete) {
1042 av_log(anim->format_ctx, AV_LOG_DEBUG,
" DECODE FROM CODEC BUFFER\n");
1043 ffmpeg_decode_store_frame_pts(anim);
1047 ffmpeg_cur_packet_clear(anim);
1050 while ((ret = ffmpeg_read_video_frame(anim, anim->cur_packet)) >= 0) {
1051 av_log(anim->format_ctx,
1054 anim->cur_packet->stream_index,
1055 (anim->cur_packet->dts == AV_NOPTS_VALUE) ? -1 :
int64_t(anim->cur_packet->dts),
1056 (anim->cur_packet->pts == AV_NOPTS_VALUE) ? -1 :
int64_t(anim->cur_packet->pts),
1057 (anim->cur_packet->flags & AV_PKT_FLAG_KEY) ?
" KEY" :
"");
1059 avcodec_send_packet(anim->codec_ctx, anim->cur_packet);
1060 anim->frame_complete = avcodec_receive_frame(anim->codec_ctx, anim->frame) == 0;
1061 if (anim->frame_complete) {
1062 ffmpeg_decode_store_frame_pts(anim);
1065 ffmpeg_cur_packet_clear(anim);
1068 if (ret == AVERROR_EOF) {
1070 avcodec_send_packet(anim->codec_ctx,
nullptr);
1071 anim->frame_complete = avcodec_receive_frame(anim->codec_ctx, anim->frame) == 0;
1072 if (anim->frame_complete) {
1073 ffmpeg_decode_store_frame_pts(anim);
1078 ffmpeg_cur_packet_clear(anim);
1080 char error_str[AV_ERROR_MAX_STRING_SIZE];
1081 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
1082 av_log(anim->format_ctx,
1084 " DECODE READ FAILED: av_read_frame() "
1085 "returned error: %s\n",
1090static int64_t ffmpeg_seek_pts_get(
const MovieReader *anim,
int64_t target_pts)
1099 int64_t seek_pts = target_pts - (ffmpeg_pts_per_frame_get(anim) * 3);
1101 seek_pts = std::max<int64_t>(seek_pts, 0);
1108static int64_t ffmpeg_frame_index_to_pts(MovieReader *anim,
int frame_index)
1110 AVStream *v_st = anim->format_ctx->streams[anim->video_stream_index];
1111 int64_t start_pts = v_st->start_time;
1113 int64_t target_pts =
round(frame_index * ffmpeg_pts_per_frame_get(anim));
1115 if (start_pts != AV_NOPTS_VALUE) {
1116 target_pts += start_pts;
1121static void ffmpeg_scan_log(MovieReader *anim,
int64_t target_pts)
1125 av_log(anim->format_ctx,
1134static void ffmpeg_decode_until_pts(MovieReader *anim,
int64_t target_pts,
const bool did_seek)
1136 const int64_t start_key_frame_pts = anim->cur_key_frame_pts;
1137 bool decode_error =
false;
1139 while (!decode_error && anim->cur_pts < target_pts) {
1140 ffmpeg_scan_log(anim, target_pts);
1141 ffmpeg_double_buffer_backup_frame_store(anim, target_pts);
1142 decode_error = !ffmpeg_decode_video_frame(anim);
1148 if (did_seek && start_key_frame_pts != anim->cur_key_frame_pts) {
1149 av_log(anim->format_ctx, AV_LOG_ERROR,
"SCAN: Frame belongs to an unexpected GOP!\n");
1158static int ffmpeg_generic_seek_workaround(MovieReader *anim,
int64_t *seek_pts,
int64_t target_pts)
1160 AVStream *v_st = anim->format_ctx->streams[anim->video_stream_index];
1161 int64_t start_pts = v_st->start_time;
1162 int64_t current_pts = *seek_pts;
1164 AVPacket *packet = av_packet_alloc();
1167 while (current_pts != 0) {
1168 current_pts = *seek_pts -
int64_t(
round(offset * ffmpeg_pts_per_frame_get(anim)));
1169 current_pts = std::max(current_pts,
int64_t(0));
1173 anim->format_ctx, anim->video_stream_index, current_pts, AVSEEK_FLAG_BACKWARD) < 0)
1179 ffmpeg_read_video_frame(anim, packet);
1182 const bool is_key_frame = packet->flags & AV_PKT_FLAG_KEY;
1187 av_packet_unref(packet);
1190 if (cur_pts <= target_pts) {
1197 if (cur_pts <= start_pts) {
1203 av_packet_free(&packet);
1205 *seek_pts = current_pts;
1208 return av_seek_frame(
1209 anim->format_ctx, anim->video_stream_index, current_pts, AVSEEK_FLAG_BACKWARD);
1214static void ffmpeg_seek_recover_stream_position(MovieReader *anim)
1216 AVPacket *temp_packet = av_packet_alloc();
1217 while (ffmpeg_read_video_frame(anim, temp_packet) >= 0) {
1220 av_packet_unref(temp_packet);
1222 if (current_pts == temp_pts) {
1226 av_packet_free(&temp_packet);
1230static bool ffmpeg_seek_needs_flush(MovieReader *anim,
int frame_index,
int64_t seek_pts)
1233 AVPacket *packet = av_packet_alloc();
1234 ffmpeg_read_video_frame(anim, packet);
1236 av_packet_free(&packet);
1240 if (seek_key_frame_pts ==
1248 if (seek_key_frame_pts == anim->cur_key_frame_pts && frame_index > anim->cur_frame_index) {
1249 ffmpeg_seek_recover_stream_position(anim);
1254 av_seek_frame(anim->format_ctx, anim->video_stream_index, seek_pts, AVSEEK_FLAG_BACKWARD);
1255 anim->cur_key_frame_pts = seek_key_frame_pts;
1260static void ffmpeg_seek_to_key_frame(MovieReader *anim,
int frame_index,
int64_t target_pts)
1262 int64_t seek_pts = ffmpeg_seek_pts_get(anim, target_pts);
1263 av_log(anim->format_ctx, AV_LOG_DEBUG,
"Final seek seek_pts = %" PRId64 "\n", seek_pts);
1269 if (!(anim->format_ctx->iformat->flags & AVFMT_TS_DISCONT)) {
1270 ret = av_seek_frame(
1271 anim->format_ctx, anim->video_stream_index, seek_pts, AVSEEK_FLAG_BACKWARD);
1274 ret = ffmpeg_generic_seek_workaround(anim, &seek_pts, target_pts);
1276 anim->format_ctx, AV_LOG_DEBUG,
"Adjusted final seek seek_pts = %" PRId64 "\n", seek_pts);
1279 if (ret <= 0 && !ffmpeg_seek_needs_flush(anim, frame_index, seek_pts)) {
1284 av_log(anim->format_ctx,
1287 "error while seeking to DTS = %" PRId64 " (frameno = %d, PTS = %" PRId64
1288 "): errcode = %d\n",
1296 avcodec_flush_buffers(anim->codec_ctx);
1297 ffmpeg_double_buffer_backup_frame_clear(anim);
1299 ffmpeg_cur_packet_clear(anim);
1302static bool ffmpeg_must_seek(
const MovieReader *anim,
int frame_index,
int64_t target_pts)
1304 if (!anim->frame_complete || frame_index < anim->cur_frame_index) {
1307 if (frame_index == anim->cur_frame_index + 1) {
1311 AVStream *stream = anim->format_ctx->streams[anim->video_stream_index];
1312 const int64_t seek_pts = ffmpeg_seek_pts_get(anim, target_pts);
1315 const int index = av_index_search_timestamp(stream, seek_pts, AVSEEK_FLAG_BACKWARD);
1319 const AVIndexEntry *key_frame = avformat_index_get_entry(stream,
index);
1320 if (key_frame ==
nullptr) {
1331 const double flush_cost_pts = ffmpeg_pts_per_frame_get(anim) * MOV_thread_count();
1332 return key_frame->timestamp > anim->cur_pts + flush_cost_pts;
1335static ImBuf *ffmpeg_fetchibuf(MovieReader *anim,
int frame_index)
1337 av_log(anim->format_ctx, AV_LOG_DEBUG,
"FETCH: frame_index=%d\n", frame_index);
1339 const int64_t target_pts = ffmpeg_frame_index_to_pts(anim, frame_index);
1341 if (anim->never_seek_decode_one_frame) {
1343 if (!anim->frame_complete) {
1344 ffmpeg_decode_video_frame(anim);
1349 const AVStream *v_st = anim->format_ctx->streams[anim->video_stream_index];
1350 av_log(anim->format_ctx,
1352 "FETCH: looking for PTS=%" PRId64 " (pts_timebase=%g, frame_rate=%g, start_pts=%" PRId64
1355 av_q2d(v_st->time_base),
1356 av_q2d(v_st->r_frame_rate),
1359 if (!anim->frame_complete || anim->cur_frame_index != frame_index) {
1360 const bool must_seek = ffmpeg_must_seek(anim, frame_index, target_pts);
1362 ffmpeg_seek_to_key_frame(anim, frame_index, target_pts);
1364 ffmpeg_decode_until_pts(anim, target_pts, must_seek);
1369 anim->x = anim->codec_ctx->width;
1370 anim->y = anim->codec_ctx->height;
1372 const AVPixFmtDescriptor *pix_fmt_descriptor = av_pix_fmt_desc_get(
1373 ffmpeg_codec_pix_fmt_get(anim->codec_ctx));
1376 if ((pix_fmt_descriptor->flags & AV_PIX_FMT_FLAG_ALPHA) == 0) {
1377 color_mode = ImColorMode::RGB;
1380 ImBuf *cur_frame_final =
IMB_allocImBuf(anim->x, anim->y, ImBufFlags::Zero);
1381 cur_frame_final->color_mode = color_mode;
1385 const size_t pixel_size = anim->is_float ? 16 : 4;
1388 if (anim->is_float) {
1389 cur_frame_final->assign_float_data((
float *)buffer_data);
1392 cur_frame_final->assign_byte_data(buffer_data);
1395 AVFrame *final_frame = ffmpeg_frame_by_pts_get(anim, target_pts);
1396 if (final_frame ==
nullptr) {
1399 final_frame = ffmpeg_double_buffer_frame_fallback_get(anim);
1403 if (final_frame !=
nullptr && final_frame->hw_frames_ctx !=
nullptr) {
1404 av_frame_unref(anim->frame_sw);
1405 const int ret = av_hwframe_transfer_data(anim->frame_sw, final_frame, 0);
1407 char error_str[AV_ERROR_MAX_STRING_SIZE];
1408 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
1409 CLOG_ERROR(&
LOG,
"ffmpeg: couldn't transfer frame from the decoding device: %s", error_str);
1410 final_frame =
nullptr;
1413 av_frame_copy_props(anim->frame_sw, final_frame);
1414 final_frame = anim->frame_sw;
1420 if (final_frame !=
nullptr) {
1421 ffmpeg_postprocess(anim, final_frame, cur_frame_final);
1424 memset(buffer_data, 0, pixel_size * anim->x * anim->y);
1427 if (anim->is_float) {
1428 if (anim->keep_original_colorspace) {
1442 cur_frame_final, anim->colorspace, ColorManagedFileOutput::Video);
1452 anim->cur_frame_index = frame_index;
1454 return cur_frame_final;
1457static void free_anim_ffmpeg(MovieReader *anim)
1459 if (anim ==
nullptr) {
1463 if (anim->codec_ctx) {
1464 ffmpeg_free_decoder(anim);
1466 anim->duration_in_frames = 0;
1477 if (anim ==
nullptr) {
1485 free_anim_ffmpeg(anim);
1486 if (startffmpeg(anim)) {
1497 ImBuf *ibuf =
nullptr;
1498 int frame_index = 0;
1516 if (anim->format_ctx) {
1517 AVStream *v_st = anim->format_ctx->streams[anim->video_stream_index];
1518 AVRational frame_rate = av_guess_frame_rate(anim->format_ctx, v_st,
nullptr);
1519 if (frame_rate.num != 0) {
1535 ImBuf *ibuf =
nullptr;
1536 if (anim ==
nullptr) {
1547 if (frame_index < 0) {
1563 ibuf = ffmpeg_fetchibuf(anim, frame_index);
1577 if (anim ==
nullptr) {
1583 if (anim->format_ctx ==
nullptr) {
1587 for (
int i = 0; i < anim->format_ctx->nb_streams; i++) {
1588 if (ffmpeg_stream_counts_as_video(anim->format_ctx, anim->format_ctx->streams[i])) {
#define SNPRINTF_UTF8(dst, format,...)
#define STRNCPY_UTF8(dst, src)
#define CLOG_ERROR(clg_ref,...)
#define CLOG_INFO(clg_ref,...)
#define USER_DEVELOPER_TOOL_TEST(userdef, member)
Read Blender memory allocation and freeing.
#define BLI_assert_msg(a, msg)
constexpr IndexRange()=default
FFMPEG_INLINE int64_t av_get_frame_duration_in_pts_units(const AVFrame *picture)
FFMPEG_INLINE size_t ffmpeg_get_buffer_alignment()
FFMPEG_INLINE int64_t timestamp_from_pts_or_dts(int64_t pts, int64_t dts)
FFMPEG_INLINE int64_t av_get_pts_from_frame(AVFrame *picture)
FFMPEG_INLINE int ffmpeg_get_video_rotation(const AVStream *stream)
void * MEM_new_uninitialized_aligned(size_t len, size_t alignment, const char *str)
std::string filename(const std::string_view &str, size_t pos)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
bool IMB_rotate_orthogonal(ImBuf *ibuf, int degrees)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool MOV_is_initialized_and_valid(const MovieReader *anim)
static bool anim_getnew(MovieReader *anim)
char * STRNCPY(char(&dst)[N], const char *src)
const ColorSpace * IMB_colormanagement_space_from_cicp(const int cicp[4], ColorManagedFileOutput output)
int MOV_get_image_height(const MovieReader *anim)
int MOV_get_image_width(const MovieReader *anim)
void MOV_close(MovieReader *anim)
bool MOV_get_fps_num_denom(const MovieReader *anim, short &r_fps_num, float &r_fps_denom)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
ImBuf * MOV_decode_frame(MovieReader *anim, int frame_index, IMB_Proxy_Size preview_size)
void colormanage_imbuf_make_linear(ImBuf *ibuf, const char *from_colorspace, const ColorManagedFileOutput output)
MINLINE int max_ii(int a, int b)
@ COLOR_ROLE_DEFAULT_BYTE
void MOV_set_multiview_suffix(MovieReader *anim, const char *suffix)
void IMB_metadata_ensure(IDProperty **metadata)
MovieReader * MOV_open_file(const char *filepath, const ImBufFlags ib_flags, const int streamindex, const bool keep_original_colorspace, ColorManagedColorspaceSettings *colorspace_settings)
void IMB_freeImBuf(ImBuf *ibuf)
const char * IMB_colormanagement_role_colorspace_name_get(int role)
void IMB_colormanagement_colorspace_settings_set(ColorManagedColorspaceSettings *settings, const char *name)
@ USER_GPU_FLAG_VIDEO_DECODING
const char * IMB_colormanagement_colorspace_get_name(const ColorSpace *colorspace)
MovieReader * movie_open_proxy(MovieReader *anim, IMB_Proxy_Size preview_size)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
double MOV_get_start_offset_seconds(const MovieReader *anim)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
IDProperty * MOV_load_metadata(MovieReader *anim)
const ColorSpace * colormanage_colorspace_get_named(const char *name)
int MOV_get_video_stream_count(MovieReader *anim)
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
float MOV_get_fps(const MovieReader *anim)
@ USER_VIDEO_DECODING_DEVICE_AUTOMATIC
@ USER_VIDEO_DECODING_DEVICE_CUDA
@ USER_VIDEO_DECODING_DEVICE_VULKAN
@ USER_VIDEO_DECODING_DEVICE_D3D11VA
@ USER_VIDEO_DECODING_DEVICE_VIDEOTOOLBOX
ImBuf * MOV_decode_preview_frame(MovieReader *anim)
void MOV_close_proxies(MovieReader *anim)
int MOV_get_duration_frames(const MovieReader *anim)
void IMB_filtery(ImBuf *ibuf)
static void probe_video_colorspace(MovieReader *anim, char r_colorspace_name[IM_MAX_SPACE])
void IMB_metadata_set_field(IDProperty *metadata, const char *key, const char *value)
void MOV_get_filename(const MovieReader *anim, char *filename, int filename_maxncpy)
ocio::ColorSpace ColorSpace
void IMB_metadata_free(IDProperty *metadata)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
IDProperty * metadata_for_write()
bool keep_original_colorspace