Hi all,<br><br>I'm developing an AAC encoder in a real time environment. <br><br>The scene is:<br>- Capture format -> PCM: 48kHz, stereo, 16b/sample. at 25fps -> so, per frame, 7680Bytes have to be encoded.<br>
<br>The first problem become when I realised that the encoder works on fixed chunk sizes (in this case, for the audio configuration, the size is 4096Bytes per chunk). So, working like a file encoder, I was only encoding 4096bytes of the 7680 per frame.<br>
The solution was implementing FIFOs, using the av_fifo_.. methods. So now, I can hear the entire captured sound per frame, but I hear some garbage and I don't know if it's because of the encoder or how I work with the fifo or if I have conceptual errors in my mind. To note that I'm playing the sound after saving it to a file, could it be also the problem? <br>
<br>I'm copying the piece of code I've implemented right now, I'd love if some one gets the error... I'm so noob...<br><br>-----------------------------------8<------------------------------------------------------------------<br>
<font>int audio_avcodec_encode(struct audio_avcodec_encode_state *aavces, unsigned char *inbuf, unsigned char *outbuf, int inbufsize) {<br> AVPacket pkt;<br> int frameBytes;<br> int outsize = 0;<br> int packetSize = 0;<br>
int ret;<br> int nfifoBytes;<br> int encBytes = 0;<br> int sizeTmp = 0;<br><br> frameBytes = aavces->c->frame_size * aavces->c->channels * 2;<br> av_fifo_realloc2(aavces->fifo_buf,av_fifo_size(aavces->fifo_buf) + inbufsize);<br>
<br> // Put the raw audio samples into the FIFO.<br> ret = av_fifo_generic_write(aavces->fifo_buf, /*(int8_t*)*/inbuf, inbufsize, NULL );<br><br> printf("\n[avcodec encode] raw buffer intput size: %d ; fifo size: %d",inbufsize, ret);<br>
<br> //encoding each frameByte block<br> while ((ret = av_fifo_size(aavces->fifo_buf)) >= frameBytes) {<br> ret = av_fifo_generic_read(aavces->fifo_buf, aavces->fifo_outbuf,frameBytes, NULL );<br>
<br> av_init_packet(&pkt);<br><br> pkt.size = avcodec_encode_audio(aavces->c, aavces->outbuf,aavces->outbuf_size, (int16_t*) aavces->fifo_outbuf);<br><br> if (pkt.size < 0) {<br> printf("FFmpeg : ERROR - Can't encode audio frame.");<br>
}<br> // Rescale from the codec time_base to the AVStream time_base.<br> if (aavces->c->coded_frame && aavces->c->coded_frame->pts != (int64_t) (AV_NOPTS_VALUE ))<br> pkt.pts = av_rescale_q(aavces->c->coded_frame->pts,aavces->c->time_base, aavces->c->time_base);<br>
<br> printf("\nFFmpeg : (%d) Writing audio frame with PTS: %lld.",aavces->c->frame_number, pkt.pts);<br> printf("\n[avcodec - audio - encode] Encoder returned %d bytes of data",pkt.size);<br>
<br> pkt.data = aavces->outbuf;<br> pkt.flags |= AV_PKT_FLAG_KEY;<br><br> memcpy(outbuf, pkt.data, pkt.size);<br> }<br><br> // any bytes left in audio FIFO to encode?<br> nfifoBytes = av_fifo_size(aavces->fifo_buf);<br>
<br> printf("\n[avcodec encode] raw buffer intput size: %d", nfifoBytes);<br><br> if (nfifoBytes > 0) {<br> memset(aavces->fifo_outbuf, 0, frameBytes);<br> if (aavces->c->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {<br>
int nFrameSizeTmp = aavces->c->frame_size;<br> if (aavces->c->frame_size != 1 && (aavces->c->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME))<br> aavces->c->frame_size = nfifoBytes / (aavces->c->channels * 2);<br>
<br> if (av_fifo_generic_read(aavces->fifo_buf, aavces->fifo_outbuf,nfifoBytes, NULL ) == 0) {<br> if (aavces->c->frame_size != 1)<br> encBytes = avcodec_encode_audio(aavces->c, aavces->outbuf,aavces->outbuf_size,(int16_t*) aavces->fifo_outbuf);<br>
else<br> encBytes = avcodec_encode_audio(aavces->c, aavces->outbuf,nfifoBytes, (int16_t*) aavces->fifo_outbuf);<br> }<br> aavces->c->frame_size = nFrameSizeTmp;// restore the native frame size<br>
} else<br> printf("\n[audio encoder] codec does not support small frames");<br> }<br><br> // Now flush the encoder.<br> if (encBytes <= 0){<br> encBytes = avcodec_encode_audio(aavces->c, aavces->outbuf,aavces->outbuf_size, NULL );<br>
printf("\nFFmpeg : flushing the encoder");<br> }<br> if (encBytes < 0) {<br> printf("\nFFmpeg : ERROR - Can't encode LAST audio frame.");<br> }<br> av_init_packet(&pkt);<br>
<br> sizeTmp = pkt.size;<br><br> pkt.size = encBytes;<br> pkt.data = aavces->outbuf;<br> pkt.flags |= AV_PKT_FLAG_KEY;<br><br> // Rescale from the codec time_base to the AVStream time_base.<br> if (aavces->c->coded_frame && aavces->c->coded_frame->pts != (int64_t) (AV_NOPTS_VALUE ))<br>
pkt.pts = av_rescale_q(aavces->c->coded_frame->pts,aavces->c->time_base, aavces->c->time_base);<br><br> printf("\nFFmpeg : (%d) Writing audio frame with PTS: %lld.",aavces->c->frame_number, pkt.pts);<br>
printf("\n[avcodec - audio - encode] Encoder returned %d bytes of data\n",pkt.size);<br><br> memcpy(outbuf + sizeTmp, pkt.data, pkt.size);<br><br> outsize = sizeTmp + pkt.size;<br><br> return outsize;<br>
}</font><br>-------------------------------------------------->8-------------------------------------------------<br><br><br>Then, I'm saving outbuf with outsize per frame encoded.<br><br>Any idea of what I'm doing wrong?<br>
<br>Thanks in advance!<br>--------------------<br><div> Gerard C.L.<br>--------------------<br></div>