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