* commit 'b70d7a4ac72d23f3448f3b08b770fdf5f57de222': lavc: add a native Opus decoder. Conflicts: Changelog configure libavcodec/version.h Fate tests pass with both avresample as well as swresample based opus decoder, but are disabled (reference files are very large so i want to think a day or 2 about if theres an alternative or if they could be avoided, they also dont match the official samples)
Unified · Split
| @@ -23,6 +23,7 @@ version <next>: · 22 unmodified lines | |||
| 23 | - support for decoding through DXVA2 in ffmpeg | 23 | - support for decoding through DXVA2 in ffmpeg |
| 24 | - libbs2b-based stereo-to-binaural audio filter | 24 | - libbs2b-based stereo-to-binaural audio filter |
| 25 | - libx264 reference frames count limiting depending on level | 25 | - libx264 reference frames count limiting depending on level |
| 26 | - native Opus decoder | ||
| 26 | 27 | ||
| 27 | 28 | ||
| 28 | version 2.2: | 29 | version 2.2: |
| @@ -2110,6 +2110,7 @@ nellymoser_decoder_select="mdct sinewin" · 2109 unmodified lines | |||
| 2110 | nellymoser_encoder_select="audio_frame_queue mdct sinewin" | 2110 | nellymoser_encoder_select="audio_frame_queue mdct sinewin" |
| 2111 | nuv_decoder_select="dsputil lzo" | 2111 | nuv_decoder_select="dsputil lzo" |
| 2112 | on2avc_decoder_select="mdct" | 2112 | on2avc_decoder_select="mdct" |
| 2113 | opus_decoder_deps="avresample" | ||
| 2113 | png_decoder_select="zlib" | 2114 | png_decoder_select="zlib" |
| 2114 | png_encoder_select="dsputil zlib" | 2115 | png_encoder_select="dsputil zlib" |
| 2115 | prores_decoder_select="dsputil" | 2116 | prores_decoder_select="dsputil" |
| @@ -5139,6 +5140,8 @@ enabled subtitles_filter && prepend avfilter_deps "avformat avcodec" · 3023 unmodified lines | |||
| 5139 | 5140 | ||
| 5140 | enabled lavfi_indev && prepend avdevice_deps "avfilter" | 5141 | enabled lavfi_indev && prepend avdevice_deps "avfilter" |
| 5141 | 5142 | ||
| 5143 | enabled opus_decoder && prepend avcodec_deps "avresample" | ||
| 5144 | |||
| 5142 | expand_deps(){ | 5145 | expand_deps(){ |
| 5143 | lib_deps=${1}_deps | 5146 | lib_deps=${1}_deps |
| 5144 | eval "deps=\$$lib_deps" | 5147 | eval "deps=\$$lib_deps" |
| @@ -332,6 +332,9 @@ OBJS-$(CONFIG_NELLYMOSER_DECODER) += nellymoserdec.o nellymoser.o · 331 unmodified lines | |||
| 332 | OBJS-$(CONFIG_NELLYMOSER_ENCODER) += nellymoserenc.o nellymoser.o | 332 | OBJS-$(CONFIG_NELLYMOSER_ENCODER) += nellymoserenc.o nellymoser.o |
| 333 | OBJS-$(CONFIG_NUV_DECODER) += nuv.o rtjpeg.o | 333 | OBJS-$(CONFIG_NUV_DECODER) += nuv.o rtjpeg.o |
| 334 | OBJS-$(CONFIG_ON2AVC_DECODER) += on2avc.o on2avcdata.o | 334 | OBJS-$(CONFIG_ON2AVC_DECODER) += on2avc.o on2avcdata.o |
| 335 | OBJS-$(CONFIG_OPUS_DECODER) += opusdec.o opus.o opus_celt.o \ | ||
| 336 | opus_imdct.o opus_silk.o \ | ||
| 337 | vorbis_data.o | ||
| 335 | OBJS-$(CONFIG_PAF_VIDEO_DECODER) += paf.o | 338 | OBJS-$(CONFIG_PAF_VIDEO_DECODER) += paf.o |
| 336 | OBJS-$(CONFIG_PAF_AUDIO_DECODER) += paf.o | 339 | OBJS-$(CONFIG_PAF_AUDIO_DECODER) += paf.o |
| 337 | OBJS-$(CONFIG_PAM_DECODER) += pnmdec.o pnm.o | 340 | OBJS-$(CONFIG_PAM_DECODER) += pnmdec.o pnm.o |
| @@ -779,6 +782,7 @@ OBJS-$(CONFIG_MPEGAUDIO_PARSER) += mpegaudio_parser.o \ · 441 unmodified lines | |||
| 779 | mpegaudiodecheader.o mpegaudiodata.o | 782 | mpegaudiodecheader.o mpegaudiodata.o |
| 780 | OBJS-$(CONFIG_MPEGVIDEO_PARSER) += mpegvideo_parser.o \ | 783 | OBJS-$(CONFIG_MPEGVIDEO_PARSER) += mpegvideo_parser.o \ |
| 781 | mpeg12.o mpeg12data.o | 784 | mpeg12.o mpeg12data.o |
| 785 | OBJS-$(CONFIG_OPUS_PARSER) += opus_parser.o opus.o vorbis_data.o | ||
| 782 | OBJS-$(CONFIG_PNG_PARSER) += png_parser.o | 786 | OBJS-$(CONFIG_PNG_PARSER) += png_parser.o |
| 783 | OBJS-$(CONFIG_PNM_PARSER) += pnm_parser.o pnm.o | 787 | OBJS-$(CONFIG_PNM_PARSER) += pnm_parser.o pnm.o |
| 784 | OBJS-$(CONFIG_RV30_PARSER) += rv34_parser.o | 788 | OBJS-$(CONFIG_RV30_PARSER) += rv34_parser.o |
| @@ -372,6 +372,7 @@ void avcodec_register_all(void) · 371 unmodified lines | |||
| 372 | REGISTER_DECODER(MPC8, mpc8); | 372 | REGISTER_DECODER(MPC8, mpc8); |
| 373 | REGISTER_ENCDEC (NELLYMOSER, nellymoser); | 373 | REGISTER_ENCDEC (NELLYMOSER, nellymoser); |
| 374 | REGISTER_DECODER(ON2AVC, on2avc); | 374 | REGISTER_DECODER(ON2AVC, on2avc); |
| 375 | REGISTER_DECODER(OPUS, opus); | ||
| 375 | REGISTER_DECODER(PAF_AUDIO, paf_audio); | 376 | REGISTER_DECODER(PAF_AUDIO, paf_audio); |
| 376 | REGISTER_DECODER(QCELP, qcelp); | 377 | REGISTER_DECODER(QCELP, qcelp); |
| 377 | REGISTER_DECODER(QDM2, qdm2); | 378 | REGISTER_DECODER(QDM2, qdm2); |
| @@ -562,6 +563,7 @@ void avcodec_register_all(void) · 184 unmodified lines | |||
| 562 | REGISTER_PARSER(MPEG4VIDEO, mpeg4video); | 563 | REGISTER_PARSER(MPEG4VIDEO, mpeg4video); |
| 563 | REGISTER_PARSER(MPEGAUDIO, mpegaudio); | 564 | REGISTER_PARSER(MPEGAUDIO, mpegaudio); |
| 564 | REGISTER_PARSER(MPEGVIDEO, mpegvideo); | 565 | REGISTER_PARSER(MPEGVIDEO, mpegvideo); |
| 566 | REGISTER_PARSER(OPUS, opus); | ||
| 565 | REGISTER_PARSER(PNG, png); | 567 | REGISTER_PARSER(PNG, png); |
| 566 | REGISTER_PARSER(PNM, pnm); | 568 | REGISTER_PARSER(PNM, pnm); |
| 567 | REGISTER_PARSER(RV30, rv30); | 569 | REGISTER_PARSER(RV30, rv30); |
| @@ -0,0 +1,428 @@ | |||
| 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 <stdint.h> | ||
| 28 | |||
| 29 | #include "libavutil/error.h" | ||
| 30 | |||
| 31 | #include "opus.h" | ||
| 32 | #include "vorbis.h" | ||
| 33 | |||
| 34 | static const uint16_t opus_frame_duration[32] = { | ||
| 35 | 480, 960, 1920, 2880, | ||
| 36 | 480, 960, 1920, 2880, | ||
| 37 | 480, 960, 1920, 2880, | ||
| 38 | 480, 960, | ||
| 39 | 480, 960, | ||
| 40 | 120, 240, 480, 960, | ||
| 41 | 120, 240, 480, 960, | ||
| 42 | 120, 240, 480, 960, | ||
| 43 | 120, 240, 480, 960, | ||
| 44 | }; | ||
| 45 | |||
| 46 | /** | ||
| 47 | * Read a 1- or 2-byte frame length | ||
| 48 | */ | ||
| 49 | static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end) | ||
| 50 | { | ||
| 51 | int val; | ||
| 52 | |||
| 53 | if (*ptr >= end) | ||
| 54 | return AVERROR_INVALIDDATA; | ||
| 55 | val = *(*ptr)++; | ||
| 56 | if (val >= 252) { | ||
| 57 | if (*ptr >= end) | ||
| 58 | return AVERROR_INVALIDDATA; | ||
| 59 | val += 4 * *(*ptr)++; | ||
| 60 | } | ||
| 61 | return val; | ||
| 62 | } | ||
| 63 | |||
| 64 | /** | ||
| 65 | * Read a multi-byte length (used for code 3 packet padding size) | ||
| 66 | */ | ||
| 67 | static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end) | ||
| 68 | { | ||
| 69 | int val = 0; | ||
| 70 | int next; | ||
| 71 | |||
| 72 | while (1) { | ||
| 73 | if (*ptr >= end || val > INT_MAX - 254) | ||
| 74 | return AVERROR_INVALIDDATA; | ||
| 75 | next = *(*ptr)++; | ||
| 76 | val += next; | ||
| 77 | if (next < 255) | ||
| 78 | break; | ||
| 79 | else | ||
| 80 | val--; | ||
| 81 | } | ||
| 82 | return val; | ||
| 83 | } | ||
| 84 | |||
| 85 | /** | ||
| 86 | * Parse Opus packet info from raw packet data | ||
| 87 | */ | ||
| 88 | int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, | ||
| 89 | int self_delimiting) | ||
| 90 | { | ||
| 91 | const uint8_t *ptr = buf; | ||
| 92 | const uint8_t *end = buf + buf_size; | ||
| 93 | int padding = 0; | ||
| 94 | int frame_bytes, i; | ||
| 95 | |||
| 96 | if (buf_size < 1) | ||
| 97 | goto fail; | ||
| 98 | |||
| 99 | /* TOC byte */ | ||
| 100 | i = *ptr++; | ||
| 101 | pkt->code = (i ) & 0x3; | ||
| 102 | pkt->stereo = (i >> 2) & 0x1; | ||
| 103 | pkt->config = (i >> 3) & 0x1F; | ||
| 104 | |||
| 105 | /* code 2 and code 3 packets have at least 1 byte after the TOC */ | ||
| 106 | if (pkt->code >= 2 && buf_size < 2) | ||
| 107 | goto fail; | ||
| 108 | |||
| 109 | switch (pkt->code) { | ||
| 110 | case 0: | ||
| 111 | /* 1 frame */ | ||
| 112 | pkt->frame_count = 1; | ||
| 113 | pkt->vbr = 0; | ||
| 114 | |||
| 115 | if (self_delimiting) { | ||
| 116 | int len = xiph_lacing_16bit(&ptr, end); | ||
| 117 | if (len < 0 || len > end - ptr) | ||
| 118 | goto fail; | ||
| 119 | end = ptr + len; | ||
| 120 | buf_size = end - buf; | ||
| 121 | } | ||
| 122 | |||
| 123 | frame_bytes = end - ptr; | ||
| 124 | if (frame_bytes > MAX_FRAME_SIZE) | ||
| 125 | goto fail; | ||
| 126 | pkt->frame_offset[0] = ptr - buf; | ||
| 127 | pkt->frame_size[0] = frame_bytes; | ||
| 128 | break; | ||
| 129 | case 1: | ||
| 130 | /* 2 frames, equal size */ | ||
| 131 | pkt->frame_count = 2; | ||
| 132 | pkt->vbr = 0; | ||
| 133 | |||
| 134 | if (self_delimiting) { | ||
| 135 | int len = xiph_lacing_16bit(&ptr, end); | ||
| 136 | if (len < 0 || 2 * len > end - ptr) | ||
| 137 | goto fail; | ||
| 138 | end = ptr + 2 * len; | ||
| 139 | buf_size = end - buf; | ||
| 140 | } | ||
| 141 | |||
| 142 | frame_bytes = end - ptr; | ||
| 143 | if (frame_bytes & 1 || frame_bytes >> 1 > MAX_FRAME_SIZE) | ||
| 144 | goto fail; | ||
| 145 | pkt->frame_offset[0] = ptr - buf; | ||
| 146 | pkt->frame_size[0] = frame_bytes >> 1; | ||
| 147 | pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0]; | ||
| 148 | pkt->frame_size[1] = frame_bytes >> 1; | ||
| 149 | break; | ||
| 150 | case 2: | ||
| 151 | /* 2 frames, different sizes */ | ||
| 152 | pkt->frame_count = 2; | ||
| 153 | pkt->vbr = 1; | ||
| 154 | |||
| 155 | /* read 1st frame size */ | ||
| 156 | frame_bytes = xiph_lacing_16bit(&ptr, end); | ||
| 157 | if (frame_bytes < 0) | ||
| 158 | goto fail; | ||
| 159 | |||
| 160 | if (self_delimiting) { | ||
| 161 | int len = xiph_lacing_16bit(&ptr, end); | ||
| 162 | if (len < 0 || len + frame_bytes > end - ptr) | ||
| 163 | goto fail; | ||
| 164 | end = ptr + frame_bytes + len; | ||
| 165 | buf_size = end - buf; | ||
| 166 | } | ||
| 167 | |||
| 168 | pkt->frame_offset[0] = ptr - buf; | ||
| 169 | pkt->frame_size[0] = frame_bytes; | ||
| 170 | |||
| 171 | /* calculate 2nd frame size */ | ||
| 172 | frame_bytes = end - ptr - pkt->frame_size[0]; | ||
| 173 | if (frame_bytes < 0 || frame_bytes > MAX_FRAME_SIZE) | ||
| 174 | goto fail; | ||
| 175 | pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0]; | ||
| 176 | pkt->frame_size[1] = frame_bytes; | ||
| 177 | break; | ||
| 178 | case 3: | ||
| 179 | /* 1 to 48 frames, can be different sizes */ | ||
| 180 | i = *ptr++; | ||
| 181 | pkt->frame_count = (i ) & 0x3F; | ||
| 182 | padding = (i >> 6) & 0x01; | ||
| 183 | pkt->vbr = (i >> 7) & 0x01; | ||
| 184 | |||
| 185 | if (pkt->frame_count == 0 || pkt->frame_count > MAX_FRAMES) | ||
| 186 | goto fail; | ||
| 187 | |||
| 188 | /* read padding size */ | ||
| 189 | if (padding) { | ||
| 190 | padding = xiph_lacing_full(&ptr, end); | ||
| 191 | if (padding < 0) | ||
| 192 | goto fail; | ||
| 193 | } | ||
| 194 | |||
| 195 | /* read frame sizes */ | ||
| 196 | if (pkt->vbr) { | ||
| 197 | /* for VBR, all frames except the final one have their size coded | ||
| 198 | in the bitstream. the last frame size is implicit. */ | ||
| 199 | int total_bytes = 0; | ||
| 200 | for (i = 0; i < pkt->frame_count - 1; i++) { | ||
| 201 | frame_bytes = xiph_lacing_16bit(&ptr, end); | ||
| 202 | if (frame_bytes < 0) | ||
| 203 | goto fail; | ||
| 204 | pkt->frame_size[i] = frame_bytes; | ||
| 205 | total_bytes += frame_bytes; | ||
| 206 | } | ||
| 207 | |||
| 208 | if (self_delimiting) { | ||
| 209 | int len = xiph_lacing_16bit(&ptr, end); | ||
| 210 | if (len < 0 || len + total_bytes + padding > end - ptr) | ||
| 211 | goto fail; | ||
| 212 | end = ptr + total_bytes + len + padding; | ||
| 213 | buf_size = end - buf; | ||
| 214 | } | ||
| 215 | |||
| 216 | frame_bytes = end - ptr - padding; | ||
| 217 | if (total_bytes > frame_bytes) | ||
| 218 | goto fail; | ||
| 219 | pkt->frame_offset[0] = ptr - buf; | ||
| 220 | for (i = 1; i < pkt->frame_count; i++) | ||
| 221 | pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1]; | ||
| 222 | pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes; | ||
| 223 | } else { | ||
| 224 | /* for CBR, the remaining packet bytes are divided evenly between | ||
| 225 | the frames */ | ||
| 226 | if (self_delimiting) { | ||
| 227 | frame_bytes = xiph_lacing_16bit(&ptr, end); | ||
| 228 | if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr) | ||
| 229 | goto fail; | ||
| 230 | end = ptr + pkt->frame_count * frame_bytes + padding; | ||
| 231 | buf_size = end - buf; | ||
| 232 | } else { | ||
| 233 | frame_bytes = end - ptr - padding; | ||
| 234 | if (frame_bytes % pkt->frame_count || | ||
| 235 | frame_bytes / pkt->frame_count > MAX_FRAME_SIZE) | ||
| 236 | goto fail; | ||
| 237 | frame_bytes /= pkt->frame_count; | ||
| 238 | } | ||
| 239 | |||
| 240 | pkt->frame_offset[0] = ptr - buf; | ||
| 241 | pkt->frame_size[0] = frame_bytes; | ||
| 242 | for (i = 1; i < pkt->frame_count; i++) { | ||
| 243 | pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1]; | ||
| 244 | pkt->frame_size[i] = frame_bytes; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | pkt->packet_size = buf_size; | ||
| 250 | pkt->data_size = pkt->packet_size - padding; | ||
| 251 | |||
| 252 | /* total packet duration cannot be larger than 120ms */ | ||
| 253 | pkt->frame_duration = opus_frame_duration[pkt->config]; | ||
| 254 | if (pkt->frame_duration * pkt->frame_count > MAX_PACKET_DUR) | ||
| 255 | goto fail; | ||
| 256 | |||
| 257 | /* set mode and bandwidth */ | ||
| 258 | if (pkt->config < 12) { | ||
| 259 | pkt->mode = OPUS_MODE_SILK; | ||
| 260 | pkt->bandwidth = pkt->config >> 2; | ||
| 261 | } else if (pkt->config < 16) { | ||
| 262 | pkt->mode = OPUS_MODE_HYBRID; | ||
| 263 | pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14); | ||
| 264 | } else { | ||
| 265 | pkt->mode = OPUS_MODE_CELT; | ||
| 266 | pkt->bandwidth = (pkt->config - 16) >> 2; | ||
| 267 | /* skip mediumband */ | ||
| 268 | if (pkt->bandwidth) | ||
| 269 | pkt->bandwidth++; | ||
| 270 | } | ||
| 271 | |||
| 272 | return 0; | ||
| 273 | |||
| 274 | fail: | ||
| 275 | memset(pkt, 0, sizeof(*pkt)); | ||
| 276 | return AVERROR_INVALIDDATA; | ||
| 277 | } | ||
| 278 | |||
| 279 | static int channel_reorder_vorbis(int nb_channels, int channel_idx) | ||
| 280 | { | ||
| 281 | return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx]; | ||
| 282 | } | ||
| 283 | |||
| 284 | static int channel_reorder_unknown(int nb_channels, int channel_idx) | ||
| 285 | { | ||
| 286 | return channel_idx; | ||
| 287 | } | ||
| 288 | |||
| 289 | av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, | ||
| 290 | OpusContext *s) | ||
| 291 | { | ||
| 292 | static const uint8_t default_channel_map[2] = { 0, 1 }; | ||
| 293 | uint8_t default_extradata[19] = { | ||
| 294 | 'O', 'p', 'u', 's', 'H', 'e', 'a', 'd', | ||
| 295 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 296 | }; | ||
| 297 | |||
| 298 | int (*channel_reorder)(int, int) = channel_reorder_unknown; | ||
| 299 | |||
| 300 | const uint8_t *extradata, *channel_map; | ||
| 301 | int extradata_size; | ||
| 302 | int version, channels, map_type, streams, stereo_streams, i, j; | ||
| 303 | uint64_t layout; | ||
| 304 | |||
| 305 | if (!avctx->extradata) { | ||
| 306 | if (avctx->channels > 2) { | ||
| 307 | av_log(avctx, AV_LOG_ERROR, | ||
| 308 | "Multichannel configuration without extradata.\n"); | ||
| 309 | return AVERROR(EINVAL); | ||
| 310 | } | ||
| 311 | default_extradata[9] = (avctx->channels == 1) ? 1 : 2; | ||
| 312 | extradata = default_extradata; | ||
| 313 | extradata_size = sizeof(default_extradata); | ||
| 314 | } else { | ||
| 315 | extradata = avctx->extradata; | ||
| 316 | extradata_size = avctx->extradata_size; | ||
| 317 | } | ||
| 318 | |||
| 319 | if (extradata_size < 19) { | ||
| 320 | av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n", | ||
| 321 | extradata_size); | ||
| 322 | return AVERROR_INVALIDDATA; | ||
| 323 | } | ||
| 324 | |||
| 325 | version = extradata[8]; | ||
| 326 | if (version > 15) { | ||
| 327 | avpriv_request_sample(avctx, "Extradata version %d", version); | ||
| 328 | return AVERROR_PATCHWELCOME; | ||
| 329 | } | ||
| 330 | |||
| 331 | avctx->delay = AV_RL16(extradata + 10); | ||
| 332 | |||
| 333 | channels = extradata[9]; | ||
| 334 | if (!channels) { | ||
| 335 | av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extadata\n"); | ||
| 336 | return AVERROR_INVALIDDATA; | ||
| 337 | } | ||
| 338 | |||
| 339 | s->gain_i = AV_RL16(extradata + 16); | ||
| 340 | if (s->gain_i) | ||
| 341 | s->gain = pow(10, s->gain_i / (20.0 * 256)); | ||
| 342 | |||
| 343 | map_type = extradata[18]; | ||
| 344 | if (!map_type) { | ||
| 345 | if (channels > 2) { | ||
| 346 | av_log(avctx, AV_LOG_ERROR, | ||
| 347 | "Channel mapping 0 is only specified for up to 2 channels\n"); | ||
| 348 | return AVERROR_INVALIDDATA; | ||
| 349 | } | ||
| 350 | layout = (channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO; | ||
| 351 | streams = 1; | ||
| 352 | stereo_streams = channels - 1; | ||
| 353 | channel_map = default_channel_map; | ||
| 354 | } else if (map_type == 1 || map_type == 255) { | ||
| 355 | if (extradata_size < 21 + channels) { | ||
| 356 | av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n", | ||
| 357 | extradata_size); | ||
| 358 | return AVERROR_INVALIDDATA; | ||
| 359 | } | ||
| 360 | |||
| 361 | streams = extradata[19]; | ||
| 362 | stereo_streams = extradata[20]; | ||
| 363 | if (!streams || stereo_streams > streams || | ||
| 364 | streams + stereo_streams > 255) { | ||
| 365 | av_log(avctx, AV_LOG_ERROR, | ||
| 366 | "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams); | ||
| 367 | return AVERROR_INVALIDDATA; | ||
| 368 | } | ||
| 369 | |||
| 370 | if (map_type == 1) { | ||
| 371 | if (channels > 8) { | ||
| 372 | av_log(avctx, AV_LOG_ERROR, | ||
| 373 | "Channel mapping 1 is only specified for up to 8 channels\n"); | ||
| 374 | return AVERROR_INVALIDDATA; | ||
| 375 | } | ||
| 376 | layout = ff_vorbis_channel_layouts[channels - 1]; | ||
| 377 | channel_reorder = channel_reorder_vorbis; | ||
| 378 | } else | ||
| 379 | layout = 0; | ||
| 380 | |||
| 381 | channel_map = extradata + 21; | ||
| 382 | } else { | ||
| 383 | avpriv_request_sample(avctx, "Mapping type %d", map_type); | ||
| 384 | return AVERROR_PATCHWELCOME; | ||
| 385 | } | ||
| 386 | |||
| 387 | s->channel_maps = av_mallocz_array(channels, sizeof(*s->channel_maps)); | ||
| 388 | if (!s->channel_maps) | ||
| 389 | return AVERROR(ENOMEM); | ||
| 390 | |||
| 391 | for (i = 0; i < channels; i++) { | ||
| 392 | ChannelMap *map = &s->channel_maps[i]; | ||
| 393 | uint8_t idx = channel_map[channel_reorder(channels, i)]; | ||
| 394 | |||
| 395 | if (idx == 255) { | ||
| 396 | map->silence = 1; | ||
| 397 | continue; | ||
| 398 | } else if (idx >= streams + stereo_streams) { | ||
| 399 | av_log(avctx, AV_LOG_ERROR, | ||
| 400 | "Invalid channel map for output channel %d: %d\n", i, idx); | ||
| 401 | return AVERROR_INVALIDDATA; | ||
| 402 | } | ||
| 403 | |||
| 404 | /* check that we din't see this index yet */ | ||
| 405 | map->copy = 0; | ||
| 406 | for (j = 0; j < i; j++) | ||
| 407 | if (channel_map[channel_reorder(channels, j)] == idx) { | ||
| 408 | map->copy = 1; | ||
| 409 | map->copy_idx = j; | ||
| 410 | break; | ||
| 411 | } | ||
| 412 | |||
| 413 | if (idx < 2 * stereo_streams) { | ||
| 414 | map->stream_idx = idx / 2; | ||
| 415 | map->channel_idx = idx & 1; | ||
| 416 | } else { | ||
| 417 | map->stream_idx = idx - stereo_streams; | ||
| 418 | map->channel_idx = 0; | ||
| 419 | } | ||
| 420 | } | ||
| 421 | |||
| 422 | avctx->channels = channels; | ||
| 423 | avctx->channel_layout = layout; | ||
| 424 | s->nb_streams = streams; | ||
| 425 | s->nb_stereo_streams = stereo_streams; | ||
| 426 | |||
| 427 | return 0; | ||
| 428 | } | ||
| @@ -0,0 +1,429 @@ | |||
| 1 | /* | ||
| 2 | * Opus decoder/demuxer common functions | ||
| 3 | * Copyright (c) 2012 Andrew D'Addesio | ||
| 4 | * Copyright (c) 2013-2014 Mozilla Corporation | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #ifndef AVCODEC_OPUS_H | ||
| 24 | #define AVCODEC_OPUS_H | ||
| 25 | |||
| 26 | #include <stdint.h> | ||
| 27 | |||
| 28 | #include "libavutil/audio_fifo.h" | ||
| 29 | #include "libavutil/float_dsp.h" | ||
| 30 | #include "libavutil/frame.h" | ||
| 31 | |||
| 32 | #include "libavresample/avresample.h" | ||
| 33 | |||
| 34 | #include "avcodec.h" | ||
| 35 | #include "get_bits.h" | ||
| 36 | |||
| 37 | #define MAX_FRAME_SIZE 1275 | ||
| 38 | #define MAX_FRAMES 48 | ||
| 39 | #define MAX_PACKET_DUR 5760 | ||
| 40 | |||
| 41 | #define CELT_SHORT_BLOCKSIZE 120 | ||
| 42 | #define CELT_OVERLAP CELT_SHORT_BLOCKSIZE | ||
| 43 | #define CELT_MAX_LOG_BLOCKS 3 | ||
| 44 | #define CELT_MAX_FRAME_SIZE (CELT_SHORT_BLOCKSIZE * (1 << CELT_MAX_LOG_BLOCKS)) | ||
| 45 | #define CELT_MAX_BANDS 21 | ||
| 46 | #define CELT_VECTORS 11 | ||
| 47 | #define CELT_ALLOC_STEPS 6 | ||
| 48 | #define CELT_FINE_OFFSET 21 | ||
| 49 | #define CELT_MAX_FINE_BITS 8 | ||
| 50 | #define CELT_NORM_SCALE 16384 | ||
| 51 | #define CELT_QTHETA_OFFSET 4 | ||
| 52 | #define CELT_QTHETA_OFFSET_TWOPHASE 16 | ||
| 53 | #define CELT_DEEMPH_COEFF 0.85000610f | ||
| 54 | #define CELT_POSTFILTER_MINPERIOD 15 | ||
| 55 | #define CELT_ENERGY_SILENCE (-28.0f) | ||
| 56 | |||
| 57 | #define SILK_HISTORY 322 | ||
| 58 | #define SILK_MAX_LPC 16 | ||
| 59 | |||
| 60 | #define ROUND_MULL(a,b,s) (((MUL64(a, b) >> (s - 1)) + 1) >> 1) | ||
| 61 | #define ROUND_MUL16(a,b) ((MUL16(a, b) + 16384) >> 15) | ||
| 62 | #define opus_ilog(i) (av_log2(i) + !!(i)) | ||
| 63 | |||
| 64 | enum OpusMode { | ||
| 65 | OPUS_MODE_SILK, | ||
| 66 | OPUS_MODE_HYBRID, | ||
| 67 | OPUS_MODE_CELT | ||
| 68 | }; | ||
| 69 | |||
| 70 | enum OpusBandwidth { | ||
| 71 | OPUS_BANDWIDTH_NARROWBAND, | ||
| 72 | OPUS_BANDWIDTH_MEDIUMBAND, | ||
| 73 | OPUS_BANDWIDTH_WIDEBAND, | ||
| 74 | OPUS_BANDWIDTH_SUPERWIDEBAND, | ||
| 75 | OPUS_BANDWIDTH_FULLBAND | ||
| 76 | }; | ||
| 77 | |||
| 78 | typedef struct RawBitsContext { | ||
| 79 | const uint8_t *position; | ||
| 80 | unsigned int bytes; | ||
| 81 | unsigned int cachelen; | ||
| 82 | unsigned int cacheval; | ||
| 83 | } RawBitsContext; | ||
| 84 | |||
| 85 | typedef struct OpusRangeCoder { | ||
| 86 | GetBitContext gb; | ||
| 87 | RawBitsContext rb; | ||
| 88 | unsigned int range; | ||
| 89 | unsigned int value; | ||
| 90 | unsigned int total_read_bits; | ||
| 91 | } OpusRangeCoder; | ||
| 92 | |||
| 93 | typedef struct SilkContext SilkContext; | ||
| 94 | |||
| 95 | typedef struct CeltIMDCTContext CeltIMDCTContext; | ||
| 96 | |||
| 97 | typedef struct CeltContext CeltContext; | ||
| 98 | |||
| 99 | typedef struct OpusPacket { | ||
| 100 | int packet_size; /** packet size */ | ||
| 101 | int data_size; /** size of the useful data -- packet size - padding */ | ||
| 102 | int code; /** packet code: specifies the frame layout */ | ||
| 103 | int stereo; /** whether this packet is mono or stereo */ | ||
| 104 | int vbr; /** vbr flag */ | ||
| 105 | int config; /** configuration: tells the audio mode, | ||
| 106 | ** bandwidth, and frame duration */ | ||
| 107 | int frame_count; /** frame count */ | ||
| 108 | int frame_offset[MAX_FRAMES]; /** frame offsets */ | ||
| 109 | int frame_size[MAX_FRAMES]; /** frame sizes */ | ||
| 110 | int frame_duration; /** frame duration, in samples @ 48kHz */ | ||
| 111 | enum OpusMode mode; /** mode */ | ||
| 112 | enum OpusBandwidth bandwidth; /** bandwidth */ | ||
| 113 | } OpusPacket; | ||
| 114 | |||
| 115 | typedef struct OpusStreamContext { | ||
| 116 | AVCodecContext *avctx; | ||
| 117 | int output_channels; | ||
| 118 | |||
| 119 | OpusRangeCoder rc; | ||
| 120 | OpusRangeCoder redundancy_rc; | ||
| 121 | SilkContext *silk; | ||
| 122 | CeltContext *celt; | ||
| 123 | AVFloatDSPContext *fdsp; | ||
| 124 | |||
| 125 | float silk_buf[2][960]; | ||
| 126 | float *silk_output[2]; | ||
| 127 | DECLARE_ALIGNED(32, float, celt_buf)[2][960]; | ||
| 128 | float *celt_output[2]; | ||
| 129 | |||
| 130 | float redundancy_buf[2][960]; | ||
| 131 | float *redundancy_output[2]; | ||
| 132 | |||
| 133 | /* data buffers for the final output data */ | ||
| 134 | float *out[2]; | ||
| 135 | int out_size; | ||
| 136 | |||
| 137 | float *out_dummy; | ||
| 138 | int out_dummy_allocated_size; | ||
| 139 | |||
| 140 | AVAudioResampleContext *avr; | ||
| 141 | AVAudioFifo *celt_delay; | ||
| 142 | int silk_samplerate; | ||
| 143 | /* number of samples we still want to get from the resampler */ | ||
| 144 | int delayed_samples; | ||
| 145 | |||
| 146 | OpusPacket packet; | ||
| 147 | |||
| 148 | int redundancy_idx; | ||
| 149 | } OpusStreamContext; | ||
| 150 | |||
| 151 | // a mapping between an opus stream and an output channel | ||
| 152 | typedef struct ChannelMap { | ||
| 153 | int stream_idx; | ||
| 154 | int channel_idx; | ||
| 155 | |||
| 156 | // when a single decoded channel is mapped to multiple output channels, we | ||
| 157 | // write to the first output directly and copy from it to the others | ||
| 158 | // this field is set to 1 for those copied output channels | ||
| 159 | int copy; | ||
| 160 | // this is the index of the output channel to copy from | ||
| 161 | int copy_idx; | ||
| 162 | |||
| 163 | // this channel is silent | ||
| 164 | int silence; | ||
| 165 | } ChannelMap; | ||
| 166 | |||
| 167 | typedef struct OpusContext { | ||
| 168 | OpusStreamContext *streams; | ||
| 169 | int nb_streams; | ||
| 170 | int nb_stereo_streams; | ||
| 171 | |||
| 172 | AVFloatDSPContext fdsp; | ||
| 173 | int16_t gain_i; | ||
| 174 | float gain; | ||
| 175 | |||
| 176 | ChannelMap *channel_maps; | ||
| 177 | } OpusContext; | ||
| 178 | |||
| 179 | static av_always_inline void opus_rc_normalize(OpusRangeCoder *rc) | ||
| 180 | { | ||
| 181 | while (rc->range <= 1<<23) { | ||
| 182 | rc->value = ((rc->value << 8) | (get_bits(&rc->gb, 8) ^ 0xFF)) & ((1u << 31) - 1); | ||
| 183 | rc->range <<= 8; | ||
| 184 | rc->total_read_bits += 8; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | static av_always_inline void opus_rc_update(OpusRangeCoder *rc, unsigned int scale, | ||
| 189 | unsigned int low, unsigned int high, | ||
| 190 | unsigned int total) | ||
| 191 | { | ||
| 192 | rc->value -= scale * (total - high); | ||
| 193 | rc->range = low ? scale * (high - low) | ||
| 194 | : rc->range - scale * (total - high); | ||
| 195 | opus_rc_normalize(rc); | ||
| 196 | } | ||
| 197 | |||
| 198 | static av_always_inline unsigned int opus_rc_getsymbol(OpusRangeCoder *rc, const uint16_t *cdf) | ||
| 199 | { | ||
| 200 | unsigned int k, scale, total, symbol, low, high; | ||
| 201 | |||
| 202 | total = *cdf++; | ||
| 203 | |||
| 204 | scale = rc->range / total; | ||
| 205 | symbol = rc->value / scale + 1; | ||
| 206 | symbol = total - FFMIN(symbol, total); | ||
| 207 | |||
| 208 | for (k = 0; cdf[k] <= symbol; k++); | ||
| 209 | high = cdf[k]; | ||
| 210 | low = k ? cdf[k-1] : 0; | ||
| 211 | |||
| 212 | opus_rc_update(rc, scale, low, high, total); | ||
| 213 | |||
| 214 | return k; | ||
| 215 | } | ||
| 216 | |||
| 217 | static av_always_inline unsigned int opus_rc_p2model(OpusRangeCoder *rc, unsigned int bits) | ||
| 218 | { | ||
| 219 | unsigned int k, scale; | ||
| 220 | scale = rc->range >> bits; // in this case, scale = symbol | ||
| 221 | |||
| 222 | if (rc->value >= scale) { | ||
| 223 | rc->value -= scale; | ||
| 224 | rc->range -= scale; | ||
| 225 | k = 0; | ||
| 226 | } else { | ||
| 227 | rc->range = scale; | ||
| 228 | k = 1; | ||
| 229 | } | ||
| 230 | opus_rc_normalize(rc); | ||
| 231 | return k; | ||
| 232 | } | ||
| 233 | |||
| 234 | /** | ||
| 235 | * CELT: estimate bits of entropy that have thus far been consumed for the | ||
| 236 | * current CELT frame, to integer and fractional (1/8th bit) precision | ||
| 237 | */ | ||
| 238 | static av_always_inline unsigned int opus_rc_tell(const OpusRangeCoder *rc) | ||
| 239 | { | ||
| 240 | return rc->total_read_bits - av_log2(rc->range) - 1; | ||
| 241 | } | ||
| 242 | |||
| 243 | static av_always_inline unsigned int opus_rc_tell_frac(const OpusRangeCoder *rc) | ||
| 244 | { | ||
| 245 | unsigned int i, total_bits, rcbuffer, range; | ||
| 246 | |||
| 247 | total_bits = rc->total_read_bits << 3; | ||
| 248 | rcbuffer = av_log2(rc->range) + 1; | ||
| 249 | range = rc->range >> (rcbuffer-16); | ||
| 250 | |||
| 251 | for (i = 0; i < 3; i++) { | ||
| 252 | int bit; | ||
| 253 | range = range * range >> 15; | ||
| 254 | bit = range >> 16; | ||
| 255 | rcbuffer = rcbuffer << 1 | bit; | ||
| 256 | range >>= bit; | ||
| 257 | } | ||
| 258 | |||
| 259 | return total_bits - rcbuffer; | ||
| 260 | } | ||
| 261 | |||
| 262 | /** | ||
| 263 | * CELT: read 1-25 raw bits at the end of the frame, backwards byte-wise | ||
| 264 | */ | ||
| 265 | static av_always_inline unsigned int opus_getrawbits(OpusRangeCoder *rc, unsigned int count) | ||
| 266 | { | ||
| 267 | unsigned int value = 0; | ||
| 268 | |||
| 269 | while (rc->rb.bytes && rc->rb.cachelen < count) { | ||
| 270 | rc->rb.cacheval |= *--rc->rb.position << rc->rb.cachelen; | ||
| 271 | rc->rb.cachelen += 8; | ||
| 272 | rc->rb.bytes--; | ||
| 273 | } | ||
| 274 | |||
| 275 | value = rc->rb.cacheval & ((1<<count)-1); | ||
| 276 | rc->rb.cacheval >>= count; | ||
| 277 | rc->rb.cachelen -= count; | ||
| 278 | rc->total_read_bits += count; | ||
| 279 | |||
| 280 | return value; | ||
| 281 | } | ||
| 282 | |||
| 283 | /** | ||
| 284 | * CELT: read a uniform distribution | ||
| 285 | */ | ||
| 286 | static av_always_inline unsigned int opus_rc_unimodel(OpusRangeCoder *rc, unsigned int size) | ||
| 287 | { | ||
| 288 | unsigned int bits, k, scale, total; | ||
| 289 | |||
| 290 | bits = opus_ilog(size - 1); | ||
| 291 | total = (bits > 8) ? ((size - 1) >> (bits - 8)) + 1 : size; | ||
| 292 | |||
| 293 | scale = rc->range / total; | ||
| 294 | k = rc->value / scale + 1; | ||
| 295 | k = total - FFMIN(k, total); | ||
| 296 | opus_rc_update(rc, scale, k, k + 1, total); | ||
| 297 | |||
| 298 | if (bits > 8) { | ||
| 299 | k = k << (bits - 8) | opus_getrawbits(rc, bits - 8); | ||
| 300 | return FFMIN(k, size - 1); | ||
| 301 | } else | ||
| 302 | return k; | ||
| 303 | } | ||
| 304 | |||
| 305 | static av_always_inline int opus_rc_laplace(OpusRangeCoder *rc, unsigned int symbol, int decay) | ||
| 306 | { | ||
| 307 | /* extends the range coder to model a Laplace distribution */ | ||
| 308 | int value = 0; | ||
| 309 | unsigned int scale, low = 0, center; | ||
| 310 | |||
| 311 | scale = rc->range >> 15; | ||
| 312 | center = rc->value / scale + 1; | ||
| 313 | center = (1 << 15) - FFMIN(center, 1 << 15); | ||
| 314 | |||
| 315 | if (center >= symbol) { | ||
| 316 | value++; | ||
| 317 | low = symbol; | ||
| 318 | symbol = 1 + ((32768 - 32 - symbol) * (16384-decay) >> 15); | ||
| 319 | |||
| 320 | while (symbol > 1 && center >= low + 2 * symbol) { | ||
| 321 | value++; | ||
| 322 | symbol *= 2; | ||
| 323 | low += symbol; | ||
| 324 | symbol = (((symbol - 2) * decay) >> 15) + 1; | ||
| 325 | } | ||
| 326 | |||
| 327 | if (symbol <= 1) { | ||
| 328 | int distance = (center - low) >> 1; | ||
| 329 | value += distance; | ||
| 330 | low += 2 * distance; | ||
| 331 | } | ||
| 332 | |||
| 333 | if (center < low + symbol) | ||
| 334 | value *= -1; | ||
| 335 | else | ||
| 336 | low += symbol; | ||
| 337 | } | ||
| 338 | |||
| 339 | opus_rc_update(rc, scale, low, FFMIN(low + symbol, 32768), 32768); | ||
| 340 | |||
| 341 | return value; | ||
| 342 | } | ||
| 343 | |||
| 344 | static av_always_inline unsigned int opus_rc_stepmodel(OpusRangeCoder *rc, int k0) | ||
| 345 | { | ||
| 346 | /* Use a probability of 3 up to itheta=8192 and then use 1 after */ | ||
| 347 | unsigned int k, scale, symbol, total = (k0+1)*3 + k0; | ||
| 348 | scale = rc->range / total; | ||
| 349 | symbol = rc->value / scale + 1; | ||
| 350 | symbol = total - FFMIN(symbol, total); | ||
| 351 | |||
| 352 | k = (symbol < (k0+1)*3) ? symbol/3 : symbol - (k0+1)*2; | ||
| 353 | |||
| 354 | opus_rc_update(rc, scale, (k <= k0) ? 3*(k+0) : (k-1-k0) + 3*(k0+1), | ||
| 355 | (k <= k0) ? 3*(k+1) : (k-0-k0) + 3*(k0+1), total); | ||
| 356 | return k; | ||
| 357 | } | ||
| 358 | |||
| 359 | static av_always_inline unsigned int opus_rc_trimodel(OpusRangeCoder *rc, int qn) | ||
| 360 | { | ||
| 361 | unsigned int k, scale, symbol, total, low, center; | ||
| 362 | |||
| 363 | total = ((qn>>1) + 1) * ((qn>>1) + 1); | ||
| 364 | scale = rc->range / total; | ||
| 365 | center = rc->value / scale + 1; | ||
| 366 | center = total - FFMIN(center, total); | ||
| 367 | |||
| 368 | if (center < total >> 1) { | ||
| 369 | k = (ff_sqrt(8 * center + 1) - 1) >> 1; | ||
| 370 | low = k * (k + 1) >> 1; | ||
| 371 | symbol = k + 1; | ||
| 372 | } else { | ||
| 373 | k = (2*(qn + 1) - ff_sqrt(8*(total - center - 1) + 1)) >> 1; | ||
| 374 | low = total - ((qn + 1 - k) * (qn + 2 - k) >> 1); | ||
| 375 | symbol = qn + 1 - k; | ||
| 376 | } | ||
| 377 | |||
| 378 | opus_rc_update(rc, scale, low, low + symbol, total); | ||
| 379 | |||
| 380 | return k; | ||
| 381 | } | ||
| 382 | |||
| 383 | int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, | ||
| 384 | int self_delimited); | ||
| 385 | |||
| 386 | int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s); | ||
| 387 | |||
| 388 | int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels); | ||
| 389 | void ff_silk_free(SilkContext **ps); | ||
| 390 | void ff_silk_flush(SilkContext *s); | ||
| 391 | |||
| 392 | /** | ||
| 393 | * Decode the LP layer of one Opus frame (which may correspond to several SILK | ||
| 394 | * frames). | ||
| 395 | */ | ||
| 396 | int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc, | ||
| 397 | float *output[2], | ||
| 398 | enum OpusBandwidth bandwidth, int coded_channels, | ||
| 399 | int duration_ms); | ||
| 400 | |||
| 401 | /** | ||
| 402 | * Init an iMDCT of the length 2 * 15 * (2^N) | ||
| 403 | */ | ||
| 404 | int ff_celt_imdct_init(CeltIMDCTContext **s, int N); | ||
| 405 | |||
| 406 | /** | ||
| 407 | * Free an iMDCT. | ||
| 408 | */ | ||
| 409 | void ff_celt_imdct_uninit(CeltIMDCTContext **s); | ||
| 410 | |||
| 411 | /** | ||
| 412 | * Calculate the middle half of the iMDCT | ||
| 413 | */ | ||
| 414 | void ff_celt_imdct_half(CeltIMDCTContext *s, float *dst, const float *src, | ||
| 415 | int src_stride, float scale); | ||
| 416 | |||
| 417 | int ff_celt_init(AVCodecContext *avctx, CeltContext **s, int output_channels); | ||
| 418 | |||
| 419 | void ff_celt_free(CeltContext **s); | ||
| 420 | |||
| 421 | void ff_celt_flush(CeltContext *s); | ||
| 422 | |||
| 423 | int ff_celt_decode_frame(CeltContext *s, OpusRangeCoder *rc, | ||
| 424 | float **output, int coded_channels, int frame_size, | ||
| 425 | int startband, int endband); | ||
| 426 | |||
| 427 | extern const float ff_celt_window2[120]; | ||
| 428 | |||
| 429 | #endif /* AVCODEC_OPUS_H */ | ||
| @@ -0,0 +1,2220 @@ | |||
| 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 CELT decoder | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include <stdint.h> | ||
| 28 | |||
| 29 | #include "libavutil/float_dsp.h" | ||
| 30 | |||
| 31 | #include "opus.h" | ||
| 32 | |||
| 33 | enum CeltSpread { | ||
| 34 | CELT_SPREAD_NONE, | ||
| 35 | CELT_SPREAD_LIGHT, | ||
| 36 | CELT_SPREAD_NORMAL, | ||
| 37 | CELT_SPREAD_AGGRESSIVE | ||
| 38 | }; | ||
| 39 | |||
| 40 | typedef struct CeltFrame { | ||
| 41 | float energy[CELT_MAX_BANDS]; | ||
| 42 | float prev_energy[2][CELT_MAX_BANDS]; | ||
| 43 | |||
| 44 | uint8_t collapse_masks[CELT_MAX_BANDS]; | ||
| 45 | |||
| 46 | /* buffer for mdct output + postfilter */ | ||
| 47 | DECLARE_ALIGNED(32, float, buf)[2048]; | ||
| 48 | |||
| 49 | /* postfilter parameters */ | ||
| 50 | int pf_period_new; | ||
| 51 | float pf_gains_new[3]; | ||
| 52 | int pf_period; | ||
| 53 | float pf_gains[3]; | ||
| 54 | int pf_period_old; | ||
| 55 | float pf_gains_old[3]; | ||
| 56 | |||
| 57 | float deemph_coeff; | ||
| 58 | } CeltFrame; | ||
| 59 | |||
| 60 | struct CeltContext { | ||
| 61 | // constant values that do not change during context lifetime | ||
| 62 | AVCodecContext *avctx; | ||
| 63 | CeltIMDCTContext *imdct[4]; | ||
| 64 | AVFloatDSPContext dsp; | ||
| 65 | int output_channels; | ||
| 66 | |||
| 67 | // values that have inter-frame effect and must be reset on flush | ||
| 68 | CeltFrame frame[2]; | ||
| 69 | uint32_t seed; | ||
| 70 | int flushed; | ||
| 71 | |||
| 72 | // values that only affect a single frame | ||
| 73 | int coded_channels; | ||
| 74 | int framebits; | ||
| 75 | int duration; | ||
| 76 | |||
| 77 | /* number of iMDCT blocks in the frame */ | ||
| 78 | int blocks; | ||
| 79 | /* size of each block */ | ||
| 80 | int blocksize; | ||
| 81 | |||
| 82 | int startband; | ||
| 83 | int endband; | ||
| 84 | int codedbands; | ||
| 85 | |||
| 86 | int anticollapse_bit; | ||
| 87 | |||
| 88 | int intensitystereo; | ||
| 89 | int dualstereo; | ||
| 90 | enum CeltSpread spread; | ||
| 91 | |||
| 92 | int remaining; | ||
| 93 | int remaining2; | ||
| 94 | int fine_bits [CELT_MAX_BANDS]; | ||
| 95 | int fine_priority[CELT_MAX_BANDS]; | ||
| 96 | int pulses [CELT_MAX_BANDS]; | ||
| 97 | int tf_change [CELT_MAX_BANDS]; | ||
| 98 | |||
| 99 | DECLARE_ALIGNED(32, float, coeffs)[2][CELT_MAX_FRAME_SIZE]; | ||
| 100 | DECLARE_ALIGNED(32, float, scratch)[22 * 8]; // MAX(celt_freq_range) * 1<<CELT_MAX_LOG_BLOCKS | ||
| 101 | }; | ||
| 102 | |||
| 103 | static const uint16_t celt_model_tapset[] = { 4, 2, 3, 4 }; | ||
| 104 | |||
| 105 | static const uint16_t celt_model_spread[] = { 32, 7, 9, 30, 32 }; | ||
| 106 | |||
| 107 | static const uint16_t celt_model_alloc_trim[] = { | ||
| 108 | 128, 2, 4, 9, 19, 41, 87, 109, 119, 124, 126, 128 | ||
| 109 | }; | ||
| 110 | |||
| 111 | static const uint16_t celt_model_energy_small[] = { 4, 2, 3, 4 }; | ||
| 112 | |||
| 113 | static const uint8_t celt_freq_bands[] = { /* in steps of 200Hz */ | ||
| 114 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100 | ||
| 115 | }; | ||
| 116 | |||
| 117 | static const uint8_t celt_freq_range[] = { | ||
| 118 | 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 6, 6, 8, 12, 18, 22 | ||
| 119 | }; | ||
| 120 | |||
| 121 | static const uint8_t celt_log_freq_range[] = { | ||
| 122 | 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 16, 16, 16, 21, 21, 24, 29, 34, 36 | ||
| 123 | }; | ||
| 124 | |||
| 125 | static const int8_t celt_tf_select[4][2][2][2] = { | ||
| 126 | { { { 0, -1 }, { 0, -1 } }, { { 0, -1 }, { 0, -1 } } }, | ||
| 127 | { { { 0, -1 }, { 0, -2 } }, { { 1, 0 }, { 1, -1 } } }, | ||
| 128 | { { { 0, -2 }, { 0, -3 } }, { { 2, 0 }, { 1, -1 } } }, | ||
| 129 | { { { 0, -2 }, { 0, -3 } }, { { 3, 0 }, { 1, -1 } } } | ||
| 130 | }; | ||
| 131 | |||
| 132 | static const float celt_mean_energy[] = { | ||
| 133 | 6.437500f, 6.250000f, 5.750000f, 5.312500f, 5.062500f, | ||
| 134 | 4.812500f, 4.500000f, 4.375000f, 4.875000f, 4.687500f, | ||
| 135 | 4.562500f, 4.437500f, 4.875000f, 4.625000f, 4.312500f, | ||
| 136 | 4.500000f, 4.375000f, 4.625000f, 4.750000f, 4.437500f, | ||
| 137 | 3.750000f, 3.750000f, 3.750000f, 3.750000f, 3.750000f | ||
| 138 | }; | ||
| 139 | |||
| 140 | static const float celt_alpha_coef[] = { | ||
| 141 | 29440.0f/32768.0f, 26112.0f/32768.0f, 21248.0f/32768.0f, 16384.0f/32768.0f | ||
| 142 | }; | ||
| 143 | |||
| 144 | static const float celt_beta_coef[] = { /* TODO: precompute 1 minus this if the code ends up neater */ | ||
| 145 | 30147.0f/32768.0f, 22282.0f/32768.0f, 12124.0f/32768.0f, 6554.0f/32768.0f | ||
| 146 | }; | ||
| 147 | |||
| 148 | static const uint8_t celt_coarse_energy_dist[4][2][42] = { | ||
| 149 | { | ||
| 150 | { // 120-sample inter | ||
| 151 | 72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128, | ||
| 152 | 64, 128, 92, 78, 92, 79, 92, 78, 90, 79, 116, 41, 115, 40, | ||
| 153 | 114, 40, 132, 26, 132, 26, 145, 17, 161, 12, 176, 10, 177, 11 | ||
| 154 | }, { // 120-sample intra | ||
| 155 | 24, 179, 48, 138, 54, 135, 54, 132, 53, 134, 56, 133, 55, 132, | ||
| 156 | 55, 132, 61, 114, 70, 96, 74, 88, 75, 88, 87, 74, 89, 66, | ||
| 157 | 91, 67, 100, 59, 108, 50, 120, 40, 122, 37, 97, 43, 78, 50 | ||
| 158 | } | ||
| 159 | }, { | ||
| 160 | { // 240-sample inter | ||
| 161 | 83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73, 93, 74, | ||
| 162 | 93, 74, 109, 40, 114, 36, 117, 34, 117, 34, 143, 17, 145, 18, | ||
| 163 | 146, 19, 162, 12, 165, 10, 178, 7, 189, 6, 190, 8, 177, 9 | ||
| 164 | }, { // 240-sample intra | ||
| 165 | 23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91, | ||
| 166 | 73, 91, 78, 89, 86, 80, 92, 66, 93, 64, 102, 59, 103, 60, | ||
| 167 | 104, 60, 117, 52, 123, 44, 138, 35, 133, 31, 97, 38, 77, 45 | ||
| 168 | } | ||
| 169 | }, { | ||
| 170 | { // 480-sample inter | ||
| 171 | 61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38, | ||
| 172 | 112, 38, 124, 26, 132, 27, 136, 19, 140, 20, 155, 14, 159, 16, | ||
| 173 | 158, 18, 170, 13, 177, 10, 187, 8, 192, 6, 175, 9, 159, 10 | ||
| 174 | }, { // 480-sample intra | ||
| 175 | 21, 178, 59, 110, 71, 86, 75, 85, 84, 83, 91, 66, 88, 73, | ||
| 176 | 87, 72, 92, 75, 98, 72, 105, 58, 107, 54, 115, 52, 114, 55, | ||
| 177 | 112, 56, 129, 51, 132, 40, 150, 33, 140, 29, 98, 35, 77, 42 | ||
| 178 | } | ||
| 179 | }, { | ||
| 180 | { // 960-sample inter | ||
| 181 | 42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32, 120, 36, | ||
| 182 | 119, 33, 127, 33, 134, 34, 139, 21, 147, 23, 152, 20, 158, 25, | ||
| 183 | 154, 26, 166, 21, 173, 16, 184, 13, 184, 10, 150, 13, 139, 15 | ||
| 184 | }, { // 960-sample intra | ||
| 185 | 22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72, | ||
| 186 | 96, 67, 101, 73, 107, 72, 113, 55, 118, 52, 125, 52, 118, 52, | ||
| 187 | 117, 55, 135, 49, 137, 39, 157, 32, 145, 29, 97, 33, 77, 40 | ||
| 188 | } | ||
| 189 | } | ||
| 190 | }; | ||
| 191 | |||
| 192 | static const uint8_t celt_static_alloc[11][21] = { /* 1/32 bit/sample */ | ||
| 193 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 194 | { 90, 80, 75, 69, 63, 56, 49, 40, 34, 29, 20, 18, 10, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 195 | { 110, 100, 90, 84, 78, 71, 65, 58, 51, 45, 39, 32, 26, 20, 12, 0, 0, 0, 0, 0, 0 }, | ||
| 196 | { 118, 110, 103, 93, 86, 80, 75, 70, 65, 59, 53, 47, 40, 31, 23, 15, 4, 0, 0, 0, 0 }, | ||
| 197 | { 126, 119, 112, 104, 95, 89, 83, 78, 72, 66, 60, 54, 47, 39, 32, 25, 17, 12, 1, 0, 0 }, | ||
| 198 | { 134, 127, 120, 114, 103, 97, 91, 85, 78, 72, 66, 60, 54, 47, 41, 35, 29, 23, 16, 10, 1 }, | ||
| 199 | { 144, 137, 130, 124, 113, 107, 101, 95, 88, 82, 76, 70, 64, 57, 51, 45, 39, 33, 26, 15, 1 }, | ||
| 200 | { 152, 145, 138, 132, 123, 117, 111, 105, 98, 92, 86, 80, 74, 67, 61, 55, 49, 43, 36, 20, 1 }, | ||
| 201 | { 162, 155, 148, 142, 133, 127, 121, 115, 108, 102, 96, 90, 84, 77, 71, 65, 59, 53, 46, 30, 1 }, | ||
| 202 | { 172, 165, 158, 152, 143, 137, 131, 125, 118, 112, 106, 100, 94, 87, 81, 75, 69, 63, 56, 45, 20 }, | ||
| 203 | { 200, 200, 200, 200, 200, 200, 200, 200, 198, 193, 188, 183, 178, 173, 168, 163, 158, 153, 148, 129, 104 } | ||
| 204 | }; | ||
| 205 | |||
| 206 | static const uint8_t celt_static_caps[4][2][21] = { | ||
| 207 | { // 120-sample | ||
| 208 | {224, 224, 224, 224, 224, 224, 224, 224, 160, 160, | ||
| 209 | 160, 160, 185, 185, 185, 178, 178, 168, 134, 61, 37}, | ||
| 210 | {224, 224, 224, 224, 224, 224, 224, 224, 240, 240, | ||
| 211 | 240, 240, 207, 207, 207, 198, 198, 183, 144, 66, 40}, | ||
| 212 | }, { // 240-sample | ||
| 213 | {160, 160, 160, 160, 160, 160, 160, 160, 185, 185, | ||
| 214 | 185, 185, 193, 193, 193, 183, 183, 172, 138, 64, 38}, | ||
| 215 | {240, 240, 240, 240, 240, 240, 240, 240, 207, 207, | ||
| 216 | 207, 207, 204, 204, 204, 193, 193, 180, 143, 66, 40}, | ||
| 217 | }, { // 480-sample | ||
| 218 | {185, 185, 185, 185, 185, 185, 185, 185, 193, 193, | ||
| 219 | 193, 193, 193, 193, 193, 183, 183, 172, 138, 65, 39}, | ||
| 220 | {207, 207, 207, 207, 207, 207, 207, 207, 204, 204, | ||
| 221 | 204, 204, 201, 201, 201, 188, 188, 176, 141, 66, 40}, | ||
| 222 | }, { // 960-sample | ||
| 223 | {193, 193, 193, 193, 193, 193, 193, 193, 193, 193, | ||
| 224 | 193, 193, 194, 194, 194, 184, 184, 173, 139, 65, 39}, | ||
| 225 | {204, 204, 204, 204, 204, 204, 204, 204, 201, 201, | ||
| 226 | 201, 201, 198, 198, 198, 187, 187, 175, 140, 66, 40} | ||
| 227 | } | ||
| 228 | }; | ||
| 229 | |||
| 230 | static const uint8_t celt_cache_bits[392] = { | ||
| 231 | 40, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | ||
| 232 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | ||
| 233 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 40, 15, 23, 28, | ||
| 234 | 31, 34, 36, 38, 39, 41, 42, 43, 44, 45, 46, 47, 47, 49, 50, | ||
| 235 | 51, 52, 53, 54, 55, 55, 57, 58, 59, 60, 61, 62, 63, 63, 65, | ||
| 236 | 66, 67, 68, 69, 70, 71, 71, 40, 20, 33, 41, 48, 53, 57, 61, | ||
| 237 | 64, 66, 69, 71, 73, 75, 76, 78, 80, 82, 85, 87, 89, 91, 92, | ||
| 238 | 94, 96, 98, 101, 103, 105, 107, 108, 110, 112, 114, 117, 119, 121, 123, | ||
| 239 | 124, 126, 128, 40, 23, 39, 51, 60, 67, 73, 79, 83, 87, 91, 94, | ||
| 240 | 97, 100, 102, 105, 107, 111, 115, 118, 121, 124, 126, 129, 131, 135, 139, | ||
| 241 | 142, 145, 148, 150, 153, 155, 159, 163, 166, 169, 172, 174, 177, 179, 35, | ||
| 242 | 28, 49, 65, 78, 89, 99, 107, 114, 120, 126, 132, 136, 141, 145, 149, | ||
| 243 | 153, 159, 165, 171, 176, 180, 185, 189, 192, 199, 205, 211, 216, 220, 225, | ||
| 244 | 229, 232, 239, 245, 251, 21, 33, 58, 79, 97, 112, 125, 137, 148, 157, | ||
| 245 | 166, 174, 182, 189, 195, 201, 207, 217, 227, 235, 243, 251, 17, 35, 63, | ||
| 246 | 86, 106, 123, 139, 152, 165, 177, 187, 197, 206, 214, 222, 230, 237, 250, | ||
| 247 | 25, 31, 55, 75, 91, 105, 117, 128, 138, 146, 154, 161, 168, 174, 180, | ||
| 248 | 185, 190, 200, 208, 215, 222, 229, 235, 240, 245, 255, 16, 36, 65, 89, | ||
| 249 | 110, 128, 144, 159, 173, 185, 196, 207, 217, 226, 234, 242, 250, 11, 41, | ||
| 250 | 74, 103, 128, 151, 172, 191, 209, 225, 241, 255, 9, 43, 79, 110, 138, | ||
| 251 | 163, 186, 207, 227, 246, 12, 39, 71, 99, 123, 144, 164, 182, 198, 214, | ||
| 252 | 228, 241, 253, 9, 44, 81, 113, 142, 168, 192, 214, 235, 255, 7, 49, | ||
| 253 | 90, 127, 160, 191, 220, 247, 6, 51, 95, 134, 170, 203, 234, 7, 47, | ||
| 254 | 87, 123, 155, 184, 212, 237, 6, 52, 97, 137, 174, 208, 240, 5, 57, | ||
| 255 | 106, 151, 192, 231, 5, 59, 111, 158, 202, 243, 5, 55, 103, 147, 187, | ||
| 256 | 224, 5, 60, 113, 161, 206, 248, 4, 65, 122, 175, 224, 4, 67, 127, | ||
| 257 | 182, 234 | ||
| 258 | }; | ||
| 259 | |||
| 260 | static const int16_t celt_cache_index[105] = { | ||
| 261 | -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 41, 41, 41, | ||
| 262 | 82, 82, 123, 164, 200, 222, 0, 0, 0, 0, 0, 0, 0, 0, 41, | ||
| 263 | 41, 41, 41, 123, 123, 123, 164, 164, 240, 266, 283, 295, 41, 41, 41, | ||
| 264 | 41, 41, 41, 41, 41, 123, 123, 123, 123, 240, 240, 240, 266, 266, 305, | ||
| 265 | 318, 328, 336, 123, 123, 123, 123, 123, 123, 123, 123, 240, 240, 240, 240, | ||
| 266 | 305, 305, 305, 318, 318, 343, 351, 358, 364, 240, 240, 240, 240, 240, 240, | ||
| 267 | 240, 240, 305, 305, 305, 305, 343, 343, 343, 351, 351, 370, 376, 382, 387, | ||
| 268 | }; | ||
| 269 | |||
| 270 | static const uint8_t celt_log2_frac[] = { | ||
| 271 | 0, 8, 13, 16, 19, 21, 23, 24, 26, 27, 28, 29, 30, 31, 32, 32, 33, 34, 34, 35, 36, 36, 37, 37 | ||
| 272 | }; | ||
| 273 | |||
| 274 | static const uint8_t celt_bit_interleave[] = { | ||
| 275 | 0, 1, 1, 1, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3 | ||
| 276 | }; | ||
| 277 | |||
| 278 | static const uint8_t celt_bit_deinterleave[] = { | ||
| 279 | 0x00, 0x03, 0x0C, 0x0F, 0x30, 0x33, 0x3C, 0x3F, | ||
| 280 | 0xC0, 0xC3, 0xCC, 0xCF, 0xF0, 0xF3, 0xFC, 0xFF | ||
| 281 | }; | ||
| 282 | |||
| 283 | static const uint8_t celt_hadamard_ordery[] = { | ||
| 284 | 1, 0, | ||
| 285 | 3, 0, 2, 1, | ||
| 286 | 7, 0, 4, 3, 6, 1, 5, 2, | ||
| 287 | 15, 0, 8, 7, 12, 3, 11, 4, 14, 1, 9, 6, 13, 2, 10, 5 | ||
| 288 | }; | ||
| 289 | |||
| 290 | static const uint16_t celt_qn_exp2[] = { | ||
| 291 | 16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048 | ||
| 292 | }; | ||
| 293 | |||
| 294 | static const uint32_t celt_pvq_u[1272] = { | ||
| 295 | /* N = 0, K = 0...176 */ | ||
| 296 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 297 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 298 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 299 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 300 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 301 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 302 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 303 | /* N = 1, K = 1...176 */ | ||
| 304 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
| 305 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
| 306 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
| 307 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
| 308 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
| 309 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
| 310 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
| 311 | /* N = 2, K = 2...176 */ | ||
| 312 | 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, | ||
| 313 | 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, | ||
| 314 | 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, | ||
| 315 | 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, | ||
| 316 | 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, | ||
| 317 | 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, | ||
| 318 | 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, | ||
| 319 | 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255, 257, 259, 261, 263, | ||
| 320 | 265, 267, 269, 271, 273, 275, 277, 279, 281, 283, 285, 287, 289, 291, 293, | ||
| 321 | 295, 297, 299, 301, 303, 305, 307, 309, 311, 313, 315, 317, 319, 321, 323, | ||
| 322 | 325, 327, 329, 331, 333, 335, 337, 339, 341, 343, 345, 347, 349, 351, | ||
| 323 | /* N = 3, K = 3...176 */ | ||
| 324 | 13, 25, 41, 61, 85, 113, 145, 181, 221, 265, 313, 365, 421, 481, 545, 613, | ||
| 325 | 685, 761, 841, 925, 1013, 1105, 1201, 1301, 1405, 1513, 1625, 1741, 1861, | ||
| 326 | 1985, 2113, 2245, 2381, 2521, 2665, 2813, 2965, 3121, 3281, 3445, 3613, 3785, | ||
| 327 | 3961, 4141, 4325, 4513, 4705, 4901, 5101, 5305, 5513, 5725, 5941, 6161, 6385, | ||
| 328 | 6613, 6845, 7081, 7321, 7565, 7813, 8065, 8321, 8581, 8845, 9113, 9385, 9661, | ||
| 329 | 9941, 10225, 10513, 10805, 11101, 11401, 11705, 12013, 12325, 12641, 12961, | ||
| 330 | 13285, 13613, 13945, 14281, 14621, 14965, 15313, 15665, 16021, 16381, 16745, | ||
| 331 | 17113, 17485, 17861, 18241, 18625, 19013, 19405, 19801, 20201, 20605, 21013, | ||
| 332 | 21425, 21841, 22261, 22685, 23113, 23545, 23981, 24421, 24865, 25313, 25765, | ||
| 333 | 26221, 26681, 27145, 27613, 28085, 28561, 29041, 29525, 30013, 30505, 31001, | ||
| 334 | 31501, 32005, 32513, 33025, 33541, 34061, 34585, 35113, 35645, 36181, 36721, | ||
| 335 | 37265, 37813, 38365, 38921, 39481, 40045, 40613, 41185, 41761, 42341, 42925, | ||
| 336 | 43513, 44105, 44701, 45301, 45905, 46513, 47125, 47741, 48361, 48985, 49613, | ||
| 337 | 50245, 50881, 51521, 52165, 52813, 53465, 54121, 54781, 55445, 56113, 56785, | ||
| 338 | 57461, 58141, 58825, 59513, 60205, 60901, 61601, | ||
| 339 | /* N = 4, K = 4...176 */ | ||
| 340 | 63, 129, 231, 377, 575, 833, 1159, 1561, 2047, 2625, 3303, 4089, 4991, 6017, | ||
| 341 | 7175, 8473, 9919, 11521, 13287, 15225, 17343, 19649, 22151, 24857, 27775, | ||
| 342 | 30913, 34279, 37881, 41727, 45825, 50183, 54809, 59711, 64897, 70375, 76153, | ||
| 343 | 82239, 88641, 95367, 102425, 109823, 117569, 125671, 134137, 142975, 152193, | ||
| 344 | 161799, 171801, 182207, 193025, 204263, 215929, 228031, 240577, 253575, | ||
| 345 | 267033, 280959, 295361, 310247, 325625, 341503, 357889, 374791, 392217, | ||
| 346 | 410175, 428673, 447719, 467321, 487487, 508225, 529543, 551449, 573951, | ||
| 347 | 597057, 620775, 645113, 670079, 695681, 721927, 748825, 776383, 804609, | ||
| 348 | 833511, 863097, 893375, 924353, 956039, 988441, 1021567, 1055425, 1090023, | ||
| 349 | 1125369, 1161471, 1198337, 1235975, 1274393, 1313599, 1353601, 1394407, | ||
| 350 | 1436025, 1478463, 1521729, 1565831, 1610777, 1656575, 1703233, 1750759, | ||
| 351 | 1799161, 1848447, 1898625, 1949703, 2001689, 2054591, 2108417, 2163175, | ||
| 352 | 2218873, 2275519, 2333121, 2391687, 2451225, 2511743, 2573249, 2635751, | ||
| 353 | 2699257, 2763775, 2829313, 2895879, 2963481, 3032127, 3101825, 3172583, | ||
| 354 | 3244409, 3317311, 3391297, 3466375, 3542553, 3619839, 3698241, 3777767, | ||
| 355 | 3858425, 3940223, 4023169, 4107271, 4192537, 4278975, 4366593, 4455399, | ||
| 356 | 4545401, 4636607, 4729025, 4822663, 4917529, 5013631, 5110977, 5209575, | ||
| 357 | 5309433, 5410559, 5512961, 5616647, 5721625, 5827903, 5935489, 6044391, | ||
| 358 | 6154617, 6266175, 6379073, 6493319, 6608921, 6725887, 6844225, 6963943, | ||
| 359 | 7085049, 7207551, | ||
| 360 | /* N = 5, K = 5...176 */ | ||
| 361 | 321, 681, 1289, 2241, 3649, 5641, 8361, 11969, 16641, 22569, 29961, 39041, | ||
| 362 | 50049, 63241, 78889, 97281, 118721, 143529, 172041, 204609, 241601, 283401, | ||
| 363 | 330409, 383041, 441729, 506921, 579081, 658689, 746241, 842249, 947241, | ||
| 364 | 1061761, 1186369, 1321641, 1468169, 1626561, 1797441, 1981449, 2179241, | ||
| 365 | 2391489, 2618881, 2862121, 3121929, 3399041, 3694209, 4008201, 4341801, | ||
| 366 | 4695809, 5071041, 5468329, 5888521, 6332481, 6801089, 7295241, 7815849, | ||
| 367 | 8363841, 8940161, 9545769, 10181641, 10848769, 11548161, 12280841, 13047849, | ||
| 368 | 13850241, 14689089, 15565481, 16480521, 17435329, 18431041, 19468809, | ||
| 369 | 20549801, 21675201, 22846209, 24064041, 25329929, 26645121, 28010881, | ||
| 370 | 29428489, 30899241, 32424449, 34005441, 35643561, 37340169, 39096641, | ||
| 371 | 40914369, 42794761, 44739241, 46749249, 48826241, 50971689, 53187081, | ||
| 372 | 55473921, 57833729, 60268041, 62778409, 65366401, 68033601, 70781609, | ||
| 373 | 73612041, 76526529, 79526721, 82614281, 85790889, 89058241, 92418049, | ||
| 374 | 95872041, 99421961, 103069569, 106816641, 110664969, 114616361, 118672641, | ||
| 375 | 122835649, 127107241, 131489289, 135983681, 140592321, 145317129, 150160041, | ||
| 376 | 155123009, 160208001, 165417001, 170752009, 176215041, 181808129, 187533321, | ||
| 377 | 193392681, 199388289, 205522241, 211796649, 218213641, 224775361, 231483969, | ||
| 378 | 238341641, 245350569, 252512961, 259831041, 267307049, 274943241, 282741889, | ||
| 379 | 290705281, 298835721, 307135529, 315607041, 324252609, 333074601, 342075401, | ||
| 380 | 351257409, 360623041, 370174729, 379914921, 389846081, 399970689, 410291241, | ||
| 381 | 420810249, 431530241, 442453761, 453583369, 464921641, 476471169, 488234561, | ||
| 382 | 500214441, 512413449, 524834241, 537479489, 550351881, 563454121, 576788929, | ||
| 383 | 590359041, 604167209, 618216201, 632508801, | ||
| 384 | /* N = 6, K = 6...96 (technically V(109,5) fits in 32 bits, but that can't be | ||
| 385 | achieved by splitting an Opus band) */ | ||
| 386 | 1683, 3653, 7183, 13073, 22363, 36365, 56695, 85305, 124515, 177045, 246047, | ||
| 387 | 335137, 448427, 590557, 766727, 982729, 1244979, 1560549, 1937199, 2383409, | ||
| 388 | 2908411, 3522221, 4235671, 5060441, 6009091, 7095093, 8332863, 9737793, | ||
| 389 | 11326283, 13115773, 15124775, 17372905, 19880915, 22670725, 25765455, | ||
| 390 | 29189457, 32968347, 37129037, 41699767, 46710137, 52191139, 58175189, | ||
| 391 | 64696159, 71789409, 79491819, 87841821, 96879431, 106646281, 117185651, | ||
| 392 | 128542501, 140763503, 153897073, 167993403, 183104493, 199284183, 216588185, | ||
| 393 | 235074115, 254801525, 275831935, 298228865, 322057867, 347386557, 374284647, | ||
| 394 | 402823977, 433078547, 465124549, 499040399, 534906769, 572806619, 612825229, | ||
| 395 | 655050231, 699571641, 746481891, 795875861, 847850911, 902506913, 959946283, | ||
| 396 | 1020274013, 1083597703, 1150027593, 1219676595, 1292660325, 1369097135, | ||
| 397 | 1449108145, 1532817275, 1620351277, 1711839767, 1807415257, 1907213187, | ||
| 398 | 2011371957, 2120032959, | ||
| 399 | /* N = 7, K = 7...54 (technically V(60,6) fits in 32 bits, but that can't be | ||
| 400 | achieved by splitting an Opus band) */ | ||
| 401 | 8989, 19825, 40081, 75517, 134245, 227305, 369305, 579125, 880685, 1303777, | ||
| 402 | 1884961, 2668525, 3707509, 5064793, 6814249, 9041957, 11847485, 15345233, | ||
| 403 | 19665841, 24957661, 31388293, 39146185, 48442297, 59511829, 72616013, | ||
| 404 | 88043969, 106114625, 127178701, 151620757, 179861305, 212358985, 249612805, | ||
| 405 | 292164445, 340600625, 395555537, 457713341, 527810725, 606639529, 695049433, | ||
| 406 | 793950709, 904317037, 1027188385, 1163673953, 1314955181, 1482288821, | ||
| 407 | 1667010073, 1870535785, 2094367717, | ||
| 408 | /* N = 8, K = 8...37 (technically V(40,7) fits in 32 bits, but that can't be | ||
| 409 | achieved by splitting an Opus band) */ | ||
| 410 | 48639, 108545, 224143, 433905, 795455, 1392065, 2340495, 3800305, 5984767, | ||
| 411 | 9173505, 13726991, 20103025, 28875327, 40754369, 56610575, 77500017, | ||
| 412 | 104692735, 139703809, 184327311, 240673265, 311207743, 398796225, 506750351, | ||
| 413 | 638878193, 799538175, 993696769, 1226990095, 1505789553, 1837271615, | ||
| 414 | 2229491905, | ||
| 415 | /* N = 9, K = 9...28 (technically V(29,8) fits in 32 bits, but that can't be | ||
| 416 | achieved by splitting an Opus band) */ | ||
| 417 | 265729, 598417, 1256465, 2485825, 4673345, 8405905, 14546705, 24331777, | ||
| 418 | 39490049, 62390545, 96220561, 145198913, 214828609, 312193553, 446304145, | ||
| 419 | 628496897, 872893441, 1196924561, 1621925137, 2173806145, | ||
| 420 | /* N = 10, K = 10...24 */ | ||
| 421 | 1462563, 3317445, 7059735, 14218905, 27298155, 50250765, 89129247, 152951073, | ||
| 422 | 254831667, 413442773, 654862247, 1014889769, 1541911931, 2300409629, | ||
| 423 | 3375210671, | ||
| 424 | /* N = 11, K = 11...19 (technically V(20,10) fits in 32 bits, but that can't be | ||
| 425 | achieved by splitting an Opus band) */ | ||
| 426 | 8097453, 18474633, 39753273, 81270333, 158819253, 298199265, 540279585, | ||
| 427 | 948062325, 1616336765, | ||
| 428 | /* N = 12, K = 12...18 */ | ||
| 429 | 45046719, 103274625, 224298231, 464387817, 921406335, 1759885185, | ||
| 430 | 3248227095, | ||
| 431 | /* N = 13, K = 13...16 */ | ||
| 432 | 251595969, 579168825, 1267854873, 2653649025, | ||
| 433 | /* N = 14, K = 14 */ | ||
| 434 | 1409933619 | ||
| 435 | }; | ||
| 436 | |||
| 437 | DECLARE_ALIGNED(32, static const float, celt_window)[120] = { | ||
| 438 | 6.7286966e-05f, 0.00060551348f, 0.0016815970f, 0.0032947962f, 0.0054439943f, | ||
| 439 | 0.0081276923f, 0.011344001f, 0.015090633f, 0.019364886f, 0.024163635f, | ||
| 440 | 0.029483315f, 0.035319905f, 0.041668911f, 0.048525347f, 0.055883718f, | ||
| 441 | 0.063737999f, 0.072081616f, 0.080907428f, 0.090207705f, 0.099974111f, | ||
| 442 | 0.11019769f, 0.12086883f, 0.13197729f, 0.14351214f, 0.15546177f, | ||
| 443 | 0.16781389f, 0.18055550f, 0.19367290f, 0.20715171f, 0.22097682f, | ||
| 444 | 0.23513243f, 0.24960208f, 0.26436860f, 0.27941419f, 0.29472040f, | ||
| 445 | 0.31026818f, 0.32603788f, 0.34200931f, 0.35816177f, 0.37447407f, | ||
| 446 | 0.39092462f, 0.40749142f, 0.42415215f, 0.44088423f, 0.45766484f, | ||
| 447 | 0.47447104f, 0.49127978f, 0.50806798f, 0.52481261f, 0.54149077f, | ||
| 448 | 0.55807973f, 0.57455701f, 0.59090049f, 0.60708841f, 0.62309951f, | ||
| 449 | 0.63891306f, 0.65450896f, 0.66986776f, 0.68497077f, 0.69980010f, | ||
| 450 | 0.71433873f, 0.72857055f, 0.74248043f, 0.75605424f, 0.76927895f, | ||
| 451 | 0.78214257f, 0.79463430f, 0.80674445f, 0.81846456f, 0.82978733f, | ||
| 452 | 0.84070669f, 0.85121779f, 0.86131698f, 0.87100183f, 0.88027111f, | ||
| 453 | 0.88912479f, 0.89756398f, 0.90559094f, 0.91320904f, 0.92042270f, | ||
| 454 | 0.92723738f, 0.93365955f, 0.93969656f, 0.94535671f, 0.95064907f, | ||
| 455 | 0.95558353f, 0.96017067f, 0.96442171f, 0.96834849f, 0.97196334f, | ||
| 456 | 0.97527906f, 0.97830883f, 0.98106616f, 0.98356480f, 0.98581869f, | ||
| 457 | 0.98784191f, 0.98964856f, 0.99125274f, 0.99266849f, 0.99390969f, | ||
| 458 | 0.99499004f, 0.99592297f, 0.99672162f, 0.99739874f, 0.99796667f, | ||
| 459 | 0.99843728f, 0.99882195f, 0.99913147f, 0.99937606f, 0.99956527f, | ||
| 460 | 0.99970802f, 0.99981248f, 0.99988613f, 0.99993565f, 0.99996697f, | ||
| 461 | 0.99998518f, 0.99999457f, 0.99999859f, 0.99999982f, 1.0000000f, | ||
| 462 | }; | ||
| 463 | |||
| 464 | /* square of the window, used for the postfilter */ | ||
| 465 | const float ff_celt_window2[120] = { | ||
| 466 | 4.5275357e-09f, 3.66647e-07f, 2.82777e-06f, 1.08557e-05f, 2.96371e-05f, 6.60594e-05f, | ||
| 467 | 0.000128686f, 0.000227727f, 0.000374999f, 0.000583881f, 0.000869266f, 0.0012475f, | ||
| 468 | 0.0017363f, 0.00235471f, 0.00312299f, 0.00406253f, 0.00519576f, 0.00654601f, | ||
| 469 | 0.00813743f, 0.00999482f, 0.0121435f, 0.0146093f, 0.017418f, 0.0205957f, 0.0241684f, | ||
| 470 | 0.0281615f, 0.0326003f, 0.0375092f, 0.0429118f, 0.0488308f, 0.0552873f, 0.0623012f, | ||
| 471 | 0.0698908f, 0.0780723f, 0.0868601f, 0.0962664f, 0.106301f, 0.11697f, 0.12828f, | ||
| 472 | 0.140231f, 0.152822f, 0.166049f, 0.179905f, 0.194379f, 0.209457f, 0.225123f, 0.241356f, | ||
| 473 | 0.258133f, 0.275428f, 0.293212f, 0.311453f, 0.330116f, 0.349163f, 0.368556f, 0.388253f, | ||
| 474 | 0.40821f, 0.428382f, 0.448723f, 0.469185f, 0.48972f, 0.51028f, 0.530815f, 0.551277f, | ||
| 475 | 0.571618f, 0.59179f, 0.611747f, 0.631444f, 0.650837f, 0.669884f, 0.688547f, 0.706788f, | ||
| 476 | 0.724572f, 0.741867f, 0.758644f, 0.774877f, 0.790543f, 0.805621f, 0.820095f, 0.833951f, | ||
| 477 | 0.847178f, 0.859769f, 0.87172f, 0.88303f, 0.893699f, 0.903734f, 0.91314f, 0.921928f, | ||
| 478 | 0.930109f, 0.937699f, 0.944713f, 0.951169f, 0.957088f, 0.962491f, 0.9674f, 0.971838f, | ||
| 479 | 0.975832f, 0.979404f, 0.982582f, 0.985391f, 0.987857f, 0.990005f, 0.991863f, 0.993454f, | ||
| 480 | 0.994804f, 0.995937f, 0.996877f, 0.997645f, 0.998264f, 0.998753f, 0.999131f, 0.999416f, | ||
| 481 | 0.999625f, 0.999772f, 0.999871f, 0.999934f, 0.99997f, 0.999989f, 0.999997f, 0.99999964f, 1.0f, | ||
| 482 | }; | ||
| 483 | |||
| 484 | static const uint32_t * const celt_pvq_u_row[15] = { | ||
| 485 | celt_pvq_u + 0, celt_pvq_u + 176, celt_pvq_u + 351, | ||
| 486 | celt_pvq_u + 525, celt_pvq_u + 698, celt_pvq_u + 870, | ||
| 487 | celt_pvq_u + 1041, celt_pvq_u + 1131, celt_pvq_u + 1178, | ||
| 488 | celt_pvq_u + 1207, celt_pvq_u + 1226, celt_pvq_u + 1240, | ||
| 489 | celt_pvq_u + 1248, celt_pvq_u + 1254, celt_pvq_u + 1257 | ||
| 490 | }; | ||
| 491 | |||
| 492 | static inline int16_t celt_cos(int16_t x) | ||
| 493 | { | ||
| 494 | x = (MUL16(x, x) + 4096) >> 13; | ||
| 495 | x = (32767-x) + ROUND_MUL16(x, (-7651 + ROUND_MUL16(x, (8277 + ROUND_MUL16(-626, x))))); | ||
| 496 | return 1+x; | ||
| 497 | } | ||
| 498 | |||
| 499 | static inline int celt_log2tan(int isin, int icos) | ||
| 500 | { | ||
| 501 | int lc, ls; | ||
| 502 | lc = opus_ilog(icos); | ||
| 503 | ls = opus_ilog(isin); | ||
| 504 | icos <<= 15 - lc; | ||
| 505 | isin <<= 15 - ls; | ||
| 506 | return (ls << 11) - (lc << 11) + | ||
| 507 | ROUND_MUL16(isin, ROUND_MUL16(isin, -2597) + 7932) - | ||
| 508 | ROUND_MUL16(icos, ROUND_MUL16(icos, -2597) + 7932); | ||
| 509 | } | ||
| 510 | |||
| 511 | static inline uint32_t celt_rng(CeltContext *s) | ||
| 512 | { | ||
| 513 | s->seed = 1664525 * s->seed + 1013904223; | ||
| 514 | return s->seed; | ||
| 515 | } | ||
| 516 | |||
| 517 | static void celt_decode_coarse_energy(CeltContext *s, OpusRangeCoder *rc) | ||
| 518 | { | ||
| 519 | int i, j; | ||
| 520 | float prev[2] = {0}; | ||
| 521 | float alpha, beta; | ||
| 522 | const uint8_t *model; | ||
| 523 | |||
| 524 | /* use the 2D z-transform to apply prediction in both */ | ||
| 525 | /* the time domain (alpha) and the frequency domain (beta) */ | ||
| 526 | |||
| 527 | if (opus_rc_tell(rc)+3 <= s->framebits && opus_rc_p2model(rc, 3)) { | ||
| 528 | /* intra frame */ | ||
| 529 | alpha = 0; | ||
| 530 | beta = 1.0f - 4915.0f/32768.0f; | ||
| 531 | model = celt_coarse_energy_dist[s->duration][1]; | ||
| 532 | } else { | ||
| 533 | alpha = celt_alpha_coef[s->duration]; | ||
| 534 | beta = 1.0f - celt_beta_coef[s->duration]; | ||
| 535 | model = celt_coarse_energy_dist[s->duration][0]; | ||
| 536 | } | ||
| 537 | |||
| 538 | for (i = 0; i < CELT_MAX_BANDS; i++) { | ||
| 539 | for (j = 0; j < s->coded_channels; j++) { | ||
| 540 | CeltFrame *frame = &s->frame[j]; | ||
| 541 | float value; | ||
| 542 | int available; | ||
| 543 | |||
| 544 | if (i < s->startband || i >= s->endband) { | ||
| 545 | frame->energy[i] = 0.0; | ||
| 546 | continue; | ||
| 547 | } | ||
| 548 | |||
| 549 | available = s->framebits - opus_rc_tell(rc); | ||
| 550 | if (available >= 15) { | ||
| 551 | /* decode using a Laplace distribution */ | ||
| 552 | int k = FFMIN(i, 20) << 1; | ||
| 553 | value = opus_rc_laplace(rc, model[k] << 7, model[k+1] << 6); | ||
| 554 | } else if (available >= 2) { | ||
| 555 | int x = opus_rc_getsymbol(rc, celt_model_energy_small); | ||
| 556 | value = (x>>1) ^ -(x&1); | ||
| 557 | } else if (available >= 1) { | ||
| 558 | value = -(float)opus_rc_p2model(rc, 1); | ||
| 559 | } else value = -1; | ||
| 560 | |||
| 561 | frame->energy[i] = FFMAX(-9.0f, frame->energy[i]) * alpha + prev[j] + value; | ||
| 562 | prev[j] += beta * value; | ||
| 563 | } | ||
| 564 | } | ||
| 565 | } | ||
| 566 | |||
| 567 | static void celt_decode_fine_energy(CeltContext *s, OpusRangeCoder *rc) | ||
| 568 | { | ||
| 569 | int i; | ||
| 570 | for (i = s->startband; i < s->endband; i++) { | ||
| 571 | int j; | ||
| 572 | if (!s->fine_bits[i]) | ||
| 573 | continue; | ||
| 574 | |||
| 575 | for (j = 0; j < s->coded_channels; j++) { | ||
| 576 | CeltFrame *frame = &s->frame[j]; | ||
| 577 | int q2; | ||
| 578 | float offset; | ||
| 579 | q2 = opus_getrawbits(rc, s->fine_bits[i]); | ||
| 580 | offset = (q2 + 0.5f) * (1 << (14 - s->fine_bits[i])) / 16384.0f - 0.5f; | ||
| 581 | frame->energy[i] += offset; | ||
| 582 | } | ||
| 583 | } | ||
| 584 | } | ||
| 585 | |||
| 586 | static void celt_decode_final_energy(CeltContext *s, OpusRangeCoder *rc, | ||
| 587 | int bits_left) | ||
| 588 | { | ||
| 589 | int priority, i, j; | ||
| 590 | |||
| 591 | for (priority = 0; priority < 2; priority++) { | ||
| 592 | for (i = s->startband; i < s->endband && bits_left >= s->coded_channels; i++) { | ||
| 593 | if (s->fine_priority[i] != priority || s->fine_bits[i] >= CELT_MAX_FINE_BITS) | ||
| 594 | continue; | ||
| 595 | |||
| 596 | for (j = 0; j < s->coded_channels; j++) { | ||
| 597 | int q2; | ||
| 598 | float offset; | ||
| 599 | q2 = opus_getrawbits(rc, 1); | ||
| 600 | offset = (q2 - 0.5f) * (1 << (14 - s->fine_bits[i] - 1)) / 16384.0f; | ||
| 601 | s->frame[j].energy[i] += offset; | ||
| 602 | bits_left--; | ||
| 603 | } | ||
| 604 | } | ||
| 605 | } | ||
| 606 | } | ||
| 607 | |||
| 608 | static void celt_decode_tf_changes(CeltContext *s, OpusRangeCoder *rc, | ||
| 609 | int transient) | ||
| 610 | { | ||
| 611 | int i, diff = 0, tf_select = 0, tf_changed = 0, tf_select_bit; | ||
| 612 | int consumed, bits = transient ? 2 : 4; | ||
| 613 | |||
| 614 | consumed = opus_rc_tell(rc); | ||
| 615 | tf_select_bit = (s->duration != 0 && consumed+bits+1 <= s->framebits); | ||
| 616 | |||
| 617 | for (i = s->startband; i < s->endband; i++) { | ||
| 618 | if (consumed+bits+tf_select_bit <= s->framebits) { | ||
| 619 | diff ^= opus_rc_p2model(rc, bits); | ||
| 620 | consumed = opus_rc_tell(rc); | ||
| 621 | tf_changed |= diff; | ||
| 622 | } | ||
| 623 | s->tf_change[i] = diff; | ||
| 624 | bits = transient ? 4 : 5; | ||
| 625 | } | ||
| 626 | |||
| 627 | if (tf_select_bit && celt_tf_select[s->duration][transient][0][tf_changed] != | ||
| 628 | celt_tf_select[s->duration][transient][1][tf_changed]) | ||
| 629 | tf_select = opus_rc_p2model(rc, 1); | ||
| 630 | |||
| 631 | for (i = s->startband; i < s->endband; i++) { | ||
| 632 | s->tf_change[i] = celt_tf_select[s->duration][transient][tf_select][s->tf_change[i]]; | ||
| 633 | } | ||
| 634 | } | ||
| 635 | |||
| 636 | static void celt_decode_allocation(CeltContext *s, OpusRangeCoder *rc) | ||
| 637 | { | ||
| 638 | // approx. maximum bit allocation for each band before boost/trim | ||
| 639 | int cap[CELT_MAX_BANDS]; | ||
| 640 | int boost[CELT_MAX_BANDS]; | ||
| 641 | int threshold[CELT_MAX_BANDS]; | ||
| 642 | int bits1[CELT_MAX_BANDS]; | ||
| 643 | int bits2[CELT_MAX_BANDS]; | ||
| 644 | int trim_offset[CELT_MAX_BANDS]; | ||
| 645 | |||
| 646 | int skip_startband = s->startband; | ||
| 647 | int dynalloc = 6; | ||
| 648 | int alloctrim = 5; | ||
| 649 | int extrabits = 0; | ||
| 650 | |||
| 651 | int skip_bit = 0; | ||
| 652 | int intensitystereo_bit = 0; | ||
| 653 | int dualstereo_bit = 0; | ||
| 654 | |||
| 655 | int remaining, bandbits; | ||
| 656 | int low, high, total, done; | ||
| 657 | int totalbits; | ||
| 658 | int consumed; | ||
| 659 | int i, j; | ||
| 660 | |||
| 661 | consumed = opus_rc_tell(rc); | ||
| 662 | |||
| 663 | /* obtain spread flag */ | ||
| 664 | s->spread = CELT_SPREAD_NORMAL; | ||
| 665 | if (consumed + 4 <= s->framebits) | ||
| 666 | s->spread = opus_rc_getsymbol(rc, celt_model_spread); | ||
| 667 | |||
| 668 | /* generate static allocation caps */ | ||
| 669 | for (i = 0; i < CELT_MAX_BANDS; i++) { | ||
| 670 | cap[i] = (celt_static_caps[s->duration][s->coded_channels - 1][i] + 64) | ||
| 671 | * celt_freq_range[i] << (s->coded_channels - 1) << s->duration >> 2; | ||
| 672 | } | ||
| 673 | |||
| 674 | /* obtain band boost */ | ||
| 675 | totalbits = s->framebits << 3; // convert to 1/8 bits | ||
| 676 | consumed = opus_rc_tell_frac(rc); | ||
| 677 | for (i = s->startband; i < s->endband; i++) { | ||
| 678 | int quanta, band_dynalloc; | ||
| 679 | |||
| 680 | boost[i] = 0; | ||
| 681 | |||
| 682 | quanta = celt_freq_range[i] << (s->coded_channels - 1) << s->duration; | ||
| 683 | quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta)); | ||
| 684 | band_dynalloc = dynalloc; | ||
| 685 | while (consumed + (band_dynalloc<<3) < totalbits && boost[i] < cap[i]) { | ||
| 686 | int add = opus_rc_p2model(rc, band_dynalloc); | ||
| 687 | consumed = opus_rc_tell_frac(rc); | ||
| 688 | if (!add) | ||
| 689 | break; | ||
| 690 | |||
| 691 | boost[i] += quanta; | ||
| 692 | totalbits -= quanta; | ||
| 693 | band_dynalloc = 1; | ||
| 694 | } | ||
| 695 | /* dynalloc is more likely to occur if it's already been used for earlier bands */ | ||
| 696 | if (boost[i]) | ||
| 697 | dynalloc = FFMAX(2, dynalloc - 1); | ||
| 698 | } | ||
| 699 | |||
| 700 | /* obtain allocation trim */ | ||
| 701 | if (consumed + (6 << 3) <= totalbits) | ||
| 702 | alloctrim = opus_rc_getsymbol(rc, celt_model_alloc_trim); | ||
| 703 | |||
| 704 | /* anti-collapse bit reservation */ | ||
| 705 | totalbits = (s->framebits << 3) - opus_rc_tell_frac(rc) - 1; | ||
| 706 | s->anticollapse_bit = 0; | ||
| 707 | if (s->blocks > 1 && s->duration >= 2 && | ||
| 708 | totalbits >= ((s->duration + 2) << 3)) | ||
| 709 | s->anticollapse_bit = 1 << 3; | ||
| 710 | totalbits -= s->anticollapse_bit; | ||
| 711 | |||
| 712 | /* band skip bit reservation */ | ||
| 713 | if (totalbits >= 1 << 3) | ||
| 714 | skip_bit = 1 << 3; | ||
| 715 | totalbits -= skip_bit; | ||
| 716 | |||
| 717 | /* intensity/dual stereo bit reservation */ | ||
| 718 | if (s->coded_channels == 2) { | ||
| 719 | intensitystereo_bit = celt_log2_frac[s->endband - s->startband]; | ||
| 720 | if (intensitystereo_bit <= totalbits) { | ||
| 721 | totalbits -= intensitystereo_bit; | ||
| 722 | if (totalbits >= 1 << 3) { | ||
| 723 | dualstereo_bit = 1 << 3; | ||
| 724 | totalbits -= 1 << 3; | ||
| 725 | } | ||
| 726 | } else | ||
| 727 | intensitystereo_bit = 0; | ||
| 728 | } | ||
| 729 | |||
| 730 | for (i = s->startband; i < s->endband; i++) { | ||
| 731 | int trim = alloctrim - 5 - s->duration; | ||
| 732 | int band = celt_freq_range[i] * (s->endband - i - 1); | ||
| 733 | int duration = s->duration + 3; | ||
| 734 | int scale = duration + s->coded_channels - 1; | ||
| 735 | |||
| 736 | /* PVQ minimum allocation threshold, below this value the band is | ||
| 737 | * skipped */ | ||
| 738 | threshold[i] = FFMAX(3 * celt_freq_range[i] << duration >> 4, | ||
| 739 | s->coded_channels << 3); | ||
| 740 | |||
| 741 | trim_offset[i] = trim * (band << scale) >> 6; | ||
| 742 | |||
| 743 | if (celt_freq_range[i] << s->duration == 1) | ||
| 744 | trim_offset[i] -= s->coded_channels << 3; | ||
| 745 | } | ||
| 746 | |||
| 747 | /* bisection */ | ||
| 748 | low = 1; | ||
| 749 | high = CELT_VECTORS - 1; | ||
| 750 | while (low <= high) { | ||
| 751 | int center = (low + high) >> 1; | ||
| 752 | done = total = 0; | ||
| 753 | |||
| 754 | for (i = s->endband - 1; i >= s->startband; i--) { | ||
| 755 | bandbits = celt_freq_range[i] * celt_static_alloc[center][i] | ||
| 756 | << (s->coded_channels - 1) << s->duration >> 2; | ||
| 757 | |||
| 758 | if (bandbits) | ||
| 759 | bandbits = FFMAX(0, bandbits + trim_offset[i]); | ||
| 760 | bandbits += boost[i]; | ||
| 761 | |||
| 762 | if (bandbits >= threshold[i] || done) { | ||
| 763 | done = 1; | ||
| 764 | total += FFMIN(bandbits, cap[i]); | ||
| 765 | } else if (bandbits >= s->coded_channels << 3) | ||
| 766 | total += s->coded_channels << 3; | ||
| 767 | } | ||
| 768 | |||
| 769 | if (total > totalbits) | ||
| 770 | high = center - 1; | ||
| 771 | else | ||
| 772 | low = center + 1; | ||
| 773 | } | ||
| 774 | high = low--; | ||
| 775 | |||
| 776 | for (i = s->startband; i < s->endband; i++) { | ||
| 777 | bits1[i] = celt_freq_range[i] * celt_static_alloc[low][i] | ||
| 778 | << (s->coded_channels - 1) << s->duration >> 2; | ||
| 779 | bits2[i] = high >= CELT_VECTORS ? cap[i] : | ||
| 780 | celt_freq_range[i] * celt_static_alloc[high][i] | ||
| 781 | << (s->coded_channels - 1) << s->duration >> 2; | ||
| 782 | |||
| 783 | if (bits1[i]) | ||
| 784 | bits1[i] = FFMAX(0, bits1[i] + trim_offset[i]); | ||
| 785 | if (bits2[i]) | ||
| 786 | bits2[i] = FFMAX(0, bits2[i] + trim_offset[i]); | ||
| 787 | if (low) | ||
| 788 | bits1[i] += boost[i]; | ||
| 789 | bits2[i] += boost[i]; | ||
| 790 | |||
| 791 | if (boost[i]) | ||
| 792 | skip_startband = i; | ||
| 793 | bits2[i] = FFMAX(0, bits2[i] - bits1[i]); | ||
| 794 | } | ||
| 795 | |||
| 796 | /* bisection */ | ||
| 797 | low = 0; | ||
| 798 | high = 1 << CELT_ALLOC_STEPS; | ||
| 799 | for (i = 0; i < CELT_ALLOC_STEPS; i++) { | ||
| 800 | int center = (low + high) >> 1; | ||
| 801 | done = total = 0; | ||
| 802 | |||
| 803 | for (j = s->endband - 1; j >= s->startband; j--) { | ||
| 804 | bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS); | ||
| 805 | |||
| 806 | if (bandbits >= threshold[j] || done) { | ||
| 807 | done = 1; | ||
| 808 | total += FFMIN(bandbits, cap[j]); | ||
| 809 | } else if (bandbits >= s->coded_channels << 3) | ||
| 810 | total += s->coded_channels << 3; | ||
| 811 | } | ||
| 812 | if (total > totalbits) | ||
| 813 | high = center; | ||
| 814 | else | ||
| 815 | low = center; | ||
| 816 | } | ||
| 817 | |||
| 818 | done = total = 0; | ||
| 819 | for (i = s->endband - 1; i >= s->startband; i--) { | ||
| 820 | bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS); | ||
| 821 | |||
| 822 | if (bandbits >= threshold[i] || done) | ||
| 823 | done = 1; | ||
| 824 | else | ||
| 825 | bandbits = (bandbits >= s->coded_channels << 3) ? | ||
| 826 | s->coded_channels << 3 : 0; | ||
| 827 | |||
| 828 | bandbits = FFMIN(bandbits, cap[i]); | ||
| 829 | s->pulses[i] = bandbits; | ||
| 830 | total += bandbits; | ||
| 831 | } | ||
| 832 | |||
| 833 | /* band skipping */ | ||
| 834 | for (s->codedbands = s->endband; ; s->codedbands--) { | ||
| 835 | int allocation; | ||
| 836 | j = s->codedbands - 1; | ||
| 837 | |||
| 838 | if (j == skip_startband) { | ||
| 839 | /* all remaining bands are not skipped */ | ||
| 840 | totalbits += skip_bit; | ||
| 841 | break; | ||
| 842 | } | ||
| 843 | |||
| 844 | /* determine the number of bits available for coding "do not skip" markers */ | ||
| 845 | remaining = totalbits - total; | ||
| 846 | bandbits = remaining / (celt_freq_bands[j+1] - celt_freq_bands[s->startband]); | ||
| 847 | remaining -= bandbits * (celt_freq_bands[j+1] - celt_freq_bands[s->startband]); | ||
| 848 | allocation = s->pulses[j] + bandbits * celt_freq_range[j] | ||
| 849 | + FFMAX(0, remaining - (celt_freq_bands[j] - celt_freq_bands[s->startband])); | ||
| 850 | |||
| 851 | /* a "do not skip" marker is only coded if the allocation is | ||
| 852 | above the chosen threshold */ | ||
| 853 | if (allocation >= FFMAX(threshold[j], (s->coded_channels + 1) <<3 )) { | ||
| 854 | if (opus_rc_p2model(rc, 1)) | ||
| 855 | break; | ||
| 856 | |||
| 857 | total += 1 << 3; | ||
| 858 | allocation -= 1 << 3; | ||
| 859 | } | ||
| 860 | |||
| 861 | /* the band is skipped, so reclaim its bits */ | ||
| 862 | total -= s->pulses[j]; | ||
| 863 | if (intensitystereo_bit) { | ||
| 864 | total -= intensitystereo_bit; | ||
| 865 | intensitystereo_bit = celt_log2_frac[j - s->startband]; | ||
| 866 | total += intensitystereo_bit; | ||
| 867 | } | ||
| 868 | |||
| 869 | total += s->pulses[j] = (allocation >= s->coded_channels << 3) ? | ||
| 870 | s->coded_channels << 3 : 0; | ||
| 871 | } | ||
| 872 | |||
| 873 | /* obtain stereo flags */ | ||
| 874 | s->intensitystereo = 0; | ||
| 875 | s->dualstereo = 0; | ||
| 876 | if (intensitystereo_bit) | ||
| 877 | s->intensitystereo = s->startband + | ||
| 878 | opus_rc_unimodel(rc, s->codedbands + 1 - s->startband); | ||
| 879 | if (s->intensitystereo <= s->startband) | ||
| 880 | totalbits += dualstereo_bit; /* no intensity stereo means no dual stereo */ | ||
| 881 | else if (dualstereo_bit) | ||
| 882 | s->dualstereo = opus_rc_p2model(rc, 1); | ||
| 883 | |||
| 884 | /* supply the remaining bits in this frame to lower bands */ | ||
| 885 | remaining = totalbits - total; | ||
| 886 | bandbits = remaining / (celt_freq_bands[s->codedbands] - celt_freq_bands[s->startband]); | ||
| 887 | remaining -= bandbits * (celt_freq_bands[s->codedbands] - celt_freq_bands[s->startband]); | ||
| 888 | for (i = s->startband; i < s->codedbands; i++) { | ||
| 889 | int bits = FFMIN(remaining, celt_freq_range[i]); | ||
| 890 | |||
| 891 | s->pulses[i] += bits + bandbits * celt_freq_range[i]; | ||
| 892 | remaining -= bits; | ||
| 893 | } | ||
| 894 | |||
| 895 | for (i = s->startband; i < s->codedbands; i++) { | ||
| 896 | int N = celt_freq_range[i] << s->duration; | ||
| 897 | int prev_extra = extrabits; | ||
| 898 | s->pulses[i] += extrabits; | ||
| 899 | |||
| 900 | if (N > 1) { | ||
| 901 | int dof; // degrees of freedom | ||
| 902 | int temp; // dof * channels * log(dof) | ||
| 903 | int offset; // fine energy quantization offset, i.e. | ||
| 904 | // extra bits assigned over the standard | ||
| 905 | // totalbits/dof | ||
| 906 | int fine_bits, max_bits; | ||
| 907 | |||
| 908 | extrabits = FFMAX(0, s->pulses[i] - cap[i]); | ||
| 909 | s->pulses[i] -= extrabits; | ||
| 910 | |||
| 911 | /* intensity stereo makes use of an extra degree of freedom */ | ||
| 912 | dof = N * s->coded_channels | ||
| 913 | + (s->coded_channels == 2 && N > 2 && !s->dualstereo && i < s->intensitystereo); | ||
| 914 | temp = dof * (celt_log_freq_range[i] + (s->duration<<3)); | ||
| 915 | offset = (temp >> 1) - dof * CELT_FINE_OFFSET; | ||
| 916 | if (N == 2) /* dof=2 is the only case that doesn't fit the model */ | ||
| 917 | offset += dof<<1; | ||
| 918 | |||
| 919 | /* grant an additional bias for the first and second pulses */ | ||
| 920 | if (s->pulses[i] + offset < 2 * (dof << 3)) | ||
| 921 | offset += temp >> 2; | ||
| 922 | else if (s->pulses[i] + offset < 3 * (dof << 3)) | ||
| 923 | offset += temp >> 3; | ||
| 924 | |||
| 925 | fine_bits = (s->pulses[i] + offset + (dof << 2)) / (dof << 3); | ||
| 926 | max_bits = FFMIN((s->pulses[i]>>3) >> (s->coded_channels - 1), | ||
| 927 | CELT_MAX_FINE_BITS); | ||
| 928 | |||
| 929 | max_bits = FFMAX(max_bits, 0); | ||
| 930 | |||
| 931 | s->fine_bits[i] = av_clip(fine_bits, 0, max_bits); | ||
| 932 | |||
| 933 | /* if fine_bits was rounded down or capped, | ||
| 934 | give priority for the final fine energy pass */ | ||
| 935 | s->fine_priority[i] = (s->fine_bits[i] * (dof<<3) >= s->pulses[i] + offset); | ||
| 936 | |||
| 937 | /* the remaining bits are assigned to PVQ */ | ||
| 938 | s->pulses[i] -= s->fine_bits[i] << (s->coded_channels - 1) << 3; | ||
| 939 | } else { | ||
| 940 | /* all bits go to fine energy except for the sign bit */ | ||
| 941 | extrabits = FFMAX(0, s->pulses[i] - (s->coded_channels << 3)); | ||
| 942 | s->pulses[i] -= extrabits; | ||
| 943 | s->fine_bits[i] = 0; | ||
| 944 | s->fine_priority[i] = 1; | ||
| 945 | } | ||
| 946 | |||
| 947 | /* hand back a limited number of extra fine energy bits to this band */ | ||
| 948 | if (extrabits > 0) { | ||
| 949 | int fineextra = FFMIN(extrabits >> (s->coded_channels + 2), | ||
| 950 | CELT_MAX_FINE_BITS - s->fine_bits[i]); | ||
| 951 | s->fine_bits[i] += fineextra; | ||
| 952 | |||
| 953 | fineextra <<= s->coded_channels + 2; | ||
| 954 | s->fine_priority[i] = (fineextra >= extrabits - prev_extra); | ||
| 955 | extrabits -= fineextra; | ||
| 956 | } | ||
| 957 | } | ||
| 958 | s->remaining = extrabits; | ||
| 959 | |||
| 960 | /* skipped bands dedicate all of their bits for fine energy */ | ||
| 961 | for (; i < s->endband; i++) { | ||
| 962 | s->fine_bits[i] = s->pulses[i] >> (s->coded_channels - 1) >> 3; | ||
| 963 | s->pulses[i] = 0; | ||
| 964 | s->fine_priority[i] = s->fine_bits[i] < 1; | ||
| 965 | } | ||
| 966 | } | ||
| 967 | |||
| 968 | static inline int celt_bits2pulses(const uint8_t *cache, int bits) | ||
| 969 | { | ||
| 970 | // TODO: Find the size of cache and make it into an array in the parameters list | ||
| 971 | int i, low = 0, high; | ||
| 972 | |||
| 973 | high = cache[0]; | ||
| 974 | bits--; | ||
| 975 | |||
| 976 | for (i = 0; i < 6; i++) { | ||
| 977 | int center = (low + high + 1) >> 1; | ||
| 978 | if (cache[center] >= bits) | ||
| 979 | high = center; | ||
| 980 | else | ||
| 981 | low = center; | ||
| 982 | } | ||
| 983 | |||
| 984 | return (bits - (low == 0 ? -1 : cache[low]) <= cache[high] - bits) ? low : high; | ||
| 985 | } | ||
| 986 | |||
| 987 | static inline int celt_pulses2bits(const uint8_t *cache, int pulses) | ||
| 988 | { | ||
| 989 | // TODO: Find the size of cache and make it into an array in the parameters list | ||
| 990 | return (pulses == 0) ? 0 : cache[pulses] + 1; | ||
| 991 | } | ||
| 992 | |||
| 993 | static inline void celt_normalize_residual(const int * restrict iy, float * restrict X, | ||
| 994 | int N, float g) | ||
| 995 | { | ||
| 996 | int i; | ||
| 997 | for (i = 0; i < N; i++) | ||
| 998 | X[i] = g * iy[i]; | ||
| 999 | } | ||
| 1000 | |||
| 1001 | static void celt_exp_rotation1(float *X, unsigned int len, unsigned int stride, | ||
| 1002 | float c, float s) | ||
| 1003 | { | ||
| 1004 | float *Xptr; | ||
| 1005 | int i; | ||
| 1006 | |||
| 1007 | Xptr = X; | ||
| 1008 | for (i = 0; i < len - stride; i++) { | ||
| 1009 | float x1, x2; | ||
| 1010 | x1 = Xptr[0]; | ||
| 1011 | x2 = Xptr[stride]; | ||
| 1012 | Xptr[stride] = c * x2 + s * x1; | ||
| 1013 | *Xptr++ = c * x1 - s * x2; | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | Xptr = &X[len - 2 * stride - 1]; | ||
| 1017 | for (i = len - 2 * stride - 1; i >= 0; i--) { | ||
| 1018 | float x1, x2; | ||
| 1019 | x1 = Xptr[0]; | ||
| 1020 | x2 = Xptr[stride]; | ||
| 1021 | Xptr[stride] = c * x2 + s * x1; | ||
| 1022 | *Xptr-- = c * x1 - s * x2; | ||
| 1023 | } | ||
| 1024 | } | ||
| 1025 | |||
| 1026 | static inline void celt_exp_rotation(float *X, unsigned int len, | ||
| 1027 | unsigned int stride, unsigned int K, | ||
| 1028 | enum CeltSpread spread) | ||
| 1029 | { | ||
| 1030 | unsigned int stride2 = 0; | ||
| 1031 | float c, s; | ||
| 1032 | float gain, theta; | ||
| 1033 | int i; | ||
| 1034 | |||
| 1035 | if (2*K >= len || spread == CELT_SPREAD_NONE) | ||
| 1036 | return; | ||
| 1037 | |||
| 1038 | gain = (float)len / (len + (20 - 5*spread) * K); | ||
| 1039 | theta = M_PI * gain * gain / 4; | ||
| 1040 | |||
| 1041 | c = cos(theta); | ||
| 1042 | s = sin(theta); | ||
| 1043 | |||
| 1044 | if (len >= stride << 3) { | ||
| 1045 | stride2 = 1; | ||
| 1046 | /* This is just a simple (equivalent) way of computing sqrt(len/stride) with rounding. | ||
| 1047 | It's basically incrementing long as (stride2+0.5)^2 < len/stride. */ | ||
| 1048 | while ((stride2 * stride2 + stride2) * stride + (stride >> 2) < len) | ||
| 1049 | stride2++; | ||
| 1050 | } | ||
| 1051 | |||
| 1052 | /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for | ||
| 1053 | extract_collapse_mask().*/ | ||
| 1054 | len /= stride; | ||
| 1055 | for (i = 0; i < stride; i++) { | ||
| 1056 | if (stride2) | ||
| 1057 | celt_exp_rotation1(X + i * len, len, stride2, s, c); | ||
| 1058 | celt_exp_rotation1(X + i * len, len, 1, c, s); | ||
| 1059 | } | ||
| 1060 | } | ||
| 1061 | |||
| 1062 | static inline unsigned int celt_extract_collapse_mask(const int *iy, | ||
| 1063 | unsigned int N, | ||
| 1064 | unsigned int B) | ||
| 1065 | { | ||
| 1066 | unsigned int collapse_mask; | ||
| 1067 | int N0; | ||
| 1068 | int i, j; | ||
| 1069 | |||
| 1070 | if (B <= 1) | ||
| 1071 | return 1; | ||
| 1072 | |||
| 1073 | /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for | ||
| 1074 | exp_rotation().*/ | ||
| 1075 | N0 = N/B; | ||
| 1076 | collapse_mask = 0; | ||
| 1077 | for (i = 0; i < B; i++) | ||
| 1078 | for (j = 0; j < N0; j++) | ||
| 1079 | collapse_mask |= (iy[i*N0+j]!=0)<<i; | ||
| 1080 | return collapse_mask; | ||
| 1081 | } | ||
| 1082 | |||
| 1083 | static inline void celt_renormalize_vector(float *X, int N, float gain) | ||
| 1084 | { | ||
| 1085 | int i; | ||
| 1086 | float g = 1e-15f; | ||
| 1087 | for (i = 0; i < N; i++) | ||
| 1088 | g += X[i] * X[i]; | ||
| 1089 | g = gain / sqrtf(g); | ||
| 1090 | |||
| 1091 | for (i = 0; i < N; i++) | ||
| 1092 | X[i] *= g; | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | static inline void celt_stereo_merge(float *X, float *Y, float mid, int N) | ||
| 1096 | { | ||
| 1097 | int i; | ||
| 1098 | float xp = 0, side = 0; | ||
| 1099 | float E[2]; | ||
| 1100 | float mid2; | ||
| 1101 | float t, gain[2]; | ||
| 1102 | |||
| 1103 | /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */ | ||
| 1104 | for (i = 0; i < N; i++) { | ||
| 1105 | xp += X[i] * Y[i]; | ||
| 1106 | side += Y[i] * Y[i]; | ||
| 1107 | } | ||
| 1108 | |||
| 1109 | /* Compensating for the mid normalization */ | ||
| 1110 | xp *= mid; | ||
| 1111 | mid2 = mid; | ||
| 1112 | E[0] = mid2 * mid2 + side - 2 * xp; | ||
| 1113 | E[1] = mid2 * mid2 + side + 2 * xp; | ||
| 1114 | if (E[0] < 6e-4f || E[1] < 6e-4f) { | ||
| 1115 | for (i = 0; i < N; i++) | ||
| 1116 | Y[i] = X[i]; | ||
| 1117 | return; | ||
| 1118 | } | ||
| 1119 | |||
| 1120 | t = E[0]; | ||
| 1121 | gain[0] = 1.0f / sqrtf(t); | ||
| 1122 | t = E[1]; | ||
| 1123 | gain[1] = 1.0f / sqrtf(t); | ||
| 1124 | |||
| 1125 | for (i = 0; i < N; i++) { | ||
| 1126 | float value[2]; | ||
| 1127 | /* Apply mid scaling (side is already scaled) */ | ||
| 1128 | value[0] = mid * X[i]; | ||
| 1129 | value[1] = Y[i]; | ||
| 1130 | X[i] = gain[0] * (value[0] - value[1]); | ||
| 1131 | Y[i] = gain[1] * (value[0] + value[1]); | ||
| 1132 | } | ||
| 1133 | } | ||
| 1134 | |||
| 1135 | static void celt_interleave_hadamard(float *tmp, float *X, int N0, | ||
| 1136 | int stride, int hadamard) | ||
| 1137 | { | ||
| 1138 | int i, j; | ||
| 1139 | int N = N0*stride; | ||
| 1140 | |||
| 1141 | if (hadamard) { | ||
| 1142 | const uint8_t *ordery = celt_hadamard_ordery + stride - 2; | ||
| 1143 | for (i = 0; i < stride; i++) | ||
| 1144 | for (j = 0; j < N0; j++) | ||
| 1145 | tmp[j*stride+i] = X[ordery[i]*N0+j]; | ||
| 1146 | } else { | ||
| 1147 | for (i = 0; i < stride; i++) | ||
| 1148 | for (j = 0; j < N0; j++) | ||
| 1149 | tmp[j*stride+i] = X[i*N0+j]; | ||
| 1150 | } | ||
| 1151 | |||
| 1152 | for (i = 0; i < N; i++) | ||
| 1153 | X[i] = tmp[i]; | ||
| 1154 | } | ||
| 1155 | |||
| 1156 | static void celt_deinterleave_hadamard(float *tmp, float *X, int N0, | ||
| 1157 | int stride, int hadamard) | ||
| 1158 | { | ||
| 1159 | int i, j; | ||
| 1160 | int N = N0*stride; | ||
| 1161 | |||
| 1162 | if (hadamard) { | ||
| 1163 | const uint8_t *ordery = celt_hadamard_ordery + stride - 2; | ||
| 1164 | for (i = 0; i < stride; i++) | ||
| 1165 | for (j = 0; j < N0; j++) | ||
| 1166 | tmp[ordery[i]*N0+j] = X[j*stride+i]; | ||
| 1167 | } else { | ||
| 1168 | for (i = 0; i < stride; i++) | ||
| 1169 | for (j = 0; j < N0; j++) | ||
| 1170 | tmp[i*N0+j] = X[j*stride+i]; | ||
| 1171 | } | ||
| 1172 | |||
| 1173 | for (i = 0; i < N; i++) | ||
| 1174 | X[i] = tmp[i]; | ||
| 1175 | } | ||
| 1176 | |||
| 1177 | static void celt_haar1(float *X, int N0, int stride) | ||
| 1178 | { | ||
| 1179 | int i, j; | ||
| 1180 | N0 >>= 1; | ||
| 1181 | for (i = 0; i < stride; i++) { | ||
| 1182 | for (j = 0; j < N0; j++) { | ||
| 1183 | float x0 = X[stride * (2 * j + 0) + i]; | ||
| 1184 | float x1 = X[stride * (2 * j + 1) + i]; | ||
| 1185 | X[stride * (2 * j + 0) + i] = (x0 + x1) * M_SQRT1_2; | ||
| 1186 | X[stride * (2 * j + 1) + i] = (x0 - x1) * M_SQRT1_2; | ||
| 1187 | } | ||
| 1188 | } | ||
| 1189 | } | ||
| 1190 | |||
| 1191 | static inline int celt_compute_qn(int N, int b, int offset, int pulse_cap, | ||
| 1192 | int dualstereo) | ||
| 1193 | { | ||
| 1194 | int qn, qb; | ||
| 1195 | int N2 = 2 * N - 1; | ||
| 1196 | if (dualstereo && N == 2) | ||
| 1197 | N2--; | ||
| 1198 | |||
| 1199 | /* The upper limit ensures that in a stereo split with itheta==16384, we'll | ||
| 1200 | * always have enough bits left over to code at least one pulse in the | ||
| 1201 | * side; otherwise it would collapse, since it doesn't get folded. */ | ||
| 1202 | qb = FFMIN3(b - pulse_cap - (4 << 3), (b + N2 * offset) / N2, 8 << 3); | ||
| 1203 | qn = (qb < (1 << 3 >> 1)) ? 1 : ((celt_qn_exp2[qb & 0x7] >> (14 - (qb >> 3))) + 1) >> 1 << 1; | ||
| 1204 | return qn; | ||
| 1205 | } | ||
| 1206 | |||
| 1207 | // this code was adapted from libopus | ||
| 1208 | static inline uint64_t celt_cwrsi(unsigned int N, unsigned int K, unsigned int i, int *y) | ||
| 1209 | { | ||
| 1210 | uint64_t norm = 0; | ||
| 1211 | uint32_t p; | ||
| 1212 | int s, val; | ||
| 1213 | int k0; | ||
| 1214 | |||
| 1215 | while (N > 2) { | ||
| 1216 | uint32_t q; | ||
| 1217 | |||
| 1218 | /*Lots of pulses case:*/ | ||
| 1219 | if (K >= N) { | ||
| 1220 | const uint32_t *row = celt_pvq_u_row[N]; | ||
| 1221 | |||
| 1222 | /* Are the pulses in this dimension negative? */ | ||
| 1223 | p = row[K + 1]; | ||
| 1224 | s = -(i >= p); | ||
| 1225 | i -= p & s; | ||
| 1226 | |||
| 1227 | /*Count how many pulses were placed in this dimension.*/ | ||
| 1228 | k0 = K; | ||
| 1229 | q = row[N]; | ||
| 1230 | if (q > i) { | ||
| 1231 | K = N; | ||
| 1232 | do { | ||
| 1233 | p = celt_pvq_u_row[--K][N]; | ||
| 1234 | } while (p > i); | ||
| 1235 | } else | ||
| 1236 | for (p = row[K]; p > i; p = row[K]) | ||
| 1237 | K--; | ||
| 1238 | |||
| 1239 | i -= p; | ||
| 1240 | val = (k0 - K + s) ^ s; | ||
| 1241 | norm += val * val; | ||
| 1242 | *y++ = val; | ||
| 1243 | } else { /*Lots of dimensions case:*/ | ||
| 1244 | /*Are there any pulses in this dimension at all?*/ | ||
| 1245 | p = celt_pvq_u_row[K ][N]; | ||
| 1246 | q = celt_pvq_u_row[K + 1][N]; | ||
| 1247 | |||
| 1248 | if (p <= i && i < q) { | ||
| 1249 | i -= p; | ||
| 1250 | *y++ = 0; | ||
| 1251 | } else { | ||
| 1252 | /*Are the pulses in this dimension negative?*/ | ||
| 1253 | s = -(i >= q); | ||
| 1254 | i -= q & s; | ||
| 1255 | |||
| 1256 | /*Count how many pulses were placed in this dimension.*/ | ||
| 1257 | k0 = K; | ||
| 1258 | do p = celt_pvq_u_row[--K][N]; | ||
| 1259 | while (p > i); | ||
| 1260 | |||
| 1261 | i -= p; | ||
| 1262 | val = (k0 - K + s) ^ s; | ||
| 1263 | norm += val * val; | ||
| 1264 | *y++ = val; | ||
| 1265 | } | ||
| 1266 | } | ||
| 1267 | N--; | ||
| 1268 | } | ||
| 1269 | |||
| 1270 | /* N == 2 */ | ||
| 1271 | p = 2 * K + 1; | ||
| 1272 | s = -(i >= p); | ||
| 1273 | i -= p & s; | ||
| 1274 | k0 = K; | ||
| 1275 | K = (i + 1) / 2; | ||
| 1276 | |||
| 1277 | if (K) | ||
| 1278 | i -= 2 * K - 1; | ||
| 1279 | |||
| 1280 | val = (k0 - K + s) ^ s; | ||
| 1281 | norm += val * val; | ||
| 1282 | *y++ = val; | ||
| 1283 | |||
| 1284 | /* N==1 */ | ||
| 1285 | s = -i; | ||
| 1286 | val = (K + s) ^ s; | ||
| 1287 | norm += val * val; | ||
| 1288 | *y = val; | ||
| 1289 | |||
| 1290 | return norm; | ||
| 1291 | } | ||
| 1292 | |||
| 1293 | static inline float celt_decode_pulses(OpusRangeCoder *rc, int *y, unsigned int N, unsigned int K) | ||
| 1294 | { | ||
| 1295 | unsigned int idx; | ||
| 1296 | #define CELT_PVQ_U(n, k) (celt_pvq_u_row[FFMIN(n, k)][FFMAX(n, k)]) | ||
| 1297 | #define CELT_PVQ_V(n, k) (CELT_PVQ_U(n, k) + CELT_PVQ_U(n, k + 1)) | ||
| 1298 | idx = opus_rc_unimodel(rc, CELT_PVQ_V(N, K)); | ||
| 1299 | return celt_cwrsi(N, K, idx, y); | ||
| 1300 | } | ||
| 1301 | |||
| 1302 | /** Decode pulse vector and combine the result with the pitch vector to produce | ||
| 1303 | the final normalised signal in the current band. */ | ||
| 1304 | static inline unsigned int celt_alg_unquant(OpusRangeCoder *rc, float *X, | ||
| 1305 | unsigned int N, unsigned int K, | ||
| 1306 | enum CeltSpread spread, | ||
| 1307 | unsigned int blocks, float gain) | ||
| 1308 | { | ||
| 1309 | int y[176]; | ||
| 1310 | |||
| 1311 | gain /= sqrtf(celt_decode_pulses(rc, y, N, K)); | ||
| 1312 | celt_normalize_residual(y, X, N, gain); | ||
| 1313 | celt_exp_rotation(X, N, blocks, K, spread); | ||
| 1314 | return celt_extract_collapse_mask(y, N, blocks); | ||
| 1315 | } | ||
| 1316 | |||
| 1317 | static unsigned int celt_decode_band(CeltContext *s, OpusRangeCoder *rc, | ||
| 1318 | const int band, float *X, float *Y, | ||
| 1319 | int N, int b, unsigned int blocks, | ||
| 1320 | float *lowband, int duration, | ||
| 1321 | float *lowband_out, int level, | ||
| 1322 | float gain, float *lowband_scratch, | ||
| 1323 | int fill) | ||
| 1324 | { | ||
| 1325 | const uint8_t *cache; | ||
| 1326 | int dualstereo, split; | ||
| 1327 | int imid = 0, iside = 0; | ||
| 1328 | unsigned int N0 = N; | ||
| 1329 | int N_B; | ||
| 1330 | int N_B0; | ||
| 1331 | int B0 = blocks; | ||
| 1332 | int time_divide = 0; | ||
| 1333 | int recombine = 0; | ||
| 1334 | int inv = 0; | ||
| 1335 | float mid = 0, side = 0; | ||
| 1336 | int longblocks = (B0 == 1); | ||
| 1337 | unsigned int cm = 0; | ||
| 1338 | |||
| 1339 | N_B0 = N_B = N / blocks; | ||
| 1340 | split = dualstereo = (Y != NULL); | ||
| 1341 | |||
| 1342 | if (N == 1) { | ||
| 1343 | /* special case for one sample */ | ||
| 1344 | int i; | ||
| 1345 | float *x = X; | ||
| 1346 | for (i = 0; i <= dualstereo; i++) { | ||
| 1347 | int sign = 0; | ||
| 1348 | if (s->remaining2 >= 1<<3) { | ||
| 1349 | sign = opus_getrawbits(rc, 1); | ||
| 1350 | s->remaining2 -= 1 << 3; | ||
| 1351 | b -= 1 << 3; | ||
| 1352 | } | ||
| 1353 | x[0] = sign ? -1.0f : 1.0f; | ||
| 1354 | x = Y; | ||
| 1355 | } | ||
| 1356 | if (lowband_out) | ||
| 1357 | lowband_out[0] = X[0]; | ||
| 1358 | return 1; | ||
| 1359 | } | ||
| 1360 | |||
| 1361 | if (!dualstereo && level == 0) { | ||
| 1362 | int tf_change = s->tf_change[band]; | ||
| 1363 | int k; | ||
| 1364 | if (tf_change > 0) | ||
| 1365 | recombine = tf_change; | ||
| 1366 | /* Band recombining to increase frequency resolution */ | ||
| 1367 | |||
| 1368 | if (lowband && | ||
| 1369 | (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) { | ||
| 1370 | int j; | ||
| 1371 | for (j = 0; j < N; j++) | ||
| 1372 | lowband_scratch[j] = lowband[j]; | ||
| 1373 | lowband = lowband_scratch; | ||
| 1374 | } | ||
| 1375 | |||
| 1376 | for (k = 0; k < recombine; k++) { | ||
| 1377 | if (lowband) | ||
| 1378 | celt_haar1(lowband, N >> k, 1 << k); | ||
| 1379 | fill = celt_bit_interleave[fill & 0xF] | celt_bit_interleave[fill >> 4] << 2; | ||
| 1380 | } | ||
| 1381 | blocks >>= recombine; | ||
| 1382 | N_B <<= recombine; | ||
| 1383 | |||
| 1384 | /* Increasing the time resolution */ | ||
| 1385 | while ((N_B & 1) == 0 && tf_change < 0) { | ||
| 1386 | if (lowband) | ||
| 1387 | celt_haar1(lowband, N_B, blocks); | ||
| 1388 | fill |= fill << blocks; | ||
| 1389 | blocks <<= 1; | ||
| 1390 | N_B >>= 1; | ||
| 1391 | time_divide++; | ||
| 1392 | tf_change++; | ||
| 1393 | } | ||
| 1394 | B0 = blocks; | ||
| 1395 | N_B0 = N_B; | ||
| 1396 | |||
| 1397 | /* Reorganize the samples in time order instead of frequency order */ | ||
| 1398 | if (B0 > 1 && lowband) | ||
| 1399 | celt_deinterleave_hadamard(s->scratch, lowband, N_B >> recombine, | ||
| 1400 | B0 << recombine, longblocks); | ||
| 1401 | } | ||
| 1402 | |||
| 1403 | /* If we need 1.5 more bit than we can produce, split the band in two. */ | ||
| 1404 | cache = celt_cache_bits + | ||
| 1405 | celt_cache_index[(duration + 1) * CELT_MAX_BANDS + band]; | ||
| 1406 | if (!dualstereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) { | ||
| 1407 | N >>= 1; | ||
| 1408 | Y = X + N; | ||
| 1409 | split = 1; | ||
| 1410 | duration -= 1; | ||
| 1411 | if (blocks == 1) | ||
| 1412 | fill = (fill & 1) | (fill << 1); | ||
| 1413 | blocks = (blocks + 1) >> 1; | ||
| 1414 | } | ||
| 1415 | |||
| 1416 | if (split) { | ||
| 1417 | int qn; | ||
| 1418 | int itheta = 0; | ||
| 1419 | int mbits, sbits, delta; | ||
| 1420 | int qalloc; | ||
| 1421 | int pulse_cap; | ||
| 1422 | int offset; | ||
| 1423 | int orig_fill; | ||
| 1424 | int tell; | ||
| 1425 | |||
| 1426 | /* Decide on the resolution to give to the split parameter theta */ | ||
| 1427 | pulse_cap = celt_log_freq_range[band] + duration * 8; | ||
| 1428 | offset = (pulse_cap >> 1) - (dualstereo && N == 2 ? CELT_QTHETA_OFFSET_TWOPHASE : | ||
| 1429 | CELT_QTHETA_OFFSET); | ||
| 1430 | qn = (dualstereo && band >= s->intensitystereo) ? 1 : | ||
| 1431 | celt_compute_qn(N, b, offset, pulse_cap, dualstereo); | ||
| 1432 | tell = opus_rc_tell_frac(rc); | ||
| 1433 | if (qn != 1) { | ||
| 1434 | /* Entropy coding of the angle. We use a uniform pdf for the | ||
| 1435 | time split, a step for stereo, and a triangular one for the rest. */ | ||
| 1436 | if (dualstereo && N > 2) | ||
| 1437 | itheta = opus_rc_stepmodel(rc, qn/2); | ||
| 1438 | else if (dualstereo || B0 > 1) | ||
| 1439 | itheta = opus_rc_unimodel(rc, qn+1); | ||
| 1440 | else | ||
| 1441 | itheta = opus_rc_trimodel(rc, qn); | ||
| 1442 | itheta = itheta * 16384 / qn; | ||
| 1443 | /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate. | ||
| 1444 | Let's do that at higher complexity */ | ||
| 1445 | } else if (dualstereo) { | ||
| 1446 | inv = (b > 2 << 3 && s->remaining2 > 2 << 3) ? opus_rc_p2model(rc, 2) : 0; | ||
| 1447 | itheta = 0; | ||
| 1448 | } | ||
| 1449 | qalloc = opus_rc_tell_frac(rc) - tell; | ||
| 1450 | b -= qalloc; | ||
| 1451 | |||
| 1452 | orig_fill = fill; | ||
| 1453 | if (itheta == 0) { | ||
| 1454 | imid = 32767; | ||
| 1455 | iside = 0; | ||
| 1456 | fill &= (1 << blocks) - 1; | ||
| 1457 | delta = -16384; | ||
| 1458 | } else if (itheta == 16384) { | ||
| 1459 | imid = 0; | ||
| 1460 | iside = 32767; | ||
| 1461 | fill &= ((1 << blocks) - 1) << blocks; | ||
| 1462 | delta = 16384; | ||
| 1463 | } else { | ||
| 1464 | imid = celt_cos(itheta); | ||
| 1465 | iside = celt_cos(16384-itheta); | ||
| 1466 | /* This is the mid vs side allocation that minimizes squared error | ||
| 1467 | in that band. */ | ||
| 1468 | delta = ROUND_MUL16((N - 1) << 7, celt_log2tan(iside, imid)); | ||
| 1469 | } | ||
| 1470 | |||
| 1471 | mid = imid / 32768.0f; | ||
| 1472 | side = iside / 32768.0f; | ||
| 1473 | |||
| 1474 | /* This is a special case for N=2 that only works for stereo and takes | ||
| 1475 | advantage of the fact that mid and side are orthogonal to encode | ||
| 1476 | the side with just one bit. */ | ||
| 1477 | if (N == 2 && dualstereo) { | ||
| 1478 | int c; | ||
| 1479 | int sign = 0; | ||
| 1480 | float tmp; | ||
| 1481 | float *x2, *y2; | ||
| 1482 | mbits = b; | ||
| 1483 | /* Only need one bit for the side */ | ||
| 1484 | sbits = (itheta != 0 && itheta != 16384) ? 1 << 3 : 0; | ||
| 1485 | mbits -= sbits; | ||
| 1486 | c = (itheta > 8192); | ||
| 1487 | s->remaining2 -= qalloc+sbits; | ||
| 1488 | |||
| 1489 | x2 = c ? Y : X; | ||
| 1490 | y2 = c ? X : Y; | ||
| 1491 | if (sbits) | ||
| 1492 | sign = opus_getrawbits(rc, 1); | ||
| 1493 | sign = 1 - 2 * sign; | ||
| 1494 | /* We use orig_fill here because we want to fold the side, but if | ||
| 1495 | itheta==16384, we'll have cleared the low bits of fill. */ | ||
| 1496 | cm = celt_decode_band(s, rc, band, x2, NULL, N, mbits, blocks, | ||
| 1497 | lowband, duration, lowband_out, level, gain, | ||
| 1498 | lowband_scratch, orig_fill); | ||
| 1499 | /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse), | ||
| 1500 | and there's no need to worry about mixing with the other channel. */ | ||
| 1501 | y2[0] = -sign * x2[1]; | ||
| 1502 | y2[1] = sign * x2[0]; | ||
| 1503 | X[0] *= mid; | ||
| 1504 | X[1] *= mid; | ||
| 1505 | Y[0] *= side; | ||
| 1506 | Y[1] *= side; | ||
| 1507 | tmp = X[0]; | ||
| 1508 | X[0] = tmp - Y[0]; | ||
| 1509 | Y[0] = tmp + Y[0]; | ||
| 1510 | tmp = X[1]; | ||
| 1511 | X[1] = tmp - Y[1]; | ||
| 1512 | Y[1] = tmp + Y[1]; | ||
| 1513 | } else { | ||
| 1514 | /* "Normal" split code */ | ||
| 1515 | float *next_lowband2 = NULL; | ||
| 1516 | float *next_lowband_out1 = NULL; | ||
| 1517 | int next_level = 0; | ||
| 1518 | int rebalance; | ||
| 1519 | |||
| 1520 | /* Give more bits to low-energy MDCTs than they would | ||
| 1521 | * otherwise deserve */ | ||
| 1522 | if (B0 > 1 && !dualstereo && (itheta & 0x3fff)) { | ||
| 1523 | if (itheta > 8192) | ||
| 1524 | /* Rough approximation for pre-echo masking */ | ||
| 1525 | delta -= delta >> (4 - duration); | ||
| 1526 | else | ||
| 1527 | /* Corresponds to a forward-masking slope of | ||
| 1528 | * 1.5 dB per 10 ms */ | ||
| 1529 | delta = FFMIN(0, delta + (N << 3 >> (5 - duration))); | ||
| 1530 | } | ||
| 1531 | mbits = av_clip((b - delta) / 2, 0, b); | ||
| 1532 | sbits = b - mbits; | ||
| 1533 | s->remaining2 -= qalloc; | ||
| 1534 | |||
| 1535 | if (lowband && !dualstereo) | ||
| 1536 | next_lowband2 = lowband + N; /* >32-bit split case */ | ||
| 1537 | |||
| 1538 | /* Only stereo needs to pass on lowband_out. | ||
| 1539 | * Otherwise, it's handled at the end */ | ||
| 1540 | if (dualstereo) | ||
| 1541 | next_lowband_out1 = lowband_out; | ||
| 1542 | else | ||
| 1543 | next_level = level + 1; | ||
| 1544 | |||
| 1545 | rebalance = s->remaining2; | ||
| 1546 | if (mbits >= sbits) { | ||
| 1547 | /* In stereo mode, we do not apply a scaling to the mid | ||
| 1548 | * because we need the normalized mid for folding later */ | ||
| 1549 | cm = celt_decode_band(s, rc, band, X, NULL, N, mbits, blocks, | ||
| 1550 | lowband, duration, next_lowband_out1, | ||
| 1551 | next_level, dualstereo ? 1.0f : (gain * mid), | ||
| 1552 | lowband_scratch, fill); | ||
| 1553 | |||
| 1554 | rebalance = mbits - (rebalance - s->remaining2); | ||
| 1555 | if (rebalance > 3 << 3 && itheta != 0) | ||
| 1556 | sbits += rebalance - (3 << 3); | ||
| 1557 | |||
| 1558 | /* For a stereo split, the high bits of fill are always zero, | ||
| 1559 | * so no folding will be done to the side. */ | ||
| 1560 | cm |= celt_decode_band(s, rc, band, Y, NULL, N, sbits, blocks, | ||
| 1561 | next_lowband2, duration, NULL, | ||
| 1562 | next_level, gain * side, NULL, | ||
| 1563 | fill >> blocks) << ((B0 >> 1) & (dualstereo - 1)); | ||
| 1564 | } else { | ||
| 1565 | /* For a stereo split, the high bits of fill are always zero, | ||
| 1566 | * so no folding will be done to the side. */ | ||
| 1567 | cm = celt_decode_band(s, rc, band, Y, NULL, N, sbits, blocks, | ||
| 1568 | next_lowband2, duration, NULL, | ||
| 1569 | next_level, gain * side, NULL, | ||
| 1570 | fill >> blocks) << ((B0 >> 1) & (dualstereo - 1)); | ||
| 1571 | |||
| 1572 | rebalance = sbits - (rebalance - s->remaining2); | ||
| 1573 | if (rebalance > 3 << 3 && itheta != 16384) | ||
| 1574 | mbits += rebalance - (3 << 3); | ||
| 1575 | |||
| 1576 | /* In stereo mode, we do not apply a scaling to the mid because | ||
| 1577 | * we need the normalized mid for folding later */ | ||
| 1578 | cm |= celt_decode_band(s, rc, band, X, NULL, N, mbits, blocks, | ||
| 1579 | lowband, duration, next_lowband_out1, | ||
| 1580 | next_level, dualstereo ? 1.0f : (gain * mid), | ||
| 1581 | lowband_scratch, fill); | ||
| 1582 | } | ||
| 1583 | } | ||
| 1584 | } else { | ||
| 1585 | /* This is the basic no-split case */ | ||
| 1586 | unsigned int q = celt_bits2pulses(cache, b); | ||
| 1587 | unsigned int curr_bits = celt_pulses2bits(cache, q); | ||
| 1588 | s->remaining2 -= curr_bits; | ||
| 1589 | |||
| 1590 | /* Ensures we can never bust the budget */ | ||
| 1591 | while (s->remaining2 < 0 && q > 0) { | ||
| 1592 | s->remaining2 += curr_bits; | ||
| 1593 | curr_bits = celt_pulses2bits(cache, --q); | ||
| 1594 | s->remaining2 -= curr_bits; | ||
| 1595 | } | ||
| 1596 | |||
| 1597 | if (q != 0) { | ||
| 1598 | /* Finally do the actual quantization */ | ||
| 1599 | cm = celt_alg_unquant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1), | ||
| 1600 | s->spread, blocks, gain); | ||
| 1601 | } else { | ||
| 1602 | /* If there's no pulse, fill the band anyway */ | ||
| 1603 | int j; | ||
| 1604 | unsigned int cm_mask = (1 << blocks) - 1; | ||
| 1605 | fill &= cm_mask; | ||
| 1606 | if (!fill) { | ||
| 1607 | for (j = 0; j < N; j++) | ||
| 1608 | X[j] = 0.0f; | ||
| 1609 | } else { | ||
| 1610 | if (lowband == NULL) { | ||
| 1611 | /* Noise */ | ||
| 1612 | for (j = 0; j < N; j++) | ||
| 1613 | X[j] = (((int32_t)celt_rng(s)) >> 20); | ||
| 1614 | cm = cm_mask; | ||
| 1615 | } else { | ||
| 1616 | /* Folded spectrum */ | ||
| 1617 | for (j = 0; j < N; j++) { | ||
| 1618 | /* About 48 dB below the "normal" folding level */ | ||
| 1619 | X[j] = lowband[j] + (((celt_rng(s)) & 0x8000) ? 1.0f / 256 : -1.0f / 256); | ||
| 1620 | } | ||
| 1621 | cm = fill; | ||
| 1622 | } | ||
| 1623 | celt_renormalize_vector(X, N, gain); | ||
| 1624 | } | ||
| 1625 | } | ||
| 1626 | } | ||
| 1627 | |||
| 1628 | /* This code is used by the decoder and by the resynthesis-enabled encoder */ | ||
| 1629 | if (dualstereo) { | ||
| 1630 | int j; | ||
| 1631 | if (N != 2) | ||
| 1632 | celt_stereo_merge(X, Y, mid, N); | ||
| 1633 | if (inv) { | ||
| 1634 | for (j = 0; j < N; j++) | ||
| 1635 | Y[j] *= -1; | ||
| 1636 | } | ||
| 1637 | } else if (level == 0) { | ||
| 1638 | int k; | ||
| 1639 | |||
| 1640 | /* Undo the sample reorganization going from time order to frequency order */ | ||
| 1641 | if (B0 > 1) | ||
| 1642 | celt_interleave_hadamard(s->scratch, X, N_B>>recombine, | ||
| 1643 | B0<<recombine, longblocks); | ||
| 1644 | |||
| 1645 | /* Undo time-freq changes that we did earlier */ | ||
| 1646 | N_B = N_B0; | ||
| 1647 | blocks = B0; | ||
| 1648 | for (k = 0; k < time_divide; k++) { | ||
| 1649 | blocks >>= 1; | ||
| 1650 | N_B <<= 1; | ||
| 1651 | cm |= cm >> blocks; | ||
| 1652 | celt_haar1(X, N_B, blocks); | ||
| 1653 | } | ||
| 1654 | |||
| 1655 | for (k = 0; k < recombine; k++) { | ||
| 1656 | cm = celt_bit_deinterleave[cm]; | ||
| 1657 | celt_haar1(X, N0>>k, 1<<k); | ||
| 1658 | } | ||
| 1659 | blocks <<= recombine; | ||
| 1660 | |||
| 1661 | /* Scale output for later folding */ | ||
| 1662 | if (lowband_out) { | ||
| 1663 | int j; | ||
| 1664 | float n = sqrtf(N0); | ||
| 1665 | for (j = 0; j < N0; j++) | ||
| 1666 | lowband_out[j] = n * X[j]; | ||
| 1667 | } | ||
| 1668 | cm &= (1 << blocks) - 1; | ||
| 1669 | } | ||
| 1670 | return cm; | ||
| 1671 | } | ||
| 1672 | |||
| 1673 | static void celt_denormalize(CeltContext *s, CeltFrame *frame, float *data) | ||
| 1674 | { | ||
| 1675 | int i, j; | ||
| 1676 | |||
| 1677 | for (i = s->startband; i < s->endband; i++) { | ||
| 1678 | float *dst = data + (celt_freq_bands[i] << s->duration); | ||
| 1679 | float norm = pow(2, frame->energy[i] + celt_mean_energy[i]); | ||
| 1680 | |||
| 1681 | for (j = 0; j < celt_freq_range[i] << s->duration; j++) | ||
| 1682 | dst[j] *= norm; | ||
| 1683 | } | ||
| 1684 | } | ||
| 1685 | |||
| 1686 | static void celt_postfilter_apply_transition(CeltFrame *frame, float *data) | ||
| 1687 | { | ||
| 1688 | const int T0 = frame->pf_period_old; | ||
| 1689 | const int T1 = frame->pf_period; | ||
| 1690 | |||
| 1691 | float g00, g01, g02; | ||
| 1692 | float g10, g11, g12; | ||
| 1693 | |||
| 1694 | float x0, x1, x2, x3, x4; | ||
| 1695 | |||
| 1696 | int i; | ||
| 1697 | |||
| 1698 | if (frame->pf_gains[0] == 0.0 && | ||
| 1699 | frame->pf_gains_old[0] == 0.0) | ||
| 1700 | return; | ||
| 1701 | |||
| 1702 | g00 = frame->pf_gains_old[0]; | ||
| 1703 | g01 = frame->pf_gains_old[1]; | ||
| 1704 | g02 = frame->pf_gains_old[2]; | ||
| 1705 | g10 = frame->pf_gains[0]; | ||
| 1706 | g11 = frame->pf_gains[1]; | ||
| 1707 | g12 = frame->pf_gains[2]; | ||
| 1708 | |||
| 1709 | x1 = data[-T1 + 1]; | ||
| 1710 | x2 = data[-T1]; | ||
| 1711 | x3 = data[-T1 - 1]; | ||
| 1712 | x4 = data[-T1 - 2]; | ||
| 1713 | |||
| 1714 | for (i = 0; i < CELT_OVERLAP; i++) { | ||
| 1715 | float w = ff_celt_window2[i]; | ||
| 1716 | x0 = data[i - T1 + 2]; | ||
| 1717 | |||
| 1718 | data[i] += (1.0 - w) * g00 * data[i - T0] + | ||
| 1719 | (1.0 - w) * g01 * (data[i - T0 - 1] + data[i - T0 + 1]) + | ||
| 1720 | (1.0 - w) * g02 * (data[i - T0 - 2] + data[i - T0 + 2]) + | ||
| 1721 | w * g10 * x2 + | ||
| 1722 | w * g11 * (x1 + x3) + | ||
| 1723 | w * g12 * (x0 + x4); | ||
| 1724 | x4 = x3; | ||
| 1725 | x3 = x2; | ||
| 1726 | x2 = x1; | ||
| 1727 | x1 = x0; | ||
| 1728 | } | ||
| 1729 | } | ||
| 1730 | |||
| 1731 | static void celt_postfilter_apply(CeltFrame *frame, | ||
| 1732 | float *data, int len) | ||
| 1733 | { | ||
| 1734 | const int T = frame->pf_period; | ||
| 1735 | float g0, g1, g2; | ||
| 1736 | float x0, x1, x2, x3, x4; | ||
| 1737 | int i; | ||
| 1738 | |||
| 1739 | if (frame->pf_gains[0] == 0.0 || len <= 0) | ||
| 1740 | return; | ||
| 1741 | |||
| 1742 | g0 = frame->pf_gains[0]; | ||
| 1743 | g1 = frame->pf_gains[1]; | ||
| 1744 | g2 = frame->pf_gains[2]; | ||
| 1745 | |||
| 1746 | x4 = data[-T - 2]; | ||
| 1747 | x3 = data[-T - 1]; | ||
| 1748 | x2 = data[-T]; | ||
| 1749 | x1 = data[-T + 1]; | ||
| 1750 | |||
| 1751 | for (i = 0; i < len; i++) { | ||
| 1752 | x0 = data[i - T + 2]; | ||
| 1753 | data[i] += g0 * x2 + | ||
| 1754 | g1 * (x1 + x3) + | ||
| 1755 | g2 * (x0 + x4); | ||
| 1756 | x4 = x3; | ||
| 1757 | x3 = x2; | ||
| 1758 | x2 = x1; | ||
| 1759 | x1 = x0; | ||
| 1760 | } | ||
| 1761 | } | ||
| 1762 | |||
| 1763 | static void celt_postfilter(CeltContext *s, CeltFrame *frame) | ||
| 1764 | { | ||
| 1765 | int len = s->blocksize * s->blocks; | ||
| 1766 | |||
| 1767 | celt_postfilter_apply_transition(frame, frame->buf + 1024); | ||
| 1768 | |||
| 1769 | frame->pf_period_old = frame->pf_period; | ||
| 1770 | memcpy(frame->pf_gains_old, frame->pf_gains, sizeof(frame->pf_gains)); | ||
| 1771 | |||
| 1772 | frame->pf_period = frame->pf_period_new; | ||
| 1773 | memcpy(frame->pf_gains, frame->pf_gains_new, sizeof(frame->pf_gains)); | ||
| 1774 | |||
| 1775 | if (len > CELT_OVERLAP) { | ||
| 1776 | celt_postfilter_apply_transition(frame, frame->buf + 1024 + CELT_OVERLAP); | ||
| 1777 | celt_postfilter_apply(frame, frame->buf + 1024 + 2 * CELT_OVERLAP, | ||
| 1778 | len - 2 * CELT_OVERLAP); | ||
| 1779 | |||
| 1780 | frame->pf_period_old = frame->pf_period; | ||
| 1781 | memcpy(frame->pf_gains_old, frame->pf_gains, sizeof(frame->pf_gains)); | ||
| 1782 | } | ||
| 1783 | |||
| 1784 | memmove(frame->buf, frame->buf + len, (1024 + CELT_OVERLAP / 2) * sizeof(float)); | ||
| 1785 | } | ||
| 1786 | |||
| 1787 | static int parse_postfilter(CeltContext *s, OpusRangeCoder *rc, int consumed) | ||
| 1788 | { | ||
| 1789 | static const float postfilter_taps[3][3] = { | ||
| 1790 | { 0.3066406250f, 0.2170410156f, 0.1296386719f }, | ||
| 1791 | { 0.4638671875f, 0.2680664062f, 0.0 }, | ||
| 1792 | { 0.7998046875f, 0.1000976562f, 0.0 } | ||
| 1793 | }; | ||
| 1794 | int i; | ||
| 1795 | |||
| 1796 | memset(s->frame[0].pf_gains_new, 0, sizeof(s->frame[0].pf_gains_new)); | ||
| 1797 | memset(s->frame[1].pf_gains_new, 0, sizeof(s->frame[1].pf_gains_new)); | ||
| 1798 | |||
| 1799 | if (s->startband == 0 && consumed + 16 <= s->framebits) { | ||
| 1800 | int has_postfilter = opus_rc_p2model(rc, 1); | ||
| 1801 | if (has_postfilter) { | ||
| 1802 | float gain; | ||
| 1803 | int tapset, octave, period; | ||
| 1804 | |||
| 1805 | octave = opus_rc_unimodel(rc, 6); | ||
| 1806 | period = (16 << octave) + opus_getrawbits(rc, 4 + octave) - 1; | ||
| 1807 | gain = 0.09375f * (opus_getrawbits(rc, 3) + 1); | ||
| 1808 | tapset = (opus_rc_tell(rc) + 2 <= s->framebits) ? | ||
| 1809 | opus_rc_getsymbol(rc, celt_model_tapset) : 0; | ||
| 1810 | |||
| 1811 | for (i = 0; i < 2; i++) { | ||
| 1812 | CeltFrame *frame = &s->frame[i]; | ||
| 1813 | |||
| 1814 | frame->pf_period_new = FFMAX(period, CELT_POSTFILTER_MINPERIOD); | ||
| 1815 | frame->pf_gains_new[0] = gain * postfilter_taps[tapset][0]; | ||
| 1816 | frame->pf_gains_new[1] = gain * postfilter_taps[tapset][1]; | ||
| 1817 | frame->pf_gains_new[2] = gain * postfilter_taps[tapset][2]; | ||
| 1818 | } | ||
| 1819 | } | ||
| 1820 | |||
| 1821 | consumed = opus_rc_tell(rc); | ||
| 1822 | } | ||
| 1823 | |||
| 1824 | return consumed; | ||
| 1825 | } | ||
| 1826 | |||
| 1827 | static void process_anticollapse(CeltContext *s, CeltFrame *frame, float *X) | ||
| 1828 | { | ||
| 1829 | int i, j, k; | ||
| 1830 | |||
| 1831 | for (i = s->startband; i < s->endband; i++) { | ||
| 1832 | int renormalize = 0; | ||
| 1833 | float *xptr; | ||
| 1834 | float prev[2]; | ||
| 1835 | float Ediff, r; | ||
| 1836 | float thresh, sqrt_1; | ||
| 1837 | int depth; | ||
| 1838 | |||
| 1839 | /* depth in 1/8 bits */ | ||
| 1840 | depth = (1 + s->pulses[i]) / (celt_freq_range[i] << s->duration); | ||
| 1841 | thresh = pow(2, -1.0 - 0.125f * depth); | ||
| 1842 | sqrt_1 = 1.0f / sqrtf(celt_freq_range[i] << s->duration); | ||
| 1843 | |||
| 1844 | xptr = X + (celt_freq_bands[i] << s->duration); | ||
| 1845 | |||
| 1846 | prev[0] = frame->prev_energy[0][i]; | ||
| 1847 | prev[1] = frame->prev_energy[1][i]; | ||
| 1848 | if (s->coded_channels == 1) { | ||
| 1849 | CeltFrame *frame1 = &s->frame[1]; | ||
| 1850 | |||
| 1851 | prev[0] = FFMAX(prev[0], frame1->prev_energy[0][i]); | ||
| 1852 | prev[1] = FFMAX(prev[1], frame1->prev_energy[1][i]); | ||
| 1853 | } | ||
| 1854 | Ediff = frame->energy[i] - FFMIN(prev[0], prev[1]); | ||
| 1855 | Ediff = FFMAX(0, Ediff); | ||
| 1856 | |||
| 1857 | /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because | ||
| 1858 | short blocks don't have the same energy as long */ | ||
| 1859 | r = pow(2, 1 - Ediff); | ||
| 1860 | if (s->duration == 3) | ||
| 1861 | r *= M_SQRT2; | ||
| 1862 | r = FFMIN(thresh, r) * sqrt_1; | ||
| 1863 | for (k = 0; k < 1 << s->duration; k++) { | ||
| 1864 | /* Detect collapse */ | ||
| 1865 | if (!(frame->collapse_masks[i] & 1 << k)) { | ||
| 1866 | /* Fill with noise */ | ||
| 1867 | for (j = 0; j < celt_freq_range[i]; j++) | ||
| 1868 | xptr[(j << s->duration) + k] = (celt_rng(s) & 0x8000) ? r : -r; | ||
| 1869 | renormalize = 1; | ||
| 1870 | } | ||
| 1871 | } | ||
| 1872 | |||
| 1873 | /* We just added some energy, so we need to renormalize */ | ||
| 1874 | if (renormalize) | ||
| 1875 | celt_renormalize_vector(xptr, celt_freq_range[i] << s->duration, 1.0f); | ||
| 1876 | } | ||
| 1877 | } | ||
| 1878 | |||
| 1879 | static void celt_decode_bands(CeltContext *s, OpusRangeCoder *rc) | ||
| 1880 | { | ||
| 1881 | float lowband_scratch[8 * 22]; | ||
| 1882 | float norm[2 * 8 * 100]; | ||
| 1883 | |||
| 1884 | int totalbits = (s->framebits << 3) - s->anticollapse_bit; | ||
| 1885 | |||
| 1886 | int update_lowband = 1; | ||
| 1887 | int lowband_offset = 0; | ||
| 1888 | |||
| 1889 | int i, j; | ||
| 1890 | |||
| 1891 | memset(s->coeffs, 0, sizeof(s->coeffs)); | ||
| 1892 | |||
| 1893 | for (i = s->startband; i < s->endband; i++) { | ||
| 1894 | int band_offset = celt_freq_bands[i] << s->duration; | ||
| 1895 | int band_size = celt_freq_range[i] << s->duration; | ||
| 1896 | float *X = s->coeffs[0] + band_offset; | ||
| 1897 | float *Y = (s->coded_channels == 2) ? s->coeffs[1] + band_offset : NULL; | ||
| 1898 | |||
| 1899 | int consumed = opus_rc_tell_frac(rc); | ||
| 1900 | float *norm2 = norm + 8 * 100; | ||
| 1901 | int effective_lowband = -1; | ||
| 1902 | unsigned int cm[2]; | ||
| 1903 | int b; | ||
| 1904 | |||
| 1905 | /* Compute how many bits we want to allocate to this band */ | ||
| 1906 | if (i != s->startband) | ||
| 1907 | s->remaining -= consumed; | ||
| 1908 | s->remaining2 = totalbits - consumed - 1; | ||
| 1909 | if (i <= s->codedbands - 1) { | ||
| 1910 | int curr_balance = s->remaining / FFMIN(3, s->codedbands-i); | ||
| 1911 | b = av_clip(FFMIN(s->remaining2 + 1, s->pulses[i] + curr_balance), 0, 16383); | ||
| 1912 | } else | ||
| 1913 | b = 0; | ||
| 1914 | |||
| 1915 | if (celt_freq_bands[i] - celt_freq_range[i] >= celt_freq_bands[s->startband] && | ||
| 1916 | (update_lowband || lowband_offset == 0)) | ||
| 1917 | lowband_offset = i; | ||
| 1918 | |||
| 1919 | /* Get a conservative estimate of the collapse_mask's for the bands we're | ||
| 1920 | going to be folding from. */ | ||
| 1921 | if (lowband_offset != 0 && (s->spread != CELT_SPREAD_AGGRESSIVE || | ||
| 1922 | s->blocks > 1 || s->tf_change[i] < 0)) { | ||
| 1923 | int foldstart, foldend; | ||
| 1924 | |||
| 1925 | /* This ensures we never repeat spectral content within one band */ | ||
| 1926 | effective_lowband = FFMAX(celt_freq_bands[s->startband], | ||
| 1927 | celt_freq_bands[lowband_offset] - celt_freq_range[i]); | ||
| 1928 | foldstart = lowband_offset; | ||
| 1929 | while (celt_freq_bands[--foldstart] > effective_lowband); | ||
| 1930 | foldend = lowband_offset - 1; | ||
| 1931 | while (celt_freq_bands[++foldend] < effective_lowband + celt_freq_range[i]); | ||
| 1932 | |||
| 1933 | cm[0] = cm[1] = 0; | ||
| 1934 | for (j = foldstart; j < foldend; j++) { | ||
| 1935 | cm[0] |= s->frame[0].collapse_masks[j]; | ||
| 1936 | cm[1] |= s->frame[s->coded_channels - 1].collapse_masks[j]; | ||
| 1937 | } | ||
| 1938 | } else | ||
| 1939 | /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost | ||
| 1940 | always) be non-zero.*/ | ||
| 1941 | cm[0] = cm[1] = (1 << s->blocks) - 1; | ||
| 1942 | |||
| 1943 | if (s->dualstereo && i == s->intensitystereo) { | ||
| 1944 | /* Switch off dual stereo to do intensity */ | ||
| 1945 | s->dualstereo = 0; | ||
| 1946 | for (j = celt_freq_bands[s->startband] << s->duration; j < band_offset; j++) | ||
| 1947 | norm[j] = (norm[j] + norm2[j]) / 2; | ||
| 1948 | } | ||
| 1949 | |||
| 1950 | if (s->dualstereo) { | ||
| 1951 | cm[0] = celt_decode_band(s, rc, i, X, NULL, band_size, b / 2, s->blocks, | ||
| 1952 | effective_lowband != -1 ? norm + (effective_lowband << s->duration) : NULL, s->duration, | ||
| 1953 | norm + band_offset, 0, 1.0f, lowband_scratch, cm[0]); | ||
| 1954 | |||
| 1955 | cm[1] = celt_decode_band(s, rc, i, Y, NULL, band_size, b/2, s->blocks, | ||
| 1956 | effective_lowband != -1 ? norm2 + (effective_lowband << s->duration) : NULL, s->duration, | ||
| 1957 | norm2 + band_offset, 0, 1.0f, lowband_scratch, cm[1]); | ||
| 1958 | } else { | ||
| 1959 | cm[0] = celt_decode_band(s, rc, i, X, Y, band_size, b, s->blocks, | ||
| 1960 | effective_lowband != -1 ? norm + (effective_lowband << s->duration) : NULL, s->duration, | ||
| 1961 | norm + band_offset, 0, 1.0f, lowband_scratch, cm[0]|cm[1]); | ||
| 1962 | |||
| 1963 | cm[1] = cm[0]; | ||
| 1964 | } | ||
| 1965 | |||
| 1966 | s->frame[0].collapse_masks[i] = (uint8_t)cm[0]; | ||
| 1967 | s->frame[s->coded_channels - 1].collapse_masks[i] = (uint8_t)cm[1]; | ||
| 1968 | s->remaining += s->pulses[i] + consumed; | ||
| 1969 | |||
| 1970 | /* Update the folding position only as long as we have 1 bit/sample depth */ | ||
| 1971 | update_lowband = (b > band_size << 3); | ||
| 1972 | } | ||
| 1973 | } | ||
| 1974 | |||
| 1975 | int ff_celt_decode_frame(CeltContext *s, OpusRangeCoder *rc, | ||
| 1976 | float **output, int coded_channels, int frame_size, | ||
| 1977 | int startband, int endband) | ||
| 1978 | { | ||
| 1979 | int i, j; | ||
| 1980 | |||
| 1981 | int consumed; // bits of entropy consumed thus far for this frame | ||
| 1982 | int silence = 0; | ||
| 1983 | int transient = 0; | ||
| 1984 | int anticollapse = 0; | ||
| 1985 | CeltIMDCTContext *imdct; | ||
| 1986 | float imdct_scale = 1.0; | ||
| 1987 | |||
| 1988 | if (coded_channels != 1 && coded_channels != 2) { | ||
| 1989 | av_log(s->avctx, AV_LOG_ERROR, "Invalid number of coded channels: %d\n", | ||
| 1990 | coded_channels); | ||
| 1991 | return AVERROR_INVALIDDATA; | ||
| 1992 | } | ||
| 1993 | if (startband < 0 || startband > endband || endband > CELT_MAX_BANDS) { | ||
| 1994 | av_log(s->avctx, AV_LOG_ERROR, "Invalid start/end band: %d %d\n", | ||
| 1995 | startband, endband); | ||
| 1996 | return AVERROR_INVALIDDATA; | ||
| 1997 | } | ||
| 1998 | |||
| 1999 | s->flushed = 0; | ||
| 2000 | s->coded_channels = coded_channels; | ||
| 2001 | s->startband = startband; | ||
| 2002 | s->endband = endband; | ||
| 2003 | s->framebits = rc->rb.bytes * 8; | ||
| 2004 | |||
| 2005 | s->duration = av_log2(frame_size / CELT_SHORT_BLOCKSIZE); | ||
| 2006 | if (s->duration > CELT_MAX_LOG_BLOCKS || | ||
| 2007 | frame_size != CELT_SHORT_BLOCKSIZE * (1 << s->duration)) { | ||
| 2008 | av_log(s->avctx, AV_LOG_ERROR, "Invalid CELT frame size: %d\n", | ||
| 2009 | frame_size); | ||
| 2010 | return AVERROR_INVALIDDATA; | ||
| 2011 | } | ||
| 2012 | |||
| 2013 | if (!s->output_channels) | ||
| 2014 | s->output_channels = coded_channels; | ||
| 2015 | |||
| 2016 | memset(s->frame[0].collapse_masks, 0, sizeof(s->frame[0].collapse_masks)); | ||
| 2017 | memset(s->frame[1].collapse_masks, 0, sizeof(s->frame[1].collapse_masks)); | ||
| 2018 | |||
| 2019 | consumed = opus_rc_tell(rc); | ||
| 2020 | |||
| 2021 | /* obtain silence flag */ | ||
| 2022 | if (consumed >= s->framebits) | ||
| 2023 | silence = 1; | ||
| 2024 | else if (consumed == 1) | ||
| 2025 | silence = opus_rc_p2model(rc, 15); | ||
| 2026 | |||
| 2027 | |||
| 2028 | if (silence) { | ||
| 2029 | consumed = s->framebits; | ||
| 2030 | rc->total_read_bits += s->framebits - opus_rc_tell(rc); | ||
| 2031 | } | ||
| 2032 | |||
| 2033 | /* obtain post-filter options */ | ||
| 2034 | consumed = parse_postfilter(s, rc, consumed); | ||
| 2035 | |||
| 2036 | /* obtain transient flag */ | ||
| 2037 | if (s->duration != 0 && consumed+3 <= s->framebits) | ||
| 2038 | transient = opus_rc_p2model(rc, 3); | ||
| 2039 | |||
| 2040 | s->blocks = transient ? 1 << s->duration : 1; | ||
| 2041 | s->blocksize = frame_size / s->blocks; | ||
| 2042 | |||
| 2043 | imdct = s->imdct[transient ? 0 : s->duration]; | ||
| 2044 | |||
| 2045 | if (coded_channels == 1) { | ||
| 2046 | for (i = 0; i < CELT_MAX_BANDS; i++) | ||
| 2047 | s->frame[0].energy[i] = FFMAX(s->frame[0].energy[i], s->frame[1].energy[i]); | ||
| 2048 | } | ||
| 2049 | |||
| 2050 | celt_decode_coarse_energy(s, rc); | ||
| 2051 | celt_decode_tf_changes (s, rc, transient); | ||
| 2052 | celt_decode_allocation (s, rc); | ||
| 2053 | celt_decode_fine_energy (s, rc); | ||
| 2054 | celt_decode_bands (s, rc); | ||
| 2055 | |||
| 2056 | if (s->anticollapse_bit) | ||
| 2057 | anticollapse = opus_getrawbits(rc, 1); | ||
| 2058 | |||
| 2059 | celt_decode_final_energy(s, rc, s->framebits - opus_rc_tell(rc)); | ||
| 2060 | |||
| 2061 | /* apply anti-collapse processing and denormalization to | ||
| 2062 | * each coded channel */ | ||
| 2063 | for (i = 0; i < s->coded_channels; i++) { | ||
| 2064 | CeltFrame *frame = &s->frame[i]; | ||
| 2065 | |||
| 2066 | if (anticollapse) | ||
| 2067 | process_anticollapse(s, frame, s->coeffs[i]); | ||
| 2068 | |||
| 2069 | celt_denormalize(s, frame, s->coeffs[i]); | ||
| 2070 | } | ||
| 2071 | |||
| 2072 | /* stereo -> mono downmix */ | ||
| 2073 | if (s->output_channels < s->coded_channels) { | ||
| 2074 | s->dsp.vector_fmac_scalar(s->coeffs[0], s->coeffs[1], 1.0, FFALIGN(frame_size, 16)); | ||
| 2075 | imdct_scale = 0.5; | ||
| 2076 | } else if (s->output_channels > s->coded_channels) | ||
| 2077 | memcpy(s->coeffs[1], s->coeffs[0], frame_size * sizeof(float)); | ||
| 2078 | |||
| 2079 | if (silence) { | ||
| 2080 | for (i = 0; i < 2; i++) { | ||
| 2081 | CeltFrame *frame = &s->frame[i]; | ||
| 2082 | |||
| 2083 | for (j = 0; j < FF_ARRAY_ELEMS(frame->energy); j++) | ||
| 2084 | frame->energy[j] = CELT_ENERGY_SILENCE; | ||
| 2085 | } | ||
| 2086 | memset(s->coeffs, 0, sizeof(s->coeffs)); | ||
| 2087 | } | ||
| 2088 | |||
| 2089 | /* transform and output for each output channel */ | ||
| 2090 | for (i = 0; i < s->output_channels; i++) { | ||
| 2091 | CeltFrame *frame = &s->frame[i]; | ||
| 2092 | float m = frame->deemph_coeff; | ||
| 2093 | |||
| 2094 | /* iMDCT and overlap-add */ | ||
| 2095 | for (j = 0; j < s->blocks; j++) { | ||
| 2096 | float *dst = frame->buf + 1024 + j * s->blocksize; | ||
| 2097 | |||
| 2098 | ff_celt_imdct_half(imdct, dst + CELT_OVERLAP / 2, s->coeffs[i] + j, | ||
| 2099 | s->blocks, imdct_scale); | ||
| 2100 | s->dsp.vector_fmul_window(dst, dst, dst + CELT_OVERLAP / 2, | ||
| 2101 | celt_window, CELT_OVERLAP / 2); | ||
| 2102 | } | ||
| 2103 | |||
| 2104 | /* postfilter */ | ||
| 2105 | celt_postfilter(s, frame); | ||
| 2106 | |||
| 2107 | /* deemphasis and output scaling */ | ||
| 2108 | for (j = 0; j < frame_size; j++) { | ||
| 2109 | float tmp = frame->buf[1024 - frame_size + j] + m; | ||
| 2110 | m = tmp * CELT_DEEMPH_COEFF; | ||
| 2111 | output[i][j] = tmp / 32768.; | ||
| 2112 | } | ||
| 2113 | frame->deemph_coeff = m; | ||
| 2114 | } | ||
| 2115 | |||
| 2116 | if (coded_channels == 1) | ||
| 2117 | memcpy(s->frame[1].energy, s->frame[0].energy, sizeof(s->frame[0].energy)); | ||
| 2118 | |||
| 2119 | for (i = 0; i < 2; i++ ) { | ||
| 2120 | CeltFrame *frame = &s->frame[i]; | ||
| 2121 | |||
| 2122 | if (!transient) { | ||
| 2123 | memcpy(frame->prev_energy[1], frame->prev_energy[0], sizeof(frame->prev_energy[0])); | ||
| 2124 | memcpy(frame->prev_energy[0], frame->energy, sizeof(frame->prev_energy[0])); | ||
| 2125 | } else { | ||
| 2126 | for (j = 0; j < CELT_MAX_BANDS; j++) | ||
| 2127 | frame->prev_energy[0][j] = FFMIN(frame->prev_energy[0][j], frame->energy[j]); | ||
| 2128 | } | ||
| 2129 | |||
| 2130 | for (j = 0; j < s->startband; j++) { | ||
| 2131 | frame->prev_energy[0][j] = CELT_ENERGY_SILENCE; | ||
| 2132 | frame->energy[j] = 0.0; | ||
| 2133 | } | ||
| 2134 | for (j = s->endband; j < CELT_MAX_BANDS; j++) { | ||
| 2135 | frame->prev_energy[0][j] = CELT_ENERGY_SILENCE; | ||
| 2136 | frame->energy[j] = 0.0; | ||
| 2137 | } | ||
| 2138 | } | ||
| 2139 | |||
| 2140 | s->seed = rc->range; | ||
| 2141 | |||
| 2142 | return 0; | ||
| 2143 | } | ||
| 2144 | |||
| 2145 | void ff_celt_flush(CeltContext *s) | ||
| 2146 | { | ||
| 2147 | int i, j; | ||
| 2148 | |||
| 2149 | if (s->flushed) | ||
| 2150 | return; | ||
| 2151 | |||
| 2152 | for (i = 0; i < 2; i++) { | ||
| 2153 | CeltFrame *frame = &s->frame[i]; | ||
| 2154 | |||
| 2155 | for (j = 0; j < CELT_MAX_BANDS; j++) | ||
| 2156 | frame->prev_energy[0][j] = frame->prev_energy[1][j] = CELT_ENERGY_SILENCE; | ||
| 2157 | |||
| 2158 | memset(frame->energy, 0, sizeof(frame->energy)); | ||
| 2159 | memset(frame->buf, 0, sizeof(frame->buf)); | ||
| 2160 | |||
| 2161 | memset(frame->pf_gains, 0, sizeof(frame->pf_gains)); | ||
| 2162 | memset(frame->pf_gains_old, 0, sizeof(frame->pf_gains_old)); | ||
| 2163 | memset(frame->pf_gains_new, 0, sizeof(frame->pf_gains_new)); | ||
| 2164 | |||
| 2165 | frame->deemph_coeff = 0.0; | ||
| 2166 | } | ||
| 2167 | s->seed = 0; | ||
| 2168 | |||
| 2169 | s->flushed = 1; | ||
| 2170 | } | ||
| 2171 | |||
| 2172 | void ff_celt_free(CeltContext **ps) | ||
| 2173 | { | ||
| 2174 | CeltContext *s = *ps; | ||
| 2175 | int i; | ||
| 2176 | |||
| 2177 | if (!s) | ||
| 2178 | return; | ||
| 2179 | |||
| 2180 | for (i = 0; i < FF_ARRAY_ELEMS(s->imdct); i++) | ||
| 2181 | ff_celt_imdct_uninit(&s->imdct[i]); | ||
| 2182 | |||
| 2183 | av_freep(ps); | ||
| 2184 | } | ||
| 2185 | |||
| 2186 | int ff_celt_init(AVCodecContext *avctx, CeltContext **ps, int output_channels) | ||
| 2187 | { | ||
| 2188 | CeltContext *s; | ||
| 2189 | int i, ret; | ||
| 2190 | |||
| 2191 | if (output_channels != 1 && output_channels != 2) { | ||
| 2192 | av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n", | ||
| 2193 | output_channels); | ||
| 2194 | return AVERROR(EINVAL); | ||
| 2195 | } | ||
| 2196 | |||
| 2197 | s = av_mallocz(sizeof(*s)); | ||
| 2198 | if (!s) | ||
| 2199 | return AVERROR(ENOMEM); | ||
| 2200 | |||
| 2201 | s->avctx = avctx; | ||
| 2202 | s->output_channels = output_channels; | ||
| 2203 | |||
| 2204 | for (i = 0; i < FF_ARRAY_ELEMS(s->imdct); i++) { | ||
| 2205 | ret = ff_celt_imdct_init(&s->imdct[i], i + 3); | ||
| 2206 | if (ret < 0) | ||
| 2207 | goto fail; | ||
| 2208 | } | ||
| 2209 | |||
| 2210 | avpriv_float_dsp_init(&s->dsp, avctx->flags & CODEC_FLAG_BITEXACT); | ||
| 2211 | |||
| 2212 | ff_celt_flush(s); | ||
| 2213 | |||
| 2214 | *ps = s; | ||
| 2215 | |||
| 2216 | return 0; | ||
| 2217 | fail: | ||
| 2218 | ff_celt_free(&s); | ||
| 2219 | return ret; | ||
| 2220 | } | ||
| @@ -0,0 +1,268 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2013-2014 Mozilla Corporation | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | /** | ||
| 22 | * @file | ||
| 23 | * Celt non-power of 2 iMDCT | ||
| 24 | */ | ||
| 25 | |||
| 26 | #include <float.h> | ||
| 27 | #include <math.h> | ||
| 28 | |||
| 29 | #include "libavutil/attributes.h" | ||
| 30 | #include "libavutil/common.h" | ||
| 31 | |||
| 32 | #include "fft.h" | ||
| 33 | #include "opus.h" | ||
| 34 | |||
| 35 | // complex c = a * b | ||
| 36 | #define CMUL3(cre, cim, are, aim, bre, bim) \ | ||
| 37 | do { \ | ||
| 38 | cre = are * bre - aim * bim; \ | ||
| 39 | cim = are * bim + aim * bre; \ | ||
| 40 | } while (0) | ||
| 41 | |||
| 42 | #define CMUL(c, a, b) CMUL3((c).re, (c).im, (a).re, (a).im, (b).re, (b).im) | ||
| 43 | |||
| 44 | // complex c = a * b | ||
| 45 | // d = a * conjugate(b) | ||
| 46 | #define CMUL2(c, d, a, b) \ | ||
| 47 | do { \ | ||
| 48 | float are = (a).re; \ | ||
| 49 | float aim = (a).im; \ | ||
| 50 | float bre = (b).re; \ | ||
| 51 | float bim = (b).im; \ | ||
| 52 | float rr = are * bre; \ | ||
| 53 | float ri = are * bim; \ | ||
| 54 | float ir = aim * bre; \ | ||
| 55 | float ii = aim * bim; \ | ||
| 56 | (c).re = rr - ii; \ | ||
| 57 | (c).im = ri + ir; \ | ||
| 58 | (d).re = rr + ii; \ | ||
| 59 | (d).im = -ri + ir; \ | ||
| 60 | } while (0) | ||
| 61 | |||
| 62 | struct CeltIMDCTContext { | ||
| 63 | int fft_n; | ||
| 64 | int len2; | ||
| 65 | int len4; | ||
| 66 | |||
| 67 | FFTComplex *tmp; | ||
| 68 | |||
| 69 | FFTComplex *twiddle_exptab; | ||
| 70 | |||
| 71 | FFTComplex *exptab[6]; | ||
| 72 | }; | ||
| 73 | |||
| 74 | av_cold void ff_celt_imdct_uninit(CeltIMDCTContext **ps) | ||
| 75 | { | ||
| 76 | CeltIMDCTContext *s = *ps; | ||
| 77 | int i; | ||
| 78 | |||
| 79 | if (!s) | ||
| 80 | return; | ||
| 81 | |||
| 82 | for (i = 0; i < FF_ARRAY_ELEMS(s->exptab); i++) | ||
| 83 | av_freep(&s->exptab[i]); | ||
| 84 | |||
| 85 | av_freep(&s->twiddle_exptab); | ||
| 86 | |||
| 87 | av_freep(&s->tmp); | ||
| 88 | |||
| 89 | av_freep(ps); | ||
| 90 | } | ||
| 91 | |||
| 92 | av_cold int ff_celt_imdct_init(CeltIMDCTContext **ps, int N) | ||
| 93 | { | ||
| 94 | CeltIMDCTContext *s; | ||
| 95 | int len2 = 15 * (1 << N); | ||
| 96 | int len = 2 * len2; | ||
| 97 | int i, j; | ||
| 98 | |||
| 99 | if (len2 > CELT_MAX_FRAME_SIZE) | ||
| 100 | return AVERROR(EINVAL); | ||
| 101 | |||
| 102 | s = av_mallocz(sizeof(*s)); | ||
| 103 | if (!s) | ||
| 104 | return AVERROR(ENOMEM); | ||
| 105 | |||
| 106 | s->fft_n = N - 1; | ||
| 107 | s->len4 = len2 / 2; | ||
| 108 | s->len2 = len2; | ||
| 109 | |||
| 110 | s->tmp = av_malloc(len * 2 * sizeof(*s->tmp)); | ||
| 111 | if (!s->tmp) | ||
| 112 | goto fail; | ||
| 113 | |||
| 114 | s->twiddle_exptab = av_malloc(s->len4 * sizeof(*s->twiddle_exptab)); | ||
| 115 | if (!s->twiddle_exptab) | ||
| 116 | goto fail; | ||
| 117 | |||
| 118 | for (i = 0; i < s->len4; i++) { | ||
| 119 | s->twiddle_exptab[i].re = cos(2 * M_PI * (i + 0.125 + s->len4) / len); | ||
| 120 | s->twiddle_exptab[i].im = sin(2 * M_PI * (i + 0.125 + s->len4) / len); | ||
| 121 | } | ||
| 122 | |||
| 123 | for (i = 0; i < FF_ARRAY_ELEMS(s->exptab); i++) { | ||
| 124 | int N = 15 * (1 << i); | ||
| 125 | s->exptab[i] = av_malloc(sizeof(*s->exptab[i]) * FFMAX(N, 19)); | ||
| 126 | if (!s->exptab[i]) | ||
| 127 | goto fail; | ||
| 128 | |||
| 129 | for (j = 0; j < N; j++) { | ||
| 130 | s->exptab[i][j].re = cos(2 * M_PI * j / N); | ||
| 131 | s->exptab[i][j].im = sin(2 * M_PI * j / N); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | // wrap around to simplify fft15 | ||
| 136 | for (j = 15; j < 19; j++) | ||
| 137 | s->exptab[0][j] = s->exptab[0][j - 15]; | ||
| 138 | |||
| 139 | *ps = s; | ||
| 140 | |||
| 141 | return 0; | ||
| 142 | fail: | ||
| 143 | ff_celt_imdct_uninit(&s); | ||
| 144 | return AVERROR(ENOMEM); | ||
| 145 | } | ||
| 146 | |||
| 147 | static void fft5(FFTComplex *out, const FFTComplex *in, int stride) | ||
| 148 | { | ||
| 149 | // [0] = exp(2 * i * pi / 5), [1] = exp(2 * i * pi * 2 / 5) | ||
| 150 | static const FFTComplex fact[] = { { 0.30901699437494745, 0.95105651629515353 }, | ||
| 151 | { -0.80901699437494734, 0.58778525229247325 } }; | ||
| 152 | |||
| 153 | FFTComplex z[4][4]; | ||
| 154 | |||
| 155 | CMUL2(z[0][0], z[0][3], in[1 * stride], fact[0]); | ||
| 156 | CMUL2(z[0][1], z[0][2], in[1 * stride], fact[1]); | ||
| 157 | CMUL2(z[1][0], z[1][3], in[2 * stride], fact[0]); | ||
| 158 | CMUL2(z[1][1], z[1][2], in[2 * stride], fact[1]); | ||
| 159 | CMUL2(z[2][0], z[2][3], in[3 * stride], fact[0]); | ||
| 160 | CMUL2(z[2][1], z[2][2], in[3 * stride], fact[1]); | ||
| 161 | CMUL2(z[3][0], z[3][3], in[4 * stride], fact[0]); | ||
| 162 | CMUL2(z[3][1], z[3][2], in[4 * stride], fact[1]); | ||
| 163 | |||
| 164 | out[0].re = in[0].re + in[stride].re + in[2 * stride].re + in[3 * stride].re + in[4 * stride].re; | ||
| 165 | out[0].im = in[0].im + in[stride].im + in[2 * stride].im + in[3 * stride].im + in[4 * stride].im; | ||
| 166 | |||
| 167 | out[1].re = in[0].re + z[0][0].re + z[1][1].re + z[2][2].re + z[3][3].re; | ||
| 168 | out[1].im = in[0].im + z[0][0].im + z[1][1].im + z[2][2].im + z[3][3].im; | ||
| 169 | |||
| 170 | out[2].re = in[0].re + z[0][1].re + z[1][3].re + z[2][0].re + z[3][2].re; | ||
| 171 | out[2].im = in[0].im + z[0][1].im + z[1][3].im + z[2][0].im + z[3][2].im; | ||
| 172 | |||
| 173 | out[3].re = in[0].re + z[0][2].re + z[1][0].re + z[2][3].re + z[3][1].re; | ||
| 174 | out[3].im = in[0].im + z[0][2].im + z[1][0].im + z[2][3].im + z[3][1].im; | ||
| 175 | |||
| 176 | out[4].re = in[0].re + z[0][3].re + z[1][2].re + z[2][1].re + z[3][0].re; | ||
| 177 | out[4].im = in[0].im + z[0][3].im + z[1][2].im + z[2][1].im + z[3][0].im; | ||
| 178 | } | ||
| 179 | |||
| 180 | static void fft15(CeltIMDCTContext *s, FFTComplex *out, const FFTComplex *in, int stride) | ||
| 181 | { | ||
| 182 | const FFTComplex *exptab = s->exptab[0]; | ||
| 183 | FFTComplex tmp[5]; | ||
| 184 | FFTComplex tmp1[5]; | ||
| 185 | FFTComplex tmp2[5]; | ||
| 186 | int k; | ||
| 187 | |||
| 188 | fft5(tmp, in, stride * 3); | ||
| 189 | fft5(tmp1, in + stride, stride * 3); | ||
| 190 | fft5(tmp2, in + 2 * stride, stride * 3); | ||
| 191 | |||
| 192 | for (k = 0; k < 5; k++) { | ||
| 193 | FFTComplex t1, t2; | ||
| 194 | |||
| 195 | CMUL(t1, tmp1[k], exptab[k]); | ||
| 196 | CMUL(t2, tmp2[k], exptab[2 * k]); | ||
| 197 | out[k].re = tmp[k].re + t1.re + t2.re; | ||
| 198 | out[k].im = tmp[k].im + t1.im + t2.im; | ||
| 199 | |||
| 200 | CMUL(t1, tmp1[k], exptab[k + 5]); | ||
| 201 | CMUL(t2, tmp2[k], exptab[2 * (k + 5)]); | ||
| 202 | out[k + 5].re = tmp[k].re + t1.re + t2.re; | ||
| 203 | out[k + 5].im = tmp[k].im + t1.im + t2.im; | ||
| 204 | |||
| 205 | CMUL(t1, tmp1[k], exptab[k + 10]); | ||
| 206 | CMUL(t2, tmp2[k], exptab[2 * k + 5]); | ||
| 207 | out[k + 10].re = tmp[k].re + t1.re + t2.re; | ||
| 208 | out[k + 10].im = tmp[k].im + t1.im + t2.im; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | /* | ||
| 213 | * FFT of the length 15 * (2^N) | ||
| 214 | */ | ||
| 215 | static void fft_calc(CeltIMDCTContext *s, FFTComplex *out, const FFTComplex *in, int N, int stride) | ||
| 216 | { | ||
| 217 | if (N) { | ||
| 218 | const FFTComplex *exptab = s->exptab[N]; | ||
| 219 | const int len2 = 15 * (1 << (N - 1)); | ||
| 220 | int k; | ||
| 221 | |||
| 222 | fft_calc(s, out, in, N - 1, stride * 2); | ||
| 223 | fft_calc(s, out + len2, in + stride, N - 1, stride * 2); | ||
| 224 | |||
| 225 | for (k = 0; k < len2; k++) { | ||
| 226 | FFTComplex t; | ||
| 227 | |||
| 228 | CMUL(t, out[len2 + k], exptab[k]); | ||
| 229 | |||
| 230 | out[len2 + k].re = out[k].re - t.re; | ||
| 231 | out[len2 + k].im = out[k].im - t.im; | ||
| 232 | |||
| 233 | out[k].re += t.re; | ||
| 234 | out[k].im += t.im; | ||
| 235 | } | ||
| 236 | } else | ||
| 237 | fft15(s, out, in, stride); | ||
| 238 | } | ||
| 239 | |||
| 240 | void ff_celt_imdct_half(CeltIMDCTContext *s, float *dst, const float *src, | ||
| 241 | int stride, float scale) | ||
| 242 | { | ||
| 243 | FFTComplex *z = (FFTComplex *)dst; | ||
| 244 | const int len8 = s->len4 / 2; | ||
| 245 | const float *in1 = src; | ||
| 246 | const float *in2 = src + (s->len2 - 1) * stride; | ||
| 247 | int i; | ||
| 248 | |||
| 249 | for (i = 0; i < s->len4; i++) { | ||
| 250 | FFTComplex tmp = { *in2, *in1 }; | ||
| 251 | CMUL(s->tmp[i], tmp, s->twiddle_exptab[i]); | ||
| 252 | in1 += 2 * stride; | ||
| 253 | in2 -= 2 * stride; | ||
| 254 | } | ||
| 255 | |||
| 256 | fft_calc(s, z, s->tmp, s->fft_n, 1); | ||
| 257 | |||
| 258 | for (i = 0; i < len8; i++) { | ||
| 259 | float r0, i0, r1, i1; | ||
| 260 | |||
| 261 | CMUL3(r0, i1, z[len8 - i - 1].im, z[len8 - i - 1].re, s->twiddle_exptab[len8 - i - 1].im, s->twiddle_exptab[len8 - i - 1].re); | ||
| 262 | CMUL3(r1, i0, z[len8 + i].im, z[len8 + i].re, s->twiddle_exptab[len8 + i].im, s->twiddle_exptab[len8 + i].re); | ||
| 263 | z[len8 - i - 1].re = scale * r0; | ||
| 264 | z[len8 - i - 1].im = scale * i0; | ||
| 265 | z[len8 + i].re = scale * r1; | ||
| 266 | z[len8 + i].im = scale * i1; | ||
| 267 | } | ||
| 268 | } | ||
| @@ -0,0 +1,75 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2013-2014 Mozilla Corporation | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | /** | ||
| 22 | * @file | ||
| 23 | * Opus parser | ||
| 24 | * | ||
| 25 | * Determines the duration for each packet. | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include "avcodec.h" | ||
| 29 | #include "opus.h" | ||
| 30 | |||
| 31 | typedef struct OpusParseContext { | ||
| 32 | OpusContext ctx; | ||
| 33 | OpusPacket pkt; | ||
| 34 | int extradata_parsed; | ||
| 35 | } OpusParseContext; | ||
| 36 | |||
| 37 | static int opus_parse(AVCodecParserContext *ctx, AVCodecContext *avctx, | ||
| 38 | const uint8_t **poutbuf, int *poutbuf_size, | ||
| 39 | const uint8_t *buf, int buf_size) | ||
| 40 | { | ||
| 41 | OpusParseContext *s = ctx->priv_data; | ||
| 42 | int ret; | ||
| 43 | |||
| 44 | if (!buf_size) | ||
| 45 | return 0; | ||
| 46 | |||
| 47 | if (avctx->extradata && !s->extradata_parsed) { | ||
| 48 | ret = ff_opus_parse_extradata(avctx, &s->ctx); | ||
| 49 | if (ret < 0) { | ||
| 50 | av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg extradata.\n"); | ||
| 51 | goto fail; | ||
| 52 | } | ||
| 53 | av_freep(&s->ctx.channel_maps); | ||
| 54 | s->extradata_parsed = 1; | ||
| 55 | } | ||
| 56 | |||
| 57 | ret = ff_opus_parse_packet(&s->pkt, buf, buf_size, s->ctx.nb_streams > 1); | ||
| 58 | if (ret < 0) { | ||
| 59 | av_log(avctx, AV_LOG_ERROR, "Error parsing Opus packet header.\n"); | ||
| 60 | goto fail; | ||
| 61 | } | ||
| 62 | |||
| 63 | ctx->duration = s->pkt.frame_count * s->pkt.frame_duration; | ||
| 64 | |||
| 65 | fail: | ||
| 66 | *poutbuf = buf; | ||
| 67 | *poutbuf_size = buf_size; | ||
| 68 | return buf_size; | ||
| 69 | } | ||
| 70 | |||
| 71 | AVCodecParser ff_opus_parser = { | ||
| 72 | .codec_ids = { AV_CODEC_ID_OPUS }, | ||
| 73 | .priv_data_size = sizeof(OpusParseContext), | ||
| 74 | .parser_parse = opus_parse, | ||
| 75 | }; | ||
| @@ -0,0 +1,1597 @@ | |||
| 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 SILK decoder | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include <stdint.h> | ||
| 28 | |||
| 29 | #include "opus.h" | ||
| 30 | |||
| 31 | typedef struct SilkFrame { | ||
| 32 | int coded; | ||
| 33 | int log_gain; | ||
| 34 | int16_t nlsf[16]; | ||
| 35 | float lpc[16]; | ||
| 36 | |||
| 37 | float output [2 * SILK_HISTORY]; | ||
| 38 | float lpc_history[2 * SILK_HISTORY]; | ||
| 39 | int primarylag; | ||
| 40 | |||
| 41 | int prev_voiced; | ||
| 42 | } SilkFrame; | ||
| 43 | |||
| 44 | struct SilkContext { | ||
| 45 | AVCodecContext *avctx; | ||
| 46 | int output_channels; | ||
| 47 | |||
| 48 | int midonly; | ||
| 49 | int subframes; | ||
| 50 | int sflength; | ||
| 51 | int flength; | ||
| 52 | int nlsf_interp_factor; | ||
| 53 | |||
| 54 | enum OpusBandwidth bandwidth; | ||
| 55 | int wb; | ||
| 56 | |||
| 57 | SilkFrame frame[2]; | ||
| 58 | float prev_stereo_weights[2]; | ||
| 59 | float stereo_weights[2]; | ||
| 60 | |||
| 61 | int prev_coded_channels; | ||
| 62 | }; | ||
| 63 | |||
| 64 | static const uint16_t silk_model_stereo_s1[] = { | ||
| 65 | 256, 7, 9, 10, 11, 12, 22, 46, 54, 55, 56, 59, 82, 174, 197, 200, | ||
| 66 | 201, 202, 210, 234, 244, 245, 246, 247, 249, 256 | ||
| 67 | }; | ||
| 68 | |||
| 69 | static const uint16_t silk_model_stereo_s2[] = {256, 85, 171, 256}; | ||
| 70 | |||
| 71 | static const uint16_t silk_model_stereo_s3[] = {256, 51, 102, 154, 205, 256}; | ||
| 72 | |||
| 73 | static const uint16_t silk_model_mid_only[] = {256, 192, 256}; | ||
| 74 | |||
| 75 | static const uint16_t silk_model_frame_type_inactive[] = {256, 26, 256}; | ||
| 76 | |||
| 77 | static const uint16_t silk_model_frame_type_active[] = {256, 24, 98, 246, 256}; | ||
| 78 | |||
| 79 | static const uint16_t silk_model_gain_highbits[3][9] = { | ||
| 80 | {256, 32, 144, 212, 241, 253, 254, 255, 256}, | ||
| 81 | {256, 2, 19, 64, 124, 186, 233, 252, 256}, | ||
| 82 | {256, 1, 4, 30, 101, 195, 245, 254, 256} | ||
| 83 | }; | ||
| 84 | |||
| 85 | static const uint16_t silk_model_gain_lowbits[] = {256, 32, 64, 96, 128, 160, 192, 224, 256}; | ||
| 86 | |||
| 87 | static const uint16_t silk_model_gain_delta[] = { | ||
| 88 | 256, 6, 11, 22, 53, 185, 206, 214, 218, 221, 223, 225, 227, 228, 229, 230, | ||
| 89 | 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, | ||
| 90 | 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 | ||
| 91 | }; | ||
| 92 | static const uint16_t silk_model_lsf_s1[2][2][33] = { | ||
| 93 | { | ||
| 94 | { // NB or MB, unvoiced | ||
| 95 | 256, 44, 78, 108, 127, 148, 160, 171, 174, 177, 179, 195, 197, 199, 200, 205, | ||
| 96 | 207, 208, 211, 214, 215, 216, 218, 220, 222, 225, 226, 235, 244, 246, 253, 255, 256 | ||
| 97 | }, { // NB or MB, voiced | ||
| 98 | 256, 1, 11, 12, 20, 23, 31, 39, 53, 66, 80, 81, 95, 107, 120, 131, | ||
| 99 | 142, 154, 165, 175, 185, 196, 204, 213, 221, 228, 236, 237, 238, 244, 245, 251, 256 | ||
| 100 | } | ||
| 101 | }, { | ||
| 102 | { // WB, unvoiced | ||
| 103 | 256, 31, 52, 55, 72, 73, 81, 98, 102, 103, 121, 137, 141, 143, 146, 147, | ||
| 104 | 157, 158, 161, 177, 188, 204, 206, 208, 211, 213, 224, 225, 229, 238, 246, 253, 256 | ||
| 105 | }, { // WB, voiced | ||
| 106 | 256, 1, 5, 21, 26, 44, 55, 60, 74, 89, 90, 93, 105, 118, 132, 146, | ||
| 107 | 152, 166, 178, 180, 186, 187, 199, 211, 222, 232, 235, 245, 250, 251, 252, 253, 256 | ||
| 108 | } | ||
| 109 | } | ||
| 110 | }; | ||
| 111 | |||
| 112 | static const uint16_t silk_model_lsf_s2[32][10] = { | ||
| 113 | // NB, MB | ||
| 114 | { 256, 1, 2, 3, 18, 242, 253, 254, 255, 256 }, | ||
| 115 | { 256, 1, 2, 4, 38, 221, 253, 254, 255, 256 }, | ||
| 116 | { 256, 1, 2, 6, 48, 197, 252, 254, 255, 256 }, | ||
| 117 | { 256, 1, 2, 10, 62, 185, 246, 254, 255, 256 }, | ||
| 118 | { 256, 1, 4, 20, 73, 174, 248, 254, 255, 256 }, | ||
| 119 | { 256, 1, 4, 21, 76, 166, 239, 254, 255, 256 }, | ||
| 120 | { 256, 1, 8, 32, 85, 159, 226, 252, 255, 256 }, | ||
| 121 | { 256, 1, 2, 20, 83, 161, 219, 249, 255, 256 }, | ||
| 122 | |||
| 123 | // WB | ||
| 124 | { 256, 1, 2, 3, 12, 244, 253, 254, 255, 256 }, | ||
| 125 | { 256, 1, 2, 4, 32, 218, 253, 254, 255, 256 }, | ||
| 126 | { 256, 1, 2, 5, 47, 199, 252, 254, 255, 256 }, | ||
| 127 | { 256, 1, 2, 12, 61, 187, 252, 254, 255, 256 }, | ||
| 128 | { 256, 1, 5, 24, 72, 172, 249, 254, 255, 256 }, | ||
| 129 | { 256, 1, 2, 16, 70, 170, 242, 254, 255, 256 }, | ||
| 130 | { 256, 1, 2, 17, 78, 165, 226, 251, 255, 256 }, | ||
| 131 | { 256, 1, 8, 29, 79, 156, 237, 254, 255, 256 } | ||
| 132 | }; | ||
| 133 | |||
| 134 | static const uint16_t silk_model_lsf_s2_ext[] = { 256, 156, 216, 240, 249, 253, 255, 256 }; | ||
| 135 | |||
| 136 | static const uint16_t silk_model_lsf_interpolation_offset[] = { 256, 13, 35, 64, 75, 256 }; | ||
| 137 | |||
| 138 | static const uint16_t silk_model_pitch_highbits[] = { | ||
| 139 | 256, 3, 6, 12, 23, 44, 74, 106, 125, 136, 146, 158, 171, 184, 196, 207, | ||
| 140 | 216, 224, 231, 237, 241, 243, 245, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 | ||
| 141 | }; | ||
| 142 | |||
| 143 | static const uint16_t silk_model_pitch_lowbits_nb[]= { 256, 64, 128, 192, 256 }; | ||
| 144 | |||
| 145 | static const uint16_t silk_model_pitch_lowbits_mb[]= { 256, 43, 85, 128, 171, 213, 256 }; | ||
| 146 | |||
| 147 | static const uint16_t silk_model_pitch_lowbits_wb[]= { 256, 32, 64, 96, 128, 160, 192, 224, 256 }; | ||
| 148 | |||
| 149 | static const uint16_t silk_model_pitch_delta[] = { | ||
| 150 | 256, 46, 48, 50, 53, 57, 63, 73, 88, 114, 152, 182, 204, 219, 229, 236, | ||
| 151 | 242, 246, 250, 252, 254, 256 | ||
| 152 | }; | ||
| 153 | |||
| 154 | static const uint16_t silk_model_pitch_contour_nb10ms[] = { 256, 143, 193, 256 }; | ||
| 155 | |||
| 156 | static const uint16_t silk_model_pitch_contour_nb20ms[] = { | ||
| 157 | 256, 68, 80, 101, 118, 137, 159, 189, 213, 230, 246, 256 | ||
| 158 | }; | ||
| 159 | |||
| 160 | static const uint16_t silk_model_pitch_contour_mbwb10ms[] = { | ||
| 161 | 256, 91, 137, 176, 195, 209, 221, 229, 236, 242, 247, 252, 256 | ||
| 162 | }; | ||
| 163 | |||
| 164 | static const uint16_t silk_model_pitch_contour_mbwb20ms[] = { | ||
| 165 | 256, 33, 55, 73, 89, 104, 118, 132, 145, 158, 168, 177, 186, 194, 200, 206, | ||
| 166 | 212, 217, 221, 225, 229, 232, 235, 238, 240, 242, 244, 246, 248, 250, 252, 253, | ||
| 167 | 254, 255, 256 | ||
| 168 | }; | ||
| 169 | |||
| 170 | static const uint16_t silk_model_ltp_filter[] = { 256, 77, 157, 256 }; | ||
| 171 | |||
| 172 | static const uint16_t silk_model_ltp_filter0_sel[] = { | ||
| 173 | 256, 185, 200, 213, 226, 235, 244, 250, 256 | ||
| 174 | }; | ||
| 175 | |||
| 176 | static const uint16_t silk_model_ltp_filter1_sel[] = { | ||
| 177 | 256, 57, 91, 112, 132, 147, 160, 172, 185, 195, 205, 214, 224, 233, 241, 248, 256 | ||
| 178 | }; | ||
| 179 | |||
| 180 | static const uint16_t silk_model_ltp_filter2_sel[] = { | ||
| 181 | 256, 15, 31, 45, 57, 69, 81, 92, 103, 114, 124, 133, 142, 151, 160, 168, | ||
| 182 | 176, 184, 192, 199, 206, 212, 218, 223, 227, 232, 236, 240, 244, 247, 251, 254, 256 | ||
| 183 | }; | ||
| 184 | |||
| 185 | static const uint16_t silk_model_ltp_scale_index[] = { 256, 128, 192, 256 }; | ||
| 186 | |||
| 187 | static const uint16_t silk_model_lcg_seed[] = { 256, 64, 128, 192, 256 }; | ||
| 188 | |||
| 189 | static const uint16_t silk_model_exc_rate[2][10] = { | ||
| 190 | { 256, 15, 66, 78, 124, 169, 182, 215, 242, 256 }, // unvoiced | ||
| 191 | { 256, 33, 63, 99, 116, 150, 199, 217, 238, 256 } // voiced | ||
| 192 | }; | ||
| 193 | |||
| 194 | static const uint16_t silk_model_pulse_count[11][19] = { | ||
| 195 | { 256, 131, 205, 230, 238, 241, 244, 245, 246, | ||
| 196 | 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 }, | ||
| 197 | { 256, 58, 151, 211, 234, 241, 244, 245, 246, | ||
| 198 | 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 }, | ||
| 199 | { 256, 43, 94, 140, 173, 197, 213, 224, 232, | ||
| 200 | 238, 241, 244, 247, 249, 250, 251, 253, 254, 256 }, | ||
| 201 | { 256, 17, 69, 140, 197, 228, 240, 245, 246, | ||
| 202 | 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 }, | ||
| 203 | { 256, 6, 27, 68, 121, 170, 205, 226, 237, | ||
| 204 | 243, 246, 248, 250, 251, 252, 253, 254, 255, 256 }, | ||
| 205 | { 256, 7, 21, 43, 71, 100, 128, 153, 173, | ||
| 206 | 190, 203, 214, 223, 230, 235, 239, 243, 246, 256 }, | ||
| 207 | { 256, 2, 7, 21, 50, 92, 138, 179, 210, | ||
| 208 | 229, 240, 246, 249, 251, 252, 253, 254, 255, 256 }, | ||
| 209 | { 256, 1, 3, 7, 17, 36, 65, 100, 137, | ||
| 210 | 171, 199, 219, 233, 241, 246, 250, 252, 254, 256 }, | ||
| 211 | { 256, 1, 3, 5, 10, 19, 33, 53, 77, | ||
| 212 | 104, 132, 158, 181, 201, 216, 227, 235, 241, 256 }, | ||
| 213 | { 256, 1, 2, 3, 9, 36, 94, 150, 189, | ||
| 214 | 214, 228, 238, 244, 247, 250, 252, 253, 254, 256 }, | ||
| 215 | { 256, 2, 3, 9, 36, 94, 150, 189, 214, | ||
| 216 | 228, 238, 244, 247, 250, 252, 253, 254, 256, 256 } | ||
| 217 | }; | ||
| 218 | |||
| 219 | static const uint16_t silk_model_pulse_location[4][168] = { | ||
| 220 | { | ||
| 221 | 256, 126, 256, | ||
| 222 | 256, 56, 198, 256, | ||
| 223 | 256, 25, 126, 230, 256, | ||
| 224 | 256, 12, 72, 180, 244, 256, | ||
| 225 | 256, 7, 42, 126, 213, 250, 256, | ||
| 226 | 256, 4, 24, 83, 169, 232, 253, 256, | ||
| 227 | 256, 3, 15, 53, 125, 200, 242, 254, 256, | ||
| 228 | 256, 2, 10, 35, 89, 162, 221, 248, 255, 256, | ||
| 229 | 256, 2, 7, 24, 63, 126, 191, 233, 251, 255, 256, | ||
| 230 | 256, 1, 5, 17, 45, 94, 157, 211, 241, 252, 255, 256, | ||
| 231 | 256, 1, 5, 13, 33, 70, 125, 182, 223, 245, 253, 255, 256, | ||
| 232 | 256, 1, 4, 11, 26, 54, 98, 151, 199, 232, 248, 254, 255, 256, | ||
| 233 | 256, 1, 3, 9, 21, 42, 77, 124, 172, 212, 237, 249, 254, 255, 256, | ||
| 234 | 256, 1, 2, 6, 16, 33, 60, 97, 144, 187, 220, 241, 250, 254, 255, 256, | ||
| 235 | 256, 1, 2, 3, 11, 25, 47, 80, 120, 163, 201, 229, 245, 253, 254, 255, 256, | ||
| 236 | 256, 1, 2, 3, 4, 17, 35, 62, 98, 139, 180, 214, 238, 252, 253, 254, 255, 256 | ||
| 237 | },{ | ||
| 238 | 256, 127, 256, | ||
| 239 | 256, 53, 202, 256, | ||
| 240 | 256, 22, 127, 233, 256, | ||
| 241 | 256, 11, 72, 183, 246, 256, | ||
| 242 | 256, 6, 41, 127, 215, 251, 256, | ||
| 243 | 256, 4, 24, 83, 170, 232, 253, 256, | ||
| 244 | 256, 3, 16, 56, 127, 200, 241, 254, 256, | ||
| 245 | 256, 3, 12, 39, 92, 162, 218, 246, 255, 256, | ||
| 246 | 256, 3, 11, 30, 67, 124, 185, 229, 249, 255, 256, | ||
| 247 | 256, 3, 10, 25, 53, 97, 151, 200, 233, 250, 255, 256, | ||
| 248 | 256, 1, 8, 21, 43, 77, 123, 171, 209, 237, 251, 255, 256, | ||
| 249 | 256, 1, 2, 13, 35, 62, 97, 139, 186, 219, 244, 254, 255, 256, | ||
| 250 | 256, 1, 2, 8, 22, 48, 85, 128, 171, 208, 234, 248, 254, 255, 256, | ||
| 251 | 256, 1, 2, 6, 16, 36, 67, 107, 149, 189, 220, 240, 250, 254, 255, 256, | ||
| 252 | 256, 1, 2, 5, 13, 29, 55, 90, 128, 166, 201, 227, 243, 251, 254, 255, 256, | ||
| 253 | 256, 1, 2, 4, 10, 22, 43, 73, 109, 147, 183, 213, 234, 246, 252, 254, 255, 256 | ||
| 254 | },{ | ||
| 255 | 256, 127, 256, | ||
| 256 | 256, 49, 206, 256, | ||
| 257 | 256, 20, 127, 236, 256, | ||
| 258 | 256, 11, 71, 184, 246, 256, | ||
| 259 | 256, 7, 43, 127, 214, 250, 256, | ||
| 260 | 256, 6, 30, 87, 169, 229, 252, 256, | ||
| 261 | 256, 5, 23, 62, 126, 194, 236, 252, 256, | ||
| 262 | 256, 6, 20, 49, 96, 157, 209, 239, 253, 256, | ||
| 263 | 256, 1, 16, 39, 74, 125, 175, 215, 245, 255, 256, | ||
| 264 | 256, 1, 2, 23, 55, 97, 149, 195, 236, 254, 255, 256, | ||
| 265 | 256, 1, 7, 23, 50, 86, 128, 170, 206, 233, 249, 255, 256, | ||
| 266 | 256, 1, 6, 18, 39, 70, 108, 148, 186, 217, 238, 250, 255, 256, | ||
| 267 | 256, 1, 4, 13, 30, 56, 90, 128, 166, 200, 226, 243, 252, 255, 256, | ||
| 268 | 256, 1, 4, 11, 25, 47, 76, 110, 146, 180, 209, 231, 245, 252, 255, 256, | ||
| 269 | 256, 1, 3, 8, 19, 37, 62, 93, 128, 163, 194, 219, 237, 248, 253, 255, 256, | ||
| 270 | 256, 1, 2, 6, 15, 30, 51, 79, 111, 145, 177, 205, 226, 241, 250, 254, 255, 256 | ||
| 271 | },{ | ||
| 272 | 256, 128, 256, | ||
| 273 | 256, 42, 214, 256, | ||
| 274 | 256, 21, 128, 235, 256, | ||
| 275 | 256, 12, 72, 184, 245, 256, | ||
| 276 | 256, 8, 42, 128, 214, 249, 256, | ||
| 277 | 256, 8, 31, 86, 176, 231, 251, 256, | ||
| 278 | 256, 5, 20, 58, 130, 202, 238, 253, 256, | ||
| 279 | 256, 6, 18, 45, 97, 174, 221, 241, 251, 256, | ||
| 280 | 256, 6, 25, 53, 88, 128, 168, 203, 231, 250, 256, | ||
| 281 | 256, 4, 18, 40, 71, 108, 148, 185, 216, 238, 252, 256, | ||
| 282 | 256, 3, 13, 31, 57, 90, 128, 166, 199, 225, 243, 253, 256, | ||
| 283 | 256, 2, 10, 23, 44, 73, 109, 147, 183, 212, 233, 246, 254, 256, | ||
| 284 | 256, 1, 6, 16, 33, 58, 90, 128, 166, 198, 223, 240, 250, 255, 256, | ||
| 285 | 256, 1, 5, 12, 25, 46, 75, 110, 146, 181, 210, 231, 244, 251, 255, 256, | ||
| 286 | 256, 1, 3, 8, 18, 35, 60, 92, 128, 164, 196, 221, 238, 248, 253, 255, 256, | ||
| 287 | 256, 1, 3, 7, 14, 27, 48, 76, 110, 146, 180, 208, 229, 242, 249, 253, 255, 256 | ||
| 288 | } | ||
| 289 | }; | ||
| 290 | |||
| 291 | static const uint16_t silk_model_excitation_lsb[] = {256, 136, 256}; | ||
| 292 | |||
| 293 | static const uint16_t silk_model_excitation_sign[3][2][7][3] = { | ||
| 294 | { // Inactive | ||
| 295 | { // Low offset | ||
| 296 | {256, 2, 256}, | ||
| 297 | {256, 207, 256}, | ||
| 298 | {256, 189, 256}, | ||
| 299 | {256, 179, 256}, | ||
| 300 | {256, 174, 256}, | ||
| 301 | {256, 163, 256}, | ||
| 302 | {256, 157, 256} | ||
| 303 | }, { // High offset | ||
| 304 | {256, 58, 256}, | ||
| 305 | {256, 245, 256}, | ||
| 306 | {256, 238, 256}, | ||
| 307 | {256, 232, 256}, | ||
| 308 | {256, 225, 256}, | ||
| 309 | {256, 220, 256}, | ||
| 310 | {256, 211, 256} | ||
| 311 | } | ||
| 312 | }, { // Unvoiced | ||
| 313 | { // Low offset | ||
| 314 | {256, 1, 256}, | ||
| 315 | {256, 210, 256}, | ||
| 316 | {256, 190, 256}, | ||
| 317 | {256, 178, 256}, | ||
| 318 | {256, 169, 256}, | ||
| 319 | {256, 162, 256}, | ||
| 320 | {256, 152, 256} | ||
| 321 | }, { // High offset | ||
| 322 | {256, 48, 256}, | ||
| 323 | {256, 242, 256}, | ||
| 324 | {256, 235, 256}, | ||
| 325 | {256, 224, 256}, | ||
| 326 | {256, 214, 256}, | ||
| 327 | {256, 205, 256}, | ||
| 328 | {256, 190, 256} | ||
| 329 | } | ||
| 330 | }, { // Voiced | ||
| 331 | { // Low offset | ||
| 332 | {256, 1, 256}, | ||
| 333 | {256, 162, 256}, | ||
| 334 | {256, 152, 256}, | ||
| 335 | {256, 147, 256}, | ||
| 336 | {256, 144, 256}, | ||
| 337 | {256, 141, 256}, | ||
| 338 | {256, 138, 256} | ||
| 339 | }, { // High offset | ||
| 340 | {256, 8, 256}, | ||
| 341 | {256, 203, 256}, | ||
| 342 | {256, 187, 256}, | ||
| 343 | {256, 176, 256}, | ||
| 344 | {256, 168, 256}, | ||
| 345 | {256, 161, 256}, | ||
| 346 | {256, 154, 256} | ||
| 347 | } | ||
| 348 | } | ||
| 349 | }; | ||
| 350 | |||
| 351 | static const int16_t silk_stereo_weights[] = { | ||
| 352 | -13732, -10050, -8266, -7526, -6500, -5000, -2950, -820, | ||
| 353 | 820, 2950, 5000, 6500, 7526, 8266, 10050, 13732 | ||
| 354 | }; | ||
| 355 | |||
| 356 | static const uint8_t silk_lsf_s2_model_sel_nbmb[32][10] = { | ||
| 357 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 358 | { 1, 3, 1, 2, 2, 1, 2, 1, 1, 1 }, | ||
| 359 | { 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, | ||
| 360 | { 1, 2, 2, 2, 2, 1, 2, 1, 1, 1 }, | ||
| 361 | { 2, 3, 3, 3, 3, 2, 2, 2, 2, 2 }, | ||
| 362 | { 0, 5, 3, 3, 2, 2, 2, 2, 1, 1 }, | ||
| 363 | { 0, 2, 2, 2, 2, 2, 2, 2, 2, 1 }, | ||
| 364 | { 2, 3, 6, 4, 4, 4, 5, 4, 5, 5 }, | ||
| 365 | { 2, 4, 5, 5, 4, 5, 4, 6, 4, 4 }, | ||
| 366 | { 2, 4, 4, 7, 4, 5, 4, 5, 5, 4 }, | ||
| 367 | { 4, 3, 3, 3, 2, 3, 2, 2, 2, 2 }, | ||
| 368 | { 1, 5, 5, 6, 4, 5, 4, 5, 5, 5 }, | ||
| 369 | { 2, 7, 4, 6, 5, 5, 5, 5, 5, 5 }, | ||
| 370 | { 2, 7, 5, 5, 5, 5, 5, 6, 5, 4 }, | ||
| 371 | { 3, 3, 5, 4, 4, 5, 4, 5, 4, 4 }, | ||
| 372 | { 2, 3, 3, 5, 5, 4, 4, 4, 4, 4 }, | ||
| 373 | { 2, 4, 4, 6, 4, 5, 4, 5, 5, 5 }, | ||
| 374 | { 2, 5, 4, 6, 5, 5, 5, 4, 5, 4 }, | ||
| 375 | { 2, 7, 4, 5, 4, 5, 4, 5, 5, 5 }, | ||
| 376 | { 2, 5, 4, 6, 7, 6, 5, 6, 5, 4 }, | ||
| 377 | { 3, 6, 7, 4, 6, 5, 5, 6, 4, 5 }, | ||
| 378 | { 2, 7, 6, 4, 4, 4, 5, 4, 5, 5 }, | ||
| 379 | { 4, 5, 5, 4, 6, 6, 5, 6, 5, 4 }, | ||
| 380 | { 2, 5, 5, 6, 5, 6, 4, 6, 4, 4 }, | ||
| 381 | { 4, 5, 5, 5, 3, 7, 4, 5, 5, 4 }, | ||
| 382 | { 2, 3, 4, 5, 5, 6, 4, 5, 5, 4 }, | ||
| 383 | { 2, 3, 2, 3, 3, 4, 2, 3, 3, 3 }, | ||
| 384 | { 1, 1, 2, 2, 2, 2, 2, 3, 2, 2 }, | ||
| 385 | { 4, 5, 5, 6, 6, 6, 5, 6, 4, 5 }, | ||
| 386 | { 3, 5, 5, 4, 4, 4, 4, 3, 3, 2 }, | ||
| 387 | { 2, 5, 3, 7, 5, 5, 4, 4, 5, 4 }, | ||
| 388 | { 4, 4, 5, 4, 5, 6, 5, 6, 5, 4 } | ||
| 389 | }; | ||
| 390 | |||
| 391 | static const uint8_t silk_lsf_s2_model_sel_wb[32][16] = { | ||
| 392 | { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }, | ||
| 393 | { 10, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 9, 9, 9, 8, 11 }, | ||
| 394 | { 10, 13, 13, 11, 15, 12, 12, 13, 10, 13, 12, 13, 13, 12, 11, 11 }, | ||
| 395 | { 8, 10, 9, 10, 10, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 9 }, | ||
| 396 | { 8, 14, 13, 12, 14, 12, 15, 13, 12, 12, 12, 13, 13, 12, 12, 11 }, | ||
| 397 | { 8, 11, 13, 13, 12, 11, 11, 13, 11, 11, 11, 11, 11, 11, 10, 12 }, | ||
| 398 | { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }, | ||
| 399 | { 8, 10, 14, 11, 15, 10, 13, 11, 12, 13, 13, 12, 11, 11, 10, 11 }, | ||
| 400 | { 8, 14, 10, 14, 14, 12, 13, 12, 14, 13, 12, 12, 13, 11, 11, 11 }, | ||
| 401 | { 10, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }, | ||
| 402 | { 8, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9 }, | ||
| 403 | { 10, 10, 11, 12, 13, 11, 11, 11, 11, 11, 11, 11, 10, 10, 9, 11 }, | ||
| 404 | { 10, 10, 11, 11, 12, 11, 11, 11, 11, 11, 11, 11, 11, 10, 9, 11 }, | ||
| 405 | { 11, 12, 12, 12, 14, 12, 12, 13, 11, 13, 12, 12, 13, 12, 11, 12 }, | ||
| 406 | { 8, 14, 12, 13, 12, 15, 13, 10, 14, 13, 15, 12, 12, 11, 13, 11 }, | ||
| 407 | { 8, 9, 8, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 9, 8 }, | ||
| 408 | { 9, 14, 13, 15, 13, 12, 13, 11, 12, 13, 12, 12, 12, 11, 11, 12 }, | ||
| 409 | { 9, 11, 11, 12, 12, 11, 11, 13, 10, 11, 11, 13, 13, 13, 11, 12 }, | ||
| 410 | { 10, 11, 11, 10, 10, 10, 11, 10, 9, 10, 9, 10, 9, 9, 9, 12 }, | ||
| 411 | { 8, 10, 11, 13, 11, 11, 10, 10, 10, 9, 9, 8, 8, 8, 8, 8 }, | ||
| 412 | { 11, 12, 11, 13, 11, 11, 10, 10, 9, 9, 9, 9, 9, 10, 10, 12 }, | ||
| 413 | { 10, 14, 11, 15, 15, 12, 13, 12, 13, 11, 13, 11, 11, 10, 11, 11 }, | ||
| 414 | { 10, 11, 13, 14, 14, 11, 13, 11, 12, 12, 11, 11, 11, 11, 10, 12 }, | ||
| 415 | { 9, 11, 11, 12, 12, 12, 12, 11, 13, 13, 13, 11, 9, 9, 9, 9 }, | ||
| 416 | { 10, 13, 11, 14, 14, 12, 15, 12, 12, 13, 11, 12, 12, 11, 11, 11 }, | ||
| 417 | { 8, 14, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }, | ||
| 418 | { 8, 14, 14, 11, 13, 10, 13, 13, 11, 12, 12, 15, 15, 12, 12, 12 }, | ||
| 419 | { 11, 11, 15, 11, 13, 12, 11, 11, 11, 10, 10, 11, 11, 11, 10, 11 }, | ||
| 420 | { 8, 8, 9, 8, 8, 8, 10, 9, 10, 9, 9, 10, 10, 10, 9, 9 }, | ||
| 421 | { 8, 11, 10, 13, 11, 11, 10, 11, 10, 9, 8, 8, 9, 8, 8, 9 }, | ||
| 422 | { 11, 13, 13, 12, 15, 13, 11, 11, 10, 11, 10, 10, 9, 8, 9, 8 }, | ||
| 423 | { 10, 11, 13, 11, 12, 11, 11, 11, 10, 9, 10, 14, 12, 8, 8, 8 } | ||
| 424 | }; | ||
| 425 | |||
| 426 | static const uint8_t silk_lsf_pred_weights_nbmb[2][9] = { | ||
| 427 | {179, 138, 140, 148, 151, 149, 153, 151, 163}, | ||
| 428 | {116, 67, 82, 59, 92, 72, 100, 89, 92} | ||
| 429 | }; | ||
| 430 | |||
| 431 | static const uint8_t silk_lsf_pred_weights_wb[2][15] = { | ||
| 432 | {175, 148, 160, 176, 178, 173, 174, 164, 177, 174, 196, 182, 198, 192, 182}, | ||
| 433 | { 68, 62, 66, 60, 72, 117, 85, 90, 118, 136, 151, 142, 160, 142, 155} | ||
| 434 | }; | ||
| 435 | |||
| 436 | static const uint8_t silk_lsf_weight_sel_nbmb[32][9] = { | ||
| 437 | { 0, 1, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 438 | { 1, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 439 | { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 440 | { 1, 1, 1, 0, 0, 0, 0, 1, 0 }, | ||
| 441 | { 0, 1, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 442 | { 0, 1, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 443 | { 1, 0, 1, 1, 0, 0, 0, 1, 0 }, | ||
| 444 | { 0, 1, 1, 0, 0, 1, 1, 0, 0 }, | ||
| 445 | { 0, 0, 1, 1, 0, 1, 0, 1, 1 }, | ||
| 446 | { 0, 0, 1, 1, 0, 0, 1, 1, 1 }, | ||
| 447 | { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 448 | { 0, 1, 0, 1, 1, 1, 1, 1, 0 }, | ||
| 449 | { 0, 1, 0, 1, 1, 1, 1, 1, 0 }, | ||
| 450 | { 0, 1, 1, 1, 1, 1, 1, 1, 0 }, | ||
| 451 | { 1, 0, 1, 1, 0, 1, 1, 1, 1 }, | ||
| 452 | { 0, 1, 1, 1, 1, 1, 0, 1, 0 }, | ||
| 453 | { 0, 0, 1, 1, 0, 1, 0, 1, 0 }, | ||
| 454 | { 0, 0, 1, 1, 1, 0, 1, 1, 1 }, | ||
| 455 | { 0, 1, 1, 0, 0, 1, 1, 1, 0 }, | ||
| 456 | { 0, 0, 0, 1, 1, 1, 0, 1, 0 }, | ||
| 457 | { 0, 1, 1, 0, 0, 1, 0, 1, 0 }, | ||
| 458 | { 0, 1, 1, 0, 0, 0, 1, 1, 0 }, | ||
| 459 | { 0, 0, 0, 0, 0, 1, 1, 1, 1 }, | ||
| 460 | { 0, 0, 1, 1, 0, 0, 0, 1, 1 }, | ||
| 461 | { 0, 0, 0, 1, 0, 1, 1, 1, 1 }, | ||
| 462 | { 0, 1, 1, 1, 1, 1, 1, 1, 0 }, | ||
| 463 | { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 464 | { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 465 | { 0, 0, 1, 0, 1, 1, 0, 1, 0 }, | ||
| 466 | { 1, 0, 0, 1, 0, 0, 0, 0, 0 }, | ||
| 467 | { 0, 0, 0, 1, 1, 0, 1, 0, 1 }, | ||
| 468 | { 1, 0, 1, 1, 0, 1, 1, 1, 1 } | ||
| 469 | }; | ||
| 470 | |||
| 471 | static const uint8_t silk_lsf_weight_sel_wb[32][15] = { | ||
| 472 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | ||
| 473 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 474 | { 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0 }, | ||
| 475 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, | ||
| 476 | { 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0 }, | ||
| 477 | { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 478 | { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 }, | ||
| 479 | { 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1 }, | ||
| 480 | { 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1 }, | ||
| 481 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | ||
| 482 | { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 483 | { 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0 }, | ||
| 484 | { 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 }, | ||
| 485 | { 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0 }, | ||
| 486 | { 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1 }, | ||
| 487 | { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, | ||
| 488 | { 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0 }, | ||
| 489 | { 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0 }, | ||
| 490 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | ||
| 491 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, | ||
| 492 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 493 | { 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0 }, | ||
| 494 | { 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0 }, | ||
| 495 | { 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0 }, | ||
| 496 | { 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1 }, | ||
| 497 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | ||
| 498 | { 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1 }, | ||
| 499 | { 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, | ||
| 500 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | ||
| 501 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | ||
| 502 | { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 }, | ||
| 503 | { 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0 } | ||
| 504 | }; | ||
| 505 | |||
| 506 | static const uint8_t silk_lsf_codebook_nbmb[32][10] = { | ||
| 507 | { 12, 35, 60, 83, 108, 132, 157, 180, 206, 228 }, | ||
| 508 | { 15, 32, 55, 77, 101, 125, 151, 175, 201, 225 }, | ||
| 509 | { 19, 42, 66, 89, 114, 137, 162, 184, 209, 230 }, | ||
| 510 | { 12, 25, 50, 72, 97, 120, 147, 172, 200, 223 }, | ||
| 511 | { 26, 44, 69, 90, 114, 135, 159, 180, 205, 225 }, | ||
| 512 | { 13, 22, 53, 80, 106, 130, 156, 180, 205, 228 }, | ||
| 513 | { 15, 25, 44, 64, 90, 115, 142, 168, 196, 222 }, | ||
| 514 | { 19, 24, 62, 82, 100, 120, 145, 168, 190, 214 }, | ||
| 515 | { 22, 31, 50, 79, 103, 120, 151, 170, 203, 227 }, | ||
| 516 | { 21, 29, 45, 65, 106, 124, 150, 171, 196, 224 }, | ||
| 517 | { 30, 49, 75, 97, 121, 142, 165, 186, 209, 229 }, | ||
| 518 | { 19, 25, 52, 70, 93, 116, 143, 166, 192, 219 }, | ||
| 519 | { 26, 34, 62, 75, 97, 118, 145, 167, 194, 217 }, | ||
| 520 | { 25, 33, 56, 70, 91, 113, 143, 165, 196, 223 }, | ||
| 521 | { 21, 34, 51, 72, 97, 117, 145, 171, 196, 222 }, | ||
| 522 | { 20, 29, 50, 67, 90, 117, 144, 168, 197, 221 }, | ||
| 523 | { 22, 31, 48, 66, 95, 117, 146, 168, 196, 222 }, | ||
| 524 | { 24, 33, 51, 77, 116, 134, 158, 180, 200, 224 }, | ||
| 525 | { 21, 28, 70, 87, 106, 124, 149, 170, 194, 217 }, | ||
| 526 | { 26, 33, 53, 64, 83, 117, 152, 173, 204, 225 }, | ||
| 527 | { 27, 34, 65, 95, 108, 129, 155, 174, 210, 225 }, | ||
| 528 | { 20, 26, 72, 99, 113, 131, 154, 176, 200, 219 }, | ||
| 529 | { 34, 43, 61, 78, 93, 114, 155, 177, 205, 229 }, | ||
| 530 | { 23, 29, 54, 97, 124, 138, 163, 179, 209, 229 }, | ||
| 531 | { 30, 38, 56, 89, 118, 129, 158, 178, 200, 231 }, | ||
| 532 | { 21, 29, 49, 63, 85, 111, 142, 163, 193, 222 }, | ||
| 533 | { 27, 48, 77, 103, 133, 158, 179, 196, 215, 232 }, | ||
| 534 | { 29, 47, 74, 99, 124, 151, 176, 198, 220, 237 }, | ||
| 535 | { 33, 42, 61, 76, 93, 121, 155, 174, 207, 225 }, | ||
| 536 | { 29, 53, 87, 112, 136, 154, 170, 188, 208, 227 }, | ||
| 537 | { 24, 30, 52, 84, 131, 150, 166, 186, 203, 229 }, | ||
| 538 | { 37, 48, 64, 84, 104, 118, 156, 177, 201, 230 } | ||
| 539 | }; | ||
| 540 | |||
| 541 | static const uint8_t silk_lsf_codebook_wb[32][16] = { | ||
| 542 | { 7, 23, 38, 54, 69, 85, 100, 116, 131, 147, 162, 178, 193, 208, 223, 239 }, | ||
| 543 | { 13, 25, 41, 55, 69, 83, 98, 112, 127, 142, 157, 171, 187, 203, 220, 236 }, | ||
| 544 | { 15, 21, 34, 51, 61, 78, 92, 106, 126, 136, 152, 167, 185, 205, 225, 240 }, | ||
| 545 | { 10, 21, 36, 50, 63, 79, 95, 110, 126, 141, 157, 173, 189, 205, 221, 237 }, | ||
| 546 | { 17, 20, 37, 51, 59, 78, 89, 107, 123, 134, 150, 164, 184, 205, 224, 240 }, | ||
| 547 | { 10, 15, 32, 51, 67, 81, 96, 112, 129, 142, 158, 173, 189, 204, 220, 236 }, | ||
| 548 | { 8, 21, 37, 51, 65, 79, 98, 113, 126, 138, 155, 168, 179, 192, 209, 218 }, | ||
| 549 | { 12, 15, 34, 55, 63, 78, 87, 108, 118, 131, 148, 167, 185, 203, 219, 236 }, | ||
| 550 | { 16, 19, 32, 36, 56, 79, 91, 108, 118, 136, 154, 171, 186, 204, 220, 237 }, | ||
| 551 | { 11, 28, 43, 58, 74, 89, 105, 120, 135, 150, 165, 180, 196, 211, 226, 241 }, | ||
| 552 | { 6, 16, 33, 46, 60, 75, 92, 107, 123, 137, 156, 169, 185, 199, 214, 225 }, | ||
| 553 | { 11, 19, 30, 44, 57, 74, 89, 105, 121, 135, 152, 169, 186, 202, 218, 234 }, | ||
| 554 | { 12, 19, 29, 46, 57, 71, 88, 100, 120, 132, 148, 165, 182, 199, 216, 233 }, | ||
| 555 | { 17, 23, 35, 46, 56, 77, 92, 106, 123, 134, 152, 167, 185, 204, 222, 237 }, | ||
| 556 | { 14, 17, 45, 53, 63, 75, 89, 107, 115, 132, 151, 171, 188, 206, 221, 240 }, | ||
| 557 | { 9, 16, 29, 40, 56, 71, 88, 103, 119, 137, 154, 171, 189, 205, 222, 237 }, | ||
| 558 | { 16, 19, 36, 48, 57, 76, 87, 105, 118, 132, 150, 167, 185, 202, 218, 236 }, | ||
| 559 | { 12, 17, 29, 54, 71, 81, 94, 104, 126, 136, 149, 164, 182, 201, 221, 237 }, | ||
| 560 | { 15, 28, 47, 62, 79, 97, 115, 129, 142, 155, 168, 180, 194, 208, 223, 238 }, | ||
| 561 | { 8, 14, 30, 45, 62, 78, 94, 111, 127, 143, 159, 175, 192, 207, 223, 239 }, | ||
| 562 | { 17, 30, 49, 62, 79, 92, 107, 119, 132, 145, 160, 174, 190, 204, 220, 235 }, | ||
| 563 | { 14, 19, 36, 45, 61, 76, 91, 108, 121, 138, 154, 172, 189, 205, 222, 238 }, | ||
| 564 | { 12, 18, 31, 45, 60, 76, 91, 107, 123, 138, 154, 171, 187, 204, 221, 236 }, | ||
| 565 | { 13, 17, 31, 43, 53, 70, 83, 103, 114, 131, 149, 167, 185, 203, 220, 237 }, | ||
| 566 | { 17, 22, 35, 42, 58, 78, 93, 110, 125, 139, 155, 170, 188, 206, 224, 240 }, | ||
| 567 | { 8, 15, 34, 50, 67, 83, 99, 115, 131, 146, 162, 178, 193, 209, 224, 239 }, | ||
| 568 | { 13, 16, 41, 66, 73, 86, 95, 111, 128, 137, 150, 163, 183, 206, 225, 241 }, | ||
| 569 | { 17, 25, 37, 52, 63, 75, 92, 102, 119, 132, 144, 160, 175, 191, 212, 231 }, | ||
| 570 | { 19, 31, 49, 65, 83, 100, 117, 133, 147, 161, 174, 187, 200, 213, 227, 242 }, | ||
| 571 | { 18, 31, 52, 68, 88, 103, 117, 126, 138, 149, 163, 177, 192, 207, 223, 239 }, | ||
| 572 | { 16, 29, 47, 61, 76, 90, 106, 119, 133, 147, 161, 176, 193, 209, 224, 240 }, | ||
| 573 | { 15, 21, 35, 50, 61, 73, 86, 97, 110, 119, 129, 141, 175, 198, 218, 237 } | ||
| 574 | }; | ||
| 575 | |||
| 576 | static const uint16_t silk_lsf_min_spacing_nbmb[] = { | ||
| 577 | 250, 3, 6, 3, 3, 3, 4, 3, 3, 3, 461 | ||
| 578 | }; | ||
| 579 | |||
| 580 | static const uint16_t silk_lsf_min_spacing_wb[] = { | ||
| 581 | 100, 3, 40, 3, 3, 3, 5, 14, 14, 10, 11, 3, 8, 9, 7, 3, 347 | ||
| 582 | }; | ||
| 583 | |||
| 584 | static const uint8_t silk_lsf_ordering_nbmb[] = { | ||
| 585 | 0, 9, 6, 3, 4, 5, 8, 1, 2, 7 | ||
| 586 | }; | ||
| 587 | |||
| 588 | static const uint8_t silk_lsf_ordering_wb[] = { | ||
| 589 | 0, 15, 8, 7, 4, 11, 12, 3, 2, 13, 10, 5, 6, 9, 14, 1 | ||
| 590 | }; | ||
| 591 | |||
| 592 | static const int16_t silk_cosine[] = { /* (0.12) */ | ||
| 593 | 4096, 4095, 4091, 4085, | ||
| 594 | 4076, 4065, 4052, 4036, | ||
| 595 | 4017, 3997, 3973, 3948, | ||
| 596 | 3920, 3889, 3857, 3822, | ||
| 597 | 3784, 3745, 3703, 3659, | ||
| 598 | 3613, 3564, 3513, 3461, | ||
| 599 | 3406, 3349, 3290, 3229, | ||
| 600 | 3166, 3102, 3035, 2967, | ||
| 601 | 2896, 2824, 2751, 2676, | ||
| 602 | 2599, 2520, 2440, 2359, | ||
| 603 | 2276, 2191, 2106, 2019, | ||
| 604 | 1931, 1842, 1751, 1660, | ||
| 605 | 1568, 1474, 1380, 1285, | ||
| 606 | 1189, 1093, 995, 897, | ||
| 607 | 799, 700, 601, 501, | ||
| 608 | 401, 301, 201, 101, | ||
| 609 | 0, -101, -201, -301, | ||
| 610 | -401, -501, -601, -700, | ||
| 611 | -799, -897, -995, -1093, | ||
| 612 | -1189, -1285, -1380, -1474, | ||
| 613 | -1568, -1660, -1751, -1842, | ||
| 614 | -1931, -2019, -2106, -2191, | ||
| 615 | -2276, -2359, -2440, -2520, | ||
| 616 | -2599, -2676, -2751, -2824, | ||
| 617 | -2896, -2967, -3035, -3102, | ||
| 618 | -3166, -3229, -3290, -3349, | ||
| 619 | -3406, -3461, -3513, -3564, | ||
| 620 | -3613, -3659, -3703, -3745, | ||
| 621 | -3784, -3822, -3857, -3889, | ||
| 622 | -3920, -3948, -3973, -3997, | ||
| 623 | -4017, -4036, -4052, -4065, | ||
| 624 | -4076, -4085, -4091, -4095, | ||
| 625 | -4096 | ||
| 626 | }; | ||
| 627 | |||
| 628 | static const uint16_t silk_pitch_scale[] = { 4, 6, 8}; | ||
| 629 | |||
| 630 | static const uint16_t silk_pitch_min_lag[] = { 16, 24, 32}; | ||
| 631 | |||
| 632 | static const uint16_t silk_pitch_max_lag[] = {144, 216, 288}; | ||
| 633 | |||
| 634 | static const int8_t silk_pitch_offset_nb10ms[3][2] = { | ||
| 635 | { 0, 0}, | ||
| 636 | { 1, 0}, | ||
| 637 | { 0, 1} | ||
| 638 | }; | ||
| 639 | |||
| 640 | static const int8_t silk_pitch_offset_nb20ms[11][4] = { | ||
| 641 | { 0, 0, 0, 0}, | ||
| 642 | { 2, 1, 0, -1}, | ||
| 643 | {-1, 0, 1, 2}, | ||
| 644 | {-1, 0, 0, 1}, | ||
| 645 | {-1, 0, 0, 0}, | ||
| 646 | { 0, 0, 0, 1}, | ||
| 647 | { 0, 0, 1, 1}, | ||
| 648 | { 1, 1, 0, 0}, | ||
| 649 | { 1, 0, 0, 0}, | ||
| 650 | { 0, 0, 0, -1}, | ||
| 651 | { 1, 0, 0, -1} | ||
| 652 | }; | ||
| 653 | |||
| 654 | static const int8_t silk_pitch_offset_mbwb10ms[12][2] = { | ||
| 655 | { 0, 0}, | ||
| 656 | { 0, 1}, | ||
| 657 | { 1, 0}, | ||
| 658 | {-1, 1}, | ||
| 659 | { 1, -1}, | ||
| 660 | {-1, 2}, | ||
| 661 | { 2, -1}, | ||
| 662 | {-2, 2}, | ||
| 663 | { 2, -2}, | ||
| 664 | {-2, 3}, | ||
| 665 | { 3, -2}, | ||
| 666 | {-3, 3} | ||
| 667 | }; | ||
| 668 | |||
| 669 | static const int8_t silk_pitch_offset_mbwb20ms[34][4] = { | ||
| 670 | { 0, 0, 0, 0}, | ||
| 671 | { 0, 0, 1, 1}, | ||
| 672 | { 1, 1, 0, 0}, | ||
| 673 | {-1, 0, 0, 0}, | ||
| 674 | { 0, 0, 0, 1}, | ||
| 675 | { 1, 0, 0, 0}, | ||
| 676 | {-1, 0, 0, 1}, | ||
| 677 | { 0, 0, 0, -1}, | ||
| 678 | {-1, 0, 1, 2}, | ||
| 679 | { 1, 0, 0, -1}, | ||
| 680 | {-2, -1, 1, 2}, | ||
| 681 | { 2, 1, 0, -1}, | ||
| 682 | {-2, 0, 0, 2}, | ||
| 683 | {-2, 0, 1, 3}, | ||
| 684 | { 2, 1, -1, -2}, | ||
| 685 | {-3, -1, 1, 3}, | ||
| 686 | { 2, 0, 0, -2}, | ||
| 687 | { 3, 1, 0, -2}, | ||
| 688 | {-3, -1, 2, 4}, | ||
| 689 | {-4, -1, 1, 4}, | ||
| 690 | { 3, 1, -1, -3}, | ||
| 691 | {-4, -1, 2, 5}, | ||
| 692 | { 4, 2, -1, -3}, | ||
| 693 | { 4, 1, -1, -4}, | ||
| 694 | {-5, -1, 2, 6}, | ||
| 695 | { 5, 2, -1, -4}, | ||
| 696 | {-6, -2, 2, 6}, | ||
| 697 | {-5, -2, 2, 5}, | ||
| 698 | { 6, 2, -1, -5}, | ||
| 699 | {-7, -2, 3, 8}, | ||
| 700 | { 6, 2, -2, -6}, | ||
| 701 | { 5, 2, -2, -5}, | ||
| 702 | { 8, 3, -2, -7}, | ||
| 703 | {-9, -3, 3, 9} | ||
| 704 | }; | ||
| 705 | |||
| 706 | static const int8_t silk_ltp_filter0_taps[8][5] = { | ||
| 707 | { 4, 6, 24, 7, 5}, | ||
| 708 | { 0, 0, 2, 0, 0}, | ||
| 709 | { 12, 28, 41, 13, -4}, | ||
| 710 | { -9, 15, 42, 25, 14}, | ||
| 711 | { 1, -2, 62, 41, -9}, | ||
| 712 | {-10, 37, 65, -4, 3}, | ||
| 713 | { -6, 4, 66, 7, -8}, | ||
| 714 | { 16, 14, 38, -3, 33} | ||
| 715 | }; | ||
| 716 | |||
| 717 | static const int8_t silk_ltp_filter1_taps[16][5] = { | ||
| 718 | { 13, 22, 39, 23, 12}, | ||
| 719 | { -1, 36, 64, 27, -6}, | ||
| 720 | { -7, 10, 55, 43, 17}, | ||
| 721 | { 1, 1, 8, 1, 1}, | ||
| 722 | { 6, -11, 74, 53, -9}, | ||
| 723 | {-12, 55, 76, -12, 8}, | ||
| 724 | { -3, 3, 93, 27, -4}, | ||
| 725 | { 26, 39, 59, 3, -8}, | ||
| 726 | { 2, 0, 77, 11, 9}, | ||
| 727 | { -8, 22, 44, -6, 7}, | ||
| 728 | { 40, 9, 26, 3, 9}, | ||
| 729 | { -7, 20, 101, -7, 4}, | ||
| 730 | { 3, -8, 42, 26, 0}, | ||
| 731 | {-15, 33, 68, 2, 23}, | ||
| 732 | { -2, 55, 46, -2, 15}, | ||
| 733 | { 3, -1, 21, 16, 41} | ||
| 734 | }; | ||
| 735 | |||
| 736 | static const int8_t silk_ltp_filter2_taps[32][5] = { | ||
| 737 | { -6, 27, 61, 39, 5}, | ||
| 738 | {-11, 42, 88, 4, 1}, | ||
| 739 | { -2, 60, 65, 6, -4}, | ||
| 740 | { -1, -5, 73, 56, 1}, | ||
| 741 | { -9, 19, 94, 29, -9}, | ||
| 742 | { 0, 12, 99, 6, 4}, | ||
| 743 | { 8, -19, 102, 46, -13}, | ||
| 744 | { 3, 2, 13, 3, 2}, | ||
| 745 | { 9, -21, 84, 72, -18}, | ||
| 746 | {-11, 46, 104, -22, 8}, | ||
| 747 | { 18, 38, 48, 23, 0}, | ||
| 748 | {-16, 70, 83, -21, 11}, | ||
| 749 | { 5, -11, 117, 22, -8}, | ||
| 750 | { -6, 23, 117, -12, 3}, | ||
| 751 | { 3, -8, 95, 28, 4}, | ||
| 752 | {-10, 15, 77, 60, -15}, | ||
| 753 | { -1, 4, 124, 2, -4}, | ||
| 754 | { 3, 38, 84, 24, -25}, | ||
| 755 | { 2, 13, 42, 13, 31}, | ||
| 756 | { 21, -4, 56, 46, -1}, | ||
| 757 | { -1, 35, 79, -13, 19}, | ||
| 758 | { -7, 65, 88, -9, -14}, | ||
| 759 | { 20, 4, 81, 49, -29}, | ||
| 760 | { 20, 0, 75, 3, -17}, | ||
| 761 | { 5, -9, 44, 92, -8}, | ||
| 762 | { 1, -3, 22, 69, 31}, | ||
| 763 | { -6, 95, 41, -12, 5}, | ||
| 764 | { 39, 67, 16, -4, 1}, | ||
| 765 | { 0, -6, 120, 55, -36}, | ||
| 766 | {-13, 44, 122, 4, -24}, | ||
| 767 | { 81, 5, 11, 3, 7}, | ||
| 768 | { 2, 0, 9, 10, 88} | ||
| 769 | }; | ||
| 770 | |||
| 771 | static const uint16_t silk_ltp_scale_factor[] = {15565, 12288, 8192}; | ||
| 772 | |||
| 773 | static const uint8_t silk_shell_blocks[3][2] = { | ||
| 774 | { 5, 10}, // NB | ||
| 775 | { 8, 15}, // MB | ||
| 776 | {10, 20} // WB | ||
| 777 | }; | ||
| 778 | |||
| 779 | static const uint8_t silk_quant_offset[2][2] = { /* (0.23) */ | ||
| 780 | {25, 60}, // Inactive or Unvoiced | ||
| 781 | { 8, 25} // Voiced | ||
| 782 | }; | ||
| 783 | |||
| 784 | static const int silk_stereo_interp_len[3] = { | ||
| 785 | 64, 96, 128 | ||
| 786 | }; | ||
| 787 | |||
| 788 | static inline void silk_stabilize_lsf(int16_t nlsf[16], int order, const uint16_t min_delta[17]) | ||
| 789 | { | ||
| 790 | int pass, i; | ||
| 791 | for (pass = 0; pass < 20; pass++) { | ||
| 792 | int k, min_diff = 0; | ||
| 793 | for (i = 0; i < order+1; i++) { | ||
| 794 | int low = i != 0 ? nlsf[i-1] : 0; | ||
| 795 | int high = i != order ? nlsf[i] : 32768; | ||
| 796 | int diff = (high - low) - (min_delta[i]); | ||
| 797 | |||
| 798 | if (diff < min_diff) { | ||
| 799 | min_diff = diff; | ||
| 800 | k = i; | ||
| 801 | |||
| 802 | if (pass == 20) | ||
| 803 | break; | ||
| 804 | } | ||
| 805 | } | ||
| 806 | if (min_diff == 0) /* no issues; stabilized */ | ||
| 807 | return; | ||
| 808 | |||
| 809 | /* wiggle one or two LSFs */ | ||
| 810 | if (k == 0) { | ||
| 811 | /* repel away from lower bound */ | ||
| 812 | nlsf[0] = min_delta[0]; | ||
| 813 | } else if (k == order) { | ||
| 814 | /* repel away from higher bound */ | ||
| 815 | nlsf[order-1] = 32768 - min_delta[order]; | ||
| 816 | } else { | ||
| 817 | /* repel away from current position */ | ||
| 818 | int min_center = 0, max_center = 32768, center_val; | ||
| 819 | |||
| 820 | /* lower extent */ | ||
| 821 | for (i = 0; i < k; i++) | ||
| 822 | min_center += min_delta[i]; | ||
| 823 | min_center += min_delta[k] >> 1; | ||
| 824 | |||
| 825 | /* upper extent */ | ||
| 826 | for (i = order; i > k; i--) | ||
| 827 | max_center -= min_delta[k]; | ||
| 828 | max_center -= min_delta[k] >> 1; | ||
| 829 | |||
| 830 | /* move apart */ | ||
| 831 | center_val = nlsf[k - 1] + nlsf[k]; | ||
| 832 | center_val = (center_val >> 1) + (center_val & 1); // rounded divide by 2 | ||
| 833 | center_val = FFMIN(max_center, FFMAX(min_center, center_val)); | ||
| 834 | |||
| 835 | nlsf[k - 1] = center_val - (min_delta[k] >> 1); | ||
| 836 | nlsf[k] = nlsf[k - 1] + min_delta[k]; | ||
| 837 | } | ||
| 838 | } | ||
| 839 | |||
| 840 | /* resort to the fall-back method, the standard method for LSF stabilization */ | ||
| 841 | |||
| 842 | /* sort; as the LSFs should be nearly sorted, use insertion sort */ | ||
| 843 | for (i = 1; i < order; i++) { | ||
| 844 | int j, value = nlsf[i]; | ||
| 845 | for (j = i - 1; j >= 0 && nlsf[j] > value; j--) | ||
| 846 | nlsf[j + 1] = nlsf[j]; | ||
| 847 | nlsf[j + 1] = value; | ||
| 848 | } | ||
| 849 | |||
| 850 | /* push forwards to increase distance */ | ||
| 851 | if (nlsf[0] < min_delta[0]) | ||
| 852 | nlsf[0] = min_delta[0]; | ||
| 853 | for (i = 1; i < order; i++) | ||
| 854 | if (nlsf[i] < nlsf[i - 1] + min_delta[i]) | ||
| 855 | nlsf[i] = nlsf[i - 1] + min_delta[i]; | ||
| 856 | |||
| 857 | /* push backwards to increase distance */ | ||
| 858 | if (nlsf[order-1] > 32768 - min_delta[order]) | ||
| 859 | nlsf[order-1] = 32768 - min_delta[order]; | ||
| 860 | for (i = order-2; i >= 0; i--) | ||
| 861 | if (nlsf[i] > nlsf[i + 1] - min_delta[i+1]) | ||
| 862 | nlsf[i] = nlsf[i + 1] - min_delta[i+1]; | ||
| 863 | |||
| 864 | return; | ||
| 865 | } | ||
| 866 | |||
| 867 | static inline int silk_is_lpc_stable(const int16_t lpc[16], int order) | ||
| 868 | { | ||
| 869 | int k, j, DC_resp = 0; | ||
| 870 | int32_t lpc32[2][16]; // Q24 | ||
| 871 | int totalinvgain = 1 << 30; // 1.0 in Q30 | ||
| 872 | int32_t *row = lpc32[0], *prevrow; | ||
| 873 | |||
| 874 | /* initialize the first row for the Levinson recursion */ | ||
| 875 | for (k = 0; k < order; k++) { | ||
| 876 | DC_resp += lpc[k]; | ||
| 877 | row[k] = lpc[k] * 4096; | ||
| 878 | } | ||
| 879 | |||
| 880 | if (DC_resp >= 4096) | ||
| 881 | return 0; | ||
| 882 | |||
| 883 | /* check if prediction gain pushes any coefficients too far */ | ||
| 884 | for (k = order - 1; 1; k--) { | ||
| 885 | int rc; // Q31; reflection coefficient | ||
| 886 | int gaindiv; // Q30; inverse of the gain (the divisor) | ||
| 887 | int gain; // gain for this reflection coefficient | ||
| 888 | int fbits; // fractional bits used for the gain | ||
| 889 | int error; // Q29; estimate of the error of our partial estimate of 1/gaindiv | ||
| 890 | |||
| 891 | if (FFABS(row[k]) > 16773022) | ||
| 892 | return 0; | ||
| 893 | |||
| 894 | rc = -(row[k] * 128); | ||
| 895 | gaindiv = (1 << 30) - MULH(rc, rc); | ||
| 896 | |||
| 897 | totalinvgain = MULH(totalinvgain, gaindiv) << 2; | ||
| 898 | if (k == 0) | ||
| 899 | return (totalinvgain >= 107374); | ||
| 900 | |||
| 901 | /* approximate 1.0/gaindiv */ | ||
| 902 | fbits = opus_ilog(gaindiv); | ||
| 903 | gain = ((1 << 29) - 1) / (gaindiv >> (fbits + 1 - 16)); // Q<fbits-16> | ||
| 904 | error = (1 << 29) - MULL(gaindiv << (15 + 16 - fbits), gain, 16); | ||
| 905 | gain = ((gain << 16) + (error * gain >> 13)); | ||
| 906 | |||
| 907 | /* switch to the next row of the LPC coefficients */ | ||
| 908 | prevrow = row; | ||
| 909 | row = lpc32[k & 1]; | ||
| 910 | |||
| 911 | for (j = 0; j < k; j++) { | ||
| 912 | int x = prevrow[j] - ROUND_MULL(prevrow[k - j - 1], rc, 31); | ||
| 913 | row[j] = ROUND_MULL(x, gain, fbits); | ||
| 914 | } | ||
| 915 | } | ||
| 916 | } | ||
| 917 | |||
| 918 | static void silk_lsp2poly(const int32_t lsp[16], int32_t pol[16], int half_order) | ||
| 919 | { | ||
| 920 | int i, j; | ||
| 921 | |||
| 922 | pol[0] = 65536; // 1.0 in Q16 | ||
| 923 | pol[1] = -lsp[0]; | ||
| 924 | |||
| 925 | for (i = 1; i < half_order; i++) { | ||
| 926 | pol[i + 1] = pol[i - 1] * 2 - ROUND_MULL(lsp[2 * i], pol[i], 16); | ||
| 927 | for (j = i; j > 1; j--) | ||
| 928 | pol[j] += pol[j - 2] - ROUND_MULL(lsp[2 * i], pol[j - 1], 16); | ||
| 929 | |||
| 930 | pol[1] -= lsp[2 * i]; | ||
| 931 | } | ||
| 932 | } | ||
| 933 | |||
| 934 | static void silk_lsf2lpc(const int16_t nlsf[16], float lpcf[16], int order) | ||
| 935 | { | ||
| 936 | int i, k; | ||
| 937 | int32_t lsp[16]; // Q17; 2*cos(LSF) | ||
| 938 | int32_t p[9], q[9]; // Q16 | ||
| 939 | int32_t lpc32[16]; // Q17 | ||
| 940 | int16_t lpc[16]; // Q12 | ||
| 941 | |||
| 942 | /* convert the LSFs to LSPs, i.e. 2*cos(LSF) */ | ||
| 943 | for (k = 0; k < order; k++) { | ||
| 944 | int index = nlsf[k] >> 8; | ||
| 945 | int offset = nlsf[k] & 255; | ||
| 946 | int k2 = (order == 10) ? silk_lsf_ordering_nbmb[k] : silk_lsf_ordering_wb[k]; | ||
| 947 | |||
| 948 | /* interpolate and round */ | ||
| 949 | lsp[k2] = silk_cosine[index] * 256; | ||
| 950 | lsp[k2] += (silk_cosine[index + 1] - silk_cosine[index]) * offset; | ||
| 951 | lsp[k2] = (lsp[k2] + 4) >> 3; | ||
| 952 | } | ||
| 953 | |||
| 954 | silk_lsp2poly(lsp , p, order >> 1); | ||
| 955 | silk_lsp2poly(lsp + 1, q, order >> 1); | ||
| 956 | |||
| 957 | /* reconstruct A(z) */ | ||
| 958 | for (k = 0; k < order>>1; k++) { | ||
| 959 | lpc32[k] = -p[k + 1] - p[k] - q[k + 1] + q[k]; | ||
| 960 | lpc32[order-k-1] = -p[k + 1] - p[k] + q[k + 1] - q[k]; | ||
| 961 | } | ||
| 962 | |||
| 963 | /* limit the range of the LPC coefficients to each fit within an int16_t */ | ||
| 964 | for (i = 0; i < 10; i++) { | ||
| 965 | int j; | ||
| 966 | unsigned int maxabs = 0; | ||
| 967 | for (j = 0, k = 0; j < order; j++) { | ||
| 968 | unsigned int x = FFABS(lpc32[k]); | ||
| 969 | if (x > maxabs) { | ||
| 970 | maxabs = x; // Q17 | ||
| 971 | k = j; | ||
| 972 | } | ||
| 973 | } | ||
| 974 | |||
| 975 | maxabs = (maxabs + 16) >> 5; // convert to Q12 | ||
| 976 | |||
| 977 | if (maxabs > 32767) { | ||
| 978 | /* perform bandwidth expansion */ | ||
| 979 | unsigned int chirp, chirp_base; // Q16 | ||
| 980 | maxabs = FFMIN(maxabs, 163838); // anything above this overflows chirp's numerator | ||
| 981 | chirp_base = chirp = 65470 - ((maxabs - 32767) << 14) / ((maxabs * (k+1)) >> 2); | ||
| 982 | |||
| 983 | for (k = 0; k < order; k++) { | ||
| 984 | lpc32[k] = ROUND_MULL(lpc32[k], chirp, 16); | ||
| 985 | chirp = (chirp_base * chirp + 32768) >> 16; | ||
| 986 | } | ||
| 987 | } else break; | ||
| 988 | } | ||
| 989 | |||
| 990 | if (i == 10) { | ||
| 991 | /* time's up: just clamp */ | ||
| 992 | for (k = 0; k < order; k++) { | ||
| 993 | int x = (lpc32[k] + 16) >> 5; | ||
| 994 | lpc[k] = av_clip_int16(x); | ||
| 995 | lpc32[k] = lpc[k] << 5; // shortcut mandated by the spec; drops lower 5 bits | ||
| 996 | } | ||
| 997 | } else { | ||
| 998 | for (k = 0; k < order; k++) | ||
| 999 | lpc[k] = (lpc32[k] + 16) >> 5; | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | /* if the prediction gain causes the LPC filter to become unstable, | ||
| 1003 | apply further bandwidth expansion on the Q17 coefficients */ | ||
| 1004 | for (i = 1; i <= 16 && !silk_is_lpc_stable(lpc, order); i++) { | ||
| 1005 | unsigned int chirp, chirp_base; | ||
| 1006 | chirp_base = chirp = 65536 - (1 << i); | ||
| 1007 | |||
| 1008 | for (k = 0; k < order; k++) { | ||
| 1009 | lpc32[k] = ROUND_MULL(lpc32[k], chirp, 16); | ||
| 1010 | lpc[k] = (lpc32[k] + 16) >> 5; | ||
| 1011 | chirp = (chirp_base * chirp + 32768) >> 16; | ||
| 1012 | } | ||
| 1013 | } | ||
| 1014 | |||
| 1015 | for (i = 0; i < order; i++) | ||
| 1016 | lpcf[i] = lpc[i] / 4096.0f; | ||
| 1017 | } | ||
| 1018 | |||
| 1019 | static inline void silk_decode_lpc(SilkContext *s, SilkFrame *frame, | ||
| 1020 | OpusRangeCoder *rc, | ||
| 1021 | float lpc_leadin[16], float lpc[16], | ||
| 1022 | int *lpc_order, int *has_lpc_leadin, int voiced) | ||
| 1023 | { | ||
| 1024 | int i; | ||
| 1025 | int order; // order of the LP polynomial; 10 for NB/MB and 16 for WB | ||
| 1026 | int8_t lsf_i1, lsf_i2[16]; // stage-1 and stage-2 codebook indices | ||
| 1027 | int16_t lsf_res[16]; // residual as a Q10 value | ||
| 1028 | int16_t nlsf[16]; // Q15 | ||
| 1029 | |||
| 1030 | *lpc_order = order = s->wb ? 16 : 10; | ||
| 1031 | |||
| 1032 | /* obtain LSF stage-1 and stage-2 indices */ | ||
| 1033 | lsf_i1 = opus_rc_getsymbol(rc, silk_model_lsf_s1[s->wb][voiced]); | ||
| 1034 | for (i = 0; i < order; i++) { | ||
| 1035 | int index = s->wb ? silk_lsf_s2_model_sel_wb [lsf_i1][i] : | ||
| 1036 | silk_lsf_s2_model_sel_nbmb[lsf_i1][i]; | ||
| 1037 | lsf_i2[i] = opus_rc_getsymbol(rc, silk_model_lsf_s2[index]) - 4; | ||
| 1038 | if (lsf_i2[i] == -4) | ||
| 1039 | lsf_i2[i] -= opus_rc_getsymbol(rc, silk_model_lsf_s2_ext); | ||
| 1040 | else if (lsf_i2[i] == 4) | ||
| 1041 | lsf_i2[i] += opus_rc_getsymbol(rc, silk_model_lsf_s2_ext); | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | /* reverse the backwards-prediction step */ | ||
| 1045 | for (i = order - 1; i >= 0; i--) { | ||
| 1046 | int qstep = s->wb ? 9830 : 11796; | ||
| 1047 | |||
| 1048 | lsf_res[i] = lsf_i2[i] * 1024; | ||
| 1049 | if (lsf_i2[i] < 0) lsf_res[i] += 102; | ||
| 1050 | else if (lsf_i2[i] > 0) lsf_res[i] -= 102; | ||
| 1051 | lsf_res[i] = (lsf_res[i] * qstep) >> 16; | ||
| 1052 | |||
| 1053 | if (i + 1 < order) { | ||
| 1054 | int weight = s->wb ? silk_lsf_pred_weights_wb [silk_lsf_weight_sel_wb [lsf_i1][i]][i] : | ||
| 1055 | silk_lsf_pred_weights_nbmb[silk_lsf_weight_sel_nbmb[lsf_i1][i]][i]; | ||
| 1056 | lsf_res[i] += (lsf_res[i+1] * weight) >> 8; | ||
| 1057 | } | ||
| 1058 | } | ||
| 1059 | |||
| 1060 | /* reconstruct the NLSF coefficients from the supplied indices */ | ||
| 1061 | for (i = 0; i < order; i++) { | ||
| 1062 | const uint8_t * codebook = s->wb ? silk_lsf_codebook_wb [lsf_i1] : | ||
| 1063 | silk_lsf_codebook_nbmb[lsf_i1]; | ||
| 1064 | int cur, prev, next, weight_sq, weight, ipart, fpart, y, value; | ||
| 1065 | |||
| 1066 | /* find the weight of the residual */ | ||
| 1067 | /* TODO: precompute */ | ||
| 1068 | cur = codebook[i]; | ||
| 1069 | prev = i ? codebook[i - 1] : 0; | ||
| 1070 | next = i + 1 < order ? codebook[i + 1] : 256; | ||
| 1071 | weight_sq = (1024 / (cur - prev) + 1024 / (next - cur)) << 16; | ||
| 1072 | |||
| 1073 | /* approximate square-root with mandated fixed-point arithmetic */ | ||
| 1074 | ipart = opus_ilog(weight_sq); | ||
| 1075 | fpart = (weight_sq >> (ipart-8)) & 127; | ||
| 1076 | y = ((ipart & 1) ? 32768 : 46214) >> ((32 - ipart)>>1); | ||
| 1077 | weight = y + ((213 * fpart * y) >> 16); | ||
| 1078 | |||
| 1079 | value = cur * 128 + (lsf_res[i] * 16384) / weight; | ||
| 1080 | nlsf[i] = av_clip(value, 0, 32767); | ||
| 1081 | } | ||
| 1082 | |||
| 1083 | /* stabilize the NLSF coefficients */ | ||
| 1084 | silk_stabilize_lsf(nlsf, order, s->wb ? silk_lsf_min_spacing_wb : | ||
| 1085 | silk_lsf_min_spacing_nbmb); | ||
| 1086 | |||
| 1087 | /* produce an interpolation for the first 2 subframes, */ | ||
| 1088 | /* and then convert both sets of NLSFs to LPC coefficients */ | ||
| 1089 | *has_lpc_leadin = 0; | ||
| 1090 | if (s->subframes == 4) { | ||
| 1091 | int offset = opus_rc_getsymbol(rc, silk_model_lsf_interpolation_offset); | ||
| 1092 | if (offset != 4 && frame->coded) { | ||
| 1093 | *has_lpc_leadin = 1; | ||
| 1094 | if (offset != 0) { | ||
| 1095 | int16_t nlsf_leadin[16]; | ||
| 1096 | for (i = 0; i < order; i++) | ||
| 1097 | nlsf_leadin[i] = frame->nlsf[i] + | ||
| 1098 | ((nlsf[i] - frame->nlsf[i]) * offset >> 2); | ||
| 1099 | silk_lsf2lpc(nlsf_leadin, lpc_leadin, order); | ||
| 1100 | } else /* avoid re-computation for a (roughly) 1-in-4 occurrence */ | ||
| 1101 | memcpy(lpc_leadin, frame->lpc, 16 * sizeof(float)); | ||
| 1102 | } else | ||
| 1103 | offset = 4; | ||
| 1104 | s->nlsf_interp_factor = offset; | ||
| 1105 | |||
| 1106 | silk_lsf2lpc(nlsf, lpc, order); | ||
| 1107 | } else { | ||
| 1108 | s->nlsf_interp_factor = 4; | ||
| 1109 | silk_lsf2lpc(nlsf, lpc, order); | ||
| 1110 | } | ||
| 1111 | |||
| 1112 | memcpy(frame->nlsf, nlsf, order * sizeof(nlsf[0])); | ||
| 1113 | memcpy(frame->lpc, lpc, order * sizeof(lpc[0])); | ||
| 1114 | } | ||
| 1115 | |||
| 1116 | static inline void silk_count_children(OpusRangeCoder *rc, int model, int32_t total, | ||
| 1117 | int32_t child[2]) | ||
| 1118 | { | ||
| 1119 | if (total != 0) { | ||
| 1120 | child[0] = opus_rc_getsymbol(rc, | ||
| 1121 | silk_model_pulse_location[model] + (((total - 1 + 5) * (total - 1)) >> 1)); | ||
| 1122 | child[1] = total - child[0]; | ||
| 1123 | } else { | ||
| 1124 | child[0] = 0; | ||
| 1125 | child[1] = 0; | ||
| 1126 | } | ||
| 1127 | } | ||
| 1128 | |||
| 1129 | static inline void silk_decode_excitation(SilkContext *s, OpusRangeCoder *rc, | ||
| 1130 | float* excitationf, | ||
| 1131 | int qoffset_high, int active, int voiced) | ||
| 1132 | { | ||
| 1133 | int i; | ||
| 1134 | uint32_t seed; | ||
| 1135 | int shellblocks; | ||
| 1136 | int ratelevel; | ||
| 1137 | uint8_t pulsecount[20]; // total pulses in each shell block | ||
| 1138 | uint8_t lsbcount[20] = {0}; // raw lsbits defined for each pulse in each shell block | ||
| 1139 | int32_t excitation[320]; // Q23 | ||
| 1140 | |||
| 1141 | /* excitation parameters */ | ||
| 1142 | seed = opus_rc_getsymbol(rc, silk_model_lcg_seed); | ||
| 1143 | shellblocks = silk_shell_blocks[s->bandwidth][s->subframes >> 2]; | ||
| 1144 | ratelevel = opus_rc_getsymbol(rc, silk_model_exc_rate[voiced]); | ||
| 1145 | |||
| 1146 | for (i = 0; i < shellblocks; i++) { | ||
| 1147 | pulsecount[i] = opus_rc_getsymbol(rc, silk_model_pulse_count[ratelevel]); | ||
| 1148 | if (pulsecount[i] == 17) { | ||
| 1149 | while (pulsecount[i] == 17 && ++lsbcount[i] != 10) | ||
| 1150 | pulsecount[i] = opus_rc_getsymbol(rc, silk_model_pulse_count[9]); | ||
| 1151 | if (lsbcount[i] == 10) | ||
| 1152 | pulsecount[i] = opus_rc_getsymbol(rc, silk_model_pulse_count[10]); | ||
| 1153 | } | ||
| 1154 | } | ||
| 1155 | |||
| 1156 | /* decode pulse locations using PVQ */ | ||
| 1157 | for (i = 0; i < shellblocks; i++) { | ||
| 1158 | if (pulsecount[i] != 0) { | ||
| 1159 | int a, b, c, d; | ||
| 1160 | int32_t * location = excitation + 16*i; | ||
| 1161 | int32_t branch[4][2]; | ||
| 1162 | branch[0][0] = pulsecount[i]; | ||
| 1163 | |||
| 1164 | /* unrolled tail recursion */ | ||
| 1165 | for (a = 0; a < 1; a++) { | ||
| 1166 | silk_count_children(rc, 0, branch[0][a], branch[1]); | ||
| 1167 | for (b = 0; b < 2; b++) { | ||
| 1168 | silk_count_children(rc, 1, branch[1][b], branch[2]); | ||
| 1169 | for (c = 0; c < 2; c++) { | ||
| 1170 | silk_count_children(rc, 2, branch[2][c], branch[3]); | ||
| 1171 | for (d = 0; d < 2; d++) { | ||
| 1172 | silk_count_children(rc, 3, branch[3][d], location); | ||
| 1173 | location += 2; | ||
| 1174 | } | ||
| 1175 | } | ||
| 1176 | } | ||
| 1177 | } | ||
| 1178 | } else | ||
| 1179 | memset(excitation + 16*i, 0, 16*sizeof(int32_t)); | ||
| 1180 | } | ||
| 1181 | |||
| 1182 | /* decode least significant bits */ | ||
| 1183 | for (i = 0; i < shellblocks << 4; i++) { | ||
| 1184 | int bit; | ||
| 1185 | for (bit = 0; bit < lsbcount[i >> 4]; bit++) | ||
| 1186 | excitation[i] = (excitation[i] << 1) | | ||
| 1187 | opus_rc_getsymbol(rc, silk_model_excitation_lsb); | ||
| 1188 | } | ||
| 1189 | |||
| 1190 | /* decode signs */ | ||
| 1191 | for (i = 0; i < shellblocks << 4; i++) { | ||
| 1192 | if (excitation[i] != 0) { | ||
| 1193 | int sign = opus_rc_getsymbol(rc, silk_model_excitation_sign[active + | ||
| 1194 | voiced][qoffset_high][FFMIN(pulsecount[i >> 4], 6)]); | ||
| 1195 | if (sign == 0) | ||
| 1196 | excitation[i] *= -1; | ||
| 1197 | } | ||
| 1198 | } | ||
| 1199 | |||
| 1200 | /* assemble the excitation */ | ||
| 1201 | for (i = 0; i < shellblocks << 4; i++) { | ||
| 1202 | int value = excitation[i]; | ||
| 1203 | excitation[i] = value * 256 | silk_quant_offset[voiced][qoffset_high]; | ||
| 1204 | if (value < 0) excitation[i] += 20; | ||
| 1205 | else if (value > 0) excitation[i] -= 20; | ||
| 1206 | |||
| 1207 | /* invert samples pseudorandomly */ | ||
| 1208 | seed = 196314165 * seed + 907633515; | ||
| 1209 | if (seed & 0x80000000) | ||
| 1210 | excitation[i] *= -1; | ||
| 1211 | seed += value; | ||
| 1212 | |||
| 1213 | excitationf[i] = excitation[i] / 8388608.0f; | ||
| 1214 | } | ||
| 1215 | } | ||
| 1216 | |||
| 1217 | /** Maximum residual history according to 4.2.7.6.1 */ | ||
| 1218 | #define SILK_MAX_LAG (288 + LTP_ORDER / 2) | ||
| 1219 | |||
| 1220 | /** Order of the LTP filter */ | ||
| 1221 | #define LTP_ORDER 5 | ||
| 1222 | |||
| 1223 | static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc, | ||
| 1224 | int frame_num, int channel, int coded_channels, int active, int active1) | ||
| 1225 | { | ||
| 1226 | /* per frame */ | ||
| 1227 | int voiced; // combines with active to indicate inactive, active, or active+voiced | ||
| 1228 | int qoffset_high; | ||
| 1229 | int order; // order of the LPC coefficients | ||
| 1230 | float lpc_leadin[16], lpc_body[16], residual[SILK_MAX_LAG + SILK_HISTORY]; | ||
| 1231 | int has_lpc_leadin; | ||
| 1232 | float ltpscale; | ||
| 1233 | |||
| 1234 | /* per subframe */ | ||
| 1235 | struct { | ||
| 1236 | float gain; | ||
| 1237 | int pitchlag; | ||
| 1238 | float ltptaps[5]; | ||
| 1239 | } sf[4]; | ||
| 1240 | |||
| 1241 | SilkFrame * const frame = s->frame + channel; | ||
| 1242 | |||
| 1243 | int i; | ||
| 1244 | |||
| 1245 | /* obtain stereo weights */ | ||
| 1246 | if (coded_channels == 2 && channel == 0) { | ||
| 1247 | int n, wi[2], ws[2], w[2]; | ||
| 1248 | n = opus_rc_getsymbol(rc, silk_model_stereo_s1); | ||
| 1249 | wi[0] = opus_rc_getsymbol(rc, silk_model_stereo_s2) + 3 * (n / 5); | ||
| 1250 | ws[0] = opus_rc_getsymbol(rc, silk_model_stereo_s3); | ||
| 1251 | wi[1] = opus_rc_getsymbol(rc, silk_model_stereo_s2) + 3 * (n % 5); | ||
| 1252 | ws[1] = opus_rc_getsymbol(rc, silk_model_stereo_s3); | ||
| 1253 | |||
| 1254 | for (i = 0; i < 2; i++) | ||
| 1255 | w[i] = silk_stereo_weights[wi[i]] + | ||
| 1256 | (((silk_stereo_weights[wi[i] + 1] - silk_stereo_weights[wi[i]]) * 6554) >> 16) | ||
| 1257 | * (ws[i]*2 + 1); | ||
| 1258 | |||
| 1259 | s->stereo_weights[0] = (w[0] - w[1]) / 8192.0; | ||
| 1260 | s->stereo_weights[1] = w[1] / 8192.0; | ||
| 1261 | |||
| 1262 | /* and read the mid-only flag */ | ||
| 1263 | s->midonly = active1 ? 0 : opus_rc_getsymbol(rc, silk_model_mid_only); | ||
| 1264 | } | ||
| 1265 | |||
| 1266 | /* obtain frame type */ | ||
| 1267 | if (!active) { | ||
| 1268 | qoffset_high = opus_rc_getsymbol(rc, silk_model_frame_type_inactive); | ||
| 1269 | voiced = 0; | ||
| 1270 | } else { | ||
| 1271 | int type = opus_rc_getsymbol(rc, silk_model_frame_type_active); | ||
| 1272 | qoffset_high = type & 1; | ||
| 1273 | voiced = type >> 1; | ||
| 1274 | } | ||
| 1275 | |||
| 1276 | /* obtain subframe quantization gains */ | ||
| 1277 | for (i = 0; i < s->subframes; i++) { | ||
| 1278 | int log_gain; //Q7 | ||
| 1279 | int ipart, fpart, lingain; | ||
| 1280 | |||
| 1281 | if (i == 0 && (frame_num == 0 || !frame->coded)) { | ||
| 1282 | /* gain is coded absolute */ | ||
| 1283 | int x = opus_rc_getsymbol(rc, silk_model_gain_highbits[active + voiced]); | ||
| 1284 | log_gain = (x<<3) | opus_rc_getsymbol(rc, silk_model_gain_lowbits); | ||
| 1285 | |||
| 1286 | if (frame->coded) | ||
| 1287 | log_gain = FFMAX(log_gain, frame->log_gain - 16); | ||
| 1288 | } else { | ||
| 1289 | /* gain is coded relative */ | ||
| 1290 | int delta_gain = opus_rc_getsymbol(rc, silk_model_gain_delta); | ||
| 1291 | log_gain = av_clip(FFMAX((delta_gain<<1) - 16, | ||
| 1292 | frame->log_gain + delta_gain - 4), 0, 63); | ||
| 1293 | } | ||
| 1294 | |||
| 1295 | frame->log_gain = log_gain; | ||
| 1296 | |||
| 1297 | /* approximate 2**(x/128) with a Q7 (i.e. non-integer) input */ | ||
| 1298 | log_gain = (log_gain * 0x1D1C71 >> 16) + 2090; | ||
| 1299 | ipart = log_gain >> 7; | ||
| 1300 | fpart = log_gain & 127; | ||
| 1301 | lingain = (1 << ipart) + ((-174 * fpart * (128-fpart) >>16) + fpart) * ((1<<ipart) >> 7); | ||
| 1302 | sf[i].gain = lingain / 65536.0f; | ||
| 1303 | } | ||
| 1304 | |||
| 1305 | /* obtain LPC filter coefficients */ | ||
| 1306 | silk_decode_lpc(s, frame, rc, lpc_leadin, lpc_body, &order, &has_lpc_leadin, voiced); | ||
| 1307 | |||
| 1308 | /* obtain pitch lags, if this is a voiced frame */ | ||
| 1309 | if (voiced) { | ||
| 1310 | int lag_absolute = (!frame_num || !frame->prev_voiced); | ||
| 1311 | int primarylag; // primary pitch lag for the entire SILK frame | ||
| 1312 | int ltpfilter; | ||
| 1313 | const int8_t * offsets; | ||
| 1314 | |||
| 1315 | if (!lag_absolute) { | ||
| 1316 | int delta = opus_rc_getsymbol(rc, silk_model_pitch_delta); | ||
| 1317 | if (delta) | ||
| 1318 | primarylag = frame->primarylag + delta - 9; | ||
| 1319 | else | ||
| 1320 | lag_absolute = 1; | ||
| 1321 | } | ||
| 1322 | |||
| 1323 | if (lag_absolute) { | ||
| 1324 | /* primary lag is coded absolute */ | ||
| 1325 | int highbits, lowbits; | ||
| 1326 | const uint16_t *model[] = { | ||
| 1327 | silk_model_pitch_lowbits_nb, silk_model_pitch_lowbits_mb, | ||
| 1328 | silk_model_pitch_lowbits_wb | ||
| 1329 | }; | ||
| 1330 | highbits = opus_rc_getsymbol(rc, silk_model_pitch_highbits); | ||
| 1331 | lowbits = opus_rc_getsymbol(rc, model[s->bandwidth]); | ||
| 1332 | |||
| 1333 | primarylag = silk_pitch_min_lag[s->bandwidth] + | ||
| 1334 | highbits*silk_pitch_scale[s->bandwidth] + lowbits; | ||
| 1335 | } | ||
| 1336 | frame->primarylag = primarylag; | ||
| 1337 | |||
| 1338 | if (s->subframes == 2) | ||
| 1339 | offsets = (s->bandwidth == OPUS_BANDWIDTH_NARROWBAND) | ||
| 1340 | ? silk_pitch_offset_nb10ms[opus_rc_getsymbol(rc, | ||
| 1341 | silk_model_pitch_contour_nb10ms)] | ||
| 1342 | : silk_pitch_offset_mbwb10ms[opus_rc_getsymbol(rc, | ||
| 1343 | silk_model_pitch_contour_mbwb10ms)]; | ||
| 1344 | else | ||
| 1345 | offsets = (s->bandwidth == OPUS_BANDWIDTH_NARROWBAND) | ||
| 1346 | ? silk_pitch_offset_nb20ms[opus_rc_getsymbol(rc, | ||
| 1347 | silk_model_pitch_contour_nb20ms)] | ||
| 1348 | : silk_pitch_offset_mbwb20ms[opus_rc_getsymbol(rc, | ||
| 1349 | silk_model_pitch_contour_mbwb20ms)]; | ||
| 1350 | |||
| 1351 | for (i = 0; i < s->subframes; i++) | ||
| 1352 | sf[i].pitchlag = av_clip(primarylag + offsets[i], | ||
| 1353 | silk_pitch_min_lag[s->bandwidth], | ||
| 1354 | silk_pitch_max_lag[s->bandwidth]); | ||
| 1355 | |||
| 1356 | /* obtain LTP filter coefficients */ | ||
| 1357 | ltpfilter = opus_rc_getsymbol(rc, silk_model_ltp_filter); | ||
| 1358 | for (i = 0; i < s->subframes; i++) { | ||
| 1359 | int index, j; | ||
| 1360 | const uint16_t *filter_sel[] = { | ||
| 1361 | silk_model_ltp_filter0_sel, silk_model_ltp_filter1_sel, | ||
| 1362 | silk_model_ltp_filter2_sel | ||
| 1363 | }; | ||
| 1364 | const int8_t (*filter_taps[])[5] = { | ||
| 1365 | silk_ltp_filter0_taps, silk_ltp_filter1_taps, silk_ltp_filter2_taps | ||
| 1366 | }; | ||
| 1367 | index = opus_rc_getsymbol(rc, filter_sel[ltpfilter]); | ||
| 1368 | for (j = 0; j < 5; j++) | ||
| 1369 | sf[i].ltptaps[j] = filter_taps[ltpfilter][index][j] / 128.0f; | ||
| 1370 | } | ||
| 1371 | } | ||
| 1372 | |||
| 1373 | /* obtain LTP scale factor */ | ||
| 1374 | if (voiced && frame_num == 0) | ||
| 1375 | ltpscale = silk_ltp_scale_factor[opus_rc_getsymbol(rc, | ||
| 1376 | silk_model_ltp_scale_index)] / 16384.0f; | ||
| 1377 | else ltpscale = 15565.0f/16384.0f; | ||
| 1378 | |||
| 1379 | /* generate the excitation signal for the entire frame */ | ||
| 1380 | silk_decode_excitation(s, rc, residual + SILK_MAX_LAG, qoffset_high, | ||
| 1381 | active, voiced); | ||
| 1382 | |||
| 1383 | /* skip synthesising the side channel if we want mono-only */ | ||
| 1384 | if (s->output_channels == channel) | ||
| 1385 | return; | ||
| 1386 | |||
| 1387 | /* generate the output signal */ | ||
| 1388 | for (i = 0; i < s->subframes; i++) { | ||
| 1389 | const float * lpc_coeff = (i < 2 && has_lpc_leadin) ? lpc_leadin : lpc_body; | ||
| 1390 | float *dst = frame->output + SILK_HISTORY + i * s->sflength; | ||
| 1391 | float *resptr = residual + SILK_MAX_LAG + i * s->sflength; | ||
| 1392 | float *lpc = frame->lpc_history + SILK_HISTORY + i * s->sflength; | ||
| 1393 | float sum; | ||
| 1394 | int j, k; | ||
| 1395 | |||
| 1396 | if (voiced) { | ||
| 1397 | int out_end; | ||
| 1398 | float scale; | ||
| 1399 | |||
| 1400 | if (i < 2 || s->nlsf_interp_factor == 4) { | ||
| 1401 | out_end = -i * s->sflength; | ||
| 1402 | scale = ltpscale; | ||
| 1403 | } else { | ||
| 1404 | out_end = -(i - 2) * s->sflength; | ||
| 1405 | scale = 1.0f; | ||
| 1406 | } | ||
| 1407 | |||
| 1408 | /* when the LPC coefficients change, a re-whitening filter is used */ | ||
| 1409 | /* to produce a residual that accounts for the change */ | ||
| 1410 | for (j = - sf[i].pitchlag - LTP_ORDER/2; j < out_end; j++) { | ||
| 1411 | sum = dst[j]; | ||
| 1412 | for (k = 0; k < order; k++) | ||
| 1413 | sum -= lpc_coeff[k] * dst[j - k - 1]; | ||
| 1414 | resptr[j] = av_clipf(sum, -1.0f, 1.0f) * scale / sf[i].gain; | ||
| 1415 | } | ||
| 1416 | |||
| 1417 | if (out_end) { | ||
| 1418 | float rescale = sf[i-1].gain / sf[i].gain; | ||
| 1419 | for (j = out_end; j < 0; j++) | ||
| 1420 | resptr[j] *= rescale; | ||
| 1421 | } | ||
| 1422 | |||
| 1423 | /* LTP synthesis */ | ||
| 1424 | for (j = 0; j < s->sflength; j++) { | ||
| 1425 | sum = resptr[j]; | ||
| 1426 | for (k = 0; k < LTP_ORDER; k++) | ||
| 1427 | sum += sf[i].ltptaps[k] * resptr[j - sf[i].pitchlag + LTP_ORDER/2 - k]; | ||
| 1428 | resptr[j] = sum; | ||
| 1429 | } | ||
| 1430 | } | ||
| 1431 | |||
| 1432 | /* LPC synthesis */ | ||
| 1433 | for (j = 0; j < s->sflength; j++) { | ||
| 1434 | sum = resptr[j] * sf[i].gain; | ||
| 1435 | for (k = 1; k <= order; k++) | ||
| 1436 | sum += lpc_coeff[k - 1] * lpc[j - k]; | ||
| 1437 | |||
| 1438 | lpc[j] = sum; | ||
| 1439 | dst[j] = av_clipf(sum, -1.0f, 1.0f); | ||
| 1440 | } | ||
| 1441 | } | ||
| 1442 | |||
| 1443 | frame->prev_voiced = voiced; | ||
| 1444 | memmove(frame->lpc_history, frame->lpc_history + s->flength, SILK_HISTORY * sizeof(float)); | ||
| 1445 | memmove(frame->output, frame->output + s->flength, SILK_HISTORY * sizeof(float)); | ||
| 1446 | |||
| 1447 | frame->coded = 1; | ||
| 1448 | } | ||
| 1449 | |||
| 1450 | static void silk_unmix_ms(SilkContext *s, float *l, float *r) | ||
| 1451 | { | ||
| 1452 | float *mid = s->frame[0].output + SILK_HISTORY - s->flength; | ||
| 1453 | float *side = s->frame[1].output + SILK_HISTORY - s->flength; | ||
| 1454 | float w0_prev = s->prev_stereo_weights[0]; | ||
| 1455 | float w1_prev = s->prev_stereo_weights[1]; | ||
| 1456 | float w0 = s->stereo_weights[0]; | ||
| 1457 | float w1 = s->stereo_weights[1]; | ||
| 1458 | int n1 = silk_stereo_interp_len[s->bandwidth]; | ||
| 1459 | int i; | ||
| 1460 | |||
| 1461 | for (i = 0; i < n1; i++) { | ||
| 1462 | float interp0 = w0_prev + i * (w0 - w0_prev) / n1; | ||
| 1463 | float interp1 = w1_prev + i * (w1 - w1_prev) / n1; | ||
| 1464 | float p0 = 0.25 * (mid[i - 2] + 2 * mid[i - 1] + mid[i]); | ||
| 1465 | |||
| 1466 | l[i] = av_clipf((1 + interp1) * mid[i - 1] + side[i - 1] + interp0 * p0, -1.0, 1.0); | ||
| 1467 | r[i] = av_clipf((1 - interp1) * mid[i - 1] - side[i - 1] - interp0 * p0, -1.0, 1.0); | ||
| 1468 | } | ||
| 1469 | |||
| 1470 | for (; i < s->flength; i++) { | ||
| 1471 | float p0 = 0.25 * (mid[i - 2] + 2 * mid[i - 1] + mid[i]); | ||
| 1472 | |||
| 1473 | l[i] = av_clipf((1 + w1) * mid[i - 1] + side[i - 1] + w0 * p0, -1.0, 1.0); | ||
| 1474 | r[i] = av_clipf((1 - w1) * mid[i - 1] - side[i - 1] - w0 * p0, -1.0, 1.0); | ||
| 1475 | } | ||
| 1476 | |||
| 1477 | memcpy(s->prev_stereo_weights, s->stereo_weights, sizeof(s->stereo_weights)); | ||
| 1478 | } | ||
| 1479 | |||
| 1480 | static void silk_flush_frame(SilkFrame *frame) | ||
| 1481 | { | ||
| 1482 | if (!frame->coded) | ||
| 1483 | return; | ||
| 1484 | |||
| 1485 | memset(frame->output, 0, sizeof(frame->output)); | ||
| 1486 | memset(frame->lpc_history, 0, sizeof(frame->lpc_history)); | ||
| 1487 | |||
| 1488 | memset(frame->lpc, 0, sizeof(frame->lpc)); | ||
| 1489 | memset(frame->nlsf, 0, sizeof(frame->nlsf)); | ||
| 1490 | |||
| 1491 | frame->log_gain = 0; | ||
| 1492 | |||
| 1493 | frame->primarylag = 0; | ||
| 1494 | frame->prev_voiced = 0; | ||
| 1495 | frame->coded = 0; | ||
| 1496 | } | ||
| 1497 | |||
| 1498 | int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc, | ||
| 1499 | float *output[2], | ||
| 1500 | enum OpusBandwidth bandwidth, | ||
| 1501 | int coded_channels, | ||
| 1502 | int duration_ms) | ||
| 1503 | { | ||
| 1504 | int active[2][6], redundancy[2]; | ||
| 1505 | int nb_frames, i, j; | ||
| 1506 | |||
| 1507 | if (bandwidth > OPUS_BANDWIDTH_WIDEBAND || | ||
| 1508 | coded_channels > 2 || duration_ms > 60) { | ||
| 1509 | av_log(s->avctx, AV_LOG_ERROR, "Invalid parameters passed " | ||
| 1510 | "to the SILK decoder.\n"); | ||
| 1511 | return AVERROR(EINVAL); | ||
| 1512 | } | ||
| 1513 | |||
| 1514 | nb_frames = 1 + (duration_ms > 20) + (duration_ms > 40); | ||
| 1515 | s->subframes = duration_ms / nb_frames / 5; // 5ms subframes | ||
| 1516 | s->sflength = 20 * (bandwidth + 2); | ||
| 1517 | s->flength = s->sflength * s->subframes; | ||
| 1518 | s->bandwidth = bandwidth; | ||
| 1519 | s->wb = bandwidth == OPUS_BANDWIDTH_WIDEBAND; | ||
| 1520 | |||
| 1521 | /* make sure to flush the side channel when switching from mono to stereo */ | ||
| 1522 | if (coded_channels > s->prev_coded_channels) | ||
| 1523 | silk_flush_frame(&s->frame[1]); | ||
| 1524 | s->prev_coded_channels = coded_channels; | ||
| 1525 | |||
| 1526 | /* read the LP-layer header bits */ | ||
| 1527 | for (i = 0; i < coded_channels; i++) { | ||
| 1528 | for (j = 0; j < nb_frames; j++) | ||
| 1529 | active[i][j] = opus_rc_p2model(rc, 1); | ||
| 1530 | |||
| 1531 | redundancy[i] = opus_rc_p2model(rc, 1); | ||
| 1532 | if (redundancy[i]) { | ||
| 1533 | av_log(s->avctx, AV_LOG_ERROR, "LBRR frames present; this is unsupported\n"); | ||
| 1534 | return AVERROR_PATCHWELCOME; | ||
| 1535 | } | ||
| 1536 | } | ||
| 1537 | |||
| 1538 | for (i = 0; i < nb_frames; i++) { | ||
| 1539 | for (j = 0; j < coded_channels && !s->midonly; j++) | ||
| 1540 | silk_decode_frame(s, rc, i, j, coded_channels, active[j][i], active[1][i]); | ||
| 1541 | |||
| 1542 | /* reset the side channel if it is not coded */ | ||
| 1543 | if (s->midonly && s->frame[1].coded) | ||
| 1544 | silk_flush_frame(&s->frame[1]); | ||
| 1545 | |||
| 1546 | if (coded_channels == 1 || s->output_channels == 1) { | ||
| 1547 | for (j = 0; j < s->output_channels; j++) { | ||
| 1548 | memcpy(output[j] + i * s->flength, | ||
| 1549 | s->frame[0].output + SILK_HISTORY - s->flength - 2, | ||
| 1550 | s->flength * sizeof(float)); | ||
| 1551 | } | ||
| 1552 | } else { | ||
| 1553 | silk_unmix_ms(s, output[0] + i * s->flength, output[1] + i * s->flength); | ||
| 1554 | } | ||
| 1555 | |||
| 1556 | s->midonly = 0; | ||
| 1557 | } | ||
| 1558 | |||
| 1559 | return nb_frames * s->flength; | ||
| 1560 | } | ||
| 1561 | |||
| 1562 | void ff_silk_free(SilkContext **ps) | ||
| 1563 | { | ||
| 1564 | av_freep(ps); | ||
| 1565 | } | ||
| 1566 | |||
| 1567 | void ff_silk_flush(SilkContext *s) | ||
| 1568 | { | ||
| 1569 | silk_flush_frame(&s->frame[0]); | ||
| 1570 | silk_flush_frame(&s->frame[1]); | ||
| 1571 | |||
| 1572 | memset(s->prev_stereo_weights, 0, sizeof(s->prev_stereo_weights)); | ||
| 1573 | } | ||
| 1574 | |||
| 1575 | int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels) | ||
| 1576 | { | ||
| 1577 | SilkContext *s; | ||
| 1578 | |||
| 1579 | if (output_channels != 1 && output_channels != 2) { | ||
| 1580 | av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n", | ||
| 1581 | output_channels); | ||
| 1582 | return AVERROR(EINVAL); | ||
| 1583 | } | ||
| 1584 | |||
| 1585 | s = av_mallocz(sizeof(*s)); | ||
| 1586 | if (!s) | ||
| 1587 | return AVERROR(ENOMEM); | ||
| 1588 | |||
| 1589 | s->avctx = avctx; | ||
| 1590 | s->output_channels = output_channels; | ||
| 1591 | |||
| 1592 | ff_silk_flush(s); | ||
| 1593 | |||
| 1594 | *ps = s; | ||
| 1595 | |||
| 1596 | return 0; | ||
| 1597 | } | ||
| @@ -0,0 +1,674 @@ | |||
| 1 | /* | ||
| 2 | * Opus decoder | ||
| 3 | * Copyright (c) 2012 Andrew D'Addesio | ||
| 4 | * Copyright (c) 2013-2014 Mozilla Corporation | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @file | ||
| 25 | * Opus decoder | ||
| 26 | * @author Andrew D'Addesio, Anton Khirnov | ||
| 27 | * | ||
| 28 | * Codec homepage: http://opus-codec.org/ | ||
| 29 | * Specification: http://tools.ietf.org/html/rfc6716 | ||
| 30 | * Ogg Opus specification: https://tools.ietf.org/html/draft-ietf-codec-oggopus-03 | ||
| 31 | * | ||
| 32 | * Ogg-contained .opus files can be produced with opus-tools: | ||
| 33 | * http://git.xiph.org/?p=opus-tools.git | ||
| 34 | */ | ||
| 35 | |||
| 36 | #include <stdint.h> | ||
| 37 | |||
| 38 | #include "libavutil/attributes.h" | ||
| 39 | #include "libavutil/audio_fifo.h" | ||
| 40 | #include "libavutil/channel_layout.h" | ||
| 41 | #include "libavutil/opt.h" | ||
| 42 | |||
| 43 | #include "libavresample/avresample.h" | ||
| 44 | |||
| 45 | #include "avcodec.h" | ||
| 46 | #include "celp_filters.h" | ||
| 47 | #include "fft.h" | ||
| 48 | #include "get_bits.h" | ||
| 49 | #include "internal.h" | ||
| 50 | #include "mathops.h" | ||
| 51 | #include "opus.h" | ||
| 52 | |||
| 53 | static const uint16_t silk_frame_duration_ms[16] = { | ||
| 54 | 10, 20, 40, 60, | ||
| 55 | 10, 20, 40, 60, | ||
| 56 | 10, 20, 40, 60, | ||
| 57 | 10, 20, | ||
| 58 | 10, 20, | ||
| 59 | }; | ||
| 60 | |||
| 61 | /* number of samples of silence to feed to the resampler | ||
| 62 | * at the beginning */ | ||
| 63 | static const int silk_resample_delay[] = { | ||
| 64 | 4, 8, 11, 11, 11 | ||
| 65 | }; | ||
| 66 | |||
| 67 | static const uint8_t celt_band_end[] = { 13, 17, 17, 19, 21 }; | ||
| 68 | |||
| 69 | static int get_silk_samplerate(int config) | ||
| 70 | { | ||
| 71 | if (config < 4) | ||
| 72 | return 8000; | ||
| 73 | else if (config < 8) | ||
| 74 | return 12000; | ||
| 75 | return 16000; | ||
| 76 | } | ||
| 77 | |||
| 78 | /** | ||
| 79 | * Range decoder | ||
| 80 | */ | ||
| 81 | static int opus_rc_init(OpusRangeCoder *rc, const uint8_t *data, int size) | ||
| 82 | { | ||
| 83 | int ret = init_get_bits8(&rc->gb, data, size); | ||
| 84 | if (ret < 0) | ||
| 85 | return ret; | ||
| 86 | |||
| 87 | rc->range = 128; | ||
| 88 | rc->value = 127 - get_bits(&rc->gb, 7); | ||
| 89 | rc->total_read_bits = 9; | ||
| 90 | opus_rc_normalize(rc); | ||
| 91 | |||
| 92 | return 0; | ||
| 93 | } | ||
| 94 | |||
| 95 | static void opus_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, | ||
| 96 | unsigned int bytes) | ||
| 97 | { | ||
| 98 | rc->rb.position = rightend; | ||
| 99 | rc->rb.bytes = bytes; | ||
| 100 | rc->rb.cachelen = 0; | ||
| 101 | rc->rb.cacheval = 0; | ||
| 102 | } | ||
| 103 | |||
| 104 | static void opus_fade(float *out, | ||
| 105 | const float *in1, const float *in2, | ||
| 106 | const float *window, int len) | ||
| 107 | { | ||
| 108 | int i; | ||
| 109 | for (i = 0; i < len; i++) | ||
| 110 | out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]); | ||
| 111 | } | ||
| 112 | |||
| 113 | static int opus_flush_resample(OpusStreamContext *s, int nb_samples) | ||
| 114 | { | ||
| 115 | int celt_size = av_audio_fifo_size(s->celt_delay); | ||
| 116 | int ret, i; | ||
| 117 | |||
| 118 | ret = avresample_convert(s->avr, (uint8_t**)s->out, s->out_size, nb_samples, | ||
| 119 | NULL, 0, 0); | ||
| 120 | if (ret < 0) | ||
| 121 | return ret; | ||
| 122 | else if (ret != nb_samples) { | ||
| 123 | av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n", | ||
| 124 | ret); | ||
| 125 | return AVERROR_BUG; | ||
| 126 | } | ||
| 127 | |||
| 128 | if (celt_size) { | ||
| 129 | if (celt_size != nb_samples) { | ||
| 130 | av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n"); | ||
| 131 | return AVERROR_BUG; | ||
| 132 | } | ||
| 133 | av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples); | ||
| 134 | for (i = 0; i < s->output_channels; i++) { | ||
| 135 | s->fdsp->vector_fmac_scalar(s->out[i], | ||
| 136 | s->celt_output[i], 1.0, | ||
| 137 | nb_samples); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | if (s->redundancy_idx) { | ||
| 142 | for (i = 0; i < s->output_channels; i++) | ||
| 143 | opus_fade(s->out[i], s->out[i], | ||
| 144 | s->redundancy_output[i] + 120 + s->redundancy_idx, | ||
| 145 | ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx); | ||
| 146 | s->redundancy_idx = 0; | ||
| 147 | } | ||
| 148 | |||
| 149 | s->out[0] += nb_samples; | ||
| 150 | s->out[1] += nb_samples; | ||
| 151 | s->out_size -= nb_samples * sizeof(float); | ||
| 152 | |||
| 153 | return 0; | ||
| 154 | } | ||
| 155 | |||
| 156 | static int opus_init_resample(OpusStreamContext *s) | ||
| 157 | { | ||
| 158 | float delay[16] = { 0.0 }; | ||
| 159 | uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay }; | ||
| 160 | int ret; | ||
| 161 | |||
| 162 | av_opt_set_int(s->avr, "in_sample_rate", s->silk_samplerate, 0); | ||
| 163 | ret = avresample_open(s->avr); | ||
| 164 | if (ret < 0) { | ||
| 165 | av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n"); | ||
| 166 | return ret; | ||
| 167 | } | ||
| 168 | |||
| 169 | ret = avresample_convert(s->avr, NULL, 0, 0, delayptr, sizeof(delay), | ||
| 170 | silk_resample_delay[s->packet.bandwidth]); | ||
| 171 | if (ret < 0) { | ||
| 172 | av_log(s->avctx, AV_LOG_ERROR, | ||
| 173 | "Error feeding initial silence to the resampler.\n"); | ||
| 174 | return ret; | ||
| 175 | } | ||
| 176 | |||
| 177 | return 0; | ||
| 178 | } | ||
| 179 | |||
| 180 | static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size) | ||
| 181 | { | ||
| 182 | int ret; | ||
| 183 | enum OpusBandwidth bw = s->packet.bandwidth; | ||
| 184 | |||
| 185 | if (s->packet.mode == OPUS_MODE_SILK && | ||
| 186 | bw == OPUS_BANDWIDTH_MEDIUMBAND) | ||
| 187 | bw = OPUS_BANDWIDTH_WIDEBAND; | ||
| 188 | |||
| 189 | ret = opus_rc_init(&s->redundancy_rc, data, size); | ||
| 190 | if (ret < 0) | ||
| 191 | goto fail; | ||
| 192 | opus_raw_init(&s->redundancy_rc, data + size, size); | ||
| 193 | |||
| 194 | ret = ff_celt_decode_frame(s->celt, &s->redundancy_rc, | ||
| 195 | s->redundancy_output, | ||
| 196 | s->packet.stereo + 1, 240, | ||
| 197 | 0, celt_band_end[s->packet.bandwidth]); | ||
| 198 | if (ret < 0) | ||
| 199 | goto fail; | ||
| 200 | |||
| 201 | return 0; | ||
| 202 | fail: | ||
| 203 | av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n"); | ||
| 204 | return ret; | ||
| 205 | } | ||
| 206 | |||
| 207 | static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size) | ||
| 208 | { | ||
| 209 | int samples = s->packet.frame_duration; | ||
| 210 | int redundancy = 0; | ||
| 211 | int redundancy_size, redundancy_pos; | ||
| 212 | int ret, i, consumed; | ||
| 213 | int delayed_samples = s->delayed_samples; | ||
| 214 | |||
| 215 | ret = opus_rc_init(&s->rc, data, size); | ||
| 216 | if (ret < 0) | ||
| 217 | return ret; | ||
| 218 | |||
| 219 | /* decode the silk frame */ | ||
| 220 | if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) { | ||
| 221 | if (!avresample_is_open(s->avr)) { | ||
| 222 | ret = opus_init_resample(s); | ||
| 223 | if (ret < 0) | ||
| 224 | return ret; | ||
| 225 | } | ||
| 226 | |||
| 227 | samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output, | ||
| 228 | FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND), | ||
| 229 | s->packet.stereo + 1, | ||
| 230 | silk_frame_duration_ms[s->packet.config]); | ||
| 231 | if (samples < 0) { | ||
| 232 | av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n"); | ||
| 233 | return samples; | ||
| 234 | } | ||
| 235 | |||
| 236 | samples = avresample_convert(s->avr, (uint8_t**)s->out, s->out_size, | ||
| 237 | s->packet.frame_duration, | ||
| 238 | (uint8_t**)s->silk_output, | ||
| 239 | sizeof(s->silk_buf[0]), | ||
| 240 | samples); | ||
| 241 | if (samples < 0) { | ||
| 242 | av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n"); | ||
| 243 | return samples; | ||
| 244 | } | ||
| 245 | s->delayed_samples += s->packet.frame_duration - samples; | ||
| 246 | } else | ||
| 247 | ff_silk_flush(s->silk); | ||
| 248 | |||
| 249 | // decode redundancy information | ||
| 250 | consumed = opus_rc_tell(&s->rc); | ||
| 251 | if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8) | ||
| 252 | redundancy = opus_rc_p2model(&s->rc, 12); | ||
| 253 | else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8) | ||
| 254 | redundancy = 1; | ||
| 255 | |||
| 256 | if (redundancy) { | ||
| 257 | redundancy_pos = opus_rc_p2model(&s->rc, 1); | ||
| 258 | |||
| 259 | if (s->packet.mode == OPUS_MODE_HYBRID) | ||
| 260 | redundancy_size = opus_rc_unimodel(&s->rc, 256) + 2; | ||
| 261 | else | ||
| 262 | redundancy_size = size - (consumed + 7) / 8; | ||
| 263 | size -= redundancy_size; | ||
| 264 | if (size < 0) { | ||
| 265 | av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n"); | ||
| 266 | return AVERROR_INVALIDDATA; | ||
| 267 | } | ||
| 268 | |||
| 269 | if (redundancy_pos) { | ||
| 270 | ret = opus_decode_redundancy(s, data + size, redundancy_size); | ||
| 271 | if (ret < 0) | ||
| 272 | return ret; | ||
| 273 | ff_celt_flush(s->celt); | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | /* decode the CELT frame */ | ||
| 278 | if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) { | ||
| 279 | float *out_tmp[2] = { s->out[0], s->out[1] }; | ||
| 280 | float **dst = (s->packet.mode == OPUS_MODE_CELT) ? | ||
| 281 | out_tmp : s->celt_output; | ||
| 282 | int celt_output_samples = samples; | ||
| 283 | int delay_samples = av_audio_fifo_size(s->celt_delay); | ||
| 284 | |||
| 285 | if (delay_samples) { | ||
| 286 | if (s->packet.mode == OPUS_MODE_HYBRID) { | ||
| 287 | av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples); | ||
| 288 | |||
| 289 | for (i = 0; i < s->output_channels; i++) { | ||
| 290 | s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0, | ||
| 291 | delay_samples); | ||
| 292 | out_tmp[i] += delay_samples; | ||
| 293 | } | ||
| 294 | celt_output_samples -= delay_samples; | ||
| 295 | } else { | ||
| 296 | av_log(s->avctx, AV_LOG_WARNING, | ||
| 297 | "Spurious CELT delay samples present.\n"); | ||
| 298 | av_audio_fifo_drain(s->celt_delay, delay_samples); | ||
| 299 | if (s->avctx->err_recognition & AV_EF_EXPLODE) | ||
| 300 | return AVERROR_BUG; | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | opus_raw_init(&s->rc, data + size, size); | ||
| 305 | |||
| 306 | ret = ff_celt_decode_frame(s->celt, &s->rc, dst, | ||
| 307 | s->packet.stereo + 1, | ||
| 308 | s->packet.frame_duration, | ||
| 309 | (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0, | ||
| 310 | celt_band_end[s->packet.bandwidth]); | ||
| 311 | if (ret < 0) | ||
| 312 | return ret; | ||
| 313 | |||
| 314 | if (s->packet.mode == OPUS_MODE_HYBRID) { | ||
| 315 | int celt_delay = s->packet.frame_duration - celt_output_samples; | ||
| 316 | void *delaybuf[2] = { s->celt_output[0] + celt_output_samples, | ||
| 317 | s->celt_output[1] + celt_output_samples }; | ||
| 318 | |||
| 319 | for (i = 0; i < s->output_channels; i++) { | ||
| 320 | s->fdsp->vector_fmac_scalar(out_tmp[i], | ||
| 321 | s->celt_output[i], 1.0, | ||
| 322 | celt_output_samples); | ||
| 323 | } | ||
| 324 | |||
| 325 | ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay); | ||
| 326 | if (ret < 0) | ||
| 327 | return ret; | ||
| 328 | } | ||
| 329 | } else | ||
| 330 | ff_celt_flush(s->celt); | ||
| 331 | |||
| 332 | if (s->redundancy_idx) { | ||
| 333 | for (i = 0; i < s->output_channels; i++) | ||
| 334 | opus_fade(s->out[i], s->out[i], | ||
| 335 | s->redundancy_output[i] + 120 + s->redundancy_idx, | ||
| 336 | ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx); | ||
| 337 | s->redundancy_idx = 0; | ||
| 338 | } | ||
| 339 | if (redundancy) { | ||
| 340 | if (!redundancy_pos) { | ||
| 341 | ff_celt_flush(s->celt); | ||
| 342 | ret = opus_decode_redundancy(s, data + size, redundancy_size); | ||
| 343 | if (ret < 0) | ||
| 344 | return ret; | ||
| 345 | |||
| 346 | for (i = 0; i < s->output_channels; i++) { | ||
| 347 | opus_fade(s->out[i] + samples - 120 + delayed_samples, | ||
| 348 | s->out[i] + samples - 120 + delayed_samples, | ||
| 349 | s->redundancy_output[i] + 120, | ||
| 350 | ff_celt_window2, 120 - delayed_samples); | ||
| 351 | if (delayed_samples) | ||
| 352 | s->redundancy_idx = 120 - delayed_samples; | ||
| 353 | } | ||
| 354 | } else { | ||
| 355 | for (i = 0; i < s->output_channels; i++) { | ||
| 356 | memcpy(s->out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float)); | ||
| 357 | opus_fade(s->out[i] + 120 + delayed_samples, | ||
| 358 | s->redundancy_output[i] + 120, | ||
| 359 | s->out[i] + 120 + delayed_samples, | ||
| 360 | ff_celt_window2, 120); | ||
| 361 | } | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | return samples; | ||
| 366 | } | ||
| 367 | |||
| 368 | static int opus_decode_subpacket(OpusStreamContext *s, | ||
| 369 | const uint8_t *buf, int buf_size, | ||
| 370 | int nb_samples) | ||
| 371 | { | ||
| 372 | int output_samples = 0; | ||
| 373 | int flush_needed = 0; | ||
| 374 | int i, j, ret; | ||
| 375 | |||
| 376 | /* check if we need to flush the resampler */ | ||
| 377 | if (avresample_is_open(s->avr)) { | ||
| 378 | if (buf) { | ||
| 379 | int64_t cur_samplerate; | ||
| 380 | av_opt_get_int(s->avr, "in_sample_rate", 0, &cur_samplerate); | ||
| 381 | flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate); | ||
| 382 | } else { | ||
| 383 | flush_needed = !!s->delayed_samples; | ||
| 384 | } | ||
| 385 | } | ||
| 386 | |||
| 387 | if (!buf && !flush_needed) | ||
| 388 | return 0; | ||
| 389 | |||
| 390 | /* use dummy output buffers if the channel is not mapped to anything */ | ||
| 391 | if (!s->out[0] || | ||
| 392 | (s->output_channels == 2 && !s->out[1])) { | ||
| 393 | av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size, s->out_size); | ||
| 394 | if (!s->out_dummy) | ||
| 395 | return AVERROR(ENOMEM); | ||
| 396 | if (!s->out[0]) | ||
| 397 | s->out[0] = s->out_dummy; | ||
| 398 | if (!s->out[1]) | ||
| 399 | s->out[1] = s->out_dummy; | ||
| 400 | } | ||
| 401 | |||
| 402 | /* flush the resampler if necessary */ | ||
| 403 | if (flush_needed) { | ||
| 404 | ret = opus_flush_resample(s, s->delayed_samples); | ||
| 405 | if (ret < 0) { | ||
| 406 | av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n"); | ||
| 407 | return ret; | ||
| 408 | } | ||
| 409 | avresample_close(s->avr); | ||
| 410 | output_samples += s->delayed_samples; | ||
| 411 | s->delayed_samples = 0; | ||
| 412 | |||
| 413 | if (!buf) | ||
| 414 | goto finish; | ||
| 415 | } | ||
| 416 | |||
| 417 | /* decode all the frames in the packet */ | ||
| 418 | for (i = 0; i < s->packet.frame_count; i++) { | ||
| 419 | int size = s->packet.frame_size[i]; | ||
| 420 | int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size); | ||
| 421 | |||
| 422 | if (samples < 0) { | ||
| 423 | av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n"); | ||
| 424 | if (s->avctx->err_recognition & AV_EF_EXPLODE) | ||
| 425 | return samples; | ||
| 426 | |||
| 427 | for (j = 0; j < s->output_channels; j++) | ||
| 428 | memset(s->out[j], 0, s->packet.frame_duration * sizeof(float)); | ||
| 429 | samples = s->packet.frame_duration; | ||
| 430 | } | ||
| 431 | output_samples += samples; | ||
| 432 | |||
| 433 | for (j = 0; j < s->output_channels; j++) | ||
| 434 | s->out[j] += samples; | ||
| 435 | s->out_size -= samples * sizeof(float); | ||
| 436 | } | ||
| 437 | |||
| 438 | finish: | ||
| 439 | s->out[0] = s->out[1] = NULL; | ||
| 440 | s->out_size = 0; | ||
| 441 | |||
| 442 | return output_samples; | ||
| 443 | } | ||
| 444 | |||
| 445 | static int opus_decode_packet(AVCodecContext *avctx, void *data, | ||
| 446 | int *got_frame_ptr, AVPacket *avpkt) | ||
| 447 | { | ||
| 448 | OpusContext *c = avctx->priv_data; | ||
| 449 | AVFrame *frame = data; | ||
| 450 | const uint8_t *buf = avpkt->data; | ||
| 451 | int buf_size = avpkt->size; | ||
| 452 | int coded_samples = 0; | ||
| 453 | int decoded_samples = 0; | ||
| 454 | int i, ret; | ||
| 455 | |||
| 456 | /* decode the header of the first sub-packet to find out the sample count */ | ||
| 457 | if (buf) { | ||
| 458 | OpusPacket *pkt = &c->streams[0].packet; | ||
| 459 | ret = ff_opus_parse_packet(pkt, buf, buf_size, c->nb_streams > 1); | ||
| 460 | if (ret < 0) { | ||
| 461 | av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n"); | ||
| 462 | return ret; | ||
| 463 | } | ||
| 464 | coded_samples += pkt->frame_count * pkt->frame_duration; | ||
| 465 | c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config); | ||
| 466 | } | ||
| 467 | |||
| 468 | frame->nb_samples = coded_samples + c->streams[0].delayed_samples; | ||
| 469 | |||
| 470 | /* no input or buffered data => nothing to do */ | ||
| 471 | if (!frame->nb_samples) { | ||
| 472 | *got_frame_ptr = 0; | ||
| 473 | return 0; | ||
| 474 | } | ||
| 475 | |||
| 476 | /* setup the data buffers */ | ||
| 477 | ret = ff_get_buffer(avctx, frame, 0); | ||
| 478 | if (ret < 0) { | ||
| 479 | av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | ||
| 480 | return ret; | ||
| 481 | } | ||
| 482 | frame->nb_samples = 0; | ||
| 483 | |||
| 484 | for (i = 0; i < avctx->channels; i++) { | ||
| 485 | ChannelMap *map = &c->channel_maps[i]; | ||
| 486 | if (!map->copy) | ||
| 487 | c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i]; | ||
| 488 | } | ||
| 489 | |||
| 490 | for (i = 0; i < c->nb_streams; i++) | ||
| 491 | c->streams[i].out_size = frame->linesize[0]; | ||
| 492 | |||
| 493 | /* decode each sub-packet */ | ||
| 494 | for (i = 0; i < c->nb_streams; i++) { | ||
| 495 | OpusStreamContext *s = &c->streams[i]; | ||
| 496 | |||
| 497 | if (i && buf) { | ||
| 498 | ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->nb_streams - 1); | ||
| 499 | if (ret < 0) { | ||
| 500 | av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n"); | ||
| 501 | return ret; | ||
| 502 | } | ||
| 503 | s->silk_samplerate = get_silk_samplerate(s->packet.config); | ||
| 504 | } | ||
| 505 | |||
| 506 | ret = opus_decode_subpacket(&c->streams[i], buf, | ||
| 507 | s->packet.data_size, coded_samples); | ||
| 508 | if (ret < 0) | ||
| 509 | return ret; | ||
| 510 | if (decoded_samples && ret != decoded_samples) { | ||
| 511 | av_log(avctx, AV_LOG_ERROR, "Different numbers of decoded samples " | ||
| 512 | "in a multi-channel stream\n"); | ||
| 513 | return AVERROR_INVALIDDATA; | ||
| 514 | } | ||
| 515 | decoded_samples = ret; | ||
| 516 | buf += s->packet.packet_size; | ||
| 517 | buf_size -= s->packet.packet_size; | ||
| 518 | } | ||
| 519 | |||
| 520 | for (i = 0; i < avctx->channels; i++) { | ||
| 521 | ChannelMap *map = &c->channel_maps[i]; | ||
| 522 | |||
| 523 | /* handle copied channels */ | ||
| 524 | if (map->copy) { | ||
| 525 | memcpy(frame->extended_data[i], | ||
| 526 | frame->extended_data[map->copy_idx], | ||
| 527 | frame->linesize[0]); | ||
| 528 | } else if (map->silence) { | ||
| 529 | memset(frame->extended_data[i], 0, frame->linesize[0]); | ||
| 530 | } | ||
| 531 | |||
| 532 | if (c->gain_i) { | ||
| 533 | c->fdsp.vector_fmul_scalar((float*)frame->extended_data[i], | ||
| 534 | (float*)frame->extended_data[i], | ||
| 535 | c->gain, FFALIGN(decoded_samples, 8)); | ||
| 536 | } | ||
| 537 | } | ||
| 538 | |||
| 539 | frame->nb_samples = decoded_samples; | ||
| 540 | *got_frame_ptr = !!decoded_samples; | ||
| 541 | |||
| 542 | return avpkt->size; | ||
| 543 | } | ||
| 544 | |||
| 545 | static av_cold void opus_decode_flush(AVCodecContext *ctx) | ||
| 546 | { | ||
| 547 | OpusContext *c = ctx->priv_data; | ||
| 548 | int i; | ||
| 549 | |||
| 550 | for (i = 0; i < c->nb_streams; i++) { | ||
| 551 | OpusStreamContext *s = &c->streams[i]; | ||
| 552 | |||
| 553 | memset(&s->packet, 0, sizeof(s->packet)); | ||
| 554 | s->delayed_samples = 0; | ||
| 555 | |||
| 556 | if (s->celt_delay) | ||
| 557 | av_audio_fifo_drain(s->celt_delay, av_audio_fifo_size(s->celt_delay)); | ||
| 558 | avresample_close(s->avr); | ||
| 559 | |||
| 560 | ff_silk_flush(s->silk); | ||
| 561 | ff_celt_flush(s->celt); | ||
| 562 | } | ||
| 563 | } | ||
| 564 | |||
| 565 | static av_cold int opus_decode_close(AVCodecContext *avctx) | ||
| 566 | { | ||
| 567 | OpusContext *c = avctx->priv_data; | ||
| 568 | int i; | ||
| 569 | |||
| 570 | for (i = 0; i < c->nb_streams; i++) { | ||
| 571 | OpusStreamContext *s = &c->streams[i]; | ||
| 572 | |||
| 573 | ff_silk_free(&s->silk); | ||
| 574 | ff_celt_free(&s->celt); | ||
| 575 | |||
| 576 | av_freep(&s->out_dummy); | ||
| 577 | s->out_dummy_allocated_size = 0; | ||
| 578 | |||
| 579 | av_audio_fifo_free(s->celt_delay); | ||
| 580 | avresample_free(&s->avr); | ||
| 581 | } | ||
| 582 | |||
| 583 | av_freep(&c->streams); | ||
| 584 | c->nb_streams = 0; | ||
| 585 | |||
| 586 | av_freep(&c->channel_maps); | ||
| 587 | |||
| 588 | return 0; | ||
| 589 | } | ||
| 590 | |||
| 591 | static av_cold int opus_decode_init(AVCodecContext *avctx) | ||
| 592 | { | ||
| 593 | OpusContext *c = avctx->priv_data; | ||
| 594 | int ret, i, j; | ||
| 595 | |||
| 596 | avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; | ||
| 597 | avctx->sample_rate = 48000; | ||
| 598 | |||
| 599 | avpriv_float_dsp_init(&c->fdsp, 0); | ||
| 600 | |||
| 601 | /* find out the channel configuration */ | ||
| 602 | ret = ff_opus_parse_extradata(avctx, c); | ||
| 603 | if (ret < 0) | ||
| 604 | return ret; | ||
| 605 | |||
| 606 | /* allocate and init each independent decoder */ | ||
| 607 | c->streams = av_mallocz_array(c->nb_streams, sizeof(*c->streams)); | ||
| 608 | if (!c->streams) { | ||
| 609 | c->nb_streams = 0; | ||
| 610 | ret = AVERROR(ENOMEM); | ||
| 611 | goto fail; | ||
| 612 | } | ||
| 613 | |||
| 614 | for (i = 0; i < c->nb_streams; i++) { | ||
| 615 | OpusStreamContext *s = &c->streams[i]; | ||
| 616 | uint64_t layout; | ||
| 617 | |||
| 618 | s->output_channels = (i < c->nb_stereo_streams) ? 2 : 1; | ||
| 619 | |||
| 620 | s->avctx = avctx; | ||
| 621 | |||
| 622 | for (j = 0; j < s->output_channels; j++) { | ||
| 623 | s->silk_output[j] = s->silk_buf[j]; | ||
| 624 | s->celt_output[j] = s->celt_buf[j]; | ||
| 625 | s->redundancy_output[j] = s->redundancy_buf[j]; | ||
| 626 | } | ||
| 627 | |||
| 628 | s->fdsp = &c->fdsp; | ||
| 629 | |||
| 630 | s->avr = avresample_alloc_context(); | ||
| 631 | if (!s->avr) | ||
| 632 | goto fail; | ||
| 633 | |||
| 634 | layout = (s->output_channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO; | ||
| 635 | av_opt_set_int(s->avr, "in_sample_fmt", avctx->sample_fmt, 0); | ||
| 636 | av_opt_set_int(s->avr, "out_sample_fmt", avctx->sample_fmt, 0); | ||
| 637 | av_opt_set_int(s->avr, "in_channel_layout", layout, 0); | ||
| 638 | av_opt_set_int(s->avr, "out_channel_layout", layout, 0); | ||
| 639 | av_opt_set_int(s->avr, "out_sample_rate", avctx->sample_rate, 0); | ||
| 640 | |||
| 641 | ret = ff_silk_init(avctx, &s->silk, s->output_channels); | ||
| 642 | if (ret < 0) | ||
| 643 | goto fail; | ||
| 644 | |||
| 645 | ret = ff_celt_init(avctx, &s->celt, s->output_channels); | ||
| 646 | if (ret < 0) | ||
| 647 | goto fail; | ||
| 648 | |||
| 649 | s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt, | ||
| 650 | s->output_channels, 1024); | ||
| 651 | if (!s->celt_delay) { | ||
| 652 | ret = AVERROR(ENOMEM); | ||
| 653 | goto fail; | ||
| 654 | } | ||
| 655 | } | ||
| 656 | |||
| 657 | return 0; | ||
| 658 | fail: | ||
| 659 | opus_decode_close(avctx); | ||
| 660 | return ret; | ||
| 661 | } | ||
| 662 | |||
| 663 | AVCodec ff_opus_decoder = { | ||
| 664 | .name = "opus", | ||
| 665 | .long_name = NULL_IF_CONFIG_SMALL("Opus"), | ||
| 666 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 667 | .id = AV_CODEC_ID_OPUS, | ||
| 668 | .priv_data_size = sizeof(OpusContext), | ||
| 669 | .init = opus_decode_init, | ||
| 670 | .close = opus_decode_close, | ||
| 671 | .decode = opus_decode_packet, | ||
| 672 | .flush = opus_decode_flush, | ||
| 673 | .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY, | ||
| 674 | }; | ||
| @@ -29,8 +29,8 @@ · 28 unmodified lines | |||
| 29 | #include "libavutil/version.h" | 29 | #include "libavutil/version.h" |
| 30 | 30 | ||
| 31 | #define LIBAVCODEC_VERSION_MAJOR 55 | 31 | #define LIBAVCODEC_VERSION_MAJOR 55 |
| 32 | #define LIBAVCODEC_VERSION_MINOR 61 | 32 | #define LIBAVCODEC_VERSION_MINOR 62 |
| 33 | #define LIBAVCODEC_VERSION_MICRO 101 | 33 | #define LIBAVCODEC_VERSION_MICRO 100 |
| 34 | 34 | ||
| 35 | #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ | 35 | #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ |
| 36 | LIBAVCODEC_VERSION_MINOR, \ | 36 | LIBAVCODEC_VERSION_MINOR, \ |
| @@ -119,6 +119,7 @@ include $(SRC_PATH)/tests/fate/microsoft.mak · 118 unmodified lines | |||
| 119 | include $(SRC_PATH)/tests/fate/monkeysaudio.mak | 119 | include $(SRC_PATH)/tests/fate/monkeysaudio.mak |
| 120 | include $(SRC_PATH)/tests/fate/mp3.mak | 120 | include $(SRC_PATH)/tests/fate/mp3.mak |
| 121 | include $(SRC_PATH)/tests/fate/mpc.mak | 121 | include $(SRC_PATH)/tests/fate/mpc.mak |
| 122 | include $(SRC_PATH)/tests/fate/opus.mak | ||
| 122 | include $(SRC_PATH)/tests/fate/pcm.mak | 123 | include $(SRC_PATH)/tests/fate/pcm.mak |
| 123 | include $(SRC_PATH)/tests/fate/probe.mak | 124 | include $(SRC_PATH)/tests/fate/probe.mak |
| 124 | include $(SRC_PATH)/tests/fate/prores.mak | 125 | include $(SRC_PATH)/tests/fate/prores.mak |
| @@ -0,0 +1,39 @@ | |||
| 1 | # The samples were produced by simply rewrapping the official test vectors from | ||
| 2 | # their custom format into Matroska. | ||
| 3 | # The reference files were created with our decoder and tested against the | ||
| 4 | # libopus output with the official opus_compare tool. We cannot use libopus | ||
| 5 | # output as reference directly, because the use of different resamplers would | ||
| 6 | # require too high fuzz values, which can hide bugs. | ||
| 7 | # Before adding new tests here, always make sure they pass opus_compare. | ||
| 8 | |||
| 9 | OPUS_CELT_SAMPLES = $(addprefix testvector, 01 07 11) tron.6ch.tinypkts | ||
| 10 | OPUS_HYBRID_SAMPLES = $(addprefix testvector, 05 06) | ||
| 11 | OPUS_SILK_SAMPLES = $(addprefix testvector, 02 03 04) | ||
| 12 | OPUS_SAMPLES = $(addprefix testvector, 08 09 10 12) | ||
| 13 | |||
| 14 | define FATE_OPUS_TEST | ||
| 15 | FATE_OPUS += fate-opus-$(1) | ||
| 16 | FATE_OPUS$(2) += fate-opus-$(1) | ||
| 17 | fate-opus-$(1): CMD = ffmpeg -i $(TARGET_SAMPLES)/opus/$(1).mka -f f32le - | ||
| 18 | fate-opus-$(1): REF = $(TARGET_SAMPLES)/opus/$(1).f32 | ||
| 19 | endef | ||
| 20 | |||
| 21 | $(foreach N,$(OPUS_CELT_SAMPLES), $(eval $(call FATE_OPUS_TEST,$(N),_CELT))) | ||
| 22 | $(foreach N,$(OPUS_HYBRID_SAMPLES),$(eval $(call FATE_OPUS_TEST,$(N),_HYBRID))) | ||
| 23 | $(foreach N,$(OPUS_SILK_SAMPLES), $(eval $(call FATE_OPUS_TEST,$(N),_SILK))) | ||
| 24 | $(foreach N,$(OPUS_SAMPLES), $(eval $(call FATE_OPUS_TEST,$(N),))) | ||
| 25 | |||
| 26 | FATE_OPUS := $(sort $(FATE_OPUS)) | ||
| 27 | |||
| 28 | $(FATE_OPUS): CMP = stddev | ||
| 29 | $(FATE_OPUS): CMP_UNIT = f32 | ||
| 30 | $(FATE_OPUS): FUZZ = 3 | ||
| 31 | |||
| 32 | $(FATE_OPUS_CELT): CMP = oneoff | ||
| 33 | $(FATE_OPUS_CELT): FUZZ = 5 | ||
| 34 | |||
| 35 | #FATE_SAMPLES_AVCONV-$(call DEMDEC, MATROSKA, OPUS) += $(FATE_OPUS) | ||
| 36 | fate-opus-celt: $(FATE_OPUS_CELT) | ||
| 37 | fate-opus-hybrid: $(FATE_OPUS_HYBRID) | ||
| 38 | fate-opus-silk: $(FATE_OPUS_SILK) | ||
| 39 | fate-opus: $(FATE_OPUS) | ||