Home | History | Annotate | Download | only in ilbc
      1 /*
      2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 /******************************************************************
     12 
     13  iLBC Speech Coder ANSI-C Source Code
     14 
     15  WebRtcIlbcfix_PackBits.c
     16 
     17 ******************************************************************/
     18 
     19 #include "defines.h"
     20 
     21 /*----------------------------------------------------------------*
     22  *  unpacking of bits from bitstream, i.e., vector of bytes
     23  *---------------------------------------------------------------*/
     24 
     25 void WebRtcIlbcfix_PackBits(
     26     uint16_t *bitstream,   /* (o) The packetized bitstream */
     27     iLBC_bits *enc_bits,  /* (i) Encoded bits */
     28     int16_t mode     /* (i) Codec mode (20 or 30) */
     29                              ){
     30   uint16_t *bitstreamPtr;
     31   int i, k;
     32   int16_t *tmpPtr;
     33 
     34   bitstreamPtr=bitstream;
     35 
     36   /* Class 1 bits of ULP */
     37   /* First int16_t */
     38   (*bitstreamPtr)  = ((uint16_t)enc_bits->lsf[0])<<10;   /* Bit 0..5  */
     39   (*bitstreamPtr) |= (enc_bits->lsf[1])<<3;     /* Bit 6..12 */
     40   (*bitstreamPtr) |= (enc_bits->lsf[2]&0x70)>>4;    /* Bit 13..15 */
     41   bitstreamPtr++;
     42   /* Second int16_t */
     43   (*bitstreamPtr)  = ((uint16_t)enc_bits->lsf[2]&0xF)<<12;  /* Bit 0..3  */
     44 
     45   if (mode==20) {
     46     (*bitstreamPtr) |= (enc_bits->startIdx)<<10;    /* Bit 4..5  */
     47     (*bitstreamPtr) |= (enc_bits->state_first)<<9;    /* Bit 6  */
     48     (*bitstreamPtr) |= (enc_bits->idxForMax)<<3;    /* Bit 7..12 */
     49     (*bitstreamPtr) |= ((enc_bits->cb_index[0])&0x70)>>4;  /* Bit 13..15 */
     50     bitstreamPtr++;
     51     /* Third int16_t */
     52     (*bitstreamPtr) = ((enc_bits->cb_index[0])&0xE)<<12;  /* Bit 0..2  */
     53     (*bitstreamPtr) |= ((enc_bits->gain_index[0])&0x18)<<8;  /* Bit 3..4  */
     54     (*bitstreamPtr) |= ((enc_bits->gain_index[1])&0x8)<<7;  /* Bit 5  */
     55     (*bitstreamPtr) |= ((enc_bits->cb_index[3])&0xFE)<<2;  /* Bit 6..12 */
     56     (*bitstreamPtr) |= ((enc_bits->gain_index[3])&0x10)>>2;  /* Bit 13  */
     57     (*bitstreamPtr) |= ((enc_bits->gain_index[4])&0x8)>>2;  /* Bit 14  */
     58     (*bitstreamPtr) |= ((enc_bits->gain_index[6])&0x10)>>4;  /* Bit 15  */
     59   } else { /* mode==30 */
     60     (*bitstreamPtr) |= (enc_bits->lsf[3])<<6;     /* Bit 4..9  */
     61     (*bitstreamPtr) |= (enc_bits->lsf[4]&0x7E)>>1;    /* Bit 10..15 */
     62     bitstreamPtr++;
     63     /* Third int16_t */
     64     (*bitstreamPtr)  = ((uint16_t)enc_bits->lsf[4]&0x1)<<15;  /* Bit 0  */
     65     (*bitstreamPtr) |= (enc_bits->lsf[5])<<8;     /* Bit 1..7  */
     66     (*bitstreamPtr) |= (enc_bits->startIdx)<<5;     /* Bit 8..10 */
     67     (*bitstreamPtr) |= (enc_bits->state_first)<<4;    /* Bit 11  */
     68     (*bitstreamPtr) |= ((enc_bits->idxForMax)&0x3C)>>2;   /* Bit 12..15 */
     69     bitstreamPtr++;
     70     /* 4:th int16_t */
     71     (*bitstreamPtr)  = ((uint16_t)enc_bits->idxForMax&0x3)<<14; /* Bit 0..1  */
     72     (*bitstreamPtr) |= (enc_bits->cb_index[0]&0x78)<<7;   /* Bit 2..5  */
     73     (*bitstreamPtr) |= (enc_bits->gain_index[0]&0x10)<<5;  /* Bit 6  */
     74     (*bitstreamPtr) |= (enc_bits->gain_index[1]&0x8)<<5;  /* Bit 7  */
     75     (*bitstreamPtr) |= (enc_bits->cb_index[3]&0xFC);   /* Bit 8..13 */
     76     (*bitstreamPtr) |= (enc_bits->gain_index[3]&0x10)>>3;  /* Bit 14  */
     77     (*bitstreamPtr) |= (enc_bits->gain_index[4]&0x8)>>3;  /* Bit 15  */
     78   }
     79   /* Class 2 bits of ULP */
     80   /* 4:th to 6:th int16_t for 20 ms case
     81      5:th to 7:th int16_t for 30 ms case */
     82   bitstreamPtr++;
     83   tmpPtr=enc_bits->idxVec;
     84   for (k=0; k<3; k++) {
     85     (*bitstreamPtr) = 0;
     86     for (i=15; i>=0; i--) {
     87       (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x4)>>2)<<i;
     88       /* Bit 15-i  */
     89       tmpPtr++;
     90     }
     91     bitstreamPtr++;
     92   }
     93 
     94   if (mode==20) {
     95     /* 7:th int16_t */
     96     (*bitstreamPtr) = 0;
     97     for (i=15; i>6; i--) {
     98       (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x4)>>2)<<i;
     99       /* Bit 15-i  */
    100       tmpPtr++;
    101     }
    102     (*bitstreamPtr) |= (enc_bits->gain_index[1]&0x4)<<4;  /* Bit 9  */
    103     (*bitstreamPtr) |= (enc_bits->gain_index[3]&0xC)<<2;  /* Bit 10..11 */
    104     (*bitstreamPtr) |= (enc_bits->gain_index[4]&0x4)<<1;  /* Bit 12  */
    105     (*bitstreamPtr) |= (enc_bits->gain_index[6]&0x8)>>1;  /* Bit 13  */
    106     (*bitstreamPtr) |= (enc_bits->gain_index[7]&0xC)>>2;  /* Bit 14..15 */
    107 
    108   } else { /* mode==30 */
    109     /* 8:th int16_t */
    110     (*bitstreamPtr) = 0;
    111     for (i=15; i>5; i--) {
    112       (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x4)>>2)<<i;
    113       /* Bit 15-i  */
    114       tmpPtr++;
    115     }
    116     (*bitstreamPtr) |= (enc_bits->cb_index[0]&0x6)<<3;   /* Bit 10..11 */
    117     (*bitstreamPtr) |= (enc_bits->gain_index[0]&0x8);   /* Bit 12  */
    118     (*bitstreamPtr) |= (enc_bits->gain_index[1]&0x4);   /* Bit 13  */
    119     (*bitstreamPtr) |= (enc_bits->cb_index[3]&0x2);    /* Bit 14  */
    120     (*bitstreamPtr) |= (enc_bits->cb_index[6]&0x80)>>7;   /* Bit 15  */
    121     bitstreamPtr++;
    122     /* 9:th int16_t */
    123     (*bitstreamPtr)  = ((uint16_t)enc_bits->cb_index[6]&0x7E)<<9;/* Bit 0..5  */
    124     (*bitstreamPtr) |= (enc_bits->cb_index[9]&0xFE)<<2;   /* Bit 6..12 */
    125     (*bitstreamPtr) |= (enc_bits->cb_index[12]&0xE0)>>5;  /* Bit 13..15 */
    126     bitstreamPtr++;
    127     /* 10:th int16_t */
    128     (*bitstreamPtr)  = ((uint16_t)enc_bits->cb_index[12]&0x1E)<<11;/* Bit 0..3 */
    129     (*bitstreamPtr) |= (enc_bits->gain_index[3]&0xC)<<8;  /* Bit 4..5  */
    130     (*bitstreamPtr) |= (enc_bits->gain_index[4]&0x6)<<7;  /* Bit 6..7  */
    131     (*bitstreamPtr) |= (enc_bits->gain_index[6]&0x18)<<3;  /* Bit 8..9  */
    132     (*bitstreamPtr) |= (enc_bits->gain_index[7]&0xC)<<2;  /* Bit 10..11 */
    133     (*bitstreamPtr) |= (enc_bits->gain_index[9]&0x10)>>1;  /* Bit 12  */
    134     (*bitstreamPtr) |= (enc_bits->gain_index[10]&0x8)>>1;  /* Bit 13  */
    135     (*bitstreamPtr) |= (enc_bits->gain_index[12]&0x10)>>3;  /* Bit 14  */
    136     (*bitstreamPtr) |= (enc_bits->gain_index[13]&0x8)>>3;  /* Bit 15  */
    137   }
    138   bitstreamPtr++;
    139   /* Class 3 bits of ULP */
    140   /*  8:th to 14:th int16_t for 20 ms case
    141       11:th to 17:th int16_t for 30 ms case */
    142   tmpPtr=enc_bits->idxVec;
    143   for (k=0; k<7; k++) {
    144     (*bitstreamPtr) = 0;
    145     for (i=14; i>=0; i-=2) {
    146       (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x3))<<i; /* Bit 15-i..14-i*/
    147       tmpPtr++;
    148     }
    149     bitstreamPtr++;
    150   }
    151 
    152   if (mode==20) {
    153     /* 15:th int16_t */
    154     (*bitstreamPtr)  = ((uint16_t)((enc_bits->idxVec[56])&0x3))<<14;/* Bit 0..1 */
    155     (*bitstreamPtr) |= (((enc_bits->cb_index[0])&1))<<13;  /* Bit 2  */
    156     (*bitstreamPtr) |= ((enc_bits->cb_index[1]))<<6;   /* Bit 3..9  */
    157     (*bitstreamPtr) |= ((enc_bits->cb_index[2])&0x7E)>>1;  /* Bit 10..15 */
    158     bitstreamPtr++;
    159     /* 16:th int16_t */
    160     (*bitstreamPtr) = ((uint16_t)((enc_bits->cb_index[2])&0x1))<<15;
    161     /* Bit 0  */
    162     (*bitstreamPtr) |= ((enc_bits->gain_index[0])&0x7)<<12;  /* Bit 1..3  */
    163     (*bitstreamPtr) |= ((enc_bits->gain_index[1])&0x3)<<10;  /* Bit 4..5  */
    164     (*bitstreamPtr) |= ((enc_bits->gain_index[2]))<<7;   /* Bit 6..8  */
    165     (*bitstreamPtr) |= ((enc_bits->cb_index[3])&0x1)<<6;  /* Bit 9  */
    166     (*bitstreamPtr) |= ((enc_bits->cb_index[4])&0x7E)>>1;  /* Bit 10..15 */
    167     bitstreamPtr++;
    168     /* 17:th int16_t */
    169     (*bitstreamPtr) = ((uint16_t)((enc_bits->cb_index[4])&0x1))<<15;
    170     /* Bit 0  */
    171     (*bitstreamPtr) |= (enc_bits->cb_index[5])<<8;    /* Bit 1..7  */
    172     (*bitstreamPtr) |= (enc_bits->cb_index[6]);     /* Bit 8..15 */
    173     bitstreamPtr++;
    174     /* 18:th int16_t */
    175     (*bitstreamPtr) = ((uint16_t)(enc_bits->cb_index[7]))<<8; /* Bit 0..7  */
    176     (*bitstreamPtr) |= (enc_bits->cb_index[8]);     /* Bit 8..15 */
    177     bitstreamPtr++;
    178     /* 19:th int16_t */
    179     (*bitstreamPtr) = ((uint16_t)((enc_bits->gain_index[3])&0x3))<<14;
    180     /* Bit 0..1  */
    181     (*bitstreamPtr) |= ((enc_bits->gain_index[4])&0x3)<<12;  /* Bit 2..3  */
    182     (*bitstreamPtr) |= ((enc_bits->gain_index[5]))<<9;   /* Bit 4..6  */
    183     (*bitstreamPtr) |= ((enc_bits->gain_index[6])&0x7)<<6;  /* Bit 7..9  */
    184     (*bitstreamPtr) |= ((enc_bits->gain_index[7])&0x3)<<4;  /* Bit 10..11 */
    185     (*bitstreamPtr) |= (enc_bits->gain_index[8])<<1;   /* Bit 12..14 */
    186   } else { /* mode==30 */
    187     /* 18:th int16_t */
    188     (*bitstreamPtr)  = ((uint16_t)((enc_bits->idxVec[56])&0x3))<<14;/* Bit 0..1 */
    189     (*bitstreamPtr) |= (((enc_bits->idxVec[57])&0x3))<<12;  /* Bit 2..3  */
    190     (*bitstreamPtr) |= (((enc_bits->cb_index[0])&1))<<11;  /* Bit 4  */
    191     (*bitstreamPtr) |= ((enc_bits->cb_index[1]))<<4;   /* Bit 5..11 */
    192     (*bitstreamPtr) |= ((enc_bits->cb_index[2])&0x78)>>3;  /* Bit 12..15 */
    193     bitstreamPtr++;
    194     /* 19:th int16_t */
    195     (*bitstreamPtr)  = ((uint16_t)(enc_bits->cb_index[2])&0x7)<<13;
    196     /* Bit 0..2  */
    197     (*bitstreamPtr) |= ((enc_bits->gain_index[0])&0x7)<<10;  /* Bit 3..5  */
    198     (*bitstreamPtr) |= ((enc_bits->gain_index[1])&0x3)<<8;  /* Bit 6..7  */
    199     (*bitstreamPtr) |= ((enc_bits->gain_index[2])&0x7)<<5;  /* Bit 8..10 */
    200     (*bitstreamPtr) |= ((enc_bits->cb_index[3])&0x1)<<4;  /* Bit 11  */
    201     (*bitstreamPtr) |= ((enc_bits->cb_index[4])&0x78)>>3;  /* Bit 12..15 */
    202     bitstreamPtr++;
    203     /* 20:th int16_t */
    204     (*bitstreamPtr)  = ((uint16_t)(enc_bits->cb_index[4])&0x7)<<13;
    205     /* Bit 0..2  */
    206     (*bitstreamPtr) |= ((enc_bits->cb_index[5]))<<6;   /* Bit 3..9  */
    207     (*bitstreamPtr) |= ((enc_bits->cb_index[6])&0x1)<<5;  /* Bit 10  */
    208     (*bitstreamPtr) |= ((enc_bits->cb_index[7])&0xF8)>>3;  /* Bit 11..15 */
    209     bitstreamPtr++;
    210     /* 21:st int16_t */
    211     (*bitstreamPtr)  = ((uint16_t)(enc_bits->cb_index[7])&0x7)<<13;
    212     /* Bit 0..2  */
    213     (*bitstreamPtr) |= ((enc_bits->cb_index[8]))<<5;   /* Bit 3..10 */
    214     (*bitstreamPtr) |= ((enc_bits->cb_index[9])&0x1)<<4;  /* Bit 11  */
    215     (*bitstreamPtr) |= ((enc_bits->cb_index[10])&0xF0)>>4;  /* Bit 12..15 */
    216     bitstreamPtr++;
    217     /* 22:nd int16_t */
    218     (*bitstreamPtr)  = ((uint16_t)(enc_bits->cb_index[10])&0xF)<<12;
    219     /* Bit 0..3  */
    220     (*bitstreamPtr) |= ((enc_bits->cb_index[11]))<<4;   /* Bit 4..11 */
    221     (*bitstreamPtr) |= ((enc_bits->cb_index[12])&0x1)<<3;  /* Bit 12  */
    222     (*bitstreamPtr) |= ((enc_bits->cb_index[13])&0xE0)>>5;  /* Bit 13..15 */
    223     bitstreamPtr++;
    224     /* 23:rd int16_t */
    225     (*bitstreamPtr)  = ((uint16_t)(enc_bits->cb_index[13])&0x1F)<<11;
    226     /* Bit 0..4  */
    227     (*bitstreamPtr) |= ((enc_bits->cb_index[14]))<<3;   /* Bit 5..12 */
    228     (*bitstreamPtr) |= ((enc_bits->gain_index[3])&0x3)<<1;  /* Bit 13..14 */
    229     (*bitstreamPtr) |= ((enc_bits->gain_index[4])&0x1);   /* Bit 15  */
    230     bitstreamPtr++;
    231     /* 24:rd int16_t */
    232     (*bitstreamPtr)  = ((uint16_t)(enc_bits->gain_index[5]))<<13;
    233     /* Bit 0..2  */
    234     (*bitstreamPtr) |= ((enc_bits->gain_index[6])&0x7)<<10;  /* Bit 3..5  */
    235     (*bitstreamPtr) |= ((enc_bits->gain_index[7])&0x3)<<8;  /* Bit 6..7  */
    236     (*bitstreamPtr) |= ((enc_bits->gain_index[8]))<<5;   /* Bit 8..10 */
    237     (*bitstreamPtr) |= ((enc_bits->gain_index[9])&0xF)<<1;  /* Bit 11..14 */
    238     (*bitstreamPtr) |= ((enc_bits->gain_index[10])&0x4)>>2;  /* Bit 15  */
    239     bitstreamPtr++;
    240     /* 25:rd int16_t */
    241     (*bitstreamPtr)  = ((uint16_t)(enc_bits->gain_index[10])&0x3)<<14;
    242     /* Bit 0..1  */
    243     (*bitstreamPtr) |= ((enc_bits->gain_index[11]))<<11;  /* Bit 2..4  */
    244     (*bitstreamPtr) |= ((enc_bits->gain_index[12])&0xF)<<7;  /* Bit 5..8  */
    245     (*bitstreamPtr) |= ((enc_bits->gain_index[13])&0x7)<<4;  /* Bit 9..11 */
    246     (*bitstreamPtr) |= ((enc_bits->gain_index[14]))<<1;   /* Bit 12..14 */
    247   }
    248   /* Last bit is automatically zero */
    249 
    250   return;
    251 }
    252