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 int aac_decode_init(AVCodecContext *avctx) { AACContext *s = avctx->priv_data; AAC_DECODER_ERROR err; int ret = AVERROR(EINVAL); s->handle = aacDecoder_Open(avctx->extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1); if (avctx->extradata_size) { if ((err = aacDecoder_ConfigRaw(s->handle, &avctx->extradata, &avctx->extradata_size)) != AAC_DEC_OK) { av_log(avctx, AV_LOG_WARNING, "Unable to set extradata\n"); goto error; } } avcodec_get_frame_defaults(&s->frame); avctx->coded_frame = &s->frame; avctx->sample_fmt = AV_SAMPLE_FMT_S16; return 0; error: return ret; }
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; }
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; }