As in the title I need to read short integers from a char buffer
The buffer
uint8_t *data[AV_NUM_DATA_POINTERS]
which is a field of the AVFrame frame
structure, is filled by a call to the ffmpeg function
avcodec_decode_audio4(avctx,frame,got_frame_ptr,avpkt)
But, I need to read this buffer as a buffer of signed 16 bits integers because this is the sample format indicated by the codec context avctx->sample_fmt==AV_SAMPLE_FMT_S16
I tried to do this using a memcpy but I have not succeeded to get reasonable values so then I tried to use a union struct as suggested on some related questions here in StackOverflow. My code is as follows: union CharToStruct{ uint8_t myCharArray[2]; short value; } presentSound;
audioRet=avcodec_decode_audio4(avctx,frame,got_frame_ptr,avpkt);
if(got_frame_ptr){
audioRet=audioRet/2;
int b=0;
for(int i=0;i<audioRet;i++){
presentSound.myCharArray[0]=frame->data[0][2*i+1];
presentSound.myCharArray[1]=frame->data[0][2*i]
dbuf[((i-b)/2)*8+info->mLeft+b]=info->presentSound.value;//the reason of the offset by 8 here is because I will be writing the result to a multichannel device
}
With this, the values are reasonable, but when I write this to a device using portaudio, I get just clicking noise. Am I doing the conversion in a wrong way? Can you help me maybe with some better way to do this reading?
Thank you very much for your help
Alba