2013-11-24 63 views
0

我試圖解碼OGG,APE,WMA,WV格式的文件 我已經發佈下面的代碼播放OGG/APE/WV/WMA,但我在輸出具有噪音太大如何使用的ffmpeg

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 

,或者任何其他的方法是有解碼這些格式的文件,那麼請讓我知道

感謝

回答