FFmpeg
aacdec_ac.c
Go to the documentation of this file.
1 /*
2  * AAC definitions and structures
3  * Copyright (c) 2024 Lynne
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavcodec/aactab.h"
23 #include "aacdec_ac.h"
24 
25 uint32_t ff_aac_ac_map_process(AACArithState *state, int reset, int N)
26 {
27  float ratio;
28  if (reset) {
29  memset(state->last, 0, sizeof(state->last));
30  state->last_len = N;
31  } else if (state->last_len != N) {
32  int i;
33  uint8_t last[512 /* 2048 / 4 */];
34  memcpy(last, state->last, sizeof(last));
35 
36  ratio = state->last_len / (float)N;
37  for (i = 0; i < N/2; i++) {
38  int k = (int)(i * ratio);
39  state->last[i] = last[k];
40  }
41 
42  for (; i < FF_ARRAY_ELEMS(state->last); i++)
43  state->last[i] = 0;
44 
45  state->last_len = N;
46  }
47 
48  state->cur[3] = 0;
49  state->cur[2] = 0;
50  state->cur[1] = 0;
51  state->cur[0] = 1;
52 
53  state->state_pre = state->last[0] << 12;
54  return state->last[0] << 12;
55 }
56 
57 uint32_t ff_aac_ac_get_context(AACArithState *state, uint32_t c, int i, int N)
58 {
59  c = state->state_pre >> 8;
60  c = c + (state->last[i + 1] << 8);
61  c = (c << 4);
62  c += state->cur[1];
63 
64  state->state_pre = c;
65 
66  if (i > 3 &&
67  ((state->cur[3] + state->cur[2] + state->cur[1]) < 5))
68  return c + 0x10000;
69 
70  return c;
71 }
72 
73 uint32_t ff_aac_ac_get_pk(uint32_t c)
74 {
75  int i_min = -1;
76  int i, j;
77  int i_max = FF_ARRAY_ELEMS(ff_aac_ac_lookup_m) - 1;
78  while ((i_max - i_min) > 1) {
79  i = i_min + ((i_max - i_min) / 2);
80  j = ff_aac_ac_hash_m[i];
81  if (c < (j >> 8))
82  i_max = i;
83  else if (c > (j >> 8))
84  i_min = i;
85  else
86  return (j & 0xFF);
87  }
88  return ff_aac_ac_lookup_m[i_max];
89 }
90 
92  uint16_t a, uint16_t b)
93 {
94  state->cur[0] = a + b + 1;
95  if (state->cur[0] > 0xF)
96  state->cur[0] = 0xF;
97 
98  state->cur[3] = state->cur[2];
99  state->cur[2] = state->cur[1];
100  state->cur[1] = state->cur[0];
101 
102  state->last[idx] = state->cur[0];
103 }
104 
105 /* Initialize AC */
107 {
108  ac->low = 0;
109  ac->high = UINT16_MAX;
110  ac->val = get_bits(gb, 16);
111 }
112 
114  const uint16_t *cdf, uint16_t cdf_len)
115 {
116  int val = ac->val;
117  int low = ac->low;
118  int high = ac->high;
119 
120  int sym;
121  int rng = high - low + 1;
122  int c = ((((int)(val - low + 1)) << 14) - ((int)1));
123 
124  const uint16_t *p = cdf - 1;
125 
126  /* One for each possible CDF length in the spec */
127  switch (cdf_len) {
128  case 2:
129  if ((p[1] * rng) > c)
130  p += 1;
131  break;
132  case 4:
133  if ((p[2] * rng) > c)
134  p += 2;
135  if ((p[1] * rng) > c)
136  p += 1;
137  break;
138  case 17:
139  /* First check if the current probability is even met at all */
140  if ((p[1] * rng) <= c)
141  break;
142  p += 1;
143  for (int i = 8; i >= 1; i >>= 1)
144  if ((p[i] * rng) > c)
145  p += i;
146  break;
147  case 27:
148  if ((p[16] * rng) > c)
149  p += 16;
150  if ((p[8] * rng) > c)
151  p += 8;
152  if (p != (cdf - 1 + 24))
153  if ((p[4] * rng) > c)
154  p += 4;
155  if ((p[2] * rng) > c)
156  p += 2;
157 
158  if (p != (cdf - 1 + 24 + 2))
159  if ((p[1] * rng) > c)
160  p += 1;
161  break;
162  default:
163  /* This should never happen */
164  av_assert2(0);
165  }
166 
167  sym = (int)((ptrdiff_t)(p - cdf)) + 1;
168  if (sym)
169  high = low + ((rng * cdf[sym - 1]) >> 14) - 1;
170  low += (rng * cdf[sym]) >> 14;
171 
172  /* This loop could be done faster */
173  while (1) {
174  if (high < 32768) {
175  ;
176  } else if (low >= 32768) {
177  val -= 32768;
178  low -= 32768;
179  high -= 32768;
180  } else if (low >= 16384 && high < 49152) {
181  val -= 16384;
182  low -= 16384;
183  high -= 16384;
184  } else {
185  break;
186  }
187  low += low;
188  high += high + 1;
189  val = (val << 1) | get_bits1(gb);
190  };
191 
192  ac->low = low;
193  ac->high = high;
194  ac->val = val;
195 
196  return sym;
197 }
198 
200 {
201  int i;
202 
203  for (i = offset; i < N/2; i++)
204  state->last[i] = 1;
205 
206  for (; i < FF_ARRAY_ELEMS(state->last); i++)
207  state->last[i] = 0;
208 }
aacdec_ac.h
b
#define b
Definition: input.c:41
high
int high
Definition: dovi_rpuenc.c:38
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
AACArith
Definition: aacdec_ac.h:34
GetBitContext
Definition: get_bits.h:108
val
static double val(void *priv, double ch)
Definition: aeval.c:77
ff_aac_ac_hash_m
const uint32_t ff_aac_ac_hash_m[742]
Definition: aactab.c:1391
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
float
float
Definition: af_crystalizer.c:122
ff_aac_ac_finish
void ff_aac_ac_finish(AACArithState *state, int offset, int N)
Definition: aacdec_ac.c:199
ff_aac_ac_init
void ff_aac_ac_init(AACArith *ac, GetBitContext *gb)
Definition: aacdec_ac.c:106
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
aactab.h
ff_aac_ac_decode
uint16_t ff_aac_ac_decode(AACArith *ac, GetBitContext *gb, const uint16_t *cdf, uint16_t cdf_len)
Definition: aacdec_ac.c:113
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
ff_aac_ac_get_context
uint32_t ff_aac_ac_get_context(AACArithState *state, uint32_t c, int i, int N)
Definition: aacdec_ac.c:57
N
#define N
Definition: af_mcompand.c:54
ff_aac_ac_get_pk
uint32_t ff_aac_ac_get_pk(uint32_t c)
Definition: aacdec_ac.c:73
AACArith::val
uint16_t val
Definition: aacdec_ac.h:37
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
state
static struct @456 state
ff_aac_ac_update_context
void ff_aac_ac_update_context(AACArithState *state, int idx, uint16_t a, uint16_t b)
Definition: aacdec_ac.c:91
ff_aac_ac_map_process
uint32_t ff_aac_ac_map_process(AACArithState *state, int reset, int N)
Definition: aacdec_ac.c:25
AACArith::low
uint16_t low
Definition: aacdec_ac.h:35
AACArith::high
uint16_t high
Definition: aacdec_ac.h:36
ff_aac_ac_lookup_m
const uint8_t ff_aac_ac_lookup_m[742]
Definition: aactab.c:1341
AACArithState
Definition: aacdec_ac.h:27