2 * Copyright (C) 2017 foo86
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/float_dsp.h"
22 #include "libavutil/thread.h"
23 #include "libavutil/mem.h"
31 static int skip_input(DBEContext *s, int nb_words)
33 if (nb_words > s->input_size) {
34 av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n");
35 return AVERROR_INVALIDDATA;
38 s->input += nb_words * s->word_bytes;
39 s->input_size -= nb_words;
43 static int parse_key(DBEContext *s)
46 uint8_t *key = s->input;
47 int ret = skip_input(s, 1);
50 return AV_RB24(key) >> 24 - s->word_bits;
55 static int convert_input(DBEContext *s, int nb_words, int key)
57 uint8_t *src = s->input;
58 uint8_t *dst = s->buffer;
62 av_assert0(nb_words <= 1024u);
64 if (nb_words > s->input_size) {
65 av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n");
66 return AVERROR_INVALIDDATA;
69 switch (s->word_bits) {
71 for (i = 0; i < nb_words; i++, src += 2, dst += 2)
72 AV_WB16(dst, AV_RB16(src) ^ key);
75 init_put_bits(&pb, s->buffer, sizeof(s->buffer));
76 for (i = 0; i < nb_words; i++, src += 3)
77 put_bits(&pb, 20, AV_RB24(src) >> 4 ^ key);
81 for (i = 0; i < nb_words; i++, src += 3, dst += 3)
82 AV_WB24(dst, AV_RB24(src) ^ key);
88 return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits);
91 static int parse_metadata(DBEContext *s)
93 int i, ret, key, mtd_size;
95 if ((key = parse_key(s)) < 0)
97 if ((ret = convert_input(s, 1, key)) < 0)
100 skip_bits(&s->gb, 4);
101 mtd_size = get_bits(&s->gb, 10);
103 av_log(s->avctx, AV_LOG_ERROR, "Invalid metadata size\n");
104 return AVERROR_INVALIDDATA;
107 if ((ret = convert_input(s, mtd_size, key)) < 0)
110 skip_bits(&s->gb, 14);
111 s->prog_conf = get_bits(&s->gb, 6);
112 if (s->prog_conf > MAX_PROG_CONF) {
113 av_log(s->avctx, AV_LOG_ERROR, "Invalid program configuration\n");
114 return AVERROR_INVALIDDATA;
117 s->nb_channels = nb_channels_tab[s->prog_conf];
118 s->nb_programs = nb_programs_tab[s->prog_conf];
120 s->fr_code = get_bits(&s->gb, 4);
121 s->fr_code_orig = get_bits(&s->gb, 4);
122 if (!sample_rate_tab[s->fr_code] ||
123 !sample_rate_tab[s->fr_code_orig]) {
124 av_log(s->avctx, AV_LOG_ERROR, "Invalid frame rate code\n");
125 return AVERROR_INVALIDDATA;
128 skip_bits_long(&s->gb, 88);
129 for (i = 0; i < s->nb_channels; i++)
130 s->ch_size[i] = get_bits(&s->gb, 10);
131 s->mtd_ext_size = get_bits(&s->gb, 8);
132 s->meter_size = get_bits(&s->gb, 8);
134 skip_bits_long(&s->gb, 10 * s->nb_programs);
135 for (i = 0; i < s->nb_channels; i++) {
136 s->rev_id[i] = get_bits(&s->gb, 4);
138 s->begin_gain[i] = get_bits(&s->gb, 10);
139 s->end_gain[i] = get_bits(&s->gb, 10);
142 if (get_bits_left(&s->gb) < 0) {
143 av_log(s->avctx, AV_LOG_ERROR, "Read past end of metadata\n");
144 return AVERROR_INVALIDDATA;
147 return skip_input(s, mtd_size + 1);
150 static int parse_metadata_ext(DBEContext *s)
153 return skip_input(s, s->key_present + s->mtd_ext_size + 1);
157 static void unbias_exponents(DBEContext *s, DBEChannel *c, DBEGroup *g)
159 int mstr_exp[MAX_MSTR_EXP];
160 int bias_exp[MAX_BIAS_EXP];
163 for (i = 0; i < c->nb_mstr_exp; i++)
164 mstr_exp[i] = get_bits(&s->gb, 2) * 6;
166 for (i = 0; i < g->nb_exponent; i++)
167 bias_exp[i] = get_bits(&s->gb, 5);
169 for (i = k = 0; i < c->nb_mstr_exp; i++)
170 for (j = 0; j < g->nb_bias_exp[i]; j++, k++)
171 c->exponents[g->exp_ofs + k] = mstr_exp[i] + bias_exp[k];
174 static int parse_exponents(DBEContext *s, DBEChannel *c)
179 for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
180 c->exp_strategy[i] = !i || g->nb_exponent != p->nb_exponent || get_bits1(&s->gb);
181 if (c->exp_strategy[i]) {
182 unbias_exponents(s, c, g);
184 memcpy(c->exponents + g->exp_ofs,
185 c->exponents + p->exp_ofs,
186 g->nb_exponent * sizeof(c->exponents[0]));
193 static inline int log_add(int a, int b)
195 int c = FFABS(a - b) >> 1;
196 return FFMAX(a, b) + log_add_tab[FFMIN(c, 211)];
199 static void calc_lowcomp(int *msk_val)
201 int lwc_val[17] = { 0 };
204 for (i = 0; i < 11; i++) {
209 for (j = FFMAX(i - 3, 0), k = 0; j <= i + 3; j++, k++) {
210 int v = msk_val[j] + lwc_gain_tab[i][k];
215 thr = log_add(thr, v);
218 if (msk_val[i] < thr) {
219 for (j = FFMAX(max_j - 3, 0),
220 k = FFMAX(3 - max_j, 0);
221 j <= max_j + 3; j++, k++)
222 lwc_val[j] += lwc_adj_tab[k];
226 for (i = 0; i < 16; i++) {
227 int v = FFMAX(lwc_val[i], -512);
228 msk_val[i] = FFMAX(msk_val[i] + v, 0);
232 static void bit_allocate(int nb_exponent, int nb_code, int fr_code,
234 int fg_spc, int fg_ofs, int msk_mod, int snr_ofs)
236 int msk_val[MAX_BIAS_EXP];
237 int psd_val[MAX_BIAS_EXP];
240 int dc_code = dc_code_tab[fr_code - 1];
241 int ht_code = ht_code_tab[fr_code - 1];
242 int fast_gain = fast_gain_tab[fg_ofs];
243 int slow_decay = slow_decay_tab[dc_code][msk_mod];
244 int misc_decay = misc_decay_tab[nb_code][dc_code][msk_mod];
245 const uint16_t *slow_gain = slow_gain_tab[nb_code][msk_mod];
246 const uint16_t *fast_decay = fast_decay_tab[nb_code][dc_code][msk_mod];
247 const uint16_t *fast_gain_adj = fast_gain_adj_tab[nb_code][dc_code];
248 const uint16_t *hearing_thresh = hearing_thresh_tab[nb_code][ht_code];
251 for (i = 0; i < nb_exponent; i++)
252 psd_val[i] = (48 - exp[i]) * 64;
254 fast_gain_adj += band_ofs_tab[nb_code][fg_spc];
255 for (i = 0; i < nb_exponent; i++) {
256 fast_leak = log_add(fast_leak - fast_decay[i],
257 psd_val[i] - fast_gain + fast_gain_adj[i]);
258 slow_leak = log_add(slow_leak - slow_decay,
259 psd_val[i] - slow_gain[i]);
260 msk_val[i] = FFMAX(fast_leak, slow_leak);
264 for (i = nb_exponent - 1; i > band_low_tab[nb_code]; i--) {
265 fast_leak = log_add(fast_leak - misc_decay, psd_val[i] - fast_gain);
266 msk_val[i] = FFMAX(msk_val[i], fast_leak);
269 for (i = 0; i < nb_exponent; i++)
270 msk_val[i] = FFMAX(msk_val[i], hearing_thresh[i]);
273 calc_lowcomp(msk_val);
275 for (i = 0; i < nb_exponent; i++) {
276 int v = 16 * (snr_ofs - 64) + psd_val[i] - msk_val[i] >> 5;
277 bap[i] = bap_tab[av_clip_uintp2(v, 6)];
281 static int parse_bit_alloc(DBEContext *s, DBEChannel *c)
284 int bap_strategy[MAX_GROUPS], fg_spc[MAX_GROUPS];
285 int fg_ofs[MAX_GROUPS], msk_mod[MAX_GROUPS];
288 for (i = 0; i < c->nb_groups; i++) {
289 bap_strategy[i] = !i || get_bits1(&s->gb);
290 if (bap_strategy[i]) {
291 fg_spc[i] = get_bits(&s->gb, 2);
292 fg_ofs[i] = get_bits(&s->gb, 3);
293 msk_mod[i] = get_bits1(&s->gb);
295 fg_spc[i] = fg_spc[i - 1];
296 fg_ofs[i] = fg_ofs[i - 1];
297 msk_mod[i] = msk_mod[i - 1];
301 if (get_bits1(&s->gb)) {
302 avpriv_report_missing_feature(s->avctx, "Delta bit allocation");
303 return AVERROR_PATCHWELCOME;
306 snr_ofs = get_bits(&s->gb, 8);
308 memset(c->bap, 0, sizeof(c->bap));
312 for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
313 if (c->exp_strategy[i] || bap_strategy[i]) {
314 bit_allocate(g->nb_exponent, g->imdct_idx, s->fr_code,
315 c->exponents + g->exp_ofs, c->bap + g->exp_ofs,
316 fg_spc[i], fg_ofs[i], msk_mod[i], snr_ofs);
318 memcpy(c->bap + g->exp_ofs,
320 g->nb_exponent * sizeof(c->bap[0]));
327 static int parse_indices(DBEContext *s, DBEChannel *c)
332 for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
333 if (get_bits1(&s->gb)) {
334 int start = get_bits(&s->gb, 6);
336 if (start > g->nb_exponent) {
337 av_log(s->avctx, AV_LOG_ERROR, "Invalid start index\n");
338 return AVERROR_INVALIDDATA;
341 for (j = 0; j < start; j++)
342 c->idx[g->exp_ofs + j] = 0;
344 for (; j < g->nb_exponent; j++)
345 c->idx[g->exp_ofs + j] = get_bits(&s->gb, 2);
346 } else if (i && g->nb_exponent == p->nb_exponent) {
347 memcpy(c->idx + g->exp_ofs,
349 g->nb_exponent * sizeof(c->idx[0]));
351 memset(c->idx + g->exp_ofs, 0, g->nb_exponent * sizeof(c->idx[0]));
358 static int parse_mantissas(DBEContext *s, DBEChannel *c)
363 for (i = 0, g = c->groups; i < c->nb_groups; i++, g++) {
364 float *mnt = c->mantissas + g->mnt_ofs;
366 for (j = 0; j < g->nb_exponent; j++) {
367 int bap = c->bap[g->exp_ofs + j];
368 int idx = c->idx[g->exp_ofs + j];
369 int size1 = mantissa_size1[bap][idx];
370 int count = g->nb_mantissa[j];
371 float exp = exponent_tab[c->exponents[g->exp_ofs + j]];
372 float scale = mantissa_tab1[size1][idx] * exp;
375 memset(mnt, 0, count * sizeof(*mnt));
378 int escape = -(1 << size1 - 1);
380 for (k = 0; k < count; k++)
381 values[k] = get_sbits(&s->gb, size1);
383 for (k = 0; k < count; k++) {
384 if (values[k] != escape) {
385 mnt[k] = values[k] * scale;
387 int size2 = mantissa_size2[bap][idx];
388 int value = get_sbits(&s->gb, size2);
389 float a = mantissa_tab2[size2][idx];
390 float b = mantissa_tab3[size2][idx];
392 mnt[k] = ((value + 1) * a - b) * exp;
394 mnt[k] = (value * a + b) * exp;
398 for (k = 0; k < count; k++)
399 mnt[k] = get_sbits(&s->gb, size1) * scale;
405 for (; j < g->nb_exponent + c->bw_code; j++) {
406 memset(mnt, 0, g->nb_mantissa[j] * sizeof(*mnt));
407 mnt += g->nb_mantissa[j];
414 static int parse_channel(DBEContext *s, int ch, int seg_id)
416 DBEChannel *c = &s->channels[seg_id][ch];
419 if (s->rev_id[ch] > 1) {
420 avpriv_report_missing_feature(s->avctx, "Encoder revision %d", s->rev_id[ch]);
421 return AVERROR_PATCHWELCOME;
424 if (ch == lfe_channel_tab[s->prog_conf]) {
428 c->gr_code = get_bits(&s->gb, 2);
429 c->bw_code = get_bits(&s->gb, 3);
430 if (c->gr_code == 3) {
431 av_log(s->avctx, AV_LOG_ERROR, "Invalid group type code\n");
432 return AVERROR_INVALIDDATA;
436 c->nb_groups = nb_groups_tab[c->gr_code];
437 c->nb_mstr_exp = nb_mstr_exp_tab[c->gr_code];
439 for (i = 0; i < c->nb_groups; i++) {
440 c->groups[i] = frm_ofs_tab[seg_id][c->gr_code][i];
441 if (c->nb_mstr_exp == 2) {
442 c->groups[i].nb_exponent -= c->bw_code;
443 c->groups[i].nb_bias_exp[1] -= c->bw_code;
447 if ((ret = parse_exponents(s, c)) < 0)
449 if ((ret = parse_bit_alloc(s, c)) < 0)
451 if ((ret = parse_indices(s, c)) < 0)
453 if ((ret = parse_mantissas(s, c)) < 0)
456 if (get_bits_left(&s->gb) < 0) {
457 av_log(s->avctx, AV_LOG_ERROR, "Read past end of channel %d\n", ch);
458 return AVERROR_INVALIDDATA;
464 static int parse_audio(DBEContext *s, int start, int end, int seg_id)
468 if ((key = parse_key(s)) < 0)
471 for (ch = start; ch < end; ch++) {
472 if (!s->ch_size[ch]) {
473 s->channels[seg_id][ch].nb_groups = 0;
476 if ((ret = convert_input(s, s->ch_size[ch], key)) < 0)
478 if ((ret = parse_channel(s, ch, seg_id)) < 0) {
479 if (s->avctx->err_recognition & AV_EF_EXPLODE)
481 s->channels[seg_id][ch].nb_groups = 0;
483 if ((ret = skip_input(s, s->ch_size[ch])) < 0)
487 return skip_input(s, 1);
490 static int parse_meter(DBEContext *s)
493 return skip_input(s, s->key_present + s->meter_size + 1);
497 static void imdct_calc(DBEContext *s, DBEGroup *g, float *result, float *values)
499 FFTContext *imdct = &s->imdct[g->imdct_idx];
500 int n = 1 << imdct_bits_tab[g->imdct_idx];
504 switch (g->imdct_phs) {
506 imdct->imdct_half(imdct, result, values);
507 for (i = 0; i < n2; i++)
508 result[n2 + i] = result[n2 - i - 1];
511 imdct->imdct_calc(imdct, result, values);
514 imdct->imdct_half(imdct, result + n2, values);
515 for (i = 0; i < n2; i++)
516 result[i] = -result[n - i - 1];
523 static void transform(DBEContext *s, DBEChannel *c, float *history, float *output)
525 LOCAL_ALIGNED_32(float, buffer, [2048]);
526 LOCAL_ALIGNED_32(float, result, [1152]);
530 memset(result, 0, 1152 * sizeof(float));
531 for (i = 0, g = c->groups; i < c->nb_groups; i++, g++) {
532 float *src = buffer + g->src_ofs;
533 float *dst = result + g->dst_ofs;
534 float *win = window + g->win_ofs;
536 imdct_calc(s, g, buffer, c->mantissas + g->mnt_ofs);
537 s->fdsp->vector_fmul_add(dst, src, win, dst, g->win_len);
540 for (i = 0; i < 256; i++)
541 output[i] = history[i] + result[i];
542 for (i = 256; i < 896; i++)
543 output[i] = result[i];
544 for (i = 0; i < 256; i++)
545 history[i] = result[896 + i];
548 static void apply_gain(DBEContext *s, int begin, int end, float *output)
550 if (begin == 960 && end == 960)
554 s->fdsp->vector_fmul_scalar(output, output, gain_tab[end], FRAME_SAMPLES);
556 float a = gain_tab[begin] * (1.0f / (FRAME_SAMPLES - 1));
557 float b = gain_tab[end ] * (1.0f / (FRAME_SAMPLES - 1));
560 for (i = 0; i < FRAME_SAMPLES; i++)
561 output[i] *= a * (FRAME_SAMPLES - i - 1) + b * i;
565 static int filter_frame(DBEContext *s, AVFrame *frame)
567 const uint8_t *reorder;
570 if (s->nb_channels == 4)
571 reorder = ch_reorder_4;
572 else if (s->nb_channels == 6)
573 reorder = ch_reorder_6;
574 else if (s->nb_programs == 1 && !(s->avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE))
575 reorder = ch_reorder_8;
577 reorder = ch_reorder_n;
579 frame->nb_samples = FRAME_SAMPLES;
580 if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0)
583 for (ch = 0; ch < s->nb_channels; ch++) {
584 float *output = (float *)frame->extended_data[reorder[ch]];
585 transform(s, &s->channels[0][ch], s->history[ch], output);
586 transform(s, &s->channels[1][ch], s->history[ch], output + FRAME_SAMPLES / 2);
587 apply_gain(s, s->begin_gain[ch], s->end_gain[ch], output);
593 static int dolby_e_decode_frame(AVCodecContext *avctx, void *data,
594 int *got_frame_ptr, AVPacket *avpkt)
596 DBEContext *s = avctx->priv_data;
600 return AVERROR_INVALIDDATA;
602 hdr = AV_RB24(avpkt->data);
603 if ((hdr & 0xfffffe) == 0x7888e) {
605 } else if ((hdr & 0xffffe0) == 0x788e0) {
607 } else if ((hdr & 0xfffe00) == 0x78e00) {
610 av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
611 return AVERROR_INVALIDDATA;
614 s->word_bytes = s->word_bits + 7 >> 3;
615 s->input = avpkt->data + s->word_bytes;
616 s->input_size = avpkt->size / s->word_bytes - 1;
617 s->key_present = hdr >> 24 - s->word_bits & 1;
619 if ((ret = parse_metadata(s)) < 0)
622 if (s->nb_programs > 1 && !s->multi_prog_warned) {
623 av_log(avctx, AV_LOG_WARNING, "Stream has %d programs (configuration %d), "
624 "channels will be output in native order.\n", s->nb_programs, s->prog_conf);
625 s->multi_prog_warned = 1;
628 switch (s->nb_channels) {
630 avctx->channel_layout = AV_CH_LAYOUT_4POINT0;
633 avctx->channel_layout = AV_CH_LAYOUT_5POINT1;
636 avctx->channel_layout = AV_CH_LAYOUT_7POINT1;
640 avctx->channels = s->nb_channels;
641 avctx->sample_rate = sample_rate_tab[s->fr_code];
642 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
644 i = s->nb_channels / 2;
646 if ((ret = parse_audio(s, 0, i, 0)) < 0)
648 if ((ret = parse_audio(s, i, j, 0)) < 0)
650 if ((ret = parse_metadata_ext(s)) < 0)
652 if ((ret = parse_audio(s, 0, i, 1)) < 0)
654 if ((ret = parse_audio(s, i, j, 1)) < 0)
656 if ((ret = parse_meter(s)) < 0)
658 if ((ret = filter_frame(s, data)) < 0)
665 static av_cold void dolby_e_flush(AVCodecContext *avctx)
667 DBEContext *s = avctx->priv_data;
669 memset(s->history, 0, sizeof(s->history));
672 static av_cold int dolby_e_close(AVCodecContext *avctx)
674 DBEContext *s = avctx->priv_data;
677 for (i = 0; i < 3; i++)
678 ff_mdct_end(&s->imdct[i]);
684 static av_cold int dolby_e_init(AVCodecContext *avctx)
686 static AVOnce init_once = AV_ONCE_INIT;
687 DBEContext *s = avctx->priv_data;
690 if (ff_thread_once(&init_once, init_tables))
691 return AVERROR_UNKNOWN;
693 for (i = 0; i < 3; i++)
694 if (ff_mdct_init(&s->imdct[i], imdct_bits_tab[i], 1, 2.0) < 0)
695 return AVERROR(ENOMEM);
697 if (!(s->fdsp = avpriv_float_dsp_alloc(0)))
698 return AVERROR(ENOMEM);
700 s->multi_prog_warned = !!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE);
705 AVCodec ff_dolby_e_decoder = {
707 .long_name = NULL_IF_CONFIG_SMALL("Dolby E"),
708 .type = AVMEDIA_TYPE_AUDIO,
709 .id = AV_CODEC_ID_DOLBY_E,
710 .priv_data_size = sizeof(DBEContext),
711 .init = dolby_e_init,
712 .decode = dolby_e_decode_frame,
713 .close = dolby_e_close,
714 .flush = dolby_e_flush,
715 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
716 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
717 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,