1

私が知っている限りでは、iOS 4.3 は AudioQueue 出力で再生するための AMR フレームをサポートしていません。RTSP サーバーから AVPacket インスタンスを AMR フレームとして受け取ります。AMR を raw バッファーにデコードし、それを AAC にエンコードする 2 つの方法を作成しようとしています。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、長さ = 16) を確認できます。それが正しいかどうかはわかりません。今エンコーディング。この文字列 frame_byte_size=c->frame_size * 2; 640 よりも大きい 2048 バイトを返すため、エンコードをまったく実行できません。FOR ループを削除して直接エンコードを実行しようとすると、0 が返されます。

たぶん、誰かがそのようなタスクに直面していて、私を助けたり、例へのリンクを知ったりできますか?

ありがとう。

4

0 に答える 0