2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libavutil/crc.h"
20 #include "libavutil/float_dsp.h"
21 #include "libavutil/intreadwrite.h"
22 #include "libavutil/tx.h"
29 typedef struct ChannelContext {
31 DECLARE_ALIGNED(32, float, imdct_in)[128];
32 DECLARE_ALIGNED(32, float, imdct_out)[128];
33 DECLARE_ALIGNED(32, float, imdct_prev)[128];
34 int8_t scale_factors[128];
42 typedef struct HCAContext {
45 const AVCRC *crc_table;
47 ChannelContext ch[16];
52 unsigned hfr_group_count;
54 uint8_t channel_config;
55 uint8_t total_band_count;
56 uint8_t base_band_count;
57 uint8_t stereo_band_count;
58 uint8_t bands_per_hfr_group;
62 AVFloatDSPContext *fdsp;
65 static void ath_init1(uint8_t *ath, int sample_rate)
70 for (int i = 0; i < 128; i++) {
75 memset(ath+i, 0xFF, (128 - i));
79 ath[i] = ath_base_curve[index];
83 static int ath_init(uint8_t *ath, int type, int sample_rate)
90 ath_init1(ath, sample_rate);
93 return AVERROR_INVALIDDATA;
99 static inline unsigned ceil2(unsigned a, unsigned b)
101 return (b > 0) ? (a / b + ((a % b) ? 1 : 0)) : 0;
104 static av_cold int decode_init(AVCodecContext *avctx)
106 HCAContext *c = avctx->priv_data;
107 GetBitContext *gb = &c->gb;
108 int8_t r[16] = { 0 };
109 float scale = 1.f / 8.f;
113 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
114 c->crc_table = av_crc_get_table(AV_CRC_16_ANSI);
116 if (avctx->channels <= 0 || avctx->channels > 16)
117 return AVERROR(EINVAL);
119 ret = init_get_bits8(gb, avctx->extradata, avctx->extradata_size);
122 skip_bits_long(gb, 32);
123 version = get_bits(gb, 16);
124 skip_bits_long(gb, 16);
126 c->ath_type = version >= 0x200 ? 0 : 1;
128 if (get_bits_long(gb, 32) != MKBETAG('f', 'm', 't', 0))
129 return AVERROR_INVALIDDATA;
130 skip_bits_long(gb, 32);
131 skip_bits_long(gb, 32);
132 skip_bits_long(gb, 32);
134 chunk = get_bits_long(gb, 32);
135 if (chunk == MKBETAG('c', 'o', 'm', 'p')) {
136 skip_bits_long(gb, 16);
137 skip_bits_long(gb, 8);
138 skip_bits_long(gb, 8);
139 c->track_count = get_bits(gb, 8);
140 c->channel_config = get_bits(gb, 8);
141 c->total_band_count = get_bits(gb, 8);
142 c->base_band_count = get_bits(gb, 8);
143 c->stereo_band_count = get_bits(gb, 8);
144 c->bands_per_hfr_group = get_bits(gb, 8);
145 } else if (chunk == MKBETAG('d', 'e', 'c', 0)) {
146 skip_bits_long(gb, 16);
147 skip_bits_long(gb, 8);
148 skip_bits_long(gb, 8);
149 c->total_band_count = get_bits(gb, 8) + 1;
150 c->base_band_count = get_bits(gb, 8) + 1;
151 c->track_count = get_bits(gb, 4);
152 c->channel_config = get_bits(gb, 4);
153 if (!get_bits(gb, 8))
154 c->base_band_count = c->total_band_count;
155 c->stereo_band_count = c->total_band_count - c->base_band_count;
156 c->bands_per_hfr_group = 0;
158 return AVERROR_INVALIDDATA;
160 while (get_bits_left(gb) >= 32) {
161 chunk = get_bits_long(gb, 32);
162 if (chunk == MKBETAG('v', 'b', 'r', 0)) {
163 skip_bits_long(gb, 16);
164 skip_bits_long(gb, 16);
165 } else if (chunk == MKBETAG('a', 't', 'h', 0)) {
166 c->ath_type = get_bits(gb, 16);
167 } else if (chunk == MKBETAG('r', 'v', 'a', 0)) {
168 skip_bits_long(gb, 32);
169 } else if (chunk == MKBETAG('c', 'o', 'm', 'm')) {
170 skip_bits_long(gb, get_bits(gb, 8) * 8);
171 } else if (chunk == MKBETAG('c', 'i', 'p', 'h')) {
172 skip_bits_long(gb, 16);
173 } else if (chunk == MKBETAG('l', 'o', 'o', 'p')) {
174 skip_bits_long(gb, 32);
175 skip_bits_long(gb, 32);
176 skip_bits_long(gb, 16);
177 skip_bits_long(gb, 16);
178 } else if (chunk == MKBETAG('p', 'a', 'd', 0)) {
185 ret = ath_init(c->ath, c->ath_type, avctx->sample_rate);
192 b = avctx->channels / c->track_count;
193 if (c->stereo_band_count && b > 1) {
196 for (int i = 0; i < c->track_count; i++, x+=b) {
205 if (c->channel_config == 0) {
212 if (c->channel_config <= 2) {
219 x[0] = 1; x[1] = 2; x[4] = 1; x[5] = 2;
222 x[0] = 1; x[1] = 2; x[4] = 1; x[5] = 2; x[6] = 1; x[7] = 2;
228 if (c->total_band_count < c->base_band_count)
229 return AVERROR_INVALIDDATA;
231 c->hfr_group_count = ceil2(c->total_band_count - (c->base_band_count + c->stereo_band_count),
232 c->bands_per_hfr_group);
234 if (c->base_band_count + c->stereo_band_count + (unsigned long)c->hfr_group_count > 128ULL)
235 return AVERROR_INVALIDDATA;
237 for (int i = 0; i < avctx->channels; i++) {
238 c->ch[i].chan_type = r[i];
239 c->ch[i].count = c->base_band_count + ((r[i] != 2) ? c->stereo_band_count : 0);
240 c->ch[i].hfr_scale = &c->ch[i].scale_factors[c->base_band_count + c->stereo_band_count];
241 if (c->ch[i].count > 128)
242 return AVERROR_INVALIDDATA;
245 c->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
247 return AVERROR(ENOMEM);
249 return av_tx_init(&c->tx_ctx, &c->tx_fn, AV_TX_FLOAT_MDCT, 1, 128, &scale, 0);
252 static void run_imdct(HCAContext *c, ChannelContext *ch, int index, float *out)
254 c->tx_fn(c->tx_ctx, ch->imdct_out, ch->imdct_in, sizeof(float));
256 c->fdsp->vector_fmul_window(out, ch->imdct_prev + (128 >> 1),
257 ch->imdct_out, window, 128 >> 1);
259 memcpy(ch->imdct_prev, ch->imdct_out, 128 * sizeof(float));
262 static void apply_intensity_stereo(HCAContext *s, ChannelContext *ch1, ChannelContext *ch2,
263 int index, unsigned band_count, unsigned base_band_count,
264 unsigned stereo_band_count)
266 float ratio_l = intensity_ratio_table[ch1->intensity[index]];
267 float ratio_r = ratio_l - 2.0f;
268 float *c1 = &ch1->imdct_in[base_band_count];
269 float *c2 = &ch2->imdct_in[base_band_count];
271 if (ch1->chan_type != 1 || !stereo_band_count)
274 for (int i = 0; i < band_count; i++) {
275 *(c2++) = *c1 * ratio_r;
280 static void reconstruct_hfr(HCAContext *s, ChannelContext *ch,
281 unsigned hfr_group_count,
282 unsigned bands_per_hfr_group,
283 unsigned start_band, unsigned total_band_count)
285 if (ch->chan_type == 2 || !bands_per_hfr_group)
288 for (int i = 0, k = start_band, l = start_band - 1; i < hfr_group_count; i++){
289 for (int j = 0; j < bands_per_hfr_group && k < total_band_count; j++, k++, l--){
290 ch->imdct_in[k] = scale_conversion_table[ch->hfr_scale[i] - ch->scale_factors[l]] * ch->imdct_in[l];
294 ch->imdct_in[127] = 0;
297 static void dequantize_coefficients(HCAContext *c, ChannelContext *ch)
299 GetBitContext *gb = &c->gb;
301 for (int i = 0; i < ch->count; i++) {
302 unsigned scale = ch->scale[i];
303 int nb_bits = max_bits_table[scale];
304 int value = get_bitsz(gb, nb_bits);
308 value = (1 - ((value & 1) << 1)) * (value >> 1);
310 skip_bits_long(gb, -1);
314 skip_bits_long(gb, quant_spectrum_bits[value] - nb_bits);
315 factor = quant_spectrum_value[value];
317 ch->imdct_in[i] = factor * ch->base[i];
320 memset(ch->imdct_in + ch->count, 0, sizeof(ch->imdct_in) - ch->count * sizeof(ch->imdct_in[0]));
323 static void unpack(HCAContext *c, ChannelContext *ch,
324 unsigned hfr_group_count,
325 int packed_noise_level,
328 GetBitContext *gb = &c->gb;
329 int delta_bits = get_bits(gb, 3);
331 if (delta_bits > 5) {
332 for (int i = 0; i < ch->count; i++)
333 ch->scale_factors[i] = get_bits(gb, 6);
334 } else if (delta_bits) {
335 int factor = get_bits(gb, 6);
336 int max_value = (1 << delta_bits) - 1;
337 int half_max = max_value >> 1;
339 ch->scale_factors[0] = factor;
340 for (int i = 1; i < ch->count; i++){
341 int delta = get_bits(gb, delta_bits);
343 if (delta == max_value) {
344 factor = get_bits(gb, 6);
346 factor += delta - half_max;
349 ch->scale_factors[i] = factor;
352 memset(ch->scale_factors, 0, 128);
355 if (ch->chan_type == 2){
356 ch->intensity[0] = get_bits(gb, 4);
357 if (ch->intensity[0] < 15) {
358 for (int i = 1; i < 8; i++)
359 ch->intensity[i] = get_bits(gb, 4);
362 for (int i = 0; i < hfr_group_count; i++)
363 ch->hfr_scale[i] = get_bits(gb, 6);
366 for (int i = 0; i < ch->count; i++) {
367 int scale = ch->scale_factors[i];
370 scale = c->ath[i] + ((packed_noise_level + i) >> 8) - ((scale * 5) >> 1) + 2;
371 scale = scale_table[av_clip(scale, 0, 58)];
373 ch->scale[i] = scale;
376 memset(ch->scale + ch->count, 0, sizeof(ch->scale) - ch->count);
378 for (int i = 0; i < ch->count; i++)
379 ch->base[i] = dequantizer_scaling_table[ch->scale_factors[i]] * quant_step_size[ch->scale[i]];
382 static int decode_frame(AVCodecContext *avctx, void *data,
383 int *got_frame_ptr, AVPacket *avpkt)
385 AVFrame *frame = data;
386 HCAContext *c = avctx->priv_data;
387 int ch, ret, packed_noise_level;
388 GetBitContext *gb = &c->gb;
391 if (avctx->err_recognition & AV_EF_CRCCHECK) {
392 if (av_crc(c->crc_table, 0, avpkt->data, avpkt->size))
393 return AVERROR_INVALIDDATA;
396 if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
399 if (get_bits(gb, 16) != 0xFFFF)
400 return AVERROR_INVALIDDATA;
402 frame->nb_samples = 1024;
403 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
405 samples = (float **)frame->extended_data;
407 packed_noise_level = (get_bits(gb, 9) << 8) - get_bits(gb, 7);
409 for (ch = 0; ch < avctx->channels; ch++)
410 unpack(c, &c->ch[ch], c->hfr_group_count, packed_noise_level, c->ath);
412 for (int i = 0; i < 8; i++) {
413 for (ch = 0; ch < avctx->channels; ch++)
414 dequantize_coefficients(c, &c->ch[ch]);
415 for (ch = 0; ch < avctx->channels; ch++)
416 reconstruct_hfr(c, &c->ch[ch], c->hfr_group_count, c->bands_per_hfr_group,
417 c->stereo_band_count + c->base_band_count, c->total_band_count);
418 for (ch = 0; ch < avctx->channels - 1; ch++)
419 apply_intensity_stereo(c, &c->ch[ch], &c->ch[ch+1], i,
420 c->total_band_count - c->base_band_count,
421 c->base_band_count, c->stereo_band_count);
422 for (ch = 0; ch < avctx->channels; ch++)
423 run_imdct(c, &c->ch[ch], i, samples[ch] + i * 128);
431 static av_cold int decode_close(AVCodecContext *avctx)
433 HCAContext *c = avctx->priv_data;
436 av_tx_uninit(&c->tx_ctx);
441 AVCodec ff_hca_decoder = {
443 .long_name = NULL_IF_CONFIG_SMALL("CRI HCA"),
444 .type = AVMEDIA_TYPE_AUDIO,
445 .id = AV_CODEC_ID_HCA,
446 .priv_data_size = sizeof(HCAContext),
448 .decode = decode_frame,
449 .close = decode_close,
450 .capabilities = AV_CODEC_CAP_DR1,
451 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
452 AV_SAMPLE_FMT_NONE },