[Libav-user] Decoding audio with libav produces errors

Dr. Sven Alisch svenali at t-online.de
Tue Jan 25 00:47:12 EET 2022


Dear libav-Developers,
Dear Users of this library,

I use this lib to decode radio channels, which using Vobis and AAC. The received audio packets will be copied into a Ringbuffer which I continuously check and copy the packets from there to an individual Buffer for this library, so to an AVPacket, which will be used for the decoding process. It works so far. Sometimes, often when the buffer is reread and new packages have to be copied, I got mistakes. I wrote an another function and gave them to the lib123 direct without any problems which means, the my RingBuffer is working. After the decoding process, I throw like an event the decoded Frames to a functions that serves the player.

The main problem is: I get many of these messages here: «Input buffer exhausted before END element found». If this error occur, the decoding process breaks and no frame will be decoded.

Here is my code that shows the function I call, if AAC is recognized.

void CInternetAudio::decodeAudio()
{
	audioFormat = «";
	if (_initNewCodec)
	{
		_Codec = avcodec_find_decoder(AV_CODEC_ID_AAC);
		if (!_Codec)
		{
			cerr << "codec not found" << endl;
			return;
		}

		_CodecContext = avcodec_alloc_context3(_Codec);

		if (avcodec_open2(_CodecContext, _Codec, NULL) < 0)
		{
			cerr << "could not open codec" << endl;
			return;
		}

		_initNewCodec = false;
	}

	this_thread::sleep_for(chrono::microseconds(3));

	SwrContext *swr = NULL;
	uint8_t *pcmAudioBuffer = (uint8_t*) av_malloc (AUDIO_INBUF_SIZE * 2);

	av_init_packet(&_AVPacket);

	uint8_t data[AUDIO_INBUF_SIZE * 20];
	int buffer_size = AUDIO_INBUF_SIZE * 20;

	_AVPacket.data = data;
	_AVPacket.size = 0;

	while (_running)
	{
		int32_t size = _streamBuffer.GetRingBufferReadAvailable();

		if (size > 0)
		{
			cout << "Decoding ... AAC ... lets go ..." << endl;

			if (_AVPacket.size > 0)
			{
				memmove(data, _AVPacket.data, _AVPacket.size);
				_AVPacket.data = data;
				int len = _streamBuffer.getDataFromBuffer(_AVPacket.data + _AVPacket.size, buffer_size - _AVPacket.size);

				if (len > 0)
					_AVPacket.size += len;
			}
			else
			{
				_AVPacket.size = _streamBuffer.getDataFromBuffer(_AVPacket.data, buffer_size);
			}

			while (_AVPacket.size > AUDIO_REFILL_THRESH)
			{
				int got_frame = 0;
				int frame_len;

				if (!_decodedFrame)
				{
					if (!(_decodedFrame = av_frame_alloc()))
					{
						cerr << "out of memory" << endl;
					}
				}
				else
				{
					cout << "Initializing Frame ..." << endl;
					av_frame_unref(_decodedFrame);
					_decodedFrame = av_frame_alloc();
			}

			cout << "Decoding Frame ..." << endl;
			frame_len = avcodec_decode_audio4(_CodecContext, _decodedFrame, &got_frame, &_AVPacket);

			if (!swr)
			{
				swr = swr_alloc_set_opts(NULL,
					AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT, // output
					AV_SAMPLE_FMT_S16, // output
					44100, // output
					AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT, // input
					AV_SAMPLE_FMT_FLTP, // input
					44100, // input
					0,
					NULL);
				int swrInit = swr_init(swr);
				if (swrInit < 0)
				{
					cerr << "swr init error swrInit " << swrInit << endl;
					exit(1);
				}
			}

			if (frame_len < 0)
			{
				cerr << "ERROR WHILE DECODING ..." << endl;
				_AVPacket.data ++;
				_AVPacket.size —;
				break;
			}
			if (got_frame)
			{
				cout << "Frame decoded ... with channels ..." << _decodedFrame->channels << endl;

				int data_size = _decodedFrame->nb_samples*_decodedFrame->channels;
				audioChannels = _decodedFrame->channels;
				swr_convert(swr, &pcmAudioBuffer, _decodedFrame->nb_samples, (const uint8_t**)_decodedFrame->extended_data, _decodedFrame->nb_samples);

				audioSamplerate = _decodedFrame->sample_rate;

				PutAudio(pcmAudioBuffer, data_size * 2);

				_AVPacket.size -= frame_len;
				_AVPacket.data += frame_len;
				_AVPacket.dts =
				_AVPacket.pts = AV_NOPTS_VALUE;
			}
		}
		else
		{
			cerr << "Wait for INPUT " << endl;
			this_thread::sleep_for(chrono::seconds(3));
		}
	}
}

Thank you for helping.

Regards,
Sven
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: Message signed with OpenPGP
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20220124/d2941f50/attachment.sig>


More information about the Libav-user mailing list