../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
72 / 74
97.3%
Branches:
35 / 40
87.5%
Functions:
7 / 7
100.0%

libavcodec/cavs_aec.h
Line Branch Exec Source
1 /*
2 * Chinese AVS video (AVS1-P16, Broadcasting profile) AEC decoder.
3 * Copyright (c) 2026 Rainbaby <r@bgr.jp>
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 /**
23 * @file
24 * Chinese AVS video (AVS1-P16, Broadcasting profile) AEC decoder
25 */
26
27 #ifndef AVCODEC_CAVS_AEC_H
28 #define AVCODEC_CAVS_AEC_H
29
30 #include <stdint.h>
31
32 #include "libavutil/attributes.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/error.h"
35 #include "libavutil/macros.h"
36
37 #include "get_bits.h"
38
39 /* Table 51's largest start index is 322, weighting_prediction, one model. */
40 #define CAVS_AEC_NUM_CTX 323
41
42 /** Table 51 start index + ctxIdxInc, or AVERROR_INVALIDDATA/AVERROR_BUG. */
43 875677 static inline int ff_cavs_aec_ctx_idx(int start, int span, int inc)
44 {
45
3/6
✓ Branch 2 → 3 taken 875677 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 875677 times.
✗ Branch 3 → 5 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 875677 times.
875677 if (span <= 0 || start < 0 || start > CAVS_AEC_NUM_CTX - span)
46 ✗ return AVERROR_BUG;
47
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 875677 times.
875677 if ((unsigned)inc >= (unsigned)span)
48 ✗ return AVERROR_INVALIDDATA;
49
50 875677 return start + inc;
51 }
52
53 typedef struct CAVSAECModel {
54 uint8_t mps; ///< mps: more probable symbol, 1 bit, init 0
55 uint8_t cycno; ///< cycno: adaptation counter, 2 bits, init 0
56 uint16_t lg_pmps; ///< lgPmps: log domain P(MPS), 11 bits, init 1023
57 } CAVSAECModel;
58
59 typedef struct CAVSAECContext {
60 GetBitContext *gb; ///< bitstream reader, shared with the slice parser
61 int rs1; ///< range exponent, init 0 (rS1)
62 int rt1; ///< range mantissa, init 0xFF (rT1)
63 int vs; ///< offset exponent (valueS)
64 int vt; ///< offset mantissa, 9 bits inside a call (valueT)
65 CAVSAECModel ctx[CAVS_AEC_NUM_CTX]; ///< context models, 8.4.2.1
66 } CAVSAECContext;
67
68 /* Constants of clauses 8.4.2 and 8.4.4.3. */
69 #define CAVS_AEC_LGPMPS_INIT 1023
70 #define CAVS_AEC_LGPMPS_MAX 1023
71 #define CAVS_AEC_LGPMPS_REFLECT 2047
72 #define CAVS_AEC_LGPMPS_BYPASS 1023
73 #define CAVS_AEC_LGPMPS_STUFF 4
74 #define CAVS_AEC_RT1_INIT 0xFF
75
76 /** Adapt one context model after a bin has been decoded, 8.4.4.3.5. */
77 4034586 static inline void cavs_aec_update_ctx(CAVSAECModel *m, int bin)
78 {
79 static const uint8_t cwr_tab[4] = { 3, 3, 4, 5 };
80 /* cwr is 3..5, the leading entries only pad the table so it indexes */
81 static const uint16_t lps_inc_tab[6] = { 0, 0, 0, 197, 95, 46 };
82 4034586 const int cwr = cwr_tab[m->cycno];
83
84
2/2
✓ Branch 2 → 3 taken 448528 times.
✓ Branch 2 → 4 taken 3586058 times.
4034586 if (bin != m->mps)
85 448528 m->cycno = FFMIN(m->cycno + 1, 3);
86
2/2
✓ Branch 4 → 5 taken 904 times.
✓ Branch 4 → 6 taken 3585154 times.
3586058 else if (m->cycno == 0)
87 904 m->cycno = 1;
88
89
2/2
✓ Branch 6 → 7 taken 3586058 times.
✓ Branch 6 → 8 taken 448528 times.
4034586 if (bin == m->mps) {
90 3586058 m->lg_pmps -= (m->lg_pmps >> cwr) + (m->lg_pmps >> (cwr + 2));
91 } else {
92 448528 m->lg_pmps += lps_inc_tab[cwr];
93
2/2
✓ Branch 8 → 9 taken 34625 times.
✓ Branch 8 → 10 taken 413903 times.
448528 if (m->lg_pmps > CAVS_AEC_LGPMPS_MAX) {
94 34625 m->lg_pmps = CAVS_AEC_LGPMPS_REFLECT - m->lg_pmps;
95 34625 m->mps ^= 1;
96 }
97 }
98 4034586 }
99
100 /** Decode one bin (8.4.4.3.2); the caller supplies predMps and lgPmps. */
101 4072808 static av_always_inline int cavs_aec_decode_bin(CAVSAECContext *aec,
102 int pred_mps, int lg_pmps)
103 {
104 4072808 GetBitContext *gb = aec->gb;
105 4072808 const int lps_range = lg_pmps >> 2;
106 int rs2, rt2, s_flag, bin;
107
108 av_assert2(lps_range > 0);
109
110
2/2
✓ Branch 2 → 3 taken 3055422 times.
✓ Branch 2 → 4 taken 1017386 times.
4072808 if (aec->rt1 >= lps_range) {
111 3055422 rs2 = aec->rs1;
112 3055422 rt2 = aec->rt1 - lps_range;
113 3055422 s_flag = 0;
114 } else {
115 1017386 rs2 = aec->rs1 + 1;
116 1017386 rt2 = 256 + aec->rt1 - lps_range;
117 1017386 s_flag = 1;
118 }
119
120
6/6
✓ Branch 5 → 6 taken 3885734 times.
✓ Branch 5 → 8 taken 187074 times.
✓ Branch 6 → 7 taken 1390356 times.
✓ Branch 6 → 20 taken 2495378 times.
✓ Branch 7 → 8 taken 356362 times.
✓ Branch 7 → 20 taken 1033994 times.
4072808 if (rs2 > aec->vs || (rs2 == aec->vs && aec->vt >= rt2)) {
121
2/2
✓ Branch 8 → 9 taken 321587 times.
✓ Branch 8 → 10 taken 221849 times.
543436 int t_rlps = s_flag ? aec->rt1 + lps_range : lps_range;
122
123 543436 bin = !pred_mps;
124
2/2
✓ Branch 11 → 12 taken 356362 times.
✓ Branch 11 → 13 taken 187074 times.
543436 if (rs2 == aec->vs)
125 356362 aec->vt -= rt2;
126 else
127 187074 aec->vt = 256 + ((aec->vt << 1) | get_bits1(gb)) - rt2;
128
2/2
✓ Branch 18 → 16 taken 798479 times.
✓ Branch 18 → 19 taken 543436 times.
1341915 while (t_rlps < 0x100) {
129 798479 t_rlps = t_rlps << 1;
130 798479 aec->vt = (aec->vt << 1) | get_bits1(gb);
131 }
132 543436 aec->rt1 = t_rlps & 0xFF;
133 } else {
134 3529372 bin = pred_mps;
135 3529372 aec->rs1 = rs2;
136 3529372 aec->rt1 = rt2;
137 }
138
139
2/2
✓ Branch 21 → 22 taken 543436 times.
✓ Branch 21 → 29 taken 3529372 times.
4072808 if (bin != pred_mps) {
140 543436 aec->rs1 = 0;
141 543436 aec->vs = 0;
142 /* the loop ends with the buffer, so truncated input terminates */
143
3/4
✓ Branch 25 → 26 taken 830305 times.
✓ Branch 25 → 28 taken 543436 times.
✓ Branch 27 → 23 taken 830305 times.
✗ Branch 27 → 28 not taken.
1373741 while (aec->vt < 0x100 && get_bits_left(gb) > 0) {
144 830305 aec->vs++;
145 830305 aec->vt = (aec->vt << 1) | get_bits1(gb);
146 }
147 543436 aec->vt &= 0xFF;
148 }
149
150 4072808 return bin;
151 }
152
153 /** Reset all contexts and start the decoder on byte aligned @p gb (8.4.1). */
154 void ff_cavs_aec_init(CAVSAECContext *aec, GetBitContext *gb);
155
156 /** Decode a bin with one context model (8.4.4.3.2). */
157 3510468 static inline int ff_cavs_aec_decode_decision(CAVSAECContext *aec, int ctx_idx)
158 {
159 CAVSAECModel *m;
160 int bin;
161
162 av_assert2(ctx_idx >= 0 && ctx_idx < CAVS_AEC_NUM_CTX);
163
164 3510468 m = &aec->ctx[ctx_idx];
165 3510468 bin = cavs_aec_decode_bin(aec, m->mps, m->lg_pmps);
166 3510468 cavs_aec_update_ctx(m, bin);
167
168 3510468 return bin;
169 }
170
171 /** Decode a bin with two weighted context models (8.4.4.3.2). */
172 262059 static inline int ff_cavs_aec_decode_decision_w(CAVSAECContext *aec,
173 int ctx_idx, int ctx_idx_w)
174 {
175 CAVSAECModel *m1, *m2;
176 int pred_mps, lg_pmps, bin;
177
178 av_assert2(ctx_idx >= 0 && ctx_idx < CAVS_AEC_NUM_CTX);
179 av_assert2(ctx_idx_w >= 0 && ctx_idx_w < CAVS_AEC_NUM_CTX);
180
181 262059 m1 = &aec->ctx[ctx_idx];
182 262059 m2 = &aec->ctx[ctx_idx_w];
183
184
2/2
✓ Branch 2 → 3 taken 227241 times.
✓ Branch 2 → 4 taken 34818 times.
262059 if (m1->mps == m2->mps) {
185 227241 pred_mps = m1->mps;
186 227241 lg_pmps = (m1->lg_pmps + m2->lg_pmps) / 2;
187
2/2
✓ Branch 4 → 5 taken 10939 times.
✓ Branch 4 → 6 taken 23879 times.
34818 } else if (m1->lg_pmps < m2->lg_pmps) {
188 10939 pred_mps = m1->mps;
189 10939 lg_pmps = CAVS_AEC_LGPMPS_MAX - ((m2->lg_pmps - m1->lg_pmps) >> 1);
190 } else {
191 23879 pred_mps = m2->mps;
192 23879 lg_pmps = CAVS_AEC_LGPMPS_MAX - ((m1->lg_pmps - m2->lg_pmps) >> 1);
193 }
194
195 262059 bin = cavs_aec_decode_bin(aec, pred_mps, lg_pmps);
196 262059 cavs_aec_update_ctx(m1, bin);
197 262059 cavs_aec_update_ctx(m2, bin);
198
199 262059 return bin;
200 }
201
202 /** Decode a bypass bin, fixed lgPmps (8.4.4.3.3). */
203 273976 static inline int ff_cavs_aec_decode_bypass(CAVSAECContext *aec)
204 {
205 273976 return cavs_aec_decode_bin(aec, 0, CAVS_AEC_LGPMPS_BYPASS);
206 }
207
208 /** Decode aec_mb_stuffing_bit, fixed lgPmps (8.4.4.3.4). */
209 26305 static inline int ff_cavs_aec_decode_stuffing_bit(CAVSAECContext *aec)
210 {
211 26305 return cavs_aec_decode_bin(aec, 0, CAVS_AEC_LGPMPS_STUFF);
212 }
213
214 #endif /* AVCODEC_CAVS_AEC_H */
215