FFmpeg
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 
28 #include "libavcodec/get_bits.h"
29 
30 #include "opus.h"
31 
32 #define opus_ilog(i) (av_log2(i) + !!(i))
33 
34 typedef struct RawBitsContext {
35  const uint8_t *position;
36  uint32_t bytes;
37  uint32_t cachelen;
38  uint32_t cacheval;
40 
41 typedef struct OpusRangeCoder {
44  uint32_t range;
45  uint32_t value;
46  uint32_t total_bits;
47 
48  /* Encoder */
49  uint8_t buf[OPUS_MAX_FRAME_SIZE + 12]; /* memcpy vs (memmove + overreading) */
50  uint8_t *rng_cur; /* Current range coded byte */
51  int ext; /* Awaiting propagation */
52  int rem; /* Carryout flag */
53 
54  /* Encoding stats */
55  int waste;
57 
58 /**
59  * CELT: estimate bits of entropy that have thus far been consumed for the
60  * current CELT frame, to integer and fractional (1/8th bit) precision
61  */
62 static av_always_inline uint32_t opus_rc_tell(const OpusRangeCoder *rc)
63 {
64  return rc->total_bits - av_log2(rc->range) - 1;
65 }
66 
68 {
69  uint32_t i, total_bits, rcbuffer, range;
70 
71  total_bits = rc->total_bits << 3;
72  rcbuffer = av_log2(rc->range) + 1;
73  range = rc->range >> (rcbuffer-16);
74 
75  for (i = 0; i < 3; i++) {
76  int bit;
77  range = range * range >> 15;
78  bit = range >> 16;
79  rcbuffer = rcbuffer << 1 | bit;
80  range >>= bit;
81  }
82 
83  return total_bits - rcbuffer;
84 }
85 
86 uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf);
87 void ff_opus_rc_enc_cdf(OpusRangeCoder *rc, int val, const uint16_t *cdf);
88 
89 uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits);
90 void ff_opus_rc_enc_log(OpusRangeCoder *rc, int val, uint32_t bits);
91 
92 uint32_t ff_opus_rc_dec_uint_step(OpusRangeCoder *rc, int k0);
93 void ff_opus_rc_enc_uint_step(OpusRangeCoder *rc, uint32_t val, int k0);
94 
95 uint32_t ff_opus_rc_dec_uint_tri(OpusRangeCoder *rc, int qn);
96 void ff_opus_rc_enc_uint_tri(OpusRangeCoder *rc, uint32_t k, int qn);
97 
98 uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size);
99 void ff_opus_rc_enc_uint(OpusRangeCoder *rc, uint32_t val, uint32_t size);
100 
101 uint32_t ff_opus_rc_get_raw(OpusRangeCoder *rc, uint32_t count);
102 void ff_opus_rc_put_raw(OpusRangeCoder *rc, uint32_t val, uint32_t count);
103 
104 int ff_opus_rc_dec_laplace(OpusRangeCoder *rc, uint32_t symbol, int decay);
105 void ff_opus_rc_enc_laplace(OpusRangeCoder *rc, int *value, uint32_t symbol, int decay);
106 
107 int ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size);
108 void ff_opus_rc_dec_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, uint32_t bytes);
109 
110 void ff_opus_rc_enc_end(OpusRangeCoder *rc, uint8_t *dst, int size);
112 
113 #define OPUS_RC_CHECKPOINT_UPDATE(rc) \
114  rc_rollback_bits = opus_rc_tell_frac(rc); \
115  rc_rollback_ctx = *rc
116 
117 #define OPUS_RC_CHECKPOINT_SPAWN(rc) \
118  uint32_t rc_rollback_bits = opus_rc_tell_frac(rc); \
119  OpusRangeCoder rc_rollback_ctx = *rc \
120 
121 #define OPUS_RC_CHECKPOINT_BITS(rc) \
122  (opus_rc_tell_frac(rc) - rc_rollback_bits)
123 
124 #define OPUS_RC_CHECKPOINT_ROLLBACK(rc) \
125  memcpy(rc, &rc_rollback_ctx, sizeof(OpusRangeCoder)); \
126 
127 #endif /* AVCODEC_OPUS_RC_H */
RawBitsContext
Definition: rc.h:34
ff_opus_rc_dec_cdf
uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf)
Definition: rc.c:90
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: rc.h:62
OPUS_MAX_FRAME_SIZE
#define OPUS_MAX_FRAME_SIZE
Definition: opus.h:28
OpusRangeCoder::rem
int rem
Definition: rc.h:52
data
const char data[16]
Definition: mxf.c:149
opus.h
ff_opus_rc_dec_init
int ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size)
Definition: rc.c:338
ff_opus_rc_enc_log
void ff_opus_rc_enc_log(OpusRangeCoder *rc, int val, uint32_t bits)
Definition: rc.c:131
RawBitsContext::position
const uint8_t * position
Definition: rc.h:35
OpusRangeCoder::ext
int ext
Definition: rc.h:51
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:56
GetBitContext
Definition: get_bits.h:108
val
static double val(void *priv, double ch)
Definition: aeval.c:77
OpusRangeCoder::buf
uint8_t buf[OPUS_MAX_FRAME_SIZE+12]
Definition: rc.h:49
OpusRangeCoder::rng_cur
uint8_t * rng_cur
Definition: rc.h:50
RawBitsContext::cachelen
uint32_t cachelen
Definition: rc.h:37
ff_opus_rc_enc_uint_step
void ff_opus_rc_enc_uint_step(OpusRangeCoder *rc, uint32_t val, int k0)
Definition: rc.c:226
bits
uint8_t bits
Definition: vp3data.h:128
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: rc.c:204
get_bits.h
opus_rc_tell_frac
static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)
Definition: rc.h:67
OpusRangeCoder::range
uint32_t range
Definition: rc.h:44
ff_opus_rc_enc_end
void ff_opus_rc_enc_end(OpusRangeCoder *rc, uint8_t *dst, int size)
Definition: rc.c:360
OpusRangeCoder::value
uint32_t value
Definition: rc.h:45
RawBitsContext::bytes
uint32_t bytes
Definition: rc.h:36
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: rc.c:161
ff_opus_rc_enc_cdf
void ff_opus_rc_enc_cdf(OpusRangeCoder *rc, int val, const uint16_t *cdf)
Definition: rc.c:109
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
size
int size
Definition: twinvq_data.h:10344
OpusRangeCoder
Definition: rc.h:41
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: rc.c:140
range
enum AVColorRange range
Definition: mediacodec_wrapper.c:2594
ff_opus_rc_enc_init
void ff_opus_rc_enc_init(OpusRangeCoder *rc)
Definition: rc.c:402
ff_opus_rc_dec_uint_step
uint32_t ff_opus_rc_dec_uint_step(OpusRangeCoder *rc, int k0)
Definition: rc.c:211
ff_opus_rc_dec_uint
uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
CELT: read a uniform distribution.
Definition: rc.c:182
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
OpusRangeCoder::waste
int waste
Definition: rc.h:55
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
OpusRangeCoder::gb
GetBitContext gb
Definition: rc.h:42
ff_opus_rc_enc_laplace
void ff_opus_rc_enc_laplace(OpusRangeCoder *rc, int *value, uint32_t symbol, int decay)
Definition: rc.c:314
ff_opus_rc_enc_uint_tri
void ff_opus_rc_enc_uint_tri(OpusRangeCoder *rc, uint32_t k, int qn)
Definition: rc.c:258
OpusRangeCoder::rb
RawBitsContext rb
Definition: rc.h:43
ff_opus_rc_dec_log
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
Definition: rc.c:114
RawBitsContext::cacheval
uint32_t cacheval
Definition: rc.h:38
ff_opus_rc_dec_raw_init
void ff_opus_rc_dec_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, uint32_t bytes)
Definition: rc.c:352
ff_opus_rc_dec_laplace
int ff_opus_rc_dec_laplace(OpusRangeCoder *rc, uint32_t symbol, int decay)
Definition: rc.c:275
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
OpusRangeCoder::total_bits
uint32_t total_bits
Definition: rc.h:46
ff_opus_rc_dec_uint_tri
uint32_t ff_opus_rc_dec_uint_tri(OpusRangeCoder *rc, int qn)
Definition: rc.c:234