OLD | NEW |
1 /* Copyright (c) 2011 Xiph.Org Foundation, Skype Limited | 1 /* Copyright (c) 2011 Xiph.Org Foundation, Skype Limited |
2 Written by Jean-Marc Valin and Koen Vos */ | 2 Written by Jean-Marc Valin and Koen Vos */ |
3 /* | 3 /* |
4 Redistribution and use in source and binary forms, with or without | 4 Redistribution and use in source and binary forms, with or without |
5 modification, are permitted provided that the following conditions | 5 modification, are permitted provided that the following conditions |
6 are met: | 6 are met: |
7 | 7 |
8 - Redistributions of source code must retain the above copyright | 8 - Redistributions of source code must retain the above copyright |
9 notice, this list of conditions and the following disclaimer. | 9 notice, this list of conditions and the following disclaimer. |
10 | 10 |
(...skipping 21 matching lines...) Expand all Loading... |
32 #include "opus.h" | 32 #include "opus.h" |
33 #include "opus_private.h" | 33 #include "opus_private.h" |
34 | 34 |
35 #ifndef DISABLE_FLOAT_API | 35 #ifndef DISABLE_FLOAT_API |
36 OPUS_EXPORT void opus_pcm_soft_clip(float *_x, int N, int C, float *declip_mem) | 36 OPUS_EXPORT void opus_pcm_soft_clip(float *_x, int N, int C, float *declip_mem) |
37 { | 37 { |
38 int c; | 38 int c; |
39 int i; | 39 int i; |
40 float *x; | 40 float *x; |
41 | 41 |
| 42 if (C<1 || N<1 || !_x || !declip_mem) return; |
| 43 |
42 /* First thing: saturate everything to +/- 2 which is the highest level our | 44 /* First thing: saturate everything to +/- 2 which is the highest level our |
43 non-linearity can handle. At the point where the signal reaches +/-2, | 45 non-linearity can handle. At the point where the signal reaches +/-2, |
44 the derivative will be zero anyway, so this doesn't introduce any | 46 the derivative will be zero anyway, so this doesn't introduce any |
45 discontinuity in the derivative. */ | 47 discontinuity in the derivative. */ |
46 for (i=0;i<N*C;i++) | 48 for (i=0;i<N*C;i++) |
47 _x[i] = MAX16(-2.f, MIN16(2.f, _x[i])); | 49 _x[i] = MAX16(-2.f, MIN16(2.f, _x[i])); |
48 for (c=0;c<C;c++) | 50 for (c=0;c<C;c++) |
49 { | 51 { |
50 float a; | 52 float a; |
51 float x0; | 53 float x0; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 { | 139 { |
138 data[0] = size; | 140 data[0] = size; |
139 return 1; | 141 return 1; |
140 } else { | 142 } else { |
141 data[0] = 252+(size&0x3); | 143 data[0] = 252+(size&0x3); |
142 data[1] = (size-(int)data[0])>>2; | 144 data[1] = (size-(int)data[0])>>2; |
143 return 2; | 145 return 2; |
144 } | 146 } |
145 } | 147 } |
146 | 148 |
| 149 static int parse_size(const unsigned char *data, opus_int32 len, opus_int16 *siz
e) |
| 150 { |
| 151 if (len<1) |
| 152 { |
| 153 *size = -1; |
| 154 return -1; |
| 155 } else if (data[0]<252) |
| 156 { |
| 157 *size = data[0]; |
| 158 return 1; |
| 159 } else if (len<2) |
| 160 { |
| 161 *size = -1; |
| 162 return -1; |
| 163 } else { |
| 164 *size = 4*data[1] + data[0]; |
| 165 return 2; |
| 166 } |
| 167 } |
| 168 |
| 169 int opus_packet_parse_impl(const unsigned char *data, opus_int32 len, |
| 170 int self_delimited, unsigned char *out_toc, |
| 171 const unsigned char *frames[48], opus_int16 size[48], |
| 172 int *payload_offset, opus_int32 *packet_offset) |
| 173 { |
| 174 int i, bytes; |
| 175 int count; |
| 176 int cbr; |
| 177 unsigned char ch, toc; |
| 178 int framesize; |
| 179 opus_int32 last_size; |
| 180 opus_int32 pad = 0; |
| 181 const unsigned char *data0 = data; |
| 182 |
| 183 if (size==NULL) |
| 184 return OPUS_BAD_ARG; |
| 185 |
| 186 framesize = opus_packet_get_samples_per_frame(data, 48000); |
| 187 |
| 188 cbr = 0; |
| 189 toc = *data++; |
| 190 len--; |
| 191 last_size = len; |
| 192 switch (toc&0x3) |
| 193 { |
| 194 /* One frame */ |
| 195 case 0: |
| 196 count=1; |
| 197 break; |
| 198 /* Two CBR frames */ |
| 199 case 1: |
| 200 count=2; |
| 201 cbr = 1; |
| 202 if (!self_delimited) |
| 203 { |
| 204 if (len&0x1) |
| 205 return OPUS_INVALID_PACKET; |
| 206 last_size = len/2; |
| 207 /* If last_size doesn't fit in size[0], we'll catch it later */ |
| 208 size[0] = (opus_int16)last_size; |
| 209 } |
| 210 break; |
| 211 /* Two VBR frames */ |
| 212 case 2: |
| 213 count = 2; |
| 214 bytes = parse_size(data, len, size); |
| 215 len -= bytes; |
| 216 if (size[0]<0 || size[0] > len) |
| 217 return OPUS_INVALID_PACKET; |
| 218 data += bytes; |
| 219 last_size = len-size[0]; |
| 220 break; |
| 221 /* Multiple CBR/VBR frames (from 0 to 120 ms) */ |
| 222 default: /*case 3:*/ |
| 223 if (len<1) |
| 224 return OPUS_INVALID_PACKET; |
| 225 /* Number of frames encoded in bits 0 to 5 */ |
| 226 ch = *data++; |
| 227 count = ch&0x3F; |
| 228 if (count <= 0 || framesize*count > 5760) |
| 229 return OPUS_INVALID_PACKET; |
| 230 len--; |
| 231 /* Padding flag is bit 6 */ |
| 232 if (ch&0x40) |
| 233 { |
| 234 int p; |
| 235 do { |
| 236 int tmp; |
| 237 if (len<=0) |
| 238 return OPUS_INVALID_PACKET; |
| 239 p = *data++; |
| 240 len--; |
| 241 tmp = p==255 ? 254: p; |
| 242 len -= tmp; |
| 243 pad += tmp; |
| 244 } while (p==255); |
| 245 } |
| 246 if (len<0) |
| 247 return OPUS_INVALID_PACKET; |
| 248 /* VBR flag is bit 7 */ |
| 249 cbr = !(ch&0x80); |
| 250 if (!cbr) |
| 251 { |
| 252 /* VBR case */ |
| 253 last_size = len; |
| 254 for (i=0;i<count-1;i++) |
| 255 { |
| 256 bytes = parse_size(data, len, size+i); |
| 257 len -= bytes; |
| 258 if (size[i]<0 || size[i] > len) |
| 259 return OPUS_INVALID_PACKET; |
| 260 data += bytes; |
| 261 last_size -= bytes+size[i]; |
| 262 } |
| 263 if (last_size<0) |
| 264 return OPUS_INVALID_PACKET; |
| 265 } else if (!self_delimited) |
| 266 { |
| 267 /* CBR case */ |
| 268 last_size = len/count; |
| 269 if (last_size*count!=len) |
| 270 return OPUS_INVALID_PACKET; |
| 271 for (i=0;i<count-1;i++) |
| 272 size[i] = (opus_int16)last_size; |
| 273 } |
| 274 break; |
| 275 } |
| 276 /* Self-delimited framing has an extra size for the last frame. */ |
| 277 if (self_delimited) |
| 278 { |
| 279 bytes = parse_size(data, len, size+count-1); |
| 280 len -= bytes; |
| 281 if (size[count-1]<0 || size[count-1] > len) |
| 282 return OPUS_INVALID_PACKET; |
| 283 data += bytes; |
| 284 /* For CBR packets, apply the size to all the frames. */ |
| 285 if (cbr) |
| 286 { |
| 287 if (size[count-1]*count > len) |
| 288 return OPUS_INVALID_PACKET; |
| 289 for (i=0;i<count-1;i++) |
| 290 size[i] = size[count-1]; |
| 291 } else if (bytes+size[count-1] > last_size) |
| 292 return OPUS_INVALID_PACKET; |
| 293 } else |
| 294 { |
| 295 /* Because it's not encoded explicitly, it's possible the size of the |
| 296 last packet (or all the packets, for the CBR case) is larger than |
| 297 1275. Reject them here.*/ |
| 298 if (last_size > 1275) |
| 299 return OPUS_INVALID_PACKET; |
| 300 size[count-1] = (opus_int16)last_size; |
| 301 } |
| 302 |
| 303 if (payload_offset) |
| 304 *payload_offset = (int)(data-data0); |
| 305 |
| 306 for (i=0;i<count;i++) |
| 307 { |
| 308 if (frames) |
| 309 frames[i] = data; |
| 310 data += size[i]; |
| 311 } |
| 312 |
| 313 if (packet_offset) |
| 314 *packet_offset = pad+(opus_int32)(data-data0); |
| 315 |
| 316 if (out_toc) |
| 317 *out_toc = toc; |
| 318 |
| 319 return count; |
| 320 } |
| 321 |
| 322 int opus_packet_parse(const unsigned char *data, opus_int32 len, |
| 323 unsigned char *out_toc, const unsigned char *frames[48], |
| 324 opus_int16 size[48], int *payload_offset) |
| 325 { |
| 326 return opus_packet_parse_impl(data, len, 0, out_toc, |
| 327 frames, size, payload_offset, NULL); |
| 328 } |
| 329 |
OLD | NEW |