Home | History | Annotate | Download | only in source
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      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     Table of contents
     20 
     21      1. Include headers
     22      2. External compiler flags
     23      3. Module defines
     24      4. Local function prototypes
     25      5. Functions
     26           h264bsdCountLeadingZeros
     27           h264bsdRbspTrailingBits
     28           h264bsdMoreRbspData
     29           h264bsdNextMbAddress
     30           h264bsdSetCurrImageMbPointers
     31 
     32 ------------------------------------------------------------------------------*/
     33 
     34 /*------------------------------------------------------------------------------
     35     1. Include headers
     36 ------------------------------------------------------------------------------*/
     37 
     38 #include "h264bsd_util.h"
     39 
     40 /*------------------------------------------------------------------------------
     41     2. External compiler flags
     42 --------------------------------------------------------------------------------
     43 
     44 --------------------------------------------------------------------------------
     45     3. Module defines
     46 ------------------------------------------------------------------------------*/
     47 
     48 /* look-up table for expected values of stuffing bits */
     49 static const u32 stuffingTable[8] = {0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80};
     50 
     51 /* look-up table for chroma quantization parameter as a function of luma QP */
     52 const u32 h264bsdQpC[52] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
     53     20,21,22,23,24,25,26,27,28,29,29,30,31,32,32,33,34,34,35,35,36,36,37,37,37,
     54     38,38,38,39,39,39,39};
     55 
     56 /*------------------------------------------------------------------------------
     57     4. Local function prototypes
     58 ------------------------------------------------------------------------------*/
     59 
     60 /*------------------------------------------------------------------------------
     61 
     62    5.1  Function: h264bsdCountLeadingZeros
     63 
     64         Functional description:
     65             Count leading zeros in a code word. Code word is assumed to be
     66             right-aligned, last bit of the code word in the lsb of the value.
     67 
     68         Inputs:
     69             value   code word
     70             length  number of bits in the code word
     71 
     72         Outputs:
     73             none
     74 
     75         Returns:
     76             number of leading zeros in the code word
     77 
     78 ------------------------------------------------------------------------------*/
     79 #ifndef H264DEC_NEON
     80 u32 h264bsdCountLeadingZeros(u32 value, u32 length)
     81 {
     82 
     83 /* Variables */
     84 
     85     u32 zeros = 0;
     86     u32 mask = 1 << (length - 1);
     87 
     88 /* Code */
     89 
     90     ASSERT(length <= 32);
     91 
     92     while (mask && !(value & mask))
     93     {
     94         zeros++;
     95         mask >>= 1;
     96     }
     97     return(zeros);
     98 
     99 }
    100 #endif
    101 /*------------------------------------------------------------------------------
    102 
    103    5.2  Function: h264bsdRbspTrailingBits
    104 
    105         Functional description:
    106             Check Raw Byte Stream Payload (RBSP) trailing bits, i.e. stuffing.
    107             Rest of the current byte (whole byte if allready byte aligned)
    108             in the stream buffer shall contain a '1' bit followed by zero or
    109             more '0' bits.
    110 
    111         Inputs:
    112             pStrmData   pointer to stream data structure
    113 
    114         Outputs:
    115             none
    116 
    117         Returns:
    118             HANTRO_OK      RBSP trailing bits found
    119             HANTRO_NOK     otherwise
    120 
    121 ------------------------------------------------------------------------------*/
    122 
    123 u32 h264bsdRbspTrailingBits(strmData_t *pStrmData)
    124 {
    125 
    126 /* Variables */
    127 
    128     u32 stuffing;
    129     u32 stuffingLength;
    130 
    131 /* Code */
    132 
    133     ASSERT(pStrmData);
    134     ASSERT(pStrmData->bitPosInWord < 8);
    135 
    136     stuffingLength = 8 - pStrmData->bitPosInWord;
    137 
    138     stuffing = h264bsdGetBits(pStrmData, stuffingLength);
    139     if (stuffing == END_OF_STREAM)
    140         return(HANTRO_NOK);
    141 
    142     if (stuffing != stuffingTable[stuffingLength - 1])
    143         return(HANTRO_NOK);
    144     else
    145         return(HANTRO_OK);
    146 
    147 }
    148 
    149 /*------------------------------------------------------------------------------
    150 
    151    5.3  Function: h264bsdMoreRbspData
    152 
    153         Functional description:
    154             Check if there is more data in the current RBSP. The standard
    155             defines this function so that there is more data if
    156                 -more than 8 bits left or
    157                 -last bits are not RBSP trailing bits
    158 
    159         Inputs:
    160             pStrmData   pointer to stream data structure
    161 
    162         Outputs:
    163             none
    164 
    165         Returns:
    166             HANTRO_TRUE    there is more data
    167             HANTRO_FALSE   no more data
    168 
    169 ------------------------------------------------------------------------------*/
    170 
    171 u32 h264bsdMoreRbspData(strmData_t *pStrmData)
    172 {
    173 
    174 /* Variables */
    175 
    176     u32 bits;
    177 
    178 /* Code */
    179 
    180     ASSERT(pStrmData);
    181     ASSERT(pStrmData->strmBuffReadBits <= 8 * pStrmData->strmBuffSize);
    182 
    183     bits = pStrmData->strmBuffSize * 8 - pStrmData->strmBuffReadBits;
    184 
    185     if (bits == 0)
    186         return(HANTRO_FALSE);
    187 
    188     if ( (bits > 8) ||
    189          ((h264bsdShowBits32(pStrmData)>>(32-bits)) != (1ul << (bits-1))) )
    190         return(HANTRO_TRUE);
    191     else
    192         return(HANTRO_FALSE);
    193 
    194 }
    195 
    196 /*------------------------------------------------------------------------------
    197 
    198    5.4  Function: h264bsdNextMbAddress
    199 
    200         Functional description:
    201             Get address of the next macroblock in the current slice group.
    202 
    203         Inputs:
    204             pSliceGroupMap      slice group for each macroblock
    205             picSizeInMbs        size of the picture
    206             currMbAddr          where to start
    207 
    208         Outputs:
    209             none
    210 
    211         Returns:
    212             address of the next macroblock
    213             0   if none of the following macroblocks belong to same slice
    214                 group as currMbAddr
    215 
    216 ------------------------------------------------------------------------------*/
    217 
    218 u32 h264bsdNextMbAddress(u32 *pSliceGroupMap, u32 picSizeInMbs, u32 currMbAddr)
    219 {
    220 
    221 /* Variables */
    222 
    223     u32 i, sliceGroup;
    224 
    225 /* Code */
    226 
    227     ASSERT(pSliceGroupMap);
    228     ASSERT(picSizeInMbs);
    229     ASSERT(currMbAddr < picSizeInMbs);
    230 
    231     sliceGroup = pSliceGroupMap[currMbAddr];
    232 
    233     i = currMbAddr + 1;
    234     while ((i < picSizeInMbs) && (pSliceGroupMap[i] != sliceGroup))
    235     {
    236         i++;
    237     }
    238 
    239     if (i == picSizeInMbs)
    240         i = 0;
    241 
    242     return(i);
    243 
    244 }
    245 
    246 
    247 /*------------------------------------------------------------------------------
    248 
    249    5.5  Function: h264bsdSetCurrImageMbPointers
    250 
    251         Functional description:
    252             Set luma and chroma pointers in image_t for current MB
    253 
    254         Inputs:
    255             image       Current image
    256             mbNum       number of current MB
    257 
    258         Outputs:
    259             none
    260 
    261         Returns:
    262             none
    263 ------------------------------------------------------------------------------*/
    264 void h264bsdSetCurrImageMbPointers(image_t *image, u32 mbNum)
    265 {
    266     u32 width, height;
    267     u32 picSize;
    268     u32 row, col;
    269     u32 tmp;
    270 
    271     width = image->width;
    272     height = image->height;
    273     row = mbNum / width;
    274     col = mbNum % width;
    275 
    276     tmp = row * width;
    277     picSize = width * height;
    278 
    279     image->luma = (u8*)(image->data + col * 16 + tmp * 256);
    280     image->cb = (u8*)(image->data + picSize * 256 + tmp * 64 + col * 8);
    281     image->cr = (u8*)(image->cb + picSize * 64);
    282 }
    283 
    284 
    285