FFmpeg
dolby_e.c
Go to the documentation of this file.
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 "kbdwin.h"
31 #include "fft.h"
32 
33 #define MAX_SEGMENTS 2
34 
35 #define MAX_GROUPS 8
36 #define MAX_EXPONENTS 304
37 #define MAX_MANTISSAS 1024
38 
39 #define MAX_MSTR_EXP 2
40 #define MAX_BIAS_EXP 50
41 
42 typedef struct DBEGroup {
45  uint16_t exp_ofs;
46  uint16_t mnt_ofs;
50  uint16_t win_len;
51  uint16_t dst_ofs;
52  uint16_t win_ofs;
53  uint16_t src_ofs;
54 } DBEGroup;
55 
56 typedef struct DBEChannel {
57  int gr_code;
58  int bw_code;
59 
60  int nb_groups;
63 
64  int exp_strategy[MAX_GROUPS];
65  int exponents[MAX_EXPONENTS];
66  int bap[MAX_EXPONENTS];
67  int idx[MAX_EXPONENTS];
68 
69  DECLARE_ALIGNED(32, float, mantissas)[MAX_MANTISSAS];
70 } DBEChannel;
71 
72 typedef struct DBEDecodeContext {
75 
77 
79 
80  DECLARE_ALIGNED(32, float, history)[MAX_CHANNELS][256];
81 
82  FFTContext imdct[3];
85 
86 static const int8_t lfe_channel_tab[MAX_PROG_CONF + 1] = {
87  5, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, 4,
88  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 5, 5
89 };
90 
91 static const uint8_t ch_reorder_4[4] = { 0, 2, 1, 3 };
92 static const uint8_t ch_reorder_6[6] = { 0, 2, 4, 1, 3, 5 };
93 static const uint8_t ch_reorder_8[8] = { 0, 2, 6, 4, 1, 3, 7, 5 };
94 static const uint8_t ch_reorder_n[8] = { 0, 2, 4, 6, 1, 3, 5, 7 };
95 
96 
97 static const uint8_t nb_groups_tab[4] = { 1, 8, 7, 1 };
98 
99 static const uint8_t nb_mstr_exp_tab[4] = { 2, 2, 2, 1 };
100 
101 static const uint8_t nb_mantissa_38[38] = {
102  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
103  2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6,
104  7, 8, 9, 10, 11, 12,
105 };
106 
107 static const uint8_t nb_mantissa_44[44] = {
108  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
109  2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 7, 7,
110  8, 9, 10, 11, 12, 13, 15, 16, 18, 20, 22, 25,
111 };
112 
113 static const uint8_t nb_mantissa_50[50] = {
114  1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3,
115  3, 4, 4, 5, 5, 6, 6, 7, 8, 9, 9, 10, 12, 13, 14, 16,
116  18, 19, 22, 24, 27, 29, 32, 36, 40, 44, 49, 54, 60, 66, 74, 82,
117  90, 100,
118 };
119 
120 static const uint8_t imdct_bits_tab[3] = { 8, 9, 11 };
121 
122 static const DBEGroup grp_tab_0[1] = {
123  { 50, { 27, 23 }, 0, 0, nb_mantissa_50, 2, 0, 1152, 0, 1408, 0 },
124 };
125 
126 static const DBEGroup grp_tab_1[8] = {
127  { 38, { 12, 26 }, 0, 0, nb_mantissa_38, 0, 0, 192, 0, 256, 0 },
128  { 38, { 12, 26 }, 38, 128, nb_mantissa_38, 0, 1, 256, 64, 448, 0 },
129  { 38, { 12, 26 }, 76, 256, nb_mantissa_38, 0, 1, 256, 192, 704, 0 },
130  { 38, { 12, 26 }, 114, 384, nb_mantissa_38, 0, 1, 256, 320, 0, 0 },
131  { 38, { 12, 26 }, 152, 512, nb_mantissa_38, 0, 1, 256, 448, 0, 0 },
132  { 38, { 12, 26 }, 190, 640, nb_mantissa_38, 0, 1, 256, 576, 0, 0 },
133  { 38, { 12, 26 }, 228, 768, nb_mantissa_38, 0, 1, 256, 704, 0, 0 },
134  { 38, { 12, 26 }, 266, 896, nb_mantissa_38, 0, 1, 256, 832, 0, 0 },
135 };
136 
137 static const DBEGroup grp_tab_2[7] = {
138  { 38, { 12, 26 }, 0, 0, nb_mantissa_38, 0, 0, 192, 0, 256, 0 },
139  { 38, { 12, 26 }, 38, 128, nb_mantissa_38, 0, 1, 256, 64, 448, 0 },
140  { 38, { 12, 26 }, 76, 256, nb_mantissa_38, 0, 1, 256, 192, 704, 0 },
141  { 38, { 12, 26 }, 114, 384, nb_mantissa_38, 0, 1, 256, 320, 0, 0 },
142  { 38, { 12, 26 }, 152, 512, nb_mantissa_38, 0, 1, 256, 448, 0, 0 },
143  { 38, { 12, 26 }, 190, 640, nb_mantissa_38, 0, 1, 256, 576, 0, 0 },
144  { 44, { 19, 25 }, 228, 768, nb_mantissa_44, 1, 1, 448, 704, 960, 64 },
145 };
146 
147 static const DBEGroup grp_tab_3[1] = {
148  { 21, { 21 }, 0, 0, nb_mantissa_50, 2, 0, 1152, 0, 1408, 0 },
149 };
150 
151 static const DBEGroup grp_tab_4[1] = {
152  { 50, { 27, 23 }, 0, 0, nb_mantissa_50, 2, 2, 1152, 0, 1408, 896 },
153 };
154 
155 static const DBEGroup grp_tab_5[8] = {
156  { 38, { 12, 26 }, 0, 0, nb_mantissa_38, 0, 1, 256, 64, 0, 0 },
157  { 38, { 12, 26 }, 38, 128, nb_mantissa_38, 0, 1, 256, 192, 0, 0 },
158  { 38, { 12, 26 }, 76, 256, nb_mantissa_38, 0, 1, 256, 320, 0, 0 },
159  { 38, { 12, 26 }, 114, 384, nb_mantissa_38, 0, 1, 256, 448, 0, 0 },
160  { 38, { 12, 26 }, 152, 512, nb_mantissa_38, 0, 1, 256, 576, 0, 0 },
161  { 38, { 12, 26 }, 190, 640, nb_mantissa_38, 0, 1, 256, 704, 3008, 0 },
162  { 38, { 12, 26 }, 228, 768, nb_mantissa_38, 0, 1, 256, 832, 2752, 0 },
163  { 38, { 12, 26 }, 266, 896, nb_mantissa_38, 0, 2, 192, 960, 2560, 64 },
164 };
165 
166 static const DBEGroup grp_tab_6[7] = {
167  { 44, { 19, 25 }, 0, 0, nb_mantissa_44, 1, 1, 448, 0, 3264, 0 },
168  { 38, { 12, 26 }, 44, 256, nb_mantissa_38, 0, 1, 256, 320, 0, 0 },
169  { 38, { 12, 26 }, 82, 384, nb_mantissa_38, 0, 1, 256, 448, 0, 0 },
170  { 38, { 12, 26 }, 120, 512, nb_mantissa_38, 0, 1, 256, 576, 0, 0 },
171  { 38, { 12, 26 }, 158, 640, nb_mantissa_38, 0, 1, 256, 704, 3008, 0 },
172  { 38, { 12, 26 }, 196, 768, nb_mantissa_38, 0, 1, 256, 832, 2752, 0 },
173  { 38, { 12, 26 }, 234, 896, nb_mantissa_38, 0, 2, 192, 960, 2560, 64 },
174 };
175 
176 static const DBEGroup grp_tab_7[1] = {
177  { 21, { 21 }, 0, 0, nb_mantissa_50, 2, 2, 1152, 0, 1408, 896 },
178 };
179 
180 static const DBEGroup *const frm_ofs_tab[2][4] = {
181  { grp_tab_0, grp_tab_1, grp_tab_2, grp_tab_3 },
182  { grp_tab_4, grp_tab_5, grp_tab_6, grp_tab_7 }
183 };
184 
185 static const uint8_t mantissa_size1[16][4] = {
186  { 0, 0, 0, 0 }, { 2, 1, 1, 1 }, { 3, 2, 1, 1 }, { 4, 3, 2, 1 },
187  { 5, 4, 3, 2 }, { 6, 5, 4, 3 }, { 7, 6, 5, 4 }, { 8, 7, 6, 5 },
188  { 9, 8, 7, 6 }, { 10, 9, 8, 7 }, { 11, 10, 9, 8 }, { 12, 11, 10, 9 },
189  { 13, 12, 11, 10 }, { 14, 13, 12, 11 }, { 15, 14, 13, 12 }, { 16, 15, 14, 13 },
190 };
191 
192 static const uint8_t mantissa_size2[16][4] = {
193  { 0, 0, 0, 0 }, { 2, 1, 2, 2 }, { 3, 2, 3, 3 }, { 4, 3, 4, 4 },
194  { 5, 4, 5, 5 }, { 6, 5, 6, 6 }, { 7, 6, 7, 7 }, { 8, 7, 8, 8 },
195  { 9, 8, 9, 9 }, { 10, 9, 10, 10 }, { 11, 10, 11, 11 }, { 12, 11, 12, 12 },
196  { 13, 12, 13, 13 }, { 14, 13, 14, 14 }, { 15, 14, 15, 15 }, { 16, 15, 16, 16 },
197 };
198 
199 static const float start_window[192] = {
200  0.00161569379826, 0.00185748233347, 0.00198562758548, 0.00207834078104,
201  0.00215717748523, 0.00223067096393, 0.00230299213147, 0.00237651215396,
202  0.00245275561606, 0.00253281402069, 0.00261754673613, 0.00270768786168,
203  0.00280390761895, 0.00290684998656, 0.00301715751161, 0.00313548872798,
204  0.00326253122934, 0.00339901215995, 0.00354570716636, 0.00370344845023,
205  0.00387313232586, 0.00405572653911, 0.00425227750970, 0.00446391759265,
206  0.00469187240551, 0.00493746822816, 0.00520213944619, 0.00548743597507,
207  0.00579503056737, 0.00612672586953, 0.00648446105606, 0.00687031782873,
208  0.00728652552677, 0.00773546505205, 0.00821967127415, 0.00874183354619,
209  0.00930479393832, 0.00991154278653, 0.01056521116692, 0.01126905994567,
210  0.01202646513050, 0.01284089936559, 0.01371590957417, 0.01465509096066,
211  0.01566205783408, 0.01674041199523, 0.01789370972358, 0.01912542867865,
212  0.02043893626265, 0.02183746113793, 0.02332406961796, 0.02490164852364,
213  0.02657289580178, 0.02834031974193, 0.03020624702903, 0.03217283918354,
214  0.03424211623810, 0.03641598586180, 0.03869627565015, 0.04108476601498,
215  0.04358322107390, 0.04619341515939, 0.04891715301882, 0.05175628239149,
216 
217  0.05471237327267, 0.05778734733755, 0.06098291402413, 0.06430101352084,
218  0.06774345212186, 0.07131188644726, 0.07500780649199, 0.07883251748595,
219  0.08278712056651, 0.08687249228061, 0.09108926295730, 0.09543779401074,
220  0.09991815425851, 0.10453009536427, 0.10927302653894, 0.11414598865987,
221  0.11914762799220, 0.12427616972097, 0.12952939152560, 0.13490459744934,
222  0.14039859233595, 0.14600765712201, 0.15172752528722, 0.15755336077528,
223  0.16347973770491, 0.16950062219342, 0.17560935661442, 0.18179864660619,
224  0.18806055113821, 0.19438647593012, 0.20076717050010, 0.20719272909882,
225  0.21365259576030, 0.22013557367283, 0.22662983904194, 0.23312295958328,
226  0.23960191774666, 0.24605313873388, 0.25246252333253, 0.25881548554631,
227  0.26509699495987, 0.27129162373316, 0.27738359807707, 0.28335685401987,
228  0.28919509723179, 0.29488186663467, 0.30040060148455, 0.30573471157819,
229  0.31086765019993, 0.31578298939317, 0.32046449711227, 0.32489621578468,
230  0.32906254179156, 0.33294830535654, 0.33653885031840, 0.33982011325336,
231  0.34277870140679, 0.34540196889300, 0.34767809062480, 0.34959613344194,
232  0.35114612391958, 0.35231911235422, 0.35310723244504, 0.35350375621308,
233 
234  0.35350314372945, 0.35310108725579, 0.35229454943591, 0.35108179521634,
235  0.34946241721522, 0.34743735430290, 0.34500890320420, 0.34218072298001,
236  0.33895783229541, 0.33534659943168, 0.33135472505060, 0.32699121776996,
237  0.32226636266000, 0.31719168282019, 0.31177989424432, 0.30604485422875,
238  0.30000150362379, 0.29366580327088, 0.28705466500775, 0.28018587766131,
239  0.27307802848095, 0.26575042049535, 0.25822298630189, 0.25051619882000,
240  0.24265097955783, 0.23464860495522, 0.22653061137548, 0.21831869932335,
241  0.21003463746705, 0.20170016703857, 0.19333690717811, 0.18496626177620,
242  0.17660932835062, 0.16828680947474, 0.16001892724986, 0.15182534128597,
243  0.14372507062477, 0.13573642000364, 0.12787691082233, 0.12016321713317,
244  0.11261110693234, 0.10523538898282, 0.09804986534955, 0.09106728977263,
245  0.08429933194438, 0.07775654768810, 0.07144835495683, 0.06538301547324,
246  0.05956762170687, 0.05400808871425, 0.04870915012107, 0.04367435714993,
247  0.03890607899172, 0.03440550179663, 0.03017262174627, 0.02620622428513,
248  0.02250383492507, 0.01906161305732, 0.01587412848221, 0.01293388032354,
249  0.01023019677288, 0.00774641320626, 0.00545109736891, 0.00325868651263,
250 };
251 
252 static const float short_window2[192] = {
253  0.00018861094606, 0.00033433010202, 0.00050309624485, 0.00070306161748,
254  0.00093995174533, 0.00121913067128, 0.00154606505568, 0.00192647806126,
255  0.00236641248692, 0.00287225985240, 0.00345077377440, 0.00410907465023,
256  0.00485464855241, 0.00569534163219, 0.00663935063508, 0.00769520981249,
257  0.00887177436246, 0.01017820046395, 0.01162392194150, 0.01321862359335,
258  0.01497221122468, 0.01689477844427, 0.01899657030441, 0.02128794388846,
259  0.02377932597692, 0.02648116795039, 0.02940389811590, 0.03255787167130,
260  0.03595331854986, 0.03960028941437, 0.04350860009563, 0.04768777479454,
261  0.05214698838949, 0.05689500821121, 0.06194013566525, 0.06729014809766,
262  0.07295224131210, 0.07893297315602, 0.08523820859989, 0.09187306673620,
263  0.09884187012422, 0.10614809690222, 0.11379433608064, 0.12178224641797,
264  0.13011251926531, 0.13878484574660, 0.14779788861830, 0.15714925912610,
265  0.16683549914631, 0.17685206886673, 0.18719334022589, 0.19785259629099,
266  0.20882203671372, 0.22009278936030, 0.23165492816694, 0.24349749722585,
267  0.25560854105961, 0.26797514099368, 0.28058345748882, 0.29341877824732,
268  0.30646557185942, 0.31970754671026, 0.33312771482295, 0.34670846027024,
269 
270  0.36043161174692, 0.37427851885723, 0.38823013163645, 0.40226708279486,
271  0.41636977214436, 0.43051845264462, 0.44469331748632, 0.45887458761470,
272  0.47304259908636, 0.48717788964798, 0.50126128392546, 0.51527397661778,
273  0.52919761310050, 0.54301436685998, 0.55670701320069, 0.57025899869448,
274  0.58365450587230, 0.59687851269542, 0.60991684638414, 0.62275623122793,
275  0.63538433005035, 0.64778977905593, 0.65996221584264, 0.67189230042379,
276  0.68357172916486, 0.69499324160511, 0.70615062019861, 0.71703868307548,
277  0.72765326998919, 0.73799122168099, 0.74805035295521, 0.75782941981995,
278  0.76732808110520, 0.77654685502339, 0.78548707118622, 0.79415081863423,
279  0.80254089047207, 0.81066072573188, 0.81851434910893, 0.82610630922734,
280  0.83344161609862, 0.84052567843230, 0.84736424144524, 0.85396332579459,
281  0.86032916822973, 0.86646816451999, 0.87238681516918, 0.87809167437532,
282  0.88358930263537, 0.88888622333073, 0.89398888356256, 0.89890361943564,
283  0.90363662591861, 0.90819393133744, 0.91258137648979, 0.91680459830070,
284  0.92086901787718, 0.92477983276087, 0.92854201312583, 0.93216030163834,
285  0.93563921662343, 0.93898305819384, 0.94219591693690, 0.94528168477979,
286 
287  0.94823843319821, 0.95106834367330, 0.95377776558539, 0.95636718335775,
288  0.95883679961479, 0.96118650212341, 0.96341583179195, 0.96552395212906,
289  0.96750962060547, 0.96937116231768, 0.97110644638309, 0.97271286544154,
290  0.97418731862798, 0.97552619834964, 0.97672538116257, 0.97778022299974,
291  0.97868555895586, 0.97943570778357, 0.98002448120255, 0.98044519806866,
292  0.98069070339493, 0.98075339216123, 0.98062523779637, 0.98029782516478,
293  0.97976238784222, 0.97900984942031, 0.97803086854002, 0.97681588731895,
294  0.97535518280755, 0.97363892108474, 0.97165721358452, 0.96940017523145,
295  0.96685798395452, 0.96402094114589, 0.96087953263194, 0.95742448973047,
296  0.95364684997699, 0.94953801711660, 0.94508981997396, 0.94029456983253,
297  0.93514511597504, 0.92963489905951, 0.92375800202883, 0.91750919827624,
298  0.91088399681406, 0.90387868421832, 0.89649036314692, 0.88871698725397,
299  0.88055739234735, 0.87201132366062, 0.86307945913336, 0.85376342861693,
300  0.84406582894455, 0.83399023482637, 0.82354120554757, 0.81272428745995,
301  0.80154601230457, 0.79001389138101, 0.77813640562199, 0.76592299164227,
302  0.75338402384395, 0.74053079267526, 0.72737547915460, 0.71393112578527,
303 };
304 
305 static const float short_window3[64] = {
306  0.00326887936450, 0.00550242900936, 0.00786846643791, 0.01045683453520,
307  0.01330402120132, 0.01643221072863, 0.01985798040609, 0.02359509464766,
308  0.02765559221954, 0.03205025893128, 0.03678884369614, 0.04188015679495,
309  0.04733210987781, 0.05315172583924, 0.05934513287609, 0.06591755045290,
310  0.07287327156378, 0.08021564389822, 0.08794705152307, 0.09606889811179,
311  0.10458159240070, 0.11348453632940, 0.12277611617809, 0.13245369691511,
312  0.14251361989876, 0.15295120402567, 0.16376075037904, 0.17493555039885,
313  0.18646789757072, 0.19834910260891, 0.21056951208995, 0.22311853047787,
314  0.23598464546683, 0.24915545655419, 0.26261770674500, 0.27635731727778,
315  0.29035942525136, 0.30460842402318, 0.31908800624032, 0.33378120935681,
316  0.34867046348260, 0.36373764140285, 0.37896411059909, 0.39433078709788,
317  0.40981819096657, 0.42540650327031, 0.44107562429959, 0.45680523287270,
318  0.47257484651351, 0.48836388230077, 0.50415171818214, 0.51991775454258,
319  0.53564147581496, 0.55130251191887, 0.56688069931047, 0.58235614142007,
320  0.59770926827271, 0.61292089506118, 0.62797227945823, 0.64284517745255,
321  0.65752189749349, 0.67198535273209, 0.68621911114984, 0.70020744337099,
322 };
323 
324 static const uint8_t dc_code_tab[5] = { 0, 0, 0, 1, 1 };
325 
326 static const uint8_t ht_code_tab[5] = { 0, 0, 1, 2, 2 };
327 
328 static const uint8_t band_ofs_tab[3][4] = {
329  { 12, 8, 4, 0 }, { 14, 10, 6, 0 }, { 12, 8, 4, 0 }
330 };
331 
332 static const uint8_t band_low_tab[3] = { 9, 17, 24 };
333 
334 static const uint16_t fast_gain_tab[8] = {
335  128, 256, 384, 512, 640, 768, 896, 1024
336 };
337 
338 static const uint16_t slow_decay_tab[2][2] = { { 27, -1 }, { 32, 21 } };
339 
340 static const uint16_t misc_decay_tab[3][2][2] = {
341  { { 354, -1 }, { 425, 425 } },
342  { { 266, -1 }, { 320, -1 } },
343  { { 213, -1 }, { 256, -1 } }
344 };
345 
346 static const uint16_t fast_decay_tab[3][2][2][50] = {
347  {{{
348  142, 142, 142, 142, 142, 142, 142, 142, 142, 142,
349  142, 142, 142, 142, 142, 142, 142, 142, 142, 142,
350  142, 142, 142, 142, 142, 142, 142, 142, 142, 142,
351  142, 142, 142, 142, 142, 142, 142, 142,
352  }, {
353  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
354  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
355  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
356  -1, -1, -1, -1, -1, -1, -1, -1,
357  }}, {{
358  170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
359  170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
360  170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
361  170, 170, 170, 170, 170, 170, 170, 170,
362  }, {
363  64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
364  64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
365  64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
366  64, 64, 64, 64, 64, 64, 64, 64,
367  }}}, {{{
368  266, 266, 106, 106, 106, 106, 106, 106, 106, 106,
369  106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
370  106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
371  106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
372  106, 106, 106, 106,
373  }, {
374  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
375  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
376  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
377  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
378  -1, -1, -1, -1,
379  }}, {{
380  319, 319, 128, 128, 128, 128, 128, 128, 128, 128,
381  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
382  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
383  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
384  128, 128, 128, 128,
385  }, {
386  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
387  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
388  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
389  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
390  -1, -1, -1, -1,
391  }}}, {{{
392  106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
393  106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
394  106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
395  106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
396  106, 106, 106, 106, 106, 106, 106, 106, 106, 106,
397  }, {
398  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
399  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
400  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
401  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
402  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
403  }}, {{
404  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
405  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
406  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
407  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
408  128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
409  }, {
410  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
411  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
412  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
413  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
414  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
415  }}}
416 };
417 
418 static const uint16_t fast_gain_adj_tab[3][2][62] = {
419  {{
420  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
421  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
422  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
423  0, 1, 2, 4, 7, 11, 16, 29, 44, 59,
424  76, 94, 116, 142, 179, 221, 252, 285, 312, 334,
425  }, {
426  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
427  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
428  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
429  2, 5, 8, 10, 15, 28, 42, 57, 75, 93,
430  115, 140, 177, 219, 247, 280, 308, 330, 427, 533,
431  }}, {{
432  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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  0, 2, 5, 8, 12, 21, 35, 51, 69, 89,
437  111, 138, 176, 220, 251, 284, 312, 334,
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, 2,
443  5, 8, 11, 18, 33, 49, 65, 84, 106, 132,
444  168, 214, 245, 279, 308, 329, 427, 533,
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, 0,
450  0, 0, 0, 0, 0, 1, 4, 7, 10, 17,
451  31, 47, 65, 84, 107, 134, 171, 215, 250, 283,
452  312, 334,
453  }, {
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, 0, 0, 0, 0, 0,
458  0, 0, 0, 0, 3, 6, 9, 13, 27, 43,
459  60, 79, 100, 126, 160, 207, 242, 276, 307, 329,
460  427, 533,
461  }}
462 };
463 
464 static const uint16_t slow_gain_tab[3][2][50] = {
465  {{
466  3072, 3072, 3072, 3072, 3072, 3072, 1063, 1063, 1063, 1063,
467  1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063,
468  1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063,
469  1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063,
470  }, {
471  3072, 3072, 3072, 3072, 3072, 3072, 850, 850, 850, 850,
472  850, 850, 850, 850, 850, 850, 850, 850, 850, 850,
473  850, 850, 850, 850, 850, 850, 850, 850, 850, 850,
474  850, 850, 850, 850, 850, 850, 850, 850,
475  }}, {{
476  3072, 1212, 1212, 1212, 999, 999, 999, 999, 999, 999,
477  999, 999, 999, 999, 999, 999, 999, 999, 999, 999,
478  999, 999, 999, 999, 999, 999, 999, 999, 999, 999,
479  999, 999, 999, 999, 999, 999, 999, 999, 999, 999,
480  999, 999, 999, 999,
481  }, {
482  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
483  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
484  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
485  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
486  -1, -1, -1, -1,
487  }}, {{
488  3072, 3072, 3072, 3072, 3072, 3072, 3072, 3072, 3072, 3072,
489  999, 999, 999, 999, 999, 999, 999, 999, 999, 999,
490  999, 999, 999, 999, 999, 999, 999, 999, 999, 999,
491  999, 999, 999, 999, 999, 999, 999, 999, 999, 999,
492  999, 999, 999, 999, 999, 999, 999, 999, 999, 999,
493  }, {
494  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
495  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
496  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
497  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
498  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
499  }}
500 };
501 
502 static const uint16_t hearing_thresh_tab[3][3][50] = {
503  {{
504  1403, 1141, 1000, 959, 948, 957, 946, 925, 899, 871,
505  843, 815, 789, 766, 745, 727, 705, 687, 681, 686,
506  701, 725, 768, 854, 940, 1018, 1075, 1103, 1111, 1106,
507  1098, 1105, 1142, 1237, 1419, 1721, 2169, 2805,
508  }, {
509  1401, 1130, 995, 957, 947, 955, 941, 918, 890, 861,
510  831, 803, 777, 754, 734, 717, 698, 684, 682, 692,
511  712, 743, 798, 894, 976, 1045, 1091, 1109, 1110, 1102,
512  1098, 1116, 1174, 1300, 1526, 1884, 2401, 3072,
513  }, {
514  1393, 1086, 974, 949, 957, 941, 913, 878, 843, 808,
515  777, 750, 727, 708, 695, 686, 681, 689, 714, 752,
516  811, 888, 971, 1044, 1087, 1108, 1110, 1102, 1098, 1115,
517  1172, 1290, 1489, 1812, 2293, 2964, 3072, 3072,
518  }}, {{
519  1412, 1343, 1141, 1047, 1000, 974, 959, 951, 948, 947,
520  957, 953, 946, 936, 925, 906, 878, 850, 822, 795,
521  771, 745, 719, 700, 687, 681, 685, 701, 733, 784,
522  885, 977, 1047, 1092, 1110, 1108, 1099, 1102, 1138, 1233,
523  1413, 1711, 2157, 2797,
524  }, {
525  1412, 1336, 1130, 1040, 995, 970, 957, 950, 947, 947,
526  955, 950, 941, 930, 918, 897, 868, 838, 810, 783,
527  759, 734, 710, 693, 684, 681, 690, 712, 752, 823,
528  924, 1009, 1069, 1102, 1111, 1104, 1098, 1111, 1168, 1295,
529  1518, 1873, 2388, 3072,
530  }, {
531  1411, 1293, 1086, 1009, 974, 957, 949, 947, 957, 951,
532  941, 928, 913, 896, 878, 852, 817, 785, 756, 732,
533  713, 695, 683, 682, 689, 710, 746, 811, 906, 992,
534  1061, 1099, 1111, 1106, 1098, 1107, 1155, 1266, 1471, 1799,
535  2277, 2945, 3072, 3072,
536  }}, {{
537  1431, 1412, 1403, 1379, 1343, 1293, 1229, 1180, 1125, 1075,
538  1040, 1014, 996, 979, 965, 957, 951, 948, 947, 957,
539  951, 940, 924, 903, 877, 846, 815, 785, 753, 725,
540  702, 686, 681, 689, 714, 760, 847, 947, 1028, 1083,
541  1108, 1109, 1101, 1100, 1132, 1222, 1402, 1705, 2160, 2803,
542  }, {
543  1431, 1412, 1401, 1375, 1336, 1278, 1215, 1168, 1115, 1066,
544  1032, 1008, 991, 975, 962, 954, 950, 947, 947, 955,
545  948, 935, 916, 894, 866, 835, 803, 772, 742, 715,
546  695, 683, 683, 697, 729, 784, 887, 982, 1054, 1096,
547  1111, 1106, 1098, 1107, 1159, 1281, 1505, 1865, 2391, 3072,
548  }, {
549  1427, 1411, 1393, 1353, 1293, 1215, 1160, 1118, 1072, 1031,
550  1003, 984, 971, 960, 952, 948, 947, 957, 952, 941,
551  924, 902, 876, 847, 815, 781, 750, 723, 700, 685,
552  681, 691, 719, 766, 858, 958, 1039, 1089, 1109, 1108,
553  1099, 1102, 1141, 1245, 1442, 1766, 2250, 2930, 3072, 3072,
554  }}
555 };
556 
557 static const int16_t lwc_gain_tab[11][7] = {
558  { -21, -197, -271, -466, 32767, 32767, 32767 },
559  { -197, -29, -244, -271, -540, 32767, 32767 },
560  { -271, -244, -29, -249, -271, -593, 32767 },
561  { -466, -271, -249, -29, -251, -271, -632 },
562  { -540, -271, -251, -29, -251, -271, -664 },
563  { -593, -271, -251, -29, -252, -271, -690 },
564  { -632, -271, -252, -29, -252, -271, -711 },
565  { -664, -271, -252, -29, -252, -271, -730 },
566  { -690, -271, -252, -29, -252, -271, -745 },
567  { -711, -271, -252, -29, -253, -271, -759 },
568  { -730, -271, -253, -29, -253, -271, -771 },
569 };
570 
571 static const int16_t lwc_adj_tab[7] = {
572  -192, -320, -448, -512, -448, -320, -192,
573 };
574 
575 static const uint8_t log_add_tab[212] = {
576  64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 52, 51, 50,
577  49, 48, 47, 47, 46, 45, 44, 44, 43, 42, 41, 41, 40, 39, 38, 38,
578  37, 36, 36, 35, 35, 34, 33, 33, 32, 32, 31, 30, 30, 29, 29, 28,
579  28, 27, 27, 26, 26, 25, 25, 24, 24, 23, 23, 22, 22, 21, 21, 21,
580  20, 20, 19, 19, 19, 18, 18, 18, 17, 17, 17, 16, 16, 16, 15, 15,
581  15, 14, 14, 14, 13, 13, 13, 13, 12, 12, 12, 12, 11, 11, 11, 11,
582  10, 10, 10, 10, 10, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 8,
583  7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5,
584  5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
585  4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2,
586  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
587  2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
588  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
589  1, 1, 0, 0,
590 };
591 
592 static const uint8_t bap_tab[64] = {
593  0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4,
594  4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8,
595  8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12,
596  12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 15,
597 };
598 
599 static float mantissa_tab1[17][4];
600 static float mantissa_tab2[17][4];
601 static float mantissa_tab3[17][4];
602 static float exponent_tab[50];
603 static float gain_tab[1024];
604 
605 DECLARE_ALIGNED(32, static float, window)[3712];
606 
607 static int skip_input(DBEContext *s, int nb_words)
608 {
609  if (nb_words > s->input_size) {
610  av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n");
611  return AVERROR_INVALIDDATA;
612  }
613 
614  s->input += nb_words * s->word_bytes;
615  s->input_size -= nb_words;
616  return 0;
617 }
618 
619 static int parse_key(DBEContext *s)
620 {
621  if (s->key_present) {
622  const uint8_t *key = s->input;
623  int ret = skip_input(s, 1);
624  if (ret < 0)
625  return ret;
626  return AV_RB24(key) >> 24 - s->word_bits;
627  }
628  return 0;
629 }
630 
631 static int convert_input(DBEContext *s, int nb_words, int key)
632 {
633  const uint8_t *src = s->input;
634  uint8_t *dst = s->buffer;
635  PutBitContext pb;
636  int i;
637 
638  av_assert0(nb_words <= 1024u);
639 
640  if (nb_words > s->input_size) {
641  av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n");
642  return AVERROR_INVALIDDATA;
643  }
644 
645  switch (s->word_bits) {
646  case 16:
647  for (i = 0; i < nb_words; i++, src += 2, dst += 2)
648  AV_WB16(dst, AV_RB16(src) ^ key);
649  break;
650  case 20:
651  init_put_bits(&pb, s->buffer, sizeof(s->buffer));
652  for (i = 0; i < nb_words; i++, src += 3)
653  put_bits(&pb, 20, AV_RB24(src) >> 4 ^ key);
654  flush_put_bits(&pb);
655  break;
656  case 24:
657  for (i = 0; i < nb_words; i++, src += 3, dst += 3)
658  AV_WB24(dst, AV_RB24(src) ^ key);
659  break;
660  default:
661  av_assert0(0);
662  }
663 
664  return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits);
665 }
666 
668 {
669  DBEContext *s = &s1->dectx;
670  if (s1->metadata.mtd_ext_size)
671  return skip_input(s, s->key_present + s1->metadata.mtd_ext_size + 1);
672  return 0;
673 }
674 
676 {
677  int mstr_exp[MAX_MSTR_EXP];
678  int bias_exp[MAX_BIAS_EXP];
679  int i, j, k;
680 
681  for (i = 0; i < c->nb_mstr_exp; i++)
682  mstr_exp[i] = get_bits(&s->gb, 2) * 6;
683 
684  for (i = 0; i < g->nb_exponent; i++)
685  bias_exp[i] = get_bits(&s->gb, 5);
686 
687  for (i = k = 0; i < c->nb_mstr_exp; i++)
688  for (j = 0; j < g->nb_bias_exp[i]; j++, k++)
689  c->exponents[g->exp_ofs + k] = mstr_exp[i] + bias_exp[k];
690 }
691 
693 {
694  DBEGroup *p, *g;
695  int i;
696 
697  for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
698  c->exp_strategy[i] = !i || g->nb_exponent != p->nb_exponent || get_bits1(&s->gb);
699  if (c->exp_strategy[i]) {
700  unbias_exponents(s, c, g);
701  } else {
702  memcpy(c->exponents + g->exp_ofs,
703  c->exponents + p->exp_ofs,
704  g->nb_exponent * sizeof(c->exponents[0]));
705  }
706  }
707 
708  return 0;
709 }
710 
711 static inline int log_add(int a, int b)
712 {
713  int c = FFABS(a - b) >> 1;
714  return FFMAX(a, b) + log_add_tab[FFMIN(c, 211)];
715 }
716 
717 static void calc_lowcomp(int *msk_val)
718 {
719  int lwc_val[17] = { 0 };
720  int i, j, k;
721 
722  for (i = 0; i < 11; i++) {
723  int max_j = 0;
724  int max_v = INT_MIN;
725  int thr = 0;
726 
727  for (j = FFMAX(i - 3, 0), k = 0; j <= i + 3; j++, k++) {
728  int v = msk_val[j] + lwc_gain_tab[i][k];
729  if (v > max_v) {
730  max_j = j;
731  max_v = v;
732  }
733  thr = log_add(thr, v);
734  }
735 
736  if (msk_val[i] < thr) {
737  for (j = FFMAX(max_j - 3, 0),
738  k = FFMAX(3 - max_j, 0);
739  j <= max_j + 3; j++, k++)
740  lwc_val[j] += lwc_adj_tab[k];
741  }
742  }
743 
744  for (i = 0; i < 16; i++) {
745  int v = FFMAX(lwc_val[i], -512);
746  msk_val[i] = FFMAX(msk_val[i] + v, 0);
747  }
748 }
749 
750 static void bit_allocate(int nb_exponent, int nb_code, int fr_code,
751  int *exp, int *bap,
752  int fg_spc, int fg_ofs, int msk_mod, int snr_ofs)
753 {
754  int msk_val[MAX_BIAS_EXP];
755  int psd_val[MAX_BIAS_EXP];
756  int fast_leak = 0;
757  int slow_leak = 0;
758  int dc_code = dc_code_tab[fr_code - 1];
759  int ht_code = ht_code_tab[fr_code - 1];
760  int fast_gain = fast_gain_tab[fg_ofs];
761  int slow_decay = slow_decay_tab[dc_code][msk_mod];
762  int misc_decay = misc_decay_tab[nb_code][dc_code][msk_mod];
763  const uint16_t *slow_gain = slow_gain_tab[nb_code][msk_mod];
764  const uint16_t *fast_decay = fast_decay_tab[nb_code][dc_code][msk_mod];
765  const uint16_t *fast_gain_adj = fast_gain_adj_tab[nb_code][dc_code];
766  const uint16_t *hearing_thresh = hearing_thresh_tab[nb_code][ht_code];
767  int i;
768 
769  for (i = 0; i < nb_exponent; i++)
770  psd_val[i] = (48 - exp[i]) * 64;
771 
772  fast_gain_adj += band_ofs_tab[nb_code][fg_spc];
773  for (i = 0; i < nb_exponent; i++) {
774  fast_leak = log_add(fast_leak - fast_decay[i],
775  psd_val[i] - fast_gain + fast_gain_adj[i]);
776  slow_leak = log_add(slow_leak - slow_decay,
777  psd_val[i] - slow_gain[i]);
778  msk_val[i] = FFMAX(fast_leak, slow_leak);
779  }
780 
781  fast_leak = 0;
782  for (i = nb_exponent - 1; i > band_low_tab[nb_code]; i--) {
783  fast_leak = log_add(fast_leak - misc_decay, psd_val[i] - fast_gain);
784  msk_val[i] = FFMAX(msk_val[i], fast_leak);
785  }
786 
787  for (i = 0; i < nb_exponent; i++)
788  msk_val[i] = FFMAX(msk_val[i], hearing_thresh[i]);
789 
790  if (!nb_code)
791  calc_lowcomp(msk_val);
792 
793  for (i = 0; i < nb_exponent; i++) {
794  int v = 16 * (snr_ofs - 64) + psd_val[i] - msk_val[i] >> 5;
795  bap[i] = bap_tab[av_clip_uintp2(v, 6)];
796  }
797 }
798 
800 {
801  DBEContext *s = &s1->dectx;
802  DBEGroup *p, *g;
803  int bap_strategy[MAX_GROUPS], fg_spc[MAX_GROUPS];
804  int fg_ofs[MAX_GROUPS], msk_mod[MAX_GROUPS];
805  int i, snr_ofs;
806 
807  for (i = 0; i < c->nb_groups; i++) {
808  bap_strategy[i] = !i || get_bits1(&s->gb);
809  if (bap_strategy[i]) {
810  fg_spc[i] = get_bits(&s->gb, 2);
811  fg_ofs[i] = get_bits(&s->gb, 3);
812  msk_mod[i] = get_bits1(&s->gb);
813  } else {
814  fg_spc[i] = fg_spc[i - 1];
815  fg_ofs[i] = fg_ofs[i - 1];
816  msk_mod[i] = msk_mod[i - 1];
817  }
818  }
819 
820  if (get_bits1(&s->gb)) {
821  avpriv_report_missing_feature(s->avctx, "Delta bit allocation");
822  return AVERROR_PATCHWELCOME;
823  }
824 
825  snr_ofs = get_bits(&s->gb, 8);
826  if (!snr_ofs) {
827  memset(c->bap, 0, sizeof(c->bap));
828  return 0;
829  }
830 
831  for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
832  if (c->exp_strategy[i] || bap_strategy[i]) {
834  c->exponents + g->exp_ofs, c->bap + g->exp_ofs,
835  fg_spc[i], fg_ofs[i], msk_mod[i], snr_ofs);
836  } else {
837  memcpy(c->bap + g->exp_ofs,
838  c->bap + p->exp_ofs,
839  g->nb_exponent * sizeof(c->bap[0]));
840  }
841  }
842 
843  return 0;
844 }
845 
847 {
848  DBEGroup *p, *g;
849  int i, j;
850 
851  for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
852  if (get_bits1(&s->gb)) {
853  int start = get_bits(&s->gb, 6);
854 
855  if (start > g->nb_exponent) {
856  av_log(s->avctx, AV_LOG_ERROR, "Invalid start index\n");
857  return AVERROR_INVALIDDATA;
858  }
859 
860  for (j = 0; j < start; j++)
861  c->idx[g->exp_ofs + j] = 0;
862 
863  for (; j < g->nb_exponent; j++)
864  c->idx[g->exp_ofs + j] = get_bits(&s->gb, 2);
865  } else if (i && g->nb_exponent == p->nb_exponent) {
866  memcpy(c->idx + g->exp_ofs,
867  c->idx + p->exp_ofs,
868  g->nb_exponent * sizeof(c->idx[0]));
869  } else {
870  memset(c->idx + g->exp_ofs, 0, g->nb_exponent * sizeof(c->idx[0]));
871  }
872  }
873 
874  return 0;
875 }
876 
878 {
879  DBEGroup *g;
880  int i, j, k;
881 
882  for (i = 0, g = c->groups; i < c->nb_groups; i++, g++) {
883  float *mnt = c->mantissas + g->mnt_ofs;
884 
885  for (j = 0; j < g->nb_exponent; j++) {
886  int bap = c->bap[g->exp_ofs + j];
887  int idx = c->idx[g->exp_ofs + j];
888  int size1 = mantissa_size1[bap][idx];
889  int count = g->nb_mantissa[j];
890  float exp = exponent_tab[c->exponents[g->exp_ofs + j]];
891  float scale = mantissa_tab1[size1][idx] * exp;
892 
893  if (!size1) {
894  memset(mnt, 0, count * sizeof(*mnt));
895  } else if (idx) {
896  int values[100];
897  int escape = -(1 << size1 - 1);
898 
899  for (k = 0; k < count; k++)
900  values[k] = get_sbits(&s->gb, size1);
901 
902  for (k = 0; k < count; k++) {
903  if (values[k] != escape) {
904  mnt[k] = values[k] * scale;
905  } else {
906  int size2 = mantissa_size2[bap][idx];
907  int value = get_sbits(&s->gb, size2);
908  float a = mantissa_tab2[size2][idx];
909  float b = mantissa_tab3[size2][idx];
910  if (value < 0)
911  mnt[k] = ((value + 1) * a - b) * exp;
912  else
913  mnt[k] = (value * a + b) * exp;
914  }
915  }
916  } else {
917  for (k = 0; k < count; k++)
918  mnt[k] = get_sbits(&s->gb, size1) * scale;
919  }
920 
921  mnt += count;
922  }
923 
924  for (; j < g->nb_exponent + c->bw_code; j++) {
925  memset(mnt, 0, g->nb_mantissa[j] * sizeof(*mnt));
926  mnt += g->nb_mantissa[j];
927  }
928  }
929 
930  return 0;
931 }
932 
933 static int parse_channel(DBEDecodeContext *s1, int ch, int seg_id)
934 {
935  DBEContext *s = &s1->dectx;
936  DBEChannel *c = &s1->channels[seg_id][ch];
937  int i, ret;
938 
939  if (s1->metadata.rev_id[ch] > 1) {
940  avpriv_report_missing_feature(s->avctx, "Encoder revision %d", s1->metadata.rev_id[ch]);
941  return AVERROR_PATCHWELCOME;
942  }
943 
944  if (ch == lfe_channel_tab[s1->metadata.prog_conf]) {
945  c->gr_code = 3;
946  c->bw_code = 29;
947  } else {
948  c->gr_code = get_bits(&s->gb, 2);
949  c->bw_code = get_bits(&s->gb, 3);
950  if (c->gr_code == 3) {
951  av_log(s->avctx, AV_LOG_ERROR, "Invalid group type code\n");
952  return AVERROR_INVALIDDATA;
953  }
954  }
955 
958 
959  for (i = 0; i < c->nb_groups; i++) {
960  c->groups[i] = frm_ofs_tab[seg_id][c->gr_code][i];
961  if (c->nb_mstr_exp == 2) {
962  c->groups[i].nb_exponent -= c->bw_code;
963  c->groups[i].nb_bias_exp[1] -= c->bw_code;
964  }
965  }
966 
967  if ((ret = parse_exponents(s, c)) < 0)
968  return ret;
969  if ((ret = parse_bit_alloc(s1, c)) < 0)
970  return ret;
971  if ((ret = parse_indices(s, c)) < 0)
972  return ret;
973  if ((ret = parse_mantissas(s, c)) < 0)
974  return ret;
975 
976  if (get_bits_left(&s->gb) < 0) {
977  av_log(s->avctx, AV_LOG_ERROR, "Read past end of channel %d\n", ch);
978  return AVERROR_INVALIDDATA;
979  }
980 
981  return 0;
982 }
983 
984 static int parse_audio(DBEDecodeContext *s1, int start, int end, int seg_id)
985 {
986  DBEContext *s = &s1->dectx;
987  int ch, ret, key;
988 
989  if ((key = parse_key(s)) < 0)
990  return key;
991 
992  for (ch = start; ch < end; ch++) {
993  if (!s1->metadata.ch_size[ch]) {
994  s1->channels[seg_id][ch].nb_groups = 0;
995  continue;
996  }
997  if ((ret = convert_input(s, s1->metadata.ch_size[ch], key)) < 0)
998  return ret;
999  if ((ret = parse_channel(s1, ch, seg_id)) < 0) {
1000  if (s1->avctx->err_recognition & AV_EF_EXPLODE)
1001  return ret;
1002  s1->channels[seg_id][ch].nb_groups = 0;
1003  }
1004  if ((ret = skip_input(s, s1->metadata.ch_size[ch])) < 0)
1005  return ret;
1006  }
1007 
1008  return skip_input(s, 1);
1009 }
1010 
1012 {
1013  DBEContext *s = &s1->dectx;
1014  if (s1->metadata.meter_size)
1015  return skip_input(s, s->key_present + s1->metadata.meter_size + 1);
1016  return 0;
1017 }
1018 
1019 static void imdct_calc(DBEDecodeContext *s1, DBEGroup *g, float *result, float *values)
1020 {
1021  FFTContext *imdct = &s1->imdct[g->imdct_idx];
1022  int n = 1 << imdct_bits_tab[g->imdct_idx];
1023  int n2 = n >> 1;
1024  int i;
1025 
1026  switch (g->imdct_phs) {
1027  case 0:
1028  imdct->imdct_half(imdct, result, values);
1029  for (i = 0; i < n2; i++)
1030  result[n2 + i] = result[n2 - i - 1];
1031  break;
1032  case 1:
1033  imdct->imdct_calc(imdct, result, values);
1034  break;
1035  case 2:
1036  imdct->imdct_half(imdct, result + n2, values);
1037  for (i = 0; i < n2; i++)
1038  result[i] = -result[n - i - 1];
1039  break;
1040  default:
1041  av_assert0(0);
1042  }
1043 }
1044 
1045 static void transform(DBEDecodeContext *s1, DBEChannel *c, float *history, float *output)
1046 {
1047  LOCAL_ALIGNED_32(float, buffer, [2048]);
1048  LOCAL_ALIGNED_32(float, result, [1152]);
1049  DBEGroup *g;
1050  int i;
1051 
1052  memset(result, 0, 1152 * sizeof(float));
1053  for (i = 0, g = c->groups; i < c->nb_groups; i++, g++) {
1054  float *src = buffer + g->src_ofs;
1055  float *dst = result + g->dst_ofs;
1056  float *win = window + g->win_ofs;
1057 
1058  imdct_calc(s1, g, buffer, c->mantissas + g->mnt_ofs);
1059  s1->fdsp->vector_fmul_add(dst, src, win, dst, g->win_len);
1060  }
1061 
1062  for (i = 0; i < 256; i++)
1063  output[i] = history[i] + result[i];
1064  for (i = 256; i < 896; i++)
1065  output[i] = result[i];
1066  for (i = 0; i < 256; i++)
1067  history[i] = result[896 + i];
1068 }
1069 
1070 static void apply_gain(DBEDecodeContext *s, int begin, int end, float *output)
1071 {
1072  if (begin == 960 && end == 960)
1073  return;
1074 
1075  if (begin == end) {
1076  s->fdsp->vector_fmul_scalar(output, output, gain_tab[end], FRAME_SAMPLES);
1077  } else {
1078  float a = gain_tab[begin] * (1.0f / (FRAME_SAMPLES - 1));
1079  float b = gain_tab[end ] * (1.0f / (FRAME_SAMPLES - 1));
1080  int i;
1081 
1082  for (i = 0; i < FRAME_SAMPLES; i++)
1083  output[i] *= a * (FRAME_SAMPLES - i - 1) + b * i;
1084  }
1085 }
1086 
1088 {
1089  const uint8_t *reorder;
1090  int ch, ret;
1091 
1092  if (s->metadata.nb_channels == 4)
1093  reorder = ch_reorder_4;
1094  else if (s->metadata.nb_channels == 6)
1095  reorder = ch_reorder_6;
1097  reorder = ch_reorder_8;
1098  else
1099  reorder = ch_reorder_n;
1100 
1101  frame->nb_samples = FRAME_SAMPLES;
1102  if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0)
1103  return ret;
1104 
1105  for (ch = 0; ch < s->metadata.nb_channels; ch++) {
1106  float *output = (float *)frame->extended_data[reorder[ch]];
1107  transform(s, &s->channels[0][ch], s->history[ch], output);
1108  transform(s, &s->channels[1][ch], s->history[ch], output + FRAME_SAMPLES / 2);
1109  apply_gain(s, s->metadata.begin_gain[ch], s->metadata.end_gain[ch], output);
1110  }
1111 
1112  return 0;
1113 }
1114 
1115 static int dolby_e_decode_frame(AVCodecContext *avctx, void *data,
1116  int *got_frame_ptr, AVPacket *avpkt)
1117 {
1118  DBEDecodeContext *s1 = avctx->priv_data;
1119  DBEContext *s = &s1->dectx;
1120  int i, j, ret;
1121 
1122  if ((ret = ff_dolby_e_parse_init(s, avpkt->data, avpkt->size)) < 0)
1123  return ret;
1124 
1125  if ((ret = ff_dolby_e_parse_header(s, &s1->metadata)) < 0)
1126  return ret;
1127 
1128  if (s1->metadata.nb_programs > 1 && !s1->metadata.multi_prog_warned) {
1129  av_log(avctx, AV_LOG_WARNING, "Stream has %d programs (configuration %d), "
1130  "channels will be output in native order.\n", s1->metadata.nb_programs, s1->metadata.prog_conf);
1131  s1->metadata.multi_prog_warned = 1;
1132  }
1133 
1134  switch (s1->metadata.nb_channels) {
1135  case 4:
1137  break;
1138  case 6:
1140  break;
1141  case 8:
1143  break;
1144  }
1145 
1146  avctx->channels = s1->metadata.nb_channels;
1148  avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1149 
1150  i = s1->metadata.nb_channels / 2;
1151  j = s1->metadata.nb_channels;
1152  if ((ret = parse_audio(s1, 0, i, 0)) < 0)
1153  return ret;
1154  if ((ret = parse_audio(s1, i, j, 0)) < 0)
1155  return ret;
1156  if ((ret = parse_metadata_ext(s1)) < 0)
1157  return ret;
1158  if ((ret = parse_audio(s1, 0, i, 1)) < 0)
1159  return ret;
1160  if ((ret = parse_audio(s1, i, j, 1)) < 0)
1161  return ret;
1162  if ((ret = parse_meter(s1)) < 0)
1163  return ret;
1164  if ((ret = filter_frame(s1, data)) < 0)
1165  return ret;
1166 
1167  *got_frame_ptr = 1;
1168  return avpkt->size;
1169 }
1170 
1172 {
1173  DBEDecodeContext *s = avctx->priv_data;
1174 
1175  memset(s->history, 0, sizeof(s->history));
1176 }
1177 
1179 {
1180  DBEDecodeContext *s = avctx->priv_data;
1181  int i;
1182 
1183  for (i = 0; i < 3; i++)
1184  ff_mdct_end(&s->imdct[i]);
1185 
1186  av_freep(&s->fdsp);
1187  return 0;
1188 }
1189 
1190 
1191 static av_cold void init_tables(void)
1192 {
1193  int i, j;
1194 
1195  for (i = 1; i < 17; i++)
1196  mantissa_tab1[i][0] = 1.0f / (1 << i - 1);
1197 
1198  for (i = 2; i < 16; i++) {
1199  mantissa_tab1[i][1] = 1.0f / ((1 << i) - 1);
1200  mantissa_tab1[i][2] = 0.5f / ((1 << i) - 1);
1201  mantissa_tab1[i][3] = 0.25f / ((1 << i) - 1);
1202  }
1203 
1204  mantissa_tab1[i][1] = 0.5f / (1 << 15);
1205  mantissa_tab1[i][2] = 0.75f / (1 << 15);
1206  mantissa_tab1[i][3] = 0.875f / (1 << 15);
1207 
1208  for (i = 1; i < 17; i++) {
1209  mantissa_tab2[i][1] = mantissa_tab1[i][0] * 0.5f;
1210  mantissa_tab2[i][2] = mantissa_tab1[i][0] * 0.75f;
1211  mantissa_tab2[i][3] = mantissa_tab1[i][0] * 0.875f;
1212  for (j = 1; j < 4; j++)
1213  mantissa_tab3[i][j] = 1.0f / (1 << i) + 1.0f / (1 << j) - 1.0f / (1 << i + j);
1214  }
1215 
1216  mantissa_tab3[1][3] = 0.6875f;
1217 
1218  for (i = 0; i < 25; i++) {
1219  exponent_tab[i * 2 ] = 1.0f / (1 << i);
1220  exponent_tab[i * 2 + 1] = M_SQRT1_2 / (1 << i);
1221  }
1222 
1223  for (i = 1; i < 1024; i++)
1224  gain_tab[i] = exp2f((i - 960) / 64.0f);
1225 
1226  // short 1
1227  ff_kbd_window_init(window, 3.0f, 128);
1228  for (i = 0; i < 128; i++)
1229  window[128 + i] = window[127 - i];
1230 
1231  // start
1232  for (i = 0; i < 192; i++)
1233  window[256 + i] = start_window[i];
1234 
1235  // short 2
1236  for (i = 0; i < 192; i++)
1237  window[448 + i] = short_window2[i];
1238  for (i = 0; i < 64; i++)
1239  window[640 + i] = window[63 - i];
1240 
1241  // short 3
1242  for (i = 0; i < 64; i++)
1243  window[704 + i] = short_window3[i];
1244  for (i = 0; i < 192; i++)
1245  window[768 + i] = window[64 + i];
1246 
1247  // bridge
1248  for (i = 0; i < 128; i++)
1249  window[960 + i] = window[i];
1250  for (i = 0; i < 64; i++)
1251  window[1088 + i] = 1.0f;
1252 
1253  // long
1254  ff_kbd_window_init(window + 1408, 3.0f, 256);
1255  for (i = 0; i < 640; i++)
1256  window[1664 + i] = 1.0f;
1257  for (i = 0; i < 256; i++)
1258  window[2304 + i] = window[1152 + i] = window[1663 - i];
1259 
1260  // reverse start
1261  for (i = 0; i < 192; i++)
1262  window[2560 + i] = window[447 - i];
1263 
1264  // reverse short 2
1265  for (i = 0; i < 256; i++)
1266  window[2752 + i] = window[703 - i];
1267 
1268  // reverse short 3
1269  for (i = 0; i < 256; i++)
1270  window[3008 + i] = window[959 - i];
1271 
1272  // reverse bridge
1273  for (i = 0; i < 448; i++)
1274  window[3264 + i] = window[1407 - i];
1275 }
1276 
1278 {
1279  static AVOnce init_once = AV_ONCE_INIT;
1280  DBEDecodeContext *s = avctx->priv_data;
1281  int i;
1282 
1283  if (ff_thread_once(&init_once, init_tables))
1284  return AVERROR_UNKNOWN;
1285 
1286  for (i = 0; i < 3; i++)
1287  if (ff_mdct_init(&s->imdct[i], imdct_bits_tab[i], 1, 2.0) < 0)
1288  return AVERROR(ENOMEM);
1289 
1290  if (!(s->fdsp = avpriv_float_dsp_alloc(0)))
1291  return AVERROR(ENOMEM);
1292 
1294  s->dectx.avctx = s->avctx = avctx;
1295  return 0;
1296 }
1297 
1299  .name = "dolby_e",
1300  .long_name = NULL_IF_CONFIG_SMALL("Dolby E"),
1301  .type = AVMEDIA_TYPE_AUDIO,
1302  .id = AV_CODEC_ID_DOLBY_E,
1303  .priv_data_size = sizeof(DBEDecodeContext),
1304  .init = dolby_e_init,
1306  .close = dolby_e_close,
1307  .flush = dolby_e_flush,
1308  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
1311 };
static const uint8_t nb_groups_tab[4]
Definition: dolby_e.c:97
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
uint16_t src_ofs
Definition: dolby_e.c:53
float, planar
Definition: samplefmt.h:69
#define NULL
Definition: coverity.c:32
#define AV_CH_LAYOUT_7POINT1
int exp_strategy[MAX_GROUPS]
Definition: dolby_e.c:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static const DBEGroup grp_tab_1[8]
Definition: dolby_e.c:126
static float mantissa_tab3[17][4]
Definition: dolby_e.c:601
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition: dolby_e.c:1087
#define MAX_GROUPS
Definition: dolby_e.c:35
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
static void apply_gain(DBEDecodeContext *s, int begin, int end, float *output)
Definition: dolby_e.c:1070
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static void flush(AVCodecContext *avctx)
Dolby E reading context used by decoder and parser.
Definition: dolby_e.h:37
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:218
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
av_cold void ff_kbd_window_init(float *window, float alpha, int n)
Generate a Kaiser-Bessel Derived Window.
Definition: kbdwin.c:26
int end_gain[MAX_CHANNELS]
Definition: dolby_e.h:72
uint8_t nb_bias_exp[MAX_MSTR_EXP]
Definition: dolby_e.c:44
FFTContext imdct[3]
Definition: dolby_e.c:82
Memory handling functions.
uint16_t win_len
Definition: dolby_e.c:50
static float win(SuperEqualizerContext *s, float n, int N)
const char * g
Definition: vf_curves.c:115
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
DBEGroup groups[MAX_GROUPS]
Definition: dolby_e.c:62
static const uint8_t mantissa_size1[16][4]
Definition: dolby_e.c:185
#define M_SQRT1_2
Definition: mathematics.h:58
int size
Definition: packet.h:364
uint16_t win_ofs
Definition: dolby_e.c:52
static const int8_t lfe_channel_tab[MAX_PROG_CONF+1]
Definition: dolby_e.c:86
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:36
static const uint8_t ht_code_tab[5]
Definition: dolby_e.c:326
static const DBEGroup grp_tab_6[7]
Definition: dolby_e.c:166
int nb_mstr_exp
Definition: dolby_e.c:61
#define AV_CH_LAYOUT_4POINT0
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:102
const char * key
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:91
static const uint8_t ch_reorder_4[4]
Definition: dolby_e.c:91
static const DBEGroup grp_tab_5[8]
Definition: dolby_e.c:155
static const uint8_t mantissa_size2[16][4]
Definition: dolby_e.c:192
AVCodec.
Definition: codec.h:190
#define MAX_MANTISSAS
Definition: dolby_e.c:37
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:359
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static const uint16_t fast_decay_tab[3][2][2][50]
Definition: dolby_e.c:346
uint8_t imdct_phs
Definition: dolby_e.c:49
static const uint8_t band_ofs_tab[3][4]
Definition: dolby_e.c:328
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static int parse_metadata_ext(DBEDecodeContext *s1)
Definition: dolby_e.c:667
AVCodecContext * avctx
Definition: dolby_e.c:73
static int parse_channel(DBEDecodeContext *s1, int ch, int seg_id)
Definition: dolby_e.c:933
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1199
uint8_t
#define av_cold
Definition: attributes.h:88
uint8_t buffer[1024 *3+AV_INPUT_BUFFER_PADDING_SIZE]
Definition: dolby_e.h:48
static int dolby_e_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: dolby_e.c:1115
int idx[MAX_EXPONENTS]
Definition: dolby_e.c:67
static const int16_t lwc_gain_tab[11][7]
Definition: dolby_e.c:557
static const OptionGroupDef groups[]
Definition: ffmpeg_opt.c:3292
#define f(width, name)
Definition: cbs_vp9.c:255
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
GetBitContext gb
Definition: dolby_e.h:39
#define LOCAL_ALIGNED_32(t, v,...)
Definition: mem_internal.h:136
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
static const uint16_t fast_gain_tab[8]
Definition: dolby_e.c:334
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:264
static const uint8_t ch_reorder_n[8]
Definition: dolby_e.c:94
DBEContext dectx
Definition: dolby_e.c:74
const uint8_t * input
Definition: dolby_e.h:41
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:117
uint8_t * data
Definition: packet.h:363
static const float short_window3[64]
Definition: dolby_e.c:305
uint16_t mnt_ofs
Definition: dolby_e.c:46
static const uint16_t hearing_thresh_tab[3][3][50]
Definition: dolby_e.c:502
bitstream reader API header.
static const uint8_t dc_code_tab[5]
Definition: dolby_e.c:324
static const uint8_t nb_mantissa_50[50]
Definition: dolby_e.c:113
channels
Definition: aptx.h:33
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
#define AVOnce
Definition: thread.h:172
#define MAX_BIAS_EXP
Definition: dolby_e.c:40
#define av_log(a,...)
int key_present
Definition: dolby_e.h:46
#define AV_CH_LAYOUT_5POINT1
static const uint8_t imdct_bits_tab[3]
Definition: dolby_e.c:120
#define src
Definition: vp8dsp.c:255
static int parse_key(DBEContext *s)
Definition: dolby_e.c:619
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static int parse_bit_alloc(DBEDecodeContext *s1, DBEChannel *c)
Definition: dolby_e.c:799
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
static float mantissa_tab2[17][4]
Definition: dolby_e.c:600
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
int gr_code
Definition: dolby_e.c:57
int multi_prog_warned
Definition: dolby_e.h:78
#define MAX_SEGMENTS
Definition: dolby_e.c:33
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:115
static const uint8_t ch_reorder_8[8]
Definition: dolby_e.c:93
static const int16_t lwc_adj_tab[7]
Definition: dolby_e.c:571
static const DBEGroup grp_tab_2[7]
Definition: dolby_e.c:137
int nb_groups
Definition: dolby_e.c:60
static const uint8_t bap_tab[64]
Definition: dolby_e.c:592
#define MAX_PROG_CONF
Definition: dolby_e.h:29
DBEChannel channels[MAX_SEGMENTS][MAX_CHANNELS]
Definition: dolby_e.c:78
static const DBEGroup grp_tab_4[1]
Definition: dolby_e.c:151
const char * name
Name of the codec implementation.
Definition: codec.h:197
void(* imdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:102
#define ff_mdct_init
Definition: fft.h:161
GLsizei count
Definition: opengl_enc.c:108
#define FFMAX(a, b)
Definition: common.h:102
AVCodec ff_dolby_e_decoder
Definition: dolby_e.c:1298
int8_t exp
Definition: eval.c:72
static const uint16_t slow_gain_tab[3][2][50]
Definition: dolby_e.c:464
static int parse_indices(DBEContext *s, DBEChannel *c)
Definition: dolby_e.c:846
static av_cold int dolby_e_init(AVCodecContext *avctx)
Definition: dolby_e.c:1277
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1242
#define FRAME_SAMPLES
Definition: dolby_e.h:27
static const uint8_t nb_mstr_exp_tab[4]
Definition: dolby_e.c:99
int ff_dolby_e_parse_init(DBEContext *s, const uint8_t *buf, int buf_size)
Initialize DBEContext.
Definition: dolby_e_parse.c:91
uint8_t imdct_idx
Definition: dolby_e.c:48
static const uint16_t sample_rate_tab[16]
Definition: dolby_e.h:82
#define b
Definition: input.c:41
static const uint16_t slow_decay_tab[2][2]
Definition: dolby_e.c:338
Definition: fft.h:83
int bap[MAX_EXPONENTS]
Definition: dolby_e.c:66
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1640
#define FFMIN(a, b)
Definition: common.h:104
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return values
#define AV_WB24(p, d)
Definition: intreadwrite.h:450
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
int ff_dolby_e_parse_header(DBEContext *s, DolbyEHeaderInfo *hdr)
Parse Dolby E metadata.
int word_bytes
Definition: dolby_e.h:45
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:1651
static const uint16_t fast_gain_adj_tab[3][2][62]
Definition: dolby_e.c:418
int mtd_ext_size
Definition: dolby_e.h:67
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int input_size
Definition: dolby_e.h:42
float mantissas[MAX_MANTISSAS]
Definition: dolby_e.c:69
DolbyEHeaderInfo metadata
Definition: dolby_e.c:76
static const uint8_t nb_mantissa_44[44]
Definition: dolby_e.c:107
#define exp2f(x)
Definition: libm.h:293
static void transform(DBEDecodeContext *s1, DBEChannel *c, float *history, float *output)
Definition: dolby_e.c:1045
static const uint16_t misc_decay_tab[3][2][2]
Definition: dolby_e.c:340
#define AV_ONCE_INIT
Definition: thread.h:173
static void imdct_calc(DBEDecodeContext *s1, DBEGroup *g, float *result, float *values)
Definition: dolby_e.c:1019
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:91
int sample_rate
samples per second
Definition: avcodec.h:1191
#define AV_CH_LAYOUT_NATIVE
Channel mask value used for AVCodecContext.request_channel_layout to indicate that the user requests ...
static const uint8_t nb_mantissa_38[38]
Definition: dolby_e.c:101
main external API structure.
Definition: avcodec.h:531
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1884
static void calc_lowcomp(int *msk_val)
Definition: dolby_e.c:717
static const DBEGroup grp_tab_3[1]
Definition: dolby_e.c:147
static const float short_window2[192]
Definition: dolby_e.c:252
static float exponent_tab[50]
Definition: dolby_e.c:602
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static float gain_tab[1024]
Definition: dolby_e.c:603
static int parse_mantissas(DBEContext *s, DBEChannel *c)
Definition: dolby_e.c:877
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
AVFloatDSPContext * fdsp
Definition: dolby_e.c:83
static int parse_audio(DBEDecodeContext *s1, int start, int end, int seg_id)
Definition: dolby_e.c:984
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition: float_dsp.h:85
int rev_id[MAX_CHANNELS]
Definition: dolby_e.h:70
#define MAX_CHANNELS
Definition: aac.h:48
uint16_t dst_ofs
Definition: dolby_e.c:51
#define s1
Definition: regdef.h:38
static const uint8_t log_add_tab[212]
Definition: dolby_e.c:575
static int log_add(int a, int b)
Definition: dolby_e.c:711
static const float start_window[192]
Definition: dolby_e.c:199
static const DBEGroup grp_tab_7[1]
Definition: dolby_e.c:176
void(* imdct_half)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:103
static av_cold void dolby_e_flush(AVCodecContext *avctx)
Definition: dolby_e.c:1171
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static const DBEGroup *const frm_ofs_tab[2][4]
Definition: dolby_e.c:180
static void bit_allocate(int nb_exponent, int nb_code, int fr_code, int *exp, int *bap, int fg_spc, int fg_ofs, int msk_mod, int snr_ofs)
Definition: dolby_e.c:750
const uint8_t * nb_mantissa
Definition: dolby_e.c:47
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:117
float history[MAX_CHANNELS][256]
Definition: dolby_e.c:80
#define MAX_EXPONENTS
Definition: dolby_e.c:36
#define ff_mdct_end
Definition: fft.h:162
void(* vector_fmul_add)(float *dst, const float *src0, const float *src1, const float *src2, int len)
Calculate the entry wise product of two vectors of floats, add a third vector of floats and store the...
Definition: float_dsp.h:137
static const DBEGroup grp_tab_0[1]
Definition: dolby_e.c:122
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:64
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
void * priv_data
Definition: avcodec.h:558
int ch_size[MAX_CHANNELS]
Definition: dolby_e.h:66
int exponents[MAX_EXPONENTS]
Definition: dolby_e.c:65
int channels
number of audio channels
Definition: avcodec.h:1192
Coded Dolby E header values up to end_gain element, plus derived values.
Definition: dolby_e.h:55
static int parse_exponents(DBEContext *s, DBEChannel *c)
Definition: dolby_e.c:692
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:175
int begin_gain[MAX_CHANNELS]
Definition: dolby_e.h:71
uint16_t exp_ofs
Definition: dolby_e.c:45
static int skip_input(DBEContext *s, int nb_words)
Definition: dolby_e.c:607
static int convert_input(DBEContext *s, int nb_words, int key)
Definition: dolby_e.c:631
static av_cold void init_tables(void)
Definition: dolby_e.c:1191
static float mantissa_tab1[17][4]
Definition: dolby_e.c:599
uint8_t nb_exponent
Definition: dolby_e.c:43
and forward the result(frame or status change) to the corresponding input.If nothing is possible
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:940
#define av_freep(p)
static const uint8_t band_low_tab[3]
Definition: dolby_e.c:332
int bw_code
Definition: dolby_e.c:58
#define MAX_MSTR_EXP
Definition: dolby_e.c:39
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:361
uint64_t request_channel_layout
Request decoder to use this channel layout if it can (0 for default)
Definition: avcodec.h:1249
This structure stores compressed data.
Definition: packet.h:340
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:380
static void unbias_exponents(DBEContext *s, DBEChannel *c, DBEGroup *g)
Definition: dolby_e.c:675
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
int word_bits
Definition: dolby_e.h:44
for(j=16;j >0;--j)
int i
Definition: input.c:407
static const uint8_t ch_reorder_6[6]
Definition: dolby_e.c:92
GLuint buffer
Definition: opengl_enc.c:101
static int parse_meter(DBEDecodeContext *s1)
Definition: dolby_e.c:1011
static float window[3712]
Definition: dolby_e.c:605
void * avctx
Definition: dolby_e.h:38
static av_cold int dolby_e_close(AVCodecContext *avctx)
Definition: dolby_e.c:1178
bitstream writer API