[Libav-user] Encoding live audiopackets
Ruurd Adema
ruurdadema at me.com
Thu Jul 16 07:34:01 CEST 2015
I finally found the problem and solved it.
Paul, your tip was very helpfull, thanks for that!
Initially I was writing to less samples to the encoder (960). To solve that I added an audio_fifo system, but I didn’t correct the amount of samples in the buffer frame, so the encoder was still processing too less samples.
Changing the sample amount of the frame made it work.
Ruurd
> On 15 Jul 2015, at 20:06, Ruurd Adema <ruurdadema at me.com> wrote:
>
> Yes, I tried that, I used an audio_fifo for that. Unfortunately makes no difference:
>
> // This part works well
> if (CODEC_TYPE == AV_CODEC_ID_PCM_S16LE)
> {
> audiopacket->GetPacketTime(&audio_pts, audio_stream->time_base.den);
>
> pkt.pts = audio_pts;
> pkt.dts = pkt.pts;
> pkt.flags |= AV_PKT_FLAG_KEY;
> pkt.stream_index = audio_stream->index;
> pkt.data = (uint8_t *)audiopacket_data;
> pkt.size = audiopacket_size;
>
> av_interleaved_write_frame(output_fmt_ctx, &pkt);
> }
> // This part doesn't work
> else if (CODEC_TYPE == AV_CODEC_ID_AAC)
> {
> frame = av_frame_alloc();
> frame->format = audio_stream->codec->sample_fmt;
> frame->channel_layout = audio_stream->codec->channel_layout;
> frame->sample_rate = audio_stream->codec->sample_rate;
> frame->nb_samples = audiopacket_sample_count;
>
> requested_size = av_samples_get_buffer_size(NULL, audio_stream->codec->channels, audio_stream->codec->frame_size, audio_stream->codec->sample_fmt, 1);
>
> result = av_audio_fifo_write(audio_fifo, &audiopacket_data, audiopacket_sample_count);
>
> audiopacket->GetPacketTime(&audio_pts, audio_stream->time_base.den);
>
> frame->pts = audio_pts;
>
> frame_buf = av_malloc(requested_size);
>
> // Check if there are enough samples to feed the encoder
> if (av_audio_fifo_size(audio_fifo) >= audio_stream->codec->frame_size)
> {
> result = av_audio_fifo_read(audio_fifo, &frame_buf, audio_stream->codec->frame_size);
>
> if (avcodec_fill_audio_frame(frame, audiopacket_channel_count, audio_stream->codec->sample_fmt, (const uint8_t *)frame_buf, requested_size, 1) < 0)
> {
> fprintf(stderr, "[ERROR] Filling audioframe failed!\n");
> exit(-1);
> }
>
> if (avcodec_encode_audio2(audio_stream->codec, &pkt, frame, &got_packet) != 0)
> {
> fprintf(stderr, "[ERROR] Encoding audio failed\n");
> }
>
> if (got_packet)
> {
> pkt.stream_index = audio_stream->index;
> pkt.flags |= AV_PKT_FLAG_KEY;
>
> av_interleaved_write_frame(output_fmt_ctx, &pkt);
> }
> }
> free(frame_buf);
> av_frame_free(&frame);
> }
> av_free_packet(&pkt);
>
> Thank, Ruurd
>
>> On 14 Jul 2015, at 21:16, Paul B Mahol <onemda at gmail.com <mailto:onemda at gmail.com>> wrote:
>>
>>
>> Dana 14. 7. 2015. 20:49 osoba "Ruurd Adema" <ruurdadema at me.com <mailto:ruurdadema at me.com>> napisala je:
>> >
>> > I'm trying to write live incoming audiopackets into a mov file with AAC encoding using the FFmpeg api.
>> >
>> > When using no encoding (AV_CODEC_ID_PCM_S16LE) it works well, when using AAC encoding (AV_CODEC_ID_AAC) it fails. The resulting audiofile plays too fast and sounds distorted.
>> >
>> > I’m new to the FFmpeg api, (and quite a beginner in programming anyway), so big chance I forgot something or doing something wrong. Is there anyone willing to help me with this one?
>> >
>> > audiopacket_sample_count = audiopacket->GetSampleFrameCount();
>> > audiopacket_channel_count = decklink_config()->audio_channel_count;
>> > audiopacket_size = audiopacket_sample_count * (decklink_config()->audio_sampletype/8) * audiopacket_channel_count;
>> >
>> > audiopacket->GetBytes(&audiopacket_data);
>> >
>> > av_init_packet(&pkt);
>> >
>> > if (AUDIO_TYPE == AV_CODEC_ID_PCM_S16LE)
>> > {
>> > audiopacket->GetPacketTime(&audio_pts, audio_stream->time_base.den);
>> >
>> > pkt.pts = audio_pts;
>> > pkt.dts = pkt.pts;
>> > pkt.flags |= AV_PKT_FLAG_KEY;
>> > pkt.stream_index = audio_stream->index;
>> > pkt.data = (uint8_t *)audiopacket_data;
>> > pkt.size = audiopacket_size;
>> >
>> > av_interleaved_write_frame(output_fmt_ctx, &pkt);
>> > }
>> > else if (AUDIO_TYPE == AV_CODEC_ID_AAC)
>> > {
>> > frame = av_frame_alloc();
>> > frame->format = audio_stream->codec->sample_fmt;
>> > frame->channel_layout = audio_stream->codec->channel_layout;
>> > frame->sample_rate = audio_stream->codec->sample_rate;
>> > frame->nb_samples = audiopacket_sample_count;
>> >
>> > audiopacket->GetPacketTime(&audio_pts, audio_stream->time_base.den);
>> >
>> > frame->pts = audio_pts;
>> >
>> > if (avcodec_fill_audio_frame(frame, audiopacket_channel_count, audio_stream->codec->sample_fmt, (const uint8_t *)audiopacket_data, audiopacket_size, 0) < 0)
>> > {
>> > fprintf(stderr, "[ERROR] Filling audioframe failed!\n");
>> > exit(-1);
>> > }
>> >
>> > if (avcodec_encode_audio2(audio_stream->codec, &pkt, frame, &got_packet) != 0)
>> > {
>> > fprintf(stderr, "[ERROR] Encoding audio failed\n");
>> > }
>> >
>> > if (got_packet)
>> > {
>> > pkt.stream_index = audio_stream->index;
>> > pkt.flags |= AV_PKT_FLAG_KEY;
>> >
>> > av_interleaved_write_frame(output_fmt_ctx, &pkt);
>> > }
>> > av_frame_free(&frame);
>> > }
>> > av_free_packet(&pkt);
>> >
>> >
>> >
>> > _______________________________________________
>> > Libav-user mailing list
>> > Libav-user at ffmpeg.org <mailto:Libav-user at ffmpeg.org>
>> > http://ffmpeg.org/mailman/listinfo/libav-user <http://ffmpeg.org/mailman/listinfo/libav-user>
>> >
>>
>> Do you send exact same number of samples that aac encoder request? You need to buffer samples....
>> _______________________________________________
>> Libav-user mailing list
>> Libav-user at ffmpeg.org <mailto:Libav-user at ffmpeg.org>
>> http://ffmpeg.org/mailman/listinfo/libav-user
>
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20150716/f0559aee/attachment.html>
More information about the Libav-user
mailing list