You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

431 lines
12KB

  1. /*
  2. * Opus decoder/demuxer common functions
  3. * Copyright (c) 2012 Andrew D'Addesio
  4. * Copyright (c) 2013-2014 Mozilla Corporation
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef AVCODEC_OPUS_H
  23. #define AVCODEC_OPUS_H
  24. #include <stdint.h>
  25. #include "libavutil/audio_fifo.h"
  26. #include "libavutil/float_dsp.h"
  27. #include "libavutil/frame.h"
  28. #include "libavresample/avresample.h"
  29. #include "avcodec.h"
  30. #include "bitstream.h"
  31. #define MAX_FRAME_SIZE 1275
  32. #define MAX_FRAMES 48
  33. #define MAX_PACKET_DUR 5760
  34. #define CELT_SHORT_BLOCKSIZE 120
  35. #define CELT_OVERLAP CELT_SHORT_BLOCKSIZE
  36. #define CELT_MAX_LOG_BLOCKS 3
  37. #define CELT_MAX_FRAME_SIZE (CELT_SHORT_BLOCKSIZE * (1 << CELT_MAX_LOG_BLOCKS))
  38. #define CELT_MAX_BANDS 21
  39. #define CELT_VECTORS 11
  40. #define CELT_ALLOC_STEPS 6
  41. #define CELT_FINE_OFFSET 21
  42. #define CELT_MAX_FINE_BITS 8
  43. #define CELT_NORM_SCALE 16384
  44. #define CELT_QTHETA_OFFSET 4
  45. #define CELT_QTHETA_OFFSET_TWOPHASE 16
  46. #define CELT_DEEMPH_COEFF 0.85000610f
  47. #define CELT_POSTFILTER_MINPERIOD 15
  48. #define CELT_ENERGY_SILENCE (-28.0f)
  49. #define SILK_HISTORY 322
  50. #define SILK_MAX_LPC 16
  51. #define ROUND_MULL(a,b,s) (((MUL64(a, b) >> (s - 1)) + 1) >> 1)
  52. #define ROUND_MUL16(a,b) ((MUL16(a, b) + 16384) >> 15)
  53. #define opus_ilog(i) (av_log2(i) + !!(i))
  54. #define OPUS_TS_HEADER 0x7FE0 // 0x3ff (11 bits)
  55. #define OPUS_TS_MASK 0xFFE0 // top 11 bits
  56. static const uint8_t opus_default_extradata[30] = {
  57. 'O', 'p', 'u', 's', 'H', 'e', 'a', 'd',
  58. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  60. };
  61. enum OpusMode {
  62. OPUS_MODE_SILK,
  63. OPUS_MODE_HYBRID,
  64. OPUS_MODE_CELT
  65. };
  66. enum OpusBandwidth {
  67. OPUS_BANDWIDTH_NARROWBAND,
  68. OPUS_BANDWIDTH_MEDIUMBAND,
  69. OPUS_BANDWIDTH_WIDEBAND,
  70. OPUS_BANDWIDTH_SUPERWIDEBAND,
  71. OPUS_BANDWIDTH_FULLBAND
  72. };
  73. typedef struct RawBitsContext {
  74. const uint8_t *position;
  75. unsigned int bytes;
  76. unsigned int cachelen;
  77. unsigned int cacheval;
  78. } RawBitsContext;
  79. typedef struct OpusRangeCoder {
  80. BitstreamContext bc;
  81. RawBitsContext rb;
  82. unsigned int range;
  83. unsigned int value;
  84. unsigned int total_read_bits;
  85. } OpusRangeCoder;
  86. typedef struct SilkContext SilkContext;
  87. typedef struct CeltContext CeltContext;
  88. typedef struct OpusPacket {
  89. int packet_size; /** packet size */
  90. int data_size; /** size of the useful data -- packet size - padding */
  91. int code; /** packet code: specifies the frame layout */
  92. int stereo; /** whether this packet is mono or stereo */
  93. int vbr; /** vbr flag */
  94. int config; /** configuration: tells the audio mode,
  95. ** bandwidth, and frame duration */
  96. int frame_count; /** frame count */
  97. int frame_offset[MAX_FRAMES]; /** frame offsets */
  98. int frame_size[MAX_FRAMES]; /** frame sizes */
  99. int frame_duration; /** frame duration, in samples @ 48kHz */
  100. enum OpusMode mode; /** mode */
  101. enum OpusBandwidth bandwidth; /** bandwidth */
  102. } OpusPacket;
  103. typedef struct OpusStreamContext {
  104. AVCodecContext *avctx;
  105. int output_channels;
  106. OpusRangeCoder rc;
  107. OpusRangeCoder redundancy_rc;
  108. SilkContext *silk;
  109. CeltContext *celt;
  110. AVFloatDSPContext *fdsp;
  111. float silk_buf[2][960];
  112. float *silk_output[2];
  113. DECLARE_ALIGNED(32, float, celt_buf)[2][960];
  114. float *celt_output[2];
  115. float redundancy_buf[2][960];
  116. float *redundancy_output[2];
  117. /* data buffers for the final output data */
  118. float *out[2];
  119. int out_size;
  120. float *out_dummy;
  121. int out_dummy_allocated_size;
  122. AVAudioResampleContext *avr;
  123. AVAudioFifo *celt_delay;
  124. int silk_samplerate;
  125. /* number of samples we still want to get from the resampler */
  126. int delayed_samples;
  127. OpusPacket packet;
  128. int redundancy_idx;
  129. } OpusStreamContext;
  130. // a mapping between an opus stream and an output channel
  131. typedef struct ChannelMap {
  132. int stream_idx;
  133. int channel_idx;
  134. // when a single decoded channel is mapped to multiple output channels, we
  135. // write to the first output directly and copy from it to the others
  136. // this field is set to 1 for those copied output channels
  137. int copy;
  138. // this is the index of the output channel to copy from
  139. int copy_idx;
  140. // this channel is silent
  141. int silence;
  142. } ChannelMap;
  143. typedef struct OpusContext {
  144. OpusStreamContext *streams;
  145. /* current output buffers for each streams */
  146. float **out;
  147. int *out_size;
  148. /* Buffers for synchronizing the streams when they have different
  149. * resampling delays */
  150. AVAudioFifo **sync_buffers;
  151. /* number of decoded samples for each stream */
  152. int *decoded_samples;
  153. int nb_streams;
  154. int nb_stereo_streams;
  155. AVFloatDSPContext fdsp;
  156. int16_t gain_i;
  157. float gain;
  158. ChannelMap *channel_maps;
  159. } OpusContext;
  160. static av_always_inline void opus_rc_normalize(OpusRangeCoder *rc)
  161. {
  162. while (rc->range <= 1<<23) {
  163. rc->value = ((rc->value << 8) | (bitstream_read(&rc->bc, 8) ^ 0xFF)) & ((1u << 31) - 1);
  164. rc->range <<= 8;
  165. rc->total_read_bits += 8;
  166. }
  167. }
  168. static av_always_inline void opus_rc_update(OpusRangeCoder *rc, unsigned int scale,
  169. unsigned int low, unsigned int high,
  170. unsigned int total)
  171. {
  172. rc->value -= scale * (total - high);
  173. rc->range = low ? scale * (high - low)
  174. : rc->range - scale * (total - high);
  175. opus_rc_normalize(rc);
  176. }
  177. static av_always_inline unsigned int opus_rc_getsymbol(OpusRangeCoder *rc, const uint16_t *cdf)
  178. {
  179. unsigned int k, scale, total, symbol, low, high;
  180. total = *cdf++;
  181. scale = rc->range / total;
  182. symbol = rc->value / scale + 1;
  183. symbol = total - FFMIN(symbol, total);
  184. for (k = 0; cdf[k] <= symbol; k++);
  185. high = cdf[k];
  186. low = k ? cdf[k-1] : 0;
  187. opus_rc_update(rc, scale, low, high, total);
  188. return k;
  189. }
  190. static av_always_inline unsigned int opus_rc_p2model(OpusRangeCoder *rc, unsigned int bits)
  191. {
  192. unsigned int k, scale;
  193. scale = rc->range >> bits; // in this case, scale = symbol
  194. if (rc->value >= scale) {
  195. rc->value -= scale;
  196. rc->range -= scale;
  197. k = 0;
  198. } else {
  199. rc->range = scale;
  200. k = 1;
  201. }
  202. opus_rc_normalize(rc);
  203. return k;
  204. }
  205. /**
  206. * CELT: estimate bits of entropy that have thus far been consumed for the
  207. * current CELT frame, to integer and fractional (1/8th bit) precision
  208. */
  209. static av_always_inline unsigned int opus_rc_tell(const OpusRangeCoder *rc)
  210. {
  211. return rc->total_read_bits - av_log2(rc->range) - 1;
  212. }
  213. static av_always_inline unsigned int opus_rc_tell_frac(const OpusRangeCoder *rc)
  214. {
  215. unsigned int i, total_bits, rcbuffer, range;
  216. total_bits = rc->total_read_bits << 3;
  217. rcbuffer = av_log2(rc->range) + 1;
  218. range = rc->range >> (rcbuffer-16);
  219. for (i = 0; i < 3; i++) {
  220. int bit;
  221. range = range * range >> 15;
  222. bit = range >> 16;
  223. rcbuffer = rcbuffer << 1 | bit;
  224. range >>= bit;
  225. }
  226. return total_bits - rcbuffer;
  227. }
  228. /**
  229. * CELT: read 1-25 raw bits at the end of the frame, backwards byte-wise
  230. */
  231. static av_always_inline unsigned int opus_getrawbits(OpusRangeCoder *rc, unsigned int count)
  232. {
  233. unsigned int value = 0;
  234. while (rc->rb.bytes && rc->rb.cachelen < count) {
  235. rc->rb.cacheval |= *--rc->rb.position << rc->rb.cachelen;
  236. rc->rb.cachelen += 8;
  237. rc->rb.bytes--;
  238. }
  239. value = rc->rb.cacheval & ((1<<count)-1);
  240. rc->rb.cacheval >>= count;
  241. rc->rb.cachelen -= count;
  242. rc->total_read_bits += count;
  243. return value;
  244. }
  245. /**
  246. * CELT: read a uniform distribution
  247. */
  248. static av_always_inline unsigned int opus_rc_unimodel(OpusRangeCoder *rc, unsigned int size)
  249. {
  250. unsigned int bits, k, scale, total;
  251. bits = opus_ilog(size - 1);
  252. total = (bits > 8) ? ((size - 1) >> (bits - 8)) + 1 : size;
  253. scale = rc->range / total;
  254. k = rc->value / scale + 1;
  255. k = total - FFMIN(k, total);
  256. opus_rc_update(rc, scale, k, k + 1, total);
  257. if (bits > 8) {
  258. k = k << (bits - 8) | opus_getrawbits(rc, bits - 8);
  259. return FFMIN(k, size - 1);
  260. } else
  261. return k;
  262. }
  263. static av_always_inline int opus_rc_laplace(OpusRangeCoder *rc, unsigned int symbol, int decay)
  264. {
  265. /* extends the range coder to model a Laplace distribution */
  266. int value = 0;
  267. unsigned int scale, low = 0, center;
  268. scale = rc->range >> 15;
  269. center = rc->value / scale + 1;
  270. center = (1 << 15) - FFMIN(center, 1 << 15);
  271. if (center >= symbol) {
  272. value++;
  273. low = symbol;
  274. symbol = 1 + ((32768 - 32 - symbol) * (16384-decay) >> 15);
  275. while (symbol > 1 && center >= low + 2 * symbol) {
  276. value++;
  277. symbol *= 2;
  278. low += symbol;
  279. symbol = (((symbol - 2) * decay) >> 15) + 1;
  280. }
  281. if (symbol <= 1) {
  282. int distance = (center - low) >> 1;
  283. value += distance;
  284. low += 2 * distance;
  285. }
  286. if (center < low + symbol)
  287. value *= -1;
  288. else
  289. low += symbol;
  290. }
  291. opus_rc_update(rc, scale, low, FFMIN(low + symbol, 32768), 32768);
  292. return value;
  293. }
  294. static av_always_inline unsigned int opus_rc_stepmodel(OpusRangeCoder *rc, int k0)
  295. {
  296. /* Use a probability of 3 up to itheta=8192 and then use 1 after */
  297. unsigned int k, scale, symbol, total = (k0+1)*3 + k0;
  298. scale = rc->range / total;
  299. symbol = rc->value / scale + 1;
  300. symbol = total - FFMIN(symbol, total);
  301. k = (symbol < (k0+1)*3) ? symbol/3 : symbol - (k0+1)*2;
  302. opus_rc_update(rc, scale, (k <= k0) ? 3*(k+0) : (k-1-k0) + 3*(k0+1),
  303. (k <= k0) ? 3*(k+1) : (k-0-k0) + 3*(k0+1), total);
  304. return k;
  305. }
  306. static av_always_inline unsigned int opus_rc_trimodel(OpusRangeCoder *rc, int qn)
  307. {
  308. unsigned int k, scale, symbol, total, low, center;
  309. total = ((qn>>1) + 1) * ((qn>>1) + 1);
  310. scale = rc->range / total;
  311. center = rc->value / scale + 1;
  312. center = total - FFMIN(center, total);
  313. if (center < total >> 1) {
  314. k = (ff_sqrt(8 * center + 1) - 1) >> 1;
  315. low = k * (k + 1) >> 1;
  316. symbol = k + 1;
  317. } else {
  318. k = (2*(qn + 1) - ff_sqrt(8*(total - center - 1) + 1)) >> 1;
  319. low = total - ((qn + 1 - k) * (qn + 2 - k) >> 1);
  320. symbol = qn + 1 - k;
  321. }
  322. opus_rc_update(rc, scale, low, low + symbol, total);
  323. return k;
  324. }
  325. int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
  326. int self_delimited);
  327. int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s);
  328. int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels);
  329. void ff_silk_free(SilkContext **ps);
  330. void ff_silk_flush(SilkContext *s);
  331. /**
  332. * Decode the LP layer of one Opus frame (which may correspond to several SILK
  333. * frames).
  334. */
  335. int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc,
  336. float *output[2],
  337. enum OpusBandwidth bandwidth, int coded_channels,
  338. int duration_ms);
  339. int ff_celt_init(AVCodecContext *avctx, CeltContext **s, int output_channels);
  340. void ff_celt_free(CeltContext **s);
  341. void ff_celt_flush(CeltContext *s);
  342. int ff_celt_decode_frame(CeltContext *s, OpusRangeCoder *rc,
  343. float **output, int coded_channels, int frame_size,
  344. int startband, int endband);
  345. extern const float ff_celt_window2[120];
  346. #endif /* AVCODEC_OPUS_H */