Skip to content

Commit

Permalink
Use WAVEFORMATEXTENSIBLE in the LAV-specific audio mediatype, when re…
Browse files Browse the repository at this point in the history
…quired
  • Loading branch information
Nevcairiel committed Apr 26, 2022
1 parent 8e18dcb commit 56fd148
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
20 changes: 20 additions & 0 deletions demuxer/Demuxers/LAVFAudioHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,26 @@ WAVEFORMATEXFFMPEG *CLAVFAudioHelper::CreateWVFMTEX_FF(const AVStream *avstream,
return wfex_ff;
}

WAVEFORMATEXFFMPEG *CLAVFAudioHelper::CreateWFMTEX_RAW_PCM_FF(const AVStream *avstream, ULONG *size, const GUID subtype, ULONG *samplesize)
{
WAVEFORMATEXTENSIBLE *wvfmt = CreateWFMTEX_RAW_PCM(avstream, size, subtype, samplesize);
if (!wvfmt)
return nullptr;

const size_t diff_size = sizeof(WAVEFORMATEXFFMPEG) - sizeof(WAVEFORMATEX);
WAVEFORMATEXFFMPEG *wfex_ff = (WAVEFORMATEXFFMPEG *)CoTaskMemAlloc(diff_size + *size);
if (!wfex_ff)
return nullptr;
memset(wfex_ff, 0, sizeof(WAVEFORMATEXFFMPEG));
memcpy(&wfex_ff->wfex, wvfmt, *size);

wfex_ff->nCodecId = avstream->codecpar->codec_id;
CoTaskMemFree(wvfmt);

*size = sizeof(WAVEFORMATEXFFMPEG) + wfex_ff->wfex.cbSize;
return wfex_ff;
}

WAVEFORMATEX_HDMV_LPCM *CLAVFAudioHelper::CreateWVFMTEX_LPCM(const AVStream *avstream, ULONG *size)
{
WAVEFORMATEX *wvfmt = CreateWVFMTEX(avstream, size);
Expand Down
2 changes: 2 additions & 0 deletions demuxer/Demuxers/LAVFAudioHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class CLAVFAudioHelper
WAVEFORMATEX_HDMV_LPCM *CreateWVFMTEX_LPCM(const AVStream *avstream, ULONG *size);
WAVEFORMATEXTENSIBLE *CreateWFMTEX_RAW_PCM(const AVStream *avstream, ULONG *size, const GUID subtype,
ULONG *samplesize);
WAVEFORMATEXFFMPEG *CreateWFMTEX_RAW_PCM_FF(const AVStream *avstream, ULONG *size,
const GUID subtype, ULONG *samplesize);
MPEG1WAVEFORMAT *CreateMP1WVFMT(const AVStream *avstream, ULONG *size);
VORBISFORMAT *CreateVorbis(const AVStream *avstream, ULONG *size);
VORBISFORMAT2 *CreateVorbis2(const AVStream *avstream, ULONG *size);
Expand Down
14 changes: 11 additions & 3 deletions demuxer/Demuxers/LAVFStreamInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ STDMETHODIMP CLAVFStreamInfo::CreateAudioMediaType(AVFormatContext *avctx, AVStr
CMediaType mtype =
g_AudioHelper.initAudioType(avstream->codecpar, avstream->codecpar->codec_tag, m_containerFormat);

// The Raw PCM type is used for PCM streams which do not already have their own Extensible header
// If the codec_tag already equals extensible, it'll be used copied as-is and we assume a valid extensible header
// exists in extradata
bool bUseRawPCMType = (mtype.subtype == MEDIASUBTYPE_PCM || mtype.subtype == MEDIASUBTYPE_IEEE_FLOAT) &&
(avstream->codecpar->codec_tag != WAVE_FORMAT_EXTENSIBLE || avstream->codecpar->extradata_size < 22);

if (mtype.formattype == FORMAT_WaveFormatEx)
{
// Special Logic for the MPEG1 Audio Formats (MP1, MP2)
Expand All @@ -94,8 +100,7 @@ STDMETHODIMP CLAVFStreamInfo::CreateAudioMediaType(AVFormatContext *avctx, AVStr
mtypes.push_back(mtype);
mtype.subtype = MEDIASUBTYPE_HDMV_LPCM_AUDIO;
}
else if ((mtype.subtype == MEDIASUBTYPE_PCM || mtype.subtype == MEDIASUBTYPE_IEEE_FLOAT) &&
avstream->codecpar->codec_tag != WAVE_FORMAT_EXTENSIBLE)
else if (bUseRawPCMType)
{
// Create raw PCM media type
mtype.pbFormat = (BYTE *)g_AudioHelper.CreateWFMTEX_RAW_PCM(avstream, &mtype.cbFormat, mtype.subtype,
Expand Down Expand Up @@ -206,7 +211,10 @@ STDMETHODIMP CLAVFStreamInfo::CreateAudioMediaType(AVFormatContext *avctx, AVStr
ff_mtype.majortype = MEDIATYPE_Audio;
ff_mtype.subtype = MEDIASUBTYPE_FFMPEG_AUDIO;
ff_mtype.formattype = FORMAT_WaveFormatExFFMPEG;
ff_mtype.pbFormat = (BYTE *)g_AudioHelper.CreateWVFMTEX_FF(avstream, &ff_mtype.cbFormat);
if (bUseRawPCMType)
ff_mtype.pbFormat = (BYTE *)g_AudioHelper.CreateWFMTEX_RAW_PCM_FF(avstream, &ff_mtype.cbFormat, mtype.subtype, &ff_mtype.lSampleSize);
else
ff_mtype.pbFormat = (BYTE *)g_AudioHelper.CreateWVFMTEX_FF(avstream, &ff_mtype.cbFormat);
mtypes.push_back(ff_mtype);

return S_OK;
Expand Down

0 comments on commit 56fd148

Please sign in to comment.