opus.c 9.6 KB

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