FFmpeg
siren.c
Go to the documentation of this file.
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 
25 #include "libavutil/tx.h"
26 #include "libavutil/float_dsp.h"
27 #include "libavutil/mem_internal.h"
28 
29 #include "avcodec.h"
30 #include "codec_internal.h"
31 #include "decode.h"
32 #include "get_bits.h"
33 
34 static const uint8_t index_table[8] = {4, 4, 3, 3, 2, 2, 1, 0};
35 static const uint8_t vector_dimension[8] = { 2, 2, 2, 4, 4, 5, 5, 1 };
36 static const uint8_t number_of_vectors[8] = { 10, 10, 10, 5, 5, 4, 4, 20 };
37 static const uint8_t expected_bits_table[8] = { 52, 47, 43, 37, 29, 22, 16, 0 };
38 static const int8_t differential_decoder_tree[27][24][2] = {
39  {
40  {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, -12}, {-11, -10}, {-8, -9}, {-7, -6}, {-13, 12},
41  {-5, -4}, {0, 13}, {-3, -14}, {-2, 14}, {-1, 15}, {-15, 16}, {-16, 17}, {-17, 18}, {19, 20},
42  {21, 22}, {-18, -19}, {-20, -21}, {-22, -23}, {-32, -32}
43  },
44  {
45  {1, 2}, {3, 4}, {5, 6}, {7, 8}, {-10, -9}, {-8, -11}, {-7, -6}, {9, -5}, {10, -12}, {-4, 11},
46  {-13, -3}, {12, -2}, {13, -14}, {-1, 14}, {15, -15}, {0, 16}, {-16, 17}, {-17, 18}, {-18, 19},
47  {20, 21},{22, -19}, {-20, -21}, {-22, -23}, {-32, -32}
48  },
49  {
50  {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {-12, 11}, {-11, -13}, {-10, -9}, {12, -14}, {-8, -7},
51  {-15, -6}, {13, -5}, {-16, -4}, {14, -17}, {15, -3}, {16, -18}, {-2, 17}, {18, -19}, {-1, 19},
52  {-20, 20}, {0, 21}, {22, -21}, {-22, -23}, {-32, -32}
53  },
54  {
55  {1, 2}, {3, 4}, {5, 6}, {-11, -10}, {7, -12}, {8, -9}, {9, -13}, {-14, 10}, {-8, -15}, {-16, 11},
56  {-7, 12}, {-17, -6}, {13, 14}, {-18, 15}, {-5, -4}, {16, 17}, {-3, -2}, {-19, 18}, {-1, 19},
57  {-20, 20}, {21, 22}, {0, -21}, {-22, -23}, {-32, -32}
58  },
59  {
60  {1, 2}, {3, 4}, {5, 6}, {-12, -11}, {-13, 7}, {8, -14}, {-10, 9}, {10, -15}, {-9, 11}, {-8, 12},
61  {-16, 13}, {-7, -6}, {-17, 14}, {-5, -18}, {15, -4}, {16, -19}, {17, -3}, {-20, 18}, {-2, 19},
62  {-21, 20}, {0, 21}, {22, -1}, {-22, -23}, {-32, -32}
63  },
64  {
65  {1, 2}, {3, 4}, {5, 6}, {-11, 7}, {-12, -10}, {-13, -9}, {8, 9}, {-14, -8}, {10, -15}, {-7, 11},
66  {-16, 12}, {-6, -17}, {13, 14}, {-5, 15}, {-18, 16}, {-4, 17}, {-3, -19}, {18, -2}, {-20, 19},
67  {-1, 20}, {0, 21}, {22, -21}, {-22, -23}, {-32, -32}
68  },
69  {
70  {1, 2}, {3, 4}, {5, -12}, {6, -11}, {-10, -13}, {-9, 7}, {8, -14}, {9, -8}, {-15, 10}, {-7, -16},
71  {11, -6}, {12, -17}, {13, -5}, {-18, 14}, {15, -4}, {-19, 16}, {17, -3}, {-20, 18}, {19, 20},
72  {21, 22}, {0, -2}, {-1, -21}, {-22, -23}, {-32, -32}
73  },
74  {
75  {1, 2}, {3, 4}, {5, -12}, {6, -13}, {-11, -10}, {7, -14}, {8, -9}, {9, -15}, {-8, 10}, {-7, -16},
76  {11, 12}, {-6, -17}, {-5, 13}, {14, 15}, {-18, -4}, {-19, 16}, {-3, 17}, {18, -2}, {-20, 19},
77  {20, 21}, {22, 0}, {-1, -21}, {-22, -23}, {-32, -32}
78  },
79  {
80  {1, 2}, {3, 4}, {5, 6}, {-11, -10}, {-12, -9}, {7, 8}, {-13, -8}, {9, -14}, {-7, 10}, {-6, -15},
81  {11, 12}, {-5, -16}, {13, 14}, {-17, 15}, {-4, 16}, {17, -18}, {18, -3}, {-2, 19}, {-1, 0},
82  {-19, 20}, {-20, 21}, {22, -21}, {-22, -23}, {-32, -32}
83  },
84  {
85  {1, 2}, {3, 4}, {5, 6}, {-11, 7}, {-10, -12}, {-9, 8}, {-8, -13}, {9, -7}, {10, -14}, {-6, 11},
86  {-15, 12}, {-5, 13}, {-16, -4}, {14, 15}, {-17, -3}, {-18, 16}, {17, -19}, {-2, 18}, {-20, 19},
87  {-1, 20}, {21, 22}, {0, -21}, {-22, -23}, {-32, -32}
88  },
89  {
90  {1, 2}, {3, 4}, {5, -12}, {6, -11}, {7, 8}, {-10, -13}, {-9, 9}, {-8, -14}, {10, -7}, {11, -15},
91  {-6, 12}, {-5, 13}, {-4, -16}, {14, 15}, {-3, -17}, {16, 17}, {-18, -2}, {18, -19}, {-1, 19},
92  {-20, 20}, {-21, 21}, {22, 0}, {-22, -23}, {-32, -32}
93  },
94  {
95  {1, 2}, {3, 4}, {5, -12}, {-13, 6}, {-11, 7}, {-14, 8}, {-10, 9}, {-15, -9}, {-8, 10}, {-7, -16},
96  {11, -6}, {12, -5}, {-17, 13}, {14, -18}, {15, -4}, {16, -19}, {17, -3}, {18, -2}, {19, -1},
97  {-20, 20}, {21, 22}, {0, -21}, {-22, -23}, {-32, -32}
98  },
99  {
100  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
101  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
102  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
103  },
104  {
105  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
106  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
107  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
108  },
109  {
110  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
111  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
112  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
113  },
114  {
115  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
116  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
117  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
118  },
119  {
120  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
121  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
122  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
123  },
124  {
125  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
126  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
127  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
128  },
129  {
130  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
131  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
132  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
133  },
134  {
135  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
136  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
137  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
138  },
139  {
140  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
141  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
142  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
143  },
144  {
145  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
146  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
147  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
148  },
149  {
150  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
151  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
152  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
153  },
154  {
155  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
156  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
157  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
158  },
159  {
160  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
161  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
162  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
163  },
164  {
165  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
166  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
167  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
168  },
169  {
170  {1, 2}, {3, 4}, {-12, 5}, {-11, -13}, {6, -14}, {-10, 7}, {8, -15}, {-9, 9}, {-16, 10}, {-8, -17},
171  {11, 12}, {-7, -18}, {-6, 13}, {14, -5}, {15, -19}, {-4, 16}, {-20, 17}, {18, 19}, {20, 21},
172  {22, 0}, {-1, -3}, {-2, -21}, {-22, -23}, {-32, -32}
173  },
174 };
175 
176 static const uint16_t decoder_tree0[360] = {
177  2, 1, 4, 6, 8, 10, 12, 14, 16, 18, 33, 3, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 35, 40,
178  42, 44, 46, 5, 48, 65, 50, 52, 54, 56, 58, 60, 62, 64, 37, 66, 67, 68, 97, 70, 72, 74, 7,
179  76, 78, 80, 82, 84, 86, 88, 99, 90, 39, 92, 94, 96, 129, 98, 9, 100, 102, 104, 106, 108,
180  110, 112, 41, 161, 69, 114, 116, 118, 131, 120, 122, 11, 124, 126, 128, 193, 130, 132, 71,
181  134, 43, 136, 138, 140, 163, 101, 13, 142, 144, 146, 148, 150, 152, 154, 225, 156, 158, 195,
182  160, 162, 45, 164, 15, 166, 73, 168, 170, 133, 47, 172, 257, 174, 176, 178, 75, 103, 180, 165,
183  182, 17, 227, 184, 105, 49, 135, 186, 289, 188, 259, 190, 192, 194, 196, 198, 291, 77, 200,
184  202, 197, 107, 204, 19, 51, 229, 206, 167, 208, 210, 212, 214, 21, 79, 81, 109, 216, 218, 220,
185  222, 53, 137, 224, 199, 226, 323, 321, 169, 228, 111, 230, 232, 139, 261, 234, 83, 236, 201,
186  238, 240, 293, 242, 353, 231, 141, 244, 246, 113, 23, 355, 85, 248, 55, 115, 250, 263, 252,
187  254, 203, 171, 256, 258, 233, 235, 143, 357, 325, 260, 295, 262, 173, 145, 177, 87, 264, 327,
188  267, 266, 268, 175, 270, 272, 117, 297, 274, 265, 147, 179, 205, 276, 207, 237, 269, 278, 57,
189  59, 387, 209, 280, 282, 149, 329, 385, 284, 25, 286, 239, 119, 288, 27, 290, 292, 299, 294, 359,
190  89, 296, 298, 419, 181, 300, 331, 271, 417, 211, 361, 151, 389, 241, 302, 304, 303, 306, 308,
191  421, 91, 310, 312, 391, 314, 121, 316, 333, 318, 275, 213, 301, 243, 183, 335, 320, 363, 322,
192  215, 324, 393, 273, 337, 153, 326, 423, 365, 328, 367, 247, 395, 185, 123, 330, 425, 245, 155,
193  332, 334, 305, 397, 336, 277, 217, 338, 340, 339, 427, 342, 344, 346, 307, 399, 187, 348, 309,
194  341, 350, 369, 279, 311, 429, 249, 219, 352, 354, 356, 358, 431, 373, 401, 371, 313, 281, 433,
195  343, 403, 251, 283
196 };
197 
198 static const uint16_t decoder_tree1[188] = {
199  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,
200  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,
201  78, 80, 129, 41, 131, 82, 9, 71, 84, 86, 101, 88, 90, 92, 94, 96, 161, 43, 11, 73, 98, 103, 100,
202  163, 102, 104, 106, 108, 133, 110, 105, 112, 75, 114, 45, 13, 116, 165, 118, 195, 135, 193, 120, 77,
203  122, 47, 124, 167, 225, 126, 79, 107, 227, 128, 137, 197, 15, 130, 169, 199, 132, 109, 134, 17, 139,
204  49, 136, 229, 138, 140, 81, 259, 142, 144, 171, 146, 141, 148, 111, 150, 201, 231, 152, 51, 257, 289,
205  154, 19, 113, 156, 261, 158, 203, 173, 263, 143, 160, 291, 235, 83, 162, 233, 265, 164, 205, 166, 293,
206  145, 168, 175, 177, 237, 115, 295, 170, 207, 172, 267, 174, 176, 297, 147, 178, 180, 269, 182, 271,
207  209, 299, 239, 179, 184, 301, 241, 211, 0, 0
208 };
209 
210 static const uint16_t decoder_tree2[96] = {
211  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,
212  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,
213  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,
214  75, 89, 61, 86, 103, 88, 77, 90, 105, 91, 92, 107, 93, 0, 0
215 };
216 
217 static const uint16_t decoder_tree3[1040] = {
218  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,
219  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,
220  84, 86, 131, 88, 1155, 1043, 1041, 90, 92, 5, 94, 96, 98, 100, 102, 104, 21, 106, 108, 2049, 2177, 110, 112, 114,
221  116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 33, 144, 163, 146, 148, 150, 152, 154, 161,
222  156, 35, 158, 1297, 160, 162, 273, 257, 164, 166, 149, 168, 1281, 170, 172, 2193, 174, 176, 178, 1299, 180, 1045,
223  182, 184, 1173, 186, 3201, 188, 190, 192, 194, 2195, 1187, 23, 2179, 196, 7, 198, 275, 200, 2051, 202, 2065, 204,
224  206, 1029, 1185, 208, 210, 1157, 37, 3073, 2067, 133, 212, 214, 2321, 216, 165, 218, 1059, 220, 1283, 222, 2305,
225  224, 226, 228, 230, 259, 232, 234, 2323, 236, 1409, 1057, 1315, 238, 240, 242, 244, 246, 1425, 248, 1313, 250, 252,
226  254, 256, 258, 260, 289, 262, 264, 1189, 266, 268, 179, 151, 270, 272, 274, 276, 278, 291, 280, 282, 9, 385, 284,
227  286, 177, 49, 401, 1061, 288, 290, 292, 51, 294, 296, 298, 300, 302, 304, 25, 306, 2083, 39, 308, 310, 3329, 167,
228  312, 314, 1175, 316, 318, 1203, 135, 320, 322, 324, 326, 328, 2211, 2307, 330, 1301, 332, 334, 1047, 336, 338, 2449,
229  3217, 340, 1427, 2209, 53, 342, 2339, 3345, 344, 346, 348, 403, 181, 4097, 2197, 350, 2181, 1285, 1317, 1031, 352,
230  354, 356, 3089, 358, 360, 4225, 277, 362, 364, 366, 368, 2069, 370, 3203, 293, 1201, 305, 372, 3219, 307, 2433, 374,
231  376, 378, 380, 2081, 1411, 382, 384, 3075, 1443, 513, 386, 387, 388, 390, 1331, 261, 392, 394, 396, 398, 400, 1441,
232  1075, 67, 1159, 402, 404, 406, 408, 410, 412, 414, 3347, 2325, 416, 65, 418, 420, 422, 424, 426, 2053, 193, 1073, 428,
233  430, 432, 1537, 1329, 2337, 2213, 434, 417, 183, 41, 436, 438, 440, 442, 444, 446, 448, 450, 195, 2435, 452, 2085, 1063,
234  1191, 454, 456, 458, 460, 419, 2071, 1553, 3091, 55, 137, 462, 464, 466, 468, 470, 472, 474, 476, 478, 2309, 4113, 480,
235  482, 484, 486, 2451, 2465, 1205, 153, 488, 490, 492, 494, 496, 498, 500, 502, 504, 506, 508, 510, 512, 514, 516, 518,
236  520, 522, 524, 1333, 526, 1555, 2467, 2227, 3205, 3331, 528, 530, 532, 534, 536, 538, 540, 542, 544, 546, 548, 529, 309,
237  1303, 3473, 3457, 389, 1569, 1445, 1077, 69, 2199, 1539, 4353, 550, 552, 554, 556, 558, 560, 562, 1459, 4241, 3221, 1429,
238  2341, 279, 3475, 169, 564, 545, 3105, 323, 2353, 2097, 3235, 421, 2229, 3107, 3233, 566, 568, 570, 572, 574, 576, 578,
239  580, 582, 584, 586, 588, 590, 592, 594, 596, 2099, 1091, 531, 2437, 4227, 405, 197, 263, 1287, 2577, 1049, 1571, 598, 600,
240  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,
241  1345, 1219, 3077, 1457, 2225, 2579, 515, 2561, 2469, 433, 1221, 2183, 4243, 652, 654, 656, 658, 660, 662, 664, 666, 668,
242  670, 1217, 3333, 3093, 435, 321, 4369, 1089, 2055, 4099, 3361, 1319, 547, 1161, 1177, 672, 2355, 4115, 1413, 4257, 3349,
243  2453, 3109, 2357, 2215, 3363, 1079, 1207, 311, 1033, 1347, 1065, 674, 676, 678, 680, 682, 684, 686, 688, 690, 692, 694, 696,
244  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,
245  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,
246  798, 800, 802, 804, 806, 808, 810, 812, 814, 2593, 2565, 4261, 3253, 437, 325, 3489, 2311, 4259, 1431, 2087, 2563, 295, 2343,
247  449, 199, 265, 2201, 4371, 1193, 816, 533, 1557, 2581, 2241, 3365, 3491, 3603, 549, 2101, 1461, 1093, 2117, 3459, 3079, 4481,
248  3095, 2327, 3461, 4129, 3249, 1447, 2471, 2231, 71, 4497, 2609, 1289, 393, 3251, 2073, 3097, 2371, 1305, 2089, 818, 820, 822,
249  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,
250  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,
251  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,
252  974, 976, 978, 980, 982, 984, 986, 988, 990, 992, 994, 996, 998, 1000, 1002, 1004, 1006, 1008, 1010, 1012, 1014, 1016, 1018,
253  1020, 1022, 1024, 1026, 1028, 1030, 1032, 1034, 1036, 4161, 4273, 3507, 3493, 4517, 2497, 1573, 2597, 3621, 4531, 4627, 3523,
254  3125, 4149, 4529, 3139, 4515, 451, 4277, 2113, 4163, 4499, 3381, 4405, 1473, 4373, 2485, 3509, 565, 1589, 2613, 3585, 3123,
255  4403, 3141, 4147, 563, 2245, 3269, 4357, 1349, 2373, 3397, 453, 1477, 2501, 2481, 579, 1601, 3477, 4103, 3265, 2243, 1587,
256  3207, 4231, 3267, 4501, 1475, 3335, 4359, 391, 1415, 2439, 3463, 4487, 519, 1543, 2567, 3591, 4609, 4289, 4611, 2499, 4119,
257  4385, 4145, 4401, 3223, 4247, 3379, 577, 3393, 3351, 4375, 407, 1585, 2455, 3479, 4503, 535, 1559, 2583, 3607, 3605, 4513,
258  4485, 3111, 4135, 3121, 517, 3377, 3239, 4263, 1541, 4291, 4229, 3367, 4391, 423, 2115, 4131, 3495, 551, 1575, 2599, 3635, 3395,
259  2103, 3127, 4151, 3589, 4101, 1603, 3255, 4279, 3601, 1335, 2359, 3383, 439, 1463, 2487, 3511, 567, 1591, 4133, 1095, 2119, 3143,
260  2369, 1223, 2247, 3271, 327, 1351, 2375, 455, 1479, 3137, 3521, 2057, 3081, 4105, 4387, 3505, 2185, 3209, 4233, 3587, 4355, 2313,
261  3337, 3237, 1417, 2441, 3465, 521, 1545, 3617, 3633, 561, 4625, 4121, 2611, 2483, 2595, 3225, 4249, 281, 4245, 2329, 3353, 409,
262  1433, 2457, 3481, 537, 1561, 4483, 3619, 4389, 3113, 4275, 4117, 2217, 3241, 297, 1321, 2345, 3369, 425, 1449, 2473, 57, 1081,
263  2105, 3129, 185, 1209, 2233, 3257, 313, 1337, 2361, 441, 1465, 73, 1097, 201, 1225, 0, 0
264 };
265 
266 static const uint16_t decoder_tree4[416] = {
267  2, 4, 6, 1, 8, 10, 12, 14, 16, 18, 20, 22, 24, 3, 129, 26, 28, 9, 33, 30, 32,
268  34, 36, 11, 161, 38, 40, 42, 41, 44, 46, 131, 43, 169, 35, 48, 137, 50, 52, 54, 56, 139,
269  163, 171, 58, 60, 62, 64, 5, 66, 68, 70, 257, 72, 74, 76, 13, 78, 80, 289, 82, 84, 17,
270  86, 88, 65, 90, 201, 19, 92, 94, 51, 193, 96, 98, 49, 100, 73, 102, 104, 106, 45, 108, 110,
271  297, 112, 114, 116, 37, 203, 118, 120, 179, 122, 177, 124, 265, 126, 75, 133, 259, 291, 147, 128, 67,
272  195, 130, 141, 173, 299, 132, 145, 134, 165, 136, 138, 140, 142, 7, 144, 146, 21, 267, 148, 53, 150,
273  321, 152, 154, 15, 156, 81, 158, 160, 385, 162, 417, 164, 166, 168, 83, 170, 172, 329, 174, 211, 176,
274  27, 178, 180, 182, 209, 184, 186, 188, 190, 25, 192, 331, 194, 196, 105, 57, 198, 97, 200, 202, 323,
275  225, 59, 149, 204, 206, 233, 307, 208, 77, 181, 210, 212, 214, 216, 218, 220, 222, 47, 224, 226, 69,
276  228, 230, 197, 232, 425, 393, 205, 275, 293, 39, 234, 236, 238, 305, 135, 155, 301, 143, 240, 242, 235,
277  395, 244, 246, 248, 250, 252, 254, 256, 258, 260, 262, 273, 269, 185, 264, 266, 268, 270, 272, 274, 276,
278  261, 153, 278, 280, 282, 187, 337, 387, 107, 284, 427, 227, 167, 419, 286, 288, 290, 292, 294, 296, 298,
279  300, 302, 304, 306, 308, 310, 312, 314, 316, 318, 320, 322, 324, 326, 328, 330, 332, 334, 336, 338, 115,
280  99, 85, 213, 29, 113, 23, 89, 241, 61, 449, 339, 175, 340, 342, 344, 346, 348, 350, 352, 354, 356,
281  358, 360, 362, 364, 366, 368, 370, 372, 374, 376, 378, 380, 382, 384, 386, 388, 390, 392, 394, 396, 398,
282  400, 402, 404, 406, 408, 410, 412, 414, 389, 361, 457, 465, 429, 451, 333, 109, 277, 243, 263, 295, 199,
283  283, 151, 55, 183, 229, 357, 363, 123, 491, 397, 411, 251, 313, 441, 467, 345, 433, 461, 219, 237, 365,
284  435, 353, 347, 405, 409, 217, 309, 437, 369, 371, 341, 117, 245, 249, 157, 285, 403, 189, 317, 93, 221,
285  315, 401, 481, 391, 489, 121, 421, 423, 71, 483, 327, 103, 231, 443, 459, 271, 399, 355, 91, 303, 431,
286  79, 207, 335, 111, 239, 281, 325, 279, 453, 101, 311, 87, 215, 31, 159, 63, 191
287 };
288 
289 static const uint16_t decoder_tree5[384] = {
290  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,
291  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,
292  76, 78, 80, 681, 171, 82, 84, 555, 86, 675, 88, 651, 5, 90, 92, 1025, 94, 96, 98, 683, 13,
293  100, 17, 102, 104, 106, 65, 108, 110, 257, 112, 114, 1153, 19, 116, 118, 120, 122, 124, 49, 126, 128,
294  769, 289, 130, 132, 134, 73, 136, 138, 140, 142, 193, 144, 146, 148, 150, 152, 154, 517, 156, 158, 37,
295  51, 160, 201, 162, 145, 164, 166, 168, 133, 170, 801, 45, 172, 174, 1057, 176, 178, 67, 180, 1027, 577,
296  182, 184, 186, 188, 190, 192, 194, 196, 198, 259, 200, 202, 204, 525, 177, 265, 141, 206, 208, 210, 212,
297  195, 297, 214, 75, 216, 1033, 203, 585, 1155, 1185, 267, 1161, 549, 218, 220, 657, 777, 147, 222, 224, 226,
298  228, 230, 232, 234, 236, 238, 240, 587, 645, 165, 242, 244, 246, 248, 250, 771, 291, 252, 579, 1065, 1035,
299  705, 531, 529, 659, 173, 254, 561, 653, 256, 713, 677, 557, 258, 260, 262, 264, 266, 268, 270, 272, 274,
300  276, 278, 280, 282, 284, 286, 288, 290, 292, 294, 296, 298, 300, 707, 1059, 809, 715, 563, 179, 691, 1193,
301  21, 779, 1067, 299, 1187, 302, 304, 306, 308, 310, 312, 314, 316, 318, 320, 322, 324, 326, 328, 330, 332,
302  334, 336, 338, 340, 342, 344, 346, 348, 350, 352, 354, 356, 358, 360, 362, 364, 366, 368, 370, 372, 374,
303  376, 378, 380, 83, 69, 1281, 803, 321, 1195, 1163, 811, 1323, 689, 1321, 1099, 305, 835, 1227, 331, 843, 785,
304  593, 1043, 1291, 1283, 1171, 275, 787, 1217, 833, 1075, 1313, 1219, 1203, 307, 819, 841, 595, 211, 723, 721, 817,
305  1029, 329, 81, 1157, 261, 773, 1097, 1089, 1061, 1169, 1091, 1189, 293, 805, 1201, 581, 197, 709, 1289, 273, 1037,
306  1315, 1041, 1165, 269, 781, 209, 1073, 1069, 323, 685, 1197, 301, 813, 77, 589, 205, 717, 1225, 533, 149, 661,
307  53, 565, 181, 693, 0, 0
308 };
309 
310 static const uint16_t decoder_tree6[62] = {
311  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,
312  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,
313  61, 47, 59, 63
314 };
315 
316 static const uint16_t *const decoder_tables[7] = {
324 };
325 
326 static const int decoder_tables_elements[7] = {
334 };
335 
336 static const float mlt_quant[7][14] = {
337  { 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 },
338  { 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 },
339  { 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 },
340  { 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 },
341  { 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 },
342  { 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 },
343  { 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 }
344 };
345 
346 static const float noise_category5[21] = {
347  0.70711f, 0.6179f, 0.5005f, 0.3220f, 0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f,
348  0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f, 0.17678f
349 };
350 
351 static const float noise_category6[21] = {
352  0.70711f, 0.5686f, 0.3563f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f,
353  0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f
354 };
355 
356 #define FRAME_SIZE 320
357 #define REGION_SIZE 20
358 
359 typedef struct SirenContext {
361 
369 
370  unsigned dw1, dw2, dw3, dw4;
371 
378 
382 
384  float *window;
385  float *imdct_in;
386  float *imdct_out;
387  float *imdct_prev;
388 } SirenContext;
389 
391 {
392  const float scale = 1.0f / (22.f * 32768.f);
393  SirenContext *s = avctx->priv_data;
394  int i;
395 
396  s->imdct_in = s->imdct_buf[0];
397  s->imdct_out = s->imdct_buf[1];
398  s->imdct_prev = s->imdct_buf[2];
399  s->window = s->imdct_buf[3];
400 
403  avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
404 
405  s->rate_control_possibilities = 16;
406  s->esf_adjustment = 7;
407  s->number_of_regions = 14;
408  s->scale_factor = 22;
409  s->dw1 = s->dw2 = s->dw3 = s->dw4 = 1;
410 
411  for (i = 0; i < 64; i++) {
412  float region_power = powf(10, (i - 24) * 0.3010299957);
413 
414  s->standard_deviation[i] = sqrtf(region_power);
415  }
416 
417  for (i = 0; i < FRAME_SIZE; i++) {
418  float angle = ((i + 0.5f) * M_PI_2) / 320.f;
419  s->window[i] = sinf(angle);
420  }
421 
423  if (!s->fdsp)
424  return AVERROR(ENOMEM);
425 
426  s->microsoft = avctx->codec->id == AV_CODEC_ID_MSNSIREN;
427  if (s->microsoft) {
428  s->esf_adjustment = -2;
429  s->number_of_regions = 14;
430  s->scale_factor = 1;
431  s->sample_rate_bits = 2;
432  s->checksum_bits = 4;
433  }
434 
435  return av_tx_init(&s->tx_ctx, &s->tx_fn, AV_TX_FLOAT_MDCT, 1, FRAME_SIZE, &scale, 0);
436 }
437 
439  int number_of_regions, float *decoder_standard_deviation,
440  int *absolute_region_power_index, int esf_adjustment)
441 {
442  absolute_region_power_index[0] = (int)get_bits(gb, 5) - esf_adjustment;
443  absolute_region_power_index[0] = av_clip(absolute_region_power_index[0], -24, 39);
444  decoder_standard_deviation[0] = s->standard_deviation[absolute_region_power_index[0] + 24];
445 
446  for (int i = 1; i < number_of_regions; i++) {
447  int index = 0;
448 
449  do {
450  if (get_bits_left(gb) < 4 + number_of_regions - i + s->checksum_bits)
451  return AVERROR_INVALIDDATA;
453  } while (index > 0);
454 
455  absolute_region_power_index[i] = av_clip(absolute_region_power_index[i - 1] - index - 12, -24, 39);
456  decoder_standard_deviation[i] = s->standard_deviation[absolute_region_power_index[i] + 24];
457  }
458 
459  return get_bits_count(gb);
460 }
461 
462 static int categorize_regions(int number_of_regions, int number_of_available_bits,
463  int *absolute_region_power_index, int *power_categories,
464  int *category_balance)
465 {
466  int region, delta, i, temp;
467  int expected_number_of_code_bits;
468  int min, max;
469  int offset, num_rate_control_possibilities = 16,
470  raw_value, raw_max_idx = 0, raw_min_idx = 0;
471  int max_rate_categories[28];
472  int min_rate_categories[28];
473  int temp_category_balances[64];
474  int *min_rate_ptr = NULL;
475  int *max_rate_ptr = NULL;
476 
477  offset = -32;
478  for (delta = 32; number_of_regions > 0 && delta > 0; delta /= 2) {
479  expected_number_of_code_bits = 0;
480  for (region = 0; region < number_of_regions; region++) {
481  i = (delta + offset -
482  absolute_region_power_index[region]) >> 1;
483  i = av_clip_uintp2(i, 3);
484  power_categories[region] = i;
485  expected_number_of_code_bits += expected_bits_table[i];
486 
487  }
488  if (expected_number_of_code_bits >= number_of_available_bits - 32)
489  offset += delta;
490  }
491 
492  expected_number_of_code_bits = 0;
493  for (region = 0; region < number_of_regions; region++) {
494  i = (offset - absolute_region_power_index[region]) >> 1;
495  i = av_clip_uintp2(i, 3);
496  max_rate_categories[region] = min_rate_categories[region] =
497  power_categories[region] = i;
498  expected_number_of_code_bits += expected_bits_table[i];
499  }
500 
501  min = max = expected_number_of_code_bits;
502  min_rate_ptr = max_rate_ptr =
503  temp_category_balances + num_rate_control_possibilities;
504  for (i = 0; i < num_rate_control_possibilities - 1; i++) {
505  if (min + max > number_of_available_bits * 2) {
506  raw_value = -99;
507  for (region = number_of_regions - 1; region >= 0; region--) {
508  if (min_rate_categories[region] < 7) {
509  temp =
510  offset - absolute_region_power_index[region] -
511  2 * min_rate_categories[region];
512  if (temp > raw_value) {
513  raw_value = temp;
514  raw_min_idx = region;
515  }
516  }
517  }
518  if (raw_value == -99)
519  return AVERROR_INVALIDDATA;
520  *min_rate_ptr++ = raw_min_idx;
521  min +=
522  expected_bits_table[min_rate_categories[raw_min_idx] + 1] -
523  expected_bits_table[min_rate_categories[raw_min_idx]];
524  min_rate_categories[raw_min_idx]++;
525  } else {
526  raw_value = 99;
527  for (region = 0; region < number_of_regions; region++) {
528  if (max_rate_categories[region] > 0) {
529  temp =
530  offset - absolute_region_power_index[region] -
531  2 * max_rate_categories[region];
532  if (temp < raw_value) {
533  raw_value = temp;
534  raw_max_idx = region;
535  }
536  }
537  }
538  if (raw_value == 99)
539  return AVERROR_INVALIDDATA;
540 
541  *--max_rate_ptr = raw_max_idx;
542  max += expected_bits_table[max_rate_categories[raw_max_idx] - 1] -
543  expected_bits_table[max_rate_categories[raw_max_idx]];
544  max_rate_categories[raw_max_idx]--;
545  }
546  }
547 
548  for (region = 0; region < number_of_regions; region++)
549  power_categories[region] = max_rate_categories[region];
550 
551  for (i = 0; i < num_rate_control_possibilities - 1; i++)
552  category_balance[i] = *max_rate_ptr++;
553 
554  return 0;
555 }
556 
557 static int get_dw(SirenContext *s)
558 {
559  int ret = s->dw1 + s->dw4;
560 
561  if ((ret & 0x8000) != 0)
562  ret++;
563 
564  s->dw1 = s->dw2;
565  s->dw2 = s->dw3;
566  s->dw3 = s->dw4;
567  s->dw4 = ret;
568 
569  return ret;
570 }
571 
572 static int decode_vector(SirenContext *s, int number_of_regions,
573  float *decoder_standard_deviation,
574  int *power_categories, float *coefs, int scale_factor)
575 {
576  GetBitContext *gb = &s->gb;
577  float *coefs_ptr;
578  float decoded_value;
579  float noise;
580  const uint16_t *decoder_tree;
581  int region;
582  int category;
583  int i, j;
584  int index;
585  int error = 0;
586  int dw1;
587  int dw2;
588 
589  for (region = 0; region < number_of_regions; region++) {
590  category = power_categories[region];
591  coefs_ptr = coefs + (region * REGION_SIZE);
592 
593  if (category >= 0 && category < 7) {
594  decoder_tree = decoder_tables[category];
595 
596  for (i = 0; i < number_of_vectors[category]; i++) {
597  index = 0;
598  do {
599  if (get_bits_left(gb) - s->checksum_bits <= 0) {
600  error = 1;
601  break;
602  }
603 
605  error = 1;
606  break;
607  }
608  index = decoder_tree[index + get_bits1(gb)];
609  } while ((index & 1) == 0);
610 
611  index >>= 1;
612 
613  if (error == 0) {
614  for (j = 0; j < vector_dimension[category]; j++) {
615  decoded_value = mlt_quant[category][index & ((1 << index_table[category]) - 1)];
617 
618  if (decoded_value) {
619  if (get_bits_left(gb) - s->checksum_bits <= 0) {
620  error = 1;
621  break;
622  }
623  if (!get_bits1(gb))
624  decoded_value *= -decoder_standard_deviation[region];
625  else
626  decoded_value *= decoder_standard_deviation[region];
627  }
628 
629  *coefs_ptr++ = decoded_value * scale_factor;
630  }
631  } else {
632  error = 1;
633  break;
634  }
635  }
636 
637  if (error == 1) {
638  for (j = region + 1; j < number_of_regions; j++)
639  power_categories[j] = 7;
640  category = 7;
641  }
642  }
643 
644  coefs_ptr = coefs + (region * REGION_SIZE);
645 
646  if (category == 5 && s->microsoft) {
647  i = 0;
648  for (j = 0; j < REGION_SIZE; j++) {
649  if (*coefs_ptr != 0) {
650  i++;
651  if (fabs(*coefs_ptr) > 2.0 * decoder_standard_deviation[region]) {
652  i += 3;
653  }
654  }
655  coefs_ptr++;
656  }
657  if (i >= FF_ARRAY_ELEMS(noise_category5)) {
658  error = 1;
659  break;
660  }
661 
662  noise = decoder_standard_deviation[region] * noise_category5[i];
663  } else
664  if (category == 5 || category == 6) {
665  i = 0;
666  for (j = 0; j < REGION_SIZE; j++) {
667  if (*coefs_ptr != 0)
668  i++;
669  coefs_ptr++;
670  }
671 
672  if (category == 5) {
673  noise = decoder_standard_deviation[region] * noise_category5[i];
674  } else
675  noise = decoder_standard_deviation[region] * noise_category6[i];
676  } else if (category == 7) {
677  noise = decoder_standard_deviation[region] * 0.70711f;
678  } else {
679  noise = 0;
680  }
681 
682  coefs_ptr = coefs + (region * REGION_SIZE);
683 
684  if (category == 5 || category == 6 || category == 7) {
685  dw1 = get_dw(s);
686  dw2 = get_dw(s);
687 
688  for (j = 0; j < 10; j++) {
689  if (category == 7 || *coefs_ptr == 0)
690  *coefs_ptr = dw1 & 1 ? noise : -noise;
691  coefs_ptr++;
692  dw1 >>= 1;
693 
694  if (category == 7 || *coefs_ptr == 0)
695  *coefs_ptr = dw2 & 1 ? noise : -noise;
696  coefs_ptr++;
697  dw2 >>= 1;
698  }
699  }
700  }
701 
702  return error == 1 ? AVERROR_INVALIDDATA : (get_bits_left(gb) - s->checksum_bits);
703 }
704 
706  int *got_frame, AVPacket *avpkt)
707 {
708  SirenContext *s = avctx->priv_data;
709  GetBitContext *gb = &s->gb;
710  int ret, number_of_valid_coefs = REGION_SIZE * s->number_of_regions;
711  int frame_error = 0, rate_control = 0;
712  int bits_per_frame;
713 
714  if (s->microsoft) {
715  bits_per_frame = avctx->sample_rate / 50;
716 
717  if (avpkt->size < bits_per_frame / 8)
718  return AVERROR_INVALIDDATA;
719 
720  if ((ret = init_get_bits(gb, avpkt->data, bits_per_frame)) < 0)
721  return ret;
722  } else
723  if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
724  return ret;
725 
726  skip_bits(gb, s->sample_rate_bits);
727 
728  ret = decode_envelope(s, gb, s->number_of_regions,
729  s->decoder_standard_deviation,
730  s->absolute_region_power_index, s->esf_adjustment);
731  if (ret < 0)
732  return ret;
733 
734  rate_control = get_bits(gb, 4);
735 
736  ret = categorize_regions(s->number_of_regions, get_bits_left(gb) - s->checksum_bits,
737  s->absolute_region_power_index, s->power_categories,
738  s->category_balance);
739  if (ret < 0)
740  return ret;
741 
742  for (int i = 0; i < rate_control; i++)
743  s->power_categories[s->category_balance[i]]++;
744 
745  ret = decode_vector(s, s->number_of_regions,
746  s->decoder_standard_deviation, s->power_categories,
747  s->imdct_in, s->scale_factor);
748  if (ret < 0 && !s->microsoft)
749  return ret;
750 
751  if (get_bits_left(gb) - s->checksum_bits > 0) {
752  do {
753  frame_error |= !get_bits1(gb);
754  } while (get_bits_left(gb) - s->checksum_bits > 0);
755  } else if (get_bits_left(gb) - s->checksum_bits < 0 &&
756  rate_control + 1 < s->rate_control_possibilities) {
757  frame_error = 1;
758  }
759 
760  for (int i = 0; i < s->number_of_regions; i++) {
761  if (s->absolute_region_power_index[i] > 33 ||
762  s->absolute_region_power_index[i] < -31)
763  frame_error = 1;
764  }
765 
766  if ((avctx->err_recognition & AV_EF_CRCCHECK) && s->checksum_bits) {
767  static const uint16_t ChecksumTable[4] = {0x7F80, 0x7878, 0x6666, 0x5555};
768  int wpf, checksum, sum, calculated_checksum, temp1;
769 
770  checksum = get_bits(gb, s->checksum_bits);
771 
772  wpf = bits_per_frame / 16;
773  sum = 0;
774  for (int i = 0; i < wpf - 1; i++)
775  sum ^= AV_RB16(avpkt->data + i * 2) << (i % 15);
776  sum ^= (AV_RB16(avpkt->data + (wpf - 1) * 2) & ~checksum) << ((wpf - 1) % 15);
777  sum = (sum >> 15) ^ (sum & 0x7FFF);
778 
779  calculated_checksum = 0;
780  for (int i = 0; i < 4; i++) {
781  temp1 = ChecksumTable[i] & sum;
782 
783  for (int j = 8; j > 0; j >>= 1)
784  temp1 ^= temp1 >> j;
785 
786  calculated_checksum <<= 1;
787  calculated_checksum |= temp1 & 1;
788  }
789 
790  if (checksum != calculated_checksum) {
791  av_log(avctx, AV_LOG_WARNING, "Invalid checksum\n");
792  if (avctx->err_recognition & AV_EF_EXPLODE)
793  return AVERROR_INVALIDDATA;
794  frame_error = 1;
795  }
796  }
797 
798  if (frame_error) {
799  memcpy(s->imdct_in, s->backup_frame, number_of_valid_coefs * sizeof(float));
800  memset(s->backup_frame, 0, number_of_valid_coefs * sizeof(float));
801  } else {
802  memcpy(s->backup_frame, s->imdct_in, number_of_valid_coefs * sizeof(float));
803  }
804 
805  frame->nb_samples = FRAME_SIZE;
806  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
807  return ret;
808 
809  for (int i = 0; i < FRAME_SIZE; i += 2)
810  s->imdct_in[i] *= -1;
811 
812  s->tx_fn(s->tx_ctx, s->imdct_out, s->imdct_in, sizeof(float));
813  s->fdsp->vector_fmul_window((float *)frame->data[0],
814  s->imdct_prev + (FRAME_SIZE >> 1),
815  s->imdct_out, s->window,
816  FRAME_SIZE >> 1);
817  FFSWAP(float *, s->imdct_out, s->imdct_prev);
818 
819  *got_frame = 1;
820 
821  return s->microsoft ? bits_per_frame / 8 : avpkt->size;
822 }
823 
824 static av_cold void siren_flush(AVCodecContext *avctx)
825 {
826  SirenContext *s = avctx->priv_data;
827 
828  memset(s->backup_frame, 0, sizeof(s->backup_frame));
829  memset(s->imdct_prev, 0, FRAME_SIZE * sizeof(*s->imdct_prev));
830  memset(s->imdct_out, 0, FRAME_SIZE * sizeof(*s->imdct_out));
831 }
832 
834 {
835  SirenContext *s = avctx->priv_data;
836 
837  av_freep(&s->fdsp);
838  av_tx_uninit(&s->tx_ctx);
839 
840  return 0;
841 }
842 
844  .p.name = "siren",
845  CODEC_LONG_NAME("Siren"),
846  .priv_data_size = sizeof(SirenContext),
847  .p.type = AVMEDIA_TYPE_AUDIO,
848  .p.id = AV_CODEC_ID_SIREN,
849  .init = siren_init,
850  .close = siren_close,
852  .flush = siren_flush,
853  .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
855  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
856 };
857 
859  .p.name = "msnsiren",
860  CODEC_LONG_NAME("MSN Siren"),
861  .priv_data_size = sizeof(SirenContext),
862  .p.type = AVMEDIA_TYPE_AUDIO,
863  .p.id = AV_CODEC_ID_MSNSIREN,
864  .init = siren_init,
865  .close = siren_close,
867  .flush = siren_flush,
868  .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
870  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
871 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
SirenContext::fdsp
AVFloatDSPContext * fdsp
Definition: siren.c:379
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: defs.h:51
av_clip
#define av_clip
Definition: common.h:95
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AV_CODEC_ID_SIREN
@ AV_CODEC_ID_SIREN
Definition: codec_id.h:530
show_bits1
static unsigned int show_bits1(GetBitContext *s)
Definition: get_bits.h:398
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:664
AVERROR
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
SirenContext::gb
GetBitContext gb
Definition: siren.c:360
mem_internal.h
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1034
decoder_tree3
static const uint16_t decoder_tree3[1040]
Definition: siren.c:217
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:119
siren_init
static av_cold int siren_init(AVCodecContext *avctx)
Definition: siren.c:390
AVTXContext
Definition: tx_priv.h:228
AVCodecContext::err_recognition
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1382
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:256
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVPacket::data
uint8_t * data
Definition: packet.h:374
M_PI_2
#define M_PI_2
Definition: mathematics.h:55
categorize_regions
static int categorize_regions(int number_of_regions, int number_of_available_bits, int *absolute_region_power_index, int *power_categories, int *category_balance)
Definition: siren.c:462
rate_control
static int rate_control(AVCodecContext *avctx, void *arg)
Definition: vc2enc.c:617
SirenContext::imdct_prev
float * imdct_prev
Definition: siren.c:387
FFCodec
Definition: codec_internal.h:127
category
category
Definition: openal-dec.c:248
max
#define max(a, b)
Definition: cuda_runtime.h:33
noise_category6
static const float noise_category6[21]
Definition: siren.c:351
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:493
av_tx_init
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition: tx.c:883
decode_vector
static int decode_vector(SirenContext *s, int number_of_regions, float *decoder_standard_deviation, int *power_categories, float *coefs, int scale_factor)
Definition: siren.c:572
siren_flush
static av_cold void siren_flush(AVCodecContext *avctx)
Definition: siren.c:824
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:371
decode_envelope
static int decode_envelope(SirenContext *s, GetBitContext *gb, int number_of_regions, float *decoder_standard_deviation, int *absolute_region_power_index, int esf_adjustment)
Definition: siren.c:438
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:325
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:435
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2054
GetBitContext
Definition: get_bits.h:107
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:506
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1389
SirenContext::decoder_standard_deviation
float decoder_standard_deviation[32]
Definition: siren.c:373
decoder_tree5
static const uint16_t decoder_tree5[384]
Definition: siren.c:289
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:524
av_tx_fn
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition: tx.h:127
SirenContext::microsoft
int microsoft
Definition: siren.c:362
AV_TX_FLOAT_MDCT
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respecively.
Definition: tx.h:68
decoder_tree0
static const uint16_t decoder_tree0[360]
Definition: siren.c:176
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
number_of_vectors
static const uint8_t number_of_vectors[8]
Definition: siren.c:36
s
#define s(width, name)
Definition: cbs_vp9.c:256
decoder_tree1
static const uint16_t decoder_tree1[188]
Definition: siren.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
decode.h
get_bits.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
siren_decode
static int siren_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: siren.c:705
SirenContext::dw1
unsigned dw1
Definition: siren.c:370
SirenContext::dw2
unsigned dw2
Definition: siren.c:370
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
NULL
#define NULL
Definition: coverity.c:32
index_table
static const uint8_t index_table[8]
Definition: siren.c:34
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:378
SirenContext::sample_rate_bits
int sample_rate_bits
Definition: siren.c:367
SirenContext::standard_deviation
float standard_deviation[64]
Definition: siren.c:376
sqrtf
static __device__ float sqrtf(float a)
Definition: cuda_runtime.h:184
sinf
#define sinf(x)
Definition: libm.h:419
AV_EF_CRCCHECK
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
Definition: defs.h:48
index
int index
Definition: gxfenc.c:89
SirenContext::dw3
unsigned dw3
Definition: siren.c:370
float_dsp.h
SirenContext::window
float * window
Definition: siren.c:384
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:103
differential_decoder_tree
static const int8_t differential_decoder_tree[27][24][2]
Definition: siren.c:38
SirenContext::tx_fn
av_tx_fn tx_fn
Definition: siren.c:380
SirenContext::imdct_buf
float imdct_buf[4][FRAME_SIZE]
Definition: siren.c:383
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1473
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
powf
#define powf(x, y)
Definition: libm.h:50
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:301
codec_internal.h
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:87
expected_bits_table
static const uint8_t expected_bits_table[8]
Definition: siren.c:37
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1050
SirenContext::number_of_regions
int number_of_regions
Definition: siren.c:365
AVFloatDSPContext
Definition: float_dsp.h:24
SirenContext::dw4
unsigned dw4
Definition: siren.c:370
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
SirenContext::absolute_region_power_index
int absolute_region_power_index[32]
Definition: siren.c:372
av_tx_uninit
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition: tx.c:294
mlt_quant
static const float mlt_quant[7][14]
Definition: siren.c:336
AVCodec::id
enum AVCodecID id
Definition: codec.h:198
SirenContext
Definition: siren.c:359
SirenContext::category_balance
int category_balance[32]
Definition: siren.c:375
AV_CODEC_ID_MSNSIREN
@ AV_CODEC_ID_MSNSIREN
Definition: codec_id.h:533
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
SirenContext::power_categories
int power_categories[32]
Definition: siren.c:374
delta
float delta
Definition: vorbis_enc_data.h:430
SirenContext::tx_ctx
AVTXContext * tx_ctx
Definition: siren.c:381
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:191
decoder_tree2
static const uint16_t decoder_tree2[96]
Definition: siren.c:210
avcodec.h
siren_close
static av_cold int siren_close(AVCodecContext *avctx)
Definition: siren.c:833
SirenContext::backup_frame
float backup_frame[FRAME_SIZE]
Definition: siren.c:377
ret
ret
Definition: filter_design.txt:187
SirenContext::scale_factor
int scale_factor
Definition: siren.c:366
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
frame
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
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:426
decoder_tables_elements
static const int decoder_tables_elements[7]
Definition: siren.c:326
channel_layout.h
decoder_tree4
static const uint16_t decoder_tree4[416]
Definition: siren.c:266
noise
static int noise(AVBSFContext *ctx, AVPacket *pkt)
Definition: noise_bsf.c:120
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:632
temp
else temp
Definition: vf_mcdeint.c:248
vector_dimension
static const uint8_t vector_dimension[8]
Definition: siren.c:35
FRAME_SIZE
#define FRAME_SIZE
Definition: siren.c:356
SirenContext::rate_control_possibilities
int rate_control_possibilities
Definition: siren.c:363
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:321
decoder_tables
static const uint16_t *const decoder_tables[7]
Definition: siren.c:316
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:368
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:453
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
avpriv_float_dsp_alloc
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
ff_siren_decoder
const FFCodec ff_siren_decoder
Definition: siren.c:843
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
SirenContext::imdct_out
float * imdct_out
Definition: siren.c:386
SirenContext::esf_adjustment
int esf_adjustment
Definition: siren.c:364
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
noise_category5
static const float noise_category5[21]
Definition: siren.c:346
get_dw
static int get_dw(SirenContext *s)
Definition: siren.c:557
SirenContext::imdct_in
float * imdct_in
Definition: siren.c:385
decoder_tree6
static const uint16_t decoder_tree6[62]
Definition: siren.c:310
int
int
Definition: ffmpeg_filter.c:156
SirenContext::checksum_bits
int checksum_bits
Definition: siren.c:368
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60
REGION_SIZE
#define REGION_SIZE
Definition: siren.c:357
AV_RB16
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:98
tx.h
ff_msnsiren_decoder
const FFCodec ff_msnsiren_decoder
Definition: siren.c:858
min
float min
Definition: vorbis_enc_data.h:429