1 /* 2 ** Copyright 2003-2010, VisualOn, Inc. 3 ** 4 ** Licensed under the Apache License, Version 2.0 (the "License"); 5 ** you may not use this file except in compliance with the License. 6 ** You may obtain a copy of the License at 7 ** 8 ** http://www.apache.org/licenses/LICENSE-2.0 9 ** 10 ** Unless required by applicable law or agreed to in writing, software 11 ** distributed under the License is distributed on an "AS IS" BASIS, 12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 ** See the License for the specific language governing permissions and 14 ** limitations under the License. 15 */ 16 17 /*********************************************************************** 18 File: bits.c 19 20 Description: Performs bit stream manipulation 21 22 ************************************************************************/ 23 24 #include <stdlib.h> 25 #include <stdio.h> 26 #include "typedef.h" 27 #include "basic_op.h" 28 #include "cnst.h" 29 #include "bits.h" 30 #include "acelp.h" 31 #include "dtx.h" 32 #include "mime_io.tab" 33 34 35 int PackBits(Word16 prms[], /* i: analysis parameters */ 36 Word16 coding_mode, /* i: coding bit-stream ratio mode */ 37 Word16 mode, /* i: coding bit-stream ratio mode*/ 38 Coder_State *st /*i/o: coder global parameters struct */ 39 ) 40 { 41 Word16 i, frame_type; 42 UWord8 temp; 43 UWord8 *stream_ptr; 44 Word16 bitstreamformat = st->frameType; 45 46 unsigned short* dataOut = st->outputStream; 47 48 if (coding_mode == MRDTX) 49 { 50 st->sid_update_counter--; 51 52 if (st->prev_ft == TX_SPEECH) 53 { 54 frame_type = TX_SID_FIRST; 55 st->sid_update_counter = 3; 56 } else 57 { 58 if ((st->sid_handover_debt > 0) && (st->sid_update_counter > 2)) 59 { 60 /* ensure extra updates are properly delayed after a possible SID_FIRST */ 61 frame_type = TX_SID_UPDATE; 62 st->sid_handover_debt--; 63 } else 64 { 65 if (st->sid_update_counter == 0) 66 { 67 frame_type = TX_SID_UPDATE; 68 st->sid_update_counter = 8; 69 } else 70 { 71 frame_type = TX_NO_DATA; 72 } 73 } 74 } 75 } else 76 { 77 st->sid_update_counter = 8; 78 frame_type = TX_SPEECH; 79 } 80 st->prev_ft = frame_type; 81 82 if(bitstreamformat == 0) /* default file format */ 83 { 84 *(dataOut) = TX_FRAME_TYPE; 85 *(dataOut + 1) = frame_type; 86 *(dataOut + 2) = mode; 87 for (i = 0; i < nb_of_bits[coding_mode]; i++) 88 { 89 *(dataOut + 3 + i) = prms[i]; 90 } 91 return (3 + nb_of_bits[coding_mode])<<1; 92 } else 93 { 94 if (bitstreamformat == 1) /* ITU file format */ 95 { 96 *(dataOut) = 0x6b21; 97 if(frame_type != TX_NO_DATA && frame_type != TX_SID_FIRST) 98 { 99 *(dataOut + 1) = nb_of_bits[coding_mode]; 100 for (i = 0; i < nb_of_bits[coding_mode]; i++) 101 { 102 if(prms[i] == BIT_0){ 103 *(dataOut + 2 + i) = BIT_0_ITU; 104 } 105 else{ 106 *(dataOut + 2 + i) = BIT_1_ITU; 107 } 108 } 109 return (2 + nb_of_bits[coding_mode])<<1; 110 } else 111 { 112 *(dataOut + 1) = 0; 113 return 2<<1; 114 } 115 } else /* MIME/storage file format */ 116 { 117 #define MRSID 9 118 /* change mode index in case of SID frame */ 119 if (coding_mode == MRDTX) 120 { 121 coding_mode = MRSID; 122 if (frame_type == TX_SID_FIRST) 123 { 124 for (i = 0; i < NBBITS_SID; i++) prms[i] = BIT_0; 125 } 126 } 127 /* -> force NO_DATA frame */ 128 if (coding_mode < 0 || coding_mode > 15 || (coding_mode > MRSID && coding_mode < 14)) 129 { 130 coding_mode = 15; 131 } 132 /* mark empty frames between SID updates as NO_DATA frames */ 133 if (coding_mode == MRSID && frame_type == TX_NO_DATA) 134 { 135 coding_mode = 15; 136 } 137 /* set pointer for packed frame, note that we handle data as bytes */ 138 stream_ptr = (UWord8*)dataOut; 139 /* insert table of contents (ToC) byte at the beginning of the packet */ 140 *stream_ptr = toc_byte[coding_mode]; 141 stream_ptr++; 142 temp = 0; 143 /* sort and pack AMR-WB speech or SID bits */ 144 for (i = 1; i < unpacked_size[coding_mode] + 1; i++) 145 { 146 if (prms[sort_ptr[coding_mode][i-1]] == BIT_1) 147 { 148 temp++; 149 } 150 if (i&0x7) 151 { 152 temp <<= 1; 153 } 154 else 155 { 156 *stream_ptr = temp; 157 stream_ptr++; 158 temp = 0; 159 } 160 } 161 /* insert SID type indication and speech mode in case of SID frame */ 162 if (coding_mode == MRSID) 163 { 164 if (frame_type == TX_SID_UPDATE) 165 { 166 temp++; 167 } 168 temp <<= 4; 169 temp += mode & 0x000F; 170 } 171 /* insert unused bits (zeros) at the tail of the last byte */ 172 if (unused_size[coding_mode]) 173 { 174 temp <<= (unused_size[coding_mode] - 1); 175 } 176 *stream_ptr = temp; 177 /* write packed frame into file (1 byte added to cover ToC entry) */ 178 return (1 + packed_size[coding_mode]); 179 } 180 } 181 } 182 183 /*-----------------------------------------------------* 184 * Parm_serial -> convert parameters to serial stream * 185 *-----------------------------------------------------*/ 186 187 void Parm_serial( 188 Word16 value, /* input : parameter value */ 189 Word16 no_of_bits, /* input : number of bits */ 190 Word16 ** prms 191 ) 192 { 193 Word16 i, bit; 194 *prms += no_of_bits; 195 for (i = 0; i < no_of_bits; i++) 196 { 197 bit = (Word16) (value & 0x0001); /* get lsb */ 198 if (bit == 0) 199 *--(*prms) = BIT_0; 200 else 201 *--(*prms) = BIT_1; 202 value >>= 1; 203 } 204 *prms += no_of_bits; 205 return; 206 } 207 208 209 210 211