aboutsummaryrefslogtreecommitdiff
path: root/encoder/ixheaace_bitbuffer_hp.c
blob: f22b8c368dab5ec73c460a6a7c43c9e6324ea58f (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
/******************************************************************************
 *                                                                            *
 * 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"

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;
}