我正在尝试解码ogg,猿,wma,wv文件格式 我已经发布了下面的代码,但我的输出噪音太大了
av_init_packet(&packet);
fmt_ctx = avformat_alloc_context();
if ((ret = avformat_open_input(&fmt_ctx, szfile, NULL, NULL)) < 0)
{
LOGE("Cannot open input file\n");
}
if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0)
{
LOGE("Cannot find stream information\n");
}
/* select the audio stream */
ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0);
if (ret < 0)
{
LOGE("Cannot find a audio stream in the input file\n");
}
audio_stream_index = ret;
dec_ctx = fmt_ctx->streams[audio_stream_index]->codec;
LOGE(" ogg code %d codec id%d\n",AV_CODEC_ID_VORBIS,dec_ctx->codec_id);
LOGE("avcodec_find_decoder\n");
dec = avcodec_find_decoder(dec_ctx->codec_id);
if (!dec) {
__android_log_print(ANDROID_LOG_INFO, "BroovPlayer", "avcodec_find_decoder failed %d Name:%s\n", dec_ctx->codec_id, dec_ctx->codec_name);
}
if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0)
{
LOGE("Cannot open audio decoder\n");
}
//dec_ctx->sample_fmt = AV_SAMPLE_FMT_S16P;
LOGS("Stage 5 sample fmt %d",dec_ctx->sample_fmt);
LOGE("Stage 5");
LOGD("........%d", packet.size);
while (1)
{
if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
{
//LOGE("Stage........... %d",ret);
break;
}
if (packet.stream_index == audio_stream_index)
{
avcodec_get_frame_defaults(frame);
got_frame = 0;
// LOGE("file size=%d packet_index=%d",packet.size,packet.dts);
ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, &packet);
// LOGE("len=%d",ret);
if (ret < 0)
{
LOGE("Error decoding audio\n");
continue;
}
if (!got_frame) {
/* stop sending empty packets if the decoder is finished */
if (!packet.data && dec->capabilities & CODEC_CAP_DELAY)
//flush_complete = 1;
continue;
}
if (got_frame)
{
// LOGE("begin frame decode\n");
int data_size = av_samples_get_buffer_size(NULL, dec_ctx->channels,frame->nb_samples,dec_ctx->sample_fmt, 1);
// LOGE("after frame decode\n");
jbyte *bytes = (*env)->GetByteArrayElements(env, array, NULL);
memcpy(bytes, frame->data[0], data_size); //
(*env)->ReleaseByteArrayElements(env, array, bytes, 0);
(*env)->CallVoidMethod(env, obj,play, array, data_size);
}
packet.size -= ret;
packet.data += ret;
packet.pts = AV_NOPTS_VALUE;
}
}
av_free_packet(&packet);
when ,i am playing ogg/ape/wv audio file format .
please help me to minimize the noise, as less as possible
或者如果有任何其他方法来解码这些文件格式,请告诉我
谢谢
答案 0 :(得分:0)
您需要为文件格式配置解码器。阅读文档如何配置它。您可以开始的好示例:http://www.roman10.net/how-to-build-ffmpeg-with-ndk-r9/,https://github.com/appunite/AndroidFFmpeg