| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2012 Andrew D'Addesio | ||
| 3 | * Copyright (c) 2013-2014 Mozilla Corporation | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | /** | ||
| 23 | * @file | ||
| 24 | * Opus decoder/parser shared code | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include "libavutil/attributes.h" | ||
| 28 | #include "libavutil/channel_layout.h" | ||
| 29 | #include "libavutil/error.h" | ||
| 30 | #include "libavutil/intreadwrite.h" | ||
| 31 | #include "libavutil/log.h" | ||
| 32 | #include "libavutil/mem.h" | ||
| 33 | |||
| 34 | #include "avcodec.h" | ||
| 35 | #include "internal.h" | ||
| 36 | #include "mathops.h" | ||
| 37 | #include "opus.h" | ||
| 38 | #include "parse.h" | ||
| 39 | #include "vorbis_data.h" | ||
| 40 | |||
| 41 | static const uint16_t opus_frame_duration[32] = { | ||
| 42 | 480, 960, 1920, 2880, | ||
| 43 | 480, 960, 1920, 2880, | ||
| 44 | 480, 960, 1920, 2880, | ||
| 45 | 480, 960, | ||
| 46 | 480, 960, | ||
| 47 | 120, 240, 480, 960, | ||
| 48 | 120, 240, 480, 960, | ||
| 49 | 120, 240, 480, 960, | ||
| 50 | 120, 240, 480, 960, | ||
| 51 | }; | ||
| 52 | |||
| 53 | /** | ||
| 54 | * Read a 1- or 2-byte frame length | ||
| 55 | */ | ||
| 56 | 21825 | static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end) | |
| 57 | { | ||
| 58 | int val; | ||
| 59 | |||
| 60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21825 times.
|
21825 | if (*ptr >= end) |
| 61 | ✗ | return AVERROR_INVALIDDATA; | |
| 62 | 21825 | val = *(*ptr)++; | |
| 63 |
2/2✓ Branch 0 taken 898 times.
✓ Branch 1 taken 20927 times.
|
21825 | if (val >= 252) { |
| 64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 898 times.
|
898 | if (*ptr >= end) |
| 65 | ✗ | return AVERROR_INVALIDDATA; | |
| 66 | 898 | val += 4 * *(*ptr)++; | |
| 67 | } | ||
| 68 | 21825 | return val; | |
| 69 | } | ||
| 70 | |||
| 71 | /** | ||
| 72 | * Read a multi-byte length (used for code 3 packet padding size) | ||
| 73 | */ | ||
| 74 | 1320 | static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end) | |
| 75 | { | ||
| 76 | 1320 | int val = 0; | |
| 77 | int next; | ||
| 78 | |||
| 79 | while (1) { | ||
| 80 |
2/4✓ Branch 0 taken 1776 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1776 times.
|
1776 | if (*ptr >= end || val > INT_MAX - 254) |
| 81 | ✗ | return AVERROR_INVALIDDATA; | |
| 82 | 1776 | next = *(*ptr)++; | |
| 83 | 1776 | val += next; | |
| 84 |
2/2✓ Branch 0 taken 1320 times.
✓ Branch 1 taken 456 times.
|
1776 | if (next < 255) |
| 85 | 1320 | break; | |
| 86 | else | ||
| 87 | 456 | val--; | |
| 88 | } | ||
| 89 | 1320 | return val; | |
| 90 | } | ||
| 91 | |||
| 92 | /** | ||
| 93 | * Parse Opus packet info from raw packet data | ||
| 94 | */ | ||
| 95 | 49157 | int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, | |
| 96 | int self_delimiting) | ||
| 97 | { | ||
| 98 | 49157 | const uint8_t *ptr = buf; | |
| 99 | 49157 | const uint8_t *end = buf + buf_size; | |
| 100 | 49157 | int padding = 0; | |
| 101 | int frame_bytes, i; | ||
| 102 | |||
| 103 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49157 times.
|
49157 | if (buf_size < 1) |
| 104 | ✗ | goto fail; | |
| 105 | |||
| 106 | /* TOC byte */ | ||
| 107 | 49157 | i = *ptr++; | |
| 108 | 49157 | pkt->code = (i ) & 0x3; | |
| 109 | 49157 | pkt->stereo = (i >> 2) & 0x1; | |
| 110 | 49157 | pkt->config = (i >> 3) & 0x1F; | |
| 111 | |||
| 112 | /* code 2 and code 3 packets have at least 1 byte after the TOC */ | ||
| 113 |
3/4✓ Branch 0 taken 8200 times.
✓ Branch 1 taken 40957 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8200 times.
|
49157 | if (pkt->code >= 2 && buf_size < 2) |
| 114 | ✗ | goto fail; | |
| 115 | |||
| 116 |
4/5✓ Branch 0 taken 40317 times.
✓ Branch 1 taken 640 times.
✓ Branch 2 taken 1998 times.
✓ Branch 3 taken 6202 times.
✗ Branch 4 not taken.
|
49157 | switch (pkt->code) { |
| 117 | 40317 | case 0: | |
| 118 | /* 1 frame */ | ||
| 119 | 40317 | pkt->frame_count = 1; | |
| 120 | 40317 | pkt->vbr = 0; | |
| 121 | |||
| 122 |
2/2✓ Branch 0 taken 6901 times.
✓ Branch 1 taken 33416 times.
|
40317 | if (self_delimiting) { |
| 123 | 6901 | int len = xiph_lacing_16bit(&ptr, end); | |
| 124 |
2/4✓ Branch 0 taken 6901 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6901 times.
|
6901 | if (len < 0 || len > end - ptr) |
| 125 | ✗ | goto fail; | |
| 126 | 6901 | end = ptr + len; | |
| 127 | 6901 | buf_size = end - buf; | |
| 128 | } | ||
| 129 | |||
| 130 | 40317 | frame_bytes = end - ptr; | |
| 131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40317 times.
|
40317 | if (frame_bytes > OPUS_MAX_FRAME_SIZE) |
| 132 | ✗ | goto fail; | |
| 133 | 40317 | pkt->frame_offset[0] = ptr - buf; | |
| 134 | 40317 | pkt->frame_size[0] = frame_bytes; | |
| 135 | 40317 | break; | |
| 136 | 640 | case 1: | |
| 137 | /* 2 frames, equal size */ | ||
| 138 | 640 | pkt->frame_count = 2; | |
| 139 | 640 | pkt->vbr = 0; | |
| 140 | |||
| 141 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 640 times.
|
640 | if (self_delimiting) { |
| 142 | ✗ | int len = xiph_lacing_16bit(&ptr, end); | |
| 143 | ✗ | if (len < 0 || 2 * len > end - ptr) | |
| 144 | ✗ | goto fail; | |
| 145 | ✗ | end = ptr + 2 * len; | |
| 146 | ✗ | buf_size = end - buf; | |
| 147 | } | ||
| 148 | |||
| 149 | 640 | frame_bytes = end - ptr; | |
| 150 |
2/4✓ Branch 0 taken 640 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 640 times.
|
640 | if (frame_bytes & 1 || frame_bytes >> 1 > OPUS_MAX_FRAME_SIZE) |
| 151 | ✗ | goto fail; | |
| 152 | 640 | pkt->frame_offset[0] = ptr - buf; | |
| 153 | 640 | pkt->frame_size[0] = frame_bytes >> 1; | |
| 154 | 640 | pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0]; | |
| 155 | 640 | pkt->frame_size[1] = frame_bytes >> 1; | |
| 156 | 640 | break; | |
| 157 | 1998 | case 2: | |
| 158 | /* 2 frames, different sizes */ | ||
| 159 | 1998 | pkt->frame_count = 2; | |
| 160 | 1998 | pkt->vbr = 1; | |
| 161 | |||
| 162 | /* read 1st frame size */ | ||
| 163 | 1998 | frame_bytes = xiph_lacing_16bit(&ptr, end); | |
| 164 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1998 times.
|
1998 | if (frame_bytes < 0) |
| 165 | ✗ | goto fail; | |
| 166 | |||
| 167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1998 times.
|
1998 | if (self_delimiting) { |
| 168 | ✗ | int len = xiph_lacing_16bit(&ptr, end); | |
| 169 | ✗ | if (len < 0 || len + frame_bytes > end - ptr) | |
| 170 | ✗ | goto fail; | |
| 171 | ✗ | end = ptr + frame_bytes + len; | |
| 172 | ✗ | buf_size = end - buf; | |
| 173 | } | ||
| 174 | |||
| 175 | 1998 | pkt->frame_offset[0] = ptr - buf; | |
| 176 | 1998 | pkt->frame_size[0] = frame_bytes; | |
| 177 | |||
| 178 | /* calculate 2nd frame size */ | ||
| 179 | 1998 | frame_bytes = end - ptr - pkt->frame_size[0]; | |
| 180 |
2/4✓ Branch 0 taken 1998 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1998 times.
|
1998 | if (frame_bytes < 0 || frame_bytes > OPUS_MAX_FRAME_SIZE) |
| 181 | ✗ | goto fail; | |
| 182 | 1998 | pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0]; | |
| 183 | 1998 | pkt->frame_size[1] = frame_bytes; | |
| 184 | 1998 | break; | |
| 185 | 6202 | case 3: | |
| 186 | /* 1 to 48 frames, can be different sizes */ | ||
| 187 | 6202 | i = *ptr++; | |
| 188 | 6202 | pkt->frame_count = (i ) & 0x3F; | |
| 189 | 6202 | padding = (i >> 6) & 0x01; | |
| 190 | 6202 | pkt->vbr = (i >> 7) & 0x01; | |
| 191 | |||
| 192 |
2/4✓ Branch 0 taken 6202 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6202 times.
|
6202 | if (pkt->frame_count == 0 || pkt->frame_count > OPUS_MAX_FRAMES) |
| 193 | ✗ | goto fail; | |
| 194 | |||
| 195 | /* read padding size */ | ||
| 196 |
2/2✓ Branch 0 taken 1320 times.
✓ Branch 1 taken 4882 times.
|
6202 | if (padding) { |
| 197 | 1320 | padding = xiph_lacing_full(&ptr, end); | |
| 198 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1320 times.
|
1320 | if (padding < 0) |
| 199 | ✗ | goto fail; | |
| 200 | } | ||
| 201 | |||
| 202 | /* read frame sizes */ | ||
| 203 |
2/2✓ Branch 0 taken 4352 times.
✓ Branch 1 taken 1850 times.
|
6202 | if (pkt->vbr) { |
| 204 | /* for VBR, all frames except the final one have their size coded | ||
| 205 | in the bitstream. the last frame size is implicit. */ | ||
| 206 | 4352 | int total_bytes = 0; | |
| 207 |
2/2✓ Branch 0 taken 12926 times.
✓ Branch 1 taken 4352 times.
|
17278 | for (i = 0; i < pkt->frame_count - 1; i++) { |
| 208 | 12926 | frame_bytes = xiph_lacing_16bit(&ptr, end); | |
| 209 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12926 times.
|
12926 | if (frame_bytes < 0) |
| 210 | ✗ | goto fail; | |
| 211 | 12926 | pkt->frame_size[i] = frame_bytes; | |
| 212 | 12926 | total_bytes += frame_bytes; | |
| 213 | } | ||
| 214 | |||
| 215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4352 times.
|
4352 | if (self_delimiting) { |
| 216 | ✗ | int len = xiph_lacing_16bit(&ptr, end); | |
| 217 | ✗ | if (len < 0 || len + total_bytes + padding > end - ptr) | |
| 218 | ✗ | goto fail; | |
| 219 | ✗ | end = ptr + total_bytes + len + padding; | |
| 220 | ✗ | buf_size = end - buf; | |
| 221 | } | ||
| 222 | |||
| 223 | 4352 | frame_bytes = end - ptr - padding; | |
| 224 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4352 times.
|
4352 | if (total_bytes > frame_bytes) |
| 225 | ✗ | goto fail; | |
| 226 | 4352 | pkt->frame_offset[0] = ptr - buf; | |
| 227 |
2/2✓ Branch 0 taken 12926 times.
✓ Branch 1 taken 4352 times.
|
17278 | for (i = 1; i < pkt->frame_count; i++) |
| 228 | 12926 | pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1]; | |
| 229 | 4352 | pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes; | |
| 230 | } else { | ||
| 231 | /* for CBR, the remaining packet bytes are divided evenly between | ||
| 232 | the frames */ | ||
| 233 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1850 times.
|
1850 | if (self_delimiting) { |
| 234 | ✗ | frame_bytes = xiph_lacing_16bit(&ptr, end); | |
| 235 | ✗ | if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr) | |
| 236 | ✗ | goto fail; | |
| 237 | ✗ | end = ptr + pkt->frame_count * frame_bytes + padding; | |
| 238 | ✗ | buf_size = end - buf; | |
| 239 | } else { | ||
| 240 | 1850 | frame_bytes = end - ptr - padding; | |
| 241 |
1/2✓ Branch 0 taken 1850 times.
✗ Branch 1 not taken.
|
1850 | if (frame_bytes % pkt->frame_count || |
| 242 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1850 times.
|
1850 | frame_bytes / pkt->frame_count > OPUS_MAX_FRAME_SIZE) |
| 243 | ✗ | goto fail; | |
| 244 | 1850 | frame_bytes /= pkt->frame_count; | |
| 245 | } | ||
| 246 | |||
| 247 | 1850 | pkt->frame_offset[0] = ptr - buf; | |
| 248 | 1850 | pkt->frame_size[0] = frame_bytes; | |
| 249 |
2/2✓ Branch 0 taken 780 times.
✓ Branch 1 taken 1850 times.
|
2630 | for (i = 1; i < pkt->frame_count; i++) { |
| 250 | 780 | pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1]; | |
| 251 | 780 | pkt->frame_size[i] = frame_bytes; | |
| 252 | } | ||
| 253 | } | ||
| 254 | } | ||
| 255 | |||
| 256 | 49157 | pkt->packet_size = buf_size; | |
| 257 | 49157 | pkt->data_size = pkt->packet_size - padding; | |
| 258 | |||
| 259 | /* total packet duration cannot be larger than 120ms */ | ||
| 260 | 49157 | pkt->frame_duration = opus_frame_duration[pkt->config]; | |
| 261 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49157 times.
|
49157 | if (pkt->frame_duration * pkt->frame_count > OPUS_MAX_PACKET_DUR) |
| 262 | ✗ | goto fail; | |
| 263 | |||
| 264 | /* set mode and bandwidth */ | ||
| 265 |
2/2✓ Branch 0 taken 9060 times.
✓ Branch 1 taken 40097 times.
|
49157 | if (pkt->config < 12) { |
| 266 | 9060 | pkt->mode = OPUS_MODE_SILK; | |
| 267 | 9060 | pkt->bandwidth = pkt->config >> 2; | |
| 268 |
2/2✓ Branch 0 taken 9851 times.
✓ Branch 1 taken 30246 times.
|
40097 | } else if (pkt->config < 16) { |
| 269 | 9851 | pkt->mode = OPUS_MODE_HYBRID; | |
| 270 |
2/2✓ Branch 0 taken 5147 times.
✓ Branch 1 taken 4704 times.
|
9851 | pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14); |
| 271 | } else { | ||
| 272 | 30246 | pkt->mode = OPUS_MODE_CELT; | |
| 273 | 30246 | pkt->bandwidth = (pkt->config - 16) >> 2; | |
| 274 | /* skip medium band */ | ||
| 275 |
2/2✓ Branch 0 taken 26402 times.
✓ Branch 1 taken 3844 times.
|
30246 | if (pkt->bandwidth) |
| 276 | 26402 | pkt->bandwidth++; | |
| 277 | } | ||
| 278 | |||
| 279 | 49157 | return 0; | |
| 280 | |||
| 281 | ✗ | fail: | |
| 282 | ✗ | memset(pkt, 0, sizeof(*pkt)); | |
| 283 | ✗ | return AVERROR_INVALIDDATA; | |
| 284 | } | ||
| 285 | |||
| 286 | 291 | static int channel_reorder_vorbis(int nb_channels, int channel_idx) | |
| 287 | { | ||
| 288 | 291 | return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx]; | |
| 289 | } | ||
| 290 | |||
| 291 | 267 | static int channel_reorder_unknown(int nb_channels, int channel_idx) | |
| 292 | { | ||
| 293 | 267 | return channel_idx; | |
| 294 | } | ||
| 295 | |||
| 296 | 154 | av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, | |
| 297 | OpusParseContext *s) | ||
| 298 | { | ||
| 299 | static const uint8_t default_channel_map[2] = { 0, 1 }; | ||
| 300 | |||
| 301 | 154 | int (*channel_reorder)(int, int) = channel_reorder_unknown; | |
| 302 | 154 | int channels = avctx->ch_layout.nb_channels; | |
| 303 | |||
| 304 | const uint8_t *extradata, *channel_map; | ||
| 305 | int extradata_size; | ||
| 306 | int version, map_type, streams, stereo_streams, i, j, ret; | ||
| 307 | 154 | AVChannelLayout layout = { 0 }; | |
| 308 | |||
| 309 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 153 times.
|
154 | if (!avctx->extradata) { |
| 310 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (channels > 2) { |
| 311 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 312 | "Multichannel configuration without extradata.\n"); | ||
| 313 | ✗ | return AVERROR(EINVAL); | |
| 314 | } | ||
| 315 | 1 | extradata = opus_default_extradata; | |
| 316 | 1 | extradata_size = sizeof(opus_default_extradata); | |
| 317 | } else { | ||
| 318 | 153 | extradata = avctx->extradata; | |
| 319 | 153 | extradata_size = avctx->extradata_size; | |
| 320 | } | ||
| 321 | |||
| 322 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 154 times.
|
154 | if (extradata_size < 19) { |
| 323 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n", | |
| 324 | extradata_size); | ||
| 325 | ✗ | return AVERROR_INVALIDDATA; | |
| 326 | } | ||
| 327 | |||
| 328 | 154 | version = extradata[8]; | |
| 329 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 154 times.
|
154 | if (version > 15) { |
| 330 | ✗ | avpriv_request_sample(avctx, "Extradata version %d", version); | |
| 331 | ✗ | return AVERROR_PATCHWELCOME; | |
| 332 | } | ||
| 333 | |||
| 334 | 154 | avctx->delay = AV_RL16(extradata + 10); | |
| 335 |
2/2✓ Branch 0 taken 126 times.
✓ Branch 1 taken 28 times.
|
154 | if (avctx->internal) |
| 336 | 126 | avctx->internal->skip_samples = avctx->delay; | |
| 337 | |||
| 338 |
3/4✓ Branch 0 taken 153 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
154 | channels = avctx->extradata ? extradata[9] : (channels == 1) ? 1 : 2; |
| 339 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 154 times.
|
154 | if (!channels) { |
| 340 | ✗ | av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extradata\n"); | |
| 341 | ✗ | return AVERROR_INVALIDDATA; | |
| 342 | } | ||
| 343 | |||
| 344 | 154 | s->gain_i = AV_RL16(extradata + 16); | |
| 345 | |||
| 346 | 154 | map_type = extradata[18]; | |
| 347 |
2/2✓ Branch 0 taken 143 times.
✓ Branch 1 taken 11 times.
|
154 | if (!map_type) { |
| 348 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 143 times.
|
143 | if (channels > 2) { |
| 349 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 350 | "Channel mapping 0 is only specified for up to 2 channels\n"); | ||
| 351 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 352 | ✗ | goto fail; | |
| 353 | } | ||
| 354 |
2/2✓ Branch 0 taken 81 times.
✓ Branch 1 taken 62 times.
|
143 | layout = (channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO : |
| 355 | (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; | ||
| 356 | 143 | streams = 1; | |
| 357 | 143 | stereo_streams = channels - 1; | |
| 358 | 143 | channel_map = default_channel_map; | |
| 359 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
11 | } else if (map_type == 1 || map_type == 2 || map_type == 255) { |
| 360 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (extradata_size < 21 + channels) { |
| 361 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n", | |
| 362 | extradata_size); | ||
| 363 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 364 | ✗ | goto fail; | |
| 365 | } | ||
| 366 | |||
| 367 | 11 | streams = extradata[19]; | |
| 368 | 11 | stereo_streams = extradata[20]; | |
| 369 |
2/4✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
11 | if (!streams || stereo_streams > streams || |
| 370 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | streams + stereo_streams > 255) { |
| 371 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 372 | "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams); | ||
| 373 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 374 | ✗ | goto fail; | |
| 375 | } | ||
| 376 | |||
| 377 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | if (map_type == 1) { |
| 378 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (channels > 8) { |
| 379 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 380 | "Channel mapping 1 is only specified for up to 8 channels\n"); | ||
| 381 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 382 | ✗ | goto fail; | |
| 383 | } | ||
| 384 | 11 | av_channel_layout_copy(&layout, &ff_vorbis_ch_layouts[channels - 1]); | |
| 385 | 11 | channel_reorder = channel_reorder_vorbis; | |
| 386 | ✗ | } else if (map_type == 2) { | |
| 387 | ✗ | int ambisonic_order = ff_sqrt(channels) - 1; | |
| 388 | ✗ | if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) && | |
| 389 | ✗ | channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) { | |
| 390 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 391 | "Channel mapping 2 is only specified for channel counts" | ||
| 392 | " which can be written as (n + 1)^2 or (n + 1)^2 + 2" | ||
| 393 | " for nonnegative integer n\n"); | ||
| 394 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 395 | ✗ | goto fail; | |
| 396 | } | ||
| 397 | ✗ | if (channels > 227) { | |
| 398 | ✗ | av_log(avctx, AV_LOG_ERROR, "Too many channels\n"); | |
| 399 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 400 | ✗ | goto fail; | |
| 401 | } | ||
| 402 | |||
| 403 | ✗ | layout.order = AV_CHANNEL_ORDER_AMBISONIC; | |
| 404 | ✗ | layout.nb_channels = channels; | |
| 405 | ✗ | if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1))) | |
| 406 | ✗ | layout.u.mask = AV_CH_LAYOUT_STEREO; | |
| 407 | } else { | ||
| 408 | ✗ | layout.order = AV_CHANNEL_ORDER_UNSPEC; | |
| 409 | ✗ | layout.nb_channels = channels; | |
| 410 | } | ||
| 411 | |||
| 412 | 11 | channel_map = extradata + 21; | |
| 413 | } else { | ||
| 414 | ✗ | avpriv_request_sample(avctx, "Mapping type %d", map_type); | |
| 415 | ✗ | return AVERROR_PATCHWELCOME; | |
| 416 | } | ||
| 417 | |||
| 418 | 154 | s->channel_maps = av_calloc(channels, sizeof(*s->channel_maps)); | |
| 419 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 154 times.
|
154 | if (!s->channel_maps) { |
| 420 | ✗ | ret = AVERROR(ENOMEM); | |
| 421 | ✗ | goto fail; | |
| 422 | } | ||
| 423 | |||
| 424 |
2/2✓ Branch 0 taken 279 times.
✓ Branch 1 taken 154 times.
|
433 | for (i = 0; i < channels; i++) { |
| 425 | 279 | ChannelMap *map = &s->channel_maps[i]; | |
| 426 | 279 | uint8_t idx = channel_map[channel_reorder(channels, i)]; | |
| 427 | |||
| 428 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
|
279 | if (idx == 255) { |
| 429 | ✗ | map->silence = 1; | |
| 430 | ✗ | continue; | |
| 431 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
|
279 | } else if (idx >= streams + stereo_streams) { |
| 432 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 433 | "Invalid channel map for output channel %d: %d\n", i, idx); | ||
| 434 | ✗ | av_freep(&s->channel_maps); | |
| 435 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 436 | ✗ | goto fail; | |
| 437 | } | ||
| 438 | |||
| 439 | /* check that we did not see this index yet */ | ||
| 440 | 279 | map->copy = 0; | |
| 441 |
2/2✓ Branch 0 taken 279 times.
✓ Branch 1 taken 279 times.
|
558 | for (j = 0; j < i; j++) |
| 442 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 279 times.
|
279 | if (channel_map[channel_reorder(channels, j)] == idx) { |
| 443 | ✗ | map->copy = 1; | |
| 444 | ✗ | map->copy_idx = j; | |
| 445 | ✗ | break; | |
| 446 | } | ||
| 447 | |||
| 448 |
2/2✓ Branch 0 taken 176 times.
✓ Branch 1 taken 103 times.
|
279 | if (idx < 2 * stereo_streams) { |
| 449 | 176 | map->stream_idx = idx / 2; | |
| 450 | 176 | map->channel_idx = idx & 1; | |
| 451 | } else { | ||
| 452 | 103 | map->stream_idx = idx - stereo_streams; | |
| 453 | 103 | map->channel_idx = 0; | |
| 454 | } | ||
| 455 | } | ||
| 456 | |||
| 457 | 154 | ret = av_channel_layout_copy(&avctx->ch_layout, &layout); | |
| 458 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 154 times.
|
154 | if (ret < 0) |
| 459 | ✗ | goto fail; | |
| 460 | |||
| 461 | 154 | s->nb_streams = streams; | |
| 462 | 154 | s->nb_stereo_streams = stereo_streams; | |
| 463 | |||
| 464 | 154 | return 0; | |
| 465 | ✗ | fail: | |
| 466 | ✗ | av_channel_layout_uninit(&layout); | |
| 467 | ✗ | return ret; | |
| 468 | } | ||
| 469 |