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 #include "arith_routines.h"
12 #include "settings.h"
13 
14 
15 /*
16  * terminate and return byte stream;
17  * returns the number of bytes in the stream
18  */
WebRtcIsac_EncTerminate(Bitstr * streamdata)19 int WebRtcIsac_EncTerminate(Bitstr *streamdata) /* in-/output struct containing bitstream */
20 {
21   uint8_t *stream_ptr;
22 
23 
24   /* point to the right place in the stream buffer */
25   stream_ptr = streamdata->stream + streamdata->stream_index;
26 
27   /* find minimum length (determined by current interval width) */
28   if ( streamdata->W_upper > 0x01FFFFFF )
29   {
30     streamdata->streamval += 0x01000000;
31     /* add carry to buffer */
32     if (streamdata->streamval < 0x01000000)
33     {
34       /* propagate carry */
35       while ( !(++(*--stream_ptr)) );
36       /* put pointer back to the old value */
37       stream_ptr = streamdata->stream + streamdata->stream_index;
38     }
39     /* write remaining data to bitstream */
40     *stream_ptr++ = (uint8_t) (streamdata->streamval >> 24);
41   }
42   else
43   {
44     streamdata->streamval += 0x00010000;
45     /* add carry to buffer */
46     if (streamdata->streamval < 0x00010000)
47     {
48       /* propagate carry */
49       while ( !(++(*--stream_ptr)) );
50       /* put pointer back to the old value */
51       stream_ptr = streamdata->stream + streamdata->stream_index;
52     }
53     /* write remaining data to bitstream */
54     *stream_ptr++ = (uint8_t) (streamdata->streamval >> 24);
55     *stream_ptr++ = (uint8_t) ((streamdata->streamval >> 16) & 0x00FF);
56   }
57 
58   /* calculate stream length */
59   return (int)(stream_ptr - streamdata->stream);
60 }
61