Skip to content

Commit

Permalink
MpaDecFilter - переделка коммита d0983ca, используем fdk-aac только д…
Browse files Browse the repository at this point in the history
…ля USAC / xHE-AAC.
  • Loading branch information
Aleksoid1978 committed Mar 1, 2024
1 parent 67668a4 commit 6668a0e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/DSUtil/MP4AudioDecoderConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const BYTE AOT_SLS_NON_CORE = 38; /**< SLS Sclable Lossless Coding Non-
const BYTE AOT_ER_AAC_ELD = 39; /**< Error Resilient AAC ELD */
const BYTE AOT_SMR_SIMPLE = 40; /**< SMR Simple */
const BYTE AOT_SMR_MAIN = 41; /**< SMR Main */
const BYTE AOT_USAC = 42; /**< Unified Speech and Audio Coding */
const BYTE AOT_USAC = 42; /**< Unified Speech and Audio Coding / xHE-AAC */

class CMP4AudioDecoderConfig {
public:
Expand Down
137 changes: 61 additions & 76 deletions src/filters/transform/MpaDecFilter/FFAudioDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "stdafx.h"
#include "FFAudioDecoder.h"
#include "MpaDecFilter.h"
#include "DSUtil/MP4AudioDecoderConfig.h"

#pragma warning(push)
#pragma warning(disable: 4005)
Expand Down Expand Up @@ -268,10 +269,6 @@ bool CFFAudioDecoder::Init(enum AVCodecID codecID, CMediaType* mediaType)
m_bNeedReinit = false;
m_bNeedMix = false;

if (mediaType || codecID != AV_CODEC_ID_AAC) {
m_bUseLibfdk = false;
}

if (codecID == AV_CODEC_ID_NONE || mediaType == nullptr) {
if (m_pAVCodec == nullptr || m_pAVCtx == nullptr || m_pAVCtx->codec_id == AV_CODEC_ID_NONE) {
return false;
Expand Down Expand Up @@ -332,98 +329,86 @@ bool CFFAudioDecoder::Init(enum AVCodecID codecID, CMediaType* mediaType)
StreamFinish();
}

m_pAVCodec = m_bUseLibfdk ? avcodec_find_decoder_by_name("libfdk_aac") : avcodec_find_decoder(codec_id);
if (codec_id == AV_CODEC_ID_AAC && extralen) {
CMP4AudioDecoderConfig MP4AudioDecoderConfig;
if (MP4AudioDecoderConfig.Parse(extradata, extralen) && MP4AudioDecoderConfig.m_ObjectType == AOT_USAC) {
m_pAVCodec = avcodec_find_decoder_by_name("libfdk_aac");
}
}

if (!m_pAVCodec) {
m_pAVCodec = avcodec_find_decoder(codec_id);
}
if (m_pAVCodec) {
for (;;) {
m_pAVCtx = avcodec_alloc_context3(m_pAVCodec);
CheckPointer(m_pAVCtx, false);
m_pAVCtx = avcodec_alloc_context3(m_pAVCodec);
CheckPointer(m_pAVCtx, false);

if (!ch_layout.nb_channels) {
av_channel_layout_from_mask(&ch_layout, GetDefChannelMask(channels));
}
if (!ch_layout.nb_channels) {
av_channel_layout_from_mask(&ch_layout, GetDefChannelMask(channels));
}

m_pAVCtx->codec_id = codec_id;
m_pAVCtx->sample_rate = samplerate;
m_pAVCtx->ch_layout = ch_layout;
m_pAVCtx->bits_per_coded_sample = bitdeph;
m_pAVCtx->block_align = block_align;
m_pAVCtx->bit_rate = bitrate;
m_pAVCtx->err_recognition = 0;
m_pAVCtx->thread_count = 1;
m_pAVCtx->thread_type = 0;

AVDictionary* options = nullptr;
if (m_bStereoDownmix) { // works to AC3, TrueHD, DTS
int ret = av_dict_set(&options, "downmix", "stereo", 0);
DLogIf(ret < 0, L"CFFAudioDecoder::Init() : Set downmix to stereo FAILED!");
}
m_pAVCtx->codec_id = codec_id;
m_pAVCtx->sample_rate = samplerate;
m_pAVCtx->ch_layout = ch_layout;
m_pAVCtx->bits_per_coded_sample = bitdeph;
m_pAVCtx->block_align = block_align;
m_pAVCtx->bit_rate = bitrate;
m_pAVCtx->err_recognition = 0;
m_pAVCtx->thread_count = 1;
m_pAVCtx->thread_type = 0;

AVDictionary* options = nullptr;
if (m_bStereoDownmix) { // works to AC3, TrueHD, DTS
int ret = av_dict_set(&options, "downmix", "stereo", 0);
DLogIf(ret < 0, L"CFFAudioDecoder::Init() : Set downmix to stereo FAILED!");
}

m_pParser = av_parser_init(codec_id);
m_pParser = av_parser_init(codec_id);

memset(&m_raData, 0, sizeof(m_raData));
memset(&m_raData, 0, sizeof(m_raData));

if (extradata && extralen) {
if (codec_id == AV_CODEC_ID_COOK || codec_id == AV_CODEC_ID_ATRAC3 || codec_id == AV_CODEC_ID_SIPR) {
if (extralen >= 4 && GETU32(extradata) == MAKEFOURCC('.', 'r', 'a', 0xfd)) {
HRESULT hr = ParseRealAudioHeader(extradata, extralen);
av_freep(&extradata);
extralen = 0;
if (FAILED(hr)) {
if (extradata && extralen) {
if (codec_id == AV_CODEC_ID_COOK || codec_id == AV_CODEC_ID_ATRAC3 || codec_id == AV_CODEC_ID_SIPR) {
if (extralen >= 4 && GETU32(extradata) == MAKEFOURCC('.', 'r', 'a', 0xfd)) {
HRESULT hr = ParseRealAudioHeader(extradata, extralen);
av_freep(&extradata);
extralen = 0;
if (FAILED(hr)) {
return false;
}
if (codec_id == AV_CODEC_ID_SIPR) {
if (m_raData.flavor > 3) {
DLog(L"CFFAudioDecoder::Init() : Invalid SIPR flavor (%d)", m_raData.flavor);
return false;
}
if (codec_id == AV_CODEC_ID_SIPR) {
if (m_raData.flavor > 3) {
DLog(L"CFFAudioDecoder::Init() : Invalid SIPR flavor (%d)", m_raData.flavor);
return false;
}
static BYTE sipr_subpk_size[4] = { 29, 19, 37, 20 };
m_pAVCtx->block_align = sipr_subpk_size[m_raData.flavor];
}
} else {
// Try without any processing?
m_pAVCtx->extradata_size = extralen;
m_pAVCtx->extradata = extradata;
static BYTE sipr_subpk_size[4] = { 29, 19, 37, 20 };
m_pAVCtx->block_align = sipr_subpk_size[m_raData.flavor];
}
} else {
// Try without any processing?
m_pAVCtx->extradata_size = extralen;
m_pAVCtx->extradata = extradata;
}
} else {
m_pAVCtx->extradata_size = extralen;
m_pAVCtx->extradata = extradata;
}
}

avcodec_lock;
if (avcodec_open2(m_pAVCtx, m_pAVCodec, &options) >= 0) {
m_pFrame = av_frame_alloc();
m_pPacket = av_packet_alloc();
}
avcodec_unlock;

if (options) {
av_dict_free(&options);
}

if (!m_pFrame && codec_id == AV_CODEC_ID_AAC && !m_bUseLibfdk && mediaType) {
StreamFinish();
m_pAVCodec = avcodec_find_decoder_by_name("libfdk_aac");
if (m_pAVCodec) {
getExtraData(mediaType->Format(), &mediaType->formattype, mediaType->cbFormat, nullptr, &extralen);
if (extralen) {
extradata = (uint8_t*)av_mallocz(extralen + AV_INPUT_BUFFER_PADDING_SIZE);
getExtraData(mediaType->Format(), &mediaType->formattype, mediaType->cbFormat, extradata, nullptr);
}

m_bUseLibfdk = true;
continue;
}
}
avcodec_lock;
if (avcodec_open2(m_pAVCtx, m_pAVCodec, &options) >= 0) {
m_pFrame = av_frame_alloc();
m_pPacket = av_packet_alloc();
}
avcodec_unlock;

break;
if (options) {
av_dict_free(&options);
}
}

if (!m_pFrame || !m_pPacket) {
m_bUseLibfdk = false;
StreamFinish();

return false;
}

Expand Down Expand Up @@ -563,7 +548,7 @@ HRESULT CFFAudioDecoder::SendData(BYTE* p, int size, int* out_size)
hr = S_OK;
}

if (hr == E_FAIL && !m_bUseLibfdk) {
if (hr == E_FAIL) {
Init(GetCodecId(), nullptr);
}

Expand Down
2 changes: 0 additions & 2 deletions src/filters/transform/MpaDecFilter/FFAudioDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class CFFAudioDecoder
AVFrame* m_pFrame = nullptr;
AVPacket* m_pPacket = nullptr;

bool m_bUseLibfdk = false;

struct {
int flavor;
int coded_frame_size;
Expand Down

0 comments on commit 6668a0e

Please sign in to comment.