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.

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