summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/avc/enc/src/vlc_encode.cpp
blob: 222e709b301b0d222eaa087d1639250141bb1264 (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
/* ------------------------------------------------------------------
 * 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"

/**
See algorithm in subclause 9.1, Table 9-1, Table 9-2. */
AVCEnc_Status ue_v(AVCEncBitstream *bitstream, uint codeNum)
{
    if (AVCENC_SUCCESS != SetEGBitstring(bitstream, codeNum))
        return AVCENC_FAIL;

    return AVCENC_SUCCESS;
}

/**
See subclause 9.1.1, Table 9-3 */
AVCEnc_Status  se_v(AVCEncBitstream *bitstream, int value)
{
    uint codeNum;
    AVCEnc_Status status;

    if (value <= 0)
    {
        codeNum = -value * 2;
    }
    else
    {
        codeNum = value * 2 - 1;
    }

    status = ue_v(bitstream, codeNum);

    return status;
}

AVCEnc_Status te_v(AVCEncBitstream *bitstream, uint value, uint range)
{
    AVCEnc_Status status;

    if (range > 1)
    {
        return ue_v(bitstream, value);
    }
    else
    {
        status = BitstreamWrite1Bit(bitstream, 1 - value);
        return status;
    }
}

/**
See subclause 9.1, Table 9-1, 9-2. */
// compute leadingZeros and inforbits
//codeNum = (1<<leadingZeros)-1+infobits;
AVCEnc_Status SetEGBitstring(AVCEncBitstream *bitstream, uint codeNum)
{
    AVCEnc_Status status;
    int leadingZeros;
    int infobits;

    if (!codeNum)
    {
        status = BitstreamWrite1Bit(bitstream, 1);
        return status;
    }

    /* calculate leadingZeros and infobits */
    leadingZeros = 1;
    while ((uint)(1 << leadingZeros) < codeNum + 2)
    {
        leadingZeros++;
    }
    leadingZeros--;
    infobits = codeNum - (1 << leadingZeros) + 1;

    status = BitstreamWriteBits(bitstream, leadingZeros, 0);
    infobits |= (1 << leadingZeros);
    status = BitstreamWriteBits(bitstream, leadingZeros + 1, infobits);
    return status;
}

/* see Table 9-4 assignment of codeNum to values of coded_block_pattern. */
const static uint8 MapCBP2code[48][2] =
{
    {3, 0}, {29, 2}, {30, 3}, {17, 7}, {31, 4}, {18, 8}, {37, 17}, {8, 13}, {32, 5}, {38, 18}, {19, 9}, {9, 14},
    {20, 10}, {10, 15}, {11, 16}, {2, 11}, {16, 1}, {33, 32}, {34, 33}, {21, 36}, {35, 34}, {22, 37}, {39, 44}, {4, 40},
    {36, 35}, {40, 45}, {23, 38}, {5, 41}, {24, 39}, {6, 42}, {7, 43}, {1, 19}, {41, 6}, {42, 24}, {43, 25}, {25, 20},
    {44, 26}, {26, 21}, {46, 46}, {12, 28}, {45, 27}, {47, 47}, {27, 22}, {13, 29}, {28, 23}, {14, 30}, {15, 31}, {0, 12}
};

AVCEnc_Status EncodeCBP(AVCMacroblock *currMB, AVCEncBitstream *stream)
{
    AVCEnc_Status status;
    uint codeNum;

    if (currMB->mbMode == AVC_I4)
    {
        codeNum = MapCBP2code[currMB->CBP][0];
    }
    else
    {
        codeNum = MapCBP2code[currMB->CBP][1];
    }

    status = ue_v(stream, codeNum);

    return status;
}

AVCEnc_Status ce_TotalCoeffTrailingOnes(AVCEncBitstream *stream, int TrailingOnes, int TotalCoeff, int nC)
{
    const static uint8 totCoeffTrailOne[3][4][17][2] =
    {
        {   // 0702
            {{1, 1}, {6, 5}, {8, 7}, {9, 7}, {10, 7}, {11, 7}, {13, 15}, {13, 11}, {13, 8}, {14, 15}, {14, 11}, {15, 15}, {15, 11}, {16, 15}, {16, 11}, {16, 7}, {16, 4}},
            {{0, 0}, {2, 1}, {6, 4}, {8, 6}, {9, 6}, {10, 6}, {11, 6}, {13, 14}, {13, 10}, {14, 14}, {14, 10}, {15, 14}, {15, 10}, {15, 1}, {16, 14}, {16, 10}, {16, 6}},
            {{0, 0}, {0, 0}, {3, 1}, {7, 5}, {8, 5}, {9, 5}, {10, 5}, {11, 5}, {13, 13}, {13, 9}, {14, 13}, {14, 9}, {15, 13}, {15, 9}, {16, 13}, {16, 9}, {16, 5}},
            {{0, 0}, {0, 0}, {0, 0}, {5, 3}, {6, 3}, {7, 4}, {8, 4}, {9, 4}, {10, 4}, {11, 4}, {13, 12}, {14, 12}, {14, 8}, {15, 12}, {15, 8}, {16, 12}, {16, 8}},
        },
        {
            {{2, 3}, {6, 11}, {6, 7}, {7, 7}, {8, 7}, {8, 4}, {9, 7}, {11, 15}, {11, 11}, {12, 15}, {12, 11}, {12, 8}, {13, 15}, {13, 11}, {13, 7}, {14, 9}, {14, 7}},
            {{0, 0}, {2, 2}, {5, 7}, {6, 10}, {6, 6}, {7, 6}, {8, 6}, {9, 6}, {11, 14}, {11, 10}, {12, 14}, {12, 10}, {13, 14}, {13, 10}, {14, 11}, {14, 8}, {14, 6}},
            {{0, 0}, {0, 0}, {3, 3}, {6, 9}, {6, 5}, {7, 5}, {8, 5}, {9, 5}, {11, 13}, {11, 9}, {12, 13}, {12, 9}, {13, 13}, {13, 9}, {13, 6}, {14, 10}, {14, 5}},
            {{0, 0}, {0, 0}, {0, 0}, {4, 5}, {4, 4}, {5, 6}, {6, 8}, {6, 4}, {7, 4}, {9, 4}, {11, 12}, {11, 8}, {12, 12}, {13, 12}, {13, 8}, {13, 1}, {14, 4}},
        },
        {
            {{4, 15}, {6, 15}, {6, 11}, {6, 8}, {7, 15}, {7, 11}, {7, 9}, {7, 8}, {8, 15}, {8, 11}, {9, 15}, {9, 11}, {9, 8}, {10, 13}, {10, 9}, {10, 5}, {10, 1}},
            {{0, 0}, {4, 14}, {5, 15}, {5, 12}, {5, 10}, {5, 8}, {6, 14}, {6, 10}, {7, 14}, {8, 14}, {8, 10}, {9, 14}, {9, 10}, {9, 7}, {10, 12}, {10, 8}, {10, 4}},
            {{0, 0}, {0, 0}, {4, 13}, {5, 14}, {5, 11}, {5, 9}, {6, 13}, {6, 9}, {7, 13}, {7, 10}, {8, 13}, {8, 9}, {9, 13}, {9, 9}, {10, 11}, {10, 7}, {10, 3}},
            {{0, 0}, {0, 0}, {0, 0}, {4, 12}, {4, 11}, {4, 10}, {4, 9}, {4, 8}, {5, 13}, {6, 12}, {7, 12}, {8, 12}, {8, 8}, {9, 12}, {10, 10}, {10, 6}, {10, 2}}
        }
    };


    AVCEnc_Status status = AVCENC_SUCCESS;
    uint code, len;
    int vlcnum;

    if (TrailingOnes > 3)
    {
        return AVCENC_TRAILINGONES_FAIL;
    }

    if (nC >= 8)
    {
        if (TotalCoeff)
        {
            code = ((TotalCoeff - 1) << 2) | (TrailingOnes);
        }
        else
        {
            code = 3;
        }
        status = BitstreamWriteBits(stream, 6, code);
    }
    else
    {
        if (nC < 2)
        {
            vlcnum = 0;
        }
        else if (nC < 4)
        {
            vlcnum = 1;
        }
        else
        {
            vlcnum = 2;
        }

        len = totCoeffTrailOne[vlcnum][TrailingOnes][TotalCoeff][0];
        code = totCoeffTrailOne[vlcnum][TrailingOnes][TotalCoeff][1];
        status = BitstreamWriteBits(stream, len, code);
    }

    return status;
}

AVCEnc_Status ce_TotalCoeffTrailingOnesChromaDC(AVCEncBitstream *stream, int TrailingOnes, int TotalCoeff)
{
    const static uint8 totCoeffTrailOneChrom[4][5][2] =
    {
        { {2, 1}, {6, 7}, {6, 4}, {6, 3}, {6, 2}},
        { {0, 0}, {1, 1}, {6, 6}, {7, 3}, {8, 3}},
        { {0, 0}, {0, 0}, {3, 1}, {7, 2}, {8, 2}},
        { {0, 0}, {0, 0}, {0, 0}, {6, 5}, {7, 0}},
    };

    AVCEnc_Status status = AVCENC_SUCCESS;
    uint code, len;

    len = totCoeffTrailOneChrom[TrailingOnes][TotalCoeff][0];
    code = totCoeffTrailOneChrom[TrailingOnes][TotalCoeff][1];
    status = BitstreamWriteBits(stream, len, code);

    return status;
}

/* see Table 9-7 and 9-8 */
AVCEnc_Status ce_TotalZeros(AVCEncBitstream *stream, int total_zeros, int TotalCoeff)
{
    const static uint8 lenTotalZeros[15][16] =
    {
        { 1, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9},
        { 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6},
        { 4, 3, 3, 3, 4, 4, 3, 3, 4, 5, 5, 6, 5, 6},
        { 5, 3, 4, 4, 3, 3, 3, 4, 3, 4, 5, 5, 5},
        { 4, 4, 4, 3, 3, 3, 3, 3, 4, 5, 4, 5},
        { 6, 5, 3, 3, 3, 3, 3, 3, 4, 3, 6},
        { 6, 5, 3, 3, 3, 2, 3, 4, 3, 6},
        { 6, 4, 5, 3, 2, 2, 3, 3, 6},
        { 6, 6, 4, 2, 2, 3, 2, 5},
        { 5, 5, 3, 2, 2, 2, 4},
        { 4, 4, 3, 3, 1, 3},
        { 4, 4, 2, 1, 3},
        { 3, 3, 1, 2},
        { 2, 2, 1},
        { 1, 1},
    };

    const static uint8 codTotalZeros[15][16] =
    {
        {1, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 1},
        {7, 6, 5, 4, 3, 5, 4, 3, 2, 3, 2, 3, 2, 1, 0},
        {5, 7, 6, 5, 4, 3, 4, 3, 2, 3, 2, 1, 1, 0},
        {3, 7, 5, 4, 6, 5, 4, 3, 3, 2, 2, 1, 0},
        {5, 4, 3, 7, 6, 5, 4, 3, 2, 1, 1, 0},
        {1, 1, 7, 6, 5, 4, 3, 2, 1, 1, 0},
        {1, 1, 5, 4, 3, 3, 2, 1, 1, 0},
        {1, 1, 1, 3, 3, 2, 2, 1, 0},
        {1, 0, 1, 3, 2, 1, 1, 1, },
        {1, 0, 1, 3, 2, 1, 1, },
        {0, 1, 1, 2, 1, 3},
        {0, 1, 1, 1, 1},
        {0, 1, 1, 1},
        {0, 1, 1},
        {0, 1},
    };
    int len, code;
    AVCEnc_Status status;

    len = lenTotalZeros[TotalCoeff-1][total_zeros];
    code = codTotalZeros[TotalCoeff-1][total_zeros];

    status = BitstreamWriteBits(stream, len, code);

    return status;
}

/* see Table 9-9 */
AVCEnc_Status ce_TotalZerosChromaDC(AVCEncBitstream *stream, int total_zeros, int TotalCoeff)
{
    const static uint8 lenTotalZerosChromaDC[3][4] =
    {
        { 1, 2, 3, 3, },
        { 1, 2, 2, 0, },
        { 1, 1, 0, 0, },
    };

    const static uint8 codTotalZerosChromaDC[3][4] =
    {
        { 1, 1, 1, 0, },
        { 1, 1, 0, 0, },
        { 1, 0, 0, 0, },
    };

    int len, code;
    AVCEnc_Status status;

    len = lenTotalZerosChromaDC[TotalCoeff-1][total_zeros];
    code = codTotalZerosChromaDC[TotalCoeff-1][total_zeros];

    status = BitstreamWriteBits(stream, len, code);

    return status;
}

/* see Table 9-10 */
AVCEnc_Status ce_RunBefore(AVCEncBitstream *stream, int run_before, int zerosLeft)
{
    const static uint8 lenRunBefore[7][16] =
    {
        {1, 1},
        {1, 2, 2},
        {2, 2, 2, 2},
        {2, 2, 2, 3, 3},
        {2, 2, 3, 3, 3, 3},
        {2, 3, 3, 3, 3, 3, 3},
        {3, 3, 3, 3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11},
    };

    const static uint8 codRunBefore[7][16] =
    {
        {1, 0},
        {1, 1, 0},
        {3, 2, 1, 0},
        {3, 2, 1, 1, 0},
        {3, 2, 3, 2, 1, 0},
        {3, 0, 1, 3, 2, 5, 4},
        {7, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    };

    int len, code;
    AVCEnc_Status status;

    if (zerosLeft <= 6)
    {
        len = lenRunBefore[zerosLeft-1][run_before];
        code = codRunBefore[zerosLeft-1][run_before];
    }
    else
    {
        len = lenRunBefore[6][run_before];
        code = codRunBefore[6][run_before];
    }

    status = BitstreamWriteBits(stream, len, code);


    return status;
}