2014-12-08 62 views
0

目前,我尝试使用av​​codec_decode_audio4(),它总是返回错误的音频解码。音频解码错误使用libavcodec avcodec_decode_audio4

注意:我的总体目标是将多个AMR_NB编码文件合并为一个音频文件,并最终将此最终音频文件与另一个视频文件复合到.mp4容器中。

下面的代码:

if(avformat_open_input(&sound, "/tmp/aud0", NULL, NULL)!=0) {             
     printf("Error in opening input file aud0\n"); 
     exit(1); 
    } else { 
     printf("Opened aud0 into FormatContext\n"); 
    } 

    if(avformat_find_stream_info(sound, NULL)<0){ 
     printf("Error in finding stream info\n"); 
     avformat_close_input(&sound); 
     exit(1); 
    } 

    int aud_stream_idx=-1; 
    for(int count=0; count<sound->nb_streams; count++) { 
     if(sound->streams[count]->codec->codec_type == AVMEDIA_TYPE_AUDIO) { 
     aud_stream_idx = count;   
     printf("Audio stream index %d found\n", count); 
     break; 
     } 
    } 
    if(aud_stream_idx==-1) { 
     printf("Audio stream not found in the aud0 file\n"); 
     avformat_close_input(&sound); 
     exit(1); 
    } 

    AVCodecContext *audioCC = sound->streams[aud_stream_idx]->codec; 
    printf("Audio codec_id=%d, codec_name=%s\n", audioCC->codec_id, audioCC->codec_name); 

    AVCodec *audioC = avcodec_find_decoder(audioCC->codec_id); 
    if (audioC == NULL) {  printf("Couldn't find decoder\n"); 
     avformat_close_input(&sound); 
     exit(1); 
    } else { 
     printf("Found decoder name %s\n", audioCC->codec_name); 
    } 

    if(avcodec_open2(audioCC, audioC, NULL)<0) { 
     printf("Couldn't open decoder\n"); 
     avformat_close_input(&sound); 
     exit(1); 
    } else { 
     printf("Found decoder name %s\n", audioCC->codec_name); 
     printf("Found bitrate %d\n", audioCC->bit_rate); 
     printf("Found sample_rate %d\n", audioCC->sample_rate); 
     printf("Found sample_fmt %d\n", audioCC->sample_fmt); } 

    /* decode until eof */ 
    av_init_packet(&avpkt); 
    avpkt.data=NULL; 
    avpkt.size=0; 
    if(av_read_frame(sound, &avpkt)<0) { 
     printf("Couldn't read encoded audio packet\n"); 
     av_free_packet(&avpkt); 
     avformat_close_input(&sound); 
     exit(1); 
    } else { 
     printf("Read encoded audio packet\n"); 
     printf("avpkt.pts = %d\n", avpkt.pts); 
     printf("avpkt.dts = %d\n", avpkt.dts); 
     printf("avpkt.duration = %d\n", avpkt.duration);  
     printf("avpkt.stream_index = %d\n", avpkt.stream_index); 
     printf("avpkt.data = %x\n", avpkt.data); 
     printf("avpkt.data[0] = %02x\n", avpkt.data[0]); 
     printf("avpkt.data[0]>>>3&0x0f = %02x\n", avpkt.data[0]>>3 & 0x0f); 
    } 

    fprintf(stderr, "avpkt.size=%d\n", avpkt.size); 
    while (avpkt.size > 0) { 
     int got_frame = 0; 

     if (!decoded_frame) { 
      if (!(decoded_frame = avcodec_alloc_frame())) { 
       fprintf(stderr, "out of memory\n"); 
       exit(1); 
      }  } else 
      avcodec_get_frame_defaults(decoded_frame); 


     len = avcodec_decode_audio4(audioCC, decoded_frame, &got_frame, &avpkt); 
     if (len < 0) { 
      fprintf(stderr, "Error %d while decoding\n", len); 
      exit(1); 
     } 

这里是输出我看到(我想多路分解工作正常):

Opened aud0 into FormatContext 
Audio stream index 0 found 
Audio codec_id=73728, codec_name= 
Found decoder name 
Found decoder name 
Found bitrate 5200 
Found sample_rate 8000 
Found sample_fmt 3 
Read encoded audio packet 
avpkt.pts = 0 
avpkt.dts = 0 
avpkt.duration = 160 
avpkt.stream_index = 0 
avpkt.data = 91e00680 
avpkt.data[0] = 04 
avpkt.data[0]>>>3&0x0f = 00 
avpkt.size=13 
[amrnb @ 0x7fce9300bc00] Corrupt bitstream 
Error -1094995529 while decoding 

这个音频是通过在Android设备上录制语音创建。

我也尝试了由QuickTime生成的.m4a,但没有运气。

我觉得我错过了一些关键的步骤(如不初始化一些CodecContext字段,或不正确读入AVPacket)。无论如何,如果有人有任何意见或类似的例子,请让我知道。

回答

0

首先你需要调用av_read_frame,直到你到达EOF。所以你需要在循环中使用av_read_frame。

让我来解释一下解码是如何工作的:
你打开formatcontext,codeccontext和decoder。这是很容易的部分。
在一个循环中你读取数据包形成一个流。所以你需要

while(av_read_frame(sound, &avpkt)) 

在这个循环中你要解码数据包中的数据。但是小心,您需要检查数据包中的数据来自哪个流。所以它总是更好地检查数据包包含了从语音串流

if (avpkt.stream_index == aud_stream_idx) 

现在可以解码和处理音频数据。

〔实施例:

AVFrame * decoded_frame = av_frame_alloc(); 
AVPacket avpkt; 
av_init_packet(&avpkt); 
avpkt.data = NULL; 
avpkt.size = 0; 
while(av_read_frame(sound, &avpkt)) 
{ 
    if (avpkt.stream_index == aud_stream_idx) 
    { 
     int got_frame; 
     int len = avcodec_decode_audio4(audioCC, decoded_frame, &got_frame, &avpkt); 
    } 
} 

这就是所有的魔法:)

PS:找一个流就叫

av_find_best_stream(sound, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0) 

PPS:没有经过测试的代码。只是编解码器在这里窗口:D
PPPS:阅读的例子。 this帮了我很多:)

1

为了帮助别人了解ffmpeg错误代码,这可能会有帮助。从ffmpeg的(error.h从avutil)错误代码: http://ffmpeg.org/doxygen/trunk/error_8h_source.html

原来你指定的值是:

#define AVERROR_INVALIDDATA  FFERRTAG('I','N','D','A') 

的-1094995529变得-0x41444E49,当你看那些信,在ACSII ,0x41 ='A',0x44 ='D',0x4E ='N,0x49 ='I'。由于宏/等东西是相反的,所以ADNI成为INDA,你可以从#define片断看到,是定义为FFERRTAG('I','N','D','A')的AVERROR_INVALIDDATA。

错误代码的其余部分是在该文件中,我已经在这里下面粘贴他们:

#define AVERROR_BSF_NOT_FOUND  FFERRTAG(0xF8,'B','S','F') ///< Bitstream filter not found 
#define AVERROR_BUG    FFERRTAG('B','U','G','!') ///< Internal bug, also see AVERROR_BUG2 
#define AVERROR_BUFFER_TOO_SMALL FFERRTAG('B','U','F','S') ///< Buffer too small 
#define AVERROR_DECODER_NOT_FOUND FFERRTAG(0xF8,'D','E','C') ///< Decoder not found 
#define AVERROR_DEMUXER_NOT_FOUND FFERRTAG(0xF8,'D','E','M') ///< Demuxer not found 
#define AVERROR_ENCODER_NOT_FOUND FFERRTAG(0xF8,'E','N','C') ///< Encoder not found 
#define AVERROR_EOF    FFERRTAG('E','O','F',' ') ///< End of file 
#define AVERROR_EXIT    FFERRTAG('E','X','I','T') ///< Immediate exit was requested; the called function should not be restarted 
#define AVERROR_EXTERNAL   FFERRTAG('E','X','T',' ') ///< Generic error in an external library 
#define AVERROR_FILTER_NOT_FOUND FFERRTAG(0xF8,'F','I','L') ///< Filter not found 
#define AVERROR_INVALIDDATA  FFERRTAG('I','N','D','A') ///< Invalid data found when processing input 
#define AVERROR_MUXER_NOT_FOUND FFERRTAG(0xF8,'M','U','X') ///< Muxer not found 
#define AVERROR_OPTION_NOT_FOUND FFERRTAG(0xF8,'O','P','T') ///< Option not found 
#define AVERROR_PATCHWELCOME  FFERRTAG('P','A','W','E') ///< Not yet implemented in FFmpeg, patches welcome 
#define AVERROR_PROTOCOL_NOT_FOUND FFERRTAG(0xF8,'P','R','O') ///< Protocol not found 
#define AVERROR_STREAM_NOT_FOUND FFERRTAG(0xF8,'S','T','R') ///< Stream not found 
#define AVERROR_BUG2    FFERRTAG('B','U','G',' ') 
#define AVERROR_UNKNOWN   FFERRTAG('U','N','K','N') ///< Unknown error, typically from an external library 
#define AVERROR_EXPERIMENTAL  (-0x2bb2afa8) ///< Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it. 
#define AVERROR_INPUT_CHANGED  (-0x636e6701) ///< Input changed between calls. Reconfiguration is required. (can be OR-ed with AVERROR_OUTPUT_CHANGED) 
#define AVERROR_OUTPUT_CHANGED  (-0x636e6702) ///< Output changed between calls. Reconfiguration is required. (can be OR-ed with AVERROR_INPUT_CHANGED) 
#define AVERROR_HTTP_BAD_REQUEST FFERRTAG(0xF8,'4','0','0') 
#define AVERROR_HTTP_UNAUTHORIZED FFERRTAG(0xF8,'4','0','1') 
#define AVERROR_HTTP_FORBIDDEN  FFERRTAG(0xF8,'4','0','3') 
#define AVERROR_HTTP_NOT_FOUND  FFERRTAG(0xF8,'4','0','4') 
#define AVERROR_HTTP_OTHER_4XX  FFERRTAG(0xF8,'4','X','X') 
#define AVERROR_HTTP_SERVER_ERROR FFERRTAG(0xF8,'5','X','X')