FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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] = FFMIN(a + b + 1, 0xF);
95  state->cur[3] = state->cur[2];
96  state->cur[2] = state->cur[1];
97  state->cur[1] = state->cur[0];
98 
99  state->last[idx] = state->cur[0];
100 }
101 
102 /* Initialize AC */
104 {
105  ac->low = 0;
106  ac->high = UINT16_MAX;
107  ac->val = get_bits(gb, 16);
108 }
109 
111  const uint16_t *cdf, uint16_t cdf_len)
112 {
113  int val = ac->val;
114  int low = ac->low;
115  int high = ac->high;
116 
117  int sym;
118  int rng = high - low + 1;
119  int c = ((((int)(val - low + 1)) << 14) - ((int)1));
120 
121  const uint16_t *p = cdf - 1;
122 
123  /* One for each possible CDF length in the spec */
124  switch (cdf_len) {
125  case 2:
126  if ((p[1] * rng) > c)
127  p += 1;
128  break;
129  case 4:
130  if ((p[2] * rng) > c)
131  p += 2;
132  if ((p[1] * rng) > c)
133  p += 1;
134  break;
135  case 17:
136  /* First check if the current probability is even met at all */
137  if ((p[1] * rng) <= c)
138  break;
139  p += 1;
140  for (int i = 8; i >= 1; i >>= 1)
141  if ((p[i] * rng) > c)
142  p += i;
143  break;
144  case 27:
145  if ((p[16] * rng) > c)
146  p += 16;
147  if ((p[8] * rng) > c)
148  p += 8;
149  if (p != (cdf - 1 + 24))
150  if ((p[4] * rng) > c)
151  p += 4;
152  if ((p[2] * rng) > c)
153  p += 2;
154 
155  if (p != (cdf - 1 + 24 + 2))
156  if ((p[1] * rng) > c)
157  p += 1;
158  break;
159  default:
160  /* This should never happen */
161  av_assert2(0);
162  }
163 
164  sym = (int)((ptrdiff_t)(p - cdf)) + 1;
165  if (sym)
166  high = low + ((rng * cdf[sym - 1]) >> 14) - 1;
167  low += (rng * cdf[sym]) >> 14;
168 
169  /* This loop could be done faster */
170  while (1) {
171  if (high < 32768) {
172  ;
173  } else if (low >= 32768) {
174  val -= 32768;
175  low -= 32768;
176  high -= 32768;
177  } else if (low >= 16384 && high < 49152) {
178  val -= 16384;
179  low -= 16384;
180  high -= 16384;
181  } else {
182  break;
183  }
184  low += low;
185  high += high + 1;
186  val = (val << 1) | get_bits1(gb);
187  };
188 
189  ac->low = low;
190  ac->high = high;
191  ac->val = val;
192 
193  return sym;
194 }
195 
197 {
198  int i;
199 
200  for (i = offset; i < N/2; i++)
201  state->last[i] = 1;
202 
203  for (; i < FF_ARRAY_ELEMS(state->last); i++)
204  state->last[i] = 0;
205 }
state
static struct @508 state
aacdec_ac.h
b
#define b
Definition: input.c:42
high
int high
Definition: dovi_rpuenc.c:39
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:318
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:1387
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:196
ff_aac_ac_init
void ff_aac_ac_init(AACArith *ac, GetBitContext *gb)
Definition: aacdec_ac.c:103
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:371
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:110
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:424
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:68
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
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:1337
AACArithState
Definition: aacdec_ac.h:27