Coverage Report

Created: 2026-05-23 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/siren.c
Line
Count
Source
1
/*
2
 * Siren audio decoder
3
 * Copyright (c) 2012 Youness Alaoui <kakaroto@kakaroto.homelinux.net>
4
 * Copyright (c) 2018 Paul B Mahol
5
 * Copyright (c) 2019 Lynne <dev@lynne.ee>
6
 *
7
 * This file is part of FFmpeg.
8
 *
9
 * FFmpeg is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * FFmpeg is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with FFmpeg; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 */
23
24
#include "libavutil/channel_layout.h"
25
#include "libavutil/mem.h"
26
#include "libavutil/tx.h"
27
#include "libavutil/float_dsp.h"
28
#include "libavutil/mem_internal.h"
29
30
#include "avcodec.h"
31
#include "codec_internal.h"
32
#include "decode.h"
33
#include "get_bits.h"
34
35
static const uint8_t index_table[8] = {4, 4, 3, 3, 2, 2, 1, 0};
36
static const uint8_t vector_dimension[8] = { 2, 2, 2, 4, 4, 5, 5, 1 };
37
static const uint8_t number_of_vectors[8] = { 10, 10, 10, 5, 5, 4, 4, 20 };
38
static const uint8_t expected_bits_table[8] = { 52, 47, 43, 37, 29, 22, 16, 0 };
39
static const int8_t differential_decoder_tree[27][24][2] = {
40
    {
41
        {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, -12}, {-11, -10}, {-8, -9}, {-7, -6}, {-13, 12},
42
        {-5, -4}, {0, 13}, {-3, -14}, {-2, 14}, {-1, 15}, {-15, 16}, {-16, 17}, {-17, 18}, {19, 20},
43
        {21, 22}, {-18, -19}, {-20, -21}, {-22, -23}, {-32, -32}
44
    },
45
    {
46
        {1, 2}, {3, 4}, {5, 6}, {7, 8}, {-10, -9}, {-8, -11}, {-7, -6}, {9, -5}, {10, -12}, {-4, 11},
47
        {-13, -3}, {12, -2}, {13, -14}, {-1, 14}, {15, -15}, {0, 16}, {-16, 17}, {-17, 18}, {-18, 19},
48
        {20, 21},{22, -19}, {-20, -21}, {-22, -23}, {-32, -32}
49
    },
50
    {
51
        {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {-12, 11}, {-11, -13}, {-10, -9}, {12, -14}, {-8, -7},
52
        {-15, -6}, {13, -5}, {-16, -4}, {14, -17}, {15, -3}, {16, -18}, {-2, 17}, {18, -19}, {-1, 19},
53
        {-20, 20}, {0, 21}, {22, -21}, {-22, -23}, {-32, -32}
54
    },
55
    {
56
        {1, 2}, {3, 4}, {5, 6}, {-11, -10}, {7, -12}, {8, -9}, {9, -13}, {-14, 10}, {-8, -15}, {-16, 11},
57
        {-7, 12}, {-17, -6}, {13, 14}, {-18, 15}, {-5, -4}, {16, 17}, {-3, -2}, {-19, 18}, {-1, 19},
58
        {-20, 20}, {21, 22}, {0, -21}, {-22, -23}, {-32, -32}
59
    },
60
    {
61
        {1, 2}, {3, 4}, {5, 6}, {-12, -11}, {-13, 7}, {8, -14}, {-10, 9}, {10, -15}, {-9, 11}, {-8, 12},
62
        {-16, 13}, {-7, -6}, {-17, 14}, {-5, -18}, {15, -4}, {16, -19}, {17, -3}, {-20, 18}, {-2, 19},
63
        {-21, 20}, {0, 21}, {22, -1}, {-22, -23}, {-32, -32}
64
    },
65
    {
66
        {1, 2}, {3, 4}, {5, 6}, {-11, 7}, {-12, -10}, {-13, -9}, {8, 9}, {-14, -8}, {10, -15}, {-7, 11},
67
        {-16, 12}, {-6, -17}, {13, 14}, {-5, 15}, {-18, 16}, {-4, 17}, {-3, -19}, {18, -2}, {-20, 19},
68
        {-1, 20}, {0, 21}, {22, -21}, {-22, -23}, {-32, -32}
69
    },
70
    {
71
        {1, 2}, {3, 4}, {5, -12}, {6, -11}, {-10, -13}, {-9, 7}, {8, -14}, {9, -8}, {-15, 10}, {-7, -16},
72
        {11, -6}, {12, -17}, {13, -5}, {-18, 14}, {15, -4}, {-19, 16}, {17, -3}, {-20, 18}, {19, 20},
73
        {21, 22}, {0, -2}, {-1, -21}, {-22, -23}, {-32, -32}
74
    },
75
    {
76
        {1, 2}, {3, 4}, {5, -12}, {6, -13}, {-11, -10}, {7, -14}, {8, -9}, {9, -15}, {-8, 10}, {-7, -16},
77
        {11, 12}, {-6, -17}, {-5, 13}, {14, 15}, {-18, -4}, {-19, 16}, {-3, 17}, {18, -2}, {-20, 19},
78
        {20, 21}, {22, 0}, {-1, -21}, {-22, -23}, {-32, -32}
79
    },
80
    {
81
        {1, 2}, {3, 4}, {5, 6}, {-11, -10}, {-12, -9}, {7, 8}, {-13, -8}, {9, -14}, {-7, 10}, {-6, -15},
82
        {11, 12}, {-5, -16}, {13, 14}, {-17, 15}, {-4, 16}, {17, -18}, {18, -3}, {-2, 19}, {-1, 0},
83
        {-19, 20}, {-20, 21}, {22, -21}, {-22, -23}, {-32, -32}
84
    },
85
    {
86
        {1, 2}, {3, 4}, {5, 6}, {-11, 7}, {-10, -12}, {-9, 8}, {-8, -13}, {9, -7}, {10, -14}, {-6, 11},
87
        {-15, 12}, {-5, 13}, {-16, -4}, {14, 15}, {-17, -3}, {-18, 16}, {17, -19}, {-2, 18}, {-20, 19},
88
        {-1, 20}, {21, 22}, {0, -21}, {-22, -23}, {-32, -32}
89
    },
90
    {
91
        {1, 2}, {3, 4}, {5, -12}, {6, -11}, {7, 8}, {-10, -13}, {-9, 9}, {-8, -14}, {10, -7}, {11, -15},
92
        {-6, 12}, {-5, 13}, {-4, -16}, {14, 15}, {-3, -17}, {16, 17}, {-18, -2}, {18, -19}, {-1, 19},
93
        {-20, 20}, {-21, 21}, {22, 0}, {-22, -23}, {-32, -32}
94
    },
95
    {
96
        {1, 2}, {3, 4}, {5, -12}, {-13, 6}, {-11, 7}, {-14, 8}, {-10, 9}, {-15, -9}, {-8, 10}, {-7, -16},
97
        {11, -6}, {12, -5}, {-17, 13}, {14, -18}, {15, -4}, {16, -19}, {17, -3}, {18, -2}, {19, -1},
98
        {-20, 20}, {21, 22}, {0, -21}, {-22, -23}, {-32, -32}
99
    },
100
    {
101
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
102
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
103
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
104
    },
105
    {
106
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
107
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
108
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
109
    },
110
    {
111
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
112
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
113
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
114
    },
115
    {
116
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
117
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
118
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
119
    },
120
    {
121
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
122
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
123
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
124
    },
125
    {
126
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
127
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
128
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
129
    },
130
    {
131
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
132
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
133
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
134
    },
135
    {
136
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
137
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
138
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
139
    },
140
    {
141
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
142
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
143
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
144
    },
145
    {
146
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
147
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
148
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
149
    },
150
    {
151
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
152
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
153
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
154
    },
155
    {
156
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
157
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
158
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
159
    },
160
    {
161
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
162
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
163
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
164
    },
165
    {
166
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
167
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
168
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
169
    },
170
    {
171
        {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
172
        {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
173
        {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
174
    },
175
};
176
177
static const uint16_t decoder_tree0[360] = {
178
    2, 1, 4, 6, 8, 10, 12, 14, 16, 18, 33, 3, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 35, 40,
179
    42, 44, 46, 5, 48, 65, 50, 52, 54, 56, 58, 60, 62, 64, 37, 66, 67, 68, 97, 70, 72, 74, 7,
180
    76, 78, 80, 82, 84, 86, 88, 99, 90, 39, 92, 94, 96, 129, 98, 9, 100, 102, 104, 106, 108,
181
    110, 112, 41, 161, 69, 114, 116, 118, 131, 120, 122, 11, 124, 126, 128, 193, 130, 132, 71,
182
    134, 43, 136, 138, 140, 163, 101, 13, 142, 144, 146, 148, 150, 152, 154, 225, 156, 158, 195,
183
    160, 162, 45, 164, 15, 166, 73, 168, 170, 133, 47, 172, 257, 174, 176, 178, 75, 103, 180, 165,
184
    182, 17, 227, 184, 105, 49, 135, 186, 289, 188, 259, 190, 192, 194, 196, 198, 291, 77, 200,
185
    202, 197, 107, 204, 19, 51, 229, 206, 167, 208, 210, 212, 214, 21, 79, 81, 109, 216, 218, 220,
186
    222, 53, 137, 224, 199, 226, 323, 321, 169, 228, 111, 230, 232, 139, 261, 234, 83, 236, 201,
187
    238, 240, 293, 242, 353, 231, 141, 244, 246, 113, 23, 355, 85, 248, 55, 115, 250, 263, 252,
188
    254, 203, 171, 256, 258, 233, 235, 143, 357, 325, 260, 295, 262, 173, 145, 177, 87, 264, 327,
189
    267, 266, 268, 175, 270, 272, 117, 297, 274, 265, 147, 179, 205, 276, 207, 237, 269, 278, 57,
190
    59, 387, 209, 280, 282, 149, 329, 385, 284, 25, 286, 239, 119, 288, 27, 290, 292, 299, 294, 359,
191
    89, 296, 298, 419, 181, 300, 331, 271, 417, 211, 361, 151, 389, 241, 302, 304, 303, 306, 308,
192
    421, 91, 310, 312, 391, 314, 121, 316, 333, 318, 275, 213, 301, 243, 183, 335, 320, 363, 322,
193
    215, 324, 393, 273, 337, 153, 326, 423, 365, 328, 367, 247, 395, 185, 123, 330, 425, 245, 155,
194
    332, 334, 305, 397, 336, 277, 217, 338, 340, 339, 427, 342, 344, 346, 307, 399, 187, 348, 309,
195
    341, 350, 369, 279, 311, 429, 249, 219, 352, 354, 356, 358, 431, 373, 401, 371, 313, 281, 433,
196
    343, 403, 251, 283
197
};
198
199
static const uint16_t decoder_tree1[188] = {
200
    2, 1, 4, 6, 8, 10, 12, 14, 16, 3, 33, 18, 20, 22, 24, 26, 35, 28, 30, 32, 34, 36, 5, 65, 38, 40,
201
    37, 42, 44, 46, 67, 48, 50, 52, 54, 56, 58, 60, 7, 62, 39, 97, 64, 69, 66, 99, 68, 70, 72, 74, 76,
202
    78, 80, 129, 41, 131, 82, 9, 71, 84, 86, 101, 88, 90, 92, 94, 96, 161, 43, 11, 73, 98, 103, 100,
203
    163, 102, 104, 106, 108, 133, 110, 105, 112, 75, 114, 45, 13, 116, 165, 118, 195, 135, 193, 120, 77,
204
    122, 47, 124, 167, 225, 126, 79, 107, 227, 128, 137, 197, 15, 130, 169, 199, 132, 109, 134, 17, 139,
205
    49, 136, 229, 138, 140, 81, 259, 142, 144, 171, 146, 141, 148, 111, 150, 201, 231, 152, 51, 257, 289,
206
    154, 19, 113, 156, 261, 158, 203, 173, 263, 143, 160, 291, 235, 83, 162, 233, 265, 164, 205, 166, 293,
207
    145, 168, 175, 177, 237, 115, 295, 170, 207, 172, 267, 174, 176, 297, 147, 178, 180, 269, 182, 271,
208
    209, 299, 239, 179, 184, 301, 241, 211, 0, 0
209
};
210
211
static const uint16_t decoder_tree2[96] = {
212
    2, 1, 4, 6, 8, 10, 12, 3, 17, 14, 19, 16, 18, 20, 22, 24, 26, 5, 21, 35, 33, 28, 30, 32, 34, 36, 38, 37,
213
    40, 23, 51, 42, 7, 49, 44, 46, 48, 50, 39, 53, 52, 54, 56, 25, 67, 9, 58, 60, 65, 55, 41, 62, 64, 69, 66,
214
    11, 27, 68, 57, 83, 70, 71, 81, 43, 72, 74, 13, 76, 85, 29, 73, 78, 99, 59, 87, 101, 80, 97, 45, 82, 84,
215
    75, 89, 61, 86, 103, 88, 77, 90, 105, 91, 92, 107, 93, 0, 0
216
};
217
218
static const uint16_t decoder_tree3[1040] = {
219
    2, 4, 6, 8, 10, 1, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 3, 36, 1025, 38, 40, 42, 44, 46, 48, 50,
220
    129, 17, 52, 54, 1153, 19, 56, 58, 60, 62, 64, 66, 68, 145, 70, 72, 74, 76, 78, 1169, 1027, 147, 80, 82, 1171,
221
    84, 86, 131, 88, 1155, 1043, 1041, 90, 92, 5, 94, 96, 98, 100, 102, 104, 21, 106, 108, 2049, 2177, 110, 112, 114,
222
    116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 33, 144, 163, 146, 148, 150, 152, 154, 161,
223
    156, 35, 158, 1297, 160, 162, 273, 257, 164, 166, 149, 168, 1281, 170, 172, 2193, 174, 176, 178, 1299, 180, 1045,
224
    182, 184, 1173, 186, 3201, 188, 190, 192, 194, 2195, 1187, 23, 2179, 196, 7, 198, 275, 200, 2051, 202, 2065, 204,
225
    206, 1029, 1185, 208, 210, 1157, 37, 3073, 2067, 133, 212, 214, 2321, 216, 165, 218, 1059, 220, 1283, 222, 2305,
226
    224, 226, 228, 230, 259, 232, 234, 2323, 236, 1409, 1057, 1315, 238, 240, 242, 244, 246, 1425, 248, 1313, 250, 252,
227
    254, 256, 258, 260, 289, 262, 264, 1189, 266, 268, 179, 151, 270, 272, 274, 276, 278, 291, 280, 282, 9, 385, 284,
228
    286, 177, 49, 401, 1061, 288, 290, 292, 51, 294, 296, 298, 300, 302, 304, 25, 306, 2083, 39, 308, 310, 3329, 167,
229
    312, 314, 1175, 316, 318, 1203, 135, 320, 322, 324, 326, 328, 2211, 2307, 330, 1301, 332, 334, 1047, 336, 338, 2449,
230
    3217, 340, 1427, 2209, 53, 342, 2339, 3345, 344, 346, 348, 403, 181, 4097, 2197, 350, 2181, 1285, 1317, 1031, 352,
231
    354, 356, 3089, 358, 360, 4225, 277, 362, 364, 366, 368, 2069, 370, 3203, 293, 1201, 305, 372, 3219, 307, 2433, 374,
232
    376, 378, 380, 2081, 1411, 382, 384, 3075, 1443, 513, 386, 387, 388, 390, 1331, 261, 392, 394, 396, 398, 400, 1441,
233
    1075, 67, 1159, 402, 404, 406, 408, 410, 412, 414, 3347, 2325, 416, 65, 418, 420, 422, 424, 426, 2053, 193, 1073, 428,
234
    430, 432, 1537, 1329, 2337, 2213, 434, 417, 183, 41, 436, 438, 440, 442, 444, 446, 448, 450, 195, 2435, 452, 2085, 1063,
235
    1191, 454, 456, 458, 460, 419, 2071, 1553, 3091, 55, 137, 462, 464, 466, 468, 470, 472, 474, 476, 478, 2309, 4113, 480,
236
    482, 484, 486, 2451, 2465, 1205, 153, 488, 490, 492, 494, 496, 498, 500, 502, 504, 506, 508, 510, 512, 514, 516, 518,
237
    520, 522, 524, 1333, 526, 1555, 2467, 2227, 3205, 3331, 528, 530, 532, 534, 536, 538, 540, 542, 544, 546, 548, 529, 309,
238
    1303, 3473, 3457, 389, 1569, 1445, 1077, 69, 2199, 1539, 4353, 550, 552, 554, 556, 558, 560, 562, 1459, 4241, 3221, 1429,
239
    2341, 279, 3475, 169, 564, 545, 3105, 323, 2353, 2097, 3235, 421, 2229, 3107, 3233, 566, 568, 570, 572, 574, 576, 578,
240
    580, 582, 584, 586, 588, 590, 592, 594, 596, 2099, 1091, 531, 2437, 4227, 405, 197, 263, 1287, 2577, 1049, 1571, 598, 600,
241
    602, 604, 606, 608, 610, 612, 614, 616, 618, 620, 622, 624, 626, 628, 630, 632, 634, 636, 638, 640, 642, 644, 646, 648, 650,
242
    1345, 1219, 3077, 1457, 2225, 2579, 515, 2561, 2469, 433, 1221, 2183, 4243, 652, 654, 656, 658, 660, 662, 664, 666, 668,
243
    670, 1217, 3333, 3093, 435, 321, 4369, 1089, 2055, 4099, 3361, 1319, 547, 1161, 1177, 672, 2355, 4115, 1413, 4257, 3349,
244
    2453, 3109, 2357, 2215, 3363, 1079, 1207, 311, 1033, 1347, 1065, 674, 676, 678, 680, 682, 684, 686, 688, 690, 692, 694, 696,
245
    698, 700, 702, 704, 706, 708, 710, 712, 714, 716, 718, 720, 722, 724, 726, 728, 730, 732, 734, 736, 738, 740, 742, 744, 746,
246
    748, 750, 752, 754, 756, 758, 760, 762, 764, 766, 768, 770, 772, 774, 776, 778, 780, 782, 784, 786, 788, 790, 792, 794, 796,
247
    798, 800, 802, 804, 806, 808, 810, 812, 814, 2593, 2565, 4261, 3253, 437, 325, 3489, 2311, 4259, 1431, 2087, 2563, 295, 2343,
248
    449, 199, 265, 2201, 4371, 1193, 816, 533, 1557, 2581, 2241, 3365, 3491, 3603, 549, 2101, 1461, 1093, 2117, 3459, 3079, 4481,
249
    3095, 2327, 3461, 4129, 3249, 1447, 2471, 2231, 71, 4497, 2609, 1289, 393, 3251, 2073, 3097, 2371, 1305, 2089, 818, 820, 822,
250
    824, 826, 828, 830, 832, 834, 836, 838, 840, 842, 844, 846, 848, 850, 852, 854, 856, 858, 860, 862, 864, 866, 868, 870, 872,
251
    874, 876, 878, 880, 882, 884, 886, 888, 890, 892, 894, 896, 898, 900, 902, 904, 906, 908, 910, 912, 914, 916, 918, 920, 922,
252
    924, 926, 928, 930, 932, 934, 936, 938, 940, 942, 944, 946, 948, 950, 952, 954, 956, 958, 960, 962, 964, 966, 968, 970, 972,
253
    974, 976, 978, 980, 982, 984, 986, 988, 990, 992, 994, 996, 998, 1000, 1002, 1004, 1006, 1008, 1010, 1012, 1014, 1016, 1018,
254
    1020, 1022, 1024, 1026, 1028, 1030, 1032, 1034, 1036, 4161, 4273, 3507, 3493, 4517, 2497, 1573, 2597, 3621, 4531, 4627, 3523,
255
    3125, 4149, 4529, 3139, 4515, 451, 4277, 2113, 4163, 4499, 3381, 4405, 1473, 4373, 2485, 3509, 565, 1589, 2613, 3585, 3123,
256
    4403, 3141, 4147, 563, 2245, 3269, 4357, 1349, 2373, 3397, 453, 1477, 2501, 2481, 579, 1601, 3477, 4103, 3265, 2243, 1587,
257
    3207, 4231, 3267, 4501, 1475, 3335, 4359, 391, 1415, 2439, 3463, 4487, 519, 1543, 2567, 3591, 4609, 4289, 4611, 2499, 4119,
258
    4385, 4145, 4401, 3223, 4247, 3379, 577, 3393, 3351, 4375, 407, 1585, 2455, 3479, 4503, 535, 1559, 2583, 3607, 3605, 4513,
259
    4485, 3111, 4135, 3121, 517, 3377, 3239, 4263, 1541, 4291, 4229, 3367, 4391, 423, 2115, 4131, 3495, 551, 1575, 2599, 3635, 3395,
260
    2103, 3127, 4151, 3589, 4101, 1603, 3255, 4279, 3601, 1335, 2359, 3383, 439, 1463, 2487, 3511, 567, 1591, 4133, 1095, 2119, 3143,
261
    2369, 1223, 2247, 3271, 327, 1351, 2375, 455, 1479, 3137, 3521, 2057, 3081, 4105, 4387, 3505, 2185, 3209, 4233, 3587, 4355, 2313,
262
    3337, 3237, 1417, 2441, 3465, 521, 1545, 3617, 3633, 561, 4625, 4121, 2611, 2483, 2595, 3225, 4249, 281, 4245, 2329, 3353, 409,
263
    1433, 2457, 3481, 537, 1561, 4483, 3619, 4389, 3113, 4275, 4117, 2217, 3241, 297, 1321, 2345, 3369, 425, 1449, 2473, 57, 1081,
264
    2105, 3129, 185, 1209, 2233, 3257, 313, 1337, 2361, 441, 1465, 73, 1097, 201, 1225, 0, 0
265
};
266
267
static const uint16_t decoder_tree4[416] = {
268
    2, 4, 6, 1, 8, 10, 12, 14, 16, 18, 20, 22, 24, 3, 129, 26, 28, 9, 33, 30, 32,
269
    34, 36, 11, 161, 38, 40, 42, 41, 44, 46, 131, 43, 169, 35, 48, 137, 50, 52, 54, 56, 139,
270
    163, 171, 58, 60, 62, 64, 5, 66, 68, 70, 257, 72, 74, 76, 13, 78, 80, 289, 82, 84, 17,
271
    86, 88, 65, 90, 201, 19, 92, 94, 51, 193, 96, 98, 49, 100, 73, 102, 104, 106, 45, 108, 110,
272
    297, 112, 114, 116, 37, 203, 118, 120, 179, 122, 177, 124, 265, 126, 75, 133, 259, 291, 147, 128, 67,
273
    195, 130, 141, 173, 299, 132, 145, 134, 165, 136, 138, 140, 142, 7, 144, 146, 21, 267, 148, 53, 150,
274
    321, 152, 154, 15, 156, 81, 158, 160, 385, 162, 417, 164, 166, 168, 83, 170, 172, 329, 174, 211, 176,
275
    27, 178, 180, 182, 209, 184, 186, 188, 190, 25, 192, 331, 194, 196, 105, 57, 198, 97, 200, 202, 323,
276
    225, 59, 149, 204, 206, 233, 307, 208, 77, 181, 210, 212, 214, 216, 218, 220, 222, 47, 224, 226, 69,
277
    228, 230, 197, 232, 425, 393, 205, 275, 293, 39, 234, 236, 238, 305, 135, 155, 301, 143, 240, 242, 235,
278
    395, 244, 246, 248, 250, 252, 254, 256, 258, 260, 262, 273, 269, 185, 264, 266, 268, 270, 272, 274, 276,
279
    261, 153, 278, 280, 282, 187, 337, 387, 107, 284, 427, 227, 167, 419, 286, 288, 290, 292, 294, 296, 298,
280
    300, 302, 304, 306, 308, 310, 312, 314, 316, 318, 320, 322, 324, 326, 328, 330, 332, 334, 336, 338, 115,
281
    99, 85, 213, 29, 113, 23, 89, 241, 61, 449, 339, 175, 340, 342, 344, 346, 348, 350, 352, 354, 356,
282
    358, 360, 362, 364, 366, 368, 370, 372, 374, 376, 378, 380, 382, 384, 386, 388, 390, 392, 394, 396, 398,
283
    400, 402, 404, 406, 408, 410, 412, 414, 389, 361, 457, 465, 429, 451, 333, 109, 277, 243, 263, 295, 199,
284
    283, 151, 55, 183, 229, 357, 363, 123, 491, 397, 411, 251, 313, 441, 467, 345, 433, 461, 219, 237, 365,
285
    435, 353, 347, 405, 409, 217, 309, 437, 369, 371, 341, 117, 245, 249, 157, 285, 403, 189, 317, 93, 221,
286
    315, 401, 481, 391, 489, 121, 421, 423, 71, 483, 327, 103, 231, 443, 459, 271, 399, 355, 91, 303, 431,
287
    79, 207, 335, 111, 239, 281, 325, 279, 453, 101, 311, 87, 215, 31, 159, 63, 191
288
};
289
290
static const uint16_t decoder_tree5[384] = {
291
    2, 4, 1, 6, 8, 10, 12, 14, 16, 18, 20, 22, 3, 513, 24, 26, 28, 9, 129, 33, 30, 32, 34, 36, 38, 40, 11, 42, 641, 44, 46, 41,
292
    161, 48, 515, 50, 52, 131, 54, 35, 545, 137, 56, 58, 60, 521, 62, 43, 673, 64, 169, 66, 68, 523, 70, 163, 643, 139, 553, 72, 649, 74, 547,
293
    76, 78, 80, 681, 171, 82, 84, 555, 86, 675, 88, 651, 5, 90, 92, 1025, 94, 96, 98, 683, 13,
294
    100, 17, 102, 104, 106, 65, 108, 110, 257, 112, 114, 1153, 19, 116, 118, 120, 122, 124, 49, 126, 128,
295
    769, 289, 130, 132, 134, 73, 136, 138, 140, 142, 193, 144, 146, 148, 150, 152, 154, 517, 156, 158, 37,
296
    51, 160, 201, 162, 145, 164, 166, 168, 133, 170, 801, 45, 172, 174, 1057, 176, 178, 67, 180, 1027, 577,
297
    182, 184, 186, 188, 190, 192, 194, 196, 198, 259, 200, 202, 204, 525, 177, 265, 141, 206, 208, 210, 212,
298
    195, 297, 214, 75, 216, 1033, 203, 585, 1155, 1185, 267, 1161, 549, 218, 220, 657, 777, 147, 222, 224, 226,
299
    228, 230, 232, 234, 236, 238, 240, 587, 645, 165, 242, 244, 246, 248, 250, 771, 291, 252, 579, 1065, 1035,
300
    705, 531, 529, 659, 173, 254, 561, 653, 256, 713, 677, 557, 258, 260, 262, 264, 266, 268, 270, 272, 274,
301
    276, 278, 280, 282, 284, 286, 288, 290, 292, 294, 296, 298, 300, 707, 1059, 809, 715, 563, 179, 691, 1193,
302
    21, 779, 1067, 299, 1187, 302, 304, 306, 308, 310, 312, 314, 316, 318, 320, 322, 324, 326, 328, 330, 332,
303
    334, 336, 338, 340, 342, 344, 346, 348, 350, 352, 354, 356, 358, 360, 362, 364, 366, 368, 370, 372, 374,
304
    376, 378, 380, 83, 69, 1281, 803, 321, 1195, 1163, 811, 1323, 689, 1321, 1099, 305, 835, 1227, 331, 843, 785,
305
    593, 1043, 1291, 1283, 1171, 275, 787, 1217, 833, 1075, 1313, 1219, 1203, 307, 819, 841, 595, 211, 723, 721, 817,
306
    1029, 329, 81, 1157, 261, 773, 1097, 1089, 1061, 1169, 1091, 1189, 293, 805, 1201, 581, 197, 709, 1289, 273, 1037,
307
    1315, 1041, 1165, 269, 781, 209, 1073, 1069, 323, 685, 1197, 301, 813, 77, 589, 205, 717, 1225, 533, 149, 661,
308
    53, 565, 181, 693, 0, 0
309
};
310
311
static const uint16_t decoder_tree6[62] = {
312
    2, 1, 4, 6, 8, 10, 12, 14, 16, 3, 33, 5, 17, 9, 18, 20, 22, 24, 26, 28, 30, 32, 34, 7, 49, 13, 25, 36, 38, 11,
313
    21, 41, 35, 37, 19, 40, 42, 44, 46, 48, 50, 15, 52, 57, 29, 27, 23, 53, 54, 51, 39, 45, 43, 56, 58, 31, 55, 60,
314
    61, 47, 59, 63
315
};
316
317
static const uint16_t *const decoder_tables[7] = {
318
    decoder_tree0,
319
    decoder_tree1,
320
    decoder_tree2,
321
    decoder_tree3,
322
    decoder_tree4,
323
    decoder_tree5,
324
    decoder_tree6,
325
};
326
327
static const int decoder_tables_elements[7] = {
328
    FF_ARRAY_ELEMS(decoder_tree0),
329
    FF_ARRAY_ELEMS(decoder_tree1),
330
    FF_ARRAY_ELEMS(decoder_tree2),
331
    FF_ARRAY_ELEMS(decoder_tree3),
332
    FF_ARRAY_ELEMS(decoder_tree4),
333
    FF_ARRAY_ELEMS(decoder_tree5),
334
    FF_ARRAY_ELEMS(decoder_tree6),
335
};
336
337
static const float mlt_quant[7][14] = {
338
    { 0.0f, 0.392f, 0.761f, 1.120f, 1.477f, 1.832f, 2.183f, 2.541f, 2.893f, 3.245f, 3.598f, 3.942f, 4.288f, 4.724f },
339
    { 0.0f, 0.544f, 1.060f, 1.563f, 2.068f, 2.571f, 3.072f, 3.562f, 4.070f, 4.620f, 0.0f, 0.0f, 0.0f, 0.0f },
340
    { 0.0f, 0.746f, 1.464f, 2.180f, 2.882f, 3.584f, 4.316f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f },
341
    { 0.0f, 1.006f, 2.000f, 2.993f, 3.985f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f },
342
    { 0.0f, 1.321f, 2.703f, 3.983f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f },
343
    { 0.0f, 1.657f, 3.491f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f },
344
    { 0.0f, 1.964f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }
345
};
346
347
static const float noise_category5[21] = {
348
    0.70711f, 0.6179f, 0.5005f, 0.3220f, 0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f,
349
    0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f
350
};
351
352
static const float noise_category6[21] = {
353
    0.70711f, 0.5686f, 0.3563f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f,
354
    0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f
355
};
356
357
133M
#define FRAME_SIZE 320
358
38.8M
#define REGION_SIZE 20
359
360
typedef struct SirenContext {
361
    GetBitContext gb;
362
363
    int microsoft;
364
    int rate_control_possibilities;
365
    int esf_adjustment;
366
    int number_of_regions;
367
    int scale_factor;
368
    int sample_rate_bits;
369
    int checksum_bits;
370
371
    unsigned dw1, dw2, dw3, dw4;
372
373
    int absolute_region_power_index[32];
374
    float decoder_standard_deviation[32];
375
    int power_categories[32];
376
    int category_balance[32];
377
    float standard_deviation[64];
378
    float backup_frame[FRAME_SIZE];
379
380
    AVFloatDSPContext *fdsp;
381
    av_tx_fn           tx_fn;
382
    AVTXContext       *tx_ctx;
383
384
    DECLARE_ALIGNED(32, float, imdct_buf)[4][FRAME_SIZE];
385
    float          *window;
386
    float          *imdct_in;
387
    float          *imdct_out;
388
    float          *imdct_prev;
389
} SirenContext;
390
391
static av_cold int siren_init(AVCodecContext *avctx)
392
1.57k
{
393
1.57k
    const float scale = 1.0f / (22.f * 32768.f);
394
1.57k
    SirenContext *s = avctx->priv_data;
395
1.57k
    int i;
396
397
1.57k
    s->imdct_in   = s->imdct_buf[0];
398
1.57k
    s->imdct_out  = s->imdct_buf[1];
399
1.57k
    s->imdct_prev = s->imdct_buf[2];
400
1.57k
    s->window     = s->imdct_buf[3];
401
402
1.57k
    av_channel_layout_uninit(&avctx->ch_layout);
403
1.57k
    avctx->ch_layout      = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
404
1.57k
    avctx->sample_fmt     = AV_SAMPLE_FMT_FLT;
405
406
1.57k
    s->rate_control_possibilities = 16;
407
1.57k
    s->esf_adjustment = 7;
408
1.57k
    s->number_of_regions = 14;
409
1.57k
    s->scale_factor = 22;
410
1.57k
    s->dw1 = s->dw2 = s->dw3 = s->dw4 = 1;
411
412
102k
    for (i = 0; i < 64; i++) {
413
100k
        float region_power = powf(10, (i - 24) * 0.3010299957);
414
415
100k
        s->standard_deviation[i] = sqrtf(region_power);
416
100k
    }
417
418
505k
    for (i = 0; i < FRAME_SIZE; i++) {
419
504k
        float angle = ((i + 0.5f) * M_PI_2) / 320.f;
420
504k
        s->window[i] = sinf(angle);
421
504k
    }
422
423
1.57k
    s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
424
1.57k
    if (!s->fdsp)
425
0
        return AVERROR(ENOMEM);
426
427
1.57k
    s->microsoft = avctx->codec->id == AV_CODEC_ID_MSNSIREN;
428
1.57k
    if (s->microsoft) {
429
810
        s->esf_adjustment = -2;
430
810
        s->number_of_regions = 14;
431
810
        s->scale_factor = 1;
432
810
        s->sample_rate_bits = 2;
433
810
        s->checksum_bits = 4;
434
810
    }
435
436
1.57k
    return av_tx_init(&s->tx_ctx, &s->tx_fn, AV_TX_FLOAT_MDCT, 1, FRAME_SIZE, &scale, 0);
437
1.57k
}
438
439
static int decode_envelope(SirenContext *s, GetBitContext *gb,
440
                           int number_of_regions, float *decoder_standard_deviation,
441
                           int *absolute_region_power_index, int esf_adjustment)
442
1.01M
{
443
1.01M
    absolute_region_power_index[0] = (int)get_bits(gb, 5) - esf_adjustment;
444
1.01M
    absolute_region_power_index[0] = av_clip(absolute_region_power_index[0], -24, 39);
445
1.01M
    decoder_standard_deviation[0] = s->standard_deviation[absolute_region_power_index[0] + 24];
446
447
12.0M
    for (int i = 1; i < number_of_regions; i++) {
448
11.2M
        int index = 0;
449
450
37.0M
        do {
451
37.0M
            if (get_bits_left(gb) < 4 + number_of_regions - i + s->checksum_bits)
452
172k
                return AVERROR_INVALIDDATA;
453
36.8M
            index = differential_decoder_tree[i - 1][index][get_bits1(gb)];
454
36.8M
        } while (index > 0);
455
456
11.0M
        absolute_region_power_index[i] = av_clip(absolute_region_power_index[i - 1] - index - 12, -24, 39);
457
11.0M
        decoder_standard_deviation[i] = s->standard_deviation[absolute_region_power_index[i] + 24];
458
11.0M
    }
459
460
842k
    return get_bits_count(gb);
461
1.01M
}
462
463
static int categorize_regions(int number_of_regions, int number_of_available_bits,
464
                              int *absolute_region_power_index, int *power_categories,
465
                              int *category_balance)
466
842k
{
467
842k
    int region, delta, i, temp;
468
842k
    int expected_number_of_code_bits;
469
842k
    int min, max;
470
842k
    int offset, num_rate_control_possibilities = 16,
471
842k
        raw_value, raw_max_idx = 0, raw_min_idx = 0;
472
842k
    int max_rate_categories[28];
473
842k
    int min_rate_categories[28];
474
842k
    int temp_category_balances[64];
475
842k
    int *min_rate_ptr = NULL;
476
842k
    int *max_rate_ptr = NULL;
477
478
842k
    offset = -32;
479
5.89M
    for (delta = 32; number_of_regions > 0 && delta > 0; delta /= 2) {
480
5.05M
        expected_number_of_code_bits = 0;
481
75.8M
        for (region = 0; region < number_of_regions; region++) {
482
70.7M
            i = (delta + offset -
483
70.7M
                 absolute_region_power_index[region]) >> 1;
484
70.7M
            i = av_clip_uintp2(i, 3);
485
70.7M
            power_categories[region] = i;
486
70.7M
            expected_number_of_code_bits += expected_bits_table[i];
487
488
70.7M
        }
489
5.05M
        if (expected_number_of_code_bits >= number_of_available_bits - 32)
490
4.81M
            offset += delta;
491
5.05M
    }
492
493
842k
    expected_number_of_code_bits = 0;
494
12.6M
    for (region = 0; region < number_of_regions; region++) {
495
11.7M
        i = (offset - absolute_region_power_index[region]) >> 1;
496
11.7M
        i = av_clip_uintp2(i, 3);
497
11.7M
        max_rate_categories[region] = min_rate_categories[region] =
498
11.7M
            power_categories[region] = i;
499
11.7M
        expected_number_of_code_bits += expected_bits_table[i];
500
11.7M
    }
501
502
842k
    min = max = expected_number_of_code_bits;
503
842k
    min_rate_ptr = max_rate_ptr =
504
842k
        temp_category_balances + num_rate_control_possibilities;
505
13.2M
    for (i = 0; i < num_rate_control_possibilities - 1; i++) {
506
12.4M
        if (min + max > number_of_available_bits * 2) {
507
11.6M
            raw_value = -99;
508
174M
            for (region = number_of_regions - 1; region >= 0; region--) {
509
162M
                if (min_rate_categories[region] < 7) {
510
125M
                    temp =
511
125M
                        offset - absolute_region_power_index[region] -
512
125M
                        2 * min_rate_categories[region];
513
125M
                    if (temp > raw_value) {
514
50.3M
                        raw_value = temp;
515
50.3M
                        raw_min_idx = region;
516
50.3M
                    }
517
125M
                }
518
162M
            }
519
11.6M
            if (raw_value == -99)
520
19.8k
                return AVERROR_INVALIDDATA;
521
11.5M
            *min_rate_ptr++ = raw_min_idx;
522
11.5M
            min +=
523
11.5M
                expected_bits_table[min_rate_categories[raw_min_idx] + 1] -
524
11.5M
                expected_bits_table[min_rate_categories[raw_min_idx]];
525
11.5M
            min_rate_categories[raw_min_idx]++;
526
11.5M
        } else {
527
804k
            raw_value = 99;
528
12.0M
            for (region = 0; region < number_of_regions; region++) {
529
11.2M
                if (max_rate_categories[region] > 0) {
530
10.4M
                    temp =
531
10.4M
                        offset - absolute_region_power_index[region] -
532
10.4M
                        2 * max_rate_categories[region];
533
10.4M
                    if (temp < raw_value) {
534
1.49M
                        raw_value = temp;
535
1.49M
                        raw_max_idx = region;
536
1.49M
                    }
537
10.4M
                }
538
11.2M
            }
539
804k
            if (raw_value == 99)
540
1.31k
                return AVERROR_INVALIDDATA;
541
542
803k
            *--max_rate_ptr = raw_max_idx;
543
803k
            max += expected_bits_table[max_rate_categories[raw_max_idx] - 1] -
544
803k
                   expected_bits_table[max_rate_categories[raw_max_idx]];
545
803k
            max_rate_categories[raw_max_idx]--;
546
803k
        }
547
12.4M
    }
548
549
12.3M
    for (region = 0; region < number_of_regions; region++)
550
11.4M
        power_categories[region] = max_rate_categories[region];
551
552
13.1M
    for (i = 0; i < num_rate_control_possibilities - 1; i++)
553
12.3M
        category_balance[i] = *max_rate_ptr++;
554
555
821k
    return 0;
556
842k
}
557
558
static int get_dw(SirenContext *s)
559
22.3M
{
560
22.3M
    int ret = s->dw1 + s->dw4;
561
562
22.3M
    if ((ret & 0x8000) != 0)
563
11.1M
        ret++;
564
565
22.3M
    s->dw1 = s->dw2;
566
22.3M
    s->dw2 = s->dw3;
567
22.3M
    s->dw3 = s->dw4;
568
22.3M
    s->dw4 = ret;
569
570
22.3M
    return ret;
571
22.3M
}
572
573
static int decode_vector(SirenContext *s, int number_of_regions,
574
                         float *decoder_standard_deviation,
575
                         int *power_categories, float *coefs, int scale_factor)
576
821k
{
577
821k
    GetBitContext *gb = &s->gb;
578
821k
    float *coefs_ptr;
579
821k
    float decoded_value;
580
821k
    float noise;
581
821k
    const uint16_t *decoder_tree;
582
821k
    int region;
583
821k
    int category;
584
821k
    int i, j;
585
821k
    int index;
586
821k
    int error = 0;
587
821k
    int dw1;
588
821k
    int dw2;
589
590
12.3M
    for (region = 0; region < number_of_regions; region++) {
591
11.4M
        category = power_categories[region];
592
11.4M
        coefs_ptr = coefs + (region * REGION_SIZE);
593
594
11.4M
        if (category >= 0 && category < 7) {
595
1.09M
            decoder_tree = decoder_tables[category];
596
597
5.40M
            for (i = 0; i < number_of_vectors[category]; i++) {
598
4.95M
                index = 0;
599
13.1M
                do {
600
13.1M
                    if (get_bits_left(gb) - s->checksum_bits <= 0) {
601
646k
                        error = 1;
602
646k
                        break;
603
646k
                    }
604
605
12.5M
                    if (index + show_bits1(gb) >= decoder_tables_elements[category]) {
606
0
                        error = 1;
607
0
                        break;
608
0
                    }
609
12.5M
                    index = decoder_tree[index + get_bits1(gb)];
610
12.5M
                } while ((index & 1) == 0);
611
612
4.95M
                index >>= 1;
613
614
4.95M
                if (error == 0) {
615
14.6M
                    for (j = 0; j < vector_dimension[category]; j++) {
616
10.9M
                        decoded_value = mlt_quant[category][index & ((1 << index_table[category]) - 1)];
617
10.9M
                        index >>= index_table[category];
618
619
10.9M
                        if (decoded_value) {
620
2.83M
                            if (get_bits_left(gb) - s->checksum_bits <= 0) {
621
621k
                                error = 1;
622
621k
                                break;
623
621k
                            }
624
2.21M
                            if (!get_bits1(gb))
625
1.30M
                                decoded_value *= -decoder_standard_deviation[region];
626
909k
                            else
627
909k
                                decoded_value *= decoder_standard_deviation[region];
628
2.21M
                        }
629
630
10.3M
                        *coefs_ptr++ = decoded_value * scale_factor;
631
10.3M
                    }
632
4.30M
                } else {
633
646k
                    error = 1;
634
646k
                    break;
635
646k
                }
636
4.95M
            }
637
638
1.09M
            if (error == 1) {
639
8.92M
                for (j = region + 1; j < number_of_regions; j++)
640
8.28M
                    power_categories[j] = 7;
641
647k
                category = 7;
642
647k
            }
643
1.09M
        }
644
645
11.4M
        coefs_ptr = coefs + (region * REGION_SIZE);
646
647
11.4M
        if (category == 5 && s->microsoft) {
648
23.6k
            i = 0;
649
497k
            for (j = 0; j < REGION_SIZE; j++) {
650
473k
                if (*coefs_ptr != 0) {
651
120k
                    i++;
652
120k
                    if (fabs(*coefs_ptr) > 2.0 * decoder_standard_deviation[region]) {
653
6.22k
                        i += 3;
654
6.22k
                    }
655
120k
                }
656
473k
                coefs_ptr++;
657
473k
            }
658
23.6k
            if (i >= FF_ARRAY_ELEMS(noise_category5)) {
659
568
                error = 1;
660
568
                break;
661
568
            }
662
663
23.1k
            noise = decoder_standard_deviation[region] * noise_category5[i];
664
23.1k
        } else
665
11.4M
        if (category == 5 || category == 6) {
666
126k
            i = 0;
667
2.65M
            for (j = 0; j < REGION_SIZE; j++) {
668
2.52M
                if (*coefs_ptr != 0)
669
357k
                    i++;
670
2.52M
                coefs_ptr++;
671
2.52M
            }
672
673
126k
            if (category == 5) {
674
54.4k
                noise = decoder_standard_deviation[region] * noise_category5[i];
675
54.4k
            } else
676
71.9k
                noise = decoder_standard_deviation[region] * noise_category6[i];
677
11.3M
        } else if (category == 7) {
678
11.0M
            noise = decoder_standard_deviation[region] * 0.70711f;
679
11.0M
        } else {
680
298k
            noise = 0;
681
298k
        }
682
683
11.4M
        coefs_ptr = coefs + (region * REGION_SIZE);
684
685
11.4M
        if (category == 5 || category == 6 || category == 7) {
686
11.1M
            dw1 = get_dw(s);
687
11.1M
            dw2 = get_dw(s);
688
689
123M
            for (j = 0; j < 10; j++) {
690
111M
                if (category == 7 || *coefs_ptr == 0)
691
111M
                    *coefs_ptr = dw1 & 1 ? noise : -noise;
692
111M
                coefs_ptr++;
693
111M
                dw1 >>= 1;
694
695
111M
                if (category == 7 || *coefs_ptr == 0)
696
111M
                    *coefs_ptr = dw2 & 1 ? noise : -noise;
697
111M
                coefs_ptr++;
698
111M
                dw2 >>= 1;
699
111M
            }
700
11.1M
        }
701
11.4M
    }
702
703
821k
    return error == 1 ? AVERROR_INVALIDDATA : (get_bits_left(gb) - s->checksum_bits);
704
821k
}
705
706
static int siren_decode(AVCodecContext *avctx, AVFrame *frame,
707
                        int *got_frame, AVPacket *avpkt)
708
1.19M
{
709
1.19M
    SirenContext *s = avctx->priv_data;
710
1.19M
    GetBitContext *gb = &s->gb;
711
1.19M
    int ret, number_of_valid_coefs = REGION_SIZE * s->number_of_regions;
712
1.19M
    int frame_error = 0, rate_control = 0;
713
1.19M
    int bits_per_frame;
714
715
1.19M
    if (s->microsoft) {
716
860k
        bits_per_frame  = avctx->sample_rate / 50;
717
718
860k
        if (avpkt->size < bits_per_frame / 8)
719
181k
            return AVERROR_INVALIDDATA;
720
721
678k
        if ((ret = init_get_bits(gb, avpkt->data, bits_per_frame)) < 0)
722
0
            return ret;
723
678k
    } else
724
336k
        if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
725
0
            return ret;
726
727
1.01M
    skip_bits(gb, s->sample_rate_bits);
728
729
1.01M
    ret = decode_envelope(s, gb, s->number_of_regions,
730
1.01M
                    s->decoder_standard_deviation,
731
1.01M
                    s->absolute_region_power_index, s->esf_adjustment);
732
1.01M
    if (ret < 0)
733
172k
        return ret;
734
735
842k
    rate_control = get_bits(gb, 4);
736
737
842k
    ret = categorize_regions(s->number_of_regions, get_bits_left(gb) - s->checksum_bits,
738
842k
                             s->absolute_region_power_index, s->power_categories,
739
842k
                             s->category_balance);
740
842k
    if (ret < 0)
741
21.1k
        return ret;
742
743
9.75M
    for (int i = 0; i < rate_control; i++)
744
8.93M
        s->power_categories[s->category_balance[i]]++;
745
746
821k
    ret = decode_vector(s, s->number_of_regions,
747
821k
                        s->decoder_standard_deviation, s->power_categories,
748
821k
                        s->imdct_in, s->scale_factor);
749
821k
    if (ret < 0 && !s->microsoft)
750
12.9k
        return ret;
751
752
808k
    if (get_bits_left(gb) - s->checksum_bits > 0) {
753
3.48M
        do {
754
3.48M
            frame_error |= !get_bits1(gb);
755
3.48M
        } while (get_bits_left(gb) - s->checksum_bits > 0);
756
645k
    } else if (get_bits_left(gb) - s->checksum_bits < 0 &&
757
0
               rate_control + 1 < s->rate_control_possibilities) {
758
0
        frame_error = 1;
759
0
    }
760
761
12.1M
    for (int i = 0; i < s->number_of_regions; i++) {
762
11.3M
        if (s->absolute_region_power_index[i] > 33 ||
763
8.69M
            s->absolute_region_power_index[i] < -31)
764
2.62M
            frame_error = 1;
765
11.3M
    }
766
767
808k
    if ((avctx->err_recognition & AV_EF_CRCCHECK) && s->checksum_bits) {
768
543k
        static const uint16_t ChecksumTable[4] = {0x7F80, 0x7878, 0x6666, 0x5555};
769
543k
        int wpf, checksum, sum, calculated_checksum, temp1;
770
771
543k
        checksum = get_bits(gb, s->checksum_bits);
772
773
543k
        wpf = bits_per_frame / 16;
774
543k
        sum = 0;
775
2.02M
        for (int i = 0; i < wpf - 1; i++)
776
1.48M
            sum ^= AV_RB16(avpkt->data + i * 2) << (i % 15);
777
543k
        sum ^= (AV_RB16(avpkt->data + (wpf - 1) * 2) & ~checksum) << ((wpf - 1) % 15);
778
543k
        sum = (sum >> 15) ^ (sum & 0x7FFF);
779
780
543k
        calculated_checksum = 0;
781
2.71M
        for (int i = 0; i < 4; i++) {
782
2.17M
            temp1 = ChecksumTable[i] & sum;
783
784
10.8M
            for (int j = 8; j > 0; j >>= 1)
785
8.69M
                temp1 ^= temp1 >> j;
786
787
2.17M
            calculated_checksum <<= 1;
788
2.17M
            calculated_checksum |= temp1 & 1;
789
2.17M
        }
790
791
543k
        if (checksum != calculated_checksum) {
792
541k
            av_log(avctx, AV_LOG_WARNING, "Invalid checksum\n");
793
541k
            if (avctx->err_recognition & AV_EF_EXPLODE)
794
207
                return AVERROR_INVALIDDATA;
795
541k
            frame_error = 1;
796
541k
        }
797
543k
    }
798
799
808k
    if (frame_error) {
800
672k
        memcpy(s->imdct_in, s->backup_frame, number_of_valid_coefs * sizeof(float));
801
672k
        memset(s->backup_frame, 0, number_of_valid_coefs * sizeof(float));
802
672k
    } else {
803
135k
        memcpy(s->backup_frame, s->imdct_in, number_of_valid_coefs * sizeof(float));
804
135k
    }
805
806
808k
    frame->nb_samples = FRAME_SIZE;
807
808k
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
808
0
        return ret;
809
810
130M
    for (int i = 0; i < FRAME_SIZE; i += 2)
811
129M
        s->imdct_in[i] *= -1;
812
813
808k
    s->tx_fn(s->tx_ctx, s->imdct_out, s->imdct_in, sizeof(float));
814
808k
    s->fdsp->vector_fmul_window((float *)frame->data[0],
815
808k
                                s->imdct_prev + (FRAME_SIZE >> 1),
816
808k
                                s->imdct_out, s->window,
817
808k
                                FRAME_SIZE >> 1);
818
808k
    FFSWAP(float *, s->imdct_out, s->imdct_prev);
819
820
808k
    *got_frame = 1;
821
822
808k
    return s->microsoft ? bits_per_frame / 8 : avpkt->size;
823
808k
}
824
825
static av_cold void siren_flush(AVCodecContext *avctx)
826
135k
{
827
135k
    SirenContext *s = avctx->priv_data;
828
829
135k
    memset(s->backup_frame, 0, sizeof(s->backup_frame));
830
135k
    memset(s->imdct_prev, 0, FRAME_SIZE * sizeof(*s->imdct_prev));
831
135k
    memset(s->imdct_out, 0, FRAME_SIZE * sizeof(*s->imdct_out));
832
135k
}
833
834
static av_cold int siren_close(AVCodecContext *avctx)
835
1.57k
{
836
1.57k
    SirenContext *s = avctx->priv_data;
837
838
1.57k
    av_freep(&s->fdsp);
839
1.57k
    av_tx_uninit(&s->tx_ctx);
840
841
1.57k
    return 0;
842
1.57k
}
843
844
const FFCodec ff_siren_decoder = {
845
    .p.name         = "siren",
846
    CODEC_LONG_NAME("Siren"),
847
    .priv_data_size = sizeof(SirenContext),
848
    .p.type         = AVMEDIA_TYPE_AUDIO,
849
    .p.id           = AV_CODEC_ID_SIREN,
850
    .init           = siren_init,
851
    .close          = siren_close,
852
    FF_CODEC_DECODE_CB(siren_decode),
853
    .flush          = siren_flush,
854
    .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
855
                      AV_CODEC_CAP_DR1,
856
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
857
};
858
859
const FFCodec ff_msnsiren_decoder = {
860
    .p.name         = "msnsiren",
861
    CODEC_LONG_NAME("MSN Siren"),
862
    .priv_data_size = sizeof(SirenContext),
863
    .p.type         = AVMEDIA_TYPE_AUDIO,
864
    .p.id           = AV_CODEC_ID_MSNSIREN,
865
    .init           = siren_init,
866
    .close          = siren_close,
867
    FF_CODEC_DECODE_CB(siren_decode),
868
    .flush          = siren_flush,
869
    .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
870
                      AV_CODEC_CAP_DR1,
871
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
872
};