ffmpeg audio decoding method (with code) + ffmpeg fast speed playback audio decoder problems (note readily available for inspection)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/xjb2006/article/details/78980628

In doing recently named the same transfer speed of a player when decoding audio I encountered some problems (fast playback speed ffmpeg audio decoding problem) method on the network effective for the vast majority of audio and video files, but to one there will be some audio problems, such as certain ADPCM-encoded WAV audio files.

Paste the code directly to the audio decoding part of it:

			int pkt_size = packet->size;
			for(;;)//解码,必须解码完,因为可能不止一帧数据。改变packet->data的指针
			{
				int got_picture=0;
				Lock.Lock();
				int ret = avcodec_decode_audio4(pCodecCtx_A, pFrame_A, &got_picture, packet);
				Lock.Unlock();
				if(ret<0)
					break;
				if(got_picture<=0)
					break;
				if(got_picture&&ret>=0)//这里是resample
				{  
					int bytes_per_sample=Getbytes_per_sample(out_sample_fmt);
					int in_samples_per_channel=pFrame_A->nb_samples;
					int out_samples_per_channel= av_rescale_rnd(128 +in_samples_per_channel , out_sample_rate, pFrame_A->sample_rate, AV_ROUND_UP);
					int size_per_sample_with_channels = out_channels*bytes_per_sample;
					int out_size = out_samples_per_channel*size_per_sample_with_channels;
					unsigned char *out[] = {(unsigned char*)audio_out_buffer};
					int converted_samplers_per_channel=swr_convert(audio_convert_ctx,out, out_samples_per_channel,(const uint8_t **)pFrame_A->extended_data , in_samples_per_channel);  
					if(converted_samplers_per_channel>0)
					{
						fifo.push((BYTE*)audio_out_buffer,converted_samplers_per_channel*size_per_sample_with_channels);
					}
				}
				Lock.Lock();
				packet->data+=ret;	//重要!!,必须改变输入数据的指针
				Lock.Unlock();
				pkt_size-=ret;
				if(pkt_size<=0)
					break;
			}

Code above: It should be noted that the general line of the code only int ret = avcodec_decode_audio4 (pCodecCtx_A, pFrame_A , & got_picture, packet); then call swr_convert for PCM format is converted into the output, a method for ADPCM compressed audio files or other a these files do not work, thus breaking the sound effects sound like accelerated playback will appear. This method is actually idealized, int = Nread by av_read_frame (pFormatCtx, Packet) separation (DEMUX) out of the audio frame is possible and more than one, where the decoding cycles must be dead by int ret = avcodec_decode_audio4 (pCodecCtx_A , pFrame_A, & got_picture, packet) ; decoding the return value is determined how much data is decoded, and the decoded data pointer is changed to continue the rest of the decoded audio frames until a decoded!

Next record, for later use, but also want to help you to encounter the same problem!

Guess you like

Origin blog.csdn.net/xjb2006/article/details/78980628