[libav-api] AAC encoding from mic
Mark Kenna
mark.kenna at sureviewsystems.com
Mon Jun 13 17:03:29 CEST 2011
Hi Guys
I am trying to encode raw PCM audio from the microphone to AAC audio
using LibAv. I am using the output_example.c as a reference but I'm
still hitting a brick wall. Here is what I am doing:
Creating the AAC codec and opening it (the input format matches what I
am setting the context to)
> AVStream* FLVTranscoder::AddAudioStream(AVFormatContext *oc,
> CodecID codec_id)
> {
> AVCodecContext *c;
> AVStream *st;
>
> st = av_new_stream(oc, 1);
> if (!st) {
> fprintf(stderr, "Could not alloc stream\n");
> exit(1);
> }
>
> c = st->codec;
> c->codec_id = codec_id;
> c->codec_type = AVMEDIA_TYPE_AUDIO;
>
> /* put sample parameters */
> c->sample_fmt = SAMPLE_FMT_S16;
> c->bit_rate = 32000;
> c->sample_rate = 8000;
> c->channels = 1;
>
> // some formats want stream headers to be separate
> if(oc->oformat->flags & AVFMT_GLOBALHEADER)
> c->flags |= CODEC_FLAG_GLOBAL_HEADER;
>
> return st;
> }
Open the Audio Codec....
Write the audio (note that the input is a 2048 length byte array)
> void Transcoder::WriteAudio(IntPtr data, int length)
> {
> array<int16_t>^ shortArray = gcnew array<int16_t>(length/2);
> Marshal::Copy(data, shortArray, 0, shortArray->Length);
>
> pin_ptr<int16_t> dataSegment = &shortArray[0];
> WriteAudioFrame(oc, audio_st, dataSegment, shortArray->Length);
> }
>
> void Transcoder::WriteAudioFrame(AVFormatContext *oc, AVStream
> *st, int16_t* data, int length)
> {
> AVCodecContext *c;
> AVPacket pkt;
> av_init_packet(&pkt);
>
> c = st->codec;
> pkt.size = avcodec_encode_audio(c, audio_outbuf,
> audio_outbuf_size, data);
>
> if (c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE)
> pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base,
> st->time_base);
>
> pkt.flags |= AV_PKT_FLAG_KEY;
> pkt.stream_index = st->index;
> pkt.data = audio_outbuf;
>
> /* write the compressed frame in the media file */
> if (av_write_frame(oc, &pkt) != 0)
> {
> fprintf(stderr, "Error while writing audio frame\n");
> exit(1);
> }
> }
But I keep getting assertion failed when calling av_write_frame.
Anyone have any suggestions?
Thanks,
Mark.
More information about the libav-api
mailing list