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.

209 lines
6.5KB

  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 "opus_rc.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_TS_HEADER 0x7FE0 // 0x3ff (11 bits)
  54. #define OPUS_TS_MASK 0xFFE0 // top 11 bits
  55. static const uint8_t opus_default_extradata[30] = {
  56. 'O', 'p', 'u', 's', 'H', 'e', 'a', 'd',
  57. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  59. };
  60. enum OpusMode {
  61. OPUS_MODE_SILK,
  62. OPUS_MODE_HYBRID,
  63. OPUS_MODE_CELT
  64. };
  65. enum OpusBandwidth {
  66. OPUS_BANDWIDTH_NARROWBAND,
  67. OPUS_BANDWIDTH_MEDIUMBAND,
  68. OPUS_BANDWIDTH_WIDEBAND,
  69. OPUS_BANDWIDTH_SUPERWIDEBAND,
  70. OPUS_BANDWIDTH_FULLBAND
  71. };
  72. typedef struct SilkContext SilkContext;
  73. typedef struct CeltContext CeltContext;
  74. typedef struct OpusPacket {
  75. int packet_size; /**< packet size */
  76. int data_size; /**< size of the useful data -- packet size - padding */
  77. int code; /**< packet code: specifies the frame layout */
  78. int stereo; /**< whether this packet is mono or stereo */
  79. int vbr; /**< vbr flag */
  80. int config; /**< configuration: tells the audio mode,
  81. ** bandwidth, and frame duration */
  82. int frame_count; /**< frame count */
  83. int frame_offset[MAX_FRAMES]; /**< frame offsets */
  84. int frame_size[MAX_FRAMES]; /**< frame sizes */
  85. int frame_duration; /**< frame duration, in samples @ 48kHz */
  86. enum OpusMode mode; /**< mode */
  87. enum OpusBandwidth bandwidth; /**< bandwidth */
  88. } OpusPacket;
  89. typedef struct OpusStreamContext {
  90. AVCodecContext *avctx;
  91. int output_channels;
  92. OpusRangeCoder rc;
  93. OpusRangeCoder redundancy_rc;
  94. SilkContext *silk;
  95. CeltContext *celt;
  96. AVFloatDSPContext *fdsp;
  97. float silk_buf[2][960];
  98. float *silk_output[2];
  99. DECLARE_ALIGNED(32, float, celt_buf)[2][960];
  100. float *celt_output[2];
  101. float redundancy_buf[2][960];
  102. float *redundancy_output[2];
  103. /* data buffers for the final output data */
  104. float *out[2];
  105. int out_size;
  106. float *out_dummy;
  107. int out_dummy_allocated_size;
  108. SwrContext *swr;
  109. AVAudioFifo *celt_delay;
  110. int silk_samplerate;
  111. /* number of samples we still want to get from the resampler */
  112. int delayed_samples;
  113. OpusPacket packet;
  114. int redundancy_idx;
  115. } OpusStreamContext;
  116. // a mapping between an opus stream and an output channel
  117. typedef struct ChannelMap {
  118. int stream_idx;
  119. int channel_idx;
  120. // when a single decoded channel is mapped to multiple output channels, we
  121. // write to the first output directly and copy from it to the others
  122. // this field is set to 1 for those copied output channels
  123. int copy;
  124. // this is the index of the output channel to copy from
  125. int copy_idx;
  126. // this channel is silent
  127. int silence;
  128. } ChannelMap;
  129. typedef struct OpusContext {
  130. OpusStreamContext *streams;
  131. /* current output buffers for each streams */
  132. float **out;
  133. int *out_size;
  134. /* Buffers for synchronizing the streams when they have different
  135. * resampling delays */
  136. AVAudioFifo **sync_buffers;
  137. /* number of decoded samples for each stream */
  138. int *decoded_samples;
  139. int nb_streams;
  140. int nb_stereo_streams;
  141. AVFloatDSPContext *fdsp;
  142. int16_t gain_i;
  143. float gain;
  144. ChannelMap *channel_maps;
  145. } OpusContext;
  146. int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
  147. int self_delimited);
  148. int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s);
  149. int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels);
  150. void ff_silk_free(SilkContext **ps);
  151. void ff_silk_flush(SilkContext *s);
  152. /**
  153. * Decode the LP layer of one Opus frame (which may correspond to several SILK
  154. * frames).
  155. */
  156. int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc,
  157. float *output[2],
  158. enum OpusBandwidth bandwidth, int coded_channels,
  159. int duration_ms);
  160. int ff_celt_init(AVCodecContext *avctx, CeltContext **s, int output_channels);
  161. void ff_celt_free(CeltContext **s);
  162. void ff_celt_flush(CeltContext *s);
  163. int ff_celt_decode_frame(CeltContext *s, OpusRangeCoder *rc,
  164. float **output, int coded_channels, int frame_size,
  165. int startband, int endband);
  166. #endif /* AVCODEC_OPUS_H */