使用libavcodec avcodec_decode_audio4进行音频解码错误

时间:2014-12-08 05:37:43

标签: audio-recording 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);
        }

这是我看到的输出(我认为demuxing工作正常):

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)。在任何情况下,如果有人有任何意见或类似的例子,请告诉我。

2 个答案:

答案 0 :(得分:2)

为了帮助其他人了解ffmpeg错误代码,这可能会有所帮助。来自ffmpeg的错误代码(来自avutil的error.h): 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片段中看到,是AVERROR_INVALIDDATA定义的FFERRTAG('I','N','D','A')。

其余的错误代码都在该文件中,我在下面粘贴了它们:

#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')

答案 1 :(得分:0)

首先你需要调用av_read_frame,直到你进入EOF。所以你需要循环中的av_read_frame。

让我解释解码的工作原理:
你打开formatcontext,codeccontext和解码器。这是容易的部分。
在循环中,您从一个流中读取数据包。所以你需要

while(av_read_frame(sound, &avpkt))

在此循环中,您希望解码数据包中的数据。但请注意,您需要检查数据包中的数据来自哪个流。因此,最好检查数据包是否包含来自audiostream的数据

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给了我很多帮助:)。