1 /******************************************************************************
2 * *
3 * Copyright (C) 2023 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
21 #include "ixheaac_type_def.h"
22 #include "ixheaac_constants.h"
23 #include "ixheaace_aac_constants.h"
24 #include <stdlib.h>
25 #include "ixheaac_basic_ops32.h"
26 #include "ixheaac_basic_ops16.h"
27 #include "ixheaac_basic_ops40.h"
28 #include "ixheaac_basic_ops.h"
29
30 #include "ixheaace_bitbuffer.h"
31 #include "ixheaace_common_utils.h"
32
33 UWORD8
ixheaace_write_bits(ixheaace_bit_buf_handle pstr_bit_buf,UWORD32 write_value,UWORD8 num_bits_to_write)34 ixheaace_write_bits(ixheaace_bit_buf_handle pstr_bit_buf, UWORD32 write_value,
35 UWORD8 num_bits_to_write) {
36 WORD8 bits_to_write;
37 WORD32 write_position;
38 UWORD8 *ptr_write_next;
39 UWORD8 *ptr_bit_buf_end;
40 UWORD8 *ptr_bit_buf_base;
41
42 UWORD8 bits_written = num_bits_to_write;
43
44 if (pstr_bit_buf == NULL) {
45 return num_bits_to_write;
46 }
47
48 pstr_bit_buf->cnt_bits += num_bits_to_write;
49
50 write_position = pstr_bit_buf->write_position;
51 ptr_write_next = pstr_bit_buf->ptr_write_next;
52 ptr_bit_buf_end = pstr_bit_buf->ptr_bit_buf_end;
53 ptr_bit_buf_base = pstr_bit_buf->ptr_bit_buf_base;
54
55 while (num_bits_to_write) {
56 UWORD8 tmp, msk;
57 bits_to_write = (WORD8)MIN(write_position + 1, num_bits_to_write);
58
59 tmp = (UWORD8)(write_value << (32 - num_bits_to_write) >>
60 (32 - bits_to_write) << (write_position + 1 - bits_to_write));
61
62 msk = ~(((1 << bits_to_write) - 1) << (write_position + 1 - bits_to_write));
63
64 *ptr_write_next &= msk;
65 *ptr_write_next |= tmp;
66
67 write_position -= bits_to_write;
68
69 num_bits_to_write -= bits_to_write;
70
71 if (write_position < 0) {
72 write_position += 8;
73 ptr_write_next++;
74
75 if (ptr_write_next > ptr_bit_buf_end) {
76 ptr_write_next = ptr_bit_buf_base;
77 }
78 }
79 }
80
81 pstr_bit_buf->write_position = write_position;
82 pstr_bit_buf->ptr_write_next = ptr_write_next;
83
84 return bits_written;
85 }
86
ixheaace_byte_align_buffer(ixheaace_bit_buf_handle pstr_it_bit_buff)87 UWORD32 ixheaace_byte_align_buffer(ixheaace_bit_buf_handle pstr_it_bit_buff) {
88 WORD alignment;
89 alignment = (WORD)((pstr_it_bit_buff->cnt_bits) & 0x07);
90
91 if (alignment) {
92 ixheaace_write_bits(pstr_it_bit_buff, 0, (UWORD8)(8 - alignment));
93 return (8 - alignment);
94 }
95 return 0;
96 }