I am having some trouble encoding audio using LibAv*.
I am currently taking raw unformatted PCM from the mic in 1024 byte chunks and trying to convert this to MP3 or AAC. The input format is single channel, 8khz 16-bit PCM. I was hoping to keep the output sample settings the same.

When encoding to MP3 I get large amounts of distortion but I can just about make out my voice in the background. When encoding to AAC, FFProbe states that there is invalid data in the input file.

Here is my audio encoding parameters and method:

...
    encContext.bit_rate = 11025;
    encContext.sample_rate = 8000;
    encContext.channels = 1;
...
    //I take the input byte array and marshal that to an array of short's

int AudioEncoder::EncodeAudio(array<short>^ input, array<unsigned char>^% output)
    {
        //make sure the output is null
        output = nullptr;

        int outbuf_size = 100000;
        uint8_t* outbuf = (uint8_t*)av_malloc(outbuf_size);

        if (outbuf)
        {
            //convert the byte array to unmanaged
            pin_ptr<short> unmanagedInput = &input[0];
            int16_t* samples = (int16_t*)unmanagedInput;

            //try and do the encoding
int out_size = avcodec_encode_audio(encContext, outbuf, input->Length, samples);

            if (out_size > 0)
            {
                //now copy the output back...
                output = gcnew array<unsigned char>(out_size);
                Marshal::Copy(IntPtr(outbuf), output, 0, output->Length);
            }

            av_free(samples);
            samples = NULL;
            av_free(outbuf);
            outbuf = NULL;

            return out_size;
        }

        return -1;
    }


_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to