+static av_cold int alac_decode_close(AVCodecContext *avctx)
+{
+ ALACContext *alac = avctx->priv_data;
+
+ int ch;
+ for (ch = 0; ch < alac->numchannels; ch++) {
+ av_freep(&alac->predicterror_buffer[ch]);
+ av_freep(&alac->outputsamples_buffer[ch]);
+ av_freep(&alac->extra_bits_buffer[ch]);
+ }
+
+ return 0;
+}
+
+static int allocate_buffers(ALACContext *alac)
+{
+ int ch;
+ for (ch = 0; ch < alac->numchannels; ch++) {
+ int buf_size = alac->setinfo_max_samples_per_frame * sizeof(int32_t);
+
+ FF_ALLOC_OR_GOTO(alac->avctx, alac->predicterror_buffer[ch],
+ buf_size, buf_alloc_fail);
+
+ FF_ALLOC_OR_GOTO(alac->avctx, alac->outputsamples_buffer[ch],
+ buf_size, buf_alloc_fail);
+
+ FF_ALLOC_OR_GOTO(alac->avctx, alac->extra_bits_buffer[ch],
+ buf_size, buf_alloc_fail);
+ }
+ return 0;
+buf_alloc_fail:
+ alac_decode_close(alac->avctx);
+ return AVERROR(ENOMEM);
+}
+
+static int alac_set_info(ALACContext *alac)
+{
+ GetByteContext gb;
+
+ bytestream2_init(&gb, alac->avctx->extradata,
+ alac->avctx->extradata_size);
+
+ bytestream2_skipu(&gb, 12); // size:4, alac:4, version:4
+
+ /* buffer size / 2 ? */
+ alac->setinfo_max_samples_per_frame = bytestream2_get_be32u(&gb);
+ if (alac->setinfo_max_samples_per_frame >= UINT_MAX/4){
+ av_log(alac->avctx, AV_LOG_ERROR,
+ "setinfo_max_samples_per_frame too large\n");
+ return AVERROR_INVALIDDATA;
+ }
+ bytestream2_skipu(&gb, 1); // compatible version
+ alac->setinfo_sample_size = bytestream2_get_byteu(&gb);
+ alac->setinfo_rice_historymult = bytestream2_get_byteu(&gb);
+ alac->setinfo_rice_initialhistory = bytestream2_get_byteu(&gb);
+ alac->setinfo_rice_kmodifier = bytestream2_get_byteu(&gb);
+ alac->numchannels = bytestream2_get_byteu(&gb);
+ bytestream2_get_be16u(&gb); // maxRun
+ bytestream2_get_be32u(&gb); // max coded frame size
+ bytestream2_get_be32u(&gb); // average bitrate
+ bytestream2_get_be32u(&gb); // samplerate
+
+ return 0;
+}
+