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