I am stuck with FFmpeg.AutoGen G726 Audio codec, it always shows error invalid argument.
Note that I Tried other codecs such as G722, G711 and it is working fine except for G726 codec. I also confirm that my binaries does support G726 codec using this command ffmpeg -formats. Please tell me what could be the reason, and if you know any other library for decoding G726 that would be better
public unsafe class AudioDecoder : IDisposable
{
private AVCodecContext* _codecContext;
private AVCodec* _codec;
private AVFrame* _frame;
private AVPacket* _packet;
public AudioDecoder()
{
// Find the G726 decoder
_codec = ffmpeg.avcodec_find_decoder(AVCodecID.AV_CODEC_ID_ADPCM_G726);
if (_codec == null)
{
throw new Exception("G726 codec not found.");
}
// Allocate codec context
_codecContext = ffmpeg.avcodec_alloc_context3(_codec);
if (_codecContext == null)
{
throw new Exception("Failed to allocate codec context.");
}
// Print supported sample formats
Console.WriteLine("Supported sample formats for G726 codec:");
AVSampleFormat* sample_fmts = _codec->sample_fmts;
if (sample_fmts != null)
{
while (*sample_fmts != AVSampleFormat.AV_SAMPLE_FMT_NONE)
{
Console.WriteLine($"Sample format: {*sample_fmts}");
sample_fmts++;
}
}
// Set codec parameters
_codecContext->sample_rate = 8000; // G726 typically uses 8000Hz
if (_codec->sample_fmts != null)
{
_codecContext->sample_fmt = *_codec->sample_fmts; // Use the first supported sample format
}
else
{
_codecContext->sample_fmt = AVSampleFormat.AV_SAMPLE_FMT_S16; // Fallback to a common format
}
_codecContext->bit_rate = 32000; // Example bitrate for G726
// Attempt to open the codec with detailed error logging
int ret = ffmpeg.avcodec_open2(_codecContext, _codec, null);
if (ret < 0)
{
byte[] errBuf = new byte[1024];
fixed (byte* errPtr = errBuf)
{
ffmpeg.av_strerror(ret, errPtr, (ulong)errBuf.Length);
}
string errMessage = System.Text.Encoding.UTF8.GetString(errBuf);
throw new Exception($"Failed to open codec (Error code: {ret}): {errMessage}");
}
// Allocate packet and frame
_packet = ffmpeg.av_packet_alloc();
_frame = ffmpeg.av_frame_alloc();
if (_packet == null || _frame == null)
{
throw new Exception("Failed to allocate packet or frame.");
}
}
}