Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#3557 closed defect (invalid)

bug when resampling stereo to stereo

Reported by: Oleg Owned by:
Priority: normal Component: swresample
Version: unspecified Keywords:
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

I found a bug in ffmpeg.

#include "stdafx.h"
#include <iostream>

extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
//#include "swscale.h"
#include "libswresample/swresample.h"
};

FILE           *fin,
	*fout;

int ffmpeg_audio_decode( const char * inFile, const char * outFile)
{
	// Initialize FFmpeg
	av_register_all();

	AVFrame* frame = avcodec_alloc_frame();
	if (!frame)
	{
		std::cout << "Error allocating the frame" << std::endl;
		return 1;
	}

	// you can change the file name "01 Push Me to the Floor.wav" to whatever the file is you're reading, like "myFile.ogg" or
	// "someFile.webm" and this should still work
	AVFormatContext* formatContext = NULL;
	//if (avformat_open_input(&formatContext, "01 Push Me to the Floor.wav", NULL, NULL) != 0)
	if (avformat_open_input(&formatContext, inFile, NULL, NULL) != 0)
	{
		av_free(frame);
		std::cout << "Error opening the file" << std::endl;
		return 1;
	}
	
	if (avformat_find_stream_info(formatContext, NULL) < 0)
	{
		av_free(frame);
		av_close_input_file(formatContext);
		std::cout << "Error finding the stream info" << std::endl;
		return 1;
	}

	AVStream* audioStream = NULL;
	// Find the audio stream (some container files can have multiple streams in them)
	for (unsigned int i = 0; i < formatContext->nb_streams; ++i)
	{
		if (formatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
		{
			audioStream = formatContext->streams[i];
			break;
		}
	}

	if (audioStream == NULL)
	{
		av_free(frame);
		av_close_input_file(formatContext);
		std::cout << "Could not find any audio stream in the file" << std::endl;
		return 1;
	}

	AVCodecContext* codecContext = audioStream->codec;

	codecContext->codec = avcodec_find_decoder(codecContext->codec_id);
	if (codecContext->codec == NULL)
	{
		av_free(frame);
		av_close_input_file(formatContext);
		std::cout << "Couldn't find a proper decoder" << std::endl;
		return 1;
	}
	else if (avcodec_open2(codecContext, codecContext->codec, NULL) != 0)
	{
		av_free(frame);
		av_close_input_file(formatContext);
		std::cout << "Couldn't open the context with the decoder" << std::endl;
		return 1;
	}

	std::cout << "This stream has " << codecContext->channels << " channels and a sample rate of " << codecContext->sample_rate << "Hz" << std::endl;
	std::cout << "The data is in the format " << av_get_sample_fmt_name(codecContext->sample_fmt) << std::endl;

	//codecContext->sample_fmt = AV_SAMPLE_FMT_S16;

	int64_t outChannelLayout = AV_CH_LAYOUT_MONO; //AV_CH_LAYOUT_STEREO;
	AVSampleFormat outSampleFormat = AV_SAMPLE_FMT_S16; // Packed audio, non-planar (this is the most common format, and probably what you want; also, WAV needs it)
	int outSampleRate = 8000;//44100;
	// Note that AVCodecContext::channel_layout may or may not be set by libavcodec. Because of this,
	// we won't use it, and will instead try to guess the layout from the number of channels.
	SwrContext* swrContext = swr_alloc_set_opts(NULL,
		outChannelLayout,
		outSampleFormat,
		outSampleRate,
		av_get_default_channel_layout(codecContext->channels),
		codecContext->sample_fmt,
		codecContext->sample_rate,
		0,
		NULL);

	if (swrContext == NULL)
	{
		av_free(frame);
		avcodec_close(codecContext);
		avformat_close_input(&formatContext);
		std::cout << "Couldn't create the SwrContext" << std::endl;
		return 1;
	}

	if (swr_init(swrContext) != 0)
	{
		av_free(frame);
		avcodec_close(codecContext);
		avformat_close_input(&formatContext);
		swr_free(&swrContext);
		std::cout << "Couldn't initialize the SwrContext" << std::endl;
		return 1;
	}

	fout = fopen(outFile, "wb+");

	AVPacket packet;
	av_init_packet(&packet);

	// Read the packets in a loop
	while (av_read_frame(formatContext, &packet) == 0)
	{
		if (packet.stream_index == audioStream->index)
		{
			AVPacket decodingPacket = packet;

			while (decodingPacket.size > 0)
			{
				// Try to decode the packet into a frame
				int frameFinished = 0;
				int result = avcodec_decode_audio4(
codecContext, 
frame, 
&frameFinished, 
&decodingPacket);

				if (result < 0 || frameFinished == 0)
				{
					break;
				}
				
				unsigned char buffer[100000] = {NULL};
				unsigned char* pointers[SWR_CH_MAX] = {NULL};
				pointers[0] = &buffer[0];
				
				int numSamplesOut = swr_convert(
swrContext,
pointers,
outSampleRate,
(const unsigned char**)frame->extended_data,
frame->nb_samples);


				fwrite(  
(short *)buffer, 
sizeof(short), 
(size_t)numSamplesOut, 
fout);

				decodingPacket.size -= result;
				decodingPacket.data += result;
			}

		}

		// You *must* call av_free_packet() after each call to av_read_frame() or else you'll leak memory
		av_free_packet(&packet);
	}

	// Some codecs will cause frames to be buffered up in the decoding process. If the CODEC_CAP_DELAY flag
	// is set, there can be buffered up frames that need to be flushed, so we'll do that
	if (codecContext->codec->capabilities & CODEC_CAP_DELAY)
	{
		av_init_packet(&packet);
		// Decode all the remaining frames in the buffer, until the end is reached
		int frameFinished = 0;
		while (avcodec_decode_audio4(codecContext, frame, &frameFinished, &packet) >= 0 && frameFinished)
		{
		}
	}

	// Clean up!
	av_free(frame);
	avcodec_close(codecContext);
	av_close_input_file(formatContext);
	fclose(fout);
}


When files 02.mp3 are converted into a format 8000 pcm mono okay.

See file voice_01_sinus_8000_mono.raw.

Any discrete mono converted well.



Any discrete stereo converted bad.

When converting to pcm stereo 8000 it turns wrong.

See file voice_01_ sinus_ 8000_stereo.raw.


When converting to pcm 44100 stereo also turns out not correct.

See file voice_01_ sinus_ 44100_stereo.raw. Distort the shape of a sine wave.


Attachments (8)

02.mp3 (623.9 KB ) - added by Oleg 11 years ago.
voice_01_sinus_8000_mono.JPG (98.4 KB ) - added by Oleg 11 years ago.
voice_01_sinus_8000_stereo.raw (624.5 KB ) - added by Oleg 11 years ago.
voice_01_sinus_8000_mono.raw (624.5 KB ) - added by Oleg 11 years ago.
voice_01_sinus_44100_stereo.raw (867.7 KB ) - added by Oleg 11 years ago.
voice_01_sinus_8000_stereo.JPG (106.7 KB ) - added by Oleg 11 years ago.
voice_01_sinus_44100_stereo.JPG (61.7 KB ) - added by Oleg 11 years ago.
decode_audio_test.cpp (7.8 KB ) - added by Oleg 11 years ago.

Change History (16)

by Oleg, 11 years ago

Attachment: 02.mp3 added

by Oleg, 11 years ago

by Oleg, 11 years ago

by Oleg, 11 years ago

by Oleg, 11 years ago

by Oleg, 11 years ago

Attachment: decode_audio_test.cpp added

comment:1 by Carl Eugen Hoyos, 11 years ago

Component: undeterminedswresample
Priority: criticalnormal

Is this not reproducible with ffmpeg (the application)?
Did you test current git head or another version?

comment:2 by Oleg, 11 years ago

only
ffmpeg-20140414-git-5e379cd-win32-dev
ffmpeg-20140414-git-5e379cd-win32-shared

comment:3 by Oleg, 11 years ago

ffmpeg-20140412-git-513a431-win32
it also has error

comment:4 by Oleg, 11 years ago

ffmpeg.exe convert and resampling ok
What's the problem?
Why not get on С++ ?

comment:5 by Oleg, 11 years ago

f:\>ffmpeg -i 02.mp3 -ar 8000 -ac 2 out.wav
ffmpeg version N-62439-g5e379cd Copyright (c) 2000-2014 the FFmpeg developers

built on Apr 13 2014 22:01:33 with gcc 4.8.2 (GCC)
configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av

isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
le-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetyp
e --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --ena
ble-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-l
ibopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libsp
eex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aa
cenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavp
ack --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable
-zlib

libavutil 52. 76.100 / 52. 76.100
libavcodec 55. 58.103 / 55. 58.103
libavformat 55. 37.100 / 55. 37.100
libavdevice 55. 13.100 / 55. 13.100
libavfilter 4. 4.100 / 4. 4.100
libswscale 2. 6.100 / 2. 6.100
libswresample 0. 18.100 / 0. 18.100
libpostproc 52. 3.100 / 52. 3.100

[mp3 @ 02c8eca0] Header missing
[mp3 @ 02c8aba0] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from '02.mp3':

Duration: 00:00:39.93, start: 0.000000, bitrate: 128 kb/s

Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s

Output #0, wav, to 'out.wav':

Metadata:

ISFT : Lavf55.37.100
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 8000 Hz, stereo, s16,

256 kb/s

Stream mapping:

Stream #0:0 -> #0:0 (mp3 -> pcm_s16le)

Press [q] to stop, ? for help
[mp3 @ 02c8eca0] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
size= 1249kB time=00:00:39.99 bitrate= 255.8kbits/s
video:0kB audio:1249kB subtitle:0kB other streams:0kB global headers:0kB muxing
overhead: 0.006255%

Version 0, edited 11 years ago by Oleg (next)

comment:6 by Oleg, 11 years ago

ffmpeg -i 02.mp3 -ar 8000 -ac 2 -f s16le out.pcm
convert and resampling ok

comment:7 by Oleg, 11 years ago

I found a bug in my program.

from
fwrite( (short *)buffer, sizeof(short), (size_t)numSamplesOut, fout);
to
fwrite( (short *)buffer, sizeof(short), (size_t)numSamplesOut*frame->channels, fout);

ffmpeg has no errors.
topic can be closed.

comment:8 by Oleg, 11 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.