Hi All,

Please help me in playing mp3 song.By using ffmpeg I am able to play wav
file.The same code I used to play mp3 file then I am getting following
error which is mentioned in log.txt.

Here I'm attaching the following files:

1.Code I used to play mp3 song.(i.e music.c)
2.The error I got while playing mp3.(i.e log.txt)

the error is while decoding the length obtained  is negative.

Thanks & Regards,
Vinay Yamusani.
10-31 16:36:54.370: D/tag(23670): RAH28 Starting
10-31 16:36:54.512: D/tag(23670): RAH getting stream info
10-31 16:36:55.057: D/tag(23670): RAH getting audio stream
10-31 16:36:55.057: D/tag(23670): RAH audio stream found with result: [0]
10-31 16:36:55.057: D/tag(23670): RAH audio codec info loaded
10-31 16:36:55.057: D/tag(23670): RAH audio codec info [86017]
10-31 16:36:55.057: D/tag(23670): RAH audio codec info found
10-31 16:36:55.057: D/tag(23670): RAH audio codec loaded [6] [0]
10-31 16:36:55.057: D/tag(23670): RAH channels [2] sample rate [44100] sample 
format [6]
10-31 16:36:55.057: D/tag(23670): RAH frame read: [0] [0]
10-31 16:36:55.057: D/tag(23670): RAH audio ready
10-31 16:36:55.057: D/tag(23670): RAH packet size: [90828]
10-31 16:36:55.057: D/tag(23670): RAH decoding: [1] [0]
10-31 16:36:55.057: D/tag(23670): RAH 1 size [90828] len [-1094995529] 
data_size [0] out_size [0]
10-31 16:36:55.062: A/libc(23670): Fatal signal 11 (SIGSEGV) at 0x5c0b5000 
(code=1), thread 23700 (Thread-3695)
#include <jni.h>

#include <assert.h>
#include <jni.h>
#include <string.h>
#include <android/log.h>
#include<errno.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#define AVCODEC_MAX_AUDIO_FRAME_SIZE 19200
#define LOGI(...)  __android_log_print(ANDROID_LOG_DEBUG,"tag", __VA_ARGS__)

#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096

char *DEBUG_TAG = "hello";

JNIEXPORT void JNICALL Java_com_example_ffmeg_MainActivity_createEngine(
		JNIEnv *env, jclass clazz) {

	__android_log_print(ANDROID_LOG_DEBUG, "TAG", "create engine");
//	avcodec_init();
	av_register_all();

}

JNIEXPORT void JNICALL Java_com_example_ffmeg_MainActivity_loadFile(JNIEnv* env, jobject obj, jstring file, jbyteArray array)
{
	jboolean isCopy;
	int i;
	int audioStream = -1;
	int res;
	int decoded = 0;
	int out_size;
	AVFormatContext *pFormatCtx =NULL;
	AVCodecContext *aCodecCtx;
	AVCodecContext *c = NULL;
	AVCodec *aCodec;
	AVPacket packet;
	jclass cls = (*env)->GetObjectClass(env, obj);
	jmethodID play = (*env)->GetMethodID(env, cls, "playSound", "([BI)V");

	const char * szfile = (*env)->GetStringUTFChars(env, file, &isCopy);
	int16_t * pAudioBuffer = (int16_t *) av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE * 2 + FF_INPUT_BUFFER_PADDING_SIZE);
	int16_t * outBuffer    = (int16_t *) av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE * 2 + FF_INPUT_BUFFER_PADDING_SIZE);

	LOGI("RAH28 Starting");

	res = avformat_open_input(&pFormatCtx, szfile, NULL,NULL);
	if (res != 0)
	{
		LOGI("RAH opening input failed with result: [%d]", res);
	}

	LOGI("RAH getting stream info");

	res = av_find_stream_info(pFormatCtx);

	if (res < 0)
	{
		LOGI("RAH getting stream info failed with result: [%d]", res);
	}

	LOGI("RAH getting audio stream");

	for (i = 0; i < pFormatCtx->nb_streams; i++)
	{
		if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO&& audioStream < 0)
		{
			audioStream = i;
		}
	}

	if (audioStream == -1)
	{
		LOGI("RAH couldn't find audio stream");
	}

	LOGI("RAH audio stream found with result: [%d]", res);

	aCodecCtx = pFormatCtx->streams[audioStream]->codec;

	LOGI("RAH audio codec info loaded");
	LOGI("RAH audio codec info [%d]", aCodecCtx->codec_id);

	aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
	if (!aCodec)
	{
		LOGI("RAH audio codec unsupported");
	}

	LOGI("RAH audio codec info found");

	res = avcodec_open2(aCodecCtx, aCodec,NULL);

	LOGI("RAH audio codec loaded [%d] [%d]", aCodecCtx->sample_fmt, res);

//	c=avcodec_alloc_context3();
	av_init_packet(&packet);

	LOGI("RAH channels [%d] sample rate [%d] sample format [%d]",aCodecCtx->channels, aCodecCtx->sample_rate, aCodecCtx->sample_fmt);

	int x, y;
	x = 0;
	y = 0;
	while (av_read_frame(pFormatCtx, &packet) >= 0)
	{
		LOGI("RAH frame read: [%d] [%d]", x++, y);

		if (aCodecCtx->codec_type == AVMEDIA_TYPE_AUDIO)
		{
			LOGI("RAH audio ready");
			int data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE
					* 2+FF_INPUT_BUFFER_PADDING_SIZE;
			int size = packet.size;
			y = 0;
			decoded = 0;
			LOGI("RAH packet size: [%d]", size);
			while (size > 0)
			{
				LOGI("RAH decoding: [%d] [%d]", x, y++);

				int len = avcodec_decode_audio3(aCodecCtx, pAudioBuffer,
						&data_size, &packet);

				LOGI("RAH 1 size [%d] len [%d] data_size [%d] out_size [%d]",size, len, data_size, out_size);

				jbyte *bytes = (*env)->GetByteArrayElements(env, array, NULL);

//                                memmove(bytes + decoded, (jbyte *) (pAudioBuffer), 192000);
				memcpy(bytes + decoded, pAudioBuffer, len); //

				LOGI("RAH 2");

				(*env)->ReleaseByteArrayElements(env, array, bytes, 0);
				LOGI("RAH 3");

				(*env)->CallVoidMethod(env, obj, play, array, len);
				LOGI("RAH 4");

				size -= len;
				decoded += len;

			}
			av_free_packet(&packet);
		}

	}

	// Close the video file
	av_close_input_file(pFormatCtx);

	//__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH Finished Running result: [%d]", res);
	(*env)->ReleaseStringUTFChars(env, file, szfile);

}
_______________________________________________
Libav-user mailing list
[email protected]
http://ffmpeg.org/mailman/listinfo/libav-user

Reply via email to