Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / codecs / isac / fix / source / isacfix.c
index 118fd2f..5e8fd4f 100644 (file)
@@ -35,6 +35,19 @@ Time2Spec WebRtcIsacfix_Time2Spec;
 MatrixProduct1 WebRtcIsacfix_MatrixProduct1;
 MatrixProduct2 WebRtcIsacfix_MatrixProduct2;
 
+/* This method assumes that |stream_size_bytes| is in valid range,
+ * i.e. >= 0 && <=  STREAM_MAXW16_60MS
+ */
+static void InitializeDecoderBitstream(int stream_size_bytes,
+                                       Bitstr_dec* bitstream) {
+  bitstream->W_upper = 0xFFFFFFFF;
+  bitstream->streamval = 0;
+  bitstream->stream_index = 0;
+  bitstream->full = 1;
+  bitstream->stream_size = (stream_size_bytes + 1) >> 1;
+  memset(bitstream->stream, 0, sizeof(bitstream->stream));
+}
+
 /**************************************************************************
  * WebRtcIsacfix_AssignSize(...)
  *
@@ -332,6 +345,35 @@ int16_t WebRtcIsacfix_EncoderInit(ISACFIX_MainStruct *ISAC_main_inst,
   return statusInit;
 }
 
+/* Read the given number of bytes of big-endian 16-bit integers from |src| and
+   write them to |dest| in host endian. If |nbytes| is odd, the number of
+   output elements is rounded up, and the least significant byte of the last
+   element is set to 0. */
+static void read_be16(const uint8_t* src, size_t nbytes, uint16_t* dest) {
+  size_t i;
+  for (i = 0; i < nbytes / 2; ++i)
+    dest[i] = src[2 * i] << 8 | src[2 * i + 1];
+  if (nbytes % 2 == 1)
+    dest[nbytes / 2] = src[nbytes - 1] << 8;
+}
+
+/* Read the given number of bytes of host-endian 16-bit integers from |src| and
+   write them to |dest| in big endian. If |nbytes| is odd, the number of source
+   elements is rounded up (but only the most significant byte of the last
+   element is used), and the number of output bytes written will be
+   nbytes + 1. */
+static void write_be16(const uint16_t* src, size_t nbytes, uint8_t* dest) {
+  size_t i;
+  for (i = 0; i < nbytes / 2; ++i) {
+    dest[2 * i] = src[i] >> 8;
+    dest[2 * i + 1] = src[i];
+  }
+  if (nbytes % 2 == 1) {
+    dest[nbytes - 1] = src[nbytes / 2] >> 8;
+    dest[nbytes] = 0;
+  }
+}
+
 /****************************************************************************
  * WebRtcIsacfix_Encode(...)
  *
@@ -359,10 +401,7 @@ int16_t WebRtcIsacfix_Encode(ISACFIX_MainStruct *ISAC_main_inst,
                              uint8_t* encoded)
 {
   ISACFIX_SubStruct *ISAC_inst;
-  int16_t stream_len, stream_len_even;
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  int k;
-#endif
+  int16_t stream_len;
 
   /* typecast pointer to rela structure */
   ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
@@ -383,28 +422,7 @@ int16_t WebRtcIsacfix_Encode(ISACFIX_MainStruct *ISAC_main_inst,
     return -1;
   }
 
-  /* One would think that only even stream lengths would make sense here. We do
-     in fact observe odd lengths, however, and in those cases we copy an extra
-     byte. */
-  stream_len_even = stream_len % 2 == 0 ? stream_len : stream_len + 1;
-
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  /* The encoded data vector is supposesd to be big-endian, but our internal
-     representation is little-endian. So byteswap. */
-  for (k = 0; k < stream_len_even / 2; ++k) {
-    uint16_t s = ISAC_inst->ISACenc_obj.bitstr_obj.stream[k];
-    /* In big-endian, we have... */
-    encoded[2 * k] = s >> 8;  /* ...most significant byte at low address... */
-    encoded[2 * k + 1] = s;  /* ...least significant byte at high address. */
-  }
-#else
-  /* The encoded data vector and our internal representation are both
-     big-endian. */
-  memcpy(encoded, ISAC_inst->ISACenc_obj.bitstr_obj.stream, stream_len_even);
-#endif
-
-
-
+  write_be16(ISAC_inst->ISACenc_obj.bitstr_obj.stream, stream_len, encoded);
   return stream_len;
 
 }
@@ -481,20 +499,9 @@ int16_t WebRtcIsacfix_EncodeNb(ISACFIX_MainStruct *ISAC_main_inst,
     return -1;
   }
 
-
-  /* convert from bytes to int16_t */
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  for (k=0;k<(stream_len+1)>>1;k++) {
-    encoded[k] = (int16_t)(((uint16_t)(ISAC_inst->ISACenc_obj.bitstr_obj).stream[k] >> 8)
-                                 | (((ISAC_inst->ISACenc_obj.bitstr_obj).stream[k] & 0x00FF) << 8));
-  }
-
-#else
-  WEBRTC_SPL_MEMCPY_W16(encoded, (ISAC_inst->ISACenc_obj.bitstr_obj).stream, (stream_len + 1)>>1);
-#endif
-
-
-
+  write_be16(ISAC_inst->ISACenc_obj.bitstr_obj.stream,
+             stream_len,
+             (uint8_t*)encoded);
   return stream_len;
 }
 #endif  /* WEBRTC_ISAC_FIX_NB_CALLS_ENABLED */
@@ -522,13 +529,10 @@ int16_t WebRtcIsacfix_EncodeNb(ISACFIX_MainStruct *ISAC_main_inst,
 int16_t WebRtcIsacfix_GetNewBitStream(ISACFIX_MainStruct *ISAC_main_inst,
                                       int16_t      bweIndex,
                                       float              scale,
-                                      int16_t        *encoded)
+                                      uint8_t* encoded)
 {
   ISACFIX_SubStruct *ISAC_inst;
   int16_t stream_len;
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  int k;
-#endif
 
   /* typecast pointer to rela structure */
   ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
@@ -548,18 +552,8 @@ int16_t WebRtcIsacfix_GetNewBitStream(ISACFIX_MainStruct *ISAC_main_inst,
     return -1;
   }
 
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  for (k=0;k<(stream_len+1)>>1;k++) {
-    encoded[k] = (int16_t)( ( (uint16_t)(ISAC_inst->ISACenc_obj.bitstr_obj).stream[k] >> 8 )
-                                  | (((ISAC_inst->ISACenc_obj.bitstr_obj).stream[k] & 0x00FF) << 8));
-  }
-
-#else
-  WEBRTC_SPL_MEMCPY_W16(encoded, (ISAC_inst->ISACenc_obj.bitstr_obj).stream, (stream_len + 1)>>1);
-#endif
-
+  write_be16(ISAC_inst->ISACenc_obj.bitstr_obj.stream, stream_len, encoded);
   return stream_len;
-
 }
 
 
@@ -621,17 +615,15 @@ int16_t WebRtcIsacfix_DecoderInit(ISACFIX_MainStruct *ISAC_main_inst)
  */
 
 int16_t WebRtcIsacfix_UpdateBwEstimate1(ISACFIX_MainStruct *ISAC_main_inst,
-                                        const uint16_t   *encoded,
+                                        const uint8_t* encoded,
                                         int32_t          packet_size,
                                         uint16_t         rtp_seq_number,
                                         uint32_t         arr_ts)
 {
   ISACFIX_SubStruct *ISAC_inst;
   Bitstr_dec streamdata;
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  int k;
-#endif
   int16_t err;
+  const int kRequiredEncodedLenBytes = 10;
 
   /* typecast pointer to real structure */
   ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
@@ -653,18 +645,9 @@ int16_t WebRtcIsacfix_UpdateBwEstimate1(ISACFIX_MainStruct *ISAC_main_inst,
     return (-1);
   }
 
-  streamdata.W_upper = 0xFFFFFFFF;
-  streamdata.streamval = 0;
-  streamdata.stream_index = 0;
-  streamdata.full = 1;
+  InitializeDecoderBitstream(packet_size, &streamdata);
 
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  for (k=0; k<5; k++) {
-    streamdata.stream[k] = (uint16_t) (((uint16_t)encoded[k] >> 8)|((encoded[k] & 0xFF)<<8));
-  }
-#else
-  memcpy(streamdata.stream, encoded, 5);
-#endif
+  read_be16(encoded, kRequiredEncodedLenBytes, streamdata.stream);
 
   err = WebRtcIsacfix_EstimateBandwidth(&ISAC_inst->bwestimator_obj,
                                         &streamdata,
@@ -704,7 +687,7 @@ int16_t WebRtcIsacfix_UpdateBwEstimate1(ISACFIX_MainStruct *ISAC_main_inst,
  */
 
 int16_t WebRtcIsacfix_UpdateBwEstimate(ISACFIX_MainStruct *ISAC_main_inst,
-                                       const uint16_t   *encoded,
+                                       const uint8_t* encoded,
                                        int32_t          packet_size,
                                        uint16_t         rtp_seq_number,
                                        uint32_t         send_ts,
@@ -712,10 +695,8 @@ int16_t WebRtcIsacfix_UpdateBwEstimate(ISACFIX_MainStruct *ISAC_main_inst,
 {
   ISACFIX_SubStruct *ISAC_inst;
   Bitstr_dec streamdata;
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  int k;
-#endif
   int16_t err;
+  const int kRequiredEncodedLenBytes = 10;
 
   /* typecast pointer to real structure */
   ISAC_inst = (ISACFIX_SubStruct *)ISAC_main_inst;
@@ -725,6 +706,9 @@ int16_t WebRtcIsacfix_UpdateBwEstimate(ISACFIX_MainStruct *ISAC_main_inst,
     /* return error code if the packet length is null  or less */
     ISAC_inst->errorcode = ISAC_EMPTY_PACKET;
     return -1;
+  } else if (packet_size < kRequiredEncodedLenBytes) {
+    ISAC_inst->errorcode = ISAC_PACKET_TOO_SHORT;
+    return -1;
   } else if (packet_size > (STREAM_MAXW16<<1)) {
     /* return error code if length of stream is too long */
     ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH;
@@ -737,18 +721,9 @@ int16_t WebRtcIsacfix_UpdateBwEstimate(ISACFIX_MainStruct *ISAC_main_inst,
     return (-1);
   }
 
-  streamdata.W_upper = 0xFFFFFFFF;
-  streamdata.streamval = 0;
-  streamdata.stream_index = 0;
-  streamdata.full = 1;
+  InitializeDecoderBitstream(packet_size, &streamdata);
 
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  for (k=0; k<5; k++) {
-    streamdata.stream[k] = (uint16_t) ((encoded[k] >> 8)|((encoded[k] & 0xFF)<<8));
-  }
-#else
-  memcpy(streamdata.stream, encoded, 5);
-#endif
+  read_be16(encoded, kRequiredEncodedLenBytes, streamdata.stream);
 
   err = WebRtcIsacfix_EstimateBandwidth(&ISAC_inst->bwestimator_obj,
                                         &streamdata,
@@ -789,7 +764,7 @@ int16_t WebRtcIsacfix_UpdateBwEstimate(ISACFIX_MainStruct *ISAC_main_inst,
 
 
 int16_t WebRtcIsacfix_Decode(ISACFIX_MainStruct *ISAC_main_inst,
-                             const uint16_t   *encoded,
+                             const uint8_t* encoded,
                              int16_t          len,
                              int16_t          *decoded,
                              int16_t     *speechType)
@@ -798,9 +773,6 @@ int16_t WebRtcIsacfix_Decode(ISACFIX_MainStruct *ISAC_main_inst,
   /* number of samples (480 or 960), output from decoder */
   /* that were actually used in the encoder/decoder (determined on the fly) */
   int16_t     number_of_samples;
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  int k;
-#endif
   int16_t declen = 0;
 
   /* typecast pointer to real structure */
@@ -823,16 +795,9 @@ int16_t WebRtcIsacfix_Decode(ISACFIX_MainStruct *ISAC_main_inst,
     return -1;
   }
 
-  ISAC_inst->ISACdec_obj.bitstr_obj.stream_size = (len + 1) >> 1;
+  InitializeDecoderBitstream(len, &ISAC_inst->ISACdec_obj.bitstr_obj);
 
-  /* convert bitstream from int16_t to bytes */
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  for (k=0; k<(len>>1); k++) {
-    (ISAC_inst->ISACdec_obj.bitstr_obj).stream[k] = (uint16_t) ((encoded[k] >> 8)|((encoded[k] & 0xFF)<<8));
-  }
-  if (len & 0x0001)
-    (ISAC_inst->ISACdec_obj.bitstr_obj).stream[k] = (uint16_t) ((encoded[k] & 0xFF)<<8);
-#endif
+  read_be16(encoded, len, ISAC_inst->ISACdec_obj.bitstr_obj.stream);
 
   /* added for NetEq purposes (VAD/DTX related) */
   *speechType=1;
@@ -901,9 +866,6 @@ int16_t WebRtcIsacfix_DecodeNb(ISACFIX_MainStruct *ISAC_main_inst,
   /* twice the number of samples (480 or 960), output from decoder */
   /* that were actually used in the encoder/decoder (determined on the fly) */
   int16_t     number_of_samples;
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  int k;
-#endif
   int16_t declen = 0;
   int16_t dummy[FRAMESAMPLES/2];
 
@@ -917,23 +879,19 @@ int16_t WebRtcIsacfix_DecodeNb(ISACFIX_MainStruct *ISAC_main_inst,
     return (-1);
   }
 
-  if (len == 0)
-  {  /* return error code if the packet length is null */
-
+  if (len <= 0) {
+    /* return error code if the packet length is null  or less */
     ISAC_inst->errorcode = ISAC_EMPTY_PACKET;
     return -1;
+  } else if (len > (STREAM_MAXW16<<1)) {
+    /* return error code if length of stream is too long */
+    ISAC_inst->errorcode = ISAC_LENGTH_MISMATCH;
+    return -1;
   }
 
-  ISAC_inst->ISACdec_obj.bitstr_obj.stream_size = (len + 1) >> 1;
+  InitializeDecoderBitstream(len, &ISAC_inst->ISACdec_obj.bitstr_obj);
 
-  /* convert bitstream from int16_t to bytes */
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  for (k=0; k<(len>>1); k++) {
-    (ISAC_inst->ISACdec_obj.bitstr_obj).stream[k] = (uint16_t) ((encoded[k] >> 8)|((encoded[k] & 0xFF)<<8));
-  }
-  if (len & 0x0001)
-    (ISAC_inst->ISACdec_obj.bitstr_obj).stream[k] = (uint16_t) ((encoded[k] & 0xFF)<<8);
-#endif
+  read_be16(encoded, len, ISAC_inst->ISACdec_obj.bitstr_obj.stream);
 
   /* added for NetEq purposes (VAD/DTX related) */
   *speechType=1;
@@ -1295,27 +1253,21 @@ int16_t WebRtcIsacfix_UpdateUplinkBw(ISACFIX_MainStruct* ISAC_main_inst,
  *
  */
 
-int16_t WebRtcIsacfix_ReadFrameLen(const int16_t* encoded,
+int16_t WebRtcIsacfix_ReadFrameLen(const uint8_t* encoded,
+                                   int encoded_len_bytes,
                                    int16_t* frameLength)
 {
   Bitstr_dec streamdata;
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  int k;
-#endif
   int16_t err;
+  const int kRequiredEncodedLenBytes = 10;
 
-  streamdata.W_upper = 0xFFFFFFFF;
-  streamdata.streamval = 0;
-  streamdata.stream_index = 0;
-  streamdata.full = 1;
-
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  for (k=0; k<5; k++) {
-    streamdata.stream[k] = (uint16_t) (((uint16_t)encoded[k] >> 8)|((encoded[k] & 0xFF)<<8));
+  if (encoded_len_bytes < kRequiredEncodedLenBytes) {
+    return -1;
   }
-#else
-  memcpy(streamdata.stream, encoded, 5);
-#endif
+
+  InitializeDecoderBitstream(encoded_len_bytes, &streamdata);
+
+  read_be16(encoded, kRequiredEncodedLenBytes, streamdata.stream);
 
   /* decode frame length */
   err = WebRtcIsacfix_DecodeFrameLen(&streamdata, frameLength);
@@ -1340,27 +1292,21 @@ int16_t WebRtcIsacfix_ReadFrameLen(const int16_t* encoded,
  *
  */
 
-int16_t WebRtcIsacfix_ReadBwIndex(const int16_t* encoded,
+int16_t WebRtcIsacfix_ReadBwIndex(const uint8_t* encoded,
+                                  int encoded_len_bytes,
                                   int16_t* rateIndex)
 {
   Bitstr_dec streamdata;
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  int k;
-#endif
   int16_t err;
+  const int kRequiredEncodedLenBytes = 10;
 
-  streamdata.W_upper = 0xFFFFFFFF;
-  streamdata.streamval = 0;
-  streamdata.stream_index = 0;
-  streamdata.full = 1;
-
-#ifndef WEBRTC_ARCH_BIG_ENDIAN
-  for (k=0; k<5; k++) {
-    streamdata.stream[k] = (uint16_t) (((uint16_t)encoded[k] >> 8)|((encoded[k] & 0xFF)<<8));
+  if (encoded_len_bytes < kRequiredEncodedLenBytes) {
+    return -1;
   }
-#else
-  memcpy(streamdata.stream, encoded, 5);
-#endif
+
+  InitializeDecoderBitstream(encoded_len_bytes, &streamdata);
+
+  read_be16(encoded, kRequiredEncodedLenBytes, streamdata.stream);
 
   /* decode frame length, needed to get to the rateIndex in the bitstream */
   err = WebRtcIsacfix_DecodeFrameLen(&streamdata, rateIndex);