summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/avc/enc/src/bitstream_io.cpp
blob: d71c32751c048bf9f4ab32bf5eb882fb811228db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* ------------------------------------------------------------------
 * Copyright (C) 1998-2009 PacketVideo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */
#include "avcenc_lib.h"

#define WORD_SIZE 32

/* array for trailing bit pattern as function of number of bits */
/* the first one is unused. */
const static uint8 trailing_bits[9] = {0, 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};

/* ======================================================================== */
/*  Function : BitstreamInit()                                              */
/*  Date     : 11/4/2003                                                    */
/*  Purpose  : Populate bitstream structure with bitstream buffer and size  */
/*             it also initializes internal data                            */
/*  In/out   :                                                              */
/*  Return   : AVCENC_SUCCESS if successed, AVCENC_FAIL if failed.              */
/*  Modified :                                                              */
/* ======================================================================== */
/* |--------|--------|----~~~~~-----|---------|---------|---------|
   ^                                          ^write_pos          ^buf_size
   bitstreamBuffer                  <--------->
                                    current_word

   |-----xxxxxxxxxxxxx|  = current_word 32 or 16 bits
    <---->
     bit_left
 ======================================================================== */

AVCEnc_Status BitstreamEncInit(AVCEncBitstream *stream, uint8 *buffer, int buf_size,
                               uint8 *overrunBuffer, int oBSize)
{
    if (stream == NULL || buffer == NULL || buf_size <= 0)
    {
        return AVCENC_BITSTREAM_INIT_FAIL;
    }

    stream->bitstreamBuffer = buffer;

    stream->buf_size = buf_size;

    stream->write_pos = 0;

    stream->count_zeros = 0;

    stream->current_word = 0;

    stream->bit_left = WORD_SIZE;

    stream->overrunBuffer = overrunBuffer;

    stream->oBSize = oBSize;

    return AVCENC_SUCCESS;
}

/* ======================================================================== */
/*  Function : AVCBitstreamSaveWord()                                           */
/*  Date     : 3/29/2004                                                    */
/*  Purpose  : Save the current_word into the buffer, byte-swap, and        */
/*              add emulation prevention insertion.                         */
/*  In/out   :                                                              */
/*  Return   : AVCENC_SUCCESS if successed, AVCENC_WRITE_FAIL if buffer is  */
/*              full.                                                       */
/*  Modified :                                                              */
/* ======================================================================== */
AVCEnc_Status AVCBitstreamSaveWord(AVCEncBitstream *stream)
{
    int num_bits;
    uint8 *write_pnt, byte;
    uint current_word;

    /* check number of bytes in current_word, must always be byte-aligned!!!! */
    num_bits = WORD_SIZE - stream->bit_left; /* must be multiple of 8 !!*/

    if (stream->buf_size - stream->write_pos <= (num_bits >> 3) + 2) /* 2 more bytes for possible EPBS */
    {
        if (AVCENC_SUCCESS != AVCBitstreamUseOverrunBuffer(stream, (num_bits >> 3) + 2))
        {
            return AVCENC_BITSTREAM_BUFFER_FULL;
        }
    }

    /* write word, byte-by-byte */
    write_pnt = stream->bitstreamBuffer + stream->write_pos;
    current_word = stream->current_word;
    while (num_bits) /* no need to check stream->buf_size and stream->write_pos, taken care already */
    {
        num_bits -= 8;
        byte = (current_word >> num_bits) & 0xFF;
        if (stream->count_zeros == 2)
        {   /* for num_bits = 32, this can add 2 more bytes extra for EPBS */
            if (byte <= 3)
            {
                *write_pnt++ = 0x3;
                stream->write_pos++;
                stream->count_zeros = 0;
            }
        }
        if (byte != 0)
        {
            *write_pnt++ = byte;
            stream->write_pos++;
            stream->count_zeros = 0;
        }
        else
        {
            stream->count_zeros++;
            *write_pnt++ = byte;
            stream->write_pos++;
        }
    }

    /* reset current_word and bit_left */
    stream->current_word = 0;
    stream->bit_left = WORD_SIZE;

    return AVCENC_SUCCESS;
}

/* ======================================================================== */
/*  Function : BitstreamWriteBits()                                         */
/*  Date     : 3/29/2004                                                    */
/*  Purpose  : Write up to machine word.                                    */
/*  In/out   : Unused bits in 'code' must be all zeros.                     */
/*  Return   : AVCENC_SUCCESS if successed, AVCENC_WRITE_FAIL if buffer is  */
/*              full.                                                       */
/*  Modified :                                                              */
/* ======================================================================== */
AVCEnc_Status BitstreamWriteBits(AVCEncBitstream *stream, int nBits, uint code)
{
    AVCEnc_Status status = AVCENC_SUCCESS;
    int bit_left = stream->bit_left;
    uint current_word = stream->current_word;

    //DEBUG_LOG(userData,AVC_LOGTYPE_INFO,"BitstreamWriteBits",nBits,-1);

    if (nBits > WORD_SIZE) /* has to be taken care of specially */
    {
        return AVCENC_FAIL; /* for now */
        /* otherwise, break it down to 2 write of less than 16 bits at a time. */
    }

    if (nBits <= bit_left) /* more bits left in current_word */
    {
        stream->current_word = (current_word << nBits) | code;
        stream->bit_left -= nBits;
        if (stream->bit_left == 0) /* prepare for the next word */
        {
            status = AVCBitstreamSaveWord(stream);
            return status;
        }
    }
    else
    {
        stream->current_word = (current_word << bit_left) | (code >> (nBits - bit_left));

        nBits -= bit_left;

        stream->bit_left = 0;

        status = AVCBitstreamSaveWord(stream); /* save current word */

        stream->bit_left = WORD_SIZE - nBits;

        stream->current_word = code; /* no extra masking for code, must be handled before saving */
    }

    return status;
}


/* ======================================================================== */
/*  Function : BitstreamWrite1Bit()                                         */
/*  Date     : 3/30/2004                                                    */
/*  Purpose  : Write 1 bit                                                  */
/*  In/out   : Unused bits in 'code' must be all zeros.                     */
/*  Return   : AVCENC_SUCCESS if successed, AVCENC_WRITE_FAIL if buffer is  */
/*              full.                                                       */
/*  Modified :                                                              */
/* ======================================================================== */
AVCEnc_Status BitstreamWrite1Bit(AVCEncBitstream *stream, uint code)
{
    AVCEnc_Status status;
    uint current_word = stream->current_word;

    //DEBUG_LOG(userData,AVC_LOGTYPE_INFO,"BitstreamWrite1Bit",code,-1);

    //if(1 <= bit_left) /* more bits left in current_word */
    /* we can assume that there always be positive bit_left in the current word */
    stream->current_word = (current_word << 1) | code;
    stream->bit_left--;
    if (stream->bit_left == 0) /* prepare for the next word */
    {
        status = AVCBitstreamSaveWord(stream);
        return status;
    }

    return AVCENC_SUCCESS;
}


/* ======================================================================== */
/*  Function : BitstreamTrailingBits()                                      */
/*  Date     : 3/31/2004                                                    */
/*  Purpose  : Add trailing bits and report the final EBSP size.            */
/*  In/out   :                                                              */
/*  Return   : AVCENC_SUCCESS if successed, AVCENC_WRITE_FAIL if buffer is  */
/*              full.                                                       */
/*  Modified :                                                              */
/* ======================================================================== */
AVCEnc_Status BitstreamTrailingBits(AVCEncBitstream *bitstream, uint *nal_size)
{
    (void)(nal_size);

    AVCEnc_Status status;
    int bit_left = bitstream->bit_left;

    bit_left &= 0x7; /* modulo by 8 */
    if (bit_left == 0) bit_left = 8;
    /* bitstream->bit_left == 0 cannot happen here since it would have been Saved already */

    status = BitstreamWriteBits(bitstream, bit_left, trailing_bits[bit_left]);

    if (status != AVCENC_SUCCESS)
    {
        return status;
    }

    /* if it's not saved, save it. */
    //if(bitstream->bit_left<(WORD_SIZE<<3)) /* in fact, no need to check */
    {
        status = AVCBitstreamSaveWord(bitstream);
    }

    return status;
}

/* check whether it's byte-aligned */
bool byte_aligned(AVCEncBitstream *stream)
{
    if (stream->bit_left % 8)
        return false;
    else
        return true;
}


/* determine whether overrun buffer can be used or not */
AVCEnc_Status AVCBitstreamUseOverrunBuffer(AVCEncBitstream* stream, int numExtraBytes)
{
    AVCEncObject *encvid = (AVCEncObject*)stream->encvid;

    if (stream->overrunBuffer != NULL) // overrunBuffer is set
    {
        if (stream->bitstreamBuffer != stream->overrunBuffer) // not already used
        {
            if (stream->write_pos + numExtraBytes >= stream->oBSize)
            {
                stream->oBSize = stream->write_pos + numExtraBytes + 100;
                stream->oBSize &= (~0x3); // make it multiple of 4

                // allocate new overrun Buffer
                if (encvid->overrunBuffer)
                {
                    encvid->avcHandle->CBAVC_Free(encvid->avcHandle->userData,
                                                  encvid->overrunBuffer);
                }

                encvid->oBSize = stream->oBSize;
                encvid->overrunBuffer = (uint8*) encvid->avcHandle->CBAVC_Malloc(encvid->avcHandle->userData,
                                        stream->oBSize, DEFAULT_ATTR);

                stream->overrunBuffer = encvid->overrunBuffer;
                if (stream->overrunBuffer == NULL)
                {
                    return AVCENC_FAIL;
                }
            }

            // copy everything to overrun buffer and start using it.
            memcpy(stream->overrunBuffer, stream->bitstreamBuffer, stream->write_pos);
            stream->bitstreamBuffer = stream->overrunBuffer;
            stream->buf_size = stream->oBSize;
        }
        else // overrun buffer is already used
        {
            stream->oBSize = stream->write_pos + numExtraBytes + 100;
            stream->oBSize &= (~0x3); // make it multiple of 4

            // allocate new overrun buffer
            encvid->oBSize = stream->oBSize;
            encvid->overrunBuffer = (uint8*) encvid->avcHandle->CBAVC_Malloc(encvid->avcHandle->userData,
                                    stream->oBSize, DEFAULT_ATTR);

            if (encvid->overrunBuffer == NULL)
            {
                return AVCENC_FAIL;
            }


            // copy from the old buffer to new buffer
            memcpy(encvid->overrunBuffer, stream->overrunBuffer, stream->write_pos);
            // free old buffer
            encvid->avcHandle->CBAVC_Free(encvid->avcHandle->userData,
                                          stream->overrunBuffer);

            // assign pointer to new buffer
            stream->overrunBuffer = encvid->overrunBuffer;
            stream->bitstreamBuffer = stream->overrunBuffer;
            stream->buf_size = stream->oBSize;
        }

        return AVCENC_SUCCESS;
    }
    else // overrunBuffer is not enable.
    {
        return AVCENC_FAIL;
    }

}