You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

456 lines
14KB

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