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.

201 lines
6.1KB

  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 SILK_HISTORY 322
  40. #define SILK_MAX_LPC 16
  41. #define ROUND_MULL(a,b,s) (((MUL64(a, b) >> ((s) - 1)) + 1) >> 1)
  42. #define ROUND_MUL16(a,b) ((MUL16(a, b) + 16384) >> 15)
  43. #define OPUS_TS_HEADER 0x7FE0 // 0x3ff (11 bits)
  44. #define OPUS_TS_MASK 0xFFE0 // top 11 bits
  45. static const uint8_t opus_default_extradata[30] = {
  46. 'O', 'p', 'u', 's', 'H', 'e', 'a', 'd',
  47. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  49. };
  50. enum OpusMode {
  51. OPUS_MODE_SILK,
  52. OPUS_MODE_HYBRID,
  53. OPUS_MODE_CELT,
  54. OPUS_MODE_NB
  55. };
  56. enum OpusBandwidth {
  57. OPUS_BANDWIDTH_NARROWBAND,
  58. OPUS_BANDWIDTH_MEDIUMBAND,
  59. OPUS_BANDWIDTH_WIDEBAND,
  60. OPUS_BANDWIDTH_SUPERWIDEBAND,
  61. OPUS_BANDWIDTH_FULLBAND,
  62. OPUS_BANDWITH_NB
  63. };
  64. typedef struct SilkContext SilkContext;
  65. typedef struct CeltFrame CeltFrame;
  66. typedef struct OpusPacket {
  67. int packet_size; /**< packet size */
  68. int data_size; /**< size of the useful data -- packet size - padding */
  69. int code; /**< packet code: specifies the frame layout */
  70. int stereo; /**< whether this packet is mono or stereo */
  71. int vbr; /**< vbr flag */
  72. int config; /**< configuration: tells the audio mode,
  73. ** bandwidth, and frame duration */
  74. int frame_count; /**< frame count */
  75. int frame_offset[MAX_FRAMES]; /**< frame offsets */
  76. int frame_size[MAX_FRAMES]; /**< frame sizes */
  77. int frame_duration; /**< frame duration, in samples @ 48kHz */
  78. enum OpusMode mode; /**< mode */
  79. enum OpusBandwidth bandwidth; /**< bandwidth */
  80. } OpusPacket;
  81. typedef struct OpusStreamContext {
  82. AVCodecContext *avctx;
  83. int output_channels;
  84. /* number of decoded samples for this stream */
  85. int decoded_samples;
  86. /* current output buffers for this stream */
  87. float *out[2];
  88. int out_size;
  89. /* Buffer with samples from this stream for synchronizing
  90. * the streams when they have different resampling delays */
  91. AVAudioFifo *sync_buffer;
  92. OpusRangeCoder rc;
  93. OpusRangeCoder redundancy_rc;
  94. SilkContext *silk;
  95. CeltFrame *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. DECLARE_ALIGNED(32, float, redundancy_buf)[2][960];
  102. float *redundancy_output[2];
  103. /* buffers for the next samples to be decoded */
  104. float *cur_out[2];
  105. int remaining_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. AVClass *av_class;
  131. OpusStreamContext *streams;
  132. int apply_phase_inv;
  133. int nb_streams;
  134. int nb_stereo_streams;
  135. AVFloatDSPContext *fdsp;
  136. int16_t gain_i;
  137. float gain;
  138. ChannelMap *channel_maps;
  139. } OpusContext;
  140. int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
  141. int self_delimited);
  142. int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s);
  143. int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels);
  144. void ff_silk_free(SilkContext **ps);
  145. void ff_silk_flush(SilkContext *s);
  146. /**
  147. * Decode the LP layer of one Opus frame (which may correspond to several SILK
  148. * frames).
  149. */
  150. int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc,
  151. float *output[2],
  152. enum OpusBandwidth bandwidth, int coded_channels,
  153. int duration_ms);
  154. /* Encode or decode CELT bands */
  155. void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc);
  156. /* Encode or decode CELT bitallocation */
  157. void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode);
  158. #endif /* AVCODEC_OPUS_H */