/* ***** BEGIN LICENSE BLOCK *****
* Version: RCSL 1.0/RPSL 1.0
*
* Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file, are
* subject to the current version of the RealNetworks Public Source License
* Version 1.0 (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the RealNetworks Community Source License Version 1.0
* (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
* in which case the RCSL will apply. You may also obtain the license terms
* directly from RealNetworks. You may not use this file except in
* compliance with the RPSL or, if you have a valid RCSL with RealNetworks
* applicable to this file, the RCSL. Please see the applicable RPSL or
* RCSL for the rights, obligations and limitations governing use of the
* contents of the file.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the portions
* it created.
*
* This file, and the files included with this file, is distributed and made
* available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
#include "hlxclib/string.h"
#include "mp4a-mux-cfg.h"
#include "bitstream.h"
MP4AAudioSpec::MP4AAudioSpec() :
m_pConfig(0),
m_ulConfigSize(0)
{}
MP4AAudioSpec::MP4AAudioSpec(const MP4AAudioSpec& rhs) :
m_pConfig(0),
m_ulConfigSize(rhs.m_ulConfigSize)
{
if (m_ulConfigSize)
{
m_pConfig = new UINT8[m_ulConfigSize];
::memcpy(m_pConfig, rhs.m_pConfig, m_ulConfigSize);
}
}
MP4AAudioSpec::~MP4AAudioSpec()
{
delete [] m_pConfig;
m_pConfig = 0;
}
MP4AAudioSpec& MP4AAudioSpec::operator=(const MP4AAudioSpec& rhs)
{
if (&rhs != this)
{
delete [] m_pConfig;
m_pConfig = 0;
m_ulConfigSize = rhs.m_ulConfigSize;
if (m_ulConfigSize)
{
m_pConfig = new UINT8[m_ulConfigSize];
::memcpy(m_pConfig, rhs.m_pConfig, m_ulConfigSize); /* Flawfinder: ignore */
}
}
return *this;
}
BOOL MP4AAudioSpec::Unpack(Bitstream& bs)
{
BOOL ret = FALSE;
ULONG32 config = bs.PeekBits(16);
ULONG32 objectType = config >> 11;
ULONG32 sampFreq = (config >> 7) & 0xf;
ULONG32 channelCfg = (config >> 3) & 0xf;
if (((objectType >= 1) && (objectType < 5) ||
(objectType >= 6) && (objectType < 8)) &&
(sampFreq != 0xf) &&
(channelCfg != 0))
{
ULONG32 ulConfigBits = 16;
delete [] m_pConfig;
m_ulConfigSize = (ulConfigBits + 7) >> 3;
m_pConfig = new UINT8 [m_ulConfigSize];
bs.GetBits(ulConfigBits, m_pConfig);
ret = TRUE;
}
return ret;
}
MP4AStreamInfo::MP4AStreamInfo() :
m_ulProgram(0),
m_ulLayer(0),
m_ulLengthType(0),
m_ulBlockDelay(0),
m_bFracDelayPresent(0),
m_ulFracDelay(0),
m_ulFrameLength(0),
m_ulCELPIndex(0),
m_ulHVXCIndex(0)
{}
MP4AMuxConfig::MP4AMuxConfig() :
m_bAllSameTiming(FALSE),
m_ulNumSubFrames(0),
m_ulNumPrograms(0),
m_pLayerCounts(0),
m_ppStreamLookup(0),
m_ulNumStreams(0),
m_pStreamInfo(0)
{}
MP4AMuxConfig::~MP4AMuxConfig()
{
Reset();
}
BOOL MP4AMuxConfig::Unpack(Bitstream& bs)
{
BOOL failed = FALSE;
Reset();
#if 1
// This updates StreamMuxConfig to ISO/IEC draft 14496-3:2001
ULONG32 ulAudioMuxVersion = bs.GetBits(1); // audioMuxVersion
if (ulAudioMuxVersion == 0)
{
ULONG32 ulStreamCount = 0;
m_bAllSameTiming = (bs.GetBits(1) ? TRUE : FALSE);
m_ulNumSubFrames = bs.GetBits(6) + 1; // 0-based (old version was not)
m_ulNumPrograms = bs.GetBits(4) + 1; // 0-based (old version was not)
// Allocate arrays
m_pLayerCounts = new ULONG32[m_ulNumPrograms];
m_ppStreamLookup = new ULONG32*[m_ulNumPrograms];
if (m_pLayerCounts && m_ppStreamLookup)
{
// Null out arrays
ULONG32 ulProg = 0;
for (ulProg = 0; ulProg < m_ulNumPrograms; ulProg++)
{
m_pLayerCounts[ulProg] = 0;
m_ppStreamLookup[ulProg] = 0;
}
// Parse programs
for (ulProg = 0; !failed && ulProg < m_ulNumPrograms; ulProg++)
{
// Get the number of layers
ULONG32 ulNumLayers = bs.GetBits(3) + 1; // 0-based (old version was not)
// Allocate stream lookup array
MP4AAudioSpec as;
m_pLayerCounts[ulProg] = ulNumLayers;
m_ppStreamLookup[ulProg] = new ULONG32[ulNumLayers];
if (m_ppStreamLookup[ulProg])
{
for (ULONG32 ulLay = 0; ulLay < ulNumLayers; ulLay++)
{
BOOL readAudioSpec = TRUE;
MP4AStreamInfo streamInfo;
m_ppStreamLookup[ulProg][ulLay] = m_ulNumStreams;
streamInfo.SetProgram(ulProg);
streamInfo.SetLayer(ulLay);
if (((ulProg != 0) || (ulLay != 0)) &&
(bs.GetBits(1) != 0))
readAudioSpec = FALSE;
if (readAudioSpec)
{
if (!as.Unpack(bs))
{
failed = TRUE;
break;
}
}
streamInfo.SetAudioSpec(as);
streamInfo.SetLengthType(bs.GetBits(3));
switch (streamInfo.GetLengthType())
{
case 0:
bs.GetBits(8); // Buffer fullness
if (!m_bAllSameTiming)
{
// Get Core Frame offset
// XXXMEH - We always hint such that m_bAllSameTiming == TRUE,
// so for now we won't worry about this
}
break;
case 1:
streamInfo.SetFrameLength(bs.GetBits(9));
break;
case 3:
case 4:
case 5:
streamInfo.SetCELPIndex(bs.GetBits(6));
break;
case 6 :
case 7 :
streamInfo.SetHVXCIndex(bs.GetBits(1));
break;
};
AddStream(streamInfo);
}
}
else
{
failed = TRUE;
}
}
// Other data present
if (bs.GetBits(1))
{
UINT8 otherDataLenTemp = 0;
UINT32 otherDataLenBits = 0;
UINT8 otherDataLenEsc = 0;
do {
otherDataLenBits *= 256;
otherDataLenEsc = (UINT8)bs.GetBits(1);
otherDataLenTemp = (UINT8)bs.GetBits(8);
otherDataLenBits += otherDataLenTemp;
} while (otherDataLenEsc);
}
// Crc present
if (bs.GetBits(1))
{
bs.GetBits(8);
}
}
else
{
failed = TRUE;
}
}
#else
if (bs.GetBits(1))
m_bAllSameTiming = TRUE;
m_ulNumSubFrames = bs.GetBits(3);
m_ulNumPrograms = bs.GetBits(4);
m_pLayerCounts = new ULONG32[m_ulNumPrograms];
m_ppStreamLookup = new ULONG32*[m_ulNumPrograms];
for (ULONG32 i = 0; i < m_ulNumPrograms ; i++)
{
m_pLayerCounts[i] = 0;
m_ppStreamLookup[i] = 0;
}
for (ULONG32 prog = 0; !failed && (prog < m_ulNumPrograms) ; prog++)
{
ULONG32 ulNumLayers = bs.GetBits(3);
MP4AAudioSpec as;
m_pLayerCounts[prog] = ulNumLayers;
m_ppStreamLookup[prog] = new ULONG32[ulNumLayers];
for (ULONG32 lay = 0; lay < ulNumLayers; lay++)
{
BOOL readAudioSpec = TRUE;
MP4AStreamInfo streamInfo;
m_ppStreamLookup[prog][lay] = m_ulNumStreams;
streamInfo.SetProgram(prog);
streamInfo.SetLayer(lay);
if (((prog != 0) || (lay != 0)) &&
(bs.GetBits(1) != 0))
readAudioSpec = FALSE;
if (readAudioSpec)
{
if (!as.Unpack(bs))
{
failed = TRUE;
break;
}
}
streamInfo.SetAudioSpec(as);
streamInfo.SetLengthType(bs.GetBits(3));
switch (streamInfo.GetLengthType()) {
case 0 :
streamInfo.SetBlockDelay(bs.GetBits(5));
if (bs.GetBits(1))
{
streamInfo.SetFracDelayPresent(TRUE);
streamInfo.SetFracDelay(bs.GetBits(8));
}
break;
case 1 :
streamInfo.SetFrameLength(bs.GetBits(9));
break;
case 3 :
case 4 :
case 5 :
streamInfo.SetCELPIndex(bs.GetBits(6));
break;
case 6 :
case 7 :
streamInfo.SetHVXCIndex(bs.GetBits(1));
break;
};
AddStream(streamInfo);
}
}
#endif
return !failed;
}
void MP4AMuxConfig::Reset()
{
delete [] m_pLayerCounts;
m_pLayerCounts = 0;
m_ulNumStreams = 0;
if (m_ppStreamLookup)
{
for (ULONG32 i = 0; i < m_ulNumPrograms; i++)
{
delete [] m_ppStreamLookup[i];
m_ppStreamLookup[i] = 0;
}
delete [] m_ppStreamLookup;
m_ppStreamLookup = 0;
}
delete [] m_pStreamInfo;
m_pStreamInfo = 0;
}
void MP4AMuxConfig::AddStream(const MP4AStreamInfo& info)
{
MP4AStreamInfo* pTmp = new MP4AStreamInfo[m_ulNumStreams + 1];
if (m_pStreamInfo)
{
for (ULONG32 i = 0; i < m_ulNumStreams; i++)
{
pTmp[i] = m_pStreamInfo[i];
}
delete [] m_pStreamInfo;
}
m_pStreamInfo = pTmp;
m_pStreamInfo[m_ulNumStreams] = info;
m_ulNumStreams++;
}