我要评分
获取效率
正确性
完整性
易理解

SILK

Decodes SILK audio data streams into the PCM format.

Input data streams support common frequencies including 8 kHz, 12 kHz, 16 kHz, and 24 kHz of the Silk protocol. In the case of uncommon frequencies, call the HMPP resampling function to convert the frequencies. When decoding the data streams where packet loss occurs, ensure that the input data streams are consistent with the packet loss rate (specified by lossProb). If packet loss occurs in the data streams, the decoding result may be different from the expected result due to the simulated packet loss process.

The function calling process is as follows:

  1. Obtain the size of the memory to be applied for decoding (dstBufLen).
  2. Initialize the HmppaSilkDecodePolicy_16s structure based on the value of decodeoutSampleRate by calling HMPPA_Silk_Init_8u16s.
  3. Call the main function HMPPA_Silk_Decode_8u16s for decoding.
  4. Call HMPPA_Silk_Release_8u16s to release the memory allocated to the HmppaSilkDecodePolicy_16s structure.

The function interface declaration is as follows:

  • Obtaining the size of the memory to be applied for decoding:

    HmppResult HMPPA_Silk_GetDecodeDstBufLen_8u16s(int32_t len, int32_t *dstBufLen);

  • Initialization:

    HmppResult HMPPA_Silk_DecodeInit_8u16s(HmppaSilkDecodePolicy_16s** policy, int32_t decodeoutSampleRate);

  • Decoding:

    HmppResult HMPPA_Silk_Decode_8u16s(const uint8_t *src, int32_t len, int16_t *dst, HmppaSilkDecodePolicy_16s *policy, int32_t *bytesConsumed, int64_t *bytesDecoded, float lossProb);

  • Release:

    HmppResult HMPPA_Silk_DecodeRelease_8u16s(HmppaSilkDecodePolicy_16s* policy);

Parameters

Parameter

Description

Value Range

Input/Output

src

Pointer to the SILK stream to be decoded

The value cannot be NULL.

Input

len

Length of the SILK stream to be decoded, in bytes

(0, INT_MAX]

Input

dst

Pointer to the destination vector

The value cannot be NULL.

Output

policy

Pointer to the special structure

The value cannot be NULL.

Input/Output

decodeoutSampleRate

Expected output bit rate, in Hz

If the input bit rate is 0, the output rate is 24,000 Hz by default. For other inputs, the output rate depends on the actual input value. Ensure that the input bit rate is valid (8000/12000/16000/24000).

Input

bytesConsumed

Pointer to the data stream length (in bytes) actually consumed for decoding

[0, len]

Output

bytesDecoded

Pointer to the data stream length (in bytes) of the actual decoded output

[0, Theoretical output data stream length after decoding]

Output

lossProb

Percentage of simulated packet loss

[0, 100]. If no packet is lost, set this parameter to 0.

Input

dstBufLen

Length (in duel-bytes) required to point to the destination vector

The value cannot be NULL.

Output

Return Value

  • Success: HMPP_STS_NO_ERR
  • Failure: An error code is returned.

Error Codes

Error Code

Description

HMPP_STS_NULL_PTR_ERR

The value of src, dst, or policy is NULL.

HMPP_STS_SIZE_ERR

The value of len is less than or equal to 0, or it does not match the valid frame length in src.

HMPP_STS_MALLOC_FAILED

Memory allocation failed.

HMPP_STS_FRAME_ERR

Decoding frame error.

Example

#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 inBuf[AUDIO_INBUF_SIZE];
    int32_t dstLen;
    HMPPA_Silk_GetDecodeDstBufLen_8u16s(AUDIO_INBUF_SIZE, &dstLen);
    int16_t *outBuf = HMPPS_Malloc_16s(dstLen);
    uint8_t *data;
    int32_t dataSize;
    int32_t bytesConsumered = 0; 
    int64_t bytesDecoded = 0;
    int32_t APIFsHz = 24000;

    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);
    }

    HmppaSilkDecodePolicy_16s *policy;
    HmppResult result = HMPPA_Silk_DecodeInit_8u16s(&policy, APIFsHz);
    if (result != HMPP_STS_NO_ERR){
	printf("HMPPA_Silk_Init_8u16s return : %d\n", result);
    }
    while (1){
        dataSize = fread(inBuf, 1, AUDIO_INBUF_SIZE, inFile);
        if (dataSize <= 0) {
			printf("INFO: Get end of the file\n");
            break;
        }
	result = HMPPA_Silk_Decode_8u16s(inBuf, dataSize, outBuf, policy, &bytesConsumered, &bytesDecoded,0.0f);
	if (result == HMPP_STS_NO_ERR) {
		printf("SUCCESS: HMPPA_Silk_Decode_8u16s : %d\n", result);
                fwrite(outBuf, 1, bytesDecoded, outFile);
	} else {
		printf("ERROR: HMPPA_Silk_Decode_8u16s : %d\n", result);
		break;
	}
    }

    HMPPA_Silk_DecodeRelease_8u16s(policy);
    HMPP_Free(outBuf);
    fclose(outFile);
    fclose(inFile);

    return 0;
}

Output:

INFO: Get end of the file
SUCCESS: HMPPA_Silk_Decode_8u16s : 0