1

据我所知,iOS 4.3 不支持通过 AudioQueue 输出播放 AMR 帧。我从 RTSP 服务器接收 AVPacket 实例作为 AMR 帧。我正在尝试使用两种方法将 AMR 解码为原始缓冲区,然后将其编码为 AAC。我找到了使用 LIBAV 执行此操作的示例代码,但遇到了问题。有我的方法:

- (void) decodeAudioPacketToRaw:(AVPacket) inPacket
{
    AVCodec *codec;
    AVCodecContext *c= NULL;

    int out_size, size, len;
    uint8_t *outbuf;    

    codec = avcodec_find_decoder(CODEC_ID_AMR_NB);
    if (!codec) {
        fprintf(stderr, "input codec not found\n");
        return;
    }

    c= avcodec_alloc_context();

    if (avcodec_open(c, codec) < 0) {
        fprintf(stderr, "could not open input codec\n");
        return;
    }

    outbuf = (uint8_t*) malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);

    size = inPacket.size;

    NSLog(@"Input packet size is %i", size);

    while (size > 0) {
        len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &inPacket);
        if (len < 0) {
           fprintf(stderr, "Error while decoding\n");
           return;
        }
        NSLog(@"Decoded len in %i", len);
        NSLog(@"Decoded out size in %i", out_size);        
        if (out_size > 0) {
            [self encodeAudioBuffer:(const short *)outbuf withSize:out_size];
        }
        size -= len;
        inPacket.data += len;
    }    
}

并编码:

- (void) encodeAudioBuffer:(const short *) in_buffer withSize:(unsigned int) in_buf_byte_size
{
    AVCodec * codec;
    AVCodecContext * c= NULL;
    int count, out_size, outbuf_size, frame_byte_size;
    uint8_t * outbuf;    

    printf("Audio encoding\n");

    codec = avcodec_find_encoder(CODEC_ID_AAC);
    if (!codec) {
       fprintf(stderr, "output codec not found\n");
       return;
    }

    c= avcodec_alloc_context();

    c->bit_rate = 32000;
    c->sample_rate = 12000;
    c->channels = 1;

    if (avcodec_open(c, codec) < 0) {
        fprintf(stderr, "could not open output codec\n");
        return;
    }

    frame_byte_size=c->frame_size * 2; // NOT SURE
    count = in_buf_byte_size / frame_byte_size;

    fprintf(stderr, "Number of frames: %d\n", count);    

    outbuf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
    outbuf = (uint8_t*) malloc(outbuf_size);

    for(int i = 0; i < count; i++) {
        out_size = avcodec_encode_audio(c, outbuf, outbuf_size, &in_buffer[frame_byte_size*i]);
        // DO SOMETHING WITH BUFFER        fwrite(outbuf, 1, out_size, f);
        fprintf(stderr, "bits_frame: %d\n", c->frame_bits);
    }
    free(outbuf);

    avcodec_close(c);
    av_free(c);


}

事实上,它解码输入数据包,我可以看到原始缓冲区及其大小(输入大小 = 64,输出 = 640,len = 16)。我不确定它是否正确。现在编码。这个字符串frame_byte_size=c->frame_size * 2; 返回大于 640 的 2048 字节,因此,它根本无法执行编码。如果我删除 FOR 循环并尝试直接执行编码,则它在 out_size = avcodec_encode_audio(c, outbuf, outbuf_size, &in_buffer[frame_byte_size*i]); 行中返回 0

也许有人面临这样的任务并且可以帮助我或知道一些指向示例的链接?

谢谢。

4

0 回答 0