From 04fadf51e1f2440393d7b2081033798fc962ef2a Mon Sep 17 00:00:00 2001 From: wrapper Date: Sat, 16 Aug 2025 12:52:06 +0700 Subject: [PATCH 01/12] aac profiles test --- libavcodec/mfenc.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c index 942c75cb9d..a35a1d1602 100644 --- a/libavcodec/mfenc.c +++ b/libavcodec/mfenc.c @@ -16,6 +16,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavcodec/profiles.h" #define COBJMACROS #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0602 #undef _WIN32_WINNT @@ -736,6 +737,23 @@ static int mf_enca_output_adjust(AVCodecContext *avctx, IMFMediaType *type) //IMFAttributes_SetUINT32(type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, avctx->bit_rate / 8); //IMFAttributes_SetUINT32(type, &MF_MT_AVG_BITRATE, avctx->bit_rate); + if (avctx->codec_id == AV_CODEC_ID_AAC) { + switch (avctx->profile) { + case AV_PROFILE_AAC_LOW: + case AV_PROFILE_UNKNOWN: + IMFAttributes_SetUINT32(type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, 0x2B); + break; + + case AV_PROFILE_AAC_HE: + IMFAttributes_SetUINT32(type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, 0x2F); + break; + + case AV_PROFILE_AAC_HE_V2: + IMFAttributes_SetUINT32(type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, 0x33); + break; + } + } + return 0; } @@ -1427,7 +1445,13 @@ static av_cold int mf_init(AVCodecContext *avctx) .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID | \ AV_CODEC_CAP_DR1 | AV_CODEC_CAP_VARIABLE_FRAME_SIZE, -MF_ENCODER(AUDIO, aac, AAC, NULL, AFMTS, ACAPS, NULL); +#define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM +static const AVOption aac_opts[] = { + FF_AAC_PROFILE_OPTS + {NULL} +}; + +MF_ENCODER(AUDIO, aac, AAC, aac_opts, AFMTS, ACAPS, NULL); MF_ENCODER(AUDIO, ac3, AC3, NULL, AFMTS, ACAPS, NULL); MF_ENCODER(AUDIO, mp3, MP3, NULL, AFMTS, ACAPS, NULL); From b547df22a2df443f320ca60bdfa999455e90a572 Mon Sep 17 00:00:00 2001 From: wrapper Date: Sat, 16 Aug 2025 14:53:56 +0700 Subject: [PATCH 02/12] output score profile --- libavcodec/mfenc.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c index a35a1d1602..c8788b2dcb 100644 --- a/libavcodec/mfenc.c +++ b/libavcodec/mfenc.c @@ -727,6 +727,26 @@ static int64_t mf_enca_output_score(AVCodecContext *avctx, IMFMediaType *type) if (!FAILED(hr) && t != 0) return -1; + if (avctx->codec_id == AV_CODEC_ID_AAC) { + hr = IMFAttributes_GetUINT32(type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &t); + switch (avctx->profile) { + case AV_PROFILE_AAC_LOW: + if (!FAILED(hr) && !(t >= 0x29 && t < 0x2c)) + return -1; + break; + + case AV_PROFILE_AAC_HE: + if (!FAILED(hr) && !(t >= 0x2c && t < 0x30)) + return -1; + break; + + case AV_PROFILE_AAC_HE_V2: + if (!FAILED(hr) && !(t >= 0x30 && t < 0x34)) + return -1; + break; + } + } + return score; } @@ -737,23 +757,6 @@ static int mf_enca_output_adjust(AVCodecContext *avctx, IMFMediaType *type) //IMFAttributes_SetUINT32(type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, avctx->bit_rate / 8); //IMFAttributes_SetUINT32(type, &MF_MT_AVG_BITRATE, avctx->bit_rate); - if (avctx->codec_id == AV_CODEC_ID_AAC) { - switch (avctx->profile) { - case AV_PROFILE_AAC_LOW: - case AV_PROFILE_UNKNOWN: - IMFAttributes_SetUINT32(type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, 0x2B); - break; - - case AV_PROFILE_AAC_HE: - IMFAttributes_SetUINT32(type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, 0x2F); - break; - - case AV_PROFILE_AAC_HE_V2: - IMFAttributes_SetUINT32(type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, 0x33); - break; - } - } - return 0; } From 90b835fb6c8ea9aabfdebcf532176794e8b4ba70 Mon Sep 17 00:00:00 2001 From: wrapper Date: Sat, 16 Aug 2025 15:09:33 +0700 Subject: [PATCH 03/12] level 1 aac --- libavcodec/mfenc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c index c8788b2dcb..d00e3ca915 100644 --- a/libavcodec/mfenc.c +++ b/libavcodec/mfenc.c @@ -731,7 +731,7 @@ static int64_t mf_enca_output_score(AVCodecContext *avctx, IMFMediaType *type) hr = IMFAttributes_GetUINT32(type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &t); switch (avctx->profile) { case AV_PROFILE_AAC_LOW: - if (!FAILED(hr) && !(t >= 0x29 && t < 0x2c)) + if (!FAILED(hr) && !(t >= 0x28 && t < 0x2c)) return -1; break; From 8180ebe904ffeda96b1dbf99f5c24741f749a794 Mon Sep 17 00:00:00 2001 From: wrapper Date: Sun, 17 Aug 2025 15:15:46 +0700 Subject: [PATCH 04/12] fail if not 24h2 --- libavcodec/mfenc.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c index d00e3ca915..47305bcb6f 100644 --- a/libavcodec/mfenc.c +++ b/libavcodec/mfenc.c @@ -16,7 +16,6 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "libavcodec/profiles.h" #define COBJMACROS #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0602 #undef _WIN32_WINNT @@ -945,6 +944,7 @@ static int mf_choose_output_type(AVCodecContext *avctx) HRESULT hr; int ret; IMFMediaType *out_type = NULL; + IMFMediaType *out_type_negotiated = NULL; int64_t out_type_score = -1; int out_type_index = -1; int n; @@ -1012,7 +1012,22 @@ static int mf_choose_output_type(AVCodecContext *avctx) hr = IMFTransform_SetOutputType(c->mft, c->out_stream_id, out_type, 0); if (!FAILED(hr)) { - ret = 1; + if (avctx->codec_id == AV_CODEC_ID_AAC) { + unsigned int profile_set; + unsigned int profile_negotiated; + hr = IMFTransform_GetOutputCurrentType(c->mft, c->out_stream_id, &out_type_negotiated); + IMFAttributes_GetUINT32(out_type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &profile_set); + IMFAttributes_GetUINT32(out_type_negotiated, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &profile_negotiated); + + if (!FAILED(hr) && profile_set != profile_negotiated) { // Setting PROFILE_LEVEL_INDICATION in AAC Encoder is only supported starting from Win11 24H2, fail if reported a mismatch in profile level indication. + av_log(avctx, AV_LOG_ERROR, "profile level indication mismatch: (out_type profile_level) %d != (out_type_negotiated profile_level) %d\n", profile_set, profile_negotiated); + ret = AVERROR_EXTERNAL; + } else { + ret = 1; + } + } else { + ret = 1; + } } else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) { av_log(avctx, AV_LOG_VERBOSE, "rejected - need to set input type\n"); ret = 0; From af417a0790ce0d104562e2542ea76d14ab5fdd2b Mon Sep 17 00:00:00 2001 From: wrapper Date: Sun, 17 Aug 2025 15:16:58 +0700 Subject: [PATCH 05/12] 2 --- libavcodec/mfenc.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c index 47305bcb6f..06d5032ab3 100644 --- a/libavcodec/mfenc.c +++ b/libavcodec/mfenc.c @@ -29,6 +29,7 @@ #include "libavutil/opt.h" #include "libavutil/time.h" #include "codec_internal.h" +#include "profiles.h" #include "internal.h" #include "compat/w32dlfcn.h" #if CONFIG_D3D11VA From ec65dd4d79af998274cdeb565b8617c50fca9cdb Mon Sep 17 00:00:00 2001 From: wrapper Date: Sun, 17 Aug 2025 15:31:14 +0700 Subject: [PATCH 06/12] again --- libavcodec/mfenc.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c index 06d5032ab3..000b4da383 100644 --- a/libavcodec/mfenc.c +++ b/libavcodec/mfenc.c @@ -1008,17 +1008,20 @@ static int mf_choose_output_type(AVCodecContext *avctx) } if (ret >= 0) { + unsigned int profile_set; + unsigned int profile_negotiated; av_log(avctx, AV_LOG_VERBOSE, "setting output type:\n"); ff_media_type_dump(avctx, out_type); + IMFAttributes_GetUINT32(out_type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &profile_set); + hr = IMFTransform_SetOutputType(c->mft, c->out_stream_id, out_type, 0); if (!FAILED(hr)) { if (avctx->codec_id == AV_CODEC_ID_AAC) { - unsigned int profile_set; - unsigned int profile_negotiated; hr = IMFTransform_GetOutputCurrentType(c->mft, c->out_stream_id, &out_type_negotiated); - IMFAttributes_GetUINT32(out_type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &profile_set); - IMFAttributes_GetUINT32(out_type_negotiated, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &profile_negotiated); + IMFAttributes_GetUINT32(out_type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &profile_negotiated); + + av_log(avctx, AV_LOG_TRACE, "cmp1: %d, cmp2: %d\n", out_type, out_type_negotiated); if (!FAILED(hr) && profile_set != profile_negotiated) { // Setting PROFILE_LEVEL_INDICATION in AAC Encoder is only supported starting from Win11 24H2, fail if reported a mismatch in profile level indication. av_log(avctx, AV_LOG_ERROR, "profile level indication mismatch: (out_type profile_level) %d != (out_type_negotiated profile_level) %d\n", profile_set, profile_negotiated); From 60223b3a0be38fdcacbcf6c0c65fe47987ce5087 Mon Sep 17 00:00:00 2001 From: wrapper Date: Sun, 17 Aug 2025 15:39:40 +0700 Subject: [PATCH 07/12] simple --- libavcodec/mfenc.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c index 000b4da383..a93d60d07b 100644 --- a/libavcodec/mfenc.c +++ b/libavcodec/mfenc.c @@ -945,7 +945,6 @@ static int mf_choose_output_type(AVCodecContext *avctx) HRESULT hr; int ret; IMFMediaType *out_type = NULL; - IMFMediaType *out_type_negotiated = NULL; int64_t out_type_score = -1; int out_type_index = -1; int n; @@ -1018,12 +1017,9 @@ static int mf_choose_output_type(AVCodecContext *avctx) hr = IMFTransform_SetOutputType(c->mft, c->out_stream_id, out_type, 0); if (!FAILED(hr)) { if (avctx->codec_id == AV_CODEC_ID_AAC) { - hr = IMFTransform_GetOutputCurrentType(c->mft, c->out_stream_id, &out_type_negotiated); IMFAttributes_GetUINT32(out_type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &profile_negotiated); - av_log(avctx, AV_LOG_TRACE, "cmp1: %d, cmp2: %d\n", out_type, out_type_negotiated); - - if (!FAILED(hr) && profile_set != profile_negotiated) { // Setting PROFILE_LEVEL_INDICATION in AAC Encoder is only supported starting from Win11 24H2, fail if reported a mismatch in profile level indication. + if (profile_set != profile_negotiated) { // Setting PROFILE_LEVEL_INDICATION in AAC Encoder is only supported starting from Win11 24H2, fail if reported a mismatch in profile level indication. av_log(avctx, AV_LOG_ERROR, "profile level indication mismatch: (out_type profile_level) %d != (out_type_negotiated profile_level) %d\n", profile_set, profile_negotiated); ret = AVERROR_EXTERNAL; } else { From 28c8e9b7aae44897f00992e0f0ccb175d21ea6a7 Mon Sep 17 00:00:00 2001 From: wrapper Date: Sun, 17 Aug 2025 21:12:20 +0700 Subject: [PATCH 08/12] 24h2 he-aac bitrate bug --- libavcodec/mfenc.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c index a93d60d07b..07d26fa934 100644 --- a/libavcodec/mfenc.c +++ b/libavcodec/mfenc.c @@ -697,6 +697,7 @@ static int64_t mf_enca_output_score(AVCodecContext *avctx, IMFMediaType *type) UINT32 t; GUID tg; int64_t score = 0; + int bitrate_now = avctx->bit_rate; hr = IMFAttributes_GetUINT32(type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &t); if (!FAILED(hr) && t == avctx->sample_rate) @@ -712,23 +713,11 @@ static int64_t mf_enca_output_score(AVCodecContext *avctx, IMFMediaType *type) score |= 4LL << 32; } - // Select the bitrate (lowest priority). - hr = IMFAttributes_GetUINT32(type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &t); - if (!FAILED(hr)) { - int diff = (int)t - avctx->bit_rate / 8; - if (diff >= 0) { - score |= (1LL << 31) - diff; // prefer lower bitrate - } else { - score |= (1LL << 30) + diff; // prefer higher bitrate - } - } - - hr = IMFAttributes_GetUINT32(type, &MF_MT_AAC_PAYLOAD_TYPE, &t); - if (!FAILED(hr) && t != 0) - return -1; - if (avctx->codec_id == AV_CODEC_ID_AAC) { hr = IMFAttributes_GetUINT32(type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &t); + if (t >= 0x2c && t < 0x34) + bitrate_now /= 2; // 24H2: bitrate of HE-AAC is core bitrate times 2 + switch (avctx->profile) { case AV_PROFILE_AAC_LOW: if (!FAILED(hr) && !(t >= 0x28 && t < 0x2c)) @@ -747,6 +736,21 @@ static int64_t mf_enca_output_score(AVCodecContext *avctx, IMFMediaType *type) } } + // Select the bitrate (lowest priority). + hr = IMFAttributes_GetUINT32(type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &t); + if (!FAILED(hr)) { + int diff = (int)t - bitrate_now / 8; + if (diff >= 0) { + score |= (1LL << 31) - diff; // prefer lower bitrate + } else { + score |= (1LL << 30) + diff; // prefer higher bitrate + } + } + + hr = IMFAttributes_GetUINT32(type, &MF_MT_AAC_PAYLOAD_TYPE, &t); + if (!FAILED(hr) && t != 0) + return -1; + return score; } From 3b2fe4b52c8dc973ba6023299cb4c80eafec1523 Mon Sep 17 00:00:00 2001 From: wrapper Date: Sun, 17 Aug 2025 22:23:59 +0700 Subject: [PATCH 09/12] info bitrate --- libavcodec/mfenc.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c index 07d26fa934..22bffd816f 100644 --- a/libavcodec/mfenc.c +++ b/libavcodec/mfenc.c @@ -715,17 +715,17 @@ static int64_t mf_enca_output_score(AVCodecContext *avctx, IMFMediaType *type) if (avctx->codec_id == AV_CODEC_ID_AAC) { hr = IMFAttributes_GetUINT32(type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &t); - if (t >= 0x2c && t < 0x34) + if ((t >= 0x2c && t < 0x34) || t == 0x52 || t == 0x53) bitrate_now /= 2; // 24H2: bitrate of HE-AAC is core bitrate times 2 switch (avctx->profile) { case AV_PROFILE_AAC_LOW: - if (!FAILED(hr) && !(t >= 0x28 && t < 0x2c)) + if (!FAILED(hr) && !(t >= 0x28 && t < 0x2c) && t != 0x50 && t != 0x51) return -1; break; case AV_PROFILE_AAC_HE: - if (!FAILED(hr) && !(t >= 0x2c && t < 0x30)) + if (!FAILED(hr) && !(t >= 0x2c && t < 0x30) && t != 0x52 && t != 0x53) return -1; break; @@ -1020,16 +1020,20 @@ static int mf_choose_output_type(AVCodecContext *avctx) hr = IMFTransform_SetOutputType(c->mft, c->out_stream_id, out_type, 0); if (!FAILED(hr)) { + unsigned int bitrate; if (avctx->codec_id == AV_CODEC_ID_AAC) { IMFAttributes_GetUINT32(out_type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &profile_negotiated); + IMFAttributes_GetUINT32(out_type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &bitrate); if (profile_set != profile_negotiated) { // Setting PROFILE_LEVEL_INDICATION in AAC Encoder is only supported starting from Win11 24H2, fail if reported a mismatch in profile level indication. av_log(avctx, AV_LOG_ERROR, "profile level indication mismatch: (out_type profile_level) %d != (out_type_negotiated profile_level) %d\n", profile_set, profile_negotiated); ret = AVERROR_EXTERNAL; } else { + av_log(log, AV_LOG_INFO, "using bitrate %dkbps, profile level: 0x%02x\n", bitrate / 125, profile_set); ret = 1; } } else { + av_log(log, AV_LOG_INFO, "using bitrate %dkbps\n", bitrate / 125); ret = 1; } } else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) { From ede3c5b102739afcf59507b74b2d579af9975f0a Mon Sep 17 00:00:00 2001 From: wrapper Date: Sun, 17 Aug 2025 22:32:39 +0700 Subject: [PATCH 10/12] log crash --- libavcodec/mfenc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c index 22bffd816f..5bdcfe64ac 100644 --- a/libavcodec/mfenc.c +++ b/libavcodec/mfenc.c @@ -1029,11 +1029,11 @@ static int mf_choose_output_type(AVCodecContext *avctx) av_log(avctx, AV_LOG_ERROR, "profile level indication mismatch: (out_type profile_level) %d != (out_type_negotiated profile_level) %d\n", profile_set, profile_negotiated); ret = AVERROR_EXTERNAL; } else { - av_log(log, AV_LOG_INFO, "using bitrate %dkbps, profile level: 0x%02x\n", bitrate / 125, profile_set); + av_log(avctx, AV_LOG_INFO, "using bitrate %dkbps, profile level: 0x%02x\n", bitrate / 125, profile_set); ret = 1; } } else { - av_log(log, AV_LOG_INFO, "using bitrate %dkbps\n", bitrate / 125); + av_log(avctx, AV_LOG_INFO, "using bitrate %dkbps\n", bitrate / 125); ret = 1; } } else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) { From eb6bb062f7f74541bab1f118b81e54a15a2487aa Mon Sep 17 00:00:00 2001 From: wrapper Date: Sun, 17 Aug 2025 22:43:01 +0700 Subject: [PATCH 11/12] bitrate again --- libavcodec/mfenc.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c index 5bdcfe64ac..c736e99ac2 100644 --- a/libavcodec/mfenc.c +++ b/libavcodec/mfenc.c @@ -715,7 +715,7 @@ static int64_t mf_enca_output_score(AVCodecContext *avctx, IMFMediaType *type) if (avctx->codec_id == AV_CODEC_ID_AAC) { hr = IMFAttributes_GetUINT32(type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &t); - if ((t >= 0x2c && t < 0x34) || t == 0x52 || t == 0x53) + if (((t >= 0x2c && t < 0x34) || t == 0x52 || t == 0x53) && (avctx->profile != AV_PROFILE_UNKNOWN || bitrate_now <= 64000)) bitrate_now /= 2; // 24H2: bitrate of HE-AAC is core bitrate times 2 switch (avctx->profile) { @@ -1025,6 +1025,9 @@ static int mf_choose_output_type(AVCodecContext *avctx) IMFAttributes_GetUINT32(out_type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &profile_negotiated); IMFAttributes_GetUINT32(out_type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &bitrate); + if ((profile_set >= 0x2c && profile_set < 0x34) || profile_set == 0x52 || profile_set == 0x53) + bitrate *= 2; + if (profile_set != profile_negotiated) { // Setting PROFILE_LEVEL_INDICATION in AAC Encoder is only supported starting from Win11 24H2, fail if reported a mismatch in profile level indication. av_log(avctx, AV_LOG_ERROR, "profile level indication mismatch: (out_type profile_level) %d != (out_type_negotiated profile_level) %d\n", profile_set, profile_negotiated); ret = AVERROR_EXTERNAL; @@ -1033,7 +1036,7 @@ static int mf_choose_output_type(AVCodecContext *avctx) ret = 1; } } else { - av_log(avctx, AV_LOG_INFO, "using bitrate %dkbps\n", bitrate / 125); + if (bitrate) av_log(avctx, AV_LOG_INFO, "using bitrate %dkbps\n", bitrate / 125); ret = 1; } } else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) { From 7ab9e564019a661e2386d88914ac396565d30562 Mon Sep 17 00:00:00 2001 From: wrapper Date: Sun, 17 Aug 2025 23:17:41 +0700 Subject: [PATCH 12/12] fix bitrate issues --- libavcodec/mfenc.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c index c736e99ac2..3569562b58 100644 --- a/libavcodec/mfenc.c +++ b/libavcodec/mfenc.c @@ -697,7 +697,6 @@ static int64_t mf_enca_output_score(AVCodecContext *avctx, IMFMediaType *type) UINT32 t; GUID tg; int64_t score = 0; - int bitrate_now = avctx->bit_rate; hr = IMFAttributes_GetUINT32(type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &t); if (!FAILED(hr) && t == avctx->sample_rate) @@ -715,9 +714,6 @@ static int64_t mf_enca_output_score(AVCodecContext *avctx, IMFMediaType *type) if (avctx->codec_id == AV_CODEC_ID_AAC) { hr = IMFAttributes_GetUINT32(type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &t); - if (((t >= 0x2c && t < 0x34) || t == 0x52 || t == 0x53) && (avctx->profile != AV_PROFILE_UNKNOWN || bitrate_now <= 64000)) - bitrate_now /= 2; // 24H2: bitrate of HE-AAC is core bitrate times 2 - switch (avctx->profile) { case AV_PROFILE_AAC_LOW: if (!FAILED(hr) && !(t >= 0x28 && t < 0x2c) && t != 0x50 && t != 0x51) @@ -739,7 +735,7 @@ static int64_t mf_enca_output_score(AVCodecContext *avctx, IMFMediaType *type) // Select the bitrate (lowest priority). hr = IMFAttributes_GetUINT32(type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &t); if (!FAILED(hr)) { - int diff = (int)t - bitrate_now / 8; + int diff = (int)t - avctx->bit_rate / 8; if (diff >= 0) { score |= (1LL << 31) - diff; // prefer lower bitrate } else { @@ -1021,12 +1017,10 @@ static int mf_choose_output_type(AVCodecContext *avctx) hr = IMFTransform_SetOutputType(c->mft, c->out_stream_id, out_type, 0); if (!FAILED(hr)) { unsigned int bitrate; + IMFAttributes_GetUINT32(out_type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &bitrate); + if (avctx->codec_id == AV_CODEC_ID_AAC) { IMFAttributes_GetUINT32(out_type, &MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, &profile_negotiated); - IMFAttributes_GetUINT32(out_type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &bitrate); - - if ((profile_set >= 0x2c && profile_set < 0x34) || profile_set == 0x52 || profile_set == 0x53) - bitrate *= 2; if (profile_set != profile_negotiated) { // Setting PROFILE_LEVEL_INDICATION in AAC Encoder is only supported starting from Win11 24H2, fail if reported a mismatch in profile level indication. av_log(avctx, AV_LOG_ERROR, "profile level indication mismatch: (out_type profile_level) %d != (out_type_negotiated profile_level) %d\n", profile_set, profile_negotiated); @@ -1036,7 +1030,7 @@ static int mf_choose_output_type(AVCodecContext *avctx) ret = 1; } } else { - if (bitrate) av_log(avctx, AV_LOG_INFO, "using bitrate %dkbps\n", bitrate / 125); + av_log(avctx, AV_LOG_INFO, "using bitrate %dkbps\n", bitrate / 125); ret = 1; } } else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {