FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hcadec.c
Date: 2023-03-31 03:41:15
Exec Total Coverage
Lines: 0 241 0.0%
Functions: 0 11 0.0%
Branches: 0 145 0.0%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "libavutil/crc.h"
20 #include "libavutil/float_dsp.h"
21 #include "libavutil/mem_internal.h"
22 #include "libavutil/tx.h"
23
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "codec_internal.h"
27 #include "decode.h"
28 #include "get_bits.h"
29 #include "hca_data.h"
30
31 typedef struct ChannelContext {
32 float base[128];
33 DECLARE_ALIGNED(32, float, imdct_in)[128];
34 DECLARE_ALIGNED(32, float, imdct_out)[128];
35 DECLARE_ALIGNED(32, float, imdct_prev)[128];
36 int8_t scale_factors[128];
37 uint8_t scale[128];
38 int8_t intensity[8];
39 int8_t *hfr_scale;
40 unsigned count;
41 int chan_type;
42 } ChannelContext;
43
44 typedef struct HCAContext {
45 const AVCRC *crc_table;
46
47 ChannelContext ch[16];
48
49 uint8_t ath[128];
50
51 int ath_type;
52 unsigned hfr_group_count;
53 uint8_t track_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;
59
60 av_tx_fn tx_fn;
61 AVTXContext *tx_ctx;
62 AVFloatDSPContext *fdsp;
63 } HCAContext;
64
65 static void ath_init1(uint8_t *ath, int sample_rate)
66 {
67 unsigned int index;
68 unsigned int acc = 0;
69
70 for (int i = 0; i < 128; i++) {
71 acc += sample_rate;
72 index = acc >> 13;
73
74 if (index >= 654) {
75 memset(ath+i, 0xFF, (128 - i));
76 break;
77 }
78
79 ath[i] = ath_base_curve[index];
80 }
81 }
82
83 static int ath_init(uint8_t *ath, int type, int sample_rate)
84 {
85 switch (type) {
86 case 0:
87 /* nothing to do */
88 break;
89 case 1:
90 ath_init1(ath, sample_rate);
91 break;
92 default:
93 return AVERROR_INVALIDDATA;
94 }
95
96 return 0;
97 }
98
99 static inline unsigned ceil2(unsigned a, unsigned b)
100 {
101 return (b > 0) ? (a / b + ((a % b) ? 1 : 0)) : 0;
102 }
103
104 static av_cold int decode_init(AVCodecContext *avctx)
105 {
106 HCAContext *c = avctx->priv_data;
107 GetByteContext gb0, *const gb = &gb0;
108 int8_t r[16] = { 0 };
109 float scale = 1.f / 8.f;
110 unsigned b, chunk;
111 int version, ret;
112
113 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
114 c->crc_table = av_crc_get_table(AV_CRC_16_ANSI);
115
116 if (avctx->ch_layout.nb_channels <= 0 || avctx->ch_layout.nb_channels > 16)
117 return AVERROR(EINVAL);
118
119 if (avctx->extradata_size < 36)
120 return AVERROR_INVALIDDATA;
121 bytestream2_init(gb, avctx->extradata, avctx->extradata_size);
122
123 bytestream2_skipu(gb, 4);
124 version = bytestream2_get_be16(gb);
125 bytestream2_skipu(gb, 2);
126
127 c->ath_type = version >= 0x200 ? 0 : 1;
128
129 if (bytestream2_get_be32u(gb) != MKBETAG('f', 'm', 't', 0))
130 return AVERROR_INVALIDDATA;
131 bytestream2_skipu(gb, 4);
132 bytestream2_skipu(gb, 4);
133 bytestream2_skipu(gb, 4);
134
135 chunk = bytestream2_get_be32u(gb);
136 if (chunk == MKBETAG('c', 'o', 'm', 'p')) {
137 bytestream2_skipu(gb, 2);
138 bytestream2_skipu(gb, 1);
139 bytestream2_skipu(gb, 1);
140 c->track_count = bytestream2_get_byteu(gb);
141 c->channel_config = bytestream2_get_byteu(gb);
142 c->total_band_count = bytestream2_get_byteu(gb);
143 c->base_band_count = bytestream2_get_byteu(gb);
144 c->stereo_band_count = bytestream2_get_byte (gb);
145 c->bands_per_hfr_group = bytestream2_get_byte (gb);
146 } else if (chunk == MKBETAG('d', 'e', 'c', 0)) {
147 bytestream2_skipu(gb, 2);
148 bytestream2_skipu(gb, 1);
149 bytestream2_skipu(gb, 1);
150 c->total_band_count = bytestream2_get_byteu(gb) + 1;
151 c->base_band_count = bytestream2_get_byteu(gb) + 1;
152 c->track_count = bytestream2_peek_byteu(gb) >> 4;
153 c->channel_config = bytestream2_get_byteu(gb) & 0xF;
154 if (!bytestream2_get_byteu(gb))
155 c->base_band_count = c->total_band_count;
156 c->stereo_band_count = c->total_band_count - c->base_band_count;
157 c->bands_per_hfr_group = 0;
158 } else
159 return AVERROR_INVALIDDATA;
160
161 if (c->total_band_count > FF_ARRAY_ELEMS(c->ch->imdct_in))
162 return AVERROR_INVALIDDATA;
163
164
165 while (bytestream2_get_bytes_left(gb) >= 4) {
166 chunk = bytestream2_get_be32u(gb);
167 if (chunk == MKBETAG('v', 'b', 'r', 0)) {
168 bytestream2_skip(gb, 2 + 2);
169 } else if (chunk == MKBETAG('a', 't', 'h', 0)) {
170 c->ath_type = bytestream2_get_be16(gb);
171 } else if (chunk == MKBETAG('r', 'v', 'a', 0)) {
172 bytestream2_skip(gb, 4);
173 } else if (chunk == MKBETAG('c', 'o', 'm', 'm')) {
174 bytestream2_skip(gb, bytestream2_get_byte(gb) * 8);
175 } else if (chunk == MKBETAG('c', 'i', 'p', 'h')) {
176 bytestream2_skip(gb, 2);
177 } else if (chunk == MKBETAG('l', 'o', 'o', 'p')) {
178 bytestream2_skip(gb, 4 + 4 + 2 + 2);
179 } else if (chunk == MKBETAG('p', 'a', 'd', 0)) {
180 break;
181 } else {
182 break;
183 }
184 }
185
186 ret = ath_init(c->ath, c->ath_type, avctx->sample_rate);
187 if (ret < 0)
188 return ret;
189
190 if (!c->track_count)
191 c->track_count = 1;
192
193 b = avctx->ch_layout.nb_channels / c->track_count;
194 if (c->stereo_band_count && b > 1) {
195 int8_t *x = r;
196
197 for (int i = 0; i < c->track_count; i++, x+=b) {
198 switch (b) {
199 case 2:
200 case 3:
201 x[0] = 1;
202 x[1] = 2;
203 break;
204 case 4:
205 x[0]=1; x[1] = 2;
206 if (c->channel_config == 0) {
207 x[2]=1;
208 x[3]=2;
209 }
210 break;
211 case 5:
212 x[0]=1; x[1] = 2;
213 if (c->channel_config <= 2) {
214 x[3]=1;
215 x[4]=2;
216 }
217 break;
218 case 6:
219 case 7:
220 x[0] = 1; x[1] = 2; x[4] = 1; x[5] = 2;
221 break;
222 case 8:
223 x[0] = 1; x[1] = 2; x[4] = 1; x[5] = 2; x[6] = 1; x[7] = 2;
224 break;
225 }
226 }
227 }
228
229 if (c->total_band_count < c->base_band_count)
230 return AVERROR_INVALIDDATA;
231
232 c->hfr_group_count = ceil2(c->total_band_count - (c->base_band_count + c->stereo_band_count),
233 c->bands_per_hfr_group);
234
235 if (c->base_band_count + c->stereo_band_count + (unsigned long)c->hfr_group_count > 128ULL)
236 return AVERROR_INVALIDDATA;
237
238 for (int i = 0; i < avctx->ch_layout.nb_channels; i++) {
239 c->ch[i].chan_type = r[i];
240 c->ch[i].count = c->base_band_count + ((r[i] != 2) ? c->stereo_band_count : 0);
241 c->ch[i].hfr_scale = &c->ch[i].scale_factors[c->base_band_count + c->stereo_band_count];
242 if (c->ch[i].count > 128)
243 return AVERROR_INVALIDDATA;
244 }
245
246 c->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
247 if (!c->fdsp)
248 return AVERROR(ENOMEM);
249
250 return av_tx_init(&c->tx_ctx, &c->tx_fn, AV_TX_FLOAT_MDCT, 1, 128, &scale, 0);
251 }
252
253 static void run_imdct(HCAContext *c, ChannelContext *ch, int index, float *out)
254 {
255 c->tx_fn(c->tx_ctx, ch->imdct_out, ch->imdct_in, sizeof(float));
256
257 c->fdsp->vector_fmul_window(out, ch->imdct_prev + (128 >> 1),
258 ch->imdct_out, window, 128 >> 1);
259
260 memcpy(ch->imdct_prev, ch->imdct_out, 128 * sizeof(float));
261 }
262
263 static void apply_intensity_stereo(HCAContext *s, ChannelContext *ch1, ChannelContext *ch2,
264 int index, unsigned band_count, unsigned base_band_count,
265 unsigned stereo_band_count)
266 {
267 float ratio_l = intensity_ratio_table[ch2->intensity[index]];
268 float ratio_r = ratio_l - 2.0f;
269 float *c1 = &ch1->imdct_in[base_band_count];
270 float *c2 = &ch2->imdct_in[base_band_count];
271
272 if (ch1->chan_type != 1 || !stereo_band_count)
273 return;
274
275 for (int i = 0; i < band_count; i++) {
276 *(c2++) = *c1 * ratio_r;
277 *(c1++) *= ratio_l;
278 }
279 }
280
281 static void reconstruct_hfr(HCAContext *s, ChannelContext *ch,
282 unsigned hfr_group_count,
283 unsigned bands_per_hfr_group,
284 unsigned start_band, unsigned total_band_count)
285 {
286 if (ch->chan_type == 2 || !bands_per_hfr_group)
287 return;
288
289 for (int i = 0, k = start_band, l = start_band - 1; i < hfr_group_count; i++){
290 for (int j = 0; j < bands_per_hfr_group && k < total_band_count && l >= 0; j++, k++, l--){
291 ch->imdct_in[k] = scale_conversion_table[ scale_conv_bias +
292 av_clip_intp2(ch->hfr_scale[i] - ch->scale_factors[l], 6) ] * ch->imdct_in[l];
293 }
294 }
295
296 ch->imdct_in[127] = 0;
297 }
298
299 static void dequantize_coefficients(HCAContext *c, ChannelContext *ch,
300 GetBitContext *gb)
301 {
302 for (int i = 0; i < ch->count; i++) {
303 unsigned scale = ch->scale[i];
304 int nb_bits = max_bits_table[scale];
305 int value = get_bitsz(gb, nb_bits);
306 float factor;
307
308 if (scale > 7) {
309 value = (1 - ((value & 1) << 1)) * (value >> 1);
310 if (!value)
311 skip_bits_long(gb, -1);
312 factor = value;
313 } else {
314 value += scale << 4;
315 skip_bits_long(gb, quant_spectrum_bits[value] - nb_bits);
316 factor = quant_spectrum_value[value];
317 }
318 ch->imdct_in[i] = factor * ch->base[i];
319 }
320
321 memset(ch->imdct_in + ch->count, 0, sizeof(ch->imdct_in) - ch->count * sizeof(ch->imdct_in[0]));
322 }
323
324 static void unpack(HCAContext *c, ChannelContext *ch,
325 GetBitContext *gb,
326 unsigned hfr_group_count,
327 int packed_noise_level,
328 const uint8_t *ath)
329 {
330 int delta_bits = get_bits(gb, 3);
331
332 if (delta_bits > 5) {
333 for (int i = 0; i < ch->count; i++)
334 ch->scale_factors[i] = get_bits(gb, 6);
335 } else if (delta_bits) {
336 int factor = get_bits(gb, 6);
337 int max_value = (1 << delta_bits) - 1;
338 int half_max = max_value >> 1;
339
340 ch->scale_factors[0] = factor;
341 for (int i = 1; i < ch->count; i++){
342 int delta = get_bits(gb, delta_bits);
343
344 if (delta == max_value) {
345 factor = get_bits(gb, 6);
346 } else {
347 factor += delta - half_max;
348 }
349 factor = av_clip_uintp2(factor, 6);
350
351 ch->scale_factors[i] = factor;
352 }
353 } else {
354 memset(ch->scale_factors, 0, 128);
355 }
356
357 if (ch->chan_type == 2){
358 ch->intensity[0] = get_bits(gb, 4);
359 if (ch->intensity[0] < 15) {
360 for (int i = 1; i < 8; i++)
361 ch->intensity[i] = get_bits(gb, 4);
362 }
363 } else {
364 for (int i = 0; i < hfr_group_count; i++)
365 ch->hfr_scale[i] = get_bits(gb, 6);
366 }
367
368 for (int i = 0; i < ch->count; i++) {
369 int scale = ch->scale_factors[i];
370
371 if (scale) {
372 scale = c->ath[i] + ((packed_noise_level + i) >> 8) - ((scale * 5) >> 1) + 2;
373 scale = scale_table[av_clip(scale, 0, 58)];
374 }
375 ch->scale[i] = scale;
376 }
377
378 memset(ch->scale + ch->count, 0, sizeof(ch->scale) - ch->count);
379
380 for (int i = 0; i < ch->count; i++)
381 ch->base[i] = dequantizer_scaling_table[ch->scale_factors[i]] * quant_step_size[ch->scale[i]];
382 }
383
384 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
385 int *got_frame_ptr, AVPacket *avpkt)
386 {
387 HCAContext *c = avctx->priv_data;
388 int ch, ret, packed_noise_level;
389 GetBitContext gb0, *const gb = &gb0;
390 float **samples;
391
392 if (avctx->err_recognition & AV_EF_CRCCHECK) {
393 if (av_crc(c->crc_table, 0, avpkt->data, avpkt->size))
394 return AVERROR_INVALIDDATA;
395 }
396
397 if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
398 return ret;
399
400 if (get_bits(gb, 16) != 0xFFFF)
401 return AVERROR_INVALIDDATA;
402
403 frame->nb_samples = 1024;
404 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
405 return ret;
406 samples = (float **)frame->extended_data;
407
408 packed_noise_level = (get_bits(gb, 9) << 8) - get_bits(gb, 7);
409
410 for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
411 unpack(c, &c->ch[ch], gb, c->hfr_group_count, packed_noise_level, c->ath);
412
413 for (int i = 0; i < 8; i++) {
414 for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
415 dequantize_coefficients(c, &c->ch[ch], gb);
416 for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
417 reconstruct_hfr(c, &c->ch[ch], c->hfr_group_count, c->bands_per_hfr_group,
418 c->stereo_band_count + c->base_band_count, c->total_band_count);
419 for (ch = 0; ch < avctx->ch_layout.nb_channels - 1; ch++)
420 apply_intensity_stereo(c, &c->ch[ch], &c->ch[ch+1], i,
421 c->total_band_count - c->base_band_count,
422 c->base_band_count, c->stereo_band_count);
423 for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
424 run_imdct(c, &c->ch[ch], i, samples[ch] + i * 128);
425 }
426
427 *got_frame_ptr = 1;
428
429 return avpkt->size;
430 }
431
432 static av_cold int decode_close(AVCodecContext *avctx)
433 {
434 HCAContext *c = avctx->priv_data;
435
436 av_freep(&c->fdsp);
437 av_tx_uninit(&c->tx_ctx);
438
439 return 0;
440 }
441
442 const FFCodec ff_hca_decoder = {
443 .p.name = "hca",
444 CODEC_LONG_NAME("CRI HCA"),
445 .p.type = AVMEDIA_TYPE_AUDIO,
446 .p.id = AV_CODEC_ID_HCA,
447 .priv_data_size = sizeof(HCAContext),
448 .init = decode_init,
449 FF_CODEC_DECODE_CB(decode_frame),
450 .close = decode_close,
451 .p.capabilities = AV_CODEC_CAP_DR1,
452 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
453 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
454 AV_SAMPLE_FMT_NONE },
455 };
456