[FFmpeg-devel] Decoding Error
Yamusani Vinay
yamusanivinay at gmail.com
Fri Oct 25 18:24:29 CEST 2013
Hi All,
I am developing an android application where i need to play mp3
song.So I ported ffmpeg to android and I enabled all decoders in config
file.So I tried with wav file it is playing fine but if i play mp3 song
then i'm getting negative length i.e(output of avcodec_decode_audio3).I
used the below code.
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"); //At
the begining of your main function
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);
__android_log_print(ANDROID_LOG_INFO, DEBUG_TAG, "RAH28 Starting");
res = avformat_open_input(&pFormatCtx, szfile, NULL,NULL);
if (res != 0) {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"RAH opening input failed with result: [%d]", res);
}
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"RAH getting stream info");
res = av_find_stream_info(pFormatCtx);
if (res < 0) {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"RAH getting stream info failed with result: [%d]", res);
}
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"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) {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"RAH couldn't find audio stream");
}
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"RAH audio stream found with result: [%d]", res);
aCodecCtx = pFormatCtx->streams[audioStream]->codec;
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"RAH audio codec info loaded");
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"RAH audio codec info [%d]", aCodecCtx->codec_id);
aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
if (!aCodec) {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"RAH audio codec unsupported");
}
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"RAH audio codec info found");
res = avcodec_open2(aCodecCtx, aCodec,NULL);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"RAH audio codec loaded [%d] [%d]", aCodecCtx->sample_fmt, res);
// c=avcodec_alloc_context3();
av_init_packet(&packet);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"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) {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"RAH frame read: [%d] [%d]", x++, y);
if (aCodecCtx->codec_type == AVMEDIA_TYPE_AUDIO) {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"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;
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"RAH packet size: [%d]", size);
while (size > 0) {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"RAH decoding: [%d] [%d]", x, y++);
int len = avcodec_decode_audio3(aCodecCtx, pAudioBuffer,
&data_size, &packet);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
"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); //
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH 2");
(*env)->ReleaseByteArrayElements(env, array, bytes, 0);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "RAH 3");
(*env)->CallVoidMethod(env, obj, play, array, len);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "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);
Thanks & Regards
bitfield
More information about the ffmpeg-devel
mailing list