FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hcadec.c
Date: 2023-12-04 05:51:44
Exec Total Coverage
Lines: 0 355 0.0%
Functions: 0 17 0.0%
Branches: 0 204 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 #define HCA_MASK 0x7f7f7f7f
32 #define MAX_CHANNELS 16
33
34 typedef struct ChannelContext {
35 DECLARE_ALIGNED(32, float, base)[128];
36 DECLARE_ALIGNED(32, float, factors)[128];
37 DECLARE_ALIGNED(32, float, imdct_in)[128];
38 DECLARE_ALIGNED(32, float, imdct_out)[128];
39 DECLARE_ALIGNED(32, float, imdct_prev)[128];
40 int8_t scale_factors[128];
41 uint8_t scale[128];
42 int8_t intensity[8];
43 int8_t *hfr_scale;
44 unsigned count;
45 int chan_type;
46 } ChannelContext;
47
48 typedef struct HCAContext {
49 const AVCRC *crc_table;
50
51 ChannelContext ch[MAX_CHANNELS];
52
53 uint8_t ath[128];
54 uint8_t cipher[256];
55 uint64_t key;
56 uint16_t subkey;
57
58 int ath_type;
59 int ciph_type;
60 unsigned hfr_group_count;
61 uint8_t track_count;
62 uint8_t channel_config;
63 uint8_t total_band_count;
64 uint8_t base_band_count;
65 uint8_t stereo_band_count;
66 uint8_t bands_per_hfr_group;
67
68 // Set during init() and freed on close(). Untouched on init_flush()
69 av_tx_fn tx_fn;
70 AVTXContext *tx_ctx;
71 AVFloatDSPContext *fdsp;
72 } HCAContext;
73
74 static void cipher_init56_create_table(uint8_t *r, uint8_t key)
75 {
76 const int mul = ((key & 1) << 3) | 5;
77 const int add = (key & 0xE) | 1;
78
79 key >>= 4;
80 for (int i = 0; i < 16; i++) {
81 key = (key * mul + add) & 0xF;
82 r[i] = key;
83 }
84 }
85
86 static void cipher_init56(uint8_t *cipher, uint64_t keycode)
87 {
88 uint8_t base[256], base_r[16], base_c[16], kc[8], seed[16];
89
90 /* 56bit keycode encryption (given as a uint64_t number, but upper 8b aren't used) */
91 /* keycode = keycode - 1 */
92 if (keycode != 0)
93 keycode--;
94
95 /* init keycode table */
96 for (int r = 0; r < (8-1); r++) {
97 kc[r] = keycode & 0xFF;
98 keycode = keycode >> 8;
99 }
100
101 /* init seed table */
102 seed[ 0] = kc[1];
103 seed[ 1] = kc[1] ^ kc[6];
104 seed[ 2] = kc[2] ^ kc[3];
105 seed[ 3] = kc[2];
106 seed[ 4] = kc[2] ^ kc[1];
107 seed[ 5] = kc[3] ^ kc[4];
108 seed[ 6] = kc[3];
109 seed[ 7] = kc[3] ^ kc[2];
110 seed[ 8] = kc[4] ^ kc[5];
111 seed[ 9] = kc[4];
112 seed[10] = kc[4] ^ kc[3];
113 seed[11] = kc[5] ^ kc[6];
114 seed[12] = kc[5];
115 seed[13] = kc[5] ^ kc[4];
116 seed[14] = kc[6] ^ kc[1];
117 seed[15] = kc[6];
118
119 /* init base table */
120 cipher_init56_create_table(base_r, kc[0]);
121 for (int r = 0; r < 16; r++) {
122 uint8_t nb;
123 cipher_init56_create_table(base_c, seed[r]);
124 nb = base_r[r] << 4;
125 for (int c = 0; c < 16; c++)
126 base[r*16 + c] = nb | base_c[c]; /* combine nibbles */
127 }
128
129 /* final shuffle table */
130 {
131 unsigned x = 0;
132 unsigned pos = 1;
133
134 for (int i = 0; i < 256; i++) {
135 x = (x + 17) & 0xFF;
136 if (base[x] != 0 && base[x] != 0xFF)
137 cipher[pos++] = base[x];
138 }
139 cipher[0] = 0;
140 cipher[0xFF] = 0xFF;
141 }
142 }
143
144 static void cipher_init(uint8_t *cipher, int type, uint64_t keycode, uint16_t subkey)
145 {
146 switch (type) {
147 case 56:
148 if (keycode) {
149 if (subkey)
150 keycode = keycode * (((uint64_t)subkey<<16u)|((uint16_t)~subkey+2u));
151 cipher_init56(cipher, keycode);
152 }
153 break;
154 case 0:
155 for (int i = 0; i < 256; i++)
156 cipher[i] = i;
157 break;
158 }
159 }
160
161 static void ath_init1(uint8_t *ath, int sample_rate)
162 {
163 unsigned int index;
164 unsigned int acc = 0;
165
166 for (int i = 0; i < 128; i++) {
167 acc += sample_rate;
168 index = acc >> 13;
169
170 if (index >= 654) {
171 memset(ath+i, 0xFF, (128 - i));
172 break;
173 }
174
175 ath[i] = ath_base_curve[index];
176 }
177 }
178
179 static int ath_init(uint8_t *ath, int type, int sample_rate)
180 {
181 switch (type) {
182 case 0:
183 /* nothing to do */
184 break;
185 case 1:
186 ath_init1(ath, sample_rate);
187 break;
188 default:
189 return AVERROR_INVALIDDATA;
190 }
191
192 return 0;
193 }
194
195 static inline unsigned ceil2(unsigned a, unsigned b)
196 {
197 return (b > 0) ? (a / b + ((a % b) ? 1 : 0)) : 0;
198 }
199
200 static av_cold void init_flush(AVCodecContext *avctx)
201 {
202 HCAContext *c = avctx->priv_data;
203
204 memset(c, 0, offsetof(HCAContext, tx_fn));
205 }
206
207 static int init_hca(AVCodecContext *avctx, const uint8_t *extradata,
208 const int extradata_size)
209 {
210 HCAContext *c = avctx->priv_data;
211 GetByteContext gb0, *const gb = &gb0;
212 int8_t r[16] = { 0 };
213 unsigned b, chunk;
214 int version, ret;
215
216 init_flush(avctx);
217
218 if (extradata_size < 36)
219 return AVERROR_INVALIDDATA;
220
221 bytestream2_init(gb, extradata, extradata_size);
222
223 bytestream2_skipu(gb, 4);
224 version = bytestream2_get_be16(gb);
225 bytestream2_skipu(gb, 2);
226
227 c->ath_type = version >= 0x200 ? 0 : 1;
228
229 if ((bytestream2_get_be32u(gb) & HCA_MASK) != MKBETAG('f', 'm', 't', 0))
230 return AVERROR_INVALIDDATA;
231 bytestream2_skipu(gb, 4);
232 bytestream2_skipu(gb, 4);
233 bytestream2_skipu(gb, 4);
234
235 chunk = bytestream2_get_be32u(gb) & HCA_MASK;
236 if (chunk == MKBETAG('c', 'o', 'm', 'p')) {
237 bytestream2_skipu(gb, 2);
238 bytestream2_skipu(gb, 1);
239 bytestream2_skipu(gb, 1);
240 c->track_count = bytestream2_get_byteu(gb);
241 c->channel_config = bytestream2_get_byteu(gb);
242 c->total_band_count = bytestream2_get_byteu(gb);
243 c->base_band_count = bytestream2_get_byteu(gb);
244 c->stereo_band_count = bytestream2_get_byte (gb);
245 c->bands_per_hfr_group = bytestream2_get_byte (gb);
246 } else if (chunk == MKBETAG('d', 'e', 'c', 0)) {
247 bytestream2_skipu(gb, 2);
248 bytestream2_skipu(gb, 1);
249 bytestream2_skipu(gb, 1);
250 c->total_band_count = bytestream2_get_byteu(gb) + 1;
251 c->base_band_count = bytestream2_get_byteu(gb) + 1;
252 c->track_count = bytestream2_peek_byteu(gb) >> 4;
253 c->channel_config = bytestream2_get_byteu(gb) & 0xF;
254 if (!bytestream2_get_byteu(gb))
255 c->base_band_count = c->total_band_count;
256 c->stereo_band_count = c->total_band_count - c->base_band_count;
257 c->bands_per_hfr_group = 0;
258 } else
259 return AVERROR_INVALIDDATA;
260
261 if (c->total_band_count > FF_ARRAY_ELEMS(c->ch->imdct_in))
262 return AVERROR_INVALIDDATA;
263
264 while (bytestream2_get_bytes_left(gb) >= 4) {
265 chunk = bytestream2_get_be32u(gb) & HCA_MASK;
266 if (chunk == MKBETAG('v', 'b', 'r', 0)) {
267 bytestream2_skip(gb, 2 + 2);
268 } else if (chunk == MKBETAG('a', 't', 'h', 0)) {
269 c->ath_type = bytestream2_get_be16(gb);
270 } else if (chunk == MKBETAG('r', 'v', 'a', 0)) {
271 bytestream2_skip(gb, 4);
272 } else if (chunk == MKBETAG('c', 'o', 'm', 'm')) {
273 bytestream2_skip(gb, bytestream2_get_byte(gb) * 8);
274 } else if (chunk == MKBETAG('c', 'i', 'p', 'h')) {
275 c->ciph_type = bytestream2_get_be16(gb);
276 } else if (chunk == MKBETAG('l', 'o', 'o', 'p')) {
277 bytestream2_skip(gb, 4 + 4 + 2 + 2);
278 } else if (chunk == MKBETAG('p', 'a', 'd', 0)) {
279 break;
280 } else {
281 break;
282 }
283 }
284
285 if (bytestream2_get_bytes_left(gb) >= 10) {
286 bytestream2_skip(gb, bytestream2_get_bytes_left(gb) - 10);
287 c->key = bytestream2_get_be64u(gb);
288 c->subkey = bytestream2_get_be16u(gb);
289 }
290
291 cipher_init(c->cipher, c->ciph_type, c->key, c->subkey);
292
293 ret = ath_init(c->ath, c->ath_type, avctx->sample_rate);
294 if (ret < 0)
295 return ret;
296
297 if (!c->track_count)
298 c->track_count = 1;
299
300 b = avctx->ch_layout.nb_channels / c->track_count;
301 if (c->stereo_band_count && b > 1) {
302 int8_t *x = r;
303
304 for (int i = 0; i < c->track_count; i++, x+=b) {
305 switch (b) {
306 case 2:
307 case 3:
308 x[0] = 1;
309 x[1] = 2;
310 break;
311 case 4:
312 x[0]=1; x[1] = 2;
313 if (c->channel_config == 0) {
314 x[2]=1;
315 x[3]=2;
316 }
317 break;
318 case 5:
319 x[0]=1; x[1] = 2;
320 if (c->channel_config <= 2) {
321 x[3]=1;
322 x[4]=2;
323 }
324 break;
325 case 6:
326 case 7:
327 x[0] = 1; x[1] = 2; x[4] = 1; x[5] = 2;
328 break;
329 case 8:
330 x[0] = 1; x[1] = 2; x[4] = 1; x[5] = 2; x[6] = 1; x[7] = 2;
331 break;
332 }
333 }
334 }
335
336 if (c->total_band_count < c->base_band_count)
337 return AVERROR_INVALIDDATA;
338
339 c->hfr_group_count = ceil2(c->total_band_count - (c->base_band_count + c->stereo_band_count),
340 c->bands_per_hfr_group);
341
342 if (c->base_band_count + c->stereo_band_count + (unsigned long)c->hfr_group_count > 128ULL)
343 return AVERROR_INVALIDDATA;
344
345 for (int i = 0; i < avctx->ch_layout.nb_channels; i++) {
346 c->ch[i].chan_type = r[i];
347 c->ch[i].count = c->base_band_count + ((r[i] != 2) ? c->stereo_band_count : 0);
348 c->ch[i].hfr_scale = &c->ch[i].scale_factors[c->base_band_count + c->stereo_band_count];
349 if (c->ch[i].count > 128)
350 return AVERROR_INVALIDDATA;
351 }
352
353 // Done last to signal init() finished
354 c->crc_table = av_crc_get_table(AV_CRC_16_ANSI);
355
356 return 0;
357 }
358
359 static av_cold int decode_init(AVCodecContext *avctx)
360 {
361 HCAContext *c = avctx->priv_data;
362 float scale = 1.f / 8.f;
363 int ret;
364
365 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
366
367 if (avctx->ch_layout.nb_channels <= 0 || avctx->ch_layout.nb_channels > FF_ARRAY_ELEMS(c->ch))
368 return AVERROR(EINVAL);
369
370 c->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
371 if (!c->fdsp)
372 return AVERROR(ENOMEM);
373
374 ret = av_tx_init(&c->tx_ctx, &c->tx_fn, AV_TX_FLOAT_MDCT, 1, 128, &scale, 0);
375 if (ret < 0)
376 return ret;
377
378 if (avctx->extradata_size != 0 && avctx->extradata_size < 36)
379 return AVERROR_INVALIDDATA;
380
381 if (!avctx->extradata_size)
382 return 0;
383
384 return init_hca(avctx, avctx->extradata, avctx->extradata_size);
385 }
386
387 static void run_imdct(HCAContext *c, ChannelContext *ch, int index, float *out)
388 {
389 c->tx_fn(c->tx_ctx, ch->imdct_out, ch->imdct_in, sizeof(float));
390
391 c->fdsp->vector_fmul_window(out, ch->imdct_prev + (128 >> 1),
392 ch->imdct_out, window, 128 >> 1);
393
394 memcpy(ch->imdct_prev, ch->imdct_out, 128 * sizeof(float));
395 }
396
397 static void apply_intensity_stereo(HCAContext *s, ChannelContext *ch1, ChannelContext *ch2,
398 int index, unsigned band_count, unsigned base_band_count,
399 unsigned stereo_band_count)
400 {
401 float ratio_l = intensity_ratio_table[ch2->intensity[index]];
402 float ratio_r = ratio_l - 2.0f;
403 float *c1 = &ch1->imdct_in[base_band_count];
404 float *c2 = &ch2->imdct_in[base_band_count];
405
406 if (ch1->chan_type != 1 || !stereo_band_count)
407 return;
408
409 for (int i = 0; i < band_count; i++) {
410 c2[i] = c1[i] * ratio_r;
411 c1[i] *= ratio_l;
412 }
413 }
414
415 static void reconstruct_hfr(HCAContext *s, ChannelContext *ch,
416 unsigned hfr_group_count,
417 unsigned bands_per_hfr_group,
418 unsigned start_band, unsigned total_band_count)
419 {
420 if (ch->chan_type == 2 || !bands_per_hfr_group)
421 return;
422
423 for (int i = 0, k = start_band, l = start_band - 1; i < hfr_group_count; i++){
424 for (int j = 0; j < bands_per_hfr_group && k < total_band_count && l >= 0; j++, k++, l--){
425 ch->imdct_in[k] = scale_conversion_table[ scale_conv_bias +
426 av_clip_intp2(ch->hfr_scale[i] - ch->scale_factors[l], 6) ] * ch->imdct_in[l];
427 }
428 }
429
430 ch->imdct_in[127] = 0;
431 }
432
433 static void dequantize_coefficients(HCAContext *c, ChannelContext *ch,
434 GetBitContext *gb)
435 {
436 const float *base = ch->base;
437 float *factors = ch->factors;
438 float *out = ch->imdct_in;
439
440 for (int i = 0; i < ch->count; i++) {
441 unsigned scale = ch->scale[i];
442 int nb_bits = max_bits_table[scale];
443 int value = get_bitsz(gb, nb_bits);
444 float factor;
445
446 if (scale > 7) {
447 value = (1 - ((value & 1) << 1)) * (value >> 1);
448 if (!value)
449 skip_bits_long(gb, -1);
450 factor = value;
451 } else {
452 value += scale << 4;
453 skip_bits_long(gb, quant_spectrum_bits[value] - nb_bits);
454 factor = quant_spectrum_value[value];
455 }
456 factors[i] = factor;
457 }
458
459 memset(factors + ch->count, 0, 512 - ch->count * sizeof(*factors));
460 c->fdsp->vector_fmul(out, factors, base, 128);
461 }
462
463 static void unpack(HCAContext *c, ChannelContext *ch,
464 GetBitContext *gb,
465 unsigned hfr_group_count,
466 int packed_noise_level,
467 const uint8_t *ath)
468 {
469 int delta_bits = get_bits(gb, 3);
470
471 if (delta_bits > 5) {
472 for (int i = 0; i < ch->count; i++)
473 ch->scale_factors[i] = get_bits(gb, 6);
474 } else if (delta_bits) {
475 int factor = get_bits(gb, 6);
476 int max_value = (1 << delta_bits) - 1;
477 int half_max = max_value >> 1;
478
479 ch->scale_factors[0] = factor;
480 for (int i = 1; i < ch->count; i++){
481 int delta = get_bits(gb, delta_bits);
482
483 if (delta == max_value) {
484 factor = get_bits(gb, 6);
485 } else {
486 factor += delta - half_max;
487 }
488 factor = av_clip_uintp2(factor, 6);
489
490 ch->scale_factors[i] = factor;
491 }
492 } else {
493 memset(ch->scale_factors, 0, 128);
494 }
495
496 if (ch->chan_type == 2){
497 ch->intensity[0] = get_bits(gb, 4);
498 if (ch->intensity[0] < 15) {
499 for (int i = 1; i < 8; i++)
500 ch->intensity[i] = get_bits(gb, 4);
501 }
502 } else {
503 for (int i = 0; i < hfr_group_count; i++)
504 ch->hfr_scale[i] = get_bits(gb, 6);
505 }
506
507 for (int i = 0; i < ch->count; i++) {
508 int scale = ch->scale_factors[i];
509
510 if (scale) {
511 scale = c->ath[i] + ((packed_noise_level + i) >> 8) - ((scale * 5) >> 1) + 2;
512 scale = scale_table[av_clip(scale, 0, 58)];
513 }
514 ch->scale[i] = scale;
515 }
516
517 memset(ch->scale + ch->count, 0, sizeof(ch->scale) - ch->count);
518
519 for (int i = 0; i < ch->count; i++)
520 ch->base[i] = dequantizer_scaling_table[ch->scale_factors[i]] * quant_step_size[ch->scale[i]];
521 }
522
523 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
524 int *got_frame_ptr, AVPacket *avpkt)
525 {
526 HCAContext *c = avctx->priv_data;
527 int ch, offset = 0, ret, packed_noise_level;
528 GetBitContext gb0, *const gb = &gb0;
529 float **samples;
530
531 if (avpkt->size <= 8)
532 return AVERROR_INVALIDDATA;
533
534 if (AV_RN16(avpkt->data) != 0xFFFF) {
535 if ((AV_RL32(avpkt->data)) != MKTAG('H','C','A',0)) {
536 return AVERROR_INVALIDDATA;
537 } else if (AV_RB16(avpkt->data + 6) <= avpkt->size) {
538 ret = init_hca(avctx, avpkt->data, AV_RB16(avpkt->data + 6));
539 if (ret < 0)
540 return ret;
541 offset = AV_RB16(avpkt->data + 6);
542 if (offset == avpkt->size)
543 return avpkt->size;
544 } else {
545 return AVERROR_INVALIDDATA;
546 }
547 }
548
549 if (!c->crc_table)
550 return AVERROR_INVALIDDATA;
551
552 if (c->key || c->subkey) {
553 uint8_t *data, *cipher = c->cipher;
554
555 if ((ret = av_packet_make_writable(avpkt)) < 0)
556 return ret;
557 data = avpkt->data;
558 for (int n = 0; n < avpkt->size; n++)
559 data[n] = cipher[data[n]];
560 }
561
562 if (avctx->err_recognition & AV_EF_CRCCHECK) {
563 if (av_crc(c->crc_table, 0, avpkt->data + offset, avpkt->size - offset))
564 return AVERROR_INVALIDDATA;
565 }
566
567 if ((ret = init_get_bits8(gb, avpkt->data + offset, avpkt->size - offset)) < 0)
568 return ret;
569
570 if (get_bits(gb, 16) != 0xFFFF)
571 return AVERROR_INVALIDDATA;
572
573 frame->nb_samples = 1024;
574 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
575 return ret;
576 samples = (float **)frame->extended_data;
577
578 packed_noise_level = (get_bits(gb, 9) << 8) - get_bits(gb, 7);
579
580 for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
581 unpack(c, &c->ch[ch], gb, c->hfr_group_count, packed_noise_level, c->ath);
582
583 for (int i = 0; i < 8; i++) {
584 for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
585 dequantize_coefficients(c, &c->ch[ch], gb);
586 for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
587 reconstruct_hfr(c, &c->ch[ch], c->hfr_group_count, c->bands_per_hfr_group,
588 c->stereo_band_count + c->base_band_count, c->total_band_count);
589 for (ch = 0; ch < avctx->ch_layout.nb_channels - 1; ch++)
590 apply_intensity_stereo(c, &c->ch[ch], &c->ch[ch+1], i,
591 c->total_band_count - c->base_band_count,
592 c->base_band_count, c->stereo_band_count);
593 for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
594 run_imdct(c, &c->ch[ch], i, samples[ch] + i * 128);
595 }
596
597 *got_frame_ptr = 1;
598
599 return avpkt->size;
600 }
601
602 static av_cold int decode_close(AVCodecContext *avctx)
603 {
604 HCAContext *c = avctx->priv_data;
605
606 av_freep(&c->fdsp);
607 av_tx_uninit(&c->tx_ctx);
608
609 return 0;
610 }
611
612 static av_cold void decode_flush(AVCodecContext *avctx)
613 {
614 HCAContext *c = avctx->priv_data;
615
616 for (int ch = 0; ch < MAX_CHANNELS; ch++)
617 memset(c->ch[ch].imdct_prev, 0, sizeof(c->ch[ch].imdct_prev));
618 }
619
620 const FFCodec ff_hca_decoder = {
621 .p.name = "hca",
622 CODEC_LONG_NAME("CRI HCA"),
623 .p.type = AVMEDIA_TYPE_AUDIO,
624 .p.id = AV_CODEC_ID_HCA,
625 .priv_data_size = sizeof(HCAContext),
626 .init = decode_init,
627 FF_CODEC_DECODE_CB(decode_frame),
628 .flush = decode_flush,
629 .close = decode_close,
630 .p.capabilities = AV_CODEC_CAP_DR1,
631 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
632 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
633 AV_SAMPLE_FMT_NONE },
634 };
635