Coverage Report

Created: 2026-05-16 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/svt-av1/Source/Lib/Codec/bitstream_unit.c
Line
Count
Source
1
/*
2
* Copyright(c) 2019 Intel Corporation
3
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
4
*
5
* This source code is subject to the terms of the BSD 2 Clause License and
6
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
7
* was not distributed with this source code in the LICENSE file, you can
8
* obtain it at https://www.aomedia.org/license/software-license. If the Alliance for Open
9
* Media Patent License 1.0 was not distributed with this source code in the
10
* PATENTS file, you can obtain it at https://www.aomedia.org/license/patent-license.
11
*/
12
13
#include <stdlib.h>
14
15
#include "bitstream_unit.h"
16
#include "definitions.h"
17
#include "utility.h"
18
#include "svt_log.h"
19
20
#if OD_MEASURE_EC_OVERHEAD
21
#include <stdio.h>
22
#endif
23
24
7.36k
static void output_bitstream_unit_dctor(EbPtr p) {
25
7.36k
    OutputBitstreamUnit* obj = (OutputBitstreamUnit*)p;
26
7.36k
    EB_FREE_ARRAY(obj->buffer_begin_av1);
27
7.36k
}
28
29
/**********************************
30
 * Constructor
31
 **********************************/
32
7.36k
EbErrorType svt_aom_output_bitstream_unit_ctor(OutputBitstreamUnit* bitstream_ptr, uint32_t buffer_size) {
33
7.36k
    bitstream_ptr->dctor = output_bitstream_unit_dctor;
34
7.36k
    if (buffer_size) {
35
7.36k
        bitstream_ptr->size = buffer_size;
36
7.36k
        EB_MALLOC_ARRAY(bitstream_ptr->buffer_begin_av1, bitstream_ptr->size);
37
7.36k
        bitstream_ptr->buffer_av1 = bitstream_ptr->buffer_begin_av1;
38
7.36k
    } else {
39
0
        bitstream_ptr->size             = 0;
40
0
        bitstream_ptr->buffer_begin_av1 = 0;
41
0
        bitstream_ptr->buffer_av1       = 0;
42
0
    }
43
44
7.36k
    return EB_ErrorNone;
45
7.36k
}
46
47
/**********************************
48
 * Reset Bitstream
49
 **********************************/
50
474
EbErrorType svt_aom_output_bitstream_reset(OutputBitstreamUnit* bitstream_ptr) {
51
474
    EbErrorType return_error = EB_ErrorNone;
52
53
    // Reset the write ptr to the beginning of the buffer
54
474
    bitstream_ptr->buffer_av1 = bitstream_ptr->buffer_begin_av1;
55
56
474
    return return_error;
57
474
}
58
59
/********************************************************************************************************************************/
60
/********************************************************************************************************************************/
61
/********************************************************************************************************************************/
62
/********************************************************************************************************************************/
63
/* Realloc when bitstream pointer size is not enough to write data of size sz */
64
0
EbErrorType svt_realloc_output_bitstream_unit(OutputBitstreamUnit* output_bitstream_ptr, uint32_t sz) {
65
0
    if (output_bitstream_ptr && sz > 0) {
66
        // Must add offset to realloc'd buffer to save any previously written bits
67
0
        uint64_t offset = output_bitstream_ptr->buffer_av1 - output_bitstream_ptr->buffer_begin_av1;
68
0
        assert(output_bitstream_ptr->buffer_av1 >= output_bitstream_ptr->buffer_begin_av1);
69
0
        output_bitstream_ptr->size = sz;
70
0
        EB_REALLOC_ARRAY(output_bitstream_ptr->buffer_begin_av1, output_bitstream_ptr->size);
71
0
        output_bitstream_ptr->buffer_av1 = output_bitstream_ptr->buffer_begin_av1 + offset;
72
0
    }
73
0
    return EB_ErrorNone;
74
0
}
75
76
/*A range encoder.
77
  See entdec.c and the references for implementation details \cite{Mar79,MNW98}.
78
79
  @INPROCEEDINGS{Mar79,
80
   author="Martin, G.N.N.",
81
   title="Range encoding: an algorithm for removing redundancy from a digitised
82
    message",
83
   booktitle="Video \& Data Recording Conference",
84
   year=1979,
85
   address="Southampton",
86
   month=Jul,
87
   URL="http://www.compressconsult.com/rangecoder/rngcod.pdf.gz"
88
  }
89
  @ARTICLE{MNW98,
90
   author="Alistair Moffat and Radford Neal and Ian H. Witten",
91
   title="Arithmetic Coding Revisited",
92
   journal="{ACM} Transactions on Information Systems",
93
   year=1998,
94
   volume=16,
95
   number=3,
96
   pages="256--294",
97
   month=Jul,
98
   URL="http://researchcommons.waikato.ac.nz/bitstream/handle/10289/78/content.pdf"
99
  }*/
100
101
/*Takes updated low and range values, renormalizes them so that
102
     32768 <= rng < 65536 (flushing bytes from low to the output buffer if
103
     necessary), and stores them back in the encoder context.
104
    low: The new value of low.
105
    rng: The new value of the range.*/
106
911k
static void svt_od_ec_enc_normalize(OdEcEnc* enc, OdEcWindow low, unsigned rng) {
107
911k
    int d;
108
911k
    int c;
109
911k
    int s;
110
911k
    if (enc->error) {
111
0
        return;
112
0
    }
113
911k
    c = enc->cnt;
114
911k
    assert(rng <= 65535U);
115
    /*The number of leading zeros in the 16-bit binary representation of rng.*/
116
911k
    d = 16 - OD_ILOG_NZ(rng);
117
911k
    s = c + d;
118
119
    /* We flush every time "low" cannot safely and efficiently accommodate any
120
       more data. Overall, c must not exceed 63 at the time of byte flush out. To
121
       facilitate this, "s" cannot exceed 56-bits because we have to keep 1 byte
122
       for carry. Also, we need to subtract 16 because we want to keep room for
123
       the next symbol worth "d"-bits (max 15). An alternate condition would be if
124
       (e < d), where e = number of leading zeros in "low", indicating there is
125
       not enough rooom to accommodate "rng" worth of "d"-bits in "low". However,
126
       this approach needs additional computations: (i) compute "e", (ii) push
127
       the leading 0x00's as a special case.
128
    */
129
911k
    if (s >= 40) { // 56 - 16
130
13.7k
        unsigned char* out     = enc->buf;
131
13.7k
        uint32_t       storage = enc->storage;
132
13.7k
        uint32_t       offs    = enc->offs;
133
13.7k
        if (offs + 8 > storage) {
134
0
            storage = 2 * storage + 8;
135
0
            EB_REALLOC_ARRAY_NO_CHECK(out, storage);
136
0
            if (out == NULL) {
137
0
                enc->error = -1;
138
0
                return;
139
0
            }
140
0
            enc->buf     = out;
141
0
            enc->storage = storage;
142
0
        }
143
        // Need to add 1 byte here since enc->cnt always counts 1 byte less
144
        // (enc->cnt = -9) to ensure correct operation
145
13.7k
        uint8_t num_bytes_ready = (s >> 3) + 1;
146
147
        // Update "c" to contain the number of non-ready bits in "low". Since "low"
148
        // has 64-bit capacity, we need to add the (64 - 40) cushion bits and take
149
        // off the number of ready bits.
150
13.7k
        c += 24 - (num_bytes_ready << 3);
151
152
        // Prepare "output" and update "low"
153
13.7k
        uint64_t output = low >> c;
154
13.7k
        low             = low & (((uint64_t)1 << c) - 1);
155
156
        // Prepare data and carry mask
157
13.7k
        uint64_t mask  = (uint64_t)1 << (num_bytes_ready << 3);
158
13.7k
        uint64_t carry = output & mask;
159
160
13.7k
        mask   = mask - 0x01;
161
13.7k
        output = output & mask;
162
163
        // Write data in a single operation
164
13.7k
        write_enc_data_to_out_buf(out, offs, output, carry, &enc->offs, num_bytes_ready);
165
166
        // Update state of the encoder: enc->cnt to contain the number of residual
167
        // bits
168
13.7k
        s = c + d - 24;
169
13.7k
    }
170
911k
    enc->low = low << d;
171
911k
    enc->rng = rng << d;
172
911k
    enc->cnt = s;
173
911k
}
174
175
/*Initializes the encoder.
176
  size: The initial size of the buffer, in bytes.*/
177
4.99k
void svt_od_ec_enc_init(OdEcEnc* enc, uint32_t size) {
178
4.99k
    svt_od_ec_enc_reset(enc);
179
4.99k
    EB_MALLOC_ARRAY_NO_CHECK(enc->buf, size);
180
4.99k
    enc->storage = size;
181
4.99k
    if (size > 0 && enc->buf == NULL) {
182
0
        enc->storage = 0;
183
0
        enc->error   = -1;
184
0
    }
185
4.99k
}
186
187
/*Reinitializes the encoder.*/
188
4.99k
void svt_od_ec_enc_reset(OdEcEnc* enc) {
189
4.99k
    enc->offs = 0;
190
4.99k
    enc->low  = 0;
191
4.99k
    enc->rng  = 0x8000;
192
    /*This is initialized to -9 so that it crosses zero after we've accumulated
193
       one byte + one carry bit.*/
194
4.99k
    enc->cnt   = -9;
195
4.99k
    enc->error = 0;
196
#if OD_MEASURE_EC_OVERHEAD
197
    enc->entropy    = 0;
198
    enc->nb_symbols = 0;
199
#endif
200
4.99k
}
201
202
/*Frees the buffers used by the encoder.*/
203
4.99k
void svt_od_ec_enc_clear(OdEcEnc* enc) {
204
4.99k
    EB_FREE_ARRAY(enc->buf);
205
4.99k
}
206
207
/*Encodes a symbol given its frequency in Q15.
208
  fl: CDF_PROB_TOP minus the cumulative frequency of all symbols that come
209
  before the one to be encoded.
210
  fh: CDF_PROB_TOP minus the cumulative frequency of all symbols up to and
211
  including the one to be encoded.*/
212
700k
static void svt_od_ec_encode_q15(OdEcEnc* enc, unsigned fl, unsigned fh, int s, int nsyms) {
213
700k
    OdEcWindow l;
214
700k
    unsigned   r;
215
700k
    l = enc->low;
216
700k
    r = enc->rng;
217
700k
    assert(32768U <= r);
218
700k
    assert(fh <= fl);
219
700k
    assert(fl <= 32768U);
220
700k
    assert(7 - EC_PROB_SHIFT >= 0);
221
700k
    const int N = nsyms - 1;
222
700k
    if (fl < CDF_PROB_TOP) {
223
266k
        unsigned u = ((r >> 8) * (uint32_t)(fl >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT)) + EC_MIN_PROB * (N - (s - 1));
224
266k
        unsigned v = ((r >> 8) * (uint32_t)(fh >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT)) + EC_MIN_PROB * (N - (s + 0));
225
266k
        l += r - u;
226
266k
        r = u - v;
227
433k
    } else {
228
433k
        r -= ((r >> 8) * (uint32_t)(fh >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT)) + EC_MIN_PROB * (N - (s + 0));
229
433k
    }
230
700k
    svt_od_ec_enc_normalize(enc, l, r);
231
#if OD_MEASURE_EC_OVERHEAD
232
    enc->entropy -= OD_LOG2((double)(OD_ICDF(fh) - OD_ICDF(fl)) / CDF_PROB_TOP.);
233
    enc->nb_symbols++;
234
#endif
235
700k
}
236
237
/*Encode a single binary value.
238
  val: The value to encode (0 or 1).
239
  f: The probability that the val is one, scaled by 32768.*/
240
211k
void svt_od_ec_encode_bool_q15(OdEcEnc* enc, int val, unsigned f) {
241
211k
    OdEcWindow l;
242
211k
    unsigned   r;
243
211k
    unsigned   v;
244
211k
    assert(0 < f);
245
211k
    assert(f < 32768U);
246
211k
    l = enc->low;
247
211k
    r = enc->rng;
248
211k
    assert(32768U <= r);
249
211k
    v = ((r >> 8) * (uint32_t)(f >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT));
250
211k
    v += EC_MIN_PROB;
251
211k
    if (val) {
252
67.8k
        l += r - v;
253
67.8k
    }
254
211k
    r = val ? v : r - v;
255
211k
    svt_od_ec_enc_normalize(enc, l, r);
256
#if OD_MEASURE_EC_OVERHEAD
257
    enc->entropy -= OD_LOG2((double)(val ? f : (32768 - f)) / 32768.);
258
    enc->nb_symbols++;
259
#endif
260
211k
}
261
262
/*Encodes a symbol given a cumulative distribution function (CDF) table in Q15.
263
  s: The index of the symbol to encode.
264
  icdf: 32768 minus the CDF, such that symbol s falls in the range
265
         [s > 0 ? (32768 - icdf[s - 1]) : 0, 32768 - icdf[s]).
266
        The values must be monotonically decreasing, and icdf[nsyms - 1] must
267
         be 0.
268
  nsyms: The number of symbols in the alphabet.
269
         This should be at most 16.*/
270
700k
void svt_od_ec_encode_cdf_q15(OdEcEnc* enc, int s, const uint16_t* icdf, int nsyms) {
271
700k
    (void)nsyms;
272
700k
    assert(s >= 0);
273
700k
    assert(s < nsyms);
274
700k
    assert(icdf[nsyms - 1] == OD_ICDF(CDF_PROB_TOP));
275
700k
    svt_od_ec_encode_q15(enc, s > 0 ? icdf[s - 1] : OD_ICDF(0), icdf[s], s, nsyms);
276
700k
}
277
278
/*Indicates that there are no more symbols to encode.
279
  All remaining output bytes are flushed to the output buffer.
280
  od_ec_enc_reset() should be called before using the encoder again.
281
  bytes: Returns the size of the encoded data in the returned buffer.
282
  Return: A pointer to the start of the final buffer, or NULL if there was an
283
           encoding error.*/
284
4.99k
unsigned char* svt_od_ec_enc_done(OdEcEnc* enc, uint32_t* nbytes) {
285
4.99k
    unsigned char* out;
286
4.99k
    uint32_t       storage;
287
4.99k
    uint32_t       offs;
288
4.99k
    OdEcWindow     m;
289
4.99k
    OdEcWindow     e;
290
4.99k
    OdEcWindow     l;
291
4.99k
    int            c;
292
4.99k
    int            s;
293
4.99k
    if (enc->error) {
294
0
        return NULL;
295
0
    }
296
#if OD_MEASURE_EC_OVERHEAD
297
    {
298
        uint32_t tell;
299
        /* Don't count the 1 bit we lose to raw bits as overhead. */
300
        tell = od_ec_enc_tell(enc) - 1;
301
        fprintf(stderr, "overhead: %f%%\n", 100 * (tell - enc->entropy) / enc->entropy);
302
        fprintf(stderr, "efficiency: %f bits/symbol\n", (double)tell / enc->nb_symbols);
303
    }
304
#endif
305
306
4.99k
    l = enc->low;
307
4.99k
    c = enc->cnt;
308
4.99k
    s = 10;
309
4.99k
    m = 0x3FFF;
310
4.99k
    e = ((l + m) & ~m) | (m + 1);
311
4.99k
    s += c;
312
4.99k
    offs = enc->offs;
313
314
    /*Make sure there's enough room for the entropy-coded bits.*/
315
4.99k
    out              = enc->buf;
316
4.99k
    storage          = enc->storage;
317
4.99k
    const int s_bits = (s + 7) >> 3;
318
4.99k
    int       b      = MAX(s_bits, 0);
319
4.99k
    if (offs + b > storage) {
320
0
        storage = offs + b;
321
0
        EB_REALLOC_ARRAY_NO_CHECK(out, storage);
322
0
        if (out == NULL) {
323
0
            enc->error = -1;
324
0
            return NULL;
325
0
        }
326
0
        enc->buf     = out;
327
0
        enc->storage = storage;
328
0
    }
329
330
    /*We output the minimum number of bits that ensures that the symbols encoded
331
       thus far will be decoded correctly regardless of the bits that follow.*/
332
4.99k
    if (s > 0) {
333
4.99k
        uint64_t n;
334
4.99k
        n = ((uint64_t)1 << (c + 16)) - 1;
335
19.1k
        do {
336
19.1k
            assert(offs < storage);
337
19.1k
            uint16_t val = (uint16_t)(e >> (c + 16));
338
19.1k
            out[offs]    = (unsigned char)(val & 0x00FF);
339
19.1k
            if (val & 0x0100) {
340
2.00k
                assert(offs > 0);
341
2.00k
                propagate_carry_bwd(out, offs - 1);
342
2.00k
            }
343
19.1k
            offs++;
344
345
19.1k
            e &= n;
346
19.1k
            s -= 8;
347
19.1k
            c -= 8;
348
19.1k
            n >>= 8;
349
19.1k
        } while (s > 0);
350
4.99k
    }
351
4.99k
    *nbytes = offs;
352
353
4.99k
    return out;
354
4.99k
}
355
356
/*Returns the number of bits "used" by the encoded symbols so far.
357
  This same number can be computed in either the encoder or the decoder, and is
358
   suitable for making coding decisions.
359
  Warning: The value returned by this function can decrease compared to an
360
   earlier call, even after encoding more data, if there is an encoding error
361
   (i.e., a failure to allocate enough space for the output buffer).
362
  Return: The number of bits.
363
          This will always be slightly larger than the exact value (e.g., all
364
           rounding error is in the positive direction).*/
365
4.99k
int svt_od_ec_enc_tell(const OdEcEnc* enc) {
366
    /*The 10 here counteracts the offset of -9 baked into cnt, and adds 1 extra
367
       bit, which we reserve for terminating the stream.*/
368
4.99k
    return (enc->cnt + 10) + enc->offs * 8;
369
4.99k
}
370
371
/*Given the current total integer number of bits used and the current value of
372
   rng, computes the fraction number of bits used to OD_BITRES precision.
373
  This is used by od_ec_enc_tell_frac() and od_ec_dec_tell_frac().
374
  nbits_total: The number of whole bits currently used, i.e., the value
375
                returned by od_ec_enc_tell() or od_ec_dec_tell().
376
  rng: The current value of rng from either the encoder or decoder state.
377
  Return: The number of bits scaled by 2**OD_BITRES.
378
          This will always be slightly larger than the exact value (e.g., all
379
           rounding error is in the positive direction).*/
380
0
uint32_t svt_od_ec_tell_frac(uint32_t nbits_total, uint32_t rng) {
381
0
    uint32_t nbits;
382
0
    int      l;
383
0
    int      i;
384
    /*To handle the non-integral number of bits still left in the encoder/decoder
385
       state, we compute the worst-case number of bits of val that must be
386
       encoded to ensure that the value is inside the range for any possible
387
       subsequent bits.
388
      The computation here is independent of val itself (the decoder does not
389
       even track that value), even though the real number of bits used after
390
       od_ec_enc_done() may be 1 smaller if rng is a power of two and the
391
       corresponding trailing bits of val are all zeros.
392
      If we did try to track that special case, then coding a value with a
393
       probability of 1/(1 << n) might sometimes appear to use more than n bits.
394
      This may help explain the surprising result that a newly initialized
395
       encoder or decoder claims to have used 1 bit.*/
396
0
    nbits = nbits_total << OD_BITRES;
397
0
    l     = 0;
398
0
    for (i = OD_BITRES; i-- > 0;) {
399
0
        int b;
400
0
        rng = rng * rng >> 15;
401
0
        b   = (int)(rng >> 16);
402
0
        l   = l << 1 | b;
403
0
        rng >>= b;
404
0
    }
405
0
    return nbits - l;
406
0
}
407
408
/*Returns the number of bits "used" by the encoded symbols so far.
409
  This same number can be computed in either the encoder or the decoder, and is
410
   suitable for making coding decisions.
411
  Warning: The value returned by this function can decrease compared to an
412
   earlier call, even after encoding more data, if there is an encoding error
413
   (i.e., a failure to allocate enough space for the output buffer).
414
  Return: The number of bits scaled by 2**OD_BITRES.
415
          This will always be slightly larger than the exact value (e.g., all
416
           rounding error is in the positive direction).*/
417
0
uint32_t svt_od_ec_enc_tell_frac(const OdEcEnc* enc) {
418
0
    return svt_od_ec_tell_frac(svt_od_ec_enc_tell(enc), enc->rng);
419
0
}
420
421
/********************************************************************************************************************************/
422
/********************************************************************************************************************************/
423
/********************************************************************************************************************************/