Coverage Report

Created: 2026-01-09 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/decoder/drc_src/impd_drc_bitbuffer.c
Line
Count
Source
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 <stdio.h>
21
#include <stdlib.h>
22
#include <math.h>
23
#include <assert.h>
24
#include <string.h>
25
26
#include "impd_type_def.h"
27
#include "impd_drc_bitbuffer.h"
28
#include "impd_drc_extr_delta_coded_info.h"
29
#include "impd_drc_common.h"
30
#include "impd_drc_struct.h"
31
#include "impd_drc_parser.h"
32
33
1.50M
WORD32 impd_read_bits_buf(ia_bit_buf_struct* it_bit_buff, WORD no_of_bits) {
34
1.50M
  UWORD32 ret_val;
35
1.50M
  UWORD8* ptr_read_next = it_bit_buff->ptr_read_next;
36
1.50M
  WORD bit_pos = it_bit_buff->bit_pos;
37
38
1.50M
  if (it_bit_buff->cnt_bits <= 0) {
39
11.1k
    it_bit_buff->error = 1;
40
11.1k
    return -1;
41
11.1k
  }
42
43
1.48M
  if (no_of_bits == 0) {
44
24.7k
    return 0;
45
24.7k
  }
46
47
1.46M
  it_bit_buff->cnt_bits -= no_of_bits;
48
1.46M
  ret_val = (UWORD32)*ptr_read_next;
49
50
1.46M
  bit_pos -= no_of_bits;
51
1.82M
  while (bit_pos < 0) {
52
355k
    bit_pos += 8;
53
355k
    ptr_read_next++;
54
55
355k
    if (ptr_read_next > it_bit_buff->ptr_bit_buf_end) {
56
9.08k
      ptr_read_next = it_bit_buff->ptr_bit_buf_base;
57
9.08k
    }
58
59
355k
    ret_val <<= 8;
60
61
355k
    ret_val |= (UWORD32)*ptr_read_next;
62
355k
  }
63
64
1.46M
  ret_val = ret_val << ((31 - no_of_bits) - bit_pos) >> (32 - no_of_bits);
65
1.46M
  it_bit_buff->ptr_read_next = ptr_read_next;
66
1.46M
  it_bit_buff->bit_pos = (WORD16)bit_pos;
67
1.46M
  return ret_val;
68
1.48M
}
69
70
39.0k
WORD32 impd_skip_bits_buf(ia_bit_buf_struct* it_bit_buff, WORD no_of_bits) {
71
39.0k
  UWORD8* ptr_read_next = it_bit_buff->ptr_read_next;
72
39.0k
  WORD bit_pos = it_bit_buff->bit_pos;
73
74
39.0k
  if (it_bit_buff->cnt_bits < no_of_bits) {
75
3.76k
    it_bit_buff->error = 1;
76
3.76k
    return -1;
77
3.76k
  }
78
79
35.2k
  it_bit_buff->cnt_bits -= no_of_bits;
80
81
35.2k
  bit_pos -= no_of_bits;
82
408k
  while (bit_pos < 0) {
83
373k
    bit_pos += 8;
84
373k
    ptr_read_next++;
85
373k
  }
86
35.2k
  it_bit_buff->ptr_read_next = ptr_read_next;
87
35.2k
  it_bit_buff->bit_pos = (WORD16)bit_pos;
88
35.2k
  return no_of_bits;
89
39.0k
}
90
VOID impd_create_bit_buf(ia_bit_buf_struct* it_bit_buff,
91
30.4k
                         UWORD8* ptr_bit_buf_base, WORD32 bit_buf_size) {
92
30.4k
  it_bit_buff->ptr_bit_buf_base = ptr_bit_buf_base;
93
30.4k
  it_bit_buff->ptr_bit_buf_end = ptr_bit_buf_base + bit_buf_size - 1;
94
95
30.4k
  it_bit_buff->ptr_read_next = ptr_bit_buf_base;
96
30.4k
  it_bit_buff->bit_pos = 7;
97
98
30.4k
  it_bit_buff->cnt_bits = 0;
99
30.4k
  it_bit_buff->size = bit_buf_size << 3;
100
30.4k
  it_bit_buff->error = 0;
101
102
30.4k
  return;
103
30.4k
}
104
105
VOID impd_create_init_bit_buf(ia_bit_buf_struct* it_bit_buff,
106
30.4k
                              UWORD8* ptr_bit_buf_base, WORD32 bit_buf_size) {
107
30.4k
  impd_create_bit_buf(it_bit_buff, ptr_bit_buf_base, bit_buf_size);
108
30.4k
  it_bit_buff->cnt_bits = (bit_buf_size << 3);
109
30.4k
  return;
110
30.4k
}
111
112
WORD32 impd_init_drc_bitstream_dec(ia_drc_bits_dec_struct* p_drc_bs_dec_struct,
113
                                   WORD32 sample_rate, WORD32 frame_size,
114
                                   WORD32 delay_mode,
115
                                   WORD32 lfe_channel_map_count,
116
1.83k
                                   WORD32* lfe_channel_map) {
117
1.83k
  WORD32 i, err_code = 0;
118
119
1.83k
  ia_drc_params_bs_dec_struct* ia_drc_params_struct =
120
1.83k
      &p_drc_bs_dec_struct->ia_drc_params_struct;
121
1.83k
  ia_drc_params_struct->drc_frame_size = frame_size;
122
1.83k
  if (sample_rate < MIN_DRC_SAMP_FREQ) {
123
6
    return -1;
124
6
  }
125
1.83k
  ia_drc_params_struct->delta_tmin_default = impd_get_delta_tmin(sample_rate);
126
1.83k
  ia_drc_params_struct->num_gain_values_max_default =
127
1.83k
      ia_drc_params_struct->drc_frame_size /
128
1.83k
      ia_drc_params_struct->delta_tmin_default;
129
1.83k
  ia_drc_params_struct->delay_mode = delay_mode;
130
131
1.83k
  if ((frame_size < 1) || (frame_size > AUDIO_CODEC_FRAME_SIZE_MAX) ||
132
1.83k
      (ia_drc_params_struct->drc_frame_size < 0.001f * sample_rate)) {
133
0
    return -1;
134
0
  }
135
136
1.83k
  if (ia_drc_params_struct->delta_tmin_default >
137
1.83k
      ia_drc_params_struct->drc_frame_size) {
138
0
    return -1;
139
0
  }
140
141
1.83k
  if (lfe_channel_map_count >= 0) {
142
0
    if ((lfe_channel_map == NULL) ||
143
0
        (lfe_channel_map_count > MAX_CHANNEL_COUNT)) {
144
0
      return (-1);
145
0
    }
146
147
0
    ia_drc_params_struct->lfe_channel_map_count = lfe_channel_map_count;
148
149
0
    for (i = 0; i < lfe_channel_map_count; i++) {
150
0
      ia_drc_params_struct->lfe_channel_map[i] = lfe_channel_map[i];
151
0
    }
152
1.83k
  } else {
153
1.83k
    ia_drc_params_struct->lfe_channel_map_count = -1;
154
155
16.4k
    for (i = 0; i < MAX_CHANNEL_COUNT; i++) {
156
14.6k
      ia_drc_params_struct->lfe_channel_map[i] = 0;
157
14.6k
    }
158
1.83k
  }
159
160
1.83k
  impd_init_tbls(ia_drc_params_struct->num_gain_values_max_default,
161
1.83k
                 &p_drc_bs_dec_struct->tables_default);
162
163
1.83k
  return err_code;
164
1.83k
}
165
166
WORD32 impd_process_drc_bitstream_dec_config(
167
    ia_drc_bits_dec_struct* p_drc_bs_dec_struct, ia_bit_buf_struct* it_bit_buff,
168
    ia_drc_config* pstr_drc_config, UWORD8* bitstream_config,
169
1.83k
    WORD32 num_bytes) {
170
1.83k
  WORD32 err_code = 0;
171
172
1.83k
  impd_create_init_bit_buf(it_bit_buff, bitstream_config, num_bytes);
173
174
1.83k
  err_code = impd_parse_drc_config(
175
1.83k
      it_bit_buff, &p_drc_bs_dec_struct->ia_drc_params_struct, pstr_drc_config);
176
1.83k
  if (err_code) return (err_code);
177
178
63
  return err_code;
179
1.83k
}
180
181
WORD32 impd_process_drc_bitstream_dec_gain(
182
    ia_drc_bits_dec_struct* p_drc_bs_dec_struct, ia_bit_buf_struct* it_bit_buff,
183
    ia_drc_config* pstr_drc_config, ia_drc_gain_struct* pstr_drc_gain,
184
    UWORD8* bitstream_gain, WORD32 num_bytes, WORD32 num_bits_offset,
185
25.2k
    WORD32* num_bits_read) {
186
25.2k
  WORD32 err_code = 0;
187
188
25.2k
  impd_create_init_bit_buf(it_bit_buff, bitstream_gain, num_bytes);
189
190
25.2k
  impd_read_bits_buf(it_bit_buff, num_bits_offset);
191
25.2k
  if (it_bit_buff->error) return it_bit_buff->error;
192
193
24.6k
  err_code = impd_drc_uni_gain_read(it_bit_buff, p_drc_bs_dec_struct,
194
24.6k
                                    pstr_drc_config, pstr_drc_gain);
195
196
24.6k
  if (err_code > PROC_COMPLETE) return (err_code);
197
198
21.7k
  *num_bits_read = (it_bit_buff->size) - it_bit_buff->cnt_bits;
199
200
21.7k
  if (err_code == PROC_COMPLETE) {
201
11.6k
    return err_code;
202
11.6k
  }
203
204
10.0k
  return 0;
205
21.7k
}
206
207
WORD32 impd_process_drc_bitstream_dec_loudness_info_set(
208
    ia_bit_buf_struct* it_bit_buff,
209
    ia_drc_loudness_info_set_struct* pstr_loudness_info,
210
1.51k
    UWORD8* bit_stream_loudness, WORD32 num_bytes_loudness) {
211
1.51k
  WORD32 err_code = 0;
212
213
1.51k
  impd_create_init_bit_buf(it_bit_buff, bit_stream_loudness,
214
1.51k
                           num_bytes_loudness);
215
216
1.51k
  err_code = impd_parse_loudness_info_set(it_bit_buff, pstr_loudness_info);
217
1.51k
  return err_code;
218
1.51k
}