/src/libxaac/encoder/ixheaace_bitbuffer_hp.c
Line | Count | Source |
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 |
34 | | ixheaace_write_bits(ixheaace_bit_buf_handle pstr_bit_buf, UWORD32 write_value, |
35 | 205M | UWORD8 num_bits_to_write) { |
36 | 205M | WORD8 bits_to_write; |
37 | 205M | WORD32 write_position; |
38 | 205M | UWORD8 *ptr_write_next; |
39 | 205M | UWORD8 *ptr_bit_buf_end; |
40 | 205M | UWORD8 *ptr_bit_buf_base; |
41 | | |
42 | 205M | UWORD8 bits_written = num_bits_to_write; |
43 | | |
44 | 205M | if (pstr_bit_buf == NULL) { |
45 | 4.50M | return num_bits_to_write; |
46 | 4.50M | } |
47 | | |
48 | 201M | pstr_bit_buf->cnt_bits += num_bits_to_write; |
49 | | |
50 | 201M | write_position = pstr_bit_buf->write_position; |
51 | 201M | ptr_write_next = pstr_bit_buf->ptr_write_next; |
52 | 201M | ptr_bit_buf_end = pstr_bit_buf->ptr_bit_buf_end; |
53 | 201M | ptr_bit_buf_base = pstr_bit_buf->ptr_bit_buf_base; |
54 | | |
55 | 492M | while (num_bits_to_write) { |
56 | 290M | UWORD8 tmp, msk; |
57 | 290M | bits_to_write = (WORD8)MIN(write_position + 1, num_bits_to_write); |
58 | | |
59 | 290M | tmp = (UWORD8)(write_value << (32 - num_bits_to_write) >> |
60 | 290M | (32 - bits_to_write) << (write_position + 1 - bits_to_write)); |
61 | | |
62 | 290M | msk = ~(((1 << bits_to_write) - 1) << (write_position + 1 - bits_to_write)); |
63 | | |
64 | 290M | *ptr_write_next &= msk; |
65 | 290M | *ptr_write_next |= tmp; |
66 | | |
67 | 290M | write_position -= bits_to_write; |
68 | | |
69 | 290M | num_bits_to_write -= bits_to_write; |
70 | | |
71 | 290M | if (write_position < 0) { |
72 | 117M | write_position += 8; |
73 | 117M | ptr_write_next++; |
74 | | |
75 | 117M | if (ptr_write_next > ptr_bit_buf_end) { |
76 | 25.0k | ptr_write_next = ptr_bit_buf_base; |
77 | 25.0k | } |
78 | 117M | } |
79 | 290M | } |
80 | | |
81 | 201M | pstr_bit_buf->write_position = write_position; |
82 | 201M | pstr_bit_buf->ptr_write_next = ptr_write_next; |
83 | | |
84 | 201M | return bits_written; |
85 | 205M | } |
86 | | |
87 | 19.6k | UWORD32 ixheaace_byte_align_buffer(ixheaace_bit_buf_handle pstr_it_bit_buff) { |
88 | 19.6k | WORD alignment; |
89 | 19.6k | alignment = (WORD)((pstr_it_bit_buff->cnt_bits) & 0x07); |
90 | | |
91 | 19.6k | if (alignment) { |
92 | 14.3k | ixheaace_write_bits(pstr_it_bit_buff, 0, (UWORD8)(8 - alignment)); |
93 | 14.3k | return (8 - alignment); |
94 | 14.3k | } |
95 | 5.28k | return 0; |
96 | 19.6k | } |