GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/dolby_e.c Lines: 382 477 80.1 %
Date: 2021-01-21 13:05:02 Branches: 216 290 74.5 %

Line Branch Exec Source
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
#include "libavutil/mem_internal.h"
25
26
#include "internal.h"
27
#include "get_bits.h"
28
#include "put_bits.h"
29
#include "dolby_e.h"
30
#include "fft.h"
31
32
6168
static int skip_input(DBEContext *s, int nb_words)
33
{
34
6168
    if (nb_words > s->input_size) {
35
        av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n");
36
        return AVERROR_INVALIDDATA;
37
    }
38
39
6168
    s->input      += nb_words * s->word_bytes;
40
6168
    s->input_size -= nb_words;
41
6168
    return 0;
42
}
43
44
1285
static int parse_key(DBEContext *s)
45
{
46
1285
    if (s->key_present) {
47
1285
        uint8_t *key = s->input;
48
1285
        int      ret = skip_input(s, 1);
49
1285
        if (ret < 0)
50
            return ret;
51
1285
        return AV_RB24(key) >> 24 - s->word_bits;
52
    }
53
    return 0;
54
}
55
56
3598
static int convert_input(DBEContext *s, int nb_words, int key)
57
{
58
3598
    uint8_t *src = s->input;
59
3598
    uint8_t *dst = s->buffer;
60
    PutBitContext pb;
61
    int i;
62
63
3598
    av_assert0(nb_words <= 1024u);
64
65
3598
    if (nb_words > s->input_size) {
66
        av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n");
67
        return AVERROR_INVALIDDATA;
68
    }
69
70

3598
    switch (s->word_bits) {
71
3598
    case 16:
72
934452
        for (i = 0; i < nb_words; i++, src += 2, dst += 2)
73
930854
            AV_WB16(dst, AV_RB16(src) ^ key);
74
3598
        break;
75
    case 20:
76
        init_put_bits(&pb, s->buffer, sizeof(s->buffer));
77
        for (i = 0; i < nb_words; i++, src += 3)
78
            put_bits(&pb, 20, AV_RB24(src) >> 4 ^ key);
79
        flush_put_bits(&pb);
80
        break;
81
    case 24:
82
        for (i = 0; i < nb_words; i++, src += 3, dst += 3)
83
            AV_WB24(dst, AV_RB24(src) ^ key);
84
        break;
85
    default:
86
        av_assert0(0);
87
    }
88
89
3598
    return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits);
90
}
91
92
257
static int parse_metadata(DBEContext *s)
93
{
94
    int i, ret, key, mtd_size;
95
96
257
    if ((key = parse_key(s)) < 0)
97
        return key;
98
257
    if ((ret = convert_input(s, 1, key)) < 0)
99
        return ret;
100
101
257
    skip_bits(&s->gb, 4);
102
257
    mtd_size = get_bits(&s->gb, 10);
103
257
    if (!mtd_size) {
104
        av_log(s->avctx, AV_LOG_ERROR, "Invalid metadata size\n");
105
        return AVERROR_INVALIDDATA;
106
    }
107
108
257
    if ((ret = convert_input(s, mtd_size, key)) < 0)
109
        return ret;
110
111
257
    skip_bits(&s->gb, 14);
112
257
    s->prog_conf = get_bits(&s->gb, 6);
113
257
    if (s->prog_conf > MAX_PROG_CONF) {
114
        av_log(s->avctx, AV_LOG_ERROR, "Invalid program configuration\n");
115
        return AVERROR_INVALIDDATA;
116
    }
117
118
257
    s->nb_channels = nb_channels_tab[s->prog_conf];
119
257
    s->nb_programs = nb_programs_tab[s->prog_conf];
120
121
257
    s->fr_code      = get_bits(&s->gb, 4);
122
257
    s->fr_code_orig = get_bits(&s->gb, 4);
123
257
    if (!sample_rate_tab[s->fr_code] ||
124
257
        !sample_rate_tab[s->fr_code_orig]) {
125
        av_log(s->avctx, AV_LOG_ERROR, "Invalid frame rate code\n");
126
        return AVERROR_INVALIDDATA;
127
    }
128
129
257
    skip_bits_long(&s->gb, 88);
130
1799
    for (i = 0; i < s->nb_channels; i++)
131
1542
        s->ch_size[i] = get_bits(&s->gb, 10);
132
257
    s->mtd_ext_size = get_bits(&s->gb, 8);
133
257
    s->meter_size   = get_bits(&s->gb, 8);
134
135
257
    skip_bits_long(&s->gb, 10 * s->nb_programs);
136
1799
    for (i = 0; i < s->nb_channels; i++) {
137
1542
        s->rev_id[i]     = get_bits(&s->gb,  4);
138
1542
        skip_bits1(&s->gb);
139
1542
        s->begin_gain[i] = get_bits(&s->gb, 10);
140
1542
        s->end_gain[i]   = get_bits(&s->gb, 10);
141
    }
142
143
257
    if (get_bits_left(&s->gb) < 0) {
144
        av_log(s->avctx, AV_LOG_ERROR, "Read past end of metadata\n");
145
        return AVERROR_INVALIDDATA;
146
    }
147
148
257
    return skip_input(s, mtd_size + 1);
149
}
150
151
257
static int parse_metadata_ext(DBEContext *s)
152
{
153
257
    if (s->mtd_ext_size)
154
257
        return skip_input(s, s->key_present + s->mtd_ext_size + 1);
155
    return 0;
156
}
157
158
3112
static void unbias_exponents(DBEContext *s, DBEChannel *c, DBEGroup *g)
159
{
160
    int mstr_exp[MAX_MSTR_EXP];
161
    int bias_exp[MAX_BIAS_EXP];
162
    int i, j, k;
163
164
8822
    for (i = 0; i < c->nb_mstr_exp; i++)
165
5710
        mstr_exp[i] = get_bits(&s->gb, 2) * 6;
166
167
143104
    for (i = 0; i < g->nb_exponent; i++)
168
139992
        bias_exp[i] = get_bits(&s->gb, 5);
169
170
8822
    for (i = k = 0; i < c->nb_mstr_exp; i++)
171
145702
        for (j = 0; j < g->nb_bias_exp[i]; j++, k++)
172
139992
            c->exponents[g->exp_ofs + k] = mstr_exp[i] + bias_exp[k];
173
3112
}
174
175
3084
static int parse_exponents(DBEContext *s, DBEChannel *c)
176
{
177
    DBEGroup *p, *g;
178
    int i;
179
180
6314
    for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
181

3230
        c->exp_strategy[i] = !i || g->nb_exponent != p->nb_exponent || get_bits1(&s->gb);
182
3230
        if (c->exp_strategy[i]) {
183
3112
            unbias_exponents(s, c, g);
184
        } else {
185
118
            memcpy(c->exponents + g->exp_ofs,
186
118
                   c->exponents + p->exp_ofs,
187
118
                   g->nb_exponent * sizeof(c->exponents[0]));
188
        }
189
    }
190
191
3084
    return 0;
192
}
193
194
348066
static inline int log_add(int a, int b)
195
{
196
348066
    int c = FFABS(a - b) >> 1;
197
348066
    return FFMAX(a, b) + log_add_tab[FFMIN(c, 211)];
198
}
199
200
43
static void calc_lowcomp(int *msk_val)
201
{
202
43
    int lwc_val[17] = { 0 };
203
    int i, j, k;
204
205
516
    for (i = 0; i < 11; i++) {
206
473
        int max_j = 0;
207
473
        int max_v = INT_MIN;
208
473
        int thr   = 0;
209
210
3526
        for (j = FFMAX(i - 3, 0), k = 0; j <= i + 3; j++, k++) {
211
3053
            int v = msk_val[j] + lwc_gain_tab[i][k];
212
3053
            if (v > max_v) {
213
1451
                max_j = j;
214
1451
                max_v = v;
215
            }
216
3053
            thr = log_add(thr, v);
217
        }
218
219
473
        if (msk_val[i] < thr) {
220
2329
            for (j = FFMAX(max_j - 3, 0),
221
273
                 k = FFMAX(3 - max_j, 0);
222
2056
                 j <= max_j + 3; j++, k++)
223
1783
                lwc_val[j] += lwc_adj_tab[k];
224
        }
225
    }
226
227
731
    for (i = 0; i < 16; i++) {
228
688
        int v = FFMAX(lwc_val[i], -512);
229
688
        msk_val[i] = FFMAX(msk_val[i] + v, 0);
230
    }
231
43
}
232
233
3113
static void bit_allocate(int nb_exponent, int nb_code, int fr_code,
234
                         int *exp, int *bap,
235
                         int fg_spc, int fg_ofs, int msk_mod, int snr_ofs)
236
{
237
    int msk_val[MAX_BIAS_EXP];
238
    int psd_val[MAX_BIAS_EXP];
239
3113
    int fast_leak  = 0;
240
3113
    int slow_leak  = 0;
241
3113
    int dc_code    = dc_code_tab[fr_code - 1];
242
3113
    int ht_code    = ht_code_tab[fr_code - 1];
243
3113
    int fast_gain  = fast_gain_tab[fg_ofs];
244
3113
    int slow_decay = slow_decay_tab[dc_code][msk_mod];
245
3113
    int misc_decay = misc_decay_tab[nb_code][dc_code][msk_mod];
246
3113
    const uint16_t *slow_gain      = slow_gain_tab[nb_code][msk_mod];
247
3113
    const uint16_t *fast_decay     = fast_decay_tab[nb_code][dc_code][msk_mod];
248
3113
    const uint16_t *fast_gain_adj  = fast_gain_adj_tab[nb_code][dc_code];
249
3113
    const uint16_t *hearing_thresh = hearing_thresh_tab[nb_code][ht_code];
250
    int i;
251
252
143140
    for (i = 0; i < nb_exponent; i++)
253
140027
        psd_val[i] = (48 - exp[i]) * 64;
254
255
3113
    fast_gain_adj += band_ofs_tab[nb_code][fg_spc];
256
143140
    for (i = 0; i < nb_exponent; i++) {
257
140027
        fast_leak = log_add(fast_leak  - fast_decay[i],
258
140027
                            psd_val[i] - fast_gain + fast_gain_adj[i]);
259
140027
        slow_leak = log_add(slow_leak  - slow_decay,
260
140027
                            psd_val[i] - slow_gain[i]);
261
140027
        msk_val[i] = FFMAX(fast_leak, slow_leak);
262
    }
263
264
3113
    fast_leak = 0;
265
68072
    for (i = nb_exponent - 1; i > band_low_tab[nb_code]; i--) {
266
64959
        fast_leak = log_add(fast_leak - misc_decay, psd_val[i] - fast_gain);
267
64959
        msk_val[i] = FFMAX(msk_val[i], fast_leak);
268
    }
269
270
143140
    for (i = 0; i < nb_exponent; i++)
271
140027
        msk_val[i] = FFMAX(msk_val[i], hearing_thresh[i]);
272
273
3113
    if (!nb_code)
274
43
        calc_lowcomp(msk_val);
275
276
143140
    for (i = 0; i < nb_exponent; i++) {
277
140027
        int v = 16 * (snr_ofs - 64) + psd_val[i] - msk_val[i] >> 5;
278
140027
        bap[i] = bap_tab[av_clip_uintp2(v, 6)];
279
    }
280
3113
}
281
282
3084
static int parse_bit_alloc(DBEContext *s, DBEChannel *c)
283
{
284
    DBEGroup *p, *g;
285
    int bap_strategy[MAX_GROUPS], fg_spc[MAX_GROUPS];
286
    int fg_ofs[MAX_GROUPS], msk_mod[MAX_GROUPS];
287
    int i, snr_ofs;
288
289
6314
    for (i = 0; i < c->nb_groups; i++) {
290

3230
        bap_strategy[i] = !i || get_bits1(&s->gb);
291
3230
        if (bap_strategy[i]) {
292
3086
             fg_spc[i] = get_bits(&s->gb, 2);
293
3086
             fg_ofs[i] = get_bits(&s->gb, 3);
294
3086
            msk_mod[i] = get_bits1(&s->gb);
295
        } else {
296
144
             fg_spc[i] =  fg_spc[i - 1];
297
144
             fg_ofs[i] =  fg_ofs[i - 1];
298
144
            msk_mod[i] = msk_mod[i - 1];
299
        }
300
    }
301
302
3084
    if (get_bits1(&s->gb)) {
303
        avpriv_report_missing_feature(s->avctx, "Delta bit allocation");
304
        return AVERROR_PATCHWELCOME;
305
    }
306
307
3084
    snr_ofs = get_bits(&s->gb, 8);
308
3084
    if (!snr_ofs) {
309
        memset(c->bap, 0, sizeof(c->bap));
310
        return 0;
311
    }
312
313
6314
    for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
314

3230
        if (c->exp_strategy[i] || bap_strategy[i]) {
315
3113
            bit_allocate(g->nb_exponent, g->imdct_idx, s->fr_code,
316
3113
                         c->exponents + g->exp_ofs, c->bap + g->exp_ofs,
317
                         fg_spc[i], fg_ofs[i], msk_mod[i], snr_ofs);
318
        } else {
319
117
            memcpy(c->bap + g->exp_ofs,
320
117
                   c->bap + p->exp_ofs,
321
117
                   g->nb_exponent * sizeof(c->bap[0]));
322
        }
323
    }
324
325
3084
    return 0;
326
}
327
328
3084
static int parse_indices(DBEContext *s, DBEChannel *c)
329
{
330
    DBEGroup *p, *g;
331
    int i, j;
332
333
6314
    for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
334
3230
        if (get_bits1(&s->gb)) {
335
3062
            int start = get_bits(&s->gb, 6);
336
337
3062
            if (start > g->nb_exponent) {
338
                av_log(s->avctx, AV_LOG_ERROR, "Invalid start index\n");
339
                return AVERROR_INVALIDDATA;
340
            }
341
342
85422
            for (j = 0; j < start; j++)
343
82360
                c->idx[g->exp_ofs + j] = 0;
344
345
58896
            for (; j < g->nb_exponent; j++)
346
55834
                c->idx[g->exp_ofs + j] = get_bits(&s->gb, 2);
347

168
        } else if (i && g->nb_exponent == p->nb_exponent) {
348
138
            memcpy(c->idx + g->exp_ofs,
349
138
                   c->idx + p->exp_ofs,
350
138
                   g->nb_exponent * sizeof(c->idx[0]));
351
        } else {
352
30
            memset(c->idx + g->exp_ofs, 0, g->nb_exponent * sizeof(c->idx[0]));
353
        }
354
    }
355
356
3084
    return 0;
357
}
358
359
3084
static int parse_mantissas(DBEContext *s, DBEChannel *c)
360
{
361
    DBEGroup *g;
362
    int i, j, k;
363
364
6314
    for (i = 0, g = c->groups; i < c->nb_groups; i++, g++) {
365
3230
        float *mnt = c->mantissas + g->mnt_ofs;
366
367
147352
        for (j = 0; j < g->nb_exponent; j++) {
368
144122
            int bap     = c->bap[g->exp_ofs + j];
369
144122
            int idx     = c->idx[g->exp_ofs + j];
370
144122
            int size1   = mantissa_size1[bap][idx];
371
144122
            int count   = g->nb_mantissa[j];
372
144122
            float exp   = exponent_tab[c->exponents[g->exp_ofs + j]];
373
144122
            float scale = mantissa_tab1[size1][idx] * exp;
374
375
144122
            if (!size1) {
376
7025
                memset(mnt, 0, count * sizeof(*mnt));
377
137097
            } else if (idx) {
378
                int values[100];
379
37574
                int escape = -(1 << size1 - 1);
380
381
1248879
                for (k = 0; k < count; k++)
382
1211305
                    values[k] = get_sbits(&s->gb, size1);
383
384
1248879
                for (k = 0; k < count; k++) {
385
1211305
                    if (values[k] != escape) {
386
1078434
                        mnt[k] = values[k] * scale;
387
                    } else {
388
132871
                        int size2 = mantissa_size2[bap][idx];
389
132871
                        int value = get_sbits(&s->gb, size2);
390
132871
                        float a = mantissa_tab2[size2][idx];
391
132871
                        float b = mantissa_tab3[size2][idx];
392
132871
                        if (value < 0)
393
66431
                            mnt[k] = ((value + 1) * a - b) * exp;
394
                        else
395
66440
                            mnt[k] = (value * a + b) * exp;
396
                    }
397
                }
398
            } else {
399
912072
                for (k = 0; k < count; k++)
400
812549
                    mnt[k] = get_sbits(&s->gb, size1) * scale;
401
            }
402
403
144122
            mnt += count;
404
        }
405
406
18640
        for (; j < g->nb_exponent + c->bw_code; j++) {
407
15410
            memset(mnt, 0, g->nb_mantissa[j] * sizeof(*mnt));
408
15410
            mnt += g->nb_mantissa[j];
409
        }
410
    }
411
412
3084
    return 0;
413
}
414
415
3084
static int parse_channel(DBEContext *s, int ch, int seg_id)
416
{
417
3084
    DBEChannel *c = &s->channels[seg_id][ch];
418
    int i, ret;
419
420
3084
    if (s->rev_id[ch] > 1) {
421
        avpriv_report_missing_feature(s->avctx, "Encoder revision %d", s->rev_id[ch]);
422
        return AVERROR_PATCHWELCOME;
423
    }
424
425
3084
    if (ch == lfe_channel_tab[s->prog_conf]) {
426
514
        c->gr_code = 3;
427
514
        c->bw_code = 29;
428
    } else {
429
2570
        c->gr_code = get_bits(&s->gb, 2);
430
2570
        c->bw_code = get_bits(&s->gb, 3);
431
2570
        if (c->gr_code == 3) {
432
            av_log(s->avctx, AV_LOG_ERROR, "Invalid group type code\n");
433
            return AVERROR_INVALIDDATA;
434
        }
435
    }
436
437
3084
    c->nb_groups   = nb_groups_tab[c->gr_code];
438
3084
    c->nb_mstr_exp = nb_mstr_exp_tab[c->gr_code];
439
440
6314
    for (i = 0; i < c->nb_groups; i++) {
441
3230
        c->groups[i] = frm_ofs_tab[seg_id][c->gr_code][i];
442
3230
        if (c->nb_mstr_exp == 2) {
443
2716
            c->groups[i].nb_exponent    -= c->bw_code;
444
2716
            c->groups[i].nb_bias_exp[1] -= c->bw_code;
445
        }
446
    }
447
448
3084
    if ((ret = parse_exponents(s, c)) < 0)
449
        return ret;
450
3084
    if ((ret = parse_bit_alloc(s, c)) < 0)
451
        return ret;
452
3084
    if ((ret = parse_indices(s, c)) < 0)
453
        return ret;
454
3084
    if ((ret = parse_mantissas(s, c)) < 0)
455
        return ret;
456
457
3084
    if (get_bits_left(&s->gb) < 0) {
458
        av_log(s->avctx, AV_LOG_ERROR, "Read past end of channel %d\n", ch);
459
        return AVERROR_INVALIDDATA;
460
    }
461
462
3084
    return 0;
463
}
464
465
1028
static int parse_audio(DBEContext *s, int start, int end, int seg_id)
466
{
467
    int ch, ret, key;
468
469
1028
    if ((key = parse_key(s)) < 0)
470
        return key;
471
472
4112
    for (ch = start; ch < end; ch++) {
473
3084
        if (!s->ch_size[ch]) {
474
            s->channels[seg_id][ch].nb_groups = 0;
475
            continue;
476
        }
477
3084
        if ((ret = convert_input(s, s->ch_size[ch], key)) < 0)
478
            return ret;
479
3084
        if ((ret = parse_channel(s, ch, seg_id)) < 0) {
480
            if (s->avctx->err_recognition & AV_EF_EXPLODE)
481
                return ret;
482
            s->channels[seg_id][ch].nb_groups = 0;
483
        }
484
3084
        if ((ret = skip_input(s, s->ch_size[ch])) < 0)
485
            return ret;
486
    }
487
488
1028
    return skip_input(s, 1);
489
}
490
491
257
static int parse_meter(DBEContext *s)
492
{
493
257
    if (s->meter_size)
494
257
        return skip_input(s, s->key_present + s->meter_size + 1);
495
    return 0;
496
}
497
498
3230
static void imdct_calc(DBEContext *s, DBEGroup *g, float *result, float *values)
499
{
500
3230
    FFTContext *imdct = &s->imdct[g->imdct_idx];
501
3230
    int n   = 1 << imdct_bits_tab[g->imdct_idx];
502
3230
    int n2  = n >> 1;
503
    int i;
504
505

3230
    switch (g->imdct_phs) {
506
1542
    case 0:
507
1542
        imdct->imdct_half(imdct, result, values);
508
1570694
        for (i = 0; i < n2; i++)
509
1569152
            result[n2 + i] = result[n2 - i - 1];
510
1542
        break;
511
146
    case 1:
512
146
        imdct->imdct_calc(imdct, result, values);
513
146
        break;
514
1542
    case 2:
515
1542
        imdct->imdct_half(imdct, result + n2, values);
516
1570694
        for (i = 0; i < n2; i++)
517
1569152
            result[i] = -result[n - i - 1];
518
1542
        break;
519
    default:
520
        av_assert0(0);
521
    }
522
3230
}
523
524
3084
static void transform(DBEContext *s, DBEChannel *c, float *history, float *output)
525
{
526
3084
    LOCAL_ALIGNED_32(float, buffer, [2048]);
527
3084
    LOCAL_ALIGNED_32(float, result, [1152]);
528
    DBEGroup *g;
529
    int i;
530
531
3084
    memset(result, 0, 1152 * sizeof(float));
532
6314
    for (i = 0, g = c->groups; i < c->nb_groups; i++, g++) {
533
3230
        float *src = buffer + g->src_ofs;
534
3230
        float *dst = result + g->dst_ofs;
535
3230
        float *win = window + g->win_ofs;
536
537
3230
        imdct_calc(s, g, buffer, c->mantissas + g->mnt_ofs);
538
3230
        s->fdsp->vector_fmul_add(dst, src, win, dst, g->win_len);
539
    }
540
541
792588
    for (i = 0; i < 256; i++)
542
789504
        output[i] = history[i] + result[i];
543
1976844
    for (i = 256; i < 896; i++)
544
1973760
        output[i] = result[i];
545
792588
    for (i = 0; i < 256; i++)
546
789504
        history[i] = result[896 + i];
547
3084
}
548
549
1542
static void apply_gain(DBEContext *s, int begin, int end, float *output)
550
{
551

1542
    if (begin == 960 && end == 960)
552
1542
        return;
553
554
    if (begin == end) {
555
        s->fdsp->vector_fmul_scalar(output, output, gain_tab[end], FRAME_SAMPLES);
556
    } else {
557
        float a = gain_tab[begin] * (1.0f / (FRAME_SAMPLES - 1));
558
        float b = gain_tab[end  ] * (1.0f / (FRAME_SAMPLES - 1));
559
        int i;
560
561
        for (i = 0; i < FRAME_SAMPLES; i++)
562
            output[i] *= a * (FRAME_SAMPLES - i - 1) + b * i;
563
    }
564
}
565
566
257
static int filter_frame(DBEContext *s, AVFrame *frame)
567
{
568
    const uint8_t *reorder;
569
    int ch, ret;
570
571
257
    if (s->nb_channels == 4)
572
        reorder = ch_reorder_4;
573
257
    else if (s->nb_channels == 6)
574
257
        reorder = ch_reorder_6;
575
    else if (s->nb_programs == 1 && !(s->avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE))
576
        reorder = ch_reorder_8;
577
    else
578
        reorder = ch_reorder_n;
579
580
257
    frame->nb_samples = FRAME_SAMPLES;
581
257
    if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0)
582
        return ret;
583
584
1799
    for (ch = 0; ch < s->nb_channels; ch++) {
585
1542
        float *output = (float *)frame->extended_data[reorder[ch]];
586
1542
        transform(s, &s->channels[0][ch], s->history[ch], output);
587
1542
        transform(s, &s->channels[1][ch], s->history[ch], output + FRAME_SAMPLES / 2);
588
1542
        apply_gain(s, s->begin_gain[ch], s->end_gain[ch], output);
589
    }
590
591
257
    return 0;
592
}
593
594
257
static int dolby_e_decode_frame(AVCodecContext *avctx, void *data,
595
                                int *got_frame_ptr, AVPacket *avpkt)
596
{
597
257
    DBEContext *s = avctx->priv_data;
598
    int i, j, hdr, ret;
599
600
257
    if (avpkt->size < 3)
601
        return AVERROR_INVALIDDATA;
602
603
257
    hdr = AV_RB24(avpkt->data);
604
257
    if ((hdr & 0xfffffe) == 0x7888e) {
605
        s->word_bits = 24;
606
257
    } else if ((hdr & 0xffffe0) == 0x788e0) {
607
        s->word_bits = 20;
608
257
    } else if ((hdr & 0xfffe00) == 0x78e00) {
609
257
        s->word_bits = 16;
610
    } else {
611
        av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
612
        return AVERROR_INVALIDDATA;
613
    }
614
615
257
    s->word_bytes  = s->word_bits + 7 >> 3;
616
257
    s->input       = avpkt->data + s->word_bytes;
617
257
    s->input_size  = avpkt->size / s->word_bytes - 1;
618
257
    s->key_present = hdr >> 24 - s->word_bits & 1;
619
620
257
    if ((ret = parse_metadata(s)) < 0)
621
        return ret;
622
623

257
    if (s->nb_programs > 1 && !s->multi_prog_warned) {
624
        av_log(avctx, AV_LOG_WARNING, "Stream has %d programs (configuration %d), "
625
               "channels will be output in native order.\n", s->nb_programs, s->prog_conf);
626
        s->multi_prog_warned = 1;
627
    }
628
629

257
    switch (s->nb_channels) {
630
    case 4:
631
        avctx->channel_layout = AV_CH_LAYOUT_4POINT0;
632
        break;
633
257
    case 6:
634
257
        avctx->channel_layout = AV_CH_LAYOUT_5POINT1;
635
257
        break;
636
    case 8:
637
        avctx->channel_layout = AV_CH_LAYOUT_7POINT1;
638
        break;
639
    }
640
641
257
    avctx->channels    = s->nb_channels;
642
257
    avctx->sample_rate = sample_rate_tab[s->fr_code];
643
257
    avctx->sample_fmt  = AV_SAMPLE_FMT_FLTP;
644
645
257
    i = s->nb_channels / 2;
646
257
    j = s->nb_channels;
647
257
    if ((ret = parse_audio(s, 0, i, 0)) < 0)
648
        return ret;
649
257
    if ((ret = parse_audio(s, i, j, 0)) < 0)
650
        return ret;
651
257
    if ((ret = parse_metadata_ext(s)) < 0)
652
        return ret;
653
257
    if ((ret = parse_audio(s, 0, i, 1)) < 0)
654
        return ret;
655
257
    if ((ret = parse_audio(s, i, j, 1)) < 0)
656
        return ret;
657
257
    if ((ret = parse_meter(s)) < 0)
658
        return ret;
659
257
    if ((ret = filter_frame(s, data)) < 0)
660
        return ret;
661
662
257
    *got_frame_ptr = 1;
663
257
    return avpkt->size;
664
}
665
666
static av_cold void dolby_e_flush(AVCodecContext *avctx)
667
{
668
    DBEContext *s = avctx->priv_data;
669
670
    memset(s->history, 0, sizeof(s->history));
671
}
672
673
2
static av_cold int dolby_e_close(AVCodecContext *avctx)
674
{
675
2
    DBEContext *s = avctx->priv_data;
676
    int i;
677
678
8
    for (i = 0; i < 3; i++)
679
6
        ff_mdct_end(&s->imdct[i]);
680
681
2
    av_freep(&s->fdsp);
682
2
    return 0;
683
}
684
685
686
1
static av_cold void init_tables(void)
687
{
688
    int i, j;
689
690
17
    for (i = 1; i < 17; i++)
691
16
        mantissa_tab1[i][0] = 1.0f / (1 << i - 1);
692
693
15
    for (i = 2; i < 16; i++) {
694
14
        mantissa_tab1[i][1] = 1.0f  / ((1 << i) - 1);
695
14
        mantissa_tab1[i][2] = 0.5f  / ((1 << i) - 1);
696
14
        mantissa_tab1[i][3] = 0.25f / ((1 << i) - 1);
697
    }
698
699
1
    mantissa_tab1[i][1] = 0.5f   / (1 << 15);
700
1
    mantissa_tab1[i][2] = 0.75f  / (1 << 15);
701
1
    mantissa_tab1[i][3] = 0.875f / (1 << 15);
702
703
17
    for (i = 1; i < 17; i++) {
704
16
        mantissa_tab2[i][1] = mantissa_tab1[i][0] * 0.5f;
705
16
        mantissa_tab2[i][2] = mantissa_tab1[i][0] * 0.75f;
706
16
        mantissa_tab2[i][3] = mantissa_tab1[i][0] * 0.875f;
707
64
        for (j = 1; j < 4; j++)
708
48
            mantissa_tab3[i][j] = 1.0f / (1 << i) + 1.0f / (1 << j) - 1.0f / (1 << i + j);
709
    }
710
711
1
    mantissa_tab3[1][3] = 0.6875f;
712
713
26
    for (i = 0; i < 25; i++) {
714
25
        exponent_tab[i * 2    ] = 1.0f      / (1 << i);
715
25
        exponent_tab[i * 2 + 1] = M_SQRT1_2 / (1 << i);
716
    }
717
718
1024
    for (i = 1; i < 1024; i++)
719
1023
        gain_tab[i] = exp2f((i - 960) / 64.0f);
720
721
    // short 1
722
1
    ff_kbd_window_init(window, 3.0f, 128);
723
129
    for (i = 0; i < 128; i++)
724
128
        window[128 + i] = window[127 - i];
725
726
    // start
727
193
    for (i = 0; i < 192; i++)
728
192
        window[256 + i] = start_window[i];
729
730
    // short 2
731
193
    for (i = 0; i < 192; i++)
732
192
        window[448 + i] = short_window2[i];
733
65
    for (i = 0; i < 64; i++)
734
64
        window[640 + i] = window[63 - i];
735
736
    // short 3
737
65
    for (i = 0; i < 64; i++)
738
64
        window[704 + i] = short_window3[i];
739
193
    for (i = 0; i < 192; i++)
740
192
        window[768 + i] = window[64 + i];
741
742
    // bridge
743
129
    for (i = 0; i < 128; i++)
744
128
        window[960 + i] = window[i];
745
65
    for (i = 0; i < 64; i++)
746
64
        window[1088 + i] = 1.0f;
747
748
    // long
749
1
    ff_kbd_window_init(window + 1408, 3.0f, 256);
750
641
    for (i = 0; i < 640; i++)
751
640
        window[1664 + i] = 1.0f;
752
257
    for (i = 0; i < 256; i++)
753
256
        window[2304 + i] = window[1152 + i] = window[1663 - i];
754
755
    // reverse start
756
193
    for (i = 0; i < 192; i++)
757
192
        window[2560 + i] = window[447 - i];
758
759
    // reverse short 2
760
257
    for (i = 0; i < 256; i++)
761
256
        window[2752 + i] = window[703 - i];
762
763
    // reverse short 3
764
257
    for (i = 0; i < 256; i++)
765
256
        window[3008 + i] = window[959 - i];
766
767
    // reverse bridge
768
449
    for (i = 0; i < 448; i++)
769
448
        window[3264 + i] = window[1407 - i];
770
1
}
771
772
2
static av_cold int dolby_e_init(AVCodecContext *avctx)
773
{
774
    static AVOnce init_once = AV_ONCE_INIT;
775
2
    DBEContext *s = avctx->priv_data;
776
    int i;
777
778
2
    if (ff_thread_once(&init_once, init_tables))
779
        return AVERROR_UNKNOWN;
780
781
8
    for (i = 0; i < 3; i++)
782
6
        if (ff_mdct_init(&s->imdct[i], imdct_bits_tab[i], 1, 2.0) < 0)
783
            return AVERROR(ENOMEM);
784
785
2
    if (!(s->fdsp = avpriv_float_dsp_alloc(0)))
786
        return AVERROR(ENOMEM);
787
788
2
    s->multi_prog_warned = !!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE);
789
2
    s->avctx = avctx;
790
2
    return 0;
791
}
792
793
AVCodec ff_dolby_e_decoder = {
794
    .name           = "dolby_e",
795
    .long_name      = NULL_IF_CONFIG_SMALL("Dolby E"),
796
    .type           = AVMEDIA_TYPE_AUDIO,
797
    .id             = AV_CODEC_ID_DOLBY_E,
798
    .priv_data_size = sizeof(DBEContext),
799
    .init           = dolby_e_init,
800
    .decode         = dolby_e_decode_frame,
801
    .close          = dolby_e_close,
802
    .flush          = dolby_e_flush,
803
    .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
804
    .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
805
    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
806
};