repacketizer.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* Copyright (c) 2011 Xiph.Org Foundation
  2. Written by Jean-Marc Valin */
  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. #include "os_support.h"
  30. int opus_repacketizer_get_size(void)
  31. {
  32. return sizeof(OpusRepacketizer);
  33. }
  34. OpusRepacketizer *opus_repacketizer_init(OpusRepacketizer *rp)
  35. {
  36. rp->nb_frames = 0;
  37. return rp;
  38. }
  39. OpusRepacketizer *opus_repacketizer_create(void)
  40. {
  41. OpusRepacketizer *rp;
  42. rp=(OpusRepacketizer *)opus_alloc(opus_repacketizer_get_size());
  43. if(rp==NULL)return NULL;
  44. return opus_repacketizer_init(rp);
  45. }
  46. void opus_repacketizer_destroy(OpusRepacketizer *rp)
  47. {
  48. opus_free(rp);
  49. }
  50. static int opus_repacketizer_cat_impl(OpusRepacketizer *rp, const unsigned char *data, opus_int32 len, int self_delimited)
  51. {
  52. unsigned char tmp_toc;
  53. int curr_nb_frames,ret;
  54. /* Set of check ToC */
  55. if (len<1) return OPUS_INVALID_PACKET;
  56. if (rp->nb_frames == 0)
  57. {
  58. rp->toc = data[0];
  59. rp->framesize = opus_packet_get_samples_per_frame(data, 8000);
  60. } else if ((rp->toc&0xFC) != (data[0]&0xFC))
  61. {
  62. /*fprintf(stderr, "toc mismatch: 0x%x vs 0x%x\n", rp->toc, data[0]);*/
  63. return OPUS_INVALID_PACKET;
  64. }
  65. curr_nb_frames = opus_packet_get_nb_frames(data, len);
  66. if(curr_nb_frames<1) return OPUS_INVALID_PACKET;
  67. /* Check the 120 ms maximum packet size */
  68. if ((curr_nb_frames+rp->nb_frames)*rp->framesize > 960)
  69. {
  70. return OPUS_INVALID_PACKET;
  71. }
  72. ret=opus_packet_parse_impl(data, len, self_delimited, &tmp_toc, &rp->frames[rp->nb_frames], &rp->len[rp->nb_frames], NULL, NULL);
  73. if(ret<1)return ret;
  74. rp->nb_frames += curr_nb_frames;
  75. return OPUS_OK;
  76. }
  77. int opus_repacketizer_cat(OpusRepacketizer *rp, const unsigned char *data, opus_int32 len)
  78. {
  79. return opus_repacketizer_cat_impl(rp, data, len, 0);
  80. }
  81. int opus_repacketizer_get_nb_frames(OpusRepacketizer *rp)
  82. {
  83. return rp->nb_frames;
  84. }
  85. opus_int32 opus_repacketizer_out_range_impl(OpusRepacketizer *rp, int begin, int end,
  86. unsigned char *data, opus_int32 maxlen, int self_delimited, int pad)
  87. {
  88. int i, count;
  89. opus_int32 tot_size;
  90. opus_int16 *len;
  91. const unsigned char **frames;
  92. unsigned char * ptr;
  93. if (begin<0 || begin>=end || end>rp->nb_frames)
  94. {
  95. /*fprintf(stderr, "%d %d %d\n", begin, end, rp->nb_frames);*/
  96. return OPUS_BAD_ARG;
  97. }
  98. count = end-begin;
  99. len = rp->len+begin;
  100. frames = rp->frames+begin;
  101. if (self_delimited)
  102. tot_size = 1 + (len[count-1]>=252);
  103. else
  104. tot_size = 0;
  105. ptr = data;
  106. if (count==1)
  107. {
  108. /* Code 0 */
  109. tot_size += len[0]+1;
  110. if (tot_size > maxlen)
  111. return OPUS_BUFFER_TOO_SMALL;
  112. *ptr++ = rp->toc&0xFC;
  113. } else if (count==2)
  114. {
  115. if (len[1] == len[0])
  116. {
  117. /* Code 1 */
  118. tot_size += 2*len[0]+1;
  119. if (tot_size > maxlen)
  120. return OPUS_BUFFER_TOO_SMALL;
  121. *ptr++ = (rp->toc&0xFC) | 0x1;
  122. } else {
  123. /* Code 2 */
  124. tot_size += len[0]+len[1]+2+(len[0]>=252);
  125. if (tot_size > maxlen)
  126. return OPUS_BUFFER_TOO_SMALL;
  127. *ptr++ = (rp->toc&0xFC) | 0x2;
  128. ptr += encode_size(len[0], ptr);
  129. }
  130. }
  131. if (count > 2 || (pad && tot_size < maxlen))
  132. {
  133. /* Code 3 */
  134. int vbr;
  135. int pad_amount=0;
  136. /* Restart the process for the padding case */
  137. ptr = data;
  138. if (self_delimited)
  139. tot_size = 1 + (len[count-1]>=252);
  140. else
  141. tot_size = 0;
  142. vbr = 0;
  143. for (i=1;i<count;i++)
  144. {
  145. if (len[i] != len[0])
  146. {
  147. vbr=1;
  148. break;
  149. }
  150. }
  151. if (vbr)
  152. {
  153. tot_size += 2;
  154. for (i=0;i<count-1;i++)
  155. tot_size += 1 + (len[i]>=252) + len[i];
  156. tot_size += len[count-1];
  157. if (tot_size > maxlen)
  158. return OPUS_BUFFER_TOO_SMALL;
  159. *ptr++ = (rp->toc&0xFC) | 0x3;
  160. *ptr++ = count | 0x80;
  161. } else {
  162. tot_size += count*len[0]+2;
  163. if (tot_size > maxlen)
  164. return OPUS_BUFFER_TOO_SMALL;
  165. *ptr++ = (rp->toc&0xFC) | 0x3;
  166. *ptr++ = count;
  167. }
  168. pad_amount = pad ? (maxlen-tot_size) : 0;
  169. if (pad_amount != 0)
  170. {
  171. int nb_255s;
  172. data[1] |= 0x40;
  173. nb_255s = (pad_amount-1)/255;
  174. for (i=0;i<nb_255s;i++)
  175. *ptr++ = 255;
  176. *ptr++ = pad_amount-255*nb_255s-1;
  177. tot_size += pad_amount;
  178. }
  179. if (vbr)
  180. {
  181. for (i=0;i<count-1;i++)
  182. ptr += encode_size(len[i], ptr);
  183. }
  184. }
  185. if (self_delimited) {
  186. int sdlen = encode_size(len[count-1], ptr);
  187. ptr += sdlen;
  188. }
  189. /* Copy the actual data */
  190. for (i=0;i<count;i++)
  191. {
  192. /* Using OPUS_MOVE() instead of OPUS_COPY() in case we're doing in-place
  193. padding from opus_packet_pad or opus_packet_unpad(). */
  194. celt_assert(frames[i] + len[i] <= data || ptr <= frames[i]);
  195. OPUS_MOVE(ptr, frames[i], len[i]);
  196. ptr += len[i];
  197. }
  198. if (pad)
  199. {
  200. /* Fill padding with zeros. */
  201. while (ptr<data+maxlen)
  202. *ptr++=0;
  203. }
  204. return tot_size;
  205. }
  206. opus_int32 opus_repacketizer_out_range(OpusRepacketizer *rp, int begin, int end, unsigned char *data, opus_int32 maxlen)
  207. {
  208. return opus_repacketizer_out_range_impl(rp, begin, end, data, maxlen, 0, 0);
  209. }
  210. opus_int32 opus_repacketizer_out(OpusRepacketizer *rp, unsigned char *data, opus_int32 maxlen)
  211. {
  212. return opus_repacketizer_out_range_impl(rp, 0, rp->nb_frames, data, maxlen, 0, 0);
  213. }
  214. int opus_packet_pad(unsigned char *data, opus_int32 len, opus_int32 new_len)
  215. {
  216. OpusRepacketizer rp;
  217. opus_int32 ret;
  218. if (len < 1)
  219. return OPUS_BAD_ARG;
  220. if (len==new_len)
  221. return OPUS_OK;
  222. else if (len > new_len)
  223. return OPUS_BAD_ARG;
  224. opus_repacketizer_init(&rp);
  225. /* Moving payload to the end of the packet so we can do in-place padding */
  226. OPUS_MOVE(data+new_len-len, data, len);
  227. ret = opus_repacketizer_cat(&rp, data+new_len-len, len);
  228. if (ret != OPUS_OK)
  229. return ret;
  230. ret = opus_repacketizer_out_range_impl(&rp, 0, rp.nb_frames, data, new_len, 0, 1);
  231. if (ret > 0)
  232. return OPUS_OK;
  233. else
  234. return ret;
  235. }
  236. opus_int32 opus_packet_unpad(unsigned char *data, opus_int32 len)
  237. {
  238. OpusRepacketizer rp;
  239. opus_int32 ret;
  240. if (len < 1)
  241. return OPUS_BAD_ARG;
  242. opus_repacketizer_init(&rp);
  243. ret = opus_repacketizer_cat(&rp, data, len);
  244. if (ret < 0)
  245. return ret;
  246. ret = opus_repacketizer_out_range_impl(&rp, 0, rp.nb_frames, data, len, 0, 0);
  247. celt_assert(ret > 0 && ret <= len);
  248. return ret;
  249. }
  250. int opus_multistream_packet_pad(unsigned char *data, opus_int32 len, opus_int32 new_len, int nb_streams)
  251. {
  252. int s;
  253. int count;
  254. unsigned char toc;
  255. opus_int16 size[48];
  256. opus_int32 packet_offset;
  257. opus_int32 amount;
  258. if (len < 1)
  259. return OPUS_BAD_ARG;
  260. if (len==new_len)
  261. return OPUS_OK;
  262. else if (len > new_len)
  263. return OPUS_BAD_ARG;
  264. amount = new_len - len;
  265. /* Seek to last stream */
  266. for (s=0;s<nb_streams-1;s++)
  267. {
  268. if (len<=0)
  269. return OPUS_INVALID_PACKET;
  270. count = opus_packet_parse_impl(data, len, 1, &toc, NULL,
  271. size, NULL, &packet_offset);
  272. if (count<0)
  273. return count;
  274. data += packet_offset;
  275. len -= packet_offset;
  276. }
  277. return opus_packet_pad(data, len, len+amount);
  278. }
  279. opus_int32 opus_multistream_packet_unpad(unsigned char *data, opus_int32 len, int nb_streams)
  280. {
  281. int s;
  282. unsigned char toc;
  283. opus_int16 size[48];
  284. opus_int32 packet_offset;
  285. OpusRepacketizer rp;
  286. unsigned char *dst;
  287. opus_int32 dst_len;
  288. if (len < 1)
  289. return OPUS_BAD_ARG;
  290. dst = data;
  291. dst_len = 0;
  292. /* Unpad all frames */
  293. for (s=0;s<nb_streams;s++)
  294. {
  295. opus_int32 ret;
  296. int self_delimited = s!=nb_streams-1;
  297. if (len<=0)
  298. return OPUS_INVALID_PACKET;
  299. opus_repacketizer_init(&rp);
  300. ret = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL,
  301. size, NULL, &packet_offset);
  302. if (ret<0)
  303. return ret;
  304. ret = opus_repacketizer_cat_impl(&rp, data, packet_offset, self_delimited);
  305. if (ret < 0)
  306. return ret;
  307. ret = opus_repacketizer_out_range_impl(&rp, 0, rp.nb_frames, dst, len, self_delimited, 0);
  308. if (ret < 0)
  309. return ret;
  310. else
  311. dst_len += ret;
  312. dst += ret;
  313. data += packet_offset;
  314. len -= packet_offset;
  315. }
  316. return dst_len;
  317. }