据我所知,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-&gt; frame_size * 2;返回最大为640的2048字节,因此,它根本无法执行编码。如果我删除FOR循环并尝试直接执行编码然后它返回0行out_size = avcodec_encode_audio(c,outbuf,outbuf_size,&amp; in_buffer [frame_byte_size * i]);
也许有人面对这样的任务,可以帮助我或知道一些指向exmaples的链接?
感谢。