]> git.sesse.net Git - ffmpeg/blob - libavcodec/opus_rc.c
avcodec/bsf: fix resource leak in av_bsf_list_parse_str
[ffmpeg] / libavcodec / opus_rc.c
1 /*
2  * Copyright (c) 2012 Andrew D'Addesio
3  * Copyright (c) 2013-2014 Mozilla Corporation
4  * Copyright (c) 2016 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 #include "opus_rc.h"
24
25 static av_always_inline void opus_rc_dec_normalize(OpusRangeCoder *rc)
26 {
27     while (rc->range <= 1<<23) {
28         rc->value = ((rc->value << 8) | (get_bits(&rc->gb, 8) ^ 0xFF)) & ((1u << 31) - 1);
29         rc->range          <<= 8;
30         rc->total_read_bits += 8;
31     }
32 }
33
34 static av_always_inline void opus_rc_dec_update(OpusRangeCoder *rc, uint32_t scale,
35                                                 uint32_t low, uint32_t high,
36                                                 uint32_t total)
37 {
38     rc->value -= scale * (total - high);
39     rc->range  = low ? scale * (high - low)
40                       : rc->range - scale * (total - high);
41     opus_rc_dec_normalize(rc);
42 }
43
44 uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf)
45 {
46     unsigned int k, scale, total, symbol, low, high;
47
48     total = *cdf++;
49
50     scale   = rc->range / total;
51     symbol = rc->value / scale + 1;
52     symbol = total - FFMIN(symbol, total);
53
54     for (k = 0; cdf[k] <= symbol; k++);
55     high = cdf[k];
56     low  = k ? cdf[k-1] : 0;
57
58     opus_rc_dec_update(rc, scale, low, high, total);
59
60     return k;
61 }
62
63 uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
64 {
65     uint32_t k, scale;
66     scale = rc->range >> bits; // in this case, scale = symbol
67
68     if (rc->value >= scale) {
69         rc->value -= scale;
70         rc->range -= scale;
71         k = 0;
72     } else {
73         rc->range = scale;
74         k = 1;
75     }
76     opus_rc_dec_normalize(rc);
77     return k;
78 }
79
80 /**
81  * CELT: read 1-25 raw bits at the end of the frame, backwards byte-wise
82  */
83 uint32_t ff_opus_rc_get_raw(OpusRangeCoder *rc, uint32_t count)
84 {
85     uint32_t value = 0;
86
87     while (rc->rb.bytes && rc->rb.cachelen < count) {
88         rc->rb.cacheval |= *--rc->rb.position << rc->rb.cachelen;
89         rc->rb.cachelen += 8;
90         rc->rb.bytes--;
91     }
92
93     value = av_mod_uintp2(rc->rb.cacheval, count);
94     rc->rb.cacheval    >>= count;
95     rc->rb.cachelen     -= count;
96     rc->total_read_bits += count;
97
98     return value;
99 }
100
101 /**
102  * CELT: read a uniform distribution
103  */
104 uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
105 {
106     uint32_t bits, k, scale, total;
107
108     bits  = opus_ilog(size - 1);
109     total = (bits > 8) ? ((size - 1) >> (bits - 8)) + 1 : size;
110
111     scale  = rc->range / total;
112     k      = rc->value / scale + 1;
113     k      = total - FFMIN(k, total);
114     opus_rc_dec_update(rc, scale, k, k + 1, total);
115
116     if (bits > 8) {
117         k = k << (bits - 8) | ff_opus_rc_get_raw(rc, bits - 8);
118         return FFMIN(k, size - 1);
119     } else
120         return k;
121 }
122
123 uint32_t ff_opus_rc_dec_uint_step(OpusRangeCoder *rc, int k0)
124 {
125     /* Use a probability of 3 up to itheta=8192 and then use 1 after */
126     uint32_t k, scale, symbol, total = (k0+1)*3 + k0;
127     scale  = rc->range / total;
128     symbol = rc->value / scale + 1;
129     symbol = total - FFMIN(symbol, total);
130
131     k = (symbol < (k0+1)*3) ? symbol/3 : symbol - (k0+1)*2;
132
133     opus_rc_dec_update(rc, scale, (k <= k0) ? 3*(k+0) : (k-1-k0) + 3*(k0+1),
134                        (k <= k0) ? 3*(k+1) : (k-0-k0) + 3*(k0+1), total);
135     return k;
136 }
137
138 uint32_t ff_opus_rc_dec_uint_tri(OpusRangeCoder *rc, int qn)
139 {
140     uint32_t k, scale, symbol, total, low, center;
141
142     total = ((qn>>1) + 1) * ((qn>>1) + 1);
143     scale   = rc->range / total;
144     center = rc->value / scale + 1;
145     center = total - FFMIN(center, total);
146
147     if (center < total >> 1) {
148         k      = (ff_sqrt(8 * center + 1) - 1) >> 1;
149         low    = k * (k + 1) >> 1;
150         symbol = k + 1;
151     } else {
152         k      = (2*(qn + 1) - ff_sqrt(8*(total - center - 1) + 1)) >> 1;
153         low    = total - ((qn + 1 - k) * (qn + 2 - k) >> 1);
154         symbol = qn + 1 - k;
155     }
156
157     opus_rc_dec_update(rc, scale, low, low + symbol, total);
158
159     return k;
160 }
161
162 int ff_opus_rc_dec_laplace(OpusRangeCoder *rc, uint32_t symbol, int decay)
163 {
164     /* extends the range coder to model a Laplace distribution */
165     int value = 0;
166     uint32_t scale, low = 0, center;
167
168     scale  = rc->range >> 15;
169     center = rc->value / scale + 1;
170     center = (1 << 15) - FFMIN(center, 1 << 15);
171
172     if (center >= symbol) {
173         value++;
174         low = symbol;
175         symbol = 1 + ((32768 - 32 - symbol) * (16384-decay) >> 15);
176
177         while (symbol > 1 && center >= low + 2 * symbol) {
178             value++;
179             symbol *= 2;
180             low    += symbol;
181             symbol  = (((symbol - 2) * decay) >> 15) + 1;
182         }
183
184         if (symbol <= 1) {
185             int distance = (center - low) >> 1;
186             value += distance;
187             low   += 2 * distance;
188         }
189
190         if (center < low + symbol)
191             value *= -1;
192         else
193             low += symbol;
194     }
195
196     opus_rc_dec_update(rc, scale, low, FFMIN(low + symbol, 32768), 32768);
197
198     return value;
199 }
200
201 int ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size)
202 {
203     int ret = init_get_bits8(&rc->gb, data, size);
204     if (ret < 0)
205         return ret;
206
207     rc->range = 128;
208     rc->value = 127 - get_bits(&rc->gb, 7);
209     rc->total_read_bits = 9;
210     opus_rc_dec_normalize(rc);
211
212     return 0;
213 }
214
215 void ff_opus_rc_dec_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, uint32_t bytes)
216 {
217     rc->rb.position = rightend;
218     rc->rb.bytes    = bytes;
219     rc->rb.cachelen = 0;
220     rc->rb.cacheval = 0;
221 }