Good moring,<br><br>I've seen that it's necessary to show the init methods, so here you have:<br><br>----------------------------------------8<-------------------------------------------<br><br>int audio_avcodec_init_encode(struct audio_avcodec_encode_state *aavces, int bit_rate, int sample_rate, int channels){<br>
<br> int enabled=0;<br> avcodec_register_all();<br><br> aavces->c= NULL;<br><br> /* find the encoder */<br> aavces->codec = avcodec_find_encoder(CODEC_ID_AAC); //AQUÍ STRING *codec, ara AAC default<br>
if (!aavces->codec) {<br> fprintf(stderr, "\n[avcodec - audio - encode] Codec not found");<br> //exit(1);<br> return enabled;<br> }else enabled = 1;<br><br> aavces->c= avcodec_alloc_context();<br>
<br> /* put sample parameters */<br> aavces->c->bit_rate = bit_rate;//64000;<br> aavces->c->sample_fmt = AV_SAMPLE_FMT_S16;<br> //aavces->c->channel_layout = AV_CH_LAYOUT_STEREO;<br><br> aavces->c->sample_rate = sample_rate;//48000; //TODO: get it from dp_map<br>
aavces->c->channels = channels;//2; //TODO<br> aavces->c->profile = FF_PROFILE_AAC_MAIN;//FF_PROFILE_AAC_LOW;<br> //aavces->c->time_base = (AVRational){1, sample_rate};<br> aavces->c->time_base.num = 1;<br>
aavces->c->time_base.den = sample_rate;<br> aavces->c->codec_type = AVMEDIA_TYPE_AUDIO;<br><br> /* open it */<br> if (avcodec_open(aavces->c, aavces->codec) < 0) {<br> fprintf(stderr, "\n[avcodec - audio - encode] Could not open codec");<br>
//exit(1);<br> return enabled;<br> }else enabled = 1;<br><br> /* the codec gives us the frame size, in samples */<br> //aavces->frame_size = aavces->c->frame_size;<br> //aavces->samples = malloc(aavces->frame_size * 2 * aavces->c->channels);<br>
<br> aavces->outbuf_size = 1024;//FF_MIN_BUFFER_SIZE * 10;<br> aavces->outbuf = (uint8_t *)av_malloc(aavces->outbuf_size);<br><br> aavces->fifo_buf = av_fifo_alloc(2*MAX_AUDIO_PACKET_SIZE);//FF_MIN_BUFFER_SIZE);<br>
aavces->fifo_outbuf = (uint8_t *)av_malloc(MAX_AUDIO_PACKET_SIZE);<br><br> if (!(aavces->outbuf == NULL))enabled = 1;<br><br> printf("\n[avcodec - audio - encode] Enabled!",enabled);<br><br> return enabled;<br>
<br>}<br><br>------------------------------->8------------------------------------------------------<br><br>Anyone can help me, please?<br><br>Hope not being a concept problem...<br><br>Thanks,<br clear="all"><div>--------------------<br>
Gerard C.L.<br>--------------------<br></div>
<br><br><div class="gmail_quote">2013/3/14 Gerard C.L. <span dir="ltr"><<a href="mailto:gerardcl@gmail.com" target="_blank">gerardcl@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
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>
</blockquote></div><br>