32#define HCA_MASK 0x7f7f7f7f
33#define MAX_CHANNELS 16
77 const int mul = ((
key & 1) << 3) | 5;
78 const int add = (
key & 0xE) | 1;
81 for (
int i = 0;
i < 16;
i++) {
82 key = (
key * mul + add) & 0xF;
89 uint8_t
base[256], base_r[16], base_c[16], kc[8],
seed[16];
97 for (
int r = 0;
r < (8-1);
r++) {
98 kc[
r] = keycode & 0xFF;
99 keycode = keycode >> 8;
104 seed[ 1] = kc[1] ^ kc[6];
105 seed[ 2] = kc[2] ^ kc[3];
107 seed[ 4] = kc[2] ^ kc[1];
108 seed[ 5] = kc[3] ^ kc[4];
110 seed[ 7] = kc[3] ^ kc[2];
111 seed[ 8] = kc[4] ^ kc[5];
113 seed[10] = kc[4] ^ kc[3];
114 seed[11] = kc[5] ^ kc[6];
116 seed[13] = kc[5] ^ kc[4];
117 seed[14] = kc[6] ^ kc[1];
122 for (
int r = 0;
r < 16;
r++) {
126 for (
int c = 0;
c < 16;
c++)
127 base[
r*16 +
c] = nb | base_c[
c];
135 for (
int i = 0;
i < 256;
i++) {
137 if (
base[x] != 0 &&
base[x] != 0xFF)
151 keycode = keycode * (((uint64_t)subkey<<16u)|((uint16_t)~subkey+2u));
156 for (
int i = 0;
i < 256;
i++)
165 unsigned int acc = 0;
167 for (
int i = 0;
i < 128;
i++) {
172 memset(
ath+
i, 0xFF, (128 -
i));
196static inline unsigned ceil2(
unsigned a,
unsigned b)
198 return (
b > 0) ? (
a /
b + ((
a %
b) ? 1 : 0)) : 0;
209 const int extradata_size)
213 int8_t
r[16] = { 0 };
216 unsigned hfr_group_count;
220 if (extradata_size < 36)
226 version = bytestream2_get_be16(gb);
229 c->ath_type =
version >= 0x200 ? 0 : 1;
231 if ((bytestream2_get_be32u(gb) &
HCA_MASK) !=
MKBETAG(
'f',
'm',
't', 0))
237 chunk = bytestream2_get_be32u(gb) &
HCA_MASK;
238 if (chunk ==
MKBETAG(
'c',
'o',
'm',
'p')) {
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);
249 }
else if (chunk ==
MKBETAG(
'd',
'e',
'c', 0)) {
253 c->total_band_count = bytestream2_get_byteu(gb) + 1;
254 c->base_band_count = bytestream2_get_byteu(gb) + 1;
255 c->track_count = bytestream2_peek_byteu(gb) >> 4;
256 c->channel_config = bytestream2_get_byteu(gb) & 0xF;
257 if (!bytestream2_get_byteu(gb))
258 c->base_band_count =
c->total_band_count;
259 c->stereo_band_count =
c->total_band_count -
c->base_band_count;
260 c->bands_per_hfr_group = 0;
268 chunk = bytestream2_get_be32u(gb) &
HCA_MASK;
269 if (chunk ==
MKBETAG(
'v',
'b',
'r', 0)) {
271 }
else if (chunk ==
MKBETAG(
'a',
't',
'h', 0)) {
272 c->ath_type = bytestream2_get_be16(gb);
273 }
else if (chunk ==
MKBETAG(
'r',
'v',
'a', 0)) {
275 }
else if (chunk ==
MKBETAG(
'c',
'o',
'm',
'm')) {
277 }
else if (chunk ==
MKBETAG(
'c',
'i',
'p',
'h')) {
278 c->ciph_type = bytestream2_get_be16(gb);
279 }
else if (chunk ==
MKBETAG(
'l',
'o',
'o',
'p')) {
281 }
else if (chunk ==
MKBETAG(
'p',
'a',
'd', 0)) {
290 c->key = bytestream2_get_be64u(gb);
291 c->subkey = bytestream2_get_be16u(gb);
304 if (
c->stereo_band_count &&
b > 1) {
307 for (
int i = 0;
i <
c->track_count;
i++, x+=
b) {
316 if (
c->channel_config == 0) {
323 if (
c->channel_config <= 2) {
330 x[0] = 1; x[1] = 2; x[4] = 1; x[5] = 2;
333 x[0] = 1; x[1] = 2; x[4] = 1; x[5] = 2; x[6] = 1; x[7] = 2;
339 if (
c->total_band_count <
c->base_band_count)
342 hfr_group_count =
ceil2(
c->total_band_count - (
c->base_band_count +
c->stereo_band_count),
343 c->bands_per_hfr_group);
345 if (
c->base_band_count +
c->stereo_band_count + (uint64_t)hfr_group_count > 128ULL)
347 c->hfr_group_count = hfr_group_count;
350 c->ch[
i].chan_type =
r[
i];
351 c->ch[
i].count =
c->base_band_count + ((
r[
i] != 2) ?
c->stereo_band_count : 0);
352 c->ch[
i].hfr_scale = &
c->ch[
i].scale_factors[
c->base_band_count +
c->stereo_band_count];
353 if (
c->ch[
i].count > 128)
366 float scale = 1.f / 8.f;
402 int index,
unsigned band_count,
unsigned base_band_count,
403 unsigned stereo_band_count)
406 float ratio_r = ratio_l - 2.0f;
410 if (ch1->
chan_type != 1 || !stereo_band_count)
413 for (
int i = 0;
i < band_count;
i++) {
420 unsigned hfr_group_count,
421 unsigned bands_per_hfr_group,
422 unsigned start_band,
unsigned total_band_count)
424 if (ch->
chan_type == 2 || !bands_per_hfr_group)
427 for (
int i = 0, k = start_band, l = start_band - 1;
i < hfr_group_count;
i++){
428 for (
int j = 0; j < bands_per_hfr_group && k < total_band_count && l >= 0; j++, k++, l--){
444 for (
int i = 0;
i < ch->
count;
i++) {
463 memset(factors + ch->
count, 0, 512 - ch->
count *
sizeof(*factors));
464 c->fdsp->vector_fmul(
out, factors,
base, 128);
469 unsigned hfr_group_count,
470 int packed_noise_level,
475 if (delta_bits > 5) {
478 }
else if (delta_bits) {
480 int max_value = (1 << delta_bits) - 1;
481 int half_max = max_value >> 1;
484 for (
int i = 1;
i < ch->
count;
i++){
487 if (
delta == max_value) {
503 for (
int i = 1;
i < 8;
i++)
507 for (
int i = 0;
i < hfr_group_count;
i++)
511 for (
int i = 0;
i < ch->
count;
i++) {
515 scale =
c->ath[
i] + ((packed_noise_level +
i) >> 8) - ((
scale * 5) >> 1) + 2;
528 int *got_frame_ptr,
AVPacket *avpkt)
531 int ch,
offset = 0, ret, packed_noise_level;
535 if (avpkt->
size <= 8)
558 if (
c->key ||
c->subkey) {
559 uint8_t *
data, *cipher =
c->cipher;
564 for (
int n = 0; n < avpkt->
size; n++)
579 frame->nb_samples = 1024;
582 samples = (
float **)
frame->extended_data;
587 unpack(
c, &
c->ch[ch], gb,
c->hfr_group_count, packed_noise_level,
c->ath);
589 for (
int i = 0;
i < 8;
i++) {
594 c->stereo_band_count +
c->base_band_count,
c->total_band_count);
597 c->total_band_count -
c->base_band_count,
598 c->base_band_count,
c->stereo_band_count);
623 memset(
c->ch[ch].imdct_prev, 0,
sizeof(
c->ch[ch].imdct_prev));
static av_cold float ath(float f, float add)
Calculate ATH value for given frequency.
static av_cold void decode_flush(AVCodecContext *avctx)
const FFCodec ff_hca_decoder
Libavcodec external API header.
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
#define i(width, name, range_min, range_max)
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Public header for CRC hash function implementation.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
int(* init)(AVBSFContext *ctx)
static SDL_Window * window
bitstream reader API header.
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
int av_packet_make_writable(AVPacket *pkt)
Create a writable reference for the data described by a given packet, avoiding data copy if possible.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
@ AV_SAMPLE_FMT_FLTP
float, planar
static const int8_t quant_spectrum_value[]
static const float quant_step_size[]
static const uint8_t max_bits_table[]
static const int scale_conv_bias
static const uint8_t quant_spectrum_bits[]
static const float scale_conversion_table[]
static const uint8_t scale_table[]
static const uint8_t ath_base_curve[656]
static const float intensity_ratio_table[]
static const float dequantizer_scaling_table[]
static av_cold void decode_flush(AVCodecContext *avctx)
static void dequantize_coefficients(HCAContext *c, ChannelContext *ch, GetBitContext *gb)
static void unpack(HCAContext *c, ChannelContext *ch, GetBitContext *gb, unsigned hfr_group_count, int packed_noise_level, const uint8_t *ath)
static void ath_init1(uint8_t *ath, int sample_rate)
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
static void cipher_init56(uint8_t *cipher, uint64_t keycode)
static void cipher_init(uint8_t *cipher, int type, uint64_t keycode, uint16_t subkey)
static void run_imdct(HCAContext *c, ChannelContext *ch, int index, float *out)
static av_cold int decode_close(AVCodecContext *avctx)
static av_cold int decode_init(AVCodecContext *avctx)
static av_cold void init_flush(AVCodecContext *avctx)
static unsigned ceil2(unsigned a, unsigned b)
static void cipher_init56_create_table(uint8_t *r, uint8_t key)
static void apply_intensity_stereo(HCAContext *s, ChannelContext *ch1, ChannelContext *ch2, int index, unsigned band_count, unsigned base_band_count, unsigned stereo_band_count)
static int ath_init(uint8_t *ath, int type, int sample_rate)
static int init_hca(AVCodecContext *avctx, const uint8_t *extradata, const int extradata_size)
static void reconstruct_hfr(HCAContext *s, ChannelContext *ch, unsigned hfr_group_count, unsigned bands_per_hfr_group, unsigned start_band, unsigned total_band_count)
static void scale(int *out, const int *in, const int w, const int h, const int shift)
static av_cold int decode_init(AVCodecContext *avctx)
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
static av_cold int decode_close(AVCodecContext *avctx)
static const int factor[16]
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
#define MKTAG(a, b, c, d)
#define MKBETAG(a, b, c, d)
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
#define FF_ARRAY_ELEMS(a)
int nb_channels
Number of channels in this layout.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
enum AVSampleFormat sample_fmt
audio sample format
int sample_rate
samples per second
int flags
AV_CODEC_FLAG_*.
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
int8_t scale_factors[128]
ChannelContext ch[MAX_CHANNELS]
uint8_t bands_per_hfr_group
uint8_t stereo_band_count
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respectively.
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.