Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/dolby_e.c
Line
Count
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/channel_layout.h"
22
#include "libavutil/float_dsp.h"
23
#include "libavutil/thread.h"
24
#include "libavutil/mem.h"
25
#include "libavutil/mem_internal.h"
26
#include "libavutil/opt.h"
27
#include "libavutil/tx.h"
28
29
#include "codec_internal.h"
30
#include "decode.h"
31
#include "get_bits.h"
32
#include "dolby_e.h"
33
#include "kbdwin.h"
34
35
#define MAX_SEGMENTS    2
36
37
#define MAX_GROUPS      8
38
#define MAX_EXPONENTS   304
39
#define MAX_MANTISSAS   1024
40
41
#define MAX_MSTR_EXP    2
42
#define MAX_BIAS_EXP    50
43
44
enum DBEOutputChannelOrder {
45
    CHANNEL_ORDER_DEFAULT,
46
    CHANNEL_ORDER_CODED,
47
};
48
49
typedef struct DBEGroup {
50
    uint8_t         nb_exponent;
51
    uint8_t         nb_bias_exp[MAX_MSTR_EXP];
52
    uint16_t        exp_ofs;
53
    uint16_t        mnt_ofs;
54
    const uint8_t   *nb_mantissa;
55
    uint8_t         imdct_idx;
56
    uint8_t         imdct_phs;
57
    uint16_t        win_len;
58
    uint16_t        dst_ofs;
59
    uint16_t        win_ofs;
60
    uint16_t        src_ofs;
61
} DBEGroup;
62
63
typedef struct DBEChannel {
64
    int     gr_code;
65
    int     bw_code;
66
67
    int         nb_groups;
68
    int         nb_mstr_exp;
69
    DBEGroup    groups[MAX_GROUPS];
70
71
    int     exp_strategy[MAX_GROUPS];
72
    int     exponents[MAX_EXPONENTS];
73
    int     bap[MAX_EXPONENTS];
74
    int     idx[MAX_EXPONENTS];
75
76
    DECLARE_ALIGNED(32, float, mantissas)[MAX_MANTISSAS];
77
} DBEChannel;
78
79
typedef struct DBEDecodeContext {
80
    const AVClass   *class;
81
    AVCodecContext  *avctx;
82
    DBEContext  dectx;
83
84
    DBEChannel  channels[MAX_SEGMENTS][MAX_CHANNELS];
85
86
    DECLARE_ALIGNED(32, float, history)[MAX_CHANNELS][256];
87
88
    AVTXContext         *imdct[2][3];
89
    av_tx_fn             imdct_fn[2][3];
90
    AVFloatDSPContext   *fdsp;
91
} DBEDecodeContext;
92
93
static const int8_t lfe_channel_tab[MAX_PROG_CONF + 1] = {
94
     5,  5, -1, -1, -1, -1, -1, -1, -1, -1, -1, 4,
95
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  5, 5
96
};
97
98
static const uint8_t ch_reorder_4[4] = { 0, 2, 1, 3 };
99
static const uint8_t ch_reorder_6[6] = { 0, 2, 4, 1, 3, 5 };
100
static const uint8_t ch_reorder_8[8] = { 0, 2, 6, 4, 1, 3, 7, 5 };
101
static const uint8_t ch_reorder_n[8] = { 0, 2, 4, 6, 1, 3, 5, 7 };
102
103
104
static const uint8_t nb_groups_tab[4] = { 1, 8, 7, 1 };
105
106
static const uint8_t nb_mstr_exp_tab[4] = { 2, 2, 2, 1 };
107
108
static const uint8_t nb_mantissa_38[38] = {
109
     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
110
     2,  2,  2,  2,  2,  2,  3,  3,  3,  4,  4,  4,  5,  5,  6,  6,
111
     7,  8,  9, 10, 11, 12,
112
};
113
114
static const uint8_t nb_mantissa_44[44] = {
115
     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,
116
     2,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  5,  5,  6,  7,  7,
117
     8,  9, 10, 11, 12, 13, 15, 16, 18, 20, 22, 25,
118
};
119
120
static const uint8_t nb_mantissa_50[50] = {
121
     1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  3,  3,  3,
122
     3,  4,  4,  5,  5,  6,  6,  7,  8,  9,  9, 10, 12, 13, 14, 16,
123
    18, 19, 22, 24, 27, 29, 32, 36, 40, 44, 49, 54, 60, 66, 74, 82,
124
    90, 100,
125
};
126
127
static const uint8_t imdct_bits_tab[3] = { 8, 9, 11 };
128
129
static const DBEGroup grp_tab_0[1] = {
130
    { 50, { 27, 23 }, 0, 0, nb_mantissa_50, 2, 0, 1152, 0, 1408, 0 },
131
};
132
133
static const DBEGroup grp_tab_1[8] = {
134
    { 38, { 12, 26 }, 0, 0, nb_mantissa_38, 0, 0, 192, 0, 256, 0 },
135
    { 38, { 12, 26 }, 38, 128, nb_mantissa_38, 0, 1, 256, 64, 448, 0 },
136
    { 38, { 12, 26 }, 76, 256, nb_mantissa_38, 0, 1, 256, 192, 704, 0 },
137
    { 38, { 12, 26 }, 114, 384, nb_mantissa_38, 0, 1, 256, 320, 0, 0 },
138
    { 38, { 12, 26 }, 152, 512, nb_mantissa_38, 0, 1, 256, 448, 0, 0 },
139
    { 38, { 12, 26 }, 190, 640, nb_mantissa_38, 0, 1, 256, 576, 0, 0 },
140
    { 38, { 12, 26 }, 228, 768, nb_mantissa_38, 0, 1, 256, 704, 0, 0 },
141
    { 38, { 12, 26 }, 266, 896, nb_mantissa_38, 0, 1, 256, 832, 0, 0 },
142
};
143
144
static const DBEGroup grp_tab_2[7] = {
145
    { 38, { 12, 26 }, 0, 0, nb_mantissa_38, 0, 0, 192, 0, 256, 0 },
146
    { 38, { 12, 26 }, 38, 128, nb_mantissa_38, 0, 1, 256, 64, 448, 0 },
147
    { 38, { 12, 26 }, 76, 256, nb_mantissa_38, 0, 1, 256, 192, 704, 0 },
148
    { 38, { 12, 26 }, 114, 384, nb_mantissa_38, 0, 1, 256, 320, 0, 0 },
149
    { 38, { 12, 26 }, 152, 512, nb_mantissa_38, 0, 1, 256, 448, 0, 0 },
150
    { 38, { 12, 26 }, 190, 640, nb_mantissa_38, 0, 1, 256, 576, 0, 0 },
151
    { 44, { 19, 25 }, 228, 768, nb_mantissa_44, 1, 1, 448, 704, 960, 64 },
152
};
153
154
static const DBEGroup grp_tab_3[1] = {
155
    { 21, { 21 }, 0, 0, nb_mantissa_50, 2, 0, 1152, 0, 1408, 0 },
156
};
157
158
static const DBEGroup grp_tab_4[1] = {
159
    { 50, { 27, 23 }, 0, 0, nb_mantissa_50, 2, 2, 1152, 0, 1408, 896 },
160
};
161
162
static const DBEGroup grp_tab_5[8] = {
163
    { 38, { 12, 26 }, 0, 0, nb_mantissa_38, 0, 1, 256, 64, 0, 0 },
164
    { 38, { 12, 26 }, 38, 128, nb_mantissa_38, 0, 1, 256, 192, 0, 0 },
165
    { 38, { 12, 26 }, 76, 256, nb_mantissa_38, 0, 1, 256, 320, 0, 0 },
166
    { 38, { 12, 26 }, 114, 384, nb_mantissa_38, 0, 1, 256, 448, 0, 0 },
167
    { 38, { 12, 26 }, 152, 512, nb_mantissa_38, 0, 1, 256, 576, 0, 0 },
168
    { 38, { 12, 26 }, 190, 640, nb_mantissa_38, 0, 1, 256, 704, 3008, 0 },
169
    { 38, { 12, 26 }, 228, 768, nb_mantissa_38, 0, 1, 256, 832, 2752, 0 },
170
    { 38, { 12, 26 }, 266, 896, nb_mantissa_38, 0, 2, 192, 960, 2560, 64 },
171
};
172
173
static const DBEGroup grp_tab_6[7] = {
174
    { 44, { 19, 25 }, 0, 0, nb_mantissa_44, 1, 1, 448, 0, 3264, 0 },
175
    { 38, { 12, 26 }, 44, 256, nb_mantissa_38, 0, 1, 256, 320, 0, 0 },
176
    { 38, { 12, 26 }, 82, 384, nb_mantissa_38, 0, 1, 256, 448, 0, 0 },
177
    { 38, { 12, 26 }, 120, 512, nb_mantissa_38, 0, 1, 256, 576, 0, 0 },
178
    { 38, { 12, 26 }, 158, 640, nb_mantissa_38, 0, 1, 256, 704, 3008, 0 },
179
    { 38, { 12, 26 }, 196, 768, nb_mantissa_38, 0, 1, 256, 832, 2752, 0 },
180
    { 38, { 12, 26 }, 234, 896, nb_mantissa_38, 0, 2, 192, 960, 2560, 64 },
181
};
182
183
static const DBEGroup grp_tab_7[1] = {
184
    { 21, { 21 }, 0, 0, nb_mantissa_50, 2, 2, 1152, 0, 1408, 896 },
185
};
186
187
static const DBEGroup *const frm_ofs_tab[2][4] = {
188
    { grp_tab_0, grp_tab_1, grp_tab_2, grp_tab_3 },
189
    { grp_tab_4, grp_tab_5, grp_tab_6, grp_tab_7 }
190
};
191
192
static const uint8_t mantissa_size1[16][4] = {
193
    {  0,  0,  0,  0 }, {  2,  1,  1,  1 }, {  3,  2,  1,  1 }, {  4,  3,  2,  1 },
194
    {  5,  4,  3,  2 }, {  6,  5,  4,  3 }, {  7,  6,  5,  4 }, {  8,  7,  6,  5 },
195
    {  9,  8,  7,  6 }, { 10,  9,  8,  7 }, { 11, 10,  9,  8 }, { 12, 11, 10,  9 },
196
    { 13, 12, 11, 10 }, { 14, 13, 12, 11 }, { 15, 14, 13, 12 }, { 16, 15, 14, 13 },
197
};
198
199
static const uint8_t mantissa_size2[16][4] = {
200
    {  0,  0,  0,  0 }, {  2,  1,  2,  2 }, {  3,  2,  3,  3 }, {  4,  3,  4,  4 },
201
    {  5,  4,  5,  5 }, {  6,  5,  6,  6 }, {  7,  6,  7,  7 }, {  8,  7,  8,  8 },
202
    {  9,  8,  9,  9 }, { 10,  9, 10, 10 }, { 11, 10, 11, 11 }, { 12, 11, 12, 12 },
203
    { 13, 12, 13, 13 }, { 14, 13, 14, 14 }, { 15, 14, 15, 15 }, { 16, 15, 16, 16 },
204
};
205
206
static const float start_window[192] = {
207
    0.00161569379826, 0.00185748233347, 0.00198562758548, 0.00207834078104,
208
    0.00215717748523, 0.00223067096393, 0.00230299213147, 0.00237651215396,
209
    0.00245275561606, 0.00253281402069, 0.00261754673613, 0.00270768786168,
210
    0.00280390761895, 0.00290684998656, 0.00301715751161, 0.00313548872798,
211
    0.00326253122934, 0.00339901215995, 0.00354570716636, 0.00370344845023,
212
    0.00387313232586, 0.00405572653911, 0.00425227750970, 0.00446391759265,
213
    0.00469187240551, 0.00493746822816, 0.00520213944619, 0.00548743597507,
214
    0.00579503056737, 0.00612672586953, 0.00648446105606, 0.00687031782873,
215
    0.00728652552677, 0.00773546505205, 0.00821967127415, 0.00874183354619,
216
    0.00930479393832, 0.00991154278653, 0.01056521116692, 0.01126905994567,
217
    0.01202646513050, 0.01284089936559, 0.01371590957417, 0.01465509096066,
218
    0.01566205783408, 0.01674041199523, 0.01789370972358, 0.01912542867865,
219
    0.02043893626265, 0.02183746113793, 0.02332406961796, 0.02490164852364,
220
    0.02657289580178, 0.02834031974193, 0.03020624702903, 0.03217283918354,
221
    0.03424211623810, 0.03641598586180, 0.03869627565015, 0.04108476601498,
222
    0.04358322107390, 0.04619341515939, 0.04891715301882, 0.05175628239149,
223
224
    0.05471237327267, 0.05778734733755, 0.06098291402413, 0.06430101352084,
225
    0.06774345212186, 0.07131188644726, 0.07500780649199, 0.07883251748595,
226
    0.08278712056651, 0.08687249228061, 0.09108926295730, 0.09543779401074,
227
    0.09991815425851, 0.10453009536427, 0.10927302653894, 0.11414598865987,
228
    0.11914762799220, 0.12427616972097, 0.12952939152560, 0.13490459744934,
229
    0.14039859233595, 0.14600765712201, 0.15172752528722, 0.15755336077528,
230
    0.16347973770491, 0.16950062219342, 0.17560935661442, 0.18179864660619,
231
    0.18806055113821, 0.19438647593012, 0.20076717050010, 0.20719272909882,
232
    0.21365259576030, 0.22013557367283, 0.22662983904194, 0.23312295958328,
233
    0.23960191774666, 0.24605313873388, 0.25246252333253, 0.25881548554631,
234
    0.26509699495987, 0.27129162373316, 0.27738359807707, 0.28335685401987,
235
    0.28919509723179, 0.29488186663467, 0.30040060148455, 0.30573471157819,
236
    0.31086765019993, 0.31578298939317, 0.32046449711227, 0.32489621578468,
237
    0.32906254179156, 0.33294830535654, 0.33653885031840, 0.33982011325336,
238
    0.34277870140679, 0.34540196889300, 0.34767809062480, 0.34959613344194,
239
    0.35114612391958, 0.35231911235422, 0.35310723244504, 0.35350375621308,
240
241
    0.35350314372945, 0.35310108725579, 0.35229454943591, 0.35108179521634,
242
    0.34946241721522, 0.34743735430290, 0.34500890320420, 0.34218072298001,
243
    0.33895783229541, 0.33534659943168, 0.33135472505060, 0.32699121776996,
244
    0.32226636266000, 0.31719168282019, 0.31177989424432, 0.30604485422875,
245
    0.30000150362379, 0.29366580327088, 0.28705466500775, 0.28018587766131,
246
    0.27307802848095, 0.26575042049535, 0.25822298630189, 0.25051619882000,
247
    0.24265097955783, 0.23464860495522, 0.22653061137548, 0.21831869932335,
248
    0.21003463746705, 0.20170016703857, 0.19333690717811, 0.18496626177620,
249
    0.17660932835062, 0.16828680947474, 0.16001892724986, 0.15182534128597,
250
    0.14372507062477, 0.13573642000364, 0.12787691082233, 0.12016321713317,
251
    0.11261110693234, 0.10523538898282, 0.09804986534955, 0.09106728977263,
252
    0.08429933194438, 0.07775654768810, 0.07144835495683, 0.06538301547324,
253
    0.05956762170687, 0.05400808871425, 0.04870915012107, 0.04367435714993,
254
    0.03890607899172, 0.03440550179663, 0.03017262174627, 0.02620622428513,
255
    0.02250383492507, 0.01906161305732, 0.01587412848221, 0.01293388032354,
256
    0.01023019677288, 0.00774641320626, 0.00545109736891, 0.00325868651263,
257
};
258
259
static const float short_window2[192] = {
260
    0.00018861094606, 0.00033433010202, 0.00050309624485, 0.00070306161748,
261
    0.00093995174533, 0.00121913067128, 0.00154606505568, 0.00192647806126,
262
    0.00236641248692, 0.00287225985240, 0.00345077377440, 0.00410907465023,
263
    0.00485464855241, 0.00569534163219, 0.00663935063508, 0.00769520981249,
264
    0.00887177436246, 0.01017820046395, 0.01162392194150, 0.01321862359335,
265
    0.01497221122468, 0.01689477844427, 0.01899657030441, 0.02128794388846,
266
    0.02377932597692, 0.02648116795039, 0.02940389811590, 0.03255787167130,
267
    0.03595331854986, 0.03960028941437, 0.04350860009563, 0.04768777479454,
268
    0.05214698838949, 0.05689500821121, 0.06194013566525, 0.06729014809766,
269
    0.07295224131210, 0.07893297315602, 0.08523820859989, 0.09187306673620,
270
    0.09884187012422, 0.10614809690222, 0.11379433608064, 0.12178224641797,
271
    0.13011251926531, 0.13878484574660, 0.14779788861830, 0.15714925912610,
272
    0.16683549914631, 0.17685206886673, 0.18719334022589, 0.19785259629099,
273
    0.20882203671372, 0.22009278936030, 0.23165492816694, 0.24349749722585,
274
    0.25560854105961, 0.26797514099368, 0.28058345748882, 0.29341877824732,
275
    0.30646557185942, 0.31970754671026, 0.33312771482295, 0.34670846027024,
276
277
    0.36043161174692, 0.37427851885723, 0.38823013163645, 0.40226708279486,
278
    0.41636977214436, 0.43051845264462, 0.44469331748632, 0.45887458761470,
279
    0.47304259908636, 0.48717788964798, 0.50126128392546, 0.51527397661778,
280
    0.52919761310050, 0.54301436685998, 0.55670701320069, 0.57025899869448,
281
    0.58365450587230, 0.59687851269542, 0.60991684638414, 0.62275623122793,
282
    0.63538433005035, 0.64778977905593, 0.65996221584264, 0.67189230042379,
283
    0.68357172916486, 0.69499324160511, 0.70615062019861, 0.71703868307548,
284
    0.72765326998919, 0.73799122168099, 0.74805035295521, 0.75782941981995,
285
    0.76732808110520, 0.77654685502339, 0.78548707118622, 0.79415081863423,
286
    0.80254089047207, 0.81066072573188, 0.81851434910893, 0.82610630922734,
287
    0.83344161609862, 0.84052567843230, 0.84736424144524, 0.85396332579459,
288
    0.86032916822973, 0.86646816451999, 0.87238681516918, 0.87809167437532,
289
    0.88358930263537, 0.88888622333073, 0.89398888356256, 0.89890361943564,
290
    0.90363662591861, 0.90819393133744, 0.91258137648979, 0.91680459830070,
291
    0.92086901787718, 0.92477983276087, 0.92854201312583, 0.93216030163834,
292
    0.93563921662343, 0.93898305819384, 0.94219591693690, 0.94528168477979,
293
294
    0.94823843319821, 0.95106834367330, 0.95377776558539, 0.95636718335775,
295
    0.95883679961479, 0.96118650212341, 0.96341583179195, 0.96552395212906,
296
    0.96750962060547, 0.96937116231768, 0.97110644638309, 0.97271286544154,
297
    0.97418731862798, 0.97552619834964, 0.97672538116257, 0.97778022299974,
298
    0.97868555895586, 0.97943570778357, 0.98002448120255, 0.98044519806866,
299
    0.98069070339493, 0.98075339216123, 0.98062523779637, 0.98029782516478,
300
    0.97976238784222, 0.97900984942031, 0.97803086854002, 0.97681588731895,
301
    0.97535518280755, 0.97363892108474, 0.97165721358452, 0.96940017523145,
302
    0.96685798395452, 0.96402094114589, 0.96087953263194, 0.95742448973047,
303
    0.95364684997699, 0.94953801711660, 0.94508981997396, 0.94029456983253,
304
    0.93514511597504, 0.92963489905951, 0.92375800202883, 0.91750919827624,
305
    0.91088399681406, 0.90387868421832, 0.89649036314692, 0.88871698725397,
306
    0.88055739234735, 0.87201132366062, 0.86307945913336, 0.85376342861693,
307
    0.84406582894455, 0.83399023482637, 0.82354120554757, 0.81272428745995,
308
    0.80154601230457, 0.79001389138101, 0.77813640562199, 0.76592299164227,
309
    0.75338402384395, 0.74053079267526, 0.72737547915460, 0.71393112578527,
310
};
311
312
static const float short_window3[64] = {
313
    0.00326887936450, 0.00550242900936, 0.00786846643791, 0.01045683453520,
314
    0.01330402120132, 0.01643221072863, 0.01985798040609, 0.02359509464766,
315
    0.02765559221954, 0.03205025893128, 0.03678884369614, 0.04188015679495,
316
    0.04733210987781, 0.05315172583924, 0.05934513287609, 0.06591755045290,
317
    0.07287327156378, 0.08021564389822, 0.08794705152307, 0.09606889811179,
318
    0.10458159240070, 0.11348453632940, 0.12277611617809, 0.13245369691511,
319
    0.14251361989876, 0.15295120402567, 0.16376075037904, 0.17493555039885,
320
    0.18646789757072, 0.19834910260891, 0.21056951208995, 0.22311853047787,
321
    0.23598464546683, 0.24915545655419, 0.26261770674500, 0.27635731727778,
322
    0.29035942525136, 0.30460842402318, 0.31908800624032, 0.33378120935681,
323
    0.34867046348260, 0.36373764140285, 0.37896411059909, 0.39433078709788,
324
    0.40981819096657, 0.42540650327031, 0.44107562429959, 0.45680523287270,
325
    0.47257484651351, 0.48836388230077, 0.50415171818214, 0.51991775454258,
326
    0.53564147581496, 0.55130251191887, 0.56688069931047, 0.58235614142007,
327
    0.59770926827271, 0.61292089506118, 0.62797227945823, 0.64284517745255,
328
    0.65752189749349, 0.67198535273209, 0.68621911114984, 0.70020744337099,
329
};
330
331
static const uint8_t dc_code_tab[5] = { 0, 0, 0, 1, 1 };
332
333
static const uint8_t ht_code_tab[5] = { 0, 0, 1, 2, 2 };
334
335
static const uint8_t band_ofs_tab[3][4] = {
336
    { 12, 8, 4, 0 }, { 14, 10, 6, 0 }, { 12, 8, 4, 0 }
337
};
338
339
static const uint8_t band_low_tab[3] = { 9, 17, 24 };
340
341
static const uint16_t fast_gain_tab[8] = {
342
    128, 256, 384, 512, 640, 768, 896, 1024
343
};
344
345
static const uint16_t slow_decay_tab[2][2] = { { 27, -1 }, { 32, 21 } };
346
347
static const uint16_t misc_decay_tab[3][2][2] = {
348
    { { 354, -1 }, { 425, 425 } },
349
    { { 266, -1 }, { 320,  -1 } },
350
    { { 213, -1 }, { 256,  -1 } }
351
};
352
353
static const uint16_t fast_decay_tab[3][2][2][50] = {
354
    {{{
355
        142, 142, 142, 142, 142, 142, 142, 142, 142, 142,
356
        142, 142, 142, 142, 142, 142, 142, 142, 142, 142,
357
        142, 142, 142, 142, 142, 142, 142, 142, 142, 142,
358
        142, 142, 142, 142, 142, 142, 142, 142,
359
    }, {
360
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
361
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
362
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
363
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
364
    }}, {{
365
        170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
366
        170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
367
        170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
368
        170, 170, 170, 170, 170, 170, 170, 170,
369
    }, {
370
         64,  64,  64,  64,  64,  64,  64,  64,  64,  64,
371
         64,  64,  64,  64,  64,  64,  64,  64,  64,  64,
372
         64,  64,  64,  64,  64,  64,  64,  64,  64,  64,
373
         64,  64,  64,  64,  64,  64,  64,  64,
374
    }}}, {{{
375
        266, 266, 106, 106, 106, 106, 106, 106, 106, 106,
376
        106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
377
        106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
378
        106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
379
        106, 106, 106, 106,
380
    }, {
381
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
382
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
383
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
384
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
385
         -1,  -1,  -1,  -1,
386
    }}, {{
387
        319, 319, 128, 128, 128, 128, 128, 128, 128, 128,
388
        128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
389
        128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
390
        128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
391
        128, 128, 128, 128,
392
    }, {
393
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
394
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
395
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
396
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
397
         -1,  -1,  -1,  -1,
398
    }}}, {{{
399
        106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
400
        106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
401
        106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
402
        106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
403
        106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
404
    }, {
405
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
406
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
407
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
408
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
409
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
410
    }}, {{
411
        128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
412
        128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
413
        128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
414
        128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
415
        128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
416
    }, {
417
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
418
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
419
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
420
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
421
         -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
422
    }}}
423
};
424
425
static const uint16_t fast_gain_adj_tab[3][2][62] = {
426
    {{
427
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
428
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
429
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
430
          0,   1,   2,   4,   7,  11,  16,  29,  44,  59,
431
         76,  94, 116, 142, 179, 221, 252, 285, 312, 334,
432
    }, {
433
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
434
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
435
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
436
          2,   5,   8,  10,  15,  28,  42,  57,  75,  93,
437
        115, 140, 177, 219, 247, 280, 308, 330, 427, 533,
438
    }}, {{
439
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
440
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
441
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
442
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
443
          0,   2,   5,   8,  12,  21,  35,  51,  69,  89,
444
        111, 138, 176, 220, 251, 284, 312, 334,
445
    }, {
446
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
447
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
448
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
449
          0,   0,   0,   0,   0,   0,   0,   0,   0,   2,
450
          5,   8,  11,  18,  33,  49,  65,  84, 106, 132,
451
        168, 214, 245, 279, 308, 329, 427, 533,
452
    }}, {{
453
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
454
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
455
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
456
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
457
          0,   0,   0,   0,   0,   1,   4,   7,  10,  17,
458
         31,  47,  65,  84, 107, 134, 171, 215, 250, 283,
459
        312, 334,
460
    }, {
461
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
462
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
463
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
464
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
465
          0,   0,   0,   0,   3,   6,   9,  13,  27,  43,
466
         60,  79, 100, 126, 160, 207, 242, 276, 307, 329,
467
        427, 533,
468
    }}
469
};
470
471
static const uint16_t slow_gain_tab[3][2][50] = {
472
    {{
473
        3072, 3072, 3072, 3072, 3072, 3072, 1063, 1063, 1063, 1063,
474
        1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063,
475
        1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063,
476
        1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063,
477
    }, {
478
        3072, 3072, 3072, 3072, 3072, 3072,  850,  850,  850,  850,
479
         850,  850,  850,  850,  850,  850,  850,  850,  850,  850,
480
         850,  850,  850,  850,  850,  850,  850,  850,  850,  850,
481
         850,  850,  850,  850,  850,  850,  850,  850,
482
    }}, {{
483
        3072, 1212, 1212, 1212,  999,  999,  999,  999,  999,  999,
484
         999,  999,  999,  999,  999,  999,  999,  999,  999,  999,
485
         999,  999,  999,  999,  999,  999,  999,  999,  999,  999,
486
         999,  999,  999,  999,  999,  999,  999,  999,  999,  999,
487
         999,  999,  999,  999,
488
    }, {
489
          -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
490
          -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
491
          -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
492
          -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
493
          -1,   -1,   -1,   -1,
494
    }}, {{
495
        3072, 3072, 3072, 3072, 3072, 3072, 3072, 3072, 3072, 3072,
496
         999,  999,  999,  999,  999,  999,  999,  999,  999,  999,
497
         999,  999,  999,  999,  999,  999,  999,  999,  999,  999,
498
         999,  999,  999,  999,  999,  999,  999,  999,  999,  999,
499
         999,  999,  999,  999,  999,  999,  999,  999,  999,  999,
500
    }, {
501
          -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
502
          -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
503
          -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
504
          -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
505
          -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
506
    }}
507
};
508
509
static const uint16_t hearing_thresh_tab[3][3][50] = {
510
    {{
511
        1403, 1141, 1000,  959,  948,  957,  946,  925,  899,  871,
512
         843,  815,  789,  766,  745,  727,  705,  687,  681,  686,
513
         701,  725,  768,  854,  940, 1018, 1075, 1103, 1111, 1106,
514
        1098, 1105, 1142, 1237, 1419, 1721, 2169, 2805,
515
    }, {
516
        1401, 1130,  995,  957,  947,  955,  941,  918,  890,  861,
517
         831,  803,  777,  754,  734,  717,  698,  684,  682,  692,
518
         712,  743,  798,  894,  976, 1045, 1091, 1109, 1110, 1102,
519
        1098, 1116, 1174, 1300, 1526, 1884, 2401, 3072,
520
    }, {
521
        1393, 1086,  974,  949,  957,  941,  913,  878,  843,  808,
522
         777,  750,  727,  708,  695,  686,  681,  689,  714,  752,
523
         811,  888,  971, 1044, 1087, 1108, 1110, 1102, 1098, 1115,
524
        1172, 1290, 1489, 1812, 2293, 2964, 3072, 3072,
525
    }}, {{
526
        1412, 1343, 1141, 1047, 1000,  974,  959,  951,  948,  947,
527
         957,  953,  946,  936,  925,  906,  878,  850,  822,  795,
528
         771,  745,  719,  700,  687,  681,  685,  701,  733,  784,
529
         885,  977, 1047, 1092, 1110, 1108, 1099, 1102, 1138, 1233,
530
        1413, 1711, 2157, 2797,
531
    }, {
532
        1412, 1336, 1130, 1040,  995,  970,  957,  950,  947,  947,
533
         955,  950,  941,  930,  918,  897,  868,  838,  810,  783,
534
         759,  734,  710,  693,  684,  681,  690,  712,  752,  823,
535
         924, 1009, 1069, 1102, 1111, 1104, 1098, 1111, 1168, 1295,
536
        1518, 1873, 2388, 3072,
537
    }, {
538
        1411, 1293, 1086, 1009,  974,  957,  949,  947,  957,  951,
539
         941,  928,  913,  896,  878,  852,  817,  785,  756,  732,
540
         713,  695,  683,  682,  689,  710,  746,  811,  906,  992,
541
        1061, 1099, 1111, 1106, 1098, 1107, 1155, 1266, 1471, 1799,
542
        2277, 2945, 3072, 3072,
543
    }}, {{
544
        1431, 1412, 1403, 1379, 1343, 1293, 1229, 1180, 1125, 1075,
545
        1040, 1014,  996,  979,  965,  957,  951,  948,  947,  957,
546
         951,  940,  924,  903,  877,  846,  815,  785,  753,  725,
547
         702,  686,  681,  689,  714,  760,  847,  947, 1028, 1083,
548
        1108, 1109, 1101, 1100, 1132, 1222, 1402, 1705, 2160, 2803,
549
    }, {
550
        1431, 1412, 1401, 1375, 1336, 1278, 1215, 1168, 1115, 1066,
551
        1032, 1008,  991,  975,  962,  954,  950,  947,  947,  955,
552
         948,  935,  916,  894,  866,  835,  803,  772,  742,  715,
553
         695,  683,  683,  697,  729,  784,  887,  982, 1054, 1096,
554
        1111, 1106, 1098, 1107, 1159, 1281, 1505, 1865, 2391, 3072,
555
    }, {
556
        1427, 1411, 1393, 1353, 1293, 1215, 1160, 1118, 1072, 1031,
557
        1003,  984,  971,  960,  952,  948,  947,  957,  952,  941,
558
         924,  902,  876,  847,  815,  781,  750,  723,  700,  685,
559
         681,  691,  719,  766,  858,  958, 1039, 1089, 1109, 1108,
560
        1099, 1102, 1141, 1245, 1442, 1766, 2250, 2930, 3072, 3072,
561
    }}
562
};
563
564
static const int16_t lwc_gain_tab[11][7] = {
565
    {   -21,  -197,  -271,  -466, 32767, 32767, 32767 },
566
    {  -197,   -29,  -244,  -271,  -540, 32767, 32767 },
567
    {  -271,  -244,   -29,  -249,  -271,  -593, 32767 },
568
    {  -466,  -271,  -249,   -29,  -251,  -271,  -632 },
569
    {  -540,  -271,  -251,   -29,  -251,  -271,  -664 },
570
    {  -593,  -271,  -251,   -29,  -252,  -271,  -690 },
571
    {  -632,  -271,  -252,   -29,  -252,  -271,  -711 },
572
    {  -664,  -271,  -252,   -29,  -252,  -271,  -730 },
573
    {  -690,  -271,  -252,   -29,  -252,  -271,  -745 },
574
    {  -711,  -271,  -252,   -29,  -253,  -271,  -759 },
575
    {  -730,  -271,  -253,   -29,  -253,  -271,  -771 },
576
};
577
578
static const int16_t lwc_adj_tab[7] = {
579
    -192, -320, -448, -512, -448, -320, -192,
580
};
581
582
static const uint8_t log_add_tab[212] = {
583
    64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 52, 51, 50,
584
    49, 48, 47, 47, 46, 45, 44, 44, 43, 42, 41, 41, 40, 39, 38, 38,
585
    37, 36, 36, 35, 35, 34, 33, 33, 32, 32, 31, 30, 30, 29, 29, 28,
586
    28, 27, 27, 26, 26, 25, 25, 24, 24, 23, 23, 22, 22, 21, 21, 21,
587
    20, 20, 19, 19, 19, 18, 18, 18, 17, 17, 17, 16, 16, 16, 15, 15,
588
    15, 14, 14, 14, 13, 13, 13, 13, 12, 12, 12, 12, 11, 11, 11, 11,
589
    10, 10, 10, 10, 10,  9,  9,  9,  9,  9,  8,  8,  8,  8,  8,  8,
590
     7,  7,  7,  7,  7,  7,  6,  6,  6,  6,  6,  6,  6,  6,  5,  5,
591
     5,  5,  5,  5,  5,  5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
592
     4,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  2,
593
     2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
594
     2,  2,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
595
     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
596
     1,  1,  0,  0,
597
};
598
599
static const uint8_t bap_tab[64] = {
600
     0,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  3,  3,  3,  3,  4,
601
     4,  4,  4,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7,  7,  7,  8,
602
     8,  8,  8,  9,  9,  9,  9, 10, 10, 10, 10, 11, 11, 11, 11, 12,
603
    12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 15,
604
};
605
606
static float mantissa_tab1[17][4];
607
static float mantissa_tab2[17][4];
608
static float mantissa_tab3[17][4];
609
static float exponent_tab[50];
610
static float gain_tab[1024];
611
612
DECLARE_ALIGNED(32, static float, window)[3712];
613
614
static int skip_input(DBEContext *s, int nb_words)
615
498k
{
616
498k
    if (nb_words > s->input_size) {
617
3.40k
        av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n");
618
3.40k
        return AVERROR_INVALIDDATA;
619
3.40k
    }
620
621
494k
    s->input      += nb_words * s->word_bytes;
622
494k
    s->input_size -= nb_words;
623
494k
    return 0;
624
498k
}
625
626
static int parse_key(DBEContext *s)
627
484k
{
628
484k
    if (s->key_present) {
629
855
        const uint8_t *key = s->input;
630
855
        int      ret = skip_input(s, 1);
631
855
        if (ret < 0)
632
203
            return ret;
633
652
        return AV_RB24(key) >> 24 - s->word_bits;
634
855
    }
635
483k
    return 0;
636
484k
}
637
638
static int parse_metadata_ext(DBEDecodeContext *s1)
639
120k
{
640
120k
    DBEContext *s = &s1->dectx;
641
120k
    if (s->metadata.mtd_ext_size)
642
681
        return skip_input(s, s->key_present + s->metadata.mtd_ext_size + 1);
643
119k
    return 0;
644
120k
}
645
646
static void unbias_exponents(DBEContext *s, DBEChannel *c, DBEGroup *g)
647
18.2k
{
648
18.2k
    int mstr_exp[MAX_MSTR_EXP];
649
18.2k
    int bias_exp[MAX_BIAS_EXP];
650
18.2k
    int i, j, k;
651
652
52.0k
    for (i = 0; i < c->nb_mstr_exp; i++)
653
33.7k
        mstr_exp[i] = get_bits(&s->gb, 2) * 6;
654
655
673k
    for (i = 0; i < g->nb_exponent; i++)
656
655k
        bias_exp[i] = get_bits(&s->gb, 5);
657
658
52.0k
    for (i = k = 0; i < c->nb_mstr_exp; i++)
659
689k
        for (j = 0; j < g->nb_bias_exp[i]; j++, k++)
660
655k
            c->exponents[g->exp_ofs + k] = mstr_exp[i] + bias_exp[k];
661
18.2k
}
662
663
static int parse_exponents(DBEContext *s, DBEChannel *c)
664
12.8k
{
665
12.8k
    DBEGroup *p, *g;
666
12.8k
    int i;
667
668
74.6k
    for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
669
61.7k
        c->exp_strategy[i] = !i || g->nb_exponent != p->nb_exponent || get_bits1(&s->gb);
670
61.7k
        if (c->exp_strategy[i]) {
671
18.2k
            unbias_exponents(s, c, g);
672
43.5k
        } else {
673
43.5k
            memcpy(c->exponents + g->exp_ofs,
674
43.5k
                   c->exponents + p->exp_ofs,
675
43.5k
                   g->nb_exponent * sizeof(c->exponents[0]));
676
43.5k
        }
677
61.7k
    }
678
679
12.8k
    return 0;
680
12.8k
}
681
682
static inline int log_add(int a, int b)
683
1.78M
{
684
1.78M
    int c = FFABS(a - b) >> 1;
685
1.78M
    return FFMAX(a, b) + log_add_tab[FFMIN(c, 211)];
686
1.78M
}
687
688
static void calc_lowcomp(int *msk_val)
689
8.63k
{
690
8.63k
    int lwc_val[17] = { 0 };
691
8.63k
    int i, j, k;
692
693
103k
    for (i = 0; i < 11; i++) {
694
95.0k
        int max_j = 0;
695
95.0k
        int max_v = INT_MIN;
696
95.0k
        int thr   = 0;
697
698
708k
        for (j = FFMAX(i - 3, 0), k = 0; j <= i + 3; j++, k++) {
699
613k
            int v = msk_val[j] + lwc_gain_tab[i][k];
700
613k
            if (v > max_v) {
701
293k
                max_j = j;
702
293k
                max_v = v;
703
293k
            }
704
613k
            thr = log_add(thr, v);
705
613k
        }
706
707
95.0k
        if (msk_val[i] < thr) {
708
52.5k
            for (j = FFMAX(max_j - 3, 0),
709
52.5k
                 k = FFMAX(3 - max_j, 0);
710
393k
                 j <= max_j + 3; j++, k++)
711
340k
                lwc_val[j] += lwc_adj_tab[k];
712
52.5k
        }
713
95.0k
    }
714
715
146k
    for (i = 0; i < 16; i++) {
716
138k
        int v = FFMAX(lwc_val[i], -512);
717
138k
        msk_val[i] = FFMAX(msk_val[i] + v, 0);
718
138k
    }
719
8.63k
}
720
721
static void bit_allocate(int nb_exponent, int nb_code, int fr_code,
722
                         int *exp, int *bap,
723
                         int fg_spc, int fg_ofs, int msk_mod, int snr_ofs)
724
12.8k
{
725
12.8k
    int msk_val[MAX_BIAS_EXP];
726
12.8k
    int psd_val[MAX_BIAS_EXP];
727
12.8k
    int fast_leak  = 0;
728
12.8k
    int slow_leak  = 0;
729
12.8k
    int dc_code    = dc_code_tab[fr_code - 1];
730
12.8k
    int ht_code    = ht_code_tab[fr_code - 1];
731
12.8k
    int fast_gain  = fast_gain_tab[fg_ofs];
732
12.8k
    int slow_decay = slow_decay_tab[dc_code][msk_mod];
733
12.8k
    int misc_decay = misc_decay_tab[nb_code][dc_code][msk_mod];
734
12.8k
    const uint16_t *slow_gain      = slow_gain_tab[nb_code][msk_mod];
735
12.8k
    const uint16_t *fast_decay     = fast_decay_tab[nb_code][dc_code][msk_mod];
736
12.8k
    const uint16_t *fast_gain_adj  = fast_gain_adj_tab[nb_code][dc_code];
737
12.8k
    const uint16_t *hearing_thresh = hearing_thresh_tab[nb_code][ht_code];
738
12.8k
    int i;
739
740
461k
    for (i = 0; i < nb_exponent; i++)
741
448k
        psd_val[i] = (48 - exp[i]) * 64;
742
743
12.8k
    fast_gain_adj += band_ofs_tab[nb_code][fg_spc];
744
461k
    for (i = 0; i < nb_exponent; i++) {
745
448k
        fast_leak = log_add(fast_leak  - fast_decay[i],
746
448k
                            psd_val[i] - fast_gain + fast_gain_adj[i]);
747
448k
        slow_leak = log_add(slow_leak  - slow_decay,
748
448k
                            psd_val[i] - slow_gain[i]);
749
448k
        msk_val[i] = FFMAX(fast_leak, slow_leak);
750
448k
    }
751
752
12.8k
    fast_leak = 0;
753
286k
    for (i = nb_exponent - 1; i > band_low_tab[nb_code]; i--) {
754
273k
        fast_leak = log_add(fast_leak - misc_decay, psd_val[i] - fast_gain);
755
273k
        msk_val[i] = FFMAX(msk_val[i], fast_leak);
756
273k
    }
757
758
461k
    for (i = 0; i < nb_exponent; i++)
759
448k
        msk_val[i] = FFMAX(msk_val[i], hearing_thresh[i]);
760
761
12.8k
    if (!nb_code)
762
8.63k
        calc_lowcomp(msk_val);
763
764
461k
    for (i = 0; i < nb_exponent; i++) {
765
448k
        int v = 16 * (snr_ofs - 64) + psd_val[i] - msk_val[i] >> 5;
766
448k
        bap[i] = bap_tab[av_clip_uintp2(v, 6)];
767
448k
    }
768
12.8k
}
769
770
static int parse_bit_alloc(DBEDecodeContext *s1, DBEChannel *c)
771
12.8k
{
772
12.8k
    DBEContext *s = &s1->dectx;
773
12.8k
    DBEGroup *p, *g;
774
12.8k
    int bap_strategy[MAX_GROUPS], fg_spc[MAX_GROUPS];
775
12.8k
    int fg_ofs[MAX_GROUPS], msk_mod[MAX_GROUPS];
776
12.8k
    int i, snr_ofs;
777
778
74.6k
    for (i = 0; i < c->nb_groups; i++) {
779
61.7k
        bap_strategy[i] = !i || get_bits1(&s->gb);
780
61.7k
        if (bap_strategy[i]) {
781
20.3k
             fg_spc[i] = get_bits(&s->gb, 2);
782
20.3k
             fg_ofs[i] = get_bits(&s->gb, 3);
783
20.3k
            msk_mod[i] = get_bits1(&s->gb);
784
41.3k
        } else {
785
41.3k
             fg_spc[i] =  fg_spc[i - 1];
786
41.3k
             fg_ofs[i] =  fg_ofs[i - 1];
787
41.3k
            msk_mod[i] = msk_mod[i - 1];
788
41.3k
        }
789
61.7k
    }
790
791
12.8k
    if (get_bits1(&s->gb)) {
792
925
        avpriv_report_missing_feature(s->avctx, "Delta bit allocation");
793
925
        return AVERROR_PATCHWELCOME;
794
925
    }
795
796
11.9k
    snr_ofs = get_bits(&s->gb, 8);
797
11.9k
    if (!snr_ofs) {
798
4.00k
        memset(c->bap, 0, sizeof(c->bap));
799
4.00k
        return 0;
800
4.00k
    }
801
802
49.2k
    for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
803
41.3k
        if (c->exp_strategy[i] || bap_strategy[i]) {
804
12.8k
            bit_allocate(g->nb_exponent, g->imdct_idx, s->metadata.fr_code,
805
12.8k
                         c->exponents + g->exp_ofs, c->bap + g->exp_ofs,
806
12.8k
                         fg_spc[i], fg_ofs[i], msk_mod[i], snr_ofs);
807
28.4k
        } else {
808
28.4k
            memcpy(c->bap + g->exp_ofs,
809
28.4k
                   c->bap + p->exp_ofs,
810
28.4k
                   g->nb_exponent * sizeof(c->bap[0]));
811
28.4k
        }
812
41.3k
    }
813
814
7.91k
    return 0;
815
11.9k
}
816
817
static int parse_indices(DBEContext *s, DBEChannel *c)
818
11.9k
{
819
11.9k
    DBEGroup *p, *g;
820
11.9k
    int i, j;
821
822
67.9k
    for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
823
56.6k
        if (get_bits1(&s->gb)) {
824
5.05k
            int start = get_bits(&s->gb, 6);
825
826
5.05k
            if (start > g->nb_exponent) {
827
533
                av_log(s->avctx, AV_LOG_ERROR, "Invalid start index\n");
828
533
                return AVERROR_INVALIDDATA;
829
533
            }
830
831
123k
            for (j = 0; j < start; j++)
832
118k
                c->idx[g->exp_ofs + j] = 0;
833
834
42.1k
            for (; j < g->nb_exponent; j++)
835
37.6k
                c->idx[g->exp_ofs + j] = get_bits(&s->gb, 2);
836
51.5k
        } else if (i && g->nb_exponent == p->nb_exponent) {
837
39.3k
            memcpy(c->idx + g->exp_ofs,
838
39.3k
                   c->idx + p->exp_ofs,
839
39.3k
                   g->nb_exponent * sizeof(c->idx[0]));
840
39.3k
        } else {
841
12.2k
            memset(c->idx + g->exp_ofs, 0, g->nb_exponent * sizeof(c->idx[0]));
842
12.2k
        }
843
56.6k
    }
844
845
11.3k
    return 0;
846
11.9k
}
847
848
629
#define MIN_MANTISSAS 800
849
static int parse_mantissas(DBEContext *s, DBEChannel *c)
850
11.3k
{
851
11.3k
    DBEGroup *g;
852
11.3k
    int i, j, k;
853
854
67.2k
    for (i = 0, g = c->groups; i < c->nb_groups; i++, g++) {
855
55.8k
        float *mnt = c->mantissas + g->mnt_ofs;
856
857
2.03M
        for (j = 0; j < g->nb_exponent; j++) {
858
1.98M
            int bap     = c->bap[g->exp_ofs + j];
859
1.98M
            int idx     = c->idx[g->exp_ofs + j];
860
1.98M
            int size1   = mantissa_size1[bap][idx];
861
1.98M
            int count   = g->nb_mantissa[j];
862
1.98M
            float exp   = exponent_tab[c->exponents[g->exp_ofs + j]];
863
1.98M
            float scale = mantissa_tab1[size1][idx] * exp;
864
865
1.98M
            if (!size1) {
866
1.00M
                memset(mnt, 0, count * sizeof(*mnt));
867
1.00M
            } else if (idx) {
868
15.1k
                int values[100];
869
15.1k
                int escape = -(1 << size1 - 1);
870
871
77.9k
                for (k = 0; k < count; k++)
872
62.8k
                    values[k] = get_sbits(&s->gb, size1);
873
874
77.9k
                for (k = 0; k < count; k++) {
875
62.8k
                    if (values[k] != escape) {
876
49.9k
                        mnt[k] = values[k] * scale;
877
49.9k
                    } else {
878
12.9k
                        int size2 = mantissa_size2[bap][idx];
879
12.9k
                        int value = get_sbits(&s->gb, size2);
880
12.9k
                        float a = mantissa_tab2[size2][idx];
881
12.9k
                        float b = mantissa_tab3[size2][idx];
882
12.9k
                        if (value < 0)
883
11.8k
                            mnt[k] = ((value + 1) * a - b) * exp;
884
1.06k
                        else
885
1.06k
                            mnt[k] = (value * a + b) * exp;
886
12.9k
                    }
887
62.8k
                }
888
958k
            } else {
889
958k
                if (i == c->nb_groups - 1
890
163k
                    && count * size1 > get_bits_left(&s->gb)
891
160k
                    && get_bits_left(&s->gb) >= 0
892
629
                    && (int)(mnt - c->mantissas) >= MIN_MANTISSAS) {
893
201
                    av_log(s->avctx, AV_LOG_WARNING, "Truncated mantissas @%d, highest frequencies not recoverable\n", (int)(mnt - c->mantissas));
894
201
                    break;
895
201
                }
896
3.87M
                for (k = 0; k < count; k++)
897
2.91M
                    mnt[k] = get_sbits(&s->gb, size1) * scale;
898
958k
            }
899
900
1.98M
            mnt += count;
901
1.98M
        }
902
903
260k
        for (; j < g->nb_exponent + c->bw_code; j++) {
904
205k
            memset(mnt, 0, g->nb_mantissa[j] * sizeof(*mnt));
905
205k
            mnt += g->nb_mantissa[j];
906
205k
        }
907
55.8k
    }
908
909
11.3k
    return 0;
910
11.3k
}
911
912
static int parse_channel(DBEDecodeContext *s1, int ch, int seg_id)
913
15.0k
{
914
15.0k
    DBEContext *s = &s1->dectx;
915
15.0k
    DBEChannel *c = &s1->channels[seg_id][ch];
916
15.0k
    int i, ret;
917
918
15.0k
    if (s->metadata.rev_id[ch] > 1) {
919
1.81k
        avpriv_report_missing_feature(s->avctx, "Encoder revision %d", s->metadata.rev_id[ch]);
920
1.81k
        return AVERROR_PATCHWELCOME;
921
1.81k
    }
922
923
13.2k
    if (ch == lfe_channel_tab[s->metadata.prog_conf]) {
924
2.75k
        c->gr_code = 3;
925
2.75k
        c->bw_code = 29;
926
10.4k
    } else {
927
10.4k
        c->gr_code = get_bits(&s->gb, 2);
928
10.4k
        c->bw_code = get_bits(&s->gb, 3);
929
10.4k
        if (c->gr_code == 3) {
930
408
            av_log(s->avctx, AV_LOG_ERROR, "Invalid group type code\n");
931
408
            return AVERROR_INVALIDDATA;
932
408
        }
933
10.4k
    }
934
935
12.8k
    c->nb_groups   = nb_groups_tab[c->gr_code];
936
12.8k
    c->nb_mstr_exp = nb_mstr_exp_tab[c->gr_code];
937
938
74.6k
    for (i = 0; i < c->nb_groups; i++) {
939
61.7k
        c->groups[i] = frm_ofs_tab[seg_id][c->gr_code][i];
940
61.7k
        if (c->nb_mstr_exp == 2) {
941
59.0k
            c->groups[i].nb_exponent    -= c->bw_code;
942
59.0k
            c->groups[i].nb_bias_exp[1] -= c->bw_code;
943
59.0k
        }
944
61.7k
    }
945
946
12.8k
    if ((ret = parse_exponents(s, c)) < 0)
947
0
        return ret;
948
12.8k
    if ((ret = parse_bit_alloc(s1, c)) < 0)
949
925
        return ret;
950
11.9k
    if ((ret = parse_indices(s, c)) < 0)
951
533
        return ret;
952
11.3k
    if ((ret = parse_mantissas(s, c)) < 0)
953
0
        return ret;
954
955
11.3k
    if (get_bits_left(&s->gb) < 0) {
956
6.49k
        av_log(s->avctx, AV_LOG_ERROR, "Read past end of channel %d\n", ch);
957
6.49k
        return AVERROR_INVALIDDATA;
958
6.49k
    }
959
960
4.89k
    return 0;
961
11.3k
}
962
963
static int parse_audio(DBEDecodeContext *s1, int start, int end, int seg_id)
964
484k
{
965
484k
    DBEContext *s = &s1->dectx;
966
484k
    int ch, ret, key;
967
968
484k
    if ((key = parse_key(s)) < 0)
969
203
        return key;
970
971
1.48M
    for (ch = start; ch < end; ch++) {
972
1.00M
        if (!s->metadata.ch_size[ch]) {
973
984k
            s1->channels[seg_id][ch].nb_groups = 0;
974
984k
            continue;
975
984k
        }
976
17.2k
        ret = ff_dolby_e_convert_input(s, s->metadata.ch_size[ch], key);
977
17.2k
        if (ret < 0)
978
2.22k
            return ret;
979
15.0k
        if ((ret = parse_channel(s1, ch, seg_id)) < 0) {
980
10.1k
            if (s1->avctx->err_recognition & AV_EF_EXPLODE)
981
361
                return ret;
982
9.81k
            s1->channels[seg_id][ch].nb_groups = 0;
983
9.81k
        }
984
14.7k
        if ((ret = skip_input(s, s->metadata.ch_size[ch])) < 0)
985
0
            return ret;
986
14.7k
    }
987
988
481k
    return skip_input(s, 1);
989
483k
}
990
991
static int parse_meter(DBEDecodeContext *s1)
992
118k
{
993
118k
    DBEContext *s = &s1->dectx;
994
118k
    if (s->metadata.meter_size)
995
811
        return skip_input(s, s->key_present + s->metadata.meter_size + 1);
996
117k
    return 0;
997
118k
}
998
999
static void imdct_calc(DBEDecodeContext *s1, DBEGroup *g, float *result, float *values)
1000
14.7k
{
1001
14.7k
    AVTXContext *imdct = s1->imdct[g->imdct_phs == 1][g->imdct_idx];
1002
14.7k
    av_tx_fn  imdct_fn = s1->imdct_fn[g->imdct_phs == 1][g->imdct_idx];
1003
14.7k
    int n   = 1 << imdct_bits_tab[g->imdct_idx];
1004
14.7k
    int n2  = n >> 1;
1005
1006
14.7k
    switch (g->imdct_phs) {
1007
2.51k
    case 0:
1008
2.51k
        imdct_fn(imdct, result, values, sizeof(float));
1009
2.05M
        for (int i = 0; i < n2; i++)
1010
2.05M
            result[n2 + i] = result[n2 - i - 1];
1011
2.51k
        break;
1012
10.2k
    case 1:
1013
10.2k
        imdct_fn(imdct, result, values, sizeof(float));
1014
10.2k
        break;
1015
1.99k
    case 2:
1016
1.99k
        imdct_fn(imdct, result + n2, values, sizeof(float));
1017
1.24M
        for (int i = 0; i < n2; i++)
1018
1.24M
            result[i] = -result[n - i - 1];
1019
1.99k
        break;
1020
0
    default:
1021
0
        av_assert0(0);
1022
14.7k
    }
1023
14.7k
}
1024
1025
static void transform(DBEDecodeContext *s1, DBEChannel *c, float *history, float *output)
1026
974k
{
1027
974k
    LOCAL_ALIGNED_32(float, buffer, [2048]);
1028
974k
    LOCAL_ALIGNED_32(float, result, [1152]);
1029
974k
    DBEGroup *g;
1030
974k
    int i;
1031
1032
974k
    memset(result, 0, 1152 * sizeof(float));
1033
989k
    for (i = 0, g = c->groups; i < c->nb_groups; i++, g++) {
1034
14.7k
        float *src = buffer + g->src_ofs;
1035
14.7k
        float *dst = result + g->dst_ofs;
1036
14.7k
        float *win = window + g->win_ofs;
1037
1038
14.7k
        imdct_calc(s1, g, buffer, c->mantissas + g->mnt_ofs);
1039
14.7k
        s1->fdsp->vector_fmul_add(dst, src, win, dst, g->win_len);
1040
14.7k
    }
1041
1042
250M
    for (i = 0; i < 256; i++)
1043
249M
        output[i] = history[i] + result[i];
1044
624M
    for (i = 256; i < 896; i++)
1045
623M
        output[i] = result[i];
1046
250M
    for (i = 0; i < 256; i++)
1047
249M
        history[i] = result[896 + i];
1048
974k
}
1049
1050
static void apply_gain(DBEDecodeContext *s, int begin, int end, float *output)
1051
487k
{
1052
487k
    if (begin == 960 && end == 960)
1053
724
        return;
1054
1055
486k
    if (begin == end) {
1056
175k
        s->fdsp->vector_fmul_scalar(output, output, gain_tab[end], FRAME_SAMPLES);
1057
310k
    } else {
1058
310k
        float a = gain_tab[begin] * (1.0f / (FRAME_SAMPLES - 1));
1059
310k
        float b = gain_tab[end  ] * (1.0f / (FRAME_SAMPLES - 1));
1060
310k
        int i;
1061
1062
557M
        for (i = 0; i < FRAME_SAMPLES; i++)
1063
557M
            output[i] *= a * (FRAME_SAMPLES - i - 1) + b * i;
1064
310k
    }
1065
486k
}
1066
1067
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
1068
117k
{
1069
117k
    const DolbyEHeaderInfo *const metadata = &s->dectx.metadata;
1070
117k
    const uint8_t *reorder;
1071
117k
    int ch, ret;
1072
1073
117k
    if (metadata->nb_channels == 4)
1074
113k
        reorder = ch_reorder_4;
1075
4.21k
    else if (metadata->nb_channels == 6)
1076
343
        reorder = ch_reorder_6;
1077
3.87k
    else if (metadata->nb_programs == 1 && metadata->output_channel_order == CHANNEL_ORDER_DEFAULT)
1078
1.53k
        reorder = ch_reorder_8;
1079
2.34k
    else
1080
2.34k
        reorder = ch_reorder_n;
1081
1082
117k
    frame->nb_samples = FRAME_SAMPLES;
1083
117k
    if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0)
1084
0
        return ret;
1085
1086
605k
    for (ch = 0; ch < metadata->nb_channels; ch++) {
1087
487k
        float *output = (float *)frame->extended_data[reorder[ch]];
1088
487k
        transform(s, &s->channels[0][ch], s->history[ch], output);
1089
487k
        transform(s, &s->channels[1][ch], s->history[ch], output + FRAME_SAMPLES / 2);
1090
487k
        apply_gain(s, metadata->begin_gain[ch], metadata->end_gain[ch], output);
1091
487k
    }
1092
1093
117k
    return 0;
1094
117k
}
1095
1096
static int dolby_e_decode_frame(AVCodecContext *avctx, AVFrame *frame,
1097
                                int *got_frame_ptr, AVPacket *avpkt)
1098
241k
{
1099
241k
    DBEDecodeContext *s1 = avctx->priv_data;
1100
241k
    DBEContext *s = &s1->dectx;
1101
241k
    int i, j, ret;
1102
1103
241k
    if ((ret = ff_dolby_e_parse_header(s, avpkt->data, avpkt->size)) < 0)
1104
117k
        return ret;
1105
1106
123k
    if (s->metadata.nb_programs > 1 && !s->metadata.multi_prog_warned) {
1107
241
        av_log(avctx, AV_LOG_WARNING, "Stream has %d programs (configuration %d), "
1108
241
               "channels will be output in native order.\n",
1109
241
               s->metadata.nb_programs, s->metadata.prog_conf);
1110
241
        s->metadata.multi_prog_warned = 1;
1111
241
    }
1112
1113
123k
    av_channel_layout_uninit(&avctx->ch_layout);
1114
123k
    switch (s->metadata.nb_channels) {
1115
118k
    case 4:
1116
118k
        avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT0;
1117
118k
        break;
1118
698
    case 6:
1119
698
        avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1;
1120
698
        break;
1121
4.81k
    case 8:
1122
4.81k
        avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_7POINT1;
1123
4.81k
        break;
1124
0
    default:
1125
0
        avctx->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
1126
0
        avctx->ch_layout.nb_channels = s->metadata.nb_channels;
1127
0
        break;
1128
123k
    }
1129
1130
123k
    avctx->sample_rate = s->metadata.sample_rate;
1131
123k
    avctx->sample_fmt  = AV_SAMPLE_FMT_FLTP;
1132
1133
123k
    i = s->metadata.nb_channels / 2;
1134
123k
    j = s->metadata.nb_channels;
1135
123k
    if ((ret = parse_audio(s1, 0, i, 0)) < 0)
1136
2.85k
        return ret;
1137
120k
    if ((ret = parse_audio(s1, i, j, 0)) < 0)
1138
571
        return ret;
1139
120k
    if ((ret = parse_metadata_ext(s1)) < 0)
1140
229
        return ret;
1141
120k
    if ((ret = parse_audio(s1, 0, i, 1)) < 0)
1142
790
        return ret;
1143
119k
    if ((ret = parse_audio(s1, i, j, 1)) < 0)
1144
1.22k
        return ret;
1145
118k
    if ((ret = parse_meter(s1)) < 0)
1146
322
        return ret;
1147
117k
    if ((ret = filter_frame(s1, frame)) < 0)
1148
0
        return ret;
1149
1150
117k
    *got_frame_ptr = 1;
1151
117k
    return avpkt->size;
1152
117k
}
1153
1154
static av_cold void dolby_e_flush(AVCodecContext *avctx)
1155
107k
{
1156
107k
    DBEDecodeContext *s = avctx->priv_data;
1157
1158
107k
    memset(s->history, 0, sizeof(s->history));
1159
107k
}
1160
1161
static av_cold int dolby_e_close(AVCodecContext *avctx)
1162
2.26k
{
1163
2.26k
    DBEDecodeContext *s = avctx->priv_data;
1164
1165
9.06k
    for (int i = 0; i < 3; i++) {
1166
6.80k
        av_tx_uninit(&s->imdct[0][i]);
1167
6.80k
        av_tx_uninit(&s->imdct[1][i]);
1168
6.80k
    }
1169
1170
2.26k
    av_freep(&s->fdsp);
1171
2.26k
    return 0;
1172
2.26k
}
1173
1174
1175
static av_cold void init_tables(void)
1176
1
{
1177
1
    int i, j;
1178
1179
17
    for (i = 1; i < 17; i++)
1180
16
        mantissa_tab1[i][0] = 1.0f / (1 << i - 1);
1181
1182
15
    for (i = 2; i < 16; i++) {
1183
14
        mantissa_tab1[i][1] = 1.0f  / ((1 << i) - 1);
1184
14
        mantissa_tab1[i][2] = 0.5f  / ((1 << i) - 1);
1185
14
        mantissa_tab1[i][3] = 0.25f / ((1 << i) - 1);
1186
14
    }
1187
1188
1
    mantissa_tab1[i][1] = 0.5f   / (1 << 15);
1189
1
    mantissa_tab1[i][2] = 0.75f  / (1 << 15);
1190
1
    mantissa_tab1[i][3] = 0.875f / (1 << 15);
1191
1192
17
    for (i = 1; i < 17; i++) {
1193
16
        mantissa_tab2[i][1] = mantissa_tab1[i][0] * 0.5f;
1194
16
        mantissa_tab2[i][2] = mantissa_tab1[i][0] * 0.75f;
1195
16
        mantissa_tab2[i][3] = mantissa_tab1[i][0] * 0.875f;
1196
64
        for (j = 1; j < 4; j++)
1197
48
            mantissa_tab3[i][j] = 1.0f / (1 << i) + 1.0f / (1 << j) - 1.0f / (1 << i + j);
1198
16
    }
1199
1200
1
    mantissa_tab3[1][3] = 0.6875f;
1201
1202
26
    for (i = 0; i < 25; i++) {
1203
25
        exponent_tab[i * 2    ] = 1.0f      / (1 << i);
1204
25
        exponent_tab[i * 2 + 1] = M_SQRT1_2 / (1 << i);
1205
25
    }
1206
1207
1.02k
    for (i = 1; i < 1024; i++)
1208
1.02k
        gain_tab[i] = exp2f((i - 960) / 64.0f);
1209
1210
    // short 1
1211
1
    ff_kbd_window_init(window, 3.0f, 128);
1212
129
    for (i = 0; i < 128; i++)
1213
128
        window[128 + i] = window[127 - i];
1214
1215
    // start
1216
193
    for (i = 0; i < 192; i++)
1217
192
        window[256 + i] = start_window[i];
1218
1219
    // short 2
1220
193
    for (i = 0; i < 192; i++)
1221
192
        window[448 + i] = short_window2[i];
1222
65
    for (i = 0; i < 64; i++)
1223
64
        window[640 + i] = window[63 - i];
1224
1225
    // short 3
1226
65
    for (i = 0; i < 64; i++)
1227
64
        window[704 + i] = short_window3[i];
1228
193
    for (i = 0; i < 192; i++)
1229
192
        window[768 + i] = window[64 + i];
1230
1231
    // bridge
1232
129
    for (i = 0; i < 128; i++)
1233
128
        window[960 + i] = window[i];
1234
65
    for (i = 0; i < 64; i++)
1235
64
        window[1088 + i] = 1.0f;
1236
1237
    // long
1238
1
    ff_kbd_window_init(window + 1408, 3.0f, 256);
1239
641
    for (i = 0; i < 640; i++)
1240
640
        window[1664 + i] = 1.0f;
1241
257
    for (i = 0; i < 256; i++)
1242
256
        window[2304 + i] = window[1152 + i] = window[1663 - i];
1243
1244
    // reverse start
1245
193
    for (i = 0; i < 192; i++)
1246
192
        window[2560 + i] = window[447 - i];
1247
1248
    // reverse short 2
1249
257
    for (i = 0; i < 256; i++)
1250
256
        window[2752 + i] = window[703 - i];
1251
1252
    // reverse short 3
1253
257
    for (i = 0; i < 256; i++)
1254
256
        window[3008 + i] = window[959 - i];
1255
1256
    // reverse bridge
1257
449
    for (i = 0; i < 448; i++)
1258
448
        window[3264 + i] = window[1407 - i];
1259
1
}
1260
1261
static av_cold int dolby_e_init(AVCodecContext *avctx)
1262
2.26k
{
1263
2.26k
    static AVOnce init_once = AV_ONCE_INIT;
1264
2.26k
    DBEDecodeContext *s = avctx->priv_data;
1265
2.26k
    float scale = 2.0f;
1266
2.26k
    int ret;
1267
1268
2.26k
    if (ff_thread_once(&init_once, init_tables))
1269
0
        return AVERROR_UNKNOWN;
1270
1271
9.06k
    for (int i = 0; i < 3; i++) {
1272
6.80k
        if ((ret = av_tx_init(&s->imdct[0][i], &s->imdct_fn[0][i], AV_TX_FLOAT_MDCT,
1273
6.80k
                              1, 1 << imdct_bits_tab[i] - 1, &scale, 0)) < 0)
1274
0
            return ret;
1275
6.80k
        if ((ret = av_tx_init(&s->imdct[1][i], &s->imdct_fn[1][i], AV_TX_FLOAT_MDCT,
1276
6.80k
                              1, 1 << imdct_bits_tab[i] - 1, &scale, AV_TX_FULL_IMDCT)) < 0)
1277
0
            return ret;
1278
6.80k
    }
1279
1280
2.26k
    if (!(s->fdsp = avpriv_float_dsp_alloc(0)))
1281
0
        return AVERROR(ENOMEM);
1282
1283
2.26k
    s->dectx.metadata.multi_prog_warned = s->dectx.metadata.output_channel_order == CHANNEL_ORDER_CODED;
1284
2.26k
    s->dectx.avctx = s->avctx = avctx;
1285
2.26k
    return 0;
1286
2.26k
}
1287
1288
#define OFFSET(x) offsetof(DBEDecodeContext, x)
1289
#define FLAGS (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1290
static const AVOption options[] = {
1291
    { "channel_order", "Order in which the channels are to be exported",
1292
        OFFSET(dectx.metadata.output_channel_order), AV_OPT_TYPE_INT,
1293
        { .i64 = CHANNEL_ORDER_DEFAULT }, 0, 1, FLAGS, .unit = "channel_order" },
1294
      { "default", "normal libavcodec channel order", 0, AV_OPT_TYPE_CONST,
1295
        { .i64 = CHANNEL_ORDER_DEFAULT }, .flags = FLAGS, .unit = "channel_order" },
1296
      { "coded",    "order in which the channels are coded in the bitstream",
1297
        0, AV_OPT_TYPE_CONST, { .i64 = CHANNEL_ORDER_CODED }, .flags = FLAGS, .unit = "channel_order" },
1298
1299
      { NULL },
1300
};
1301
1302
static const AVClass dolby_e_decoder_class = {
1303
    .class_name = "Dolby E decoder",
1304
    .item_name  = av_default_item_name,
1305
    .option     = options,
1306
    .version    = LIBAVUTIL_VERSION_INT,
1307
};
1308
1309
const FFCodec ff_dolby_e_decoder = {
1310
    .p.name         = "dolby_e",
1311
    CODEC_LONG_NAME("Dolby E"),
1312
    .p.type         = AVMEDIA_TYPE_AUDIO,
1313
    .p.id           = AV_CODEC_ID_DOLBY_E,
1314
    .priv_data_size = sizeof(DBEDecodeContext),
1315
    .p.priv_class   = &dolby_e_decoder_class,
1316
    .init           = dolby_e_init,
1317
    FF_CODEC_DECODE_CB(dolby_e_decode_frame),
1318
    .close          = dolby_e_close,
1319
    .flush          = dolby_e_flush,
1320
    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
1321
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
1322
};