Note that there are some explanatory texts on larger screens.

plurals
  1. POEncode audio to aac with libavcodec
    text
    copied!<p>I'm using libavcodec (latest git as of 3/3/10) to encode raw pcm to aac (libfaac support enabled). I do this by calling avcodec_encode_audio repeatedly with codec_context->frame_size samples each time. The first four calls return successfully, but the fifth call never returns. When I use gdb to break, the stack is corrupt.</p> <p>If I use audacity to export the pcm data to a .wav file, then I can use command-line ffmpeg to convert to aac without any issues, so I'm sure it's something I'm doing wrong.</p> <p>I've written a small test program that duplicates my problem. It reads the test data from a file, which is available here: <a href="http://birdie.protoven.com/audio.pcm" rel="noreferrer">http://birdie.protoven.com/audio.pcm</a> (~2 seconds of signed 16 bit LE pcm)</p> <p>I can make it all work if I use FAAC directly, but the code would be a little cleaner if I could just use libavcodec, as I'm also encoding video, and writing both to an mp4.</p> <p>ffmpeg version info:</p> <pre><code>FFmpeg version git-c280040, Copyright (c) 2000-2010 the FFmpeg developers built on Mar 3 2010 15:40:46 with gcc 4.4.1 configuration: --enable-libfaac --enable-gpl --enable-nonfree --enable-version3 --enable-postproc --enable-pthreads --enable-debug=3 --enable-shared libavutil 50.10. 0 / 50.10. 0 libavcodec 52.55. 0 / 52.55. 0 libavformat 52.54. 0 / 52.54. 0 libavdevice 52. 2. 0 / 52. 2. 0 libswscale 0.10. 0 / 0.10. 0 libpostproc 51. 2. 0 / 51. 2. 0 </code></pre> <p>Is there something I'm not setting, or setting incorrectly in my codec context, maybe? Any help is greatly appreciated!</p> <p>Here is my test code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;libavcodec/avcodec.h&gt; void EncodeTest(int sampleRate, int channels, int audioBitrate, uint8_t *audioData, size_t audioSize) { AVCodecContext *audioCodec; AVCodec *codec; uint8_t *buf; int bufSize, frameBytes; avcodec_register_all(); //Set up audio encoder codec = avcodec_find_encoder(CODEC_ID_AAC); if (codec == NULL) return; audioCodec = avcodec_alloc_context(); audioCodec-&gt;bit_rate = audioBitrate; audioCodec-&gt;sample_fmt = SAMPLE_FMT_S16; audioCodec-&gt;sample_rate = sampleRate; audioCodec-&gt;channels = channels; audioCodec-&gt;profile = FF_PROFILE_AAC_MAIN; audioCodec-&gt;time_base = (AVRational){1, sampleRate}; audioCodec-&gt;codec_type = CODEC_TYPE_AUDIO; if (avcodec_open(audioCodec, codec) &lt; 0) return; bufSize = FF_MIN_BUFFER_SIZE * 10; buf = (uint8_t *)malloc(bufSize); if (buf == NULL) return; frameBytes = audioCodec-&gt;frame_size * audioCodec-&gt;channels * 2; while (audioSize &gt;= frameBytes) { int packetSize; packetSize = avcodec_encode_audio(audioCodec, buf, bufSize, (short *)audioData); printf("encoder returned %d bytes of data\n", packetSize); audioData += frameBytes; audioSize -= frameBytes; } } int main() { FILE *stream = fopen("audio.pcm", "rb"); size_t size; uint8_t *buf; if (stream == NULL) { printf("Unable to open file\n"); return 1; } fseek(stream, 0, SEEK_END); size = ftell(stream); fseek(stream, 0, SEEK_SET); buf = (uint8_t *)malloc(size); fread(buf, sizeof(uint8_t), size, stream); fclose(stream); EncodeTest(32000, 2, 448000, buf, size); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload