Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* Copyright (c) 2011 Xiph.Org Foundation, Skype Limited |
michael@0 | 2 | Written by Jean-Marc Valin and Koen Vos */ |
michael@0 | 3 | /* |
michael@0 | 4 | Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | modification, are permitted provided that the following conditions |
michael@0 | 6 | are met: |
michael@0 | 7 | |
michael@0 | 8 | - Redistributions of source code must retain the above copyright |
michael@0 | 9 | notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | |
michael@0 | 11 | - Redistributions in binary form must reproduce the above copyright |
michael@0 | 12 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 13 | documentation and/or other materials provided with the distribution. |
michael@0 | 14 | |
michael@0 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 16 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 17 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 18 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
michael@0 | 19 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 20 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
michael@0 | 23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
michael@0 | 24 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@0 | 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | #ifdef HAVE_CONFIG_H |
michael@0 | 29 | #include "config.h" |
michael@0 | 30 | #endif |
michael@0 | 31 | |
michael@0 | 32 | #include "opus.h" |
michael@0 | 33 | #include "opus_private.h" |
michael@0 | 34 | |
michael@0 | 35 | #ifndef DISABLE_FLOAT_API |
michael@0 | 36 | OPUS_EXPORT void opus_pcm_soft_clip(float *_x, int N, int C, float *declip_mem) |
michael@0 | 37 | { |
michael@0 | 38 | int c; |
michael@0 | 39 | int i; |
michael@0 | 40 | float *x; |
michael@0 | 41 | |
michael@0 | 42 | if (C<1 || N<1 || !_x || !declip_mem) return; |
michael@0 | 43 | |
michael@0 | 44 | /* First thing: saturate everything to +/- 2 which is the highest level our |
michael@0 | 45 | non-linearity can handle. At the point where the signal reaches +/-2, |
michael@0 | 46 | the derivative will be zero anyway, so this doesn't introduce any |
michael@0 | 47 | discontinuity in the derivative. */ |
michael@0 | 48 | for (i=0;i<N*C;i++) |
michael@0 | 49 | _x[i] = MAX16(-2.f, MIN16(2.f, _x[i])); |
michael@0 | 50 | for (c=0;c<C;c++) |
michael@0 | 51 | { |
michael@0 | 52 | float a; |
michael@0 | 53 | float x0; |
michael@0 | 54 | int curr; |
michael@0 | 55 | |
michael@0 | 56 | x = _x+c; |
michael@0 | 57 | a = declip_mem[c]; |
michael@0 | 58 | /* Continue applying the non-linearity from the previous frame to avoid |
michael@0 | 59 | any discontinuity. */ |
michael@0 | 60 | for (i=0;i<N;i++) |
michael@0 | 61 | { |
michael@0 | 62 | if (x[i*C]*a>=0) |
michael@0 | 63 | break; |
michael@0 | 64 | x[i*C] = x[i*C]+a*x[i*C]*x[i*C]; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | curr=0; |
michael@0 | 68 | x0 = x[0]; |
michael@0 | 69 | while(1) |
michael@0 | 70 | { |
michael@0 | 71 | int start, end; |
michael@0 | 72 | float maxval; |
michael@0 | 73 | int special=0; |
michael@0 | 74 | int peak_pos; |
michael@0 | 75 | for (i=curr;i<N;i++) |
michael@0 | 76 | { |
michael@0 | 77 | if (x[i*C]>1 || x[i*C]<-1) |
michael@0 | 78 | break; |
michael@0 | 79 | } |
michael@0 | 80 | if (i==N) |
michael@0 | 81 | { |
michael@0 | 82 | a=0; |
michael@0 | 83 | break; |
michael@0 | 84 | } |
michael@0 | 85 | peak_pos = i; |
michael@0 | 86 | start=end=i; |
michael@0 | 87 | maxval=ABS16(x[i*C]); |
michael@0 | 88 | /* Look for first zero crossing before clipping */ |
michael@0 | 89 | while (start>0 && x[i*C]*x[(start-1)*C]>=0) |
michael@0 | 90 | start--; |
michael@0 | 91 | /* Look for first zero crossing after clipping */ |
michael@0 | 92 | while (end<N && x[i*C]*x[end*C]>=0) |
michael@0 | 93 | { |
michael@0 | 94 | /* Look for other peaks until the next zero-crossing. */ |
michael@0 | 95 | if (ABS16(x[end*C])>maxval) |
michael@0 | 96 | { |
michael@0 | 97 | maxval = ABS16(x[end*C]); |
michael@0 | 98 | peak_pos = end; |
michael@0 | 99 | } |
michael@0 | 100 | end++; |
michael@0 | 101 | } |
michael@0 | 102 | /* Detect the special case where we clip before the first zero crossing */ |
michael@0 | 103 | special = (start==0 && x[i*C]*x[0]>=0); |
michael@0 | 104 | |
michael@0 | 105 | /* Compute a such that maxval + a*maxval^2 = 1 */ |
michael@0 | 106 | a=(maxval-1)/(maxval*maxval); |
michael@0 | 107 | if (x[i*C]>0) |
michael@0 | 108 | a = -a; |
michael@0 | 109 | /* Apply soft clipping */ |
michael@0 | 110 | for (i=start;i<end;i++) |
michael@0 | 111 | x[i*C] = x[i*C]+a*x[i*C]*x[i*C]; |
michael@0 | 112 | |
michael@0 | 113 | if (special && peak_pos>=2) |
michael@0 | 114 | { |
michael@0 | 115 | /* Add a linear ramp from the first sample to the signal peak. |
michael@0 | 116 | This avoids a discontinuity at the beginning of the frame. */ |
michael@0 | 117 | float delta; |
michael@0 | 118 | float offset = x0-x[0]; |
michael@0 | 119 | delta = offset / peak_pos; |
michael@0 | 120 | for (i=curr;i<peak_pos;i++) |
michael@0 | 121 | { |
michael@0 | 122 | offset -= delta; |
michael@0 | 123 | x[i*C] += offset; |
michael@0 | 124 | x[i*C] = MAX16(-1.f, MIN16(1.f, x[i*C])); |
michael@0 | 125 | } |
michael@0 | 126 | } |
michael@0 | 127 | curr = end; |
michael@0 | 128 | if (curr==N) |
michael@0 | 129 | break; |
michael@0 | 130 | } |
michael@0 | 131 | declip_mem[c] = a; |
michael@0 | 132 | } |
michael@0 | 133 | } |
michael@0 | 134 | #endif |
michael@0 | 135 | |
michael@0 | 136 | int encode_size(int size, unsigned char *data) |
michael@0 | 137 | { |
michael@0 | 138 | if (size < 252) |
michael@0 | 139 | { |
michael@0 | 140 | data[0] = size; |
michael@0 | 141 | return 1; |
michael@0 | 142 | } else { |
michael@0 | 143 | data[0] = 252+(size&0x3); |
michael@0 | 144 | data[1] = (size-(int)data[0])>>2; |
michael@0 | 145 | return 2; |
michael@0 | 146 | } |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | static int parse_size(const unsigned char *data, opus_int32 len, opus_int16 *size) |
michael@0 | 150 | { |
michael@0 | 151 | if (len<1) |
michael@0 | 152 | { |
michael@0 | 153 | *size = -1; |
michael@0 | 154 | return -1; |
michael@0 | 155 | } else if (data[0]<252) |
michael@0 | 156 | { |
michael@0 | 157 | *size = data[0]; |
michael@0 | 158 | return 1; |
michael@0 | 159 | } else if (len<2) |
michael@0 | 160 | { |
michael@0 | 161 | *size = -1; |
michael@0 | 162 | return -1; |
michael@0 | 163 | } else { |
michael@0 | 164 | *size = 4*data[1] + data[0]; |
michael@0 | 165 | return 2; |
michael@0 | 166 | } |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | int opus_packet_parse_impl(const unsigned char *data, opus_int32 len, |
michael@0 | 170 | int self_delimited, unsigned char *out_toc, |
michael@0 | 171 | const unsigned char *frames[48], opus_int16 size[48], |
michael@0 | 172 | int *payload_offset, opus_int32 *packet_offset) |
michael@0 | 173 | { |
michael@0 | 174 | int i, bytes; |
michael@0 | 175 | int count; |
michael@0 | 176 | int cbr; |
michael@0 | 177 | unsigned char ch, toc; |
michael@0 | 178 | int framesize; |
michael@0 | 179 | opus_int32 last_size; |
michael@0 | 180 | opus_int32 pad = 0; |
michael@0 | 181 | const unsigned char *data0 = data; |
michael@0 | 182 | |
michael@0 | 183 | if (size==NULL) |
michael@0 | 184 | return OPUS_BAD_ARG; |
michael@0 | 185 | |
michael@0 | 186 | framesize = opus_packet_get_samples_per_frame(data, 48000); |
michael@0 | 187 | |
michael@0 | 188 | cbr = 0; |
michael@0 | 189 | toc = *data++; |
michael@0 | 190 | len--; |
michael@0 | 191 | last_size = len; |
michael@0 | 192 | switch (toc&0x3) |
michael@0 | 193 | { |
michael@0 | 194 | /* One frame */ |
michael@0 | 195 | case 0: |
michael@0 | 196 | count=1; |
michael@0 | 197 | break; |
michael@0 | 198 | /* Two CBR frames */ |
michael@0 | 199 | case 1: |
michael@0 | 200 | count=2; |
michael@0 | 201 | cbr = 1; |
michael@0 | 202 | if (!self_delimited) |
michael@0 | 203 | { |
michael@0 | 204 | if (len&0x1) |
michael@0 | 205 | return OPUS_INVALID_PACKET; |
michael@0 | 206 | last_size = len/2; |
michael@0 | 207 | /* If last_size doesn't fit in size[0], we'll catch it later */ |
michael@0 | 208 | size[0] = (opus_int16)last_size; |
michael@0 | 209 | } |
michael@0 | 210 | break; |
michael@0 | 211 | /* Two VBR frames */ |
michael@0 | 212 | case 2: |
michael@0 | 213 | count = 2; |
michael@0 | 214 | bytes = parse_size(data, len, size); |
michael@0 | 215 | len -= bytes; |
michael@0 | 216 | if (size[0]<0 || size[0] > len) |
michael@0 | 217 | return OPUS_INVALID_PACKET; |
michael@0 | 218 | data += bytes; |
michael@0 | 219 | last_size = len-size[0]; |
michael@0 | 220 | break; |
michael@0 | 221 | /* Multiple CBR/VBR frames (from 0 to 120 ms) */ |
michael@0 | 222 | default: /*case 3:*/ |
michael@0 | 223 | if (len<1) |
michael@0 | 224 | return OPUS_INVALID_PACKET; |
michael@0 | 225 | /* Number of frames encoded in bits 0 to 5 */ |
michael@0 | 226 | ch = *data++; |
michael@0 | 227 | count = ch&0x3F; |
michael@0 | 228 | if (count <= 0 || framesize*count > 5760) |
michael@0 | 229 | return OPUS_INVALID_PACKET; |
michael@0 | 230 | len--; |
michael@0 | 231 | /* Padding flag is bit 6 */ |
michael@0 | 232 | if (ch&0x40) |
michael@0 | 233 | { |
michael@0 | 234 | int p; |
michael@0 | 235 | do { |
michael@0 | 236 | int tmp; |
michael@0 | 237 | if (len<=0) |
michael@0 | 238 | return OPUS_INVALID_PACKET; |
michael@0 | 239 | p = *data++; |
michael@0 | 240 | len--; |
michael@0 | 241 | tmp = p==255 ? 254: p; |
michael@0 | 242 | len -= tmp; |
michael@0 | 243 | pad += tmp; |
michael@0 | 244 | } while (p==255); |
michael@0 | 245 | } |
michael@0 | 246 | if (len<0) |
michael@0 | 247 | return OPUS_INVALID_PACKET; |
michael@0 | 248 | /* VBR flag is bit 7 */ |
michael@0 | 249 | cbr = !(ch&0x80); |
michael@0 | 250 | if (!cbr) |
michael@0 | 251 | { |
michael@0 | 252 | /* VBR case */ |
michael@0 | 253 | last_size = len; |
michael@0 | 254 | for (i=0;i<count-1;i++) |
michael@0 | 255 | { |
michael@0 | 256 | bytes = parse_size(data, len, size+i); |
michael@0 | 257 | len -= bytes; |
michael@0 | 258 | if (size[i]<0 || size[i] > len) |
michael@0 | 259 | return OPUS_INVALID_PACKET; |
michael@0 | 260 | data += bytes; |
michael@0 | 261 | last_size -= bytes+size[i]; |
michael@0 | 262 | } |
michael@0 | 263 | if (last_size<0) |
michael@0 | 264 | return OPUS_INVALID_PACKET; |
michael@0 | 265 | } else if (!self_delimited) |
michael@0 | 266 | { |
michael@0 | 267 | /* CBR case */ |
michael@0 | 268 | last_size = len/count; |
michael@0 | 269 | if (last_size*count!=len) |
michael@0 | 270 | return OPUS_INVALID_PACKET; |
michael@0 | 271 | for (i=0;i<count-1;i++) |
michael@0 | 272 | size[i] = (opus_int16)last_size; |
michael@0 | 273 | } |
michael@0 | 274 | break; |
michael@0 | 275 | } |
michael@0 | 276 | /* Self-delimited framing has an extra size for the last frame. */ |
michael@0 | 277 | if (self_delimited) |
michael@0 | 278 | { |
michael@0 | 279 | bytes = parse_size(data, len, size+count-1); |
michael@0 | 280 | len -= bytes; |
michael@0 | 281 | if (size[count-1]<0 || size[count-1] > len) |
michael@0 | 282 | return OPUS_INVALID_PACKET; |
michael@0 | 283 | data += bytes; |
michael@0 | 284 | /* For CBR packets, apply the size to all the frames. */ |
michael@0 | 285 | if (cbr) |
michael@0 | 286 | { |
michael@0 | 287 | if (size[count-1]*count > len) |
michael@0 | 288 | return OPUS_INVALID_PACKET; |
michael@0 | 289 | for (i=0;i<count-1;i++) |
michael@0 | 290 | size[i] = size[count-1]; |
michael@0 | 291 | } else if (bytes+size[count-1] > last_size) |
michael@0 | 292 | return OPUS_INVALID_PACKET; |
michael@0 | 293 | } else |
michael@0 | 294 | { |
michael@0 | 295 | /* Because it's not encoded explicitly, it's possible the size of the |
michael@0 | 296 | last packet (or all the packets, for the CBR case) is larger than |
michael@0 | 297 | 1275. Reject them here.*/ |
michael@0 | 298 | if (last_size > 1275) |
michael@0 | 299 | return OPUS_INVALID_PACKET; |
michael@0 | 300 | size[count-1] = (opus_int16)last_size; |
michael@0 | 301 | } |
michael@0 | 302 | |
michael@0 | 303 | if (payload_offset) |
michael@0 | 304 | *payload_offset = (int)(data-data0); |
michael@0 | 305 | |
michael@0 | 306 | for (i=0;i<count;i++) |
michael@0 | 307 | { |
michael@0 | 308 | if (frames) |
michael@0 | 309 | frames[i] = data; |
michael@0 | 310 | data += size[i]; |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | if (packet_offset) |
michael@0 | 314 | *packet_offset = pad+(opus_int32)(data-data0); |
michael@0 | 315 | |
michael@0 | 316 | if (out_toc) |
michael@0 | 317 | *out_toc = toc; |
michael@0 | 318 | |
michael@0 | 319 | return count; |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | int opus_packet_parse(const unsigned char *data, opus_int32 len, |
michael@0 | 323 | unsigned char *out_toc, const unsigned char *frames[48], |
michael@0 | 324 | opus_int16 size[48], int *payload_offset) |
michael@0 | 325 | { |
michael@0 | 326 | return opus_packet_parse_impl(data, len, 0, out_toc, |
michael@0 | 327 | frames, size, payload_offset, NULL); |
michael@0 | 328 | } |
michael@0 | 329 |