I have char pAudioBuffer
buffer which i got from function ffmpeg:
int len = avcodec_decode_audio3(av_codec_context,
(int16_t *) pAudioBuffer, &out_size, &packet);
I know that audio format is two bytes per sample, i need to convert every two bytes to short value, i have tried to use code snippet below, but i often got zero instead of short value:
int shortBufIndex = 0;
for (int i = 0; i < (out_size); i += 2) {
char b1 = pAudioBuffer[i];
char b2 = pAudioBuffer[i + 1];
short sample = atoi(&b1) + atoi(&b2);
shortBuffer[shortBufIndex] = sample;
shortBufIndex++;
LOGI("BUFFER_ITEM='%d'", sample);
}
What i'm doing wrong, how to convert every two bytes in char buffer to short and and back.
UPDATE:
system's byte order is LITTLE_ENDIAN i have test it like this: Endianness of Android NDK
How can i convert every two bytes in buffer to sample of short type and back. Please can you provide any code sample.
UPDATE
I have tried to access to short as pairs, here is my fixed code, but it not work, i don't hear any sound:
int shortBufIndex = 0;
for (int i = 0; i < (out_size); i += 2) {
char * byte = (char *) pAudioBuffer[i];
short * sample = byte;
shortBuffer[shortBufIndex] = sample;
}
What i'm doing wrong? I need conversion like this: byte array to short array and back again in java but in c.