1 /*
2 * Copyright (C) 2017 foo86
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "libavutil/float_dsp.h"
22 #include "libavutil/thread.h"
23 #include "libavutil/mem.h"
24
25 #include "internal.h"
26 #include "get_bits.h"
27 #include "put_bits.h"
28 #include "dolby_e.h"
29 #include "fft.h"
30
skip_input(DBEContext * s,int nb_words)31 static int skip_input(DBEContext *s, int nb_words)
32 {
33 if (nb_words > s->input_size) {
34 av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n");
35 return AVERROR_INVALIDDATA;
36 }
37
38 s->input += nb_words * s->word_bytes;
39 s->input_size -= nb_words;
40 return 0;
41 }
42
parse_key(DBEContext * s)43 static int parse_key(DBEContext *s)
44 {
45 if (s->key_present) {
46 uint8_t *key = s->input;
47 int ret = skip_input(s, 1);
48 if (ret < 0)
49 return ret;
50 return AV_RB24(key) >> 24 - s->word_bits;
51 }
52 return 0;
53 }
54
convert_input(DBEContext * s,int nb_words,int key)55 static int convert_input(DBEContext *s, int nb_words, int key)
56 {
57 uint8_t *src = s->input;
58 uint8_t *dst = s->buffer;
59 PutBitContext pb;
60 int i;
61
62 av_assert0(nb_words <= 1024u);
63
64 if (nb_words > s->input_size) {
65 av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n");
66 return AVERROR_INVALIDDATA;
67 }
68
69 switch (s->word_bits) {
70 case 16:
71 for (i = 0; i < nb_words; i++, src += 2, dst += 2)
72 AV_WB16(dst, AV_RB16(src) ^ key);
73 break;
74 case 20:
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);
78 flush_put_bits(&pb);
79 break;
80 case 24:
81 for (i = 0; i < nb_words; i++, src += 3, dst += 3)
82 AV_WB24(dst, AV_RB24(src) ^ key);
83 break;
84 default:
85 av_assert0(0);
86 }
87
88 return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits);
89 }
90
parse_metadata(DBEContext * s)91 static int parse_metadata(DBEContext *s)
92 {
93 int i, ret, key, mtd_size;
94
95 if ((key = parse_key(s)) < 0)
96 return key;
97 if ((ret = convert_input(s, 1, key)) < 0)
98 return ret;
99
100 skip_bits(&s->gb, 4);
101 mtd_size = get_bits(&s->gb, 10);
102 if (!mtd_size) {
103 av_log(s->avctx, AV_LOG_ERROR, "Invalid metadata size\n");
104 return AVERROR_INVALIDDATA;
105 }
106
107 if ((ret = convert_input(s, mtd_size, key)) < 0)
108 return ret;
109
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;
115 }
116
117 s->nb_channels = nb_channels_tab[s->prog_conf];
118 s->nb_programs = nb_programs_tab[s->prog_conf];
119
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;
126 }
127
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);
133
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);
137 skip_bits1(&s->gb);
138 s->begin_gain[i] = get_bits(&s->gb, 10);
139 s->end_gain[i] = get_bits(&s->gb, 10);
140 }
141
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;
145 }
146
147 return skip_input(s, mtd_size + 1);
148 }
149
parse_metadata_ext(DBEContext * s)150 static int parse_metadata_ext(DBEContext *s)
151 {
152 if (s->mtd_ext_size)
153 return skip_input(s, s->key_present + s->mtd_ext_size + 1);
154 return 0;
155 }
156
unbias_exponents(DBEContext * s,DBEChannel * c,DBEGroup * g)157 static void unbias_exponents(DBEContext *s, DBEChannel *c, DBEGroup *g)
158 {
159 int mstr_exp[MAX_MSTR_EXP];
160 int bias_exp[MAX_BIAS_EXP];
161 int i, j, k;
162
163 for (i = 0; i < c->nb_mstr_exp; i++)
164 mstr_exp[i] = get_bits(&s->gb, 2) * 6;
165
166 for (i = 0; i < g->nb_exponent; i++)
167 bias_exp[i] = get_bits(&s->gb, 5);
168
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];
172 }
173
parse_exponents(DBEContext * s,DBEChannel * c)174 static int parse_exponents(DBEContext *s, DBEChannel *c)
175 {
176 DBEGroup *p, *g;
177 int i;
178
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);
183 } else {
184 memcpy(c->exponents + g->exp_ofs,
185 c->exponents + p->exp_ofs,
186 g->nb_exponent * sizeof(c->exponents[0]));
187 }
188 }
189
190 return 0;
191 }
192
log_add(int a,int b)193 static inline int log_add(int a, int b)
194 {
195 int c = FFABS(a - b) >> 1;
196 return FFMAX(a, b) + log_add_tab[FFMIN(c, 211)];
197 }
198
calc_lowcomp(int * msk_val)199 static void calc_lowcomp(int *msk_val)
200 {
201 int lwc_val[17] = { 0 };
202 int i, j, k;
203
204 for (i = 0; i < 11; i++) {
205 int max_j = 0;
206 int max_v = INT_MIN;
207 int thr = 0;
208
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];
211 if (v > max_v) {
212 max_j = j;
213 max_v = v;
214 }
215 thr = log_add(thr, v);
216 }
217
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];
223 }
224 }
225
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);
229 }
230 }
231
bit_allocate(int nb_exponent,int nb_code,int fr_code,int * exp,int * bap,int fg_spc,int fg_ofs,int msk_mod,int snr_ofs)232 static void bit_allocate(int nb_exponent, int nb_code, int fr_code,
233 int *exp, int *bap,
234 int fg_spc, int fg_ofs, int msk_mod, int snr_ofs)
235 {
236 int msk_val[MAX_BIAS_EXP];
237 int psd_val[MAX_BIAS_EXP];
238 int fast_leak = 0;
239 int slow_leak = 0;
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];
249 int i;
250
251 for (i = 0; i < nb_exponent; i++)
252 psd_val[i] = (48 - exp[i]) * 64;
253
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);
261 }
262
263 fast_leak = 0;
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);
267 }
268
269 for (i = 0; i < nb_exponent; i++)
270 msk_val[i] = FFMAX(msk_val[i], hearing_thresh[i]);
271
272 if (!nb_code)
273 calc_lowcomp(msk_val);
274
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)];
278 }
279 }
280
parse_bit_alloc(DBEContext * s,DBEChannel * c)281 static int parse_bit_alloc(DBEContext *s, DBEChannel *c)
282 {
283 DBEGroup *p, *g;
284 int bap_strategy[MAX_GROUPS], fg_spc[MAX_GROUPS];
285 int fg_ofs[MAX_GROUPS], msk_mod[MAX_GROUPS];
286 int i, snr_ofs;
287
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);
294 } else {
295 fg_spc[i] = fg_spc[i - 1];
296 fg_ofs[i] = fg_ofs[i - 1];
297 msk_mod[i] = msk_mod[i - 1];
298 }
299 }
300
301 if (get_bits1(&s->gb)) {
302 avpriv_report_missing_feature(s->avctx, "Delta bit allocation");
303 return AVERROR_PATCHWELCOME;
304 }
305
306 snr_ofs = get_bits(&s->gb, 8);
307 if (!snr_ofs) {
308 memset(c->bap, 0, sizeof(c->bap));
309 return 0;
310 }
311
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);
317 } else {
318 memcpy(c->bap + g->exp_ofs,
319 c->bap + p->exp_ofs,
320 g->nb_exponent * sizeof(c->bap[0]));
321 }
322 }
323
324 return 0;
325 }
326
parse_indices(DBEContext * s,DBEChannel * c)327 static int parse_indices(DBEContext *s, DBEChannel *c)
328 {
329 DBEGroup *p, *g;
330 int i, j;
331
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);
335
336 if (start > g->nb_exponent) {
337 av_log(s->avctx, AV_LOG_ERROR, "Invalid start index\n");
338 return AVERROR_INVALIDDATA;
339 }
340
341 for (j = 0; j < start; j++)
342 c->idx[g->exp_ofs + j] = 0;
343
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,
348 c->idx + p->exp_ofs,
349 g->nb_exponent * sizeof(c->idx[0]));
350 } else {
351 memset(c->idx + g->exp_ofs, 0, g->nb_exponent * sizeof(c->idx[0]));
352 }
353 }
354
355 return 0;
356 }
357
parse_mantissas(DBEContext * s,DBEChannel * c)358 static int parse_mantissas(DBEContext *s, DBEChannel *c)
359 {
360 DBEGroup *g;
361 int i, j, k;
362
363 for (i = 0, g = c->groups; i < c->nb_groups; i++, g++) {
364 float *mnt = c->mantissas + g->mnt_ofs;
365
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;
373
374 if (!size1) {
375 memset(mnt, 0, count * sizeof(*mnt));
376 } else if (idx) {
377 int values[100];
378 int escape = -(1 << size1 - 1);
379
380 for (k = 0; k < count; k++)
381 values[k] = get_sbits(&s->gb, size1);
382
383 for (k = 0; k < count; k++) {
384 if (values[k] != escape) {
385 mnt[k] = values[k] * scale;
386 } else {
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];
391 if (value < 0)
392 mnt[k] = ((value + 1) * a - b) * exp;
393 else
394 mnt[k] = (value * a + b) * exp;
395 }
396 }
397 } else {
398 for (k = 0; k < count; k++)
399 mnt[k] = get_sbits(&s->gb, size1) * scale;
400 }
401
402 mnt += count;
403 }
404
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];
408 }
409 }
410
411 return 0;
412 }
413
parse_channel(DBEContext * s,int ch,int seg_id)414 static int parse_channel(DBEContext *s, int ch, int seg_id)
415 {
416 DBEChannel *c = &s->channels[seg_id][ch];
417 int i, ret;
418
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;
422 }
423
424 if (ch == lfe_channel_tab[s->prog_conf]) {
425 c->gr_code = 3;
426 c->bw_code = 29;
427 } else {
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;
433 }
434 }
435
436 c->nb_groups = nb_groups_tab[c->gr_code];
437 c->nb_mstr_exp = nb_mstr_exp_tab[c->gr_code];
438
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;
444 }
445 }
446
447 if ((ret = parse_exponents(s, c)) < 0)
448 return ret;
449 if ((ret = parse_bit_alloc(s, c)) < 0)
450 return ret;
451 if ((ret = parse_indices(s, c)) < 0)
452 return ret;
453 if ((ret = parse_mantissas(s, c)) < 0)
454 return ret;
455
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;
459 }
460
461 return 0;
462 }
463
parse_audio(DBEContext * s,int start,int end,int seg_id)464 static int parse_audio(DBEContext *s, int start, int end, int seg_id)
465 {
466 int ch, ret, key;
467
468 if ((key = parse_key(s)) < 0)
469 return key;
470
471 for (ch = start; ch < end; ch++) {
472 if (!s->ch_size[ch]) {
473 s->channels[seg_id][ch].nb_groups = 0;
474 continue;
475 }
476 if ((ret = convert_input(s, s->ch_size[ch], key)) < 0)
477 return ret;
478 if ((ret = parse_channel(s, ch, seg_id)) < 0) {
479 if (s->avctx->err_recognition & AV_EF_EXPLODE)
480 return ret;
481 s->channels[seg_id][ch].nb_groups = 0;
482 }
483 if ((ret = skip_input(s, s->ch_size[ch])) < 0)
484 return ret;
485 }
486
487 return skip_input(s, 1);
488 }
489
parse_meter(DBEContext * s)490 static int parse_meter(DBEContext *s)
491 {
492 if (s->meter_size)
493 return skip_input(s, s->key_present + s->meter_size + 1);
494 return 0;
495 }
496
imdct_calc(DBEContext * s,DBEGroup * g,float * result,float * values)497 static void imdct_calc(DBEContext *s, DBEGroup *g, float *result, float *values)
498 {
499 FFTContext *imdct = &s->imdct[g->imdct_idx];
500 int n = 1 << imdct_bits_tab[g->imdct_idx];
501 int n2 = n >> 1;
502 int i;
503
504 switch (g->imdct_phs) {
505 case 0:
506 imdct->imdct_half(imdct, result, values);
507 for (i = 0; i < n2; i++)
508 result[n2 + i] = result[n2 - i - 1];
509 break;
510 case 1:
511 imdct->imdct_calc(imdct, result, values);
512 break;
513 case 2:
514 imdct->imdct_half(imdct, result + n2, values);
515 for (i = 0; i < n2; i++)
516 result[i] = -result[n - i - 1];
517 break;
518 default:
519 av_assert0(0);
520 }
521 }
522
transform(DBEContext * s,DBEChannel * c,float * history,float * output)523 static void transform(DBEContext *s, DBEChannel *c, float *history, float *output)
524 {
525 LOCAL_ALIGNED_32(float, buffer, [2048]);
526 LOCAL_ALIGNED_32(float, result, [1152]);
527 DBEGroup *g;
528 int i;
529
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;
535
536 imdct_calc(s, g, buffer, c->mantissas + g->mnt_ofs);
537 s->fdsp->vector_fmul_add(dst, src, win, dst, g->win_len);
538 }
539
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];
546 }
547
apply_gain(DBEContext * s,int begin,int end,float * output)548 static void apply_gain(DBEContext *s, int begin, int end, float *output)
549 {
550 if (begin == 960 && end == 960)
551 return;
552
553 if (begin == end) {
554 s->fdsp->vector_fmul_scalar(output, output, gain_tab[end], FRAME_SAMPLES);
555 } else {
556 float a = gain_tab[begin] * (1.0f / (FRAME_SAMPLES - 1));
557 float b = gain_tab[end ] * (1.0f / (FRAME_SAMPLES - 1));
558 int i;
559
560 for (i = 0; i < FRAME_SAMPLES; i++)
561 output[i] *= a * (FRAME_SAMPLES - i - 1) + b * i;
562 }
563 }
564
filter_frame(DBEContext * s,AVFrame * frame)565 static int filter_frame(DBEContext *s, AVFrame *frame)
566 {
567 const uint8_t *reorder;
568 int ch, ret;
569
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;
576 else
577 reorder = ch_reorder_n;
578
579 frame->nb_samples = FRAME_SAMPLES;
580 if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0)
581 return ret;
582
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);
588 }
589
590 return 0;
591 }
592
dolby_e_decode_frame(AVCodecContext * avctx,void * data,int * got_frame_ptr,AVPacket * avpkt)593 static int dolby_e_decode_frame(AVCodecContext *avctx, void *data,
594 int *got_frame_ptr, AVPacket *avpkt)
595 {
596 DBEContext *s = avctx->priv_data;
597 int i, j, hdr, ret;
598
599 if (avpkt->size < 3)
600 return AVERROR_INVALIDDATA;
601
602 hdr = AV_RB24(avpkt->data);
603 if ((hdr & 0xfffffe) == 0x7888e) {
604 s->word_bits = 24;
605 } else if ((hdr & 0xffffe0) == 0x788e0) {
606 s->word_bits = 20;
607 } else if ((hdr & 0xfffe00) == 0x78e00) {
608 s->word_bits = 16;
609 } else {
610 av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
611 return AVERROR_INVALIDDATA;
612 }
613
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;
618
619 if ((ret = parse_metadata(s)) < 0)
620 return ret;
621
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;
626 }
627
628 switch (s->nb_channels) {
629 case 4:
630 avctx->channel_layout = AV_CH_LAYOUT_4POINT0;
631 break;
632 case 6:
633 avctx->channel_layout = AV_CH_LAYOUT_5POINT1;
634 break;
635 case 8:
636 avctx->channel_layout = AV_CH_LAYOUT_7POINT1;
637 break;
638 }
639
640 avctx->channels = s->nb_channels;
641 avctx->sample_rate = sample_rate_tab[s->fr_code];
642 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
643
644 i = s->nb_channels / 2;
645 j = s->nb_channels;
646 if ((ret = parse_audio(s, 0, i, 0)) < 0)
647 return ret;
648 if ((ret = parse_audio(s, i, j, 0)) < 0)
649 return ret;
650 if ((ret = parse_metadata_ext(s)) < 0)
651 return ret;
652 if ((ret = parse_audio(s, 0, i, 1)) < 0)
653 return ret;
654 if ((ret = parse_audio(s, i, j, 1)) < 0)
655 return ret;
656 if ((ret = parse_meter(s)) < 0)
657 return ret;
658 if ((ret = filter_frame(s, data)) < 0)
659 return ret;
660
661 *got_frame_ptr = 1;
662 return avpkt->size;
663 }
664
dolby_e_flush(AVCodecContext * avctx)665 static av_cold void dolby_e_flush(AVCodecContext *avctx)
666 {
667 DBEContext *s = avctx->priv_data;
668
669 memset(s->history, 0, sizeof(s->history));
670 }
671
dolby_e_close(AVCodecContext * avctx)672 static av_cold int dolby_e_close(AVCodecContext *avctx)
673 {
674 DBEContext *s = avctx->priv_data;
675 int i;
676
677 for (i = 0; i < 3; i++)
678 ff_mdct_end(&s->imdct[i]);
679
680 av_freep(&s->fdsp);
681 return 0;
682 }
683
684
init_tables(void)685 static av_cold void init_tables(void)
686 {
687 int i, j;
688
689 for (i = 1; i < 17; i++)
690 mantissa_tab1[i][0] = 1.0f / (1 << i - 1);
691
692 for (i = 2; i < 16; i++) {
693 mantissa_tab1[i][1] = 1.0f / ((1 << i) - 1);
694 mantissa_tab1[i][2] = 0.5f / ((1 << i) - 1);
695 mantissa_tab1[i][3] = 0.25f / ((1 << i) - 1);
696 }
697
698 mantissa_tab1[i][1] = 0.5f / (1 << 15);
699 mantissa_tab1[i][2] = 0.75f / (1 << 15);
700 mantissa_tab1[i][3] = 0.875f / (1 << 15);
701
702 for (i = 1; i < 17; i++) {
703 mantissa_tab2[i][1] = mantissa_tab1[i][0] * 0.5f;
704 mantissa_tab2[i][2] = mantissa_tab1[i][0] * 0.75f;
705 mantissa_tab2[i][3] = mantissa_tab1[i][0] * 0.875f;
706 for (j = 1; j < 4; j++)
707 mantissa_tab3[i][j] = 1.0f / (1 << i) + 1.0f / (1 << j) - 1.0f / (1 << i + j);
708 }
709
710 mantissa_tab3[1][3] = 0.6875f;
711
712 for (i = 0; i < 25; i++) {
713 exponent_tab[i * 2 ] = 1.0f / (1 << i);
714 exponent_tab[i * 2 + 1] = M_SQRT1_2 / (1 << i);
715 }
716
717 for (i = 1; i < 1024; i++)
718 gain_tab[i] = exp2f((i - 960) / 64.0f);
719
720 // short 1
721 ff_kbd_window_init(window, 3.0f, 128);
722 for (i = 0; i < 128; i++)
723 window[128 + i] = window[127 - i];
724
725 // start
726 for (i = 0; i < 192; i++)
727 window[256 + i] = start_window[i];
728
729 // short 2
730 for (i = 0; i < 192; i++)
731 window[448 + i] = short_window2[i];
732 for (i = 0; i < 64; i++)
733 window[640 + i] = window[63 - i];
734
735 // short 3
736 for (i = 0; i < 64; i++)
737 window[704 + i] = short_window3[i];
738 for (i = 0; i < 192; i++)
739 window[768 + i] = window[64 + i];
740
741 // bridge
742 for (i = 0; i < 128; i++)
743 window[960 + i] = window[i];
744 for (i = 0; i < 64; i++)
745 window[1088 + i] = 1.0f;
746
747 // long
748 ff_kbd_window_init(window + 1408, 3.0f, 256);
749 for (i = 0; i < 640; i++)
750 window[1664 + i] = 1.0f;
751 for (i = 0; i < 256; i++)
752 window[2304 + i] = window[1152 + i] = window[1663 - i];
753
754 // reverse start
755 for (i = 0; i < 192; i++)
756 window[2560 + i] = window[447 - i];
757
758 // reverse short 2
759 for (i = 0; i < 256; i++)
760 window[2752 + i] = window[703 - i];
761
762 // reverse short 3
763 for (i = 0; i < 256; i++)
764 window[3008 + i] = window[959 - i];
765
766 // reverse bridge
767 for (i = 0; i < 448; i++)
768 window[3264 + i] = window[1407 - i];
769 }
770
dolby_e_init(AVCodecContext * avctx)771 static av_cold int dolby_e_init(AVCodecContext *avctx)
772 {
773 static AVOnce init_once = AV_ONCE_INIT;
774 DBEContext *s = avctx->priv_data;
775 int i;
776
777 if (ff_thread_once(&init_once, init_tables))
778 return AVERROR_UNKNOWN;
779
780 for (i = 0; i < 3; i++)
781 if (ff_mdct_init(&s->imdct[i], imdct_bits_tab[i], 1, 2.0) < 0)
782 return AVERROR(ENOMEM);
783
784 if (!(s->fdsp = avpriv_float_dsp_alloc(0)))
785 return AVERROR(ENOMEM);
786
787 s->multi_prog_warned = !!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE);
788 s->avctx = avctx;
789 return 0;
790 }
791
792 AVCodec ff_dolby_e_decoder = {
793 .name = "dolby_e",
794 .long_name = NULL_IF_CONFIG_SMALL("Dolby E"),
795 .type = AVMEDIA_TYPE_AUDIO,
796 .id = AV_CODEC_ID_DOLBY_E,
797 .priv_data_size = sizeof(DBEContext),
798 .init = dolby_e_init,
799 .decode = dolby_e_decode_frame,
800 .close = dolby_e_close,
801 .flush = dolby_e_flush,
802 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
803 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
804 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
805 };
806