1/*
2 * Copyright (C) 2007-2008 ARM Limited
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 *
19 * File Name:  armVCM4P2_PutVLCBits.c
20 * OpenMAX DL: v1.0.2
21 * Revision:   9641
22 * Date:       Thursday, February 7, 2008
23 *
24 *
25 *
26 *
27 * Description:
28 * Contains module for VLC put bits to bitstream
29 *
30 */
31
32#include "omxtypes.h"
33#include "armOMX.h"
34
35#include "armVC.h"
36#include "armCOMM.h"
37#include "armCOMM_Bitstream.h"
38#include "armVCM4P2_ZigZag_Tables.h"
39#include "armVCM4P2_Huff_Tables_VLC.h"
40
41
42/**
43 * Function: armVCM4P2_PutVLCBits
44 *
45 * Description:
46 * Checks the type of Escape Mode and put encoded bits for
47 * quantized DCT coefficients.
48 *
49 * Remarks:
50 *
51 * Parameters:
52 * [in]	 ppBitStream      pointer to the pointer to the current byte in
53 *						  the bit stream
54 * [in]	 pBitOffset       pointer to the bit position in the byte pointed
55 *                        by *ppBitStream. Valid within 0 to 7
56 * [in] shortVideoHeader binary flag indicating presence of short_video_header; escape modes 0-3 are used if shortVideoHeader==0,
57 *                           and escape mode 4 is used when shortVideoHeader==1.
58 * [in]  start            start indicates whether the encoding begins with
59 *                        0th element or 1st.
60 * [in]  maxStoreRunL0    Max store possible (considering last and inter/intra)
61 *                        for last = 0
62 * [in]  maxStoreRunL1    Max store possible (considering last and inter/intra)
63 *                        for last = 1
64 * [in]  maxRunForMultipleEntriesL0
65 *                        The run value after which level
66 *                        will be equal to 1:
67 *                        (considering last and inter/intra status) for last = 0
68 * [in]  maxRunForMultipleEntriesL1
69 *                        The run value after which level
70 *                        will be equal to 1:
71 *                        (considering last and inter/intra status) for last = 1
72 * [in]  pRunIndexTableL0 Run Index table defined in
73 *                        armVCM4P2_Huff_Tables_VLC.c for last == 0
74 * [in]  pVlcTableL0      VLC table for last == 0
75 * [in]  pRunIndexTableL1 Run Index table defined in
76 *                        armVCM4P2_Huff_Tables_VLC.c for last == 1
77 * [in]  pVlcTableL1      VLC table for last == 1
78 * [in]  pLMAXTableL0     Level MAX table defined in
79 *                        armVCM4P2_Huff_Tables_VLC.c for last == 0
80 * [in]  pLMAXTableL1     Level MAX table defined in
81 *                        armVCM4P2_Huff_Tables_VLC.c for last == 1
82 * [in]  pRMAXTableL0     Run MAX table defined in
83 *                        armVCM4P2_Huff_Tables_VLC.c for last == 0
84 * [in]  pRMAXTableL1     Run MAX table defined in
85 *                        armVCM4P2_Huff_Tables_VLC.c for last == 1
86 * [out] pQDctBlkCoef     pointer to the quantized DCT coefficient
87 * [out] ppBitStream      *ppBitStream is updated after the block is encoded
88 *                        so that it points to the current byte in the bit
89 *                        stream buffer.
90 * [out] pBitOffset       *pBitOffset is updated so that it points to the
91 *                        current bit position in the byte pointed by
92 *                        *ppBitStream.
93 *
94 * Return Value:
95 * Standard OMXResult result. See enumeration for possible result codes.
96 *
97 */
98
99
100OMXResult armVCM4P2_PutVLCBits (
101              OMX_U8 **ppBitStream,
102              OMX_INT * pBitOffset,
103              const OMX_S16 *pQDctBlkCoef,
104              OMX_INT shortVideoHeader,
105              OMX_U8 start,
106              OMX_U8 maxStoreRunL0,
107              OMX_U8 maxStoreRunL1,
108              OMX_U8  maxRunForMultipleEntriesL0,
109              OMX_U8  maxRunForMultipleEntriesL1,
110              const OMX_U8  * pRunIndexTableL0,
111              const ARM_VLC32 *pVlcTableL0,
112			  const OMX_U8  * pRunIndexTableL1,
113              const ARM_VLC32 *pVlcTableL1,
114              const OMX_U8  * pLMAXTableL0,
115              const OMX_U8  * pLMAXTableL1,
116              const OMX_U8  * pRMAXTableL0,
117              const OMX_U8  * pRMAXTableL1,
118              const OMX_U8  * pZigzagTable
119)
120{
121
122    OMX_U32 storeRun = 0, run, storeRunPlus;
123    OMX_U8  last = 0, first = 1, fMode;
124    OMX_S16 level, storeLevel = 0, storeLevelPlus;
125    OMX_INT i;
126
127        /* RLE encoding and packing the bits into the streams */
128        for (i = start, run=0; i < 64; i++)
129        {
130            level   = pQDctBlkCoef[pZigzagTable[i]];
131
132            /* Counting the run */
133            if (level == 0)
134            {
135                run++;
136            }
137
138            /* Found a non-zero coeff */
139            else
140            {
141                if (first == 0)
142                {
143                    last = 0;
144
145                    /* Check for a valid entry in the VLC table */
146                    storeLevelPlus = armSignCheck(storeLevel) *
147                      (armAbs(storeLevel) - pLMAXTableL0[storeRun]);
148                    storeRunPlus = storeRun -
149                                  (pRMAXTableL0[armAbs(storeLevel) - 1] + 1);
150
151                    fMode = armVCM4P2_CheckVLCEscapeMode(
152                                             storeRun,
153                                             storeRunPlus,
154                                             storeLevel,
155                                             storeLevelPlus,
156                                             maxStoreRunL0,
157                                             maxRunForMultipleEntriesL0,
158                                             shortVideoHeader,
159                                             pRunIndexTableL0);
160
161                    armVCM4P2_FillVLCBuffer (
162                                      ppBitStream,
163                                      pBitOffset,
164                                      storeRun,
165                                      storeLevel,
166									  storeRunPlus,
167                                      storeLevelPlus,
168                                      fMode,
169									  last,
170                                      maxRunForMultipleEntriesL0,
171                                      pRunIndexTableL0,
172                                      pVlcTableL0);
173                }
174                storeLevel = level;
175                storeRun   = run;
176                first = 0;
177                run = 0;
178            }
179
180        } /* end of for loop for 64 elements */
181
182        /* writing the last element */
183        last = 1;
184
185        /* Check for a valid entry in the VLC table */
186        storeLevelPlus = armSignCheck(storeLevel) *
187                        (armAbs(storeLevel) - pLMAXTableL1[run]);
188        storeRunPlus = storeRun -
189                      (pRMAXTableL1[armAbs(storeLevel) - 1] + 1);
190        fMode = armVCM4P2_CheckVLCEscapeMode(
191                                 storeRun,
192                                 storeRunPlus,
193                                 storeLevel,
194                                 storeLevelPlus,
195                                 maxStoreRunL1,
196                                 maxRunForMultipleEntriesL1,
197                                 shortVideoHeader,
198                                 pRunIndexTableL1);
199
200        armVCM4P2_FillVLCBuffer (
201                          ppBitStream,
202                          pBitOffset,
203                          storeRun,
204                          storeLevel,
205						  storeRunPlus,
206                          storeLevelPlus,
207                          fMode,
208						  last,
209                          maxRunForMultipleEntriesL1,
210                          pRunIndexTableL1,
211                          pVlcTableL1);
212	return OMX_Sts_NoErr;
213}
214
215/* End of File */
216