static av_cold int fdk_aac_decode_init(AVCodecContext *avctx) { FDKAACDecContext *s = avctx->priv_data; AAC_DECODER_ERROR err; s->handle = aacDecoder_Open(avctx->extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1); if (!s->handle) { av_log(avctx, AV_LOG_ERROR, "Error opening decoder\n"); return AVERROR_UNKNOWN; } if (avctx->extradata_size) { if ((err = aacDecoder_ConfigRaw(s->handle, &avctx->extradata, &avctx->extradata_size)) != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "Unable to set extradata\n"); return AVERROR_INVALIDDATA; } } if ((err = aacDecoder_SetParam(s->handle, AAC_CONCEAL_METHOD, s->conceal_method)) != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "Unable to set error concealment method\n"); return AVERROR_UNKNOWN; } avctx->sample_fmt = AV_SAMPLE_FMT_S16; return 0; }
static av_cold void fdk_aac_decode_flush(AVCodecContext *avctx) { FDKAACDecContext *s = avctx->priv_data; AAC_DECODER_ERROR err; if (!s->handle) return; if ((err = aacDecoder_SetParam(s->handle, AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK) av_log(avctx, AV_LOG_WARNING, "failed to clear buffer when flushing\n"); }
void fdk_aac_decode_flush(AAC_Dec codec) { AAC_DECODER_ERROR err; if(!codec->handle) return; if ((err = aacDecoder_SetParam(codec->handle, AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK) { LOGI("failed to clear buffer when flushing"); } }
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; }
static av_cold int fdk_aac_decode_init(AVCodecContext *avctx) { FDKAACDecContext *s = avctx->priv_data; AAC_DECODER_ERROR err; s->handle = aacDecoder_Open(avctx->extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1); if (!s->handle) { av_log(avctx, AV_LOG_ERROR, "Error opening decoder\n"); return AVERROR_UNKNOWN; } if (avctx->extradata_size) { if ((err = aacDecoder_ConfigRaw(s->handle, &avctx->extradata, &avctx->extradata_size)) != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "Unable to set extradata\n"); return AVERROR_INVALIDDATA; } } if ((err = aacDecoder_SetParam(s->handle, AAC_CONCEAL_METHOD, s->conceal_method)) != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "Unable to set error concealment method\n"); return AVERROR_UNKNOWN; } if (avctx->request_channel_layout > 0 && avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE) { int downmix_channels = -1; switch (avctx->request_channel_layout) { case AV_CH_LAYOUT_STEREO: case AV_CH_LAYOUT_STEREO_DOWNMIX: downmix_channels = 2; break; case AV_CH_LAYOUT_MONO: downmix_channels = 1; break; default: av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n"); break; } if (downmix_channels != -1) { if (aacDecoder_SetParam(s->handle, AAC_PCM_MAX_OUTPUT_CHANNELS, downmix_channels) != AAC_DEC_OK) { av_log(avctx, AV_LOG_WARNING, "Unable to set output channels in the decoder\n"); } else { s->anc_buffer = av_malloc(DMX_ANC_BUFFSIZE); if (!s->anc_buffer) { av_log(avctx, AV_LOG_ERROR, "Unable to allocate ancillary buffer for the decoder\n"); return AVERROR(ENOMEM); } if (aacDecoder_AncDataInit(s->handle, s->anc_buffer, DMX_ANC_BUFFSIZE)) { av_log(avctx, AV_LOG_ERROR, "Unable to register downmix ancillary buffer in the decoder\n"); return AVERROR_UNKNOWN; } } } } if (s->drc_boost != -1) { if (aacDecoder_SetParam(s->handle, AAC_DRC_BOOST_FACTOR, s->drc_boost) != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "Unable to set DRC boost factor in the decoder\n"); return AVERROR_UNKNOWN; } } if (s->drc_cut != -1) { if (aacDecoder_SetParam(s->handle, AAC_DRC_ATTENUATION_FACTOR, s->drc_cut) != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "Unable to set DRC attenuation factor in the decoder\n"); return AVERROR_UNKNOWN; } } if (s->drc_level != -1) { if (aacDecoder_SetParam(s->handle, AAC_DRC_REFERENCE_LEVEL, s->drc_level) != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "Unable to set DRC reference level in the decoder\n"); return AVERROR_UNKNOWN; } } if (s->drc_heavy != -1) { if (aacDecoder_SetParam(s->handle, AAC_DRC_HEAVY_COMPRESSION, s->drc_heavy) != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "Unable to set DRC heavy compression in the decoder\n"); return AVERROR_UNKNOWN; } } #ifdef AACDECODER_LIB_VL0 if (aacDecoder_SetParam(s->handle, AAC_PCM_LIMITER_ENABLE, s->level_limit) != AAC_DEC_OK) { av_log(avctx, AV_LOG_ERROR, "Unable to set in signal level limiting in the decoder\n"); return AVERROR_UNKNOWN; } #endif avctx->sample_fmt = AV_SAMPLE_FMT_S16; s->decoder_buffer_size = DECODER_BUFFSIZE * DECODER_MAX_CHANNELS; s->decoder_buffer = av_malloc(s->decoder_buffer_size); if (!s->decoder_buffer) return AVERROR(ENOMEM); return 0; }