2015-11-12 23 views
0

我用FFmpeg解碼我flac文件,並將其寫入pcm文件,然後使用GoldenWave與pcm signed 16bit, little endian, mono和總播放時間是確定的播放。解碼FLAC文件無法正常工作

我懷疑我在一個地方寫了2頻道文件,但我不知道如何獲得每個信號通道並將其寫入pcm文件。

有幫助嗎?謝謝。

while (av_read_frame(fmt_ctx, &pkt) >= 0) { 
      AVPacket orig_pkt = pkt; 
      do { 
       ret = decode_packet(&got_frame, 0); 
       if (ret < 0) 
        break; 
       pkt.data += ret; 
       pkt.size -= ret; 
      } while (pkt.size > 0); 
      av_free_packet(&orig_pkt); 
     } 

     pkt.data = NULL; 
     pkt.size = 0; 
     do { 
      decode_packet(&got_frame, 1); 
      LOG("flush cached frames"); 
     } while (got_frame); 



static int decode_packet(int *got_frame, int cached) 
{ 
    int ret = 0; 
    int decoded = pkt.size; 

    *got_frame = 0; 

    if (pkt.stream_index == audio_stream_idx) { 
     ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, &pkt); 
     if (ret < 0) { 
      LOG("Error decoding audio frame (%s)\n", av_err2str(ret)); 
      return ret; 
     } 

     decoded = FFMIN(ret, pkt.size); 

     if (*got_frame) { 
      size_t unpadded_linesize = frame->nb_samples * av_get_bytes_per_sample(audio_dec_ctx->sample_fmt); 
      //decode packet nb_samples:4608, xx:2, unpadded_linesize: 9216 
      LOG("decode packet nb_samples:%d, xx:%d, unpadded_linesize: %d", 
        frame->nb_samples, av_get_bytes_per_sample(audio_dec_ctx->sample_fmt), unpadded_linesize); 
      fwrite(frame->extended_data[0], 1, unpadded_linesize, audio_dst_file); 

      //int nb_sample = frame->nb_samples; 

      //fwrite(frame->extended_data[0], 1, nb_sample, audio_dst_file); 
      //fwrite(frame->extended_data[0] + nb_sample, 1, nb_sample, audio_dst_file); 

     } 
    } 

    if (*got_frame && api_mode == API_MODE_NEW_API_REF_COUNT) 
     av_frame_unref(frame); 

    return decoded; 
} 

回答

1

您沒有說明您所遇到的問題,但你在寫什麼,我看到了兩個問題:

  1. 你不檢查幀的原始音頻格式,請參閱frame->format(或audio_dec_ctx->sample_fmt)。你寫它,就好像它是AV_SAMPLE_FMT_S16,但你不檢查,這是
  2. 你unpadded_linesize不信道的數量乘以(見例如幀 - >channels
+0

事實上,這是demuxering_decodeing.c中的一段FFmpeg示例代碼。並修改'got_packet'部分用'frame-> linesize [0]替換unpadded_linesize'那麼它就行了。謝謝 – chaoqi