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