/src/opus/src/extensions.c
Line | Count | Source |
1 | | /* Copyright (c) 2022 Amazon */ |
2 | | /* |
3 | | Redistribution and use in source and binary forms, with or without |
4 | | modification, are permitted provided that the following conditions |
5 | | are met: |
6 | | |
7 | | - Redistributions of source code must retain the above copyright |
8 | | notice, this list of conditions and the following disclaimer. |
9 | | |
10 | | - Redistributions in binary form must reproduce the above copyright |
11 | | notice, this list of conditions and the following disclaimer in the |
12 | | documentation and/or other materials provided with the distribution. |
13 | | |
14 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
15 | | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
16 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
17 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
18 | | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
22 | | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
23 | | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
24 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | | */ |
26 | | |
27 | | #ifdef HAVE_CONFIG_H |
28 | | #include "config.h" |
29 | | #endif |
30 | | |
31 | | |
32 | | #include "opus_types.h" |
33 | | #include "opus_defines.h" |
34 | | #include "arch.h" |
35 | | #include "os_support.h" |
36 | | #include "opus_private.h" |
37 | | |
38 | | |
39 | | /* Given an extension payload (i.e., excluding the initial ID byte), advance |
40 | | data to the next extension and return the length of the remaining |
41 | | extensions. |
42 | | N.B., a "Repeat These Extensions" extension (ID==2) does not advance past |
43 | | the repeated extension payloads. |
44 | | That requires higher-level logic. */ |
45 | | static opus_int32 skip_extension_payload(const unsigned char **pdata, |
46 | | opus_int32 len, opus_int32 *pheader_size, int id_byte, |
47 | | opus_int32 trailing_short_len) |
48 | 0 | { |
49 | 0 | const unsigned char *data; |
50 | 0 | opus_int32 header_size; |
51 | 0 | int id, L; |
52 | 0 | data = *pdata; |
53 | 0 | header_size = 0; |
54 | 0 | id = id_byte>>1; |
55 | 0 | L = id_byte&1; |
56 | 0 | if ((id == 0 && L == 1) || id == 2) |
57 | 0 | { |
58 | | /* Nothing to do. */ |
59 | 0 | } else if (id > 0 && id < 32) |
60 | 0 | { |
61 | 0 | if (len < L) |
62 | 0 | return -1; |
63 | 0 | data += L; |
64 | 0 | len -= L; |
65 | 0 | } else { |
66 | 0 | if (L==0) |
67 | 0 | { |
68 | 0 | if (len < trailing_short_len) return -1; |
69 | 0 | data += len - trailing_short_len; |
70 | 0 | len = trailing_short_len; |
71 | 0 | } else { |
72 | 0 | opus_int32 bytes=0; |
73 | 0 | opus_int32 lacing; |
74 | 0 | do { |
75 | 0 | if (len < 1) |
76 | 0 | return -1; |
77 | 0 | lacing = *data++; |
78 | 0 | bytes += lacing; |
79 | 0 | header_size++; |
80 | 0 | len -= lacing + 1; |
81 | 0 | } while (lacing == 255); |
82 | 0 | if (len < 0) |
83 | 0 | return -1; |
84 | 0 | data += bytes; |
85 | 0 | } |
86 | 0 | } |
87 | 0 | *pdata = data; |
88 | 0 | *pheader_size = header_size; |
89 | 0 | return len; |
90 | 0 | } |
91 | | |
92 | | /* Given an extension, advance data to the next extension and return the |
93 | | length of the remaining extensions. |
94 | | N.B., a "Repeat These Extensions" extension (ID==2) only advances past the |
95 | | extension ID byte. |
96 | | Higher-level logic is required to skip the extension payloads that come |
97 | | after it.*/ |
98 | | static opus_int32 skip_extension(const unsigned char **pdata, opus_int32 len, |
99 | | opus_int32 *pheader_size) |
100 | 0 | { |
101 | 0 | const unsigned char *data; |
102 | 0 | int id_byte; |
103 | 0 | if (len == 0) { |
104 | 0 | *pheader_size = 0; |
105 | 0 | return 0; |
106 | 0 | } |
107 | 0 | if (len < 1) |
108 | 0 | return -1; |
109 | 0 | data = *pdata; |
110 | 0 | id_byte = *data++; |
111 | 0 | len--; |
112 | 0 | len = skip_extension_payload(&data, len, pheader_size, id_byte, 0); |
113 | 0 | if (len >= 0) { |
114 | 0 | *pdata = data; |
115 | 0 | (*pheader_size)++; |
116 | 0 | } |
117 | 0 | return len; |
118 | 0 | } |
119 | | |
120 | | void opus_extension_iterator_init(OpusExtensionIterator *iter, |
121 | 237k | const unsigned char *data, opus_int32 len, opus_int32 nb_frames) { |
122 | 237k | celt_assert(len >= 0); |
123 | 237k | celt_assert(data != NULL || len == 0); |
124 | 237k | celt_assert(nb_frames >= 0 && nb_frames <= 48); |
125 | 237k | iter->repeat_data = iter->curr_data = iter->data = data; |
126 | 237k | iter->last_long = iter->src_data = NULL; |
127 | 237k | iter->curr_len = iter->len = len; |
128 | 237k | iter->repeat_len = iter->src_len = 0; |
129 | 237k | iter->trailing_short_len = 0; |
130 | 237k | iter->frame_max = iter->nb_frames = nb_frames; |
131 | 237k | iter->repeat_frame = iter->curr_frame = 0; |
132 | 237k | iter->repeat_l = 0; |
133 | 237k | } |
134 | | |
135 | | /* Reset the iterator so it can start iterating again from the first |
136 | | extension. */ |
137 | 0 | void opus_extension_iterator_reset(OpusExtensionIterator *iter) { |
138 | 0 | iter->repeat_data = iter->curr_data = iter->data; |
139 | 0 | iter->last_long = NULL; |
140 | 0 | iter->curr_len = iter->len; |
141 | 0 | iter->repeat_frame = iter->curr_frame = 0; |
142 | 0 | iter->trailing_short_len = 0; |
143 | 0 | } |
144 | | |
145 | | /* Tell the iterator not to return any extensions for frames of index |
146 | | frame_max or larger. |
147 | | This can allow it to stop iterating early if these extensions are not |
148 | | needed. */ |
149 | | void opus_extension_iterator_set_frame_max(OpusExtensionIterator *iter, |
150 | 0 | int frame_max) { |
151 | 0 | iter->frame_max = frame_max; |
152 | 0 | } |
153 | | |
154 | | /* Return the next repeated extension. |
155 | | The return value is non-zero if one is found, negative on error, or 0 if we |
156 | | have finished repeating extensions. */ |
157 | | static int opus_extension_iterator_next_repeat(OpusExtensionIterator *iter, |
158 | 0 | opus_extension_data *ext) { |
159 | 0 | opus_int32 header_size; |
160 | 0 | celt_assert(iter->repeat_frame > 0); |
161 | 0 | for (;iter->repeat_frame < iter->nb_frames; iter->repeat_frame++) { |
162 | 0 | while (iter->src_len > 0) { |
163 | 0 | const unsigned char *curr_data0; |
164 | 0 | int repeat_id_byte; |
165 | 0 | repeat_id_byte = *iter->src_data; |
166 | 0 | iter->src_len = skip_extension(&iter->src_data, iter->src_len, |
167 | 0 | &header_size); |
168 | | /* We skipped this extension earlier, so it should not fail now. */ |
169 | 0 | celt_assert(iter->src_len >= 0); |
170 | | /* Don't repeat padding or frame separators with a 0 increment. */ |
171 | 0 | if (repeat_id_byte <= 3) continue; |
172 | | /* If the "Repeat These Extensions" extension had L == 0 and this |
173 | | is the last repeated long extension, then force decoding the |
174 | | payload with L = 0. */ |
175 | 0 | if (iter->repeat_l == 0 |
176 | 0 | && iter->repeat_frame + 1 >= iter->nb_frames |
177 | 0 | && iter->src_data == iter->last_long) { |
178 | 0 | repeat_id_byte &= ~1; |
179 | 0 | } |
180 | 0 | curr_data0 = iter->curr_data; |
181 | 0 | iter->curr_len = skip_extension_payload(&iter->curr_data, |
182 | 0 | iter->curr_len, &header_size, repeat_id_byte, |
183 | 0 | iter->trailing_short_len); |
184 | 0 | if (iter->curr_len < 0) { |
185 | 0 | return OPUS_INVALID_PACKET; |
186 | 0 | } |
187 | 0 | celt_assert(iter->curr_data - iter->data |
188 | 0 | == iter->len - iter->curr_len); |
189 | | /* If we were asked to stop at frame_max, skip extensions for later |
190 | | frames. */ |
191 | 0 | if (iter->repeat_frame >= iter->frame_max) { |
192 | 0 | continue; |
193 | 0 | } |
194 | 0 | if (ext != NULL) { |
195 | 0 | ext->id = repeat_id_byte >> 1; |
196 | 0 | ext->frame = iter->repeat_frame; |
197 | 0 | ext->data = curr_data0 + header_size; |
198 | 0 | ext->len = iter->curr_data - curr_data0 - header_size; |
199 | 0 | } |
200 | 0 | return 1; |
201 | 0 | } |
202 | | /* We finished repeating the extensions for this frame. */ |
203 | 0 | iter->src_data = iter->repeat_data; |
204 | 0 | iter->src_len = iter->repeat_len; |
205 | 0 | } |
206 | | /* We finished repeating extensions. */ |
207 | 0 | iter->repeat_data = iter->curr_data; |
208 | 0 | iter->last_long = NULL; |
209 | | /* If L == 0, advance the frame number to handle the case where we did |
210 | | not consume all of the data with an L == 0 long extension. */ |
211 | 0 | if (iter->repeat_l == 0) { |
212 | 0 | iter->curr_frame++; |
213 | | /* Ignore additional padding if this was already the last frame. */ |
214 | 0 | if (iter->curr_frame >= iter->nb_frames) { |
215 | 0 | iter->curr_len = 0; |
216 | 0 | } |
217 | 0 | } |
218 | 0 | iter->repeat_frame = 0; |
219 | 0 | return 0; |
220 | 0 | } |
221 | | |
222 | | /* Return the next extension (excluding real padding, separators, and repeat |
223 | | indicators, but including the repeated extensions) in bitstream order. |
224 | | Due to the extension repetition mechanism, extensions are not necessarily |
225 | | returned in frame order. */ |
226 | | int opus_extension_iterator_next(OpusExtensionIterator *iter, |
227 | 0 | opus_extension_data *ext) { |
228 | 0 | opus_int32 header_size; |
229 | 0 | if (iter->curr_len < 0) { |
230 | 0 | return OPUS_INVALID_PACKET; |
231 | 0 | } |
232 | 0 | if (iter->repeat_frame > 0) { |
233 | 0 | int ret; |
234 | | /* We are in the process of repeating some extensions. */ |
235 | 0 | ret = opus_extension_iterator_next_repeat(iter, ext); |
236 | 0 | if (ret) return ret; |
237 | 0 | } |
238 | | /* Checking this here allows opus_extension_iterator_set_frame_max() to be |
239 | | called at any point. */ |
240 | 0 | if (iter->curr_frame >= iter->frame_max) { |
241 | 0 | return 0; |
242 | 0 | } |
243 | 0 | while (iter->curr_len > 0) { |
244 | 0 | const unsigned char *curr_data0; |
245 | 0 | int id; |
246 | 0 | int L; |
247 | 0 | curr_data0 = iter->curr_data; |
248 | 0 | id = *curr_data0>>1; |
249 | 0 | L = *curr_data0&1; |
250 | 0 | iter->curr_len = skip_extension(&iter->curr_data, iter->curr_len, |
251 | 0 | &header_size); |
252 | 0 | if (iter->curr_len < 0) { |
253 | 0 | return OPUS_INVALID_PACKET; |
254 | 0 | } |
255 | 0 | celt_assert(iter->curr_data - iter->data == iter->len - iter->curr_len); |
256 | 0 | if (id == 1) { |
257 | 0 | if (L == 0) { |
258 | 0 | iter->curr_frame++; |
259 | 0 | } |
260 | 0 | else { |
261 | | /* A frame increment of 0 is a no-op. */ |
262 | 0 | if (!curr_data0[1]) continue; |
263 | 0 | iter->curr_frame += curr_data0[1]; |
264 | 0 | } |
265 | 0 | if (iter->curr_frame >= iter->nb_frames) { |
266 | 0 | iter->curr_len = -1; |
267 | 0 | return OPUS_INVALID_PACKET; |
268 | 0 | } |
269 | | /* If we were asked to stop at frame_max, skip extensions for later |
270 | | frames. */ |
271 | 0 | if (iter->curr_frame >= iter->frame_max) { |
272 | 0 | iter->curr_len = 0; |
273 | 0 | } |
274 | 0 | iter->repeat_data = iter->curr_data; |
275 | 0 | iter->last_long = NULL; |
276 | 0 | iter->trailing_short_len = 0; |
277 | 0 | } |
278 | 0 | else if (id == 2) { |
279 | 0 | int ret; |
280 | 0 | iter->repeat_l = L; |
281 | 0 | iter->repeat_frame = iter->curr_frame + 1; |
282 | 0 | iter->repeat_len = curr_data0 - iter->repeat_data; |
283 | 0 | iter->src_data = iter->repeat_data; |
284 | 0 | iter->src_len = iter->repeat_len; |
285 | 0 | ret = opus_extension_iterator_next_repeat(iter, ext); |
286 | 0 | if (ret) return ret; |
287 | 0 | } |
288 | 0 | else if (id > 2) { |
289 | | /* Update the location of the last long extension. |
290 | | This lets us know when we need to modify the last L flag if we |
291 | | repeat these extensions with L=0. */ |
292 | 0 | if (id >= 32) { |
293 | 0 | iter->last_long = iter->curr_data; |
294 | 0 | iter->trailing_short_len = 0; |
295 | 0 | } |
296 | | /* Otherwise, keep track of how many payload bytes follow the last |
297 | | long extension. */ |
298 | 0 | else iter->trailing_short_len += L; |
299 | 0 | if (ext != NULL) { |
300 | 0 | ext->id = id; |
301 | 0 | ext->frame = iter->curr_frame; |
302 | 0 | ext->data = curr_data0 + header_size; |
303 | 0 | ext->len = iter->curr_data - curr_data0 - header_size; |
304 | 0 | } |
305 | 0 | return 1; |
306 | 0 | } |
307 | 0 | } |
308 | 0 | return 0; |
309 | 0 | } |
310 | | |
311 | | int opus_extension_iterator_find(OpusExtensionIterator *iter, |
312 | 0 | opus_extension_data *ext, int id) { |
313 | 0 | opus_extension_data curr_ext; |
314 | 0 | int ret; |
315 | 0 | for(;;) { |
316 | 0 | ret = opus_extension_iterator_next(iter, &curr_ext); |
317 | 0 | if (ret <= 0) { |
318 | 0 | return ret; |
319 | 0 | } |
320 | 0 | if (curr_ext.id == id) { |
321 | 0 | *ext = curr_ext; |
322 | 0 | return ret; |
323 | 0 | } |
324 | 0 | } |
325 | 0 | } |
326 | | |
327 | | /* Count the number of extensions, excluding real padding, separators, and |
328 | | repeat indicators, but including the repeated extensions. */ |
329 | | opus_int32 opus_packet_extensions_count(const unsigned char *data, |
330 | | opus_int32 len, int nb_frames) |
331 | 0 | { |
332 | 0 | OpusExtensionIterator iter; |
333 | 0 | int count; |
334 | 0 | opus_extension_iterator_init(&iter, data, len, nb_frames); |
335 | 0 | for (count=0; opus_extension_iterator_next(&iter, NULL) > 0; count++); |
336 | 0 | return count; |
337 | 0 | } |
338 | | |
339 | | /* Count the number of extensions for each frame, excluding real padding and |
340 | | separators and repeat indicators, but including the repeated extensions. */ |
341 | | opus_int32 opus_packet_extensions_count_ext(const unsigned char *data, |
342 | 0 | opus_int32 len, opus_int32 *nb_frame_exts, int nb_frames) { |
343 | 0 | OpusExtensionIterator iter; |
344 | 0 | opus_extension_data ext; |
345 | 0 | int count; |
346 | 0 | opus_extension_iterator_init(&iter, data, len, nb_frames); |
347 | 0 | OPUS_CLEAR(nb_frame_exts, nb_frames); |
348 | 0 | for (count=0; opus_extension_iterator_next(&iter, &ext) > 0; count++) { |
349 | 0 | nb_frame_exts[ext.frame]++; |
350 | 0 | } |
351 | 0 | return count; |
352 | 0 | } |
353 | | |
354 | | /* Extract extensions from Opus padding (excluding real padding, separators, |
355 | | and repeat indicators, but including the repeated extensions) in bitstream |
356 | | order. |
357 | | Due to the extension repetition mechanism, extensions are not necessarily |
358 | | returned in frame order. */ |
359 | | opus_int32 opus_packet_extensions_parse(const unsigned char *data, |
360 | | opus_int32 len, opus_extension_data *extensions, opus_int32 *nb_extensions, |
361 | 0 | int nb_frames) { |
362 | 0 | OpusExtensionIterator iter; |
363 | 0 | int count; |
364 | 0 | int ret; |
365 | 0 | celt_assert(nb_extensions != NULL); |
366 | 0 | celt_assert(extensions != NULL || *nb_extensions == 0); |
367 | 0 | opus_extension_iterator_init(&iter, data, len, nb_frames); |
368 | 0 | for (count=0;; count++) { |
369 | 0 | opus_extension_data ext; |
370 | 0 | ret = opus_extension_iterator_next(&iter, &ext); |
371 | 0 | if (ret <= 0) break; |
372 | 0 | if (count == *nb_extensions) { |
373 | 0 | return OPUS_BUFFER_TOO_SMALL; |
374 | 0 | } |
375 | 0 | extensions[count] = ext; |
376 | 0 | } |
377 | 0 | *nb_extensions = count; |
378 | 0 | return ret; |
379 | 0 | } |
380 | | |
381 | | /* Extract extensions from Opus padding (excluding real padding, separators, |
382 | | and repeat indicators, but including the repeated extensions) in frame |
383 | | order. |
384 | | nb_frame_exts must be filled with the output of |
385 | | opus_packet_extensions_count_ext(). */ |
386 | | opus_int32 opus_packet_extensions_parse_ext(const unsigned char *data, |
387 | | opus_int32 len, opus_extension_data *extensions, opus_int32 *nb_extensions, |
388 | 0 | const opus_int32 *nb_frame_exts, int nb_frames) { |
389 | 0 | OpusExtensionIterator iter; |
390 | 0 | opus_extension_data ext; |
391 | 0 | opus_int32 nb_frames_cum[49]; |
392 | 0 | int count; |
393 | 0 | int prev_total; |
394 | 0 | int ret; |
395 | 0 | celt_assert(nb_extensions != NULL); |
396 | 0 | celt_assert(extensions != NULL || *nb_extensions == 0); |
397 | 0 | celt_assert(nb_frames <= 48); |
398 | | /* Convert the frame extension count array to a cumulative sum. */ |
399 | 0 | prev_total = 0; |
400 | 0 | for (count=0; count<nb_frames; count++) { |
401 | 0 | int total; |
402 | 0 | total = nb_frame_exts[count] + prev_total; |
403 | 0 | nb_frames_cum[count] = prev_total; |
404 | 0 | prev_total = total; |
405 | 0 | } |
406 | 0 | nb_frames_cum[count] = prev_total; |
407 | 0 | opus_extension_iterator_init(&iter, data, len, nb_frames); |
408 | 0 | for (count=0;; count++) { |
409 | 0 | opus_int32 idx; |
410 | 0 | ret = opus_extension_iterator_next(&iter, &ext); |
411 | 0 | if (ret <= 0) break; |
412 | 0 | idx = nb_frames_cum[ext.frame]++; |
413 | 0 | if (idx >= *nb_extensions) { |
414 | 0 | return OPUS_BUFFER_TOO_SMALL; |
415 | 0 | } |
416 | 0 | celt_assert(idx < nb_frames_cum[ext.frame + 1]); |
417 | 0 | extensions[idx] = ext; |
418 | 0 | } |
419 | 0 | *nb_extensions = count; |
420 | 0 | return ret; |
421 | 0 | } |
422 | | |
423 | | static int write_extension_payload(unsigned char *data, opus_int32 len, |
424 | 0 | opus_int32 pos, const opus_extension_data *ext, int last) { |
425 | 0 | celt_assert(ext->id >= 3 && ext->id <= 127); |
426 | 0 | if (ext->id < 32) |
427 | 0 | { |
428 | 0 | if (ext->len < 0 || ext->len > 1) |
429 | 0 | return OPUS_BAD_ARG; |
430 | 0 | if (ext->len > 0) { |
431 | 0 | if (len-pos < ext->len) |
432 | 0 | return OPUS_BUFFER_TOO_SMALL; |
433 | 0 | if (data) data[pos] = ext->data[0]; |
434 | 0 | pos++; |
435 | 0 | } |
436 | 0 | } else { |
437 | 0 | opus_int32 length_bytes; |
438 | 0 | if (ext->len < 0) |
439 | 0 | return OPUS_BAD_ARG; |
440 | 0 | length_bytes = 1 + ext->len/255; |
441 | 0 | if (last) |
442 | 0 | length_bytes = 0; |
443 | 0 | if (len-pos < length_bytes + ext->len) |
444 | 0 | return OPUS_BUFFER_TOO_SMALL; |
445 | 0 | if (!last) |
446 | 0 | { |
447 | 0 | opus_int32 j; |
448 | 0 | for (j=0;j<ext->len/255;j++) { |
449 | 0 | if (data) data[pos] = 255; |
450 | 0 | pos++; |
451 | 0 | } |
452 | 0 | if (data) data[pos] = ext->len % 255; |
453 | 0 | pos++; |
454 | 0 | } |
455 | 0 | if (data) OPUS_COPY(&data[pos], ext->data, ext->len); |
456 | 0 | pos += ext->len; |
457 | 0 | } |
458 | 0 | return pos; |
459 | 0 | } |
460 | | |
461 | | static int write_extension(unsigned char *data, opus_int32 len, opus_int32 pos, |
462 | 0 | const opus_extension_data *ext, int last) { |
463 | 0 | if (len-pos < 1) |
464 | 0 | return OPUS_BUFFER_TOO_SMALL; |
465 | 0 | celt_assert(ext->id >= 3 && ext->id <= 127); |
466 | 0 | if (data) data[pos] = (ext->id<<1) + (ext->id < 32 ? ext->len : !last); |
467 | 0 | pos++; |
468 | 0 | return write_extension_payload(data, len, pos, ext, last); |
469 | 0 | } |
470 | | |
471 | | opus_int32 opus_packet_extensions_generate(unsigned char *data, opus_int32 len, |
472 | | const opus_extension_data *extensions, opus_int32 nb_extensions, |
473 | | int nb_frames, int pad) |
474 | 0 | { |
475 | 0 | opus_int32 frame_min_idx[48]; |
476 | 0 | opus_int32 frame_max_idx[48]; |
477 | 0 | opus_int32 frame_repeat_idx[48]; |
478 | 0 | opus_int32 i; |
479 | 0 | int f; |
480 | 0 | int curr_frame = 0; |
481 | 0 | opus_int32 pos = 0; |
482 | 0 | opus_int32 written = 0; |
483 | |
|
484 | 0 | celt_assert(len >= 0); |
485 | 0 | if (nb_frames > 48) return OPUS_BAD_ARG; |
486 | | |
487 | | /* Do a little work up-front to make this O(nb_extensions) instead of |
488 | | O(nb_extensions*nb_frames) so long as the extensions are in frame |
489 | | order (without requiring that they be in frame order). */ |
490 | 0 | for (f=0;f<nb_frames;f++) frame_min_idx[f] = nb_extensions; |
491 | 0 | OPUS_CLEAR(frame_max_idx, nb_frames); |
492 | 0 | for (i=0;i<nb_extensions;i++) |
493 | 0 | { |
494 | 0 | f = extensions[i].frame; |
495 | 0 | if (f < 0 || f >= nb_frames) return OPUS_BAD_ARG; |
496 | 0 | if (extensions[i].id < 3 || extensions[i].id > 127) return OPUS_BAD_ARG; |
497 | 0 | frame_min_idx[f] = IMIN(frame_min_idx[f], i); |
498 | 0 | frame_max_idx[f] = IMAX(frame_max_idx[f], i+1); |
499 | 0 | } |
500 | 0 | for (f=0;f<nb_frames;f++) frame_repeat_idx[f] = frame_min_idx[f]; |
501 | 0 | for (f=0;f<nb_frames;f++) |
502 | 0 | { |
503 | 0 | opus_int32 last_long_idx; |
504 | 0 | opus_int32 trailing_short_len; |
505 | 0 | int repeat_count; |
506 | 0 | repeat_count = 0; |
507 | 0 | last_long_idx = -1; |
508 | 0 | trailing_short_len = 0; |
509 | 0 | if (f + 1 < nb_frames) |
510 | 0 | { |
511 | 0 | for (i=frame_min_idx[f];i<frame_max_idx[f];i++) |
512 | 0 | { |
513 | 0 | if (extensions[i].frame == f) |
514 | 0 | { |
515 | 0 | int g; |
516 | | /* Test if we can repeat this extension in future frames. */ |
517 | 0 | for (g=f+1;g<nb_frames;g++) |
518 | 0 | { |
519 | 0 | if (frame_repeat_idx[g] >= frame_max_idx[g]) break; |
520 | 0 | celt_assert(extensions[frame_repeat_idx[g]].frame == g); |
521 | 0 | if (extensions[frame_repeat_idx[g]].id != extensions[i].id) |
522 | 0 | { |
523 | 0 | break; |
524 | 0 | } |
525 | 0 | if (extensions[frame_repeat_idx[g]].id < 32 |
526 | 0 | && extensions[frame_repeat_idx[g]].len |
527 | 0 | != extensions[i].len) |
528 | 0 | { |
529 | 0 | break; |
530 | 0 | } |
531 | 0 | } |
532 | 0 | if (g < nb_frames) break; |
533 | | /* We can! */ |
534 | | /* If this is a long extension, save the index of the last |
535 | | instance, so we can modify its L flag. */ |
536 | 0 | if (extensions[i].id >= 32) { |
537 | 0 | last_long_idx = frame_repeat_idx[nb_frames-1]; |
538 | 0 | trailing_short_len = 0; |
539 | 0 | } |
540 | | /* Otherwise, keep track of how many payload bytes follow the |
541 | | last long extension. */ |
542 | 0 | else trailing_short_len += extensions[i].len; |
543 | | /* Using the repeat mechanism almost always makes the |
544 | | encoding smaller (or at least no larger). |
545 | | However, there's one case where that might not be true: if |
546 | | the last repeated long extension in the last frame was |
547 | | previously the last extension, but using the repeat |
548 | | mechanism makes that no longer true (because there are other |
549 | | non-repeated extensions in earlier frames that must now be |
550 | | coded after it), and coding its length requires more bytes |
551 | | than the repeat mechanism saves. |
552 | | This can only be true if its length is at least 255 bytes |
553 | | (although sometimes it requires even more). |
554 | | Currently we do not check for that, and just always use the |
555 | | repeat mechanism if we can. |
556 | | See git history for code that does the check. */ |
557 | | /* Advance the repeat pointers. */ |
558 | 0 | for (g=f+1; g<nb_frames; g++) |
559 | 0 | { |
560 | 0 | int j; |
561 | 0 | for (j=frame_repeat_idx[g]+1; j<frame_max_idx[g] |
562 | 0 | && extensions[j].frame != g; j++); |
563 | 0 | frame_repeat_idx[g] = j; |
564 | 0 | } |
565 | 0 | repeat_count++; |
566 | | /* Point the repeat pointer for this frame to the current |
567 | | extension, so we know when to trigger the repeats. */ |
568 | 0 | frame_repeat_idx[f] = i; |
569 | 0 | } |
570 | 0 | } |
571 | 0 | } |
572 | 0 | for (i=frame_min_idx[f];i<frame_max_idx[f];i++) |
573 | 0 | { |
574 | 0 | if (extensions[i].frame == f) |
575 | 0 | { |
576 | | /* Insert separator when needed. */ |
577 | 0 | if (f != curr_frame) { |
578 | 0 | int diff = f - curr_frame; |
579 | 0 | if (len-pos < 2) |
580 | 0 | return OPUS_BUFFER_TOO_SMALL; |
581 | 0 | if (diff == 1) { |
582 | 0 | if (data) data[pos] = 0x02; |
583 | 0 | pos++; |
584 | 0 | } else { |
585 | 0 | if (data) data[pos] = 0x03; |
586 | 0 | pos++; |
587 | 0 | if (data) data[pos] = diff; |
588 | 0 | pos++; |
589 | 0 | } |
590 | 0 | curr_frame = f; |
591 | 0 | } |
592 | | |
593 | 0 | pos = write_extension(data, len, pos, extensions + i, |
594 | 0 | written == nb_extensions - 1); |
595 | 0 | if (pos < 0) return pos; |
596 | 0 | written++; |
597 | |
|
598 | 0 | if (repeat_count > 0 && frame_repeat_idx[f] == i) { |
599 | 0 | int nb_repeated; |
600 | 0 | int last; |
601 | 0 | int g; |
602 | | /* Add the repeat indicator. */ |
603 | 0 | nb_repeated = repeat_count*(nb_frames - (f + 1)); |
604 | 0 | last = written + nb_repeated == nb_extensions |
605 | 0 | || (last_long_idx < 0 && i+1 >= frame_max_idx[f]); |
606 | 0 | if (len-pos < 1) |
607 | 0 | return OPUS_BUFFER_TOO_SMALL; |
608 | 0 | if (data) data[pos] = 0x04 + !last; |
609 | 0 | pos++; |
610 | 0 | for (g=f+1;g<nb_frames;g++) |
611 | 0 | { |
612 | 0 | int j; |
613 | 0 | for (j=frame_min_idx[g];j<frame_repeat_idx[g];j++) |
614 | 0 | { |
615 | 0 | if (extensions[j].frame == g) |
616 | 0 | { |
617 | 0 | pos = write_extension_payload(data, len, pos, |
618 | 0 | extensions + j, last && j == last_long_idx); |
619 | 0 | if (pos < 0) return pos; |
620 | 0 | written++; |
621 | 0 | } |
622 | 0 | } |
623 | 0 | frame_min_idx[g] = j; |
624 | 0 | } |
625 | 0 | if (last) curr_frame++; |
626 | 0 | } |
627 | 0 | } |
628 | 0 | } |
629 | 0 | } |
630 | 0 | celt_assert(written == nb_extensions); |
631 | | /* If we need to pad, just prepend 0x01 bytes. Even better would be to fill the |
632 | | end with zeros, but that requires checking that turning the last extension into |
633 | | an L=1 case still fits. */ |
634 | 0 | if (pad && pos < len) |
635 | 0 | { |
636 | 0 | opus_int32 padding = len - pos; |
637 | 0 | if (data) { |
638 | 0 | OPUS_MOVE(data+padding, data, pos); |
639 | 0 | for (i=0;i<padding;i++) |
640 | 0 | data[i] = 0x01; |
641 | 0 | } |
642 | 0 | pos += padding; |
643 | 0 | } |
644 | 0 | return pos; |
645 | 0 | } |
646 | | |
647 | | #if 0 |
648 | | #include <stdio.h> |
649 | | int main() |
650 | | { |
651 | | opus_extension_data ext[] = {{2, 0, (const unsigned char *)"a", 1}, |
652 | | {32, 10, (const unsigned char *)"DRED", 4}, |
653 | | {33, 1, (const unsigned char *)"NOT DRED", 8}, |
654 | | {3, 4, (const unsigned char *)NULL, 0} |
655 | | }; |
656 | | opus_extension_data ext2[10]; |
657 | | int i, len; |
658 | | int nb_ext = 10; |
659 | | unsigned char packet[10000]; |
660 | | len = opus_packet_extensions_generate(packet, 32, ext, 4, 1); |
661 | | for (i=0;i<len;i++) |
662 | | { |
663 | | printf("%#04x ", packet[i]); |
664 | | if (i%16 == 15) |
665 | | printf("\n"); |
666 | | } |
667 | | printf("\n"); |
668 | | printf("count = %d\n", opus_packet_extensions_count(packet, len)); |
669 | | opus_packet_extensions_parse(packet, len, ext2, &nb_ext); |
670 | | for (i=0;i<nb_ext;i++) |
671 | | { |
672 | | int j; |
673 | | printf("%d %d {", ext2[i].id, ext2[i].frame); |
674 | | for (j=0;j<ext2[i].len;j++) printf("%#04x ", ext2[i].data[j]); |
675 | | printf("} %d\n", ext2[i].len); |
676 | | } |
677 | | } |
678 | | #endif |