G729a
将G729a格式音频数据流解码成PCM格式音频数据流。
该函数调用流程如下:
- 获取解码需要申请的内存大小dstBufLen。
- 调用HMPPA_G729a_DecodeInit_8u16s初始化HmppaG729aDecodePolicy_16s。
- 调用主函数HMPPA_G729a_Decode_8u16s。
- 最后调用HMPPA_G729a_DecodeRelease_8u16s释放HmppaG729aDecodePolicy_16s所包含的内存。
函数接口声明如下:
- 获取解码需要申请的内存大小:
HmppResult HMPPA_G729a_GetDecodeDstBufLen_8u16s(int32_t len, int32_t *dstBufLen);
- 初始化函数:
HmppResult HMPPA_G729a_DecodeInit_8u16s(HmppaG729DecodePolicy_16s **policy);
- 解码函数:
HmppResult HMPPA_G729a_Decode_8u16s(const uint8_t *src, int32_t len, int16_t *dst, HmppaG729DecodePolicy_16s *policy, int32_t *bytesConsumed, int64_t *bytesDecoded);
- 释放函数:
HmppResult HMPPA_G729a_DecodeRelease_8u16s(HmppaG729DecodePolicy_16s *policy);
参数
参数名 |
描述 |
取值范围 |
输入/输出 |
|---|---|---|---|
src |
指向待解码的G729a码流指针。 |
非空 |
输入 |
len |
待解码G729a码流长度(以字节为单位)。 |
(0, INT_MAX] |
输入 |
dst |
指向目的向量的指针。 |
非空 |
输出 |
policy |
特殊结构体的指针。 |
非空 |
输入/输出 |
bytesConsumed |
指向实际解码消耗的长度(以字节为单位)。 |
[0, len] |
输出 |
bytesDecoded |
指向实际解码输出的长度(以字节为单位)。 |
[0, 理论解码len后输出的字节数] |
输出 |
dstBufLen |
指向目的向量需要的长度(以双字节为单位)。 |
非空 |
输出 |
返回值
- 成功:返回HMPP_STS_NO_ERR。
- 失败:返回错误码。
错误码
错误码 |
描述 |
|---|---|
HMPP_STS_NULL_PTR_ERR |
src、dst、policy中存在空指针。 |
HMPP_STS_SIZE_ERR |
len小于或者等于0。 |
HMPP_STS_BAD_ARG_ERR |
policy结构体成员不合法。 |
HMPP_STS_MALLOC_FAILED |
申请内存失败。 |
HMPP_STS_SIZE_WRN |
len不是每帧所含字节数的整数倍。 |
示例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hmpp.h"
#define AUDIO_INBUF_SIZE 20480
int main(int argc, char **argv)
{
const char *outFileName, *inFileName;
int32_t len;
FILE *inFile, *outFile;
uint8_t *data;
int32_t dataSize;
int32_t bytesConsumered = 0;
int64_t bytesDecoded = 0;
if (argc <= 2) {
printf("Usage: %s <input file> <output file>\n", argv[0]);
exit(0);
}
inFileName = argv[1];
outFileName = argv[2];
inFile = fopen(inFileName, "rb");
if (!inFile) {
printf("Could not open %s\n", inFileName);
exit(1);
}
outFile = fopen(outFileName, "wb");
if (!outFile) {
printf("Could not open %s\n", outFileName);
exit(1);
}
HmppaG729aDecodePolicy_16s *policy;
HmppResult result = HMPPA_G729a_DecodeInit_8u16s(&policy);
if (result != HMPP_STS_NO_ERR){
printf("HMPPA_G729a_DecodeInit_8u16s return : %d\n", result);
}
fseek(inFile, 0, SEEK_END);
int32_t srcLen = ftell(inFile);
fseek(inFile, 0, SEEK_SET);
int32_t dstLen;
HMPPA_G729a_GetDecodeDstBufLen_8u16s(srcLen, &dstLen);
uint8_t *inBuf = (uint8_t*)malloc(srcLen * sizeof(uint8_t));
int16_t *outBuf = (int16_t *)malloc(dstLen * sizeof(int16_t));
fread(inBuf, sizeof(uint8_t), srcLen, inFile);
dataSize = srcLen;
result = HMPPA_G729a_Decode_8u16s(inBuf, dataSize, outBuf, policy, &bytesConsumered, &bytesDecoded);
fwrite(outBuf, 1, bytesDecoded, outFile);
printf("HmppResult: %d, bytesConsumed: %d, bytesDecoded: %d\n", result, bytesConsumered, bytesDecoded);
HMPPA_G729a_DecodeRelease_8u16s(policy);
free(inBuf);
free(outBuf);
fclose(outFile);
fclose(inFile);
return 0;
}
运行结果:
HmppResult: 0, bytesConsumed: 3160, bytesDecoded: 50560