GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/dolby_e.c Lines: 382 477 80.1 %
Date: 2019-11-18 18:00:01 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
25
#include "internal.h"
26
#include "get_bits.h"
27
#include "put_bits.h"
28
#include "dolby_e.h"
29
#include "fft.h"
30
31
6168
static int skip_input(DBEContext *s, int nb_words)
32
{
33
6168
    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
6168
    s->input      += nb_words * s->word_bytes;
39
6168
    s->input_size -= nb_words;
40
6168
    return 0;
41
}
42
43
1285
static int parse_key(DBEContext *s)
44
{
45
1285
    if (s->key_present) {
46
1285
        uint8_t *key = s->input;
47
1285
        int      ret = skip_input(s, 1);
48
1285
        if (ret < 0)
49
            return ret;
50
1285
        return AV_RB24(key) >> 24 - s->word_bits;
51
    }
52
    return 0;
53
}
54
55
3598
static int convert_input(DBEContext *s, int nb_words, int key)
56
{
57
3598
    uint8_t *src = s->input;
58
3598
    uint8_t *dst = s->buffer;
59
    PutBitContext pb;
60
    int i;
61
62
3598
    av_assert0(nb_words <= 1024u);
63
64
3598
    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

3598
    switch (s->word_bits) {
70
3598
    case 16:
71
934452
        for (i = 0; i < nb_words; i++, src += 2, dst += 2)
72
930854
            AV_WB16(dst, AV_RB16(src) ^ key);
73
3598
        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
3598
    return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits);
89
}
90
91
257
static int parse_metadata(DBEContext *s)
92
{
93
    int i, ret, key, mtd_size;
94
95
257
    if ((key = parse_key(s)) < 0)
96
        return key;
97
257
    if ((ret = convert_input(s, 1, key)) < 0)
98
        return ret;
99
100
257
    skip_bits(&s->gb, 4);
101
257
    mtd_size = get_bits(&s->gb, 10);
102
257
    if (!mtd_size) {
103
        av_log(s->avctx, AV_LOG_ERROR, "Invalid metadata size\n");
104
        return AVERROR_INVALIDDATA;
105
    }
106
107
257
    if ((ret = convert_input(s, mtd_size, key)) < 0)
108
        return ret;
109
110
257
    skip_bits(&s->gb, 14);
111
257
    s->prog_conf = get_bits(&s->gb, 6);
112
257
    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
257
    s->nb_channels = nb_channels_tab[s->prog_conf];
118
257
    s->nb_programs = nb_programs_tab[s->prog_conf];
119
120
257
    s->fr_code      = get_bits(&s->gb, 4);
121
257
    s->fr_code_orig = get_bits(&s->gb, 4);
122
257
    if (!sample_rate_tab[s->fr_code] ||
123
257
        !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
257
    skip_bits_long(&s->gb, 88);
129
1799
    for (i = 0; i < s->nb_channels; i++)
130
1542
        s->ch_size[i] = get_bits(&s->gb, 10);
131
257
    s->mtd_ext_size = get_bits(&s->gb, 8);
132
257
    s->meter_size   = get_bits(&s->gb, 8);
133
134
257
    skip_bits_long(&s->gb, 10 * s->nb_programs);
135
1799
    for (i = 0; i < s->nb_channels; i++) {
136
1542
        s->rev_id[i]     = get_bits(&s->gb,  4);
137
1542
        skip_bits1(&s->gb);
138
1542
        s->begin_gain[i] = get_bits(&s->gb, 10);
139
1542
        s->end_gain[i]   = get_bits(&s->gb, 10);
140
    }
141
142
257
    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
257
    return skip_input(s, mtd_size + 1);
148
}
149
150
257
static int parse_metadata_ext(DBEContext *s)
151
{
152
257
    if (s->mtd_ext_size)
153
257
        return skip_input(s, s->key_present + s->mtd_ext_size + 1);
154
    return 0;
155
}
156
157
3112
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
8822
    for (i = 0; i < c->nb_mstr_exp; i++)
164
5710
        mstr_exp[i] = get_bits(&s->gb, 2) * 6;
165
166
143104
    for (i = 0; i < g->nb_exponent; i++)
167
139992
        bias_exp[i] = get_bits(&s->gb, 5);
168
169
8822
    for (i = k = 0; i < c->nb_mstr_exp; i++)
170
145702
        for (j = 0; j < g->nb_bias_exp[i]; j++, k++)
171
139992
            c->exponents[g->exp_ofs + k] = mstr_exp[i] + bias_exp[k];
172
3112
}
173
174
3084
static int parse_exponents(DBEContext *s, DBEChannel *c)
175
{
176
    DBEGroup *p, *g;
177
    int i;
178
179
6314
    for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
180

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

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

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

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

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

1542
    if (begin == 960 && end == 960)
551
1542
        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
565
257
static int filter_frame(DBEContext *s, AVFrame *frame)
566
{
567
    const uint8_t *reorder;
568
    int ch, ret;
569
570
257
    if (s->nb_channels == 4)
571
        reorder = ch_reorder_4;
572
257
    else if (s->nb_channels == 6)
573
257
        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
257
    frame->nb_samples = FRAME_SAMPLES;
580
257
    if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0)
581
        return ret;
582
583
1799
    for (ch = 0; ch < s->nb_channels; ch++) {
584
1542
        float *output = (float *)frame->extended_data[reorder[ch]];
585
1542
        transform(s, &s->channels[0][ch], s->history[ch], output);
586
1542
        transform(s, &s->channels[1][ch], s->history[ch], output + FRAME_SAMPLES / 2);
587
1542
        apply_gain(s, s->begin_gain[ch], s->end_gain[ch], output);
588
    }
589
590
257
    return 0;
591
}
592
593
257
static int dolby_e_decode_frame(AVCodecContext *avctx, void *data,
594
                                int *got_frame_ptr, AVPacket *avpkt)
595
{
596
257
    DBEContext *s = avctx->priv_data;
597
    int i, j, hdr, ret;
598
599
257
    if (avpkt->size < 3)
600
        return AVERROR_INVALIDDATA;
601
602
257
    hdr = AV_RB24(avpkt->data);
603
257
    if ((hdr & 0xfffffe) == 0x7888e) {
604
        s->word_bits = 24;
605
257
    } else if ((hdr & 0xffffe0) == 0x788e0) {
606
        s->word_bits = 20;
607
257
    } else if ((hdr & 0xfffe00) == 0x78e00) {
608
257
        s->word_bits = 16;
609
    } else {
610
        av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
611
        return AVERROR_INVALIDDATA;
612
    }
613
614
257
    s->word_bytes  = s->word_bits + 7 >> 3;
615
257
    s->input       = avpkt->data + s->word_bytes;
616
257
    s->input_size  = avpkt->size / s->word_bytes - 1;
617
257
    s->key_present = hdr >> 24 - s->word_bits & 1;
618
619
257
    if ((ret = parse_metadata(s)) < 0)
620
        return ret;
621
622

257
    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

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