michael@0: /* Copyright (c) 2011 Xiph.Org Foundation michael@0: Written by Jean-Marc Valin */ michael@0: /* michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: michael@0: - Redistributions of source code must retain the above copyright michael@0: notice, this list of conditions and the following disclaimer. michael@0: michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER michael@0: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifdef HAVE_CONFIG_H michael@0: #include "config.h" michael@0: #endif michael@0: michael@0: #include "opus.h" michael@0: #include "opus_private.h" michael@0: #include "os_support.h" michael@0: michael@0: michael@0: int opus_repacketizer_get_size(void) michael@0: { michael@0: return sizeof(OpusRepacketizer); michael@0: } michael@0: michael@0: OpusRepacketizer *opus_repacketizer_init(OpusRepacketizer *rp) michael@0: { michael@0: rp->nb_frames = 0; michael@0: return rp; michael@0: } michael@0: michael@0: OpusRepacketizer *opus_repacketizer_create(void) michael@0: { michael@0: OpusRepacketizer *rp; michael@0: rp=(OpusRepacketizer *)opus_alloc(opus_repacketizer_get_size()); michael@0: if(rp==NULL)return NULL; michael@0: return opus_repacketizer_init(rp); michael@0: } michael@0: michael@0: void opus_repacketizer_destroy(OpusRepacketizer *rp) michael@0: { michael@0: opus_free(rp); michael@0: } michael@0: michael@0: static int opus_repacketizer_cat_impl(OpusRepacketizer *rp, const unsigned char *data, opus_int32 len, int self_delimited) michael@0: { michael@0: unsigned char tmp_toc; michael@0: int curr_nb_frames,ret; michael@0: /* Set of check ToC */ michael@0: if (len<1) return OPUS_INVALID_PACKET; michael@0: if (rp->nb_frames == 0) michael@0: { michael@0: rp->toc = data[0]; michael@0: rp->framesize = opus_packet_get_samples_per_frame(data, 8000); michael@0: } else if ((rp->toc&0xFC) != (data[0]&0xFC)) michael@0: { michael@0: /*fprintf(stderr, "toc mismatch: 0x%x vs 0x%x\n", rp->toc, data[0]);*/ michael@0: return OPUS_INVALID_PACKET; michael@0: } michael@0: curr_nb_frames = opus_packet_get_nb_frames(data, len); michael@0: if(curr_nb_frames<1) return OPUS_INVALID_PACKET; michael@0: michael@0: /* Check the 120 ms maximum packet size */ michael@0: if ((curr_nb_frames+rp->nb_frames)*rp->framesize > 960) michael@0: { michael@0: return OPUS_INVALID_PACKET; michael@0: } michael@0: michael@0: ret=opus_packet_parse_impl(data, len, self_delimited, &tmp_toc, &rp->frames[rp->nb_frames], &rp->len[rp->nb_frames], NULL, NULL); michael@0: if(ret<1)return ret; michael@0: michael@0: rp->nb_frames += curr_nb_frames; michael@0: return OPUS_OK; michael@0: } michael@0: michael@0: int opus_repacketizer_cat(OpusRepacketizer *rp, const unsigned char *data, opus_int32 len) michael@0: { michael@0: return opus_repacketizer_cat_impl(rp, data, len, 0); michael@0: } michael@0: michael@0: int opus_repacketizer_get_nb_frames(OpusRepacketizer *rp) michael@0: { michael@0: return rp->nb_frames; michael@0: } michael@0: michael@0: opus_int32 opus_repacketizer_out_range_impl(OpusRepacketizer *rp, int begin, int end, michael@0: unsigned char *data, opus_int32 maxlen, int self_delimited, int pad) michael@0: { michael@0: int i, count; michael@0: opus_int32 tot_size; michael@0: opus_int16 *len; michael@0: const unsigned char **frames; michael@0: unsigned char * ptr; michael@0: michael@0: if (begin<0 || begin>=end || end>rp->nb_frames) michael@0: { michael@0: /*fprintf(stderr, "%d %d %d\n", begin, end, rp->nb_frames);*/ michael@0: return OPUS_BAD_ARG; michael@0: } michael@0: count = end-begin; michael@0: michael@0: len = rp->len+begin; michael@0: frames = rp->frames+begin; michael@0: if (self_delimited) michael@0: tot_size = 1 + (len[count-1]>=252); michael@0: else michael@0: tot_size = 0; michael@0: michael@0: ptr = data; michael@0: if (count==1) michael@0: { michael@0: /* Code 0 */ michael@0: tot_size += len[0]+1; michael@0: if (tot_size > maxlen) michael@0: return OPUS_BUFFER_TOO_SMALL; michael@0: *ptr++ = rp->toc&0xFC; michael@0: } else if (count==2) michael@0: { michael@0: if (len[1] == len[0]) michael@0: { michael@0: /* Code 1 */ michael@0: tot_size += 2*len[0]+1; michael@0: if (tot_size > maxlen) michael@0: return OPUS_BUFFER_TOO_SMALL; michael@0: *ptr++ = (rp->toc&0xFC) | 0x1; michael@0: } else { michael@0: /* Code 2 */ michael@0: tot_size += len[0]+len[1]+2+(len[0]>=252); michael@0: if (tot_size > maxlen) michael@0: return OPUS_BUFFER_TOO_SMALL; michael@0: *ptr++ = (rp->toc&0xFC) | 0x2; michael@0: ptr += encode_size(len[0], ptr); michael@0: } michael@0: } michael@0: if (count > 2 || (pad && tot_size < maxlen)) michael@0: { michael@0: /* Code 3 */ michael@0: int vbr; michael@0: int pad_amount=0; michael@0: michael@0: /* Restart the process for the padding case */ michael@0: ptr = data; michael@0: if (self_delimited) michael@0: tot_size = 1 + (len[count-1]>=252); michael@0: else michael@0: tot_size = 0; michael@0: vbr = 0; michael@0: for (i=1;i=252) + len[i]; michael@0: tot_size += len[count-1]; michael@0: michael@0: if (tot_size > maxlen) michael@0: return OPUS_BUFFER_TOO_SMALL; michael@0: *ptr++ = (rp->toc&0xFC) | 0x3; michael@0: *ptr++ = count | 0x80; michael@0: } else { michael@0: tot_size += count*len[0]+2; michael@0: if (tot_size > maxlen) michael@0: return OPUS_BUFFER_TOO_SMALL; michael@0: *ptr++ = (rp->toc&0xFC) | 0x3; michael@0: *ptr++ = count; michael@0: } michael@0: pad_amount = pad ? (maxlen-tot_size) : 0; michael@0: if (pad_amount != 0) michael@0: { michael@0: int nb_255s; michael@0: data[1] |= 0x40; michael@0: nb_255s = (pad_amount-1)/255; michael@0: for (i=0;inb_frames, data, maxlen, 0, 0); michael@0: } michael@0: michael@0: int opus_packet_pad(unsigned char *data, opus_int32 len, opus_int32 new_len) michael@0: { michael@0: OpusRepacketizer rp; michael@0: opus_int32 ret; michael@0: if (len < 1) michael@0: return OPUS_BAD_ARG; michael@0: if (len==new_len) michael@0: return OPUS_OK; michael@0: else if (len > new_len) michael@0: return OPUS_BAD_ARG; michael@0: opus_repacketizer_init(&rp); michael@0: /* Moving payload to the end of the packet so we can do in-place padding */ michael@0: OPUS_MOVE(data+new_len-len, data, len); michael@0: opus_repacketizer_cat(&rp, data+new_len-len, len); michael@0: ret = opus_repacketizer_out_range_impl(&rp, 0, rp.nb_frames, data, new_len, 0, 1); michael@0: if (ret > 0) michael@0: return OPUS_OK; michael@0: else michael@0: return ret; michael@0: } michael@0: michael@0: opus_int32 opus_packet_unpad(unsigned char *data, opus_int32 len) michael@0: { michael@0: OpusRepacketizer rp; michael@0: opus_int32 ret; michael@0: if (len < 1) michael@0: return OPUS_BAD_ARG; michael@0: opus_repacketizer_init(&rp); michael@0: ret = opus_repacketizer_cat(&rp, data, len); michael@0: if (ret < 0) michael@0: return ret; michael@0: ret = opus_repacketizer_out_range_impl(&rp, 0, rp.nb_frames, data, len, 0, 0); michael@0: celt_assert(ret > 0 && ret <= len); michael@0: return ret; michael@0: } michael@0: michael@0: int opus_multistream_packet_pad(unsigned char *data, opus_int32 len, opus_int32 new_len, int nb_streams) michael@0: { michael@0: int s; michael@0: int count; michael@0: unsigned char toc; michael@0: opus_int16 size[48]; michael@0: opus_int32 packet_offset; michael@0: opus_int32 amount; michael@0: michael@0: if (len < 1) michael@0: return OPUS_BAD_ARG; michael@0: if (len==new_len) michael@0: return OPUS_OK; michael@0: else if (len > new_len) michael@0: return OPUS_BAD_ARG; michael@0: amount = new_len - len; michael@0: /* Seek to last stream */ michael@0: for (s=0;s