Hi,
the example you are following just writes the frame out in raw format,
without doing any kind of sample rate or format conversion.
To convert a sample you need to check against the sample format
(dec_ctx->sample_fmt) and implement the right conversion.
For example if your audio stream uses a sample format of S16 (Signed
16-bit integer), converting that to float is really easy (Just divide by
max int16):
int sampleStride = av_get_bytes_per_sample(dec_ctx->sample_fmt);
for (int sampleIndex = 0; sampleIndex < frame->nb_samples;
++sampleIndex) {
for (int channel = 0; channel < dec_ctx->channels; ++channel) {
// For signed 16-bit integer to float conversion
if (dec_ctx->sample_fmt == AV_SAMPLE_FMT_S16) {
int16_t *inputPtr = (int16_t *)(frame->data[ch] +
sampleStride * sampleIndex);
int16_t inputSample = *inputPtr;
float outputSample;
if (inputSample < 0) {
outputSample = inputSample / (float)(INT16_MAX - 1);
} else {
outputSample = inputSample / (float)INT16_MAX;
}
}
}
}
Hope this helps.
Greetings,
Final
Am 20.10.2018 um 11:30 schrieb Matthieu Regnauld:
Hello,
I try to extract raw audio frames from an audio file, using the
following example:
http://ffmpeg.org/doxygen/3.4/decode_audio_8c-example.html
As far as I understand, I get these frames in the decode() function,
in the frame->data array.
That said, I need to convert the frames into floats between -1 and 1.
Here is what I tried (in my test, dataSize == 4):
// here is where I want to get my frame:
float myFrame;
// first try:
memcpy(&myFrame, &frame->data[ch][i], dataSize * sizeof(uint8_t));
// second try:
myFrame = (frame->data[ch][i]<<0) | (frame->data[ch][i + 1]<<8) |
(frame->data[ch][i + 2]<<16) | (frame->data[ch][i + 3]<<24);
// third try:
myFrame = (frame->data[ch][i + 3]<<0) | (frame->data[ch][i + 2]<<8) |
(frame->data[ch][i + 1]<<16) | (frame->data[ch][i]<<24);
But the problem is; it doesn't work, all I got so far is some kind of
white noice.
So what is the proper way to extract audio frames and convert them to
float amplitudes?
Thanks for your help.
_______________________________________________
Libav-user mailing list
[email protected]
http://ffmpeg.org/mailman/listinfo/libav-user
_______________________________________________
Libav-user mailing list
[email protected]
http://ffmpeg.org/mailman/listinfo/libav-user