/******************************************************************************
 *                                                                            *
 * Copyright (C) 2023 The Android Open Source Project
 *
 * 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.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
 */

#include "ixheaac_type_def.h"
#include "ixheaac_constants.h"
#include "ixheaace_aac_constants.h"
#include <stdlib.h>
#include "ixheaac_basic_ops32.h"
#include "ixheaac_basic_ops16.h"
#include "ixheaac_basic_ops40.h"
#include "ixheaac_basic_ops.h"

#include "ixheaace_bitbuffer.h"
#include "ixheaace_common_utils.h"

const UWORD32 aac_pad_bits[22] = {
  0xB7A50050, 0xD62353EA, 0x7AA78655, 0xEAFB51ED, 0x313CA8E1, 
  0x39A2E1BE, 0xD64272CE, 0xF2114960, 0x26B0CFCA, 0xAC02917B, 0xFC6EE713, 
  0x43B0163C, 0x6302EBFA, 0x2F1E1F33, 0x26BA3B22, 0x8D0C7ABC, 0x7ECC65DF, 
  0xD304FAEA, 0xB0BAF083, 0x78625459, 0xD45F869F, 0x0140D316
};

UWORD8
ixheaace_write_bits(ixheaace_bit_buf_handle pstr_bit_buf, UWORD32 write_value,
                    UWORD8 num_bits_to_write) {
  WORD8 bits_to_write;
  WORD32 write_position;
  UWORD8 *ptr_write_next;
  UWORD8 *ptr_bit_buf_end;
  UWORD8 *ptr_bit_buf_base;

  UWORD8 bits_written = num_bits_to_write;

  if (pstr_bit_buf == NULL) {
    return num_bits_to_write;
  }

  pstr_bit_buf->cnt_bits += num_bits_to_write;

  write_position = pstr_bit_buf->write_position;
  ptr_write_next = pstr_bit_buf->ptr_write_next;
  ptr_bit_buf_end = pstr_bit_buf->ptr_bit_buf_end;
  ptr_bit_buf_base = pstr_bit_buf->ptr_bit_buf_base;

  while (num_bits_to_write) {
    UWORD8 tmp, msk;
    bits_to_write = (WORD8)MIN(write_position + 1, num_bits_to_write);

    tmp = (UWORD8)(write_value << (32 - num_bits_to_write) >>
                   (32 - bits_to_write) << (write_position + 1 - bits_to_write));

    msk = ~(((1 << bits_to_write) - 1) << (write_position + 1 - bits_to_write));

    *ptr_write_next &= msk;
    *ptr_write_next |= tmp;

    write_position -= bits_to_write;

    num_bits_to_write -= bits_to_write;

    if (write_position < 0) {
      write_position += 8;
      ptr_write_next++;

      if (ptr_write_next > ptr_bit_buf_end) {
        ptr_write_next = ptr_bit_buf_base;
      }
    }
  }

  pstr_bit_buf->write_position = write_position;
  pstr_bit_buf->ptr_write_next = ptr_write_next;

  return bits_written;
}

UWORD32 ixheaace_byte_align_buffer(ixheaace_bit_buf_handle pstr_it_bit_buff) {
  WORD alignment;
  alignment = (WORD)((pstr_it_bit_buff->cnt_bits) & 0x07);

  if (alignment) {
    ixheaace_write_bits(pstr_it_bit_buff, 0, (UWORD8)(8 - alignment));
    return (8 - alignment);
  }
  return 0;
}

WORD ixheaace_get_align_bits(ixheaace_bit_buf_handle pstr_it_bit_buff, WORD count) {
  WORD temp = 0;
  WORD padOffset;
  WORD padBitOffset;

  while (count--) {
    padOffset = pstr_it_bit_buff->padding_offset >> 5;
    padBitOffset = pstr_it_bit_buff->padding_offset & 0x1f;

    temp <<= 1;
    temp |= (aac_pad_bits[padOffset] >> (0x1f - padBitOffset)) & 1;

    pstr_it_bit_buff->padding_offset = (pstr_it_bit_buff->padding_offset + 1) % 0x2c0;
  }

  return temp;
}

UWORD32 ixheaace_byte_align_buffer_codebook(ixheaace_bit_buf_handle pstr_it_bit_buff) {
  WORD alignment;
  alignment = (WORD)((pstr_it_bit_buff->cnt_bits) & 0x07);

  if (alignment) {
    ixheaace_write_bits(pstr_it_bit_buff, ixheaace_get_align_bits(pstr_it_bit_buff, (8 - alignment)), (UWORD8)(8 - alignment));
    return (8 - alignment);
  }
  return 0;
}