iPhone ffmpeg dev using libav to decode from AMR to ACC codec
It seems to be that, the AMR support of AudioQueue has been disappeared since iOS 4.3 was released. I can't use audio frame received from RSTP server with old way:
audioFormat.mFormatID = kAudioFormatAMR;
int err = AudioQueueNewOutput(&audioFormat, MyAudioQueueOutputCallback, self, NULL, kCFRunLoopCommonModes, 0, &audioQueue);
As a result I received an error in last string.
Maybe someone know how to decode AMR AVPacket into raw buffer and encode it with AAC or MP3 using LIBAV?
I've tried to use
avcodec_decode_audio3
It works and I can get raw buffer but when I'm trying to encode it with
avcodec_encode_audio
I get 0 as result
This is my method to encode buffer:
- (AVPacket) encodeRawFrame:(const short *) in_buffer withSize:(unsigned int) in_buf_byte_size
{
AVPacket res;
AVCodec *codec;
AVCodecContext *c= NULL;
int count, out_size, outbuf_size, frame_byte_size;
uint8_t *outbuf;
avcodec_init();
avcodec_register_all();
printf("Audio encoding\n");
codec = avcodec_find_encoder(CODEC_ID_AAC);
if (!codec) {
fprintf(stderr, "codec not found\n");
return res;
}
c= avcodec_alloc_context();
c->bit_rate = 64000;
c->sample_rate = 24000;
c->channels = 2;
if (avcodec_open(c, codec) < 0)
{
fprintf(stderr, "could not open codec\n");
}
else
{
frame_byte_size=c->frame_size*2*2;
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);
out_size = avcodec_encode_audio(c, outbuf, outbuf_size, &in_buffer[frame_byte_size*i]);
if(out_size >= 0)
开发者_如何学Go {
res.size = outbuf_size;
res.data = malloc(outbuf_size);
}
free(outbuf);
}
avcodec_close(c);
av_free(c);
return res;
}
After encoding "out_size" is always 0 and result buffer is empty.
Thanks.
So, I've found all solutions and now my media player class can play RTSP audio and video. I understood that using decoding from AMR to AAC isn't a good idea because of resources usage and it would better to play RAW buffer which you can get using decoding means which allow get RAW buffer from AMR. I have a good sample (actually, I have to make small refactoring :) ) how to play RTSP streams and I'm ready to share it with ppl if they would mind providing emails :). Also, I have universal binary (armv6, armv7, i386) of LIBAV, AMR_NB and AAC encoder.
Thanks.
精彩评论