FFmpeg coverage


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