📄 cra2ihxaprop.cpp
字号:
/* ***** 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 "hxcom.h"
#include "hxtypes.h"
#include "hxresult.h"
#include "hlxclib/string.h"
#include "racodec.h"
#include "gcd.h"
#include "cra2ihxaenc.h"
/* heap, memory debugging */
#include "hxheap.h"
#ifdef _DEBUG
#undef HX_THIS_FILE
static const char HX_THIS_FILE[] = __FILE__;
#endif
static ULONG32 getULONG32Prop(RACODEC codecRef,UINT16 flvIndex, UINT16 propIndex)
{
UINT16 pSize ;
ULONG32 *x = (ULONG32*)ENTRYPOINT(RAGetFlavorProperty)(codecRef, flvIndex, propIndex, &pSize) ;
if (!x || !pSize)
return -1 ; // should really fault. XXX TODO wschildbach
else
return *x ;
}
HX_RESULT HXAudioEncoder::retrieveProperties(void)
{
int i, n = GetNumberOfFlavors() ;
mpPropTable = new struct tag_propTable[n] ;
for (i = 0 ; i < n ; i++)
{
int t ;
mpPropTable[i].bytesPerEncode = getULONG32Prop(mpCodecRef, i, ::FLV_PROP_GRANULARITY) ;
if (mpPropTable[i].bytesPerEncode == -1)
return HXR_NOTIMPL ; // we absolutely need this number.
// tried to outsmart the broken sipro codec here, but to no avail.
#if 1
t = mpPropTable[i].bytesPerEncode ;
#else
t = getULONG32Prop(mpCodecRef, i, ::FLV_PROP_BITS_PER_FRAME) ;
if (t == -1)
{
// FLV_PROP_BITS_PER_FRAME does not exist -> try FLV_PROP_FRAME_SIZE
t = getULONG32Prop(mpCodecRef, i, ::FLV_PROP_FRAME_SIZE) ;
}
else if (t & 0x7)
{
// number of bits per frame is not a whole number. In that case, just go with the codec's
// idea of how to bundle frames.
t = mpPropTable[i].bytesPerEncode ;
}
else
{
t = t / 8 ;
}
if (t == -1) // ouch. Could not determine how many bytes per encode.
return HXR_NOTIMPL ;
#endif
mpPropTable[i].bytesPerFrame = t ;
mpPropTable[i].framesPerBlock = mpPropTable[i].bytesPerEncode / mpPropTable[i].bytesPerFrame ;
mpPropTable[i].samplesPerFrame = getULONG32Prop(mpCodecRef, i, ::FLV_PROP_SAMPLES_IN) ;
if (mpPropTable[i].samplesPerFrame == -1)
return HXR_NOTIMPL ; // we absolutely need this number
mpPropTable[i].samplesPerFrame /= mpPropTable[i].framesPerBlock ;
}
return HXR_OK ;
}
STDMETHODIMP_(UINT32) HXAudioEncoder::GetNumberOfFlavors() CONSTMETHOD
{
return ENTRYPOINT(RAGetNumberOfFlavors2)(mpCodecRef) ;
}
STDMETHODIMP HXAudioEncoder::SetFlavor(INT32 nFlavor)
{
return ENTRYPOINT(RASetFlavor)(mpCodecRef, mCurrentFlavorIndex = nFlavor) ;
}
STDMETHODIMP HXAudioEncoder::GetFlavorProperty(INT32 flvIndex, PROPERTY_LONG p, INT32& l) CONSTMETHOD
{
UINT16 pSize ;
UINT32* x ;
int code ;
if (flvIndex >= (INT32)GetNumberOfFlavors())
return HXR_INVALID_PARAMETER ;
if (flvIndex == FLV_CURRENT)
flvIndex = mCurrentFlavorIndex ;
code = -1 ;
switch (p)
{
case FLV_PROP_MIN_SAMPLES_IN:
l = mnSamplesIn ; break ;
// l = mpPropTable[flvIndex].minSamplesIn ; break ;
case FLV_PROP_MAX_BYTES_OUT:
l = mpPropTable[flvIndex].bytesPerFrame ; break ;
case FLV_PROP_PREDATA:
l = 0; break ;
case FLV_PROP_OBSOLETE_BITSPERFRAME:
if (code < 0) code = ::FLV_PROP_BITS_PER_FRAME ; // no break by intention
case FLV_PROP_OBSOLETE_GRANULARITY:
if (code < 0) code = ::FLV_PROP_GRANULARITY ; // no break by intention
case FLV_PROP_OBSOLETE_INTERLEAVE_FACTOR:
if (code < 0) code = ::FLV_PROP_INTERLEAVE_FACTOR ; // no break by intention
x = (UINT32*)ENTRYPOINT(RAGetFlavorProperty)(mpCodecRef, flvIndex, code, &pSize) ;
if (!x || pSize != sizeof(*x))
return HXR_NOTIMPL ;
else
l = *x ;
break ;
default:
return HXR_NOTIMPL ;
}
return HXR_OK ;
}
STDMETHODIMP HXAudioEncoder::GetFlavorProperty(INT32 flvIndex, PROPERTY_FLOAT p, float& l) CONSTMETHOD
{
HX_RESULT res ;
struct {long n,d;} x ;
UINT32 buflen ;
if (flvIndex >= (INT32)GetNumberOfFlavors())
return HXR_INVALID_PARAMETER ;
if (flvIndex == FLV_CURRENT)
flvIndex = mCurrentFlavorIndex ;
switch (p)
{
case FLV_PROP_BIT_RATE_APPROX:
res = GetFlavorProperty(flvIndex, FLV_PROP_BITS_PER_SECOND, (void*)&x, buflen = sizeof(x)) ;
if (FAILED(res))
return HXR_NOTIMPL ;
l = (float)((double)x.n / (double)x.d) ;
break ;
default:
return HXR_NOTIMPL;
}
return HXR_OK ;
}
static void RemoveCommonFactors(long &n, long &d)
{
long g = gcd(n,d) ;
n /= g ;
d /= g ;
}
STDMETHODIMP HXAudioEncoder::GetFlavorProperty(INT32 flvIndex, PROPERTY_VOID p, void *v, UINT32& buflen) CONSTMETHOD
{
UINT16 pSize ;
HX_AUDIO_FORMAT *pFmt ;
int code ;
if (flvIndex >= (INT32)GetNumberOfFlavors())
return HXR_INVALID_PARAMETER ;
if (flvIndex == FLV_CURRENT)
flvIndex = mCurrentFlavorIndex ;
pFmt = (HX_AUDIO_FORMAT *)ENTRYPOINT(RAGetFlavorProperty)(mpCodecRef, flvIndex, ::FLV_PROP_INPUT_AUDIO_FORMAT, &pSize) ;
if (!pFmt || pSize != sizeof(*pFmt))
return HXR_NOTIMPL ;
code = -1 ;
switch (p)
{
case FLV_PROP_BITS_PER_SAMPLE: {
struct tagX {long n,d;} *x = (struct tagX *)v ;
/* 8*bytesperframe / samplesPerFrame */
if (x && buflen >= sizeof(*x))
{
x->n = 8*mpPropTable[flvIndex].bytesPerFrame ;
x->d = mpPropTable[flvIndex].samplesPerFrame ;
RemoveCommonFactors(x->n, x->d) ;
}
buflen = sizeof(*x) ;
} break ;
case FLV_PROP_BITS_PER_SECOND: {
struct tagX {long n,d;} *x = (struct tagX *)v ;
/* 8*bytesperframe * sampleRate * nChannels /samplesPerFrame */
if (x && buflen >= sizeof(*x))
{
x->n = 8 * mpPropTable[flvIndex].bytesPerFrame ;
x->d = mpPropTable[flvIndex].samplesPerFrame ;
RemoveCommonFactors(x->n, x->d) ;
x->n *= pFmt->SamplesPerSec ;
RemoveCommonFactors(x->n, x->d) ;
x->n *= pFmt->Channels ;
RemoveCommonFactors(x->n, x->d) ;
}
buflen = sizeof(*x) ;
} break ;
case FLV_PROP_INPUT_AUDIO_FORMAT:
if (code < 0) code = ::FLV_PROP_INPUT_AUDIO_FORMAT ; // no break by intention
case FLV_PROP_FLAVOR_NAME:
if (code < 0) code = ::FLV_PROP_NAME ; // no break by intention
case FLV_PROP_CODEC_NAME:
if (code < 0) code = ::FLV_PROP_STATUS_TEXT ; // no break by intention
case FLV_PROP_FLAVOR_DESCRIPTION:
if (code < 0) code = ::FLV_PROP_DESCRIPTION ; // no break by intention
case FLV_PROP_OPAQUE_DATA:
if (code < 0) code = ::FLV_PROP_OPAQUE_DATA ; // no break by intention
{
void *x = ENTRYPOINT(RAGetFlavorProperty)(mpCodecRef, flvIndex, code, &pSize) ;
if (x && v && pSize <= buflen)
{
memcpy(v, x, pSize) ; /* Flawfinder: ignore */
}
buflen = pSize;
} break ;
default:
return HXR_NOTIMPL;
}
return HXR_OK ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -