Home | History | Annotate | Download | only in decoder
      1 /******************************************************************************
      2  *                                                                            *
      3  * Copyright (C) 2018 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at:
      8  *
      9  * http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  *****************************************************************************
     18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
     19 */
     20 #include "ixheaacd_sbr_common.h"
     21 #include <ixheaacd_type_def.h>
     22 
     23 #include "ixheaacd_constants.h"
     24 #include <ixheaacd_basic_ops32.h>
     25 #include <ixheaacd_basic_ops16.h>
     26 #include <ixheaacd_basic_ops40.h>
     27 #include "ixheaacd_basic_ops.h"
     28 
     29 #include <ixheaacd_basic_op.h>
     30 #include "ixheaacd_intrinsics.h"
     31 #include "ixheaacd_bitbuffer.h"
     32 
     33 #include "ixheaacd_adts_crc_check.h"
     34 
     35 VOID ixheaacd_byte_align(ia_bit_buf_struct *it_bit_buff,
     36                          WORD32 *align_bits_cnt) {
     37   WORD alignment;
     38   alignment = (WORD)((*align_bits_cnt - it_bit_buff->cnt_bits) & 0x07);
     39 
     40   if (alignment) {
     41     ixheaacd_read_bits_buf(it_bit_buff, (8 - alignment));
     42   }
     43 
     44   *align_bits_cnt = it_bit_buff->cnt_bits;
     45 }
     46 
     47 WORD32 ixheaacd_show_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits) {
     48   UWORD32 ret_val;
     49   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
     50   WORD bit_pos = it_bit_buff->bit_pos;
     51 
     52   ret_val = (UWORD32)*ptr_read_next;
     53 
     54   bit_pos -= no_of_bits;
     55   while (bit_pos < 0) {
     56     bit_pos += 8;
     57     ptr_read_next++;
     58 
     59     if (ptr_read_next > it_bit_buff->ptr_bit_buf_end) {
     60       ptr_read_next = it_bit_buff->ptr_bit_buf_base;
     61     }
     62 
     63     ret_val <<= 8;
     64 
     65     ret_val |= (UWORD32)*ptr_read_next;
     66   }
     67 
     68   ret_val = ret_val << ((31 - no_of_bits) - bit_pos) >> (32 - no_of_bits);
     69 
     70   return ret_val;
     71 }
     72 
     73 WORD32 ixheaacd_read_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits) {
     74   UWORD32 ret_val;
     75   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
     76   WORD bit_pos = it_bit_buff->bit_pos;
     77 
     78   if (no_of_bits == 0) {
     79     return 0;
     80   }
     81 
     82   it_bit_buff->cnt_bits -= no_of_bits;
     83   ret_val = (UWORD32)*ptr_read_next;
     84 
     85   bit_pos -= no_of_bits;
     86   while (bit_pos < 0) {
     87     bit_pos += 8;
     88     ptr_read_next++;
     89 
     90     if (ptr_read_next > it_bit_buff->ptr_bit_buf_end) {
     91       ptr_read_next = it_bit_buff->ptr_bit_buf_base;
     92     }
     93 
     94     ret_val <<= 8;
     95 
     96     ret_val |= (UWORD32)*ptr_read_next;
     97   }
     98 
     99   ret_val = ret_val << ((31 - no_of_bits) - bit_pos) >> (32 - no_of_bits);
    100   it_bit_buff->ptr_read_next = ptr_read_next;
    101   it_bit_buff->bit_pos = (WORD16)bit_pos;
    102   return ret_val;
    103 }
    104 
    105 UWORD32 ixheaacd_aac_read_byte(UWORD8 **ptr_read_next, WORD32 *bit_pos,
    106                                WORD32 *readword) {
    107   UWORD8 *v = *ptr_read_next;
    108   WORD32 bits_consumed = *bit_pos;
    109 
    110   if ((bits_consumed -= 8) >= 0) {
    111     *readword = (*readword << 8) | *v;
    112     v++;
    113   } else {
    114     bits_consumed += 8;
    115   }
    116   *bit_pos = bits_consumed;
    117   *ptr_read_next = v;
    118   return 1;
    119 }
    120 
    121 UWORD32 ixheaacd_aac_read_2bytes(UWORD8 **ptr_read_next, WORD32 *bit_pos,
    122                                  WORD32 *readword) {
    123   UWORD8 *v = *ptr_read_next;
    124   WORD32 bits_consumed = *bit_pos;
    125 
    126   if ((bits_consumed - 16) >= 0) {
    127     *readword = (*readword << 8) | *v;
    128     v++;
    129     *readword = (*readword << 8) | *v;
    130     v++;
    131     bits_consumed -= 16;
    132 
    133   } else if ((bits_consumed - 8) >= 0) {
    134     *readword = (*readword << 8) | *v;
    135     v++;
    136     bits_consumed -= 8;
    137   }
    138 
    139   *bit_pos = bits_consumed;
    140   *ptr_read_next = v;
    141   return 1;
    142 }
    143 
    144 UWORD32 ixheaacd_aac_read_byte_corr1(UWORD8 **ptr_read_next,
    145                                      WORD16 *ptr_bit_pos, WORD32 *readword) {
    146   UWORD8 *v = *ptr_read_next;
    147   WORD16 bits_consumed = *ptr_bit_pos;
    148 
    149   while (bits_consumed >= 8) {
    150     if ((bits_consumed -= 8) >= 0) {
    151       {
    152         *readword = (*readword << 8) | *v;
    153         v++;
    154       }
    155     } else {
    156       bits_consumed += 8;
    157     }
    158   }
    159   *ptr_bit_pos = bits_consumed;
    160   *ptr_read_next = v;
    161   return 1;
    162 }
    163 
    164 UWORD32 ixheaacd_aac_read_byte_corr(UWORD8 **ptr_read_next, WORD32 *ptr_bit_pos,
    165                                     WORD32 *readword, UWORD8 *p_bit_buf_end) {
    166   UWORD8 *v = *ptr_read_next;
    167   WORD32 bits_consumed = *ptr_bit_pos;
    168 
    169   if ((bits_consumed -= 8) >= 0) {
    170     if (p_bit_buf_end < v)
    171       bits_consumed += 8;
    172     else {
    173       *readword = (*readword << 8) | *v;
    174       v++;
    175     }
    176   } else {
    177     bits_consumed += 8;
    178   }
    179   *ptr_bit_pos = bits_consumed;
    180   *ptr_read_next = v;
    181   return 1;
    182 }
    183 
    184 WORD32 ixheaacd_aac_read_bit(ia_bit_buf_struct *it_bit_buff) {
    185   UWORD8 ret_val;
    186   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
    187   WORD bit_pos = it_bit_buff->bit_pos;
    188   UWORD32 temp;
    189   WORD no_of_bits = 1;
    190 
    191   if (bit_pos < 0) {
    192     bit_pos = 7;
    193     ptr_read_next--;
    194   }
    195 
    196   it_bit_buff->cnt_bits += no_of_bits;
    197   ret_val = *ptr_read_next;
    198   bit_pos -= no_of_bits;
    199 
    200   temp = (ret_val << 24) << (bit_pos + no_of_bits);
    201   it_bit_buff->ptr_read_next = ptr_read_next;
    202   it_bit_buff->bit_pos = (WORD16)bit_pos;
    203 
    204   return temp >> (32 - no_of_bits);
    205 }
    206 
    207 WORD32 ixheaacd_aac_read_bit_rev(ia_bit_buf_struct *it_bit_buff) {
    208   UWORD8 ret_val;
    209   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
    210   WORD bit_pos = it_bit_buff->bit_pos;
    211   UWORD32 temp;
    212   WORD no_of_bits = 1;
    213 
    214   if (bit_pos >= 8) {
    215     bit_pos -= 8;
    216     ptr_read_next++;
    217   }
    218 
    219   it_bit_buff->cnt_bits -= no_of_bits;
    220   ret_val = *ptr_read_next;
    221   bit_pos += no_of_bits;
    222 
    223   temp = (ret_val << 24) << (bit_pos - no_of_bits);
    224   it_bit_buff->ptr_read_next = ptr_read_next;
    225   it_bit_buff->bit_pos = (WORD16)bit_pos;
    226 
    227   return temp >> (32 - no_of_bits);
    228 }
    229 
    230 VOID ixheaacd_write_bit(ia_bit_buf_struct *it_bit_buff, WORD32 value,
    231                         WORD32 no_of_bits)
    232 
    233 {
    234   WORD32 mask;
    235 
    236   if (no_of_bits == 0) return;
    237 
    238   mask = 0x1;
    239   mask <<= no_of_bits - 1;
    240 
    241   it_bit_buff->bit_count += no_of_bits;
    242 
    243   while (no_of_bits > 0) {
    244     while (no_of_bits > 0 && it_bit_buff->valid_bits < 8) {
    245       it_bit_buff->byte <<= 1;
    246       if (value & mask) it_bit_buff->byte |= 0x1;
    247       value <<= 1;
    248       no_of_bits--;
    249       it_bit_buff->valid_bits++;
    250     }
    251     if (it_bit_buff->valid_bits == 8) {
    252       *it_bit_buff->byte_ptr++ = it_bit_buff->byte;
    253       it_bit_buff->byte = 0;
    254       it_bit_buff->valid_bits = 0;
    255     }
    256   }
    257 }
    258 
    259 WORD32 ixheaacd_read_bit(ia_bit_buf_struct *it_bit_buff, WORD32 no_of_bits) {
    260   UWORD32 ret_val;
    261   UWORD8 *ptr_read_next = it_bit_buff->byte_ptr;
    262 
    263   if (no_of_bits == 0) {
    264     return 0;
    265   }
    266 
    267   ret_val = ixheaacd_aac_showbits_32(ptr_read_next);
    268   it_bit_buff->byte_ptr += (no_of_bits >> 3);
    269 
    270   if (it_bit_buff->valid_bits != 8) {
    271     UWORD8 *v = it_bit_buff->byte_ptr;
    272     ret_val = (ret_val << (8 - it_bit_buff->valid_bits)) |
    273               (*v >> it_bit_buff->valid_bits);
    274   }
    275 
    276   it_bit_buff->valid_bits -= (no_of_bits % 8);
    277 
    278   ret_val = ret_val >> (32 - no_of_bits);
    279 
    280   return ret_val;
    281 }
    282