使用avcodec_decode_audio4()解码AAC时出错

使用avcodec_decode_audio4()解码AAC时出错,c,ffmpeg,decode,aac,C,Ffmpeg,Decode,Aac,我试图用FFmpeg本机解码器解码AAC,但遇到错误 SSR is not implemeted. Update your FFmpeg version to newest from Git. If the problem still occurs, it mean that your file has a feature which has not implemented. 函数avcodec_decode_audio4()返回-1163346256。这是因为FFmpeg版本吗?我

我试图用FFmpeg本机解码器解码AAC,但遇到错误

SSR is not implemeted. Update your FFmpeg version to newest from Git. If the      problem still occurs, it mean that your file has a feature which has not implemented.
函数avcodec_decode_audio4()返回-1163346256。这是因为FFmpeg版本吗?我从下载了共享和开发版本。这是最新的吗

以下是源代码:

#include "stdafx.h"
#include "stdio.h"
#include "conio.h"

extern "C" 
{
#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
#endif
#include <libavcodec\avcodec.h>
#include <libavformat/avformat.h>
}

// compatibility with newer API
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
#define av_frame_alloc avcodec_alloc_frame
#define av_frame_free avcodec_free_frame
#endif

#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096


static void audio_decode_example(const char *outfilename, const char *filename);


int main(int argc, char *argv[]) {
    audio_decode_example("D:\\sample.pcm","D:\\sample.m4a");
    getch();
    return 0;
}


/*
 * Audio decoding.
 */
static void audio_decode_example(const char *outfilename, const char *filename)
{
    AVCodec *codec;
    AVFormatContext   *pFormatCtx = NULL;
    AVCodecContext    *pCodecCtxOrig = NULL;
    AVCodecContext * pCodecCtx= NULL;
    int len;
    FILE *f, *outfile;
    uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
    AVPacket avpkt;
    AVFrame *decoded_frame = NULL;


    av_register_all();

    av_init_packet(&avpkt);

    printf("Decode audio file %s to %s\n", filename, outfilename);

    // Open file to get format context
    if(avformat_open_input(&pFormatCtx, filename, NULL, NULL)!=0){
        printf("Couldn't open file");
        return; // Couldn't open file
    }

    // Retrieve stream information
    if(avformat_find_stream_info(pFormatCtx, NULL)<0){
        printf("Couldn't find stream information");
        return; // Couldn't find stream information
    }

    // Dump information about file onto standard error
    av_dump_format(pFormatCtx, 0, filename, 0);

    // Find the first audio stream
    int audioStream = -1;
    int i =0;
    for(i=0; i<pFormatCtx->nb_streams; i++) {
        if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
            audioStream=i;
            break;
        }
    }

    if(audioStream==-1) {
        printf("Didn't find a audio stream");
        return; // Didn't find a audio stream
    }

    // Get a pointer to the codec context for the audio stream
    pCodecCtxOrig=pFormatCtx->streams[audioStream]->codec;

    // Find the decoder for the audio stream
    codec=avcodec_find_decoder(pCodecCtxOrig->codec_id);
    if(codec==NULL) {
        fprintf(stderr, "Codec not found\n");
        return; // Codec not found
    }

    pCodecCtx = avcodec_alloc_context3(codec);
    if (!pCodecCtx) {
        fprintf(stderr, "Could not allocate audio codec context\n");
        return;
    }

    if(avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {
        fprintf(stderr, "Couldn't copy codec context");
        return; // Error copying codec context
    }


    /* open it */
    if (avcodec_open2(pCodecCtx, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        return;
    }

    f = fopen(filename, "rb");
    if (!f) {
        fprintf(stderr, "Could not open %s\n", filename);
        return;
    }
    outfile = fopen(outfilename, "wb");
    if (!outfile) {
        av_free(pCodecCtx);
        return;
    }

    /* decode until eof */
    avpkt.data = inbuf;
    avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);

    while (avpkt.size > 0) {
        int i, ch;
        int got_frame = 0;

        if (!decoded_frame) {
            if (!(decoded_frame = av_frame_alloc())) {
                fprintf(stderr, "Could not allocate audio frame\n");
                return;
            }
        }

        len = avcodec_decode_audio4(pCodecCtx, decoded_frame, &got_frame, &avpkt);
        if (len < 0) {
            fprintf(stderr, "Error while decoding. len = %d \n",len);
            return;
        }
        if (got_frame) {
            /* if a frame has been decoded, output it */
            int data_size = av_get_bytes_per_sample(pCodecCtx->sample_fmt);
            if (data_size < 0) {
                /* This should not occur, checking just for paranoia */
                fprintf(stderr, "Failed to calculate data size\n");
                return;
            }
            for (i=0; i < decoded_frame->nb_samples; i++)
                for (ch=0; ch < pCodecCtx->channels; ch++)
                    fwrite(decoded_frame->data[ch] + data_size*i, 1, data_size, outfile);
        }
        avpkt.size -= len;
        avpkt.data += len;
        avpkt.dts =
        avpkt.pts = AV_NOPTS_VALUE;
        if (avpkt.size < AUDIO_REFILL_THRESH) {
            /* Refill the input buffer, to avoid trying to decode
             * incomplete frames. Instead of this, one could also use
             * a parser, or use a proper container format through
             * libavformat. */
            memmove(inbuf, avpkt.data, avpkt.size);
            avpkt.data = inbuf;
            len = fread(avpkt.data + avpkt.size, 1,
                        AUDIO_INBUF_SIZE - avpkt.size, f);
            if (len > 0)
                avpkt.size += len;
        }
    }

    fclose(outfile);
    fclose(f);

    avcodec_close(pCodecCtx);
    av_free(pCodecCtx);
    av_frame_free(&decoded_frame);
}
#包括“stdafx.h”
#包括“stdio.h”
#包括“conio.h”
外部“C”
{
#ifndef uu STDC u常量u宏
#定义\uu STDC\u常量\u宏
#恩迪夫
#包括
#包括
}
//与较新API的兼容性
#如果LIBAVCODEC_VERSION_INT是的,这是行不通的。你不能将一些随机muxing格式(可能是mp4)中的原始字节转储到解码器中,并期望它能够工作。使用()从muxing格式读取单个音频数据包,然后使用avcodec\u decode\u audio4()将生成的AVPacket馈送到解码器中。请参见例如.I know api example.c使用上述代码,但遗憾的是,这只适用于非常有限的一部分情况。另请参见中的详细说明。

您的音频输入文件使用了解码器尚不支持的功能。我建议打开错误报告并上载文件的示例,以便可以实现()。您从何处获取此文件/数据/流?这里的SSR是指AAC SSR配置文件吗?我使用了AAC LC配置文件编码的文件。我现在无法上载此文件,因为它位于其他计算机中。我还尝试使用mp2(从这里返回-11634656):以及我的计算机中的一些mp3(缺少错误标头)。源代码有什么问题吗?谢谢。我会再试一次。谢谢,我用av_read_frame()替换了fread函数,现在可以使用了。我还有一些问题,如果您能给我一些建议,我将不胜感激。av_read_frame()的输入是AVFormatContext,那么如果我有多个流呢?我如何设置pcm类型(s16le、f32be等)设置为输出文件.pcm。我尝试设置AVCODECONTEXT->sample_fmt,但出现错误。同一文件(AVFormatContext)中的每个流(AVStream)将AVPacket.stream_索引设置为AVFormatContext->streams[]中该AVStream的索引数组。您不能设置pcm_类型,每个本机解码器都有一个输出类型,这就是您得到的。如果您想从本机输出类型转换为其他类型,请使用libswresample进行格式(以及采样器和通道)转换。
f = fopen(filename, "rb");
if (!f) {
    fprintf(stderr, "Could not open %s\n", filename);
    return;
}
outfile = fopen(outfilename, "wb");
if (!outfile) {
    av_free(pCodecCtx);
    return;
}

/* decode until eof */
avpkt.data = inbuf;
avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);

while (avpkt.size > 0) {
    int i, ch;
    int got_frame = 0;