opus.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. OpusRangeCoder rc;
  85. OpusRangeCoder redundancy_rc;
  86. SilkContext *silk;
  87. CeltFrame *celt;
  88. AVFloatDSPContext *fdsp;
  89. float silk_buf[2][960];
  90. float *silk_output[2];
  91. DECLARE_ALIGNED(32, float, celt_buf)[2][960];
  92. float *celt_output[2];
  93. DECLARE_ALIGNED(32, float, redundancy_buf)[2][960];
  94. float *redundancy_output[2];
  95. /* data buffers for the final output data */
  96. float *out[2];
  97. int out_size;
  98. float *out_dummy;
  99. int out_dummy_allocated_size;
  100. SwrContext *swr;
  101. AVAudioFifo *celt_delay;
  102. int silk_samplerate;
  103. /* number of samples we still want to get from the resampler */
  104. int delayed_samples;
  105. OpusPacket packet;
  106. int redundancy_idx;
  107. } OpusStreamContext;
  108. // a mapping between an opus stream and an output channel
  109. typedef struct ChannelMap {
  110. int stream_idx;
  111. int channel_idx;
  112. // when a single decoded channel is mapped to multiple output channels, we
  113. // write to the first output directly and copy from it to the others
  114. // this field is set to 1 for those copied output channels
  115. int copy;
  116. // this is the index of the output channel to copy from
  117. int copy_idx;
  118. // this channel is silent
  119. int silence;
  120. } ChannelMap;
  121. typedef struct OpusContext {
  122. AVClass *av_class;
  123. OpusStreamContext *streams;
  124. int apply_phase_inv;
  125. /* current output buffers for each streams */
  126. float **out;
  127. int *out_size;
  128. /* Buffers for synchronizing the streams when they have different
  129. * resampling delays */
  130. AVAudioFifo **sync_buffers;
  131. /* number of decoded samples for each stream */
  132. int *decoded_samples;
  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 */