Home | History | Annotate | Download | only in src
      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 
      8    - Redistributions of source code must retain the above copyright
      9    notice, this list of conditions and the following disclaimer.
     10 
     11    - Redistributions in binary form must reproduce the above copyright
     12    notice, this list of conditions and the following disclaimer in the
     13    documentation and/or other materials provided with the distribution.
     14 
     15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     18    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     19    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     20    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     22    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     23    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     24    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     25    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26 */
     27 
     28 #ifdef HAVE_CONFIG_H
     29 #include "config.h"
     30 #endif
     31 
     32 #include "opus.h"
     33 #include "opus_private.h"
     34 #include "os_support.h"
     35 
     36 
     37 int opus_repacketizer_get_size(void)
     38 {
     39    return sizeof(OpusRepacketizer);
     40 }
     41 
     42 OpusRepacketizer *opus_repacketizer_init(OpusRepacketizer *rp)
     43 {
     44    rp->nb_frames = 0;
     45    return rp;
     46 }
     47 
     48 OpusRepacketizer *opus_repacketizer_create(void)
     49 {
     50    OpusRepacketizer *rp;
     51    rp=(OpusRepacketizer *)opus_alloc(opus_repacketizer_get_size());
     52    if(rp==NULL)return NULL;
     53    return opus_repacketizer_init(rp);
     54 }
     55 
     56 void opus_repacketizer_destroy(OpusRepacketizer *rp)
     57 {
     58    opus_free(rp);
     59 }
     60 
     61 int opus_repacketizer_cat(OpusRepacketizer *rp, const unsigned char *data, opus_int32 len)
     62 {
     63    unsigned char tmp_toc;
     64    int curr_nb_frames,ret;
     65    /* Set of check ToC */
     66    if (len<1) return OPUS_INVALID_PACKET;
     67    if (rp->nb_frames == 0)
     68    {
     69       rp->toc = data[0];
     70       rp->framesize = opus_packet_get_samples_per_frame(data, 8000);
     71    } else if ((rp->toc&0xFC) != (data[0]&0xFC))
     72    {
     73       /*fprintf(stderr, "toc mismatch: 0x%x vs 0x%x\n", rp->toc, data[0]);*/
     74       return OPUS_INVALID_PACKET;
     75    }
     76    curr_nb_frames = opus_packet_get_nb_frames(data, len);
     77    if(curr_nb_frames<1) return OPUS_INVALID_PACKET;
     78 
     79    /* Check the 120 ms maximum packet size */
     80    if ((curr_nb_frames+rp->nb_frames)*rp->framesize > 960)
     81    {
     82       return OPUS_INVALID_PACKET;
     83    }
     84 
     85    ret=opus_packet_parse(data, len, &tmp_toc, &rp->frames[rp->nb_frames], &rp->len[rp->nb_frames], NULL);
     86    if(ret<1)return ret;
     87 
     88    rp->nb_frames += curr_nb_frames;
     89    return OPUS_OK;
     90 }
     91 
     92 int opus_repacketizer_get_nb_frames(OpusRepacketizer *rp)
     93 {
     94    return rp->nb_frames;
     95 }
     96 
     97 opus_int32 opus_repacketizer_out_range_impl(OpusRepacketizer *rp, int begin, int end, unsigned char *data, opus_int32 maxlen, int self_delimited)
     98 {
     99    int i, count;
    100    opus_int32 tot_size;
    101    short *len;
    102    const unsigned char **frames;
    103 
    104    if (begin<0 || begin>=end || end>rp->nb_frames)
    105    {
    106       /*fprintf(stderr, "%d %d %d\n", begin, end, rp->nb_frames);*/
    107       return OPUS_BAD_ARG;
    108    }
    109    count = end-begin;
    110 
    111    len = rp->len+begin;
    112    frames = rp->frames+begin;
    113    if (self_delimited)
    114       tot_size = 1 + (len[count-1]>=252);
    115    else
    116       tot_size = 0;
    117 
    118    switch (count)
    119    {
    120    case 1:
    121    {
    122       /* Code 0 */
    123       tot_size += len[0]+1;
    124       if (tot_size > maxlen)
    125          return OPUS_BUFFER_TOO_SMALL;
    126       *data++ = rp->toc&0xFC;
    127    }
    128    break;
    129    case 2:
    130    {
    131       if (len[1] == len[0])
    132       {
    133          /* Code 1 */
    134          tot_size += 2*len[0]+1;
    135          if (tot_size > maxlen)
    136             return OPUS_BUFFER_TOO_SMALL;
    137          *data++ = (rp->toc&0xFC) | 0x1;
    138       } else {
    139          /* Code 2 */
    140          tot_size += len[0]+len[1]+2+(len[0]>=252);
    141          if (tot_size > maxlen)
    142             return OPUS_BUFFER_TOO_SMALL;
    143          *data++ = (rp->toc&0xFC) | 0x2;
    144          data += encode_size(len[0], data);
    145       }
    146    }
    147    break;
    148    default:
    149    {
    150       /* Code 3 */
    151       int vbr;
    152 
    153       vbr = 0;
    154       for (i=1;i<count;i++)
    155       {
    156          if (len[i] != len[0])
    157          {
    158             vbr=1;
    159             break;
    160          }
    161       }
    162       if (vbr)
    163       {
    164          tot_size += 2;
    165          for (i=0;i<count-1;i++)
    166             tot_size += 1 + (len[i]>=252) + len[i];
    167          tot_size += len[count-1];
    168 
    169          if (tot_size > maxlen)
    170             return OPUS_BUFFER_TOO_SMALL;
    171          *data++ = (rp->toc&0xFC) | 0x3;
    172          *data++ = count | 0x80;
    173          for (i=0;i<count-1;i++)
    174             data += encode_size(len[i], data);
    175       } else {
    176          tot_size += count*len[0]+2;
    177          if (tot_size > maxlen)
    178             return OPUS_BUFFER_TOO_SMALL;
    179          *data++ = (rp->toc&0xFC) | 0x3;
    180          *data++ = count;
    181       }
    182    }
    183    break;
    184    }
    185    if (self_delimited) {
    186       int sdlen = encode_size(len[count-1], data);
    187       data += sdlen;
    188    }
    189    /* Copy the actual data */
    190    for (i=0;i<count;i++)
    191    {
    192       OPUS_COPY(data, frames[i], len[i]);
    193       data += len[i];
    194    }
    195    return tot_size;
    196 }
    197 
    198 opus_int32 opus_repacketizer_out_range(OpusRepacketizer *rp, int begin, int end, unsigned char *data, opus_int32 maxlen)
    199 {
    200    return opus_repacketizer_out_range_impl(rp, begin, end, data, maxlen, 0);
    201 }
    202 
    203 opus_int32 opus_repacketizer_out(OpusRepacketizer *rp, unsigned char *data, opus_int32 maxlen)
    204 {
    205    return opus_repacketizer_out_range_impl(rp, 0, rp->nb_frames, data, maxlen, 0);
    206 }
    207 
    208 
    209