---
title: GSMHR
description: "将PCM格式音频数据流编码成GSMHR格式音频数据流。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/240RC2/accel/kunpengaccel_hmpp_06_0280.html
sourcePath: /source/zh/kunpengboostkithistory/240RC2/accel/kunpengaccel_hmpp_06_0280.html
indexId: 94b38b901662e5355eae0301737284dd36d2f7f39a01f479e30da7af423a5da577
---
# GSMHR

将PCM格式音频数据流编码成GSMHR格式音频数据流。

该函数调用流程如下：

1. 获取编码需要申请的内存大小dstBufLen。
2. 调用HMPPA_Gsmhr_EncodeInit_16s8u初始化HmppaGsmhrEncodePolicy_16s结构体。
3. 调用主函数HMPPA_Gsmhr_Encode_16s8u解码。
4. 最后调用HMPPA_Gsmhr_EncodeRelease_16s8u释放HmppaGsmhrEncodePolicy_16s结构体所包含的内存。

函数接口声明如下：

- 获取编码需要申请的内存大小：
  HmppResult HMPPA_Gsmhr_GetEncodeDstBufLen_16s8u(int32_t len, int32_t *dstBufLen);

- 初始化函数：
  HmppResult HMPPA_Gsmhr_EncodeInit_16s8u(HmppaGsmhrEncodePolicy_16s **policy);

- 编码函数：
  HmppResult HMPPA_Gsmhr_Encode_16s8u(const int16_t *src, int32_t len, int32_t dtx, uint8_t *dst, HmppaGsmhrEncodePolicy_16s *policy, int64_t *bytesConsumed, int32_t *bytesEncoded);

- 释放函数：
  HmppResult HMPPA_Gsmhr_EncodeRelease_16s8u(HmppaGsmhrEncodePolicy_16s *policy);


#### 参数

| 参数名 | 描述 | 取值范围 | 输入/输出 |
| --- | --- | --- | --- |
| src | 指向待编码的PCM码流指针。 | 非空 | 输入 |
| len | 待编码PCM码流长度（以字节为单位）。 | (0, INT\_MAX] | 输入 |
| dtx | 是否开启DTX（非连续传输）功能的标志。 | 0：关闭DTX 1：开启DTX | 输入 |
| dst | 指向目的向量的指针。 | 非空 | 输出 |
| policy | 指向GSMHR结构体的指针。 | 非空 | 输入/输出 |
| bytesConsumed | 指向实际编码消耗的长度（以字节为单位）。 | [0, len] | 输出 |
| bytesEncoded | 指向实际编码输出的长度（以字节为单位）。 | [0, 理论编码len后输出的字节数] | 输出 |
| dstBufLen | 指向目的向量需要的长度（以双字节为单位）。 | 非空 | 输出 |


#### 返回值

- 成功：返回HMPP_STS_NO_ERR。
- 失败：返回错误码。


#### 错误码

| 错误码 | 描述 |
| --- | --- |
| HMPP\_STS\_NULL\_PTR\_ERR | 指针参数中包含空指针。 |
| HMPP\_STS\_SIZE\_ERR | len不为正数。 |
| HMPP\_STS\_SIZE\_WRN | src未全部编码。 |


#### 示例

```
#include<stdlib.h>
#include<stdio.h>
#include"hmppa.h"
#define ONE_FRAME_GSM 14
#define ONE_FRAME_PCM 160
int main(int argc, char* argv[])
{
if (argc <3) {
printf("%s in.pcm out.gsmhr\n", argv[0]);
return 0;
}
FILE *file =fopen(argv[1], "rb");
if (file ==NULL) {
printf("Open File %s Failed\n", argv[1]);
return 0;
}
fseek(file, 0, SEEK_END);
int32_t srcLen =ftell(file) / 2;
int32_t dstLen;
HMPPA_Gsmhr_GetEncodeDstBufLen_16s8u(srcLen, &dstLen);
fseek(file, 0, SEEK_SET);
short src[srcLen];
unsigned char rb[dstLen];
HmppaGsmhrEncodePolicy_16s *policy;
HMPPA_Gsmhr_EncodeInit_16s8u(&policy);
FILE *out = fopen(argv[2], "wb");
fread(src, sizeof(short), srcLen, file);
long int bytesConsumed;
int bytesEncoded;
int dtx = 1;
int res = HMPPA_Gsmhr_Encode_16s8u(src, srcLen, dtx, rb, policy, &bytesConsumed, &bytesEncoded);
HMPPA_Gsmhr_EncodeRelease_16s8u(policy);
printf("bytesConsumed = %d, bytesDecoded = %d, res = %d\n", bytesConsumed, bytesEncoded, res);
fwrite(rb, sizeof(unsigned char), bytesEncoded, out);
}
```

运行结果：

```
bytesConsumed = 50560, bytesDecoded = 2212, res = 0
```
