1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * Copyright (c) 2012 Andrew D'Addesio
3cabdff1aSopenharmony_ci * Copyright (c) 2013-2014 Mozilla Corporation
4cabdff1aSopenharmony_ci * Copyright (c) 2017 Rostislav Pehlivanov <atomnuker@gmail.com>
5cabdff1aSopenharmony_ci *
6cabdff1aSopenharmony_ci * This file is part of FFmpeg.
7cabdff1aSopenharmony_ci *
8cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
9cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
10cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
11cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
12cabdff1aSopenharmony_ci *
13cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
14cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
15cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16cabdff1aSopenharmony_ci * Lesser General Public License for more details.
17cabdff1aSopenharmony_ci *
18cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
19cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
20cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21cabdff1aSopenharmony_ci */
22cabdff1aSopenharmony_ci
23cabdff1aSopenharmony_ci#ifndef AVCODEC_OPUS_RC_H
24cabdff1aSopenharmony_ci#define AVCODEC_OPUS_RC_H
25cabdff1aSopenharmony_ci
26cabdff1aSopenharmony_ci#include <stdint.h>
27cabdff1aSopenharmony_ci#include "get_bits.h"
28cabdff1aSopenharmony_ci
29cabdff1aSopenharmony_ci#define OPUS_MAX_PACKET_SIZE 1275
30cabdff1aSopenharmony_ci
31cabdff1aSopenharmony_ci#define opus_ilog(i) (av_log2(i) + !!(i))
32cabdff1aSopenharmony_ci
33cabdff1aSopenharmony_citypedef struct RawBitsContext {
34cabdff1aSopenharmony_ci    const uint8_t *position;
35cabdff1aSopenharmony_ci    uint32_t bytes;
36cabdff1aSopenharmony_ci    uint32_t cachelen;
37cabdff1aSopenharmony_ci    uint32_t cacheval;
38cabdff1aSopenharmony_ci} RawBitsContext;
39cabdff1aSopenharmony_ci
40cabdff1aSopenharmony_citypedef struct OpusRangeCoder {
41cabdff1aSopenharmony_ci    GetBitContext gb;
42cabdff1aSopenharmony_ci    RawBitsContext rb;
43cabdff1aSopenharmony_ci    uint32_t range;
44cabdff1aSopenharmony_ci    uint32_t value;
45cabdff1aSopenharmony_ci    uint32_t total_bits;
46cabdff1aSopenharmony_ci
47cabdff1aSopenharmony_ci    /* Encoder */
48cabdff1aSopenharmony_ci    uint8_t buf[OPUS_MAX_PACKET_SIZE + 12]; /* memcpy vs (memmove + overreading) */
49cabdff1aSopenharmony_ci    uint8_t *rng_cur;                      /* Current range coded byte */
50cabdff1aSopenharmony_ci    int ext;                               /* Awaiting propagation */
51cabdff1aSopenharmony_ci    int rem;                               /* Carryout flag */
52cabdff1aSopenharmony_ci
53cabdff1aSopenharmony_ci    /* Encoding stats */
54cabdff1aSopenharmony_ci    int waste;
55cabdff1aSopenharmony_ci} OpusRangeCoder;
56cabdff1aSopenharmony_ci
57cabdff1aSopenharmony_ci/**
58cabdff1aSopenharmony_ci * CELT: estimate bits of entropy that have thus far been consumed for the
59cabdff1aSopenharmony_ci *       current CELT frame, to integer and fractional (1/8th bit) precision
60cabdff1aSopenharmony_ci */
61cabdff1aSopenharmony_cistatic av_always_inline uint32_t opus_rc_tell(const OpusRangeCoder *rc)
62cabdff1aSopenharmony_ci{
63cabdff1aSopenharmony_ci    return rc->total_bits - av_log2(rc->range) - 1;
64cabdff1aSopenharmony_ci}
65cabdff1aSopenharmony_ci
66cabdff1aSopenharmony_cistatic av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)
67cabdff1aSopenharmony_ci{
68cabdff1aSopenharmony_ci    uint32_t i, total_bits, rcbuffer, range;
69cabdff1aSopenharmony_ci
70cabdff1aSopenharmony_ci    total_bits = rc->total_bits << 3;
71cabdff1aSopenharmony_ci    rcbuffer   = av_log2(rc->range) + 1;
72cabdff1aSopenharmony_ci    range      = rc->range >> (rcbuffer-16);
73cabdff1aSopenharmony_ci
74cabdff1aSopenharmony_ci    for (i = 0; i < 3; i++) {
75cabdff1aSopenharmony_ci        int bit;
76cabdff1aSopenharmony_ci        range = range * range >> 15;
77cabdff1aSopenharmony_ci        bit = range >> 16;
78cabdff1aSopenharmony_ci        rcbuffer = rcbuffer << 1 | bit;
79cabdff1aSopenharmony_ci        range >>= bit;
80cabdff1aSopenharmony_ci    }
81cabdff1aSopenharmony_ci
82cabdff1aSopenharmony_ci    return total_bits - rcbuffer;
83cabdff1aSopenharmony_ci}
84cabdff1aSopenharmony_ci
85cabdff1aSopenharmony_ciuint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf);
86cabdff1aSopenharmony_civoid     ff_opus_rc_enc_cdf(OpusRangeCoder *rc, int val, const uint16_t *cdf);
87cabdff1aSopenharmony_ci
88cabdff1aSopenharmony_ciuint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits);
89cabdff1aSopenharmony_civoid     ff_opus_rc_enc_log(OpusRangeCoder *rc, int val, uint32_t bits);
90cabdff1aSopenharmony_ci
91cabdff1aSopenharmony_ciuint32_t ff_opus_rc_dec_uint_step(OpusRangeCoder *rc, int k0);
92cabdff1aSopenharmony_civoid     ff_opus_rc_enc_uint_step(OpusRangeCoder *rc, uint32_t val, int k0);
93cabdff1aSopenharmony_ci
94cabdff1aSopenharmony_ciuint32_t ff_opus_rc_dec_uint_tri(OpusRangeCoder *rc, int qn);
95cabdff1aSopenharmony_civoid     ff_opus_rc_enc_uint_tri(OpusRangeCoder *rc, uint32_t k, int qn);
96cabdff1aSopenharmony_ci
97cabdff1aSopenharmony_ciuint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size);
98cabdff1aSopenharmony_civoid     ff_opus_rc_enc_uint(OpusRangeCoder *rc, uint32_t val, uint32_t size);
99cabdff1aSopenharmony_ci
100cabdff1aSopenharmony_ciuint32_t ff_opus_rc_get_raw(OpusRangeCoder *rc, uint32_t count);
101cabdff1aSopenharmony_civoid     ff_opus_rc_put_raw(OpusRangeCoder *rc, uint32_t val, uint32_t count);
102cabdff1aSopenharmony_ci
103cabdff1aSopenharmony_ciint      ff_opus_rc_dec_laplace(OpusRangeCoder *rc, uint32_t symbol, int decay);
104cabdff1aSopenharmony_civoid     ff_opus_rc_enc_laplace(OpusRangeCoder *rc, int *value, uint32_t symbol, int decay);
105cabdff1aSopenharmony_ci
106cabdff1aSopenharmony_ciint      ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size);
107cabdff1aSopenharmony_civoid     ff_opus_rc_dec_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, uint32_t bytes);
108cabdff1aSopenharmony_ci
109cabdff1aSopenharmony_civoid     ff_opus_rc_enc_end(OpusRangeCoder *rc, uint8_t *dst, int size);
110cabdff1aSopenharmony_civoid     ff_opus_rc_enc_init(OpusRangeCoder *rc);
111cabdff1aSopenharmony_ci
112cabdff1aSopenharmony_ci#define OPUS_RC_CHECKPOINT_UPDATE(rc) \
113cabdff1aSopenharmony_ci    rc_rollback_bits = opus_rc_tell_frac(rc); \
114cabdff1aSopenharmony_ci    rc_rollback_ctx  = *rc
115cabdff1aSopenharmony_ci
116cabdff1aSopenharmony_ci#define OPUS_RC_CHECKPOINT_SPAWN(rc) \
117cabdff1aSopenharmony_ci    uint32_t rc_rollback_bits = opus_rc_tell_frac(rc); \
118cabdff1aSopenharmony_ci    OpusRangeCoder rc_rollback_ctx = *rc \
119cabdff1aSopenharmony_ci
120cabdff1aSopenharmony_ci#define OPUS_RC_CHECKPOINT_BITS(rc) \
121cabdff1aSopenharmony_ci    (opus_rc_tell_frac(rc) - rc_rollback_bits)
122cabdff1aSopenharmony_ci
123cabdff1aSopenharmony_ci#define OPUS_RC_CHECKPOINT_ROLLBACK(rc) \
124cabdff1aSopenharmony_ci    memcpy(rc, &rc_rollback_ctx, sizeof(OpusRangeCoder)); \
125cabdff1aSopenharmony_ci
126cabdff1aSopenharmony_ci#endif /* AVCODEC_OPUS_RC_H */
127