【问题标题】:audio decoding error using libavcodec avcodec_decode_audio4使用 libavcodec avcodec_decode_audio4 的音频解码错误
【发布时间】:2016-02-02 20:57:09
【问题描述】:

目前,我第一次尝试使用 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)。无论如何,如果有人有任何意见或类似的例子,请告诉我。

【问题讨论】:

    标签: audio-recording libavcodec


    【解决方案1】:

    为了帮助其他人理解 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'。由于macro/etc的事情是相反的,所以ADNI变成了INDA,从#define sn-p可以看出,是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')
    

    【讨论】:

      【解决方案2】:

      首先你需要调用 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 帮了我很多:)

      【讨论】:

        猜你喜欢
        • 2012-11-15
        • 2012-04-05
        • 2015-10-28
        • 2011-01-25
        • 2015-03-08
        • 2014-10-07
        • 2019-01-21
        • 2014-09-21
        • 2012-01-04
        相关资源
        最近更新 更多