30# include <libavcodec/avcodec.h>
31# include <libavdevice/avdevice.h>
32# include <libavformat/avformat.h>
33# include <libavutil/hwcontext.h>
34# include <libavutil/log.h>
42static CLG_LogRef
LOG = {
"video.ffmpeg"};
44static char ffmpeg_last_error_buffer[1024];
48# pragma GCC diagnostic push
49# pragma GCC diagnostic ignored "-Wmissing-format-attribute"
52static size_t ffmpeg_log_to_buffer(
char *buffer,
53 const size_t buffer_maxncpy,
60 va_copy(args_cpy, arg);
67static void ffmpeg_log_callback(
void * ,
int level,
const char *format, va_list arg)
94 static std::mutex mutex;
95 std::scoped_lock
lock(mutex);
97 if (
ELEM(level, AV_LOG_PANIC, AV_LOG_FATAL, AV_LOG_ERROR)) {
98 const size_t n = ffmpeg_log_to_buffer(
99 ffmpeg_last_error_buffer,
sizeof(ffmpeg_last_error_buffer), format, arg);
101 ffmpeg_last_error_buffer[n - 1] =
'\0';
108 static char buffer[1024];
109 static int buffer_used = 0;
111 buffer_used += ffmpeg_log_to_buffer(
112 buffer + buffer_used,
sizeof(buffer) - buffer_used, format, arg);
114 if (buffer_used >=
sizeof(buffer) || (buffer_used > 0 && buffer[buffer_used - 1] ==
'\n')) {
115 if (buffer[buffer_used - 1] ==
'\n') {
116 buffer[buffer_used - 1] =
'\0';
125# pragma GCC diagnostic pop
128const char *ffmpeg_last_error()
130 return ffmpeg_last_error_buffer;
133static bool ffmpeg_container_has_real_video(
const AVFormatContext *format_ctx)
135 for (
int i = 0; i < format_ctx->nb_streams; i++) {
136 const AVStream *stream = format_ctx->streams[i];
137 if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
138 (stream->disposition & AV_DISPOSITION_ATTACHED_PIC) == 0)
146bool ffmpeg_stream_counts_as_video(
const AVFormatContext *format_ctx,
const AVStream *stream)
148 if (stream->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
151 if ((stream->disposition & AV_DISPOSITION_ATTACHED_PIC) == 0) {
154 return !ffmpeg_container_has_real_video(format_ctx);
157static int isffmpeg(
const char *filepath)
159 AVFormatContext *pFormatCtx =
nullptr;
162 const AVCodec *pCodec;
182 if (avformat_open_input(&pFormatCtx, filepath,
nullptr,
nullptr) != 0) {
186 if (avformat_find_stream_info(pFormatCtx,
nullptr) < 0) {
187 avformat_close_input(&pFormatCtx);
193 for (i = 0; i < pFormatCtx->nb_streams; i++) {
194 if (pFormatCtx->streams[i] && pFormatCtx->streams[i]->codecpar &&
195 (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO))
202 if (videoStream == -1) {
203 avformat_close_input(&pFormatCtx);
207 AVCodecParameters *codec_par = pFormatCtx->streams[videoStream]->codecpar;
210 pCodec = avcodec_find_decoder(codec_par->codec_id);
211 if (pCodec ==
nullptr) {
212 avformat_close_input(&pFormatCtx);
216 avformat_close_input(&pFormatCtx);
224# define MAX_NEG_CROP 1024
226# define times4(x) x, x, x, x
227# define times256(x) times4(times4(times4(times4(times4(x)))))
229static const uint8_t ff_compat_crop_tab[256 + 2 * MAX_NEG_CROP] = {
230 times256(0x00), 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
231 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
232 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22,
233 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E,
234 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
235 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
236 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52,
237 0x53, 0x54, 0x55, 0x56, 0x57, 0x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MAX_NEG_CROP;
270 sum += lum_m3[0] << 2;
271 sum += lum_m2[0] << 1;
272 sum += lum_m1[0] << 2;
274 dst[0] = cm[(sum + 4) >> 3];
287 const uint8_t *cm = ff_compat_crop_tab + MAX_NEG_CROP;
292 sum += lum_m3[0] << 2;
293 sum += lum_m2[0] << 1;
294 lum_m4[0] = lum_m2[0];
295 sum += lum_m1[0] << 2;
297 lum_m2[0] = cm[(sum + 4) >> 3];
311 uint8_t *dst,
int dst_wrap,
const uint8_t *src1,
int src_wrap,
int width,
int height)
313 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
318 src_0 = &src_m1[src_wrap];
319 src_p1 = &src_0[src_wrap];
320 src_p2 = &src_p1[src_wrap];
321 for (
y = 0;
y < (height - 2);
y += 2) {
322 memcpy(dst, src_m1, width);
324 deinterlace_line(dst, src_m2, src_m1, src_0, src_p1, src_p2, width);
328 src_p1 += 2 * src_wrap;
329 src_p2 += 2 * src_wrap;
332 memcpy(dst, src_m1, width);
335 deinterlace_line(dst, src_m2, src_m1, src_0, src_0, src_0, width);
343 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
347 return AVERROR(ENOMEM);
351 memcpy(buf, src_m1, width);
352 src_0 = &src_m1[src_wrap];
353 src_p1 = &src_0[src_wrap];
354 src_p2 = &src_p1[src_wrap];
355 for (
y = 0;
y < (height - 2);
y += 2) {
356 deinterlace_line_inplace(buf, src_m1, src_0, src_p1, src_p2, width);
359 src_p1 += 2 * src_wrap;
360 src_p2 += 2 * src_wrap;
363 deinterlace_line_inplace(buf, src_m1, src_0, src_0, src_0, width);
368int ffmpeg_deinterlace(
369 AVFrame *dst,
const AVFrame *src,
enum AVPixelFormat pix_fmt,
int width,
int height)
384 if ((width & 3) != 0 || (height & 3) != 0) {
388 for (i = 0; i < 3; i++) {
391 case AV_PIX_FMT_YUVJ420P:
392 case AV_PIX_FMT_YUV420P:
396 case AV_PIX_FMT_YUV422P:
397 case AV_PIX_FMT_YUVJ422P:
400 case AV_PIX_FMT_YUV411P:
406 if (pix_fmt == AV_PIX_FMT_GRAY8) {
411 ret = deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i], width, height);
417 deinterlace_bottom_field(
418 dst->data[i], dst->linesize[i], src->data[i], src->linesize[i], width, height);
428 return AV_CODEC_ID_NONE;
430 return AV_CODEC_ID_MPEG1VIDEO;
432 return AV_CODEC_ID_MPEG2VIDEO;
434 return AV_CODEC_ID_MPEG4;
436 return AV_CODEC_ID_FLV1;
438 return AV_CODEC_ID_DVVIDEO;
440 return AV_CODEC_ID_HUFFYUV;
442 return AV_CODEC_ID_H264;
444 return AV_CODEC_ID_THEORA;
446 return AV_CODEC_ID_FFV1;
448 return AV_CODEC_ID_QTRLE;
450 return AV_CODEC_ID_PNG;
452 return AV_CODEC_ID_DNXHD;
454 return AV_CODEC_ID_VP9;
456 return AV_CODEC_ID_H265;
458 return AV_CODEC_ID_AV1;
460 return AV_CODEC_ID_PRORES;
462 return AV_CODEC_ID_PCM_S16LE;
464 return AV_CODEC_ID_MP2;
466 return AV_CODEC_ID_MP3;
468 return AV_CODEC_ID_AAC;
470 return AV_CODEC_ID_AC3;
472 return AV_CODEC_ID_VORBIS;
474 return AV_CODEC_ID_FLAC;
476 return AV_CODEC_ID_OPUS;
480 return AV_CODEC_ID_NONE;
483static void ffmpeg_preset_set(
RenderData *rd,
int preset)
485 bool is_ntsc = (rd->frs_sec != 25);
491 rd->ffcodecdata.video_bitrate = 6000;
492 rd->ffcodecdata.gop_size = is_ntsc ? 18 : 15;
493 rd->ffcodecdata.rc_max_rate = 9000;
494 rd->ffcodecdata.rc_min_rate = 0;
495 rd->ffcodecdata.rc_buffer_size = 224 * 8;
496 rd->ffcodecdata.mux_packet_size = 2048;
497 rd->ffcodecdata.mux_rate = 10080000;
511 rd->ffcodecdata.video_bitrate = 6000;
512 rd->ffcodecdata.gop_size = is_ntsc ? 18 : 15;
513 rd->ffcodecdata.rc_max_rate = 9000;
514 rd->ffcodecdata.rc_min_rate = 0;
515 rd->ffcodecdata.rc_buffer_size = 224 * 8;
516 rd->ffcodecdata.mux_packet_size = 2048;
517 rd->ffcodecdata.mux_rate = 10080000;
523 rd->ffcodecdata.video_bitrate = 6000;
524 rd->ffcodecdata.gop_size = is_ntsc ? 18 : 15;
525 rd->ffcodecdata.rc_max_rate = 9000;
526 rd->ffcodecdata.rc_min_rate = 0;
527 rd->ffcodecdata.rc_buffer_size = 224 * 8;
528 rd->ffcodecdata.mux_packet_size = 2048;
529 rd->ffcodecdata.mux_rate = 10080000;
538 if (
ELEM(av_codec_id,
547 if (
ELEM(av_codec_id, AV_CODEC_ID_H265, AV_CODEC_ID_AV1, AV_CODEC_ID_FFV1)) {
550 if (
ELEM(av_codec_id, AV_CODEC_ID_FFV1)) {
558 if (av_codec_id == AV_CODEC_ID_PRORES) {
561 return ELEM(av_codec_id,
566 AV_CODEC_ID_HUFFYUV);
571 return ELEM(av_codec_id,
579int MOV_thread_count()
585static Mutex hw_device_lock;
588AVBufferRef *ffmpeg_hw_device_get(
const AVHWDeviceType device_type)
590 std::lock_guard
lock(hw_device_lock);
591 return hw_devices.lookup_or_add_cb(device_type, [&]() {
592 AVBufferRef *hw_device_ctx =
nullptr;
593 const int ret = av_hwdevice_ctx_create(&hw_device_ctx, device_type,
nullptr,
nullptr, 0);
595 char error_str[AV_ERROR_MAX_STRING_SIZE];
596 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE,
ret);
598 "ffmpeg: couldn't create %s decoding device: %s",
599 av_hwdevice_get_type_name(device_type),
602 return hw_device_ctx;
613 if (isffmpeg(filepath)) {
626 avdevice_register_all();
628 ffmpeg_last_error_buffer[0] =
'\0';
631 av_log_set_level(AV_LOG_TRACE);
634 av_log_set_level(AV_LOG_DEBUG);
637 av_log_set_level(AV_LOG_INFO);
641 av_log_set_callback(ffmpeg_log_callback);
649 std::lock_guard
lock(hw_device_lock);
650 for (AVBufferRef *&hw_device_ctx : hw_devices.values()) {
651 av_buffer_unref(&hw_device_ctx);
#define CLOG_CHECK(clg_ref, verbose_level,...)
#define CLOG_STR_AT_LEVEL(clg_ref, verbose_level, str)
#define CLOG_INFO(clg_ref,...)
#define BLI_assert_unreachable()
int size(const AssetLibraryReference *library_reference)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
size_t BLI_vsnprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format, va_list arg) ATTR_PRINTF_FORMAT(3
bool MOV_codec_supports_alpha(IMB_Ffmpeg_Codec_ID codec_id, int ffmpeg_profile)
eImageFormatDepth MOV_codec_valid_bit_depths(IMB_Ffmpeg_Codec_ID codec_id)
bool MOV_is_movie_file(const char *filepath)
int BLI_system_thread_count()
bool MOV_codec_supports_crf(IMB_Ffmpeg_Codec_ID codec_id)
void MOV_validate_output_settings(RenderData *rd, const ImageFormatData *imf)
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
@ FFMPEG_CODEC_ID_PCM_S16LE
@ FFMPEG_CODEC_ID_MPEG1VIDEO
@ FFMPEG_CODEC_ID_DVVIDEO
@ FFMPEG_CODEC_ID_HUFFYUV
@ FFMPEG_CODEC_ID_MPEG2VIDEO
@ FFM_PRORES_PROFILE_4444_XQ
@ FFM_PRORES_PROFILE_4444
bool BLI_path_extension_check_n(const char *path,...) ATTR_NONNULL(1) ATTR_SENTINEL(0)
eFFMpegPreset ffmpeg_preset
eFFMpegCrf constant_rate_factor
struct FFMpegCodecData ffcodecdata