遇到和这个人一毛一样的问题 声音中存在噪声 其他的还好
[Libav-user] aac encoder in real time scenario
Gerard C.L.
gerardcl at gmail.com
Fri Mar 15 09:19:26 CET 2013
Good moring,
I've seen that it's necessary to show the init methods, so here you have:
----------------------------------------8<-------------------------------------------
int audio_avcodec_init_encode(struct audio_avcodec_encode_state *aavces,
int bit_rate, int sample_rate, int channels){
int enabled=0;
avcodec_register_all();
aavces->c= NULL;
/* find the encoder */
aavces->codec = avcodec_find_encoder(CODEC_ID_AAC); //AQUÍ STRING
*codec, ara AAC default
if (!aavces->codec) {
fprintf(stderr, "\n[avcodec - audio - encode] Codec not found");
//exit(1);
return enabled;
}else enabled = 1;
aavces->c= avcodec_alloc_context();
/* put sample parameters */
aavces->c->bit_rate = bit_rate;//64000;
aavces->c->sample_fmt = AV_SAMPLE_FMT_S16;
//aavces->c->channel_layout = AV_CH_LAYOUT_STEREO;
aavces->c->sample_rate = sample_rate;//48000; //TODO: get it from
dp_map
aavces->c->channels = channels;//2; //TODO
aavces->c->profile = FF_PROFILE_AAC_MAIN;//FF_PROFILE_AAC_LOW;
//aavces->c->time_base = (AVRational){1, sample_rate};
aavces->c->time_base.num = 1;
aavces->c->time_base.den = sample_rate;
aavces->c->codec_type = AVMEDIA_TYPE_AUDIO;
/* open it */
if (avcodec_open(aavces->c, aavces->codec) < 0) {
fprintf(stderr, "\n[avcodec - audio - encode] Could not open
codec");
//exit(1);
return enabled;
}else enabled = 1;
/* the codec gives us the frame size, in samples */
//aavces->frame_size = aavces->c->frame_size;
//aavces->samples = malloc(aavces->frame_size * 2 *
aavces->c->channels);
aavces->outbuf_size = 1024;//FF_MIN_BUFFER_SIZE * 10;
aavces->outbuf = (uint8_t *)av_malloc(aavces->outbuf_size);
aavces->fifo_buf =
av_fifo_alloc(2*MAX_AUDIO_PACKET_SIZE);//FF_MIN_BUFFER_SIZE);
aavces->fifo_outbuf = (uint8_t *)av_malloc(MAX_AUDIO_PACKET_SIZE);
if (!(aavces->outbuf == NULL))enabled = 1;
printf("\n[avcodec - audio - encode] Enabled!",enabled);
return enabled;
}
------------------------------->8------------------------------------------------------
Anyone can help me, please?
Hope not being a concept problem...
Thanks,
--------------------
Gerard C.L.
--------------------
2013/3/14 Gerard C.L. <gerardcl at gmail.com>
> Hi all,
>
> I'm developing an AAC encoder in a real time environment.
>
> The scene is:
> - Capture format -> PCM: 48kHz, stereo, 16b/sample. at 25fps -> so, per
> frame, 7680Bytes have to be encoded.
>
> 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.
> 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?
>
> 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...
>
>
> -----------------------------------8<------------------------------------------------------------------
> int audio_avcodec_encode(struct audio_avcodec_encode_state *aavces,
> unsigned char *inbuf, unsigned char *outbuf, int inbufsize) {
> AVPacket pkt;
> int frameBytes;
> int outsize = 0;
> int packetSize = 0;
> int ret;
> int nfifoBytes;
> int encBytes = 0;
> int sizeTmp = 0;
>
> frameBytes = aavces->c->frame_size * aavces->c->channels * 2;
> av_fifo_realloc2(aavces->fifo_buf,av_fifo_size(aavces->fifo_buf) +
> inbufsize);
>
> // Put the raw audio samples into the FIFO.
> ret = av_fifo_generic_write(aavces->fifo_buf, /*(int8_t*)*/inbuf,
> inbufsize, NULL );
>
> printf("\n[avcodec encode] raw buffer intput size: %d ; fifo size:
> %d",inbufsize, ret);
>
> //encoding each frameByte block
> while ((ret = av_fifo_size(aavces->fifo_buf)) >= frameBytes) {
> ret = av_fifo_generic_read(aavces->fifo_buf,
> aavces->fifo_outbuf,frameBytes, NULL );
>
> av_init_packet(&pkt);
>
> pkt.size = avcodec_encode_audio(aavces->c,
> aavces->outbuf,aavces->outbuf_size, (int16_t*) aavces->fifo_outbuf);
>
> if (pkt.size < 0) {
> printf("FFmpeg : ERROR - Can't encode audio frame.");
> }
> // Rescale from the codec time_base to the AVStream time_base.
> if (aavces->c->coded_frame && aavces->c->coded_frame->pts !=
> (int64_t) (AV_NOPTS_VALUE ))
> pkt.pts =
> av_rescale_q(aavces->c->coded_frame->pts,aavces->c->time_base,
> aavces->c->time_base);
>
> printf("\nFFmpeg : (%d) Writing audio frame with PTS:
> %lld.",aavces->c->frame_number, pkt.pts);
> printf("\n[avcodec - audio - encode] Encoder returned %d bytes of
> data",pkt.size);
>
> pkt.data = aavces->outbuf;
> pkt.flags |= AV_PKT_FLAG_KEY;
>
> memcpy(outbuf, pkt.data, pkt.size);
> }
>
> // any bytes left in audio FIFO to encode?
> nfifoBytes = av_fifo_size(aavces->fifo_buf);
>
> printf("\n[avcodec encode] raw buffer intput size: %d", nfifoBytes);
>
> if (nfifoBytes > 0) {
> memset(aavces->fifo_outbuf, 0, frameBytes);
> if (aavces->c->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
> int nFrameSizeTmp = aavces->c->frame_size;
> if (aavces->c->frame_size != 1 &&
> (aavces->c->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME))
> aavces->c->frame_size = nfifoBytes / (aavces->c->channels
> * 2);
>
> if (av_fifo_generic_read(aavces->fifo_buf,
> aavces->fifo_outbuf,nfifoBytes, NULL ) == 0) {
> if (aavces->c->frame_size != 1)
> encBytes = avcodec_encode_audio(aavces->c,
> aavces->outbuf,aavces->outbuf_size,(int16_t*) aavces->fifo_outbuf);
> else
> encBytes = avcodec_encode_audio(aavces->c,
> aavces->outbuf,nfifoBytes, (int16_t*) aavces->fifo_outbuf);
> }
> aavces->c->frame_size = nFrameSizeTmp;// restore the native
> frame size
> } else
> printf("\n[audio encoder] codec does not support small
> frames");
> }
>
> // Now flush the encoder.
> if (encBytes <= 0){
> encBytes = avcodec_encode_audio(aavces->c,
> aavces->outbuf,aavces->outbuf_size, NULL );
> printf("\nFFmpeg : flushing the encoder");
> }
> if (encBytes < 0) {
> printf("\nFFmpeg : ERROR - Can't encode LAST audio frame.");
> }
> av_init_packet(&pkt);
>
> sizeTmp = pkt.size;
>
> pkt.size = encBytes;
> pkt.data = aavces->outbuf;
> pkt.flags |= AV_PKT_FLAG_KEY;
>
> // Rescale from the codec time_base to the AVStream time_base.
> if (aavces->c->coded_frame && aavces->c->coded_frame->pts != (int64_t)
> (AV_NOPTS_VALUE ))
> pkt.pts =
> av_rescale_q(aavces->c->coded_frame->pts,aavces->c->time_base,
> aavces->c->time_base);
>
> printf("\nFFmpeg : (%d) Writing audio frame with PTS:
> %lld.",aavces->c->frame_number, pkt.pts);
> printf("\n[avcodec - audio - encode] Encoder returned %d bytes of
> data\n",pkt.size);
>
> memcpy(outbuf + sizeTmp, pkt.data, pkt.size);
>
> outsize = sizeTmp + pkt.size;
>
> return outsize;
> }
>
> -------------------------------------------------->8-------------------------------------------------
>
>
> Then, I'm saving outbuf with outsize per frame encoded.
>
> Any idea of what I'm doing wrong?
>
> Thanks in advance!
> --------------------
> Gerard C.L.
> --------------------
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20130315/c5f874e2/attachment.html>
More information about the Libav-user mailing list