static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { FDKAACDecContext *s = avctx->priv_data; AVFrame *frame = data; int ret; AAC_DECODER_ERROR err; UINT valid = avpkt->size; err = aacDecoder_Fill(s->handle, &avpkt->data, &avpkt->size, &valid); if (err != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "aacDecoder_Fill() failed: %x\n", err); return AVERROR_INVALIDDATA; } err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) s->decoder_buffer, s->decoder_buffer_size / sizeof(INT_PCM), 0); if (err == AAC_DEC_NOT_ENOUGH_BITS) { ret = avpkt->size - valid; goto end; } if (err != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "aacDecoder_DecodeFrame() failed: %x\n", err); ret = AVERROR_UNKNOWN; goto end; } if ((ret = get_stream_info(avctx)) < 0) goto end; frame->nb_samples = avctx->frame_size; if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) goto end; memcpy(frame->extended_data[0], s->decoder_buffer, avctx->channels * avctx->frame_size * av_get_bytes_per_sample(avctx->sample_fmt)); *got_frame_ptr = 1; ret = avpkt->size - valid; end: return ret; }
int fdk_aac_decode_frame(AAC_Dec codec, INT_PCM *output_buf, int *output_size, int *got_frame) { AAC_DECODER_ERROR err; int ret; UINT valid = codec->in_buffer_size; err = aacDecoder_Fill(codec->handle, codec->in_buffer, &codec->in_buffer_size, &valid); if(err != AAC_DEC_OK){ LOGI("aacDecoder_Fill() failed: 0x%x", err); return err; } if(codec->initialized){ } err = aacDecoder_DecodeFrame(codec->handle, output_buf, codec->pcm_pkt_size, codec->flags); if (err == AAC_DEC_NOT_ENOUGH_BITS) { ret = codec->in_buffer_size - valid; goto end; } if(err != AAC_DEC_OK){ LOGI("aacDecoder_DecodeFrame() failed: 0x%x", err); ret = AAC_DEC_DECODE_FRAME_ERROR; goto end; } if(!codec->initialized) { ret = get_stream_info(codec); if(ret != 0) { LOGI("get_stream_info failed"); goto end; } codec->initialized = 1; } *got_frame = 1; *output_size = codec->pcm_pkt_size; return (codec->in_buffer_size - valid); end: return ret; }
void *io_thread_a2dp_sink_aac(void *arg) { struct ba_transport *t = (struct ba_transport *)arg; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); pthread_cleanup_push(CANCEL_ROUTINE(io_thread_release), t); if (t->bt_fd == -1) { error("Invalid BT socket: %d", t->bt_fd); goto fail_open; } if (t->mtu_read <= 0) { error("Invalid reading MTU: %zu", t->mtu_read); goto fail_open; } HANDLE_AACDECODER handle; AAC_DECODER_ERROR err; if ((handle = aacDecoder_Open(TT_MP4_LATM_MCP1, 1)) == NULL) { error("Couldn't open AAC decoder"); goto fail_open; } pthread_cleanup_push(CANCEL_ROUTINE(aacDecoder_Close), handle); const unsigned int channels = transport_get_channels(t); #ifdef AACDECODER_LIB_VL0 if ((err = aacDecoder_SetParam(handle, AAC_PCM_MIN_OUTPUT_CHANNELS, channels)) != AAC_DEC_OK) { error("Couldn't set min output channels: %s", aacdec_strerror(err)); goto fail_init; } if ((err = aacDecoder_SetParam(handle, AAC_PCM_MAX_OUTPUT_CHANNELS, channels)) != AAC_DEC_OK) { error("Couldn't set max output channels: %s", aacdec_strerror(err)); goto fail_init; } #else if ((err = aacDecoder_SetParam(handle, AAC_PCM_OUTPUT_CHANNELS, channels)) != AAC_DEC_OK) { error("Couldn't set output channels: %s", aacdec_strerror(err)); goto fail_init; } #endif const size_t in_buffer_size = t->mtu_read; const size_t out_buffer_size = 2048 * channels * sizeof(INT_PCM); uint8_t *in_buffer = malloc(in_buffer_size); int16_t *out_buffer = malloc(out_buffer_size); pthread_cleanup_push(CANCEL_ROUTINE(free), in_buffer); pthread_cleanup_push(CANCEL_ROUTINE(free), out_buffer); if (in_buffer == NULL || out_buffer == NULL) { error("Couldn't create data buffers: %s", strerror(ENOMEM)); goto fail; } struct pollfd pfds[] = { { t->event_fd, POLLIN, 0 }, { -1, POLLIN, 0 }, }; debug("Starting IO loop: %s", bluetooth_profile_to_string(t->profile, t->codec)); for (;;) { pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); CStreamInfo *aacinf; ssize_t len; /* add BT socket to the poll if transport is active */ pfds[1].fd = t->state == TRANSPORT_ACTIVE ? t->bt_fd : -1; if (poll(pfds, sizeof(pfds) / sizeof(*pfds), -1) == -1) { error("Transport poll error: %s", strerror(errno)); goto fail; } if (pfds[0].revents & POLLIN) { /* dispatch incoming event */ eventfd_t event; eventfd_read(pfds[0].fd, &event); continue; } if ((len = read(pfds[1].fd, in_buffer, in_buffer_size)) == -1) { debug("BT read error: %s", strerror(errno)); continue; } pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); /* it seems that zero is never returned... */ if (len == 0) { debug("BT socket has been closed: %d", pfds[1].fd); /* Prevent sending the release request to the BlueZ. If the socket has * been closed, it means that BlueZ has already closed the connection. */ close(pfds[1].fd); t->bt_fd = -1; goto fail; } if (io_thread_open_pcm_write(&t->a2dp.pcm) == -1) { if (errno != ENXIO) error("Couldn't open FIFO: %s", strerror(errno)); continue; } const rtp_header_t *rtp_header = (rtp_header_t *)in_buffer; uint8_t *rtp_latm = (uint8_t *)&rtp_header->csrc[rtp_header->cc]; size_t rtp_latm_len = len - ((void *)rtp_latm - (void *)rtp_header); if (rtp_header->paytype != 96) { warn("Unsupported RTP payload type: %u", rtp_header->paytype); continue; } unsigned int data_len = rtp_latm_len; unsigned int valid = rtp_latm_len; if ((err = aacDecoder_Fill(handle, &rtp_latm, &data_len, &valid)) != AAC_DEC_OK) error("AAC buffer fill error: %s", aacdec_strerror(err)); else if ((err = aacDecoder_DecodeFrame(handle, out_buffer, out_buffer_size, 0)) != AAC_DEC_OK) error("AAC decode frame error: %s", aacdec_strerror(err)); else if ((aacinf = aacDecoder_GetStreamInfo(handle)) == NULL) error("Couldn't get AAC stream info"); else { const size_t size = aacinf->frameSize * aacinf->numChannels; if (io_thread_write_pcm(&t->a2dp.pcm, out_buffer, size) == -1) error("FIFO write error: %s", strerror(errno)); } } fail: pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); pthread_cleanup_pop(1); pthread_cleanup_pop(1); fail_init: pthread_cleanup_pop(1); fail_open: pthread_cleanup_pop(1); return NULL; }
int main(int argc, char *argv[]) { const char *infile, *outfile; AVFormatContext *in = NULL; AVStream *st = NULL; void *wav = NULL; int output_size, ret, i; uint8_t *output_buf; int16_t *decode_buf; HANDLE_AACDECODER handle; AAC_DECODER_ERROR err; int frame_size = 0; if (argc < 3) { fprintf(stderr, "%s in.m4a out.wav\n", argv[0]); return 1; } infile = argv[1]; outfile = argv[2]; av_register_all(); avformat_network_init(); ret = avformat_open_input(&in, infile, NULL, NULL); if (ret < 0) { char buf[100]; av_strerror(ret, buf, sizeof(buf)); fprintf(stderr, "%s: %s\n", infile, buf); return 1; } for (i = 0; i < in->nb_streams && !st; i++) { if (in->streams[i]->codec->codec_id == AV_CODEC_ID_AAC) st = in->streams[i]; } if (!st) { fprintf(stderr, "No AAC stream found\n"); return 1; } if (!st->codec->extradata_size) { fprintf(stderr, "No AAC ASC found\n"); return 1; } handle = aacDecoder_Open(TT_MP4_RAW, 1); err = aacDecoder_ConfigRaw(handle, &st->codec->extradata, &st->codec->extradata_size); if (err != AAC_DEC_OK) { fprintf(stderr, "Unable to decode the ASC\n"); return 1; } output_size = 8*2*1024; output_buf = (uint8_t*) malloc(output_size); decode_buf = (int16_t*) malloc(output_size); while (1) { int i; UINT valid; AVPacket pkt = { 0 }; int ret = av_read_frame(in, &pkt); if (ret < 0) { if (ret == AVERROR(EAGAIN)) continue; break; } if (pkt.stream_index != st->index) { av_free_packet(&pkt); continue; } valid = pkt.size; err = aacDecoder_Fill(handle, &pkt.data, &pkt.size, &valid); if (err != AAC_DEC_OK) { fprintf(stderr, "Fill failed: %x\n", err); break; } err = aacDecoder_DecodeFrame(handle, decode_buf, output_size, 0); av_free_packet(&pkt); if (err == AAC_DEC_NOT_ENOUGH_BITS) continue; if (err != AAC_DEC_OK) { fprintf(stderr, "Decode failed: %x\n", err); continue; } if (!wav) { CStreamInfo *info = aacDecoder_GetStreamInfo(handle); if (!info || info->sampleRate <= 0) { fprintf(stderr, "No stream info\n"); break; } frame_size = info->frameSize * info->numChannels; // Note, this probably doesn't return channels > 2 in the right order for wav wav = wav_write_open(outfile, info->sampleRate, 16, info->numChannels); if (!wav) { perror(outfile); break; } } for (i = 0; i < frame_size; i++) { uint8_t* out = &output_buf[2*i]; out[0] = decode_buf[i] & 0xff; out[1] = decode_buf[i] >> 8; } wav_write_data(wav, output_buf, 2*frame_size); } free(output_buf); free(decode_buf); avformat_close_input(&in); if (wav) wav_write_close(wav); aacDecoder_Close(handle); return 0; }
static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { FDKAACDecContext *s = avctx->priv_data; AVFrame *frame = data; int ret; AAC_DECODER_ERROR err; UINT valid = avpkt->size; uint8_t *buf, *tmpptr = NULL; int buf_size; err = aacDecoder_Fill(s->handle, &avpkt->data, &avpkt->size, &valid); if (err != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "aacDecoder_Fill() failed: %x\n", err); return AVERROR_INVALIDDATA; } if (s->initialized) { frame->nb_samples = avctx->frame_size; if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) return ret; buf = frame->extended_data[0]; buf_size = avctx->channels * frame->nb_samples * av_get_bytes_per_sample(avctx->sample_fmt); } else { buf_size = 50 * 1024; buf = tmpptr = av_malloc(buf_size); if (!buf) return AVERROR(ENOMEM); } err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) buf, buf_size, 0); if (err == AAC_DEC_NOT_ENOUGH_BITS) { ret = avpkt->size - valid; goto end; } if (err != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "aacDecoder_DecodeFrame() failed: %x\n", err); ret = AVERROR_UNKNOWN; goto end; } if (!s->initialized) { if ((ret = get_stream_info(avctx)) < 0) goto end; s->initialized = 1; frame->nb_samples = avctx->frame_size; } if (tmpptr) { frame->nb_samples = avctx->frame_size; if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) goto end; memcpy(frame->extended_data[0], tmpptr, avctx->channels * avctx->frame_size * av_get_bytes_per_sample(avctx->sample_fmt)); } *got_frame_ptr = 1; ret = avpkt->size - valid; end: av_free(tmpptr); return ret; }
static int aac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { AACContext *s = avctx->priv_data; int ret; AAC_DECODER_ERROR err; UINT valid = avpkt->size; uint8_t *buf, *tmpptr = NULL; int buf_size; err = aacDecoder_Fill(s->handle, &avpkt->data, &avpkt->size, &valid); if (err != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "Fill failed: %x\n", err); return AVERROR_INVALIDDATA; } if (avctx->channels) { s->frame.nb_samples = !s->initialized ? 2048 : avctx->frame_size; if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } buf = s->frame.data[0]; buf_size = 2*avctx->channels*s->frame.nb_samples; } else { buf_size = 50*1024; buf = tmpptr = av_malloc(buf_size); if (!buf) return AVERROR(ENOMEM); } err = aacDecoder_DecodeFrame(s->handle, (INT_PCM*) buf, buf_size, 0); if (err == AAC_DEC_NOT_ENOUGH_BITS) { av_free(tmpptr); return avpkt->size - valid; } if (err != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "Decode failed: %x\n", err); av_free(tmpptr); return AVERROR_INVALIDDATA; } if (!s->initialized) { if ((ret = aac_get_stream_info(avctx)) < 0) { av_free(tmpptr); return ret; } s->initialized = 1; s->frame.nb_samples = avctx->frame_size; } if (tmpptr) { s->frame.nb_samples = avctx->frame_size; if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } memcpy(s->frame.data[0], tmpptr, 2*avctx->channels*avctx->frame_size); } *got_frame_ptr = 1; *(AVFrame *)data = s->frame; av_free(tmpptr); return avpkt->size - valid; }