Jean-Marc Valin | 6696a14 | 2011-08-22 10:40:38 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited |
Jean-Marc Valin | fbaabc1 | 2010-07-05 15:47:04 -0400 | [diff] [blame] | 2 | Written by Jean-Marc Valin and Koen Vos */ |
Jean-Marc Valin | 3e66e91 | 2010-06-30 14:16:14 -0400 | [diff] [blame] | 3 | /* |
| 4 | Redistribution and use in source and binary forms, with or without |
| 5 | modification, are permitted provided that the following conditions |
| 6 | are met: |
| 7 | |
| 8 | - Redistributions of source code must retain the above copyright |
| 9 | notice, this list of conditions and the following disclaimer. |
| 10 | |
| 11 | - Redistributions in binary form must reproduce the above copyright |
| 12 | notice, this list of conditions and the following disclaimer in the |
| 13 | documentation and/or other materials provided with the distribution. |
| 14 | |
Jean-Marc Valin | 3e66e91 | 2010-06-30 14:16:14 -0400 | [diff] [blame] | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 18 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 19 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 24 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | #include "config.h" |
| 30 | #endif |
| 31 | |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 32 | #include <stdarg.h> |
Jean-Marc Valin | 8ea6704 | 2011-03-11 17:49:10 -0500 | [diff] [blame] | 33 | #include "celt.h" |
Jean-Marc Valin | a0cbeca | 2010-07-08 11:27:20 -0400 | [diff] [blame] | 34 | #include "entenc.h" |
| 35 | #include "modes.h" |
Jean-Marc Valin | 1c2f563 | 2011-09-16 01:16:53 -0700 | [diff] [blame] | 36 | #include "API.h" |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 37 | #include "stack_alloc.h" |
| 38 | #include "float_cast.h" |
Jean-Marc Valin | d9920f3 | 2011-08-19 13:00:49 -0400 | [diff] [blame] | 39 | #include "opus.h" |
| 40 | #include "arch.h" |
Jean-Marc Valin | 6696a14 | 2011-08-22 10:40:38 -0400 | [diff] [blame] | 41 | #include "opus_private.h" |
Jean-Marc Valin | 07f8840 | 2011-08-29 15:08:51 -0400 | [diff] [blame] | 42 | #include "os_support.h" |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 43 | |
Jean-Marc Valin | 1c2f563 | 2011-09-16 01:16:53 -0700 | [diff] [blame] | 44 | #include "tuning_parameters.h" |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 45 | #ifdef FIXED_POINT |
Jean-Marc Valin | 1c2f563 | 2011-09-16 01:16:53 -0700 | [diff] [blame] | 46 | #include "fixed/structs_FIX.h" |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 47 | #else |
Jean-Marc Valin | 1c2f563 | 2011-09-16 01:16:53 -0700 | [diff] [blame] | 48 | #include "float/structs_FLP.h" |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 49 | #endif |
| 50 | |
Jean-Marc Valin | d9920f3 | 2011-08-19 13:00:49 -0400 | [diff] [blame] | 51 | #define MAX_ENCODER_BUFFER 480 |
| 52 | |
| 53 | struct OpusEncoder { |
| 54 | int celt_enc_offset; |
| 55 | int silk_enc_offset; |
| 56 | silk_EncControlStruct silk_mode; |
Jean-Marc Valin | d9920f3 | 2011-08-19 13:00:49 -0400 | [diff] [blame] | 57 | int application; |
Jean-Marc Valin | 927488b | 2011-08-30 20:09:22 -0400 | [diff] [blame] | 58 | int channels; |
| 59 | int delay_compensation; |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 60 | int force_channels; |
Jean-Marc Valin | d9920f3 | 2011-08-19 13:00:49 -0400 | [diff] [blame] | 61 | int signal_type; |
Jean-Marc Valin | d9920f3 | 2011-08-19 13:00:49 -0400 | [diff] [blame] | 62 | int user_bandwidth; |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 63 | int user_forced_mode; |
Jean-Marc Valin | d9920f3 | 2011-08-19 13:00:49 -0400 | [diff] [blame] | 64 | int voice_ratio; |
Gregory Maxwell | 64a3541 | 2011-09-02 10:31:17 -0400 | [diff] [blame] | 65 | opus_int32 Fs; |
Jean-Marc Valin | d9920f3 | 2011-08-19 13:00:49 -0400 | [diff] [blame] | 66 | int use_vbr; |
| 67 | int vbr_constraint; |
Jean-Marc Valin | 8fdf2af | 2011-09-10 21:56:34 -0400 | [diff] [blame] | 68 | opus_int32 bitrate_bps; |
| 69 | opus_int32 user_bitrate_bps; |
Jean-Marc Valin | d9920f3 | 2011-08-19 13:00:49 -0400 | [diff] [blame] | 70 | int encoder_buffer; |
Jean-Marc Valin | 927488b | 2011-08-30 20:09:22 -0400 | [diff] [blame] | 71 | |
| 72 | #define OPUS_ENCODER_RESET_START stream_channels |
| 73 | int stream_channels; |
Jean-Marc Valin | 8fdf2af | 2011-09-10 21:56:34 -0400 | [diff] [blame] | 74 | opus_int16 hybrid_stereo_width_Q14; |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 75 | opus_int32 variable_HP_smth2_Q15; |
| 76 | opus_val32 hp_mem[4]; |
Jean-Marc Valin | 927488b | 2011-08-30 20:09:22 -0400 | [diff] [blame] | 77 | int mode; |
| 78 | int prev_mode; |
Jean-Marc Valin | de3e16c | 2011-10-03 00:39:29 -0400 | [diff] [blame] | 79 | int prev_channels; |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 80 | int prev_framesize; |
Jean-Marc Valin | 927488b | 2011-08-30 20:09:22 -0400 | [diff] [blame] | 81 | int bandwidth; |
| 82 | /* Sampling rate (at the API level) */ |
Jean-Marc Valin | d9920f3 | 2011-08-19 13:00:49 -0400 | [diff] [blame] | 83 | int first; |
| 84 | opus_val16 delay_buffer[MAX_ENCODER_BUFFER*2]; |
| 85 | |
Gregory Maxwell | 64a3541 | 2011-09-02 10:31:17 -0400 | [diff] [blame] | 86 | opus_uint32 rangeFinal; |
Jean-Marc Valin | d9920f3 | 2011-08-19 13:00:49 -0400 | [diff] [blame] | 87 | }; |
| 88 | |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 89 | /* Transition tables for the voice and music. First column is the |
Jean-Marc Valin | 9edde42 | 2011-05-05 15:30:44 -0400 | [diff] [blame] | 90 | middle (memoriless) threshold. The second column is the hysteresis |
| 91 | (difference with the middle) */ |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 92 | static const opus_int32 mono_voice_bandwidth_thresholds[8] = { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 93 | 11000, 1000, /* NB<->MB */ |
| 94 | 14000, 1000, /* MB<->WB */ |
| 95 | 21000, 2000, /* WB<->SWB */ |
| 96 | 29000, 2000, /* SWB<->FB */ |
Jean-Marc Valin | 09c6766 | 2011-05-04 22:34:53 -0400 | [diff] [blame] | 97 | }; |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 98 | static const opus_int32 mono_music_bandwidth_thresholds[8] = { |
| 99 | 14000, 1000, /* MB not allowed */ |
| 100 | 18000, 2000, /* MB<->WB */ |
| 101 | 24000, 2000, /* WB<->SWB */ |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 102 | 33000, 2000, /* SWB<->FB */ |
Jean-Marc Valin | 09c6766 | 2011-05-04 22:34:53 -0400 | [diff] [blame] | 103 | }; |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 104 | static const opus_int32 stereo_voice_bandwidth_thresholds[8] = { |
| 105 | 11000, 1000, /* NB<->MB */ |
| 106 | 14000, 1000, /* MB<->WB */ |
| 107 | 21000, 2000, /* WB<->SWB */ |
| 108 | 32000, 2000, /* SWB<->FB */ |
| 109 | }; |
| 110 | static const opus_int32 stereo_music_bandwidth_thresholds[8] = { |
| 111 | 14000, 1000, /* MB not allowed */ |
| 112 | 18000, 2000, /* MB<->WB */ |
| 113 | 24000, 2000, /* WB<->SWB */ |
| 114 | 48000, 2000, /* SWB<->FB */ |
| 115 | }; |
| 116 | /* Threshold bit-rates for switching between mono and stereo */ |
| 117 | static const opus_int32 stereo_voice_threshold = 26000; |
| 118 | static const opus_int32 stereo_music_threshold = 36000; |
Jean-Marc Valin | 09c6766 | 2011-05-04 22:34:53 -0400 | [diff] [blame] | 119 | |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 120 | /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */ |
| 121 | static const opus_int32 mode_thresholds[2][2] = { |
| 122 | /* voice */ /* music */ |
| 123 | { 48000, 24000}, /* mono */ |
| 124 | { 48000, 24000}, /* stereo */ |
| 125 | }; |
Jean-Marc Valin | 280c060 | 2011-05-05 20:47:42 -0400 | [diff] [blame] | 126 | int opus_encoder_get_size(int channels) |
| 127 | { |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 128 | int silkEncSizeBytes, celtEncSizeBytes; |
| 129 | int ret; |
Gregory Maxwell | f451b33 | 2011-09-04 10:47:15 -0400 | [diff] [blame] | 130 | if (channels<1 || channels > 2) |
| 131 | return 0; |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 132 | ret = silk_Get_Encoder_Size( &silkEncSizeBytes ); |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 133 | if (ret) |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 134 | return 0; |
| 135 | silkEncSizeBytes = align(silkEncSizeBytes); |
Jean-Marc Valin | 280c060 | 2011-05-05 20:47:42 -0400 | [diff] [blame] | 136 | celtEncSizeBytes = celt_encoder_get_size(channels); |
| 137 | return align(sizeof(OpusEncoder))+silkEncSizeBytes+celtEncSizeBytes; |
Jean-Marc Valin | 280c060 | 2011-05-05 20:47:42 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Gregory Maxwell | 64a3541 | 2011-09-02 10:31:17 -0400 | [diff] [blame] | 140 | int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int application) |
Jean-Marc Valin | 3e66e91 | 2010-06-30 14:16:14 -0400 | [diff] [blame] | 141 | { |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 142 | void *silk_enc; |
| 143 | CELTEncoder *celt_enc; |
Jean-Marc Valin | f9bc460 | 2011-03-08 14:57:46 -0500 | [diff] [blame] | 144 | int err; |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 145 | int ret, silkEncSizeBytes; |
Jean-Marc Valin | 3e66e91 | 2010-06-30 14:16:14 -0400 | [diff] [blame] | 146 | |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 147 | if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)|| |
| 148 | (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO |
| 149 | && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)) |
Jean-Marc Valin | 9d8dc3a | 2011-08-29 09:40:57 -0400 | [diff] [blame] | 150 | return OPUS_BAD_ARG; |
Jean-Marc Valin | d3358b1 | 2011-06-14 14:48:53 -0400 | [diff] [blame] | 151 | |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 152 | OPUS_CLEAR((char*)st, opus_encoder_get_size(channels)); |
Jean-Marc Valin | d3358b1 | 2011-06-14 14:48:53 -0400 | [diff] [blame] | 153 | /* Create SILK encoder */ |
| 154 | ret = silk_Get_Encoder_Size( &silkEncSizeBytes ); |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 155 | if (ret) |
Jean-Marc Valin | 9d8dc3a | 2011-08-29 09:40:57 -0400 | [diff] [blame] | 156 | return OPUS_BAD_ARG; |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 157 | silkEncSizeBytes = align(silkEncSizeBytes); |
Jean-Marc Valin | 280c060 | 2011-05-05 20:47:42 -0400 | [diff] [blame] | 158 | st->silk_enc_offset = align(sizeof(OpusEncoder)); |
| 159 | st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes; |
Jean-Marc Valin | 5095c47 | 2011-05-05 19:47:48 -0400 | [diff] [blame] | 160 | silk_enc = (char*)st+st->silk_enc_offset; |
| 161 | celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); |
| 162 | |
Jean-Marc Valin | b386458 | 2011-01-31 12:41:49 -0500 | [diff] [blame] | 163 | st->stream_channels = st->channels = channels; |
Jean-Marc Valin | bcdfe63 | 2010-08-31 11:22:43 -0400 | [diff] [blame] | 164 | |
| 165 | st->Fs = Fs; |
Jean-Marc Valin | 3e66e91 | 2010-06-30 14:16:14 -0400 | [diff] [blame] | 166 | |
Jean-Marc Valin | 103302b | 2011-05-19 17:17:44 -0400 | [diff] [blame] | 167 | ret = silk_InitEncoder( silk_enc, &st->silk_mode ); |
Gregory Maxwell | e03af44 | 2011-09-04 04:43:11 -0400 | [diff] [blame] | 168 | if(ret)return OPUS_INTERNAL_ERROR; |
Jean-Marc Valin | ae18090 | 2010-07-19 11:44:36 -0400 | [diff] [blame] | 169 | |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 170 | /* default SILK parameters */ |
Jean-Marc Valin | 103302b | 2011-05-19 17:17:44 -0400 | [diff] [blame] | 171 | st->silk_mode.nChannelsAPI = channels; |
| 172 | st->silk_mode.nChannelsInternal = channels; |
| 173 | st->silk_mode.API_sampleRate = st->Fs; |
| 174 | st->silk_mode.maxInternalSampleRate = 16000; |
| 175 | st->silk_mode.minInternalSampleRate = 8000; |
| 176 | st->silk_mode.desiredInternalSampleRate = 16000; |
| 177 | st->silk_mode.payloadSize_ms = 20; |
| 178 | st->silk_mode.bitRate = 25000; |
| 179 | st->silk_mode.packetLossPercentage = 0; |
| 180 | st->silk_mode.complexity = 10; |
| 181 | st->silk_mode.useInBandFEC = 0; |
| 182 | st->silk_mode.useDTX = 0; |
| 183 | st->silk_mode.useCBR = 0; |
| 184 | |
Jean-Marc Valin | c5ea074 | 2010-07-05 11:11:54 -0400 | [diff] [blame] | 185 | /* Create CELT encoder */ |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 186 | /* Initialize CELT encoder */ |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 187 | err = celt_encoder_init(celt_enc, Fs, channels); |
Gregory Maxwell | e03af44 | 2011-09-04 04:43:11 -0400 | [diff] [blame] | 188 | if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR; |
| 189 | |
Jean-Marc Valin | 5095c47 | 2011-05-05 19:47:48 -0400 | [diff] [blame] | 190 | celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0)); |
Jean-Marc Valin | cdfb8f3 | 2011-09-09 14:50:15 -0400 | [diff] [blame] | 191 | celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(10)); |
Jean-Marc Valin | 3e66e91 | 2010-06-30 14:16:14 -0400 | [diff] [blame] | 192 | |
Jean-Marc Valin | 2145288 | 2011-09-09 14:57:43 -0400 | [diff] [blame] | 193 | st->use_vbr = 1; |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 194 | st->user_bitrate_bps = OPUS_AUTO; |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 195 | st->bitrate_bps = 3000+Fs*channels; |
Jean-Marc Valin | 69549ac | 2011-08-18 17:28:28 -0400 | [diff] [blame] | 196 | st->application = application; |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 197 | st->signal_type = OPUS_AUTO; |
| 198 | st->user_bandwidth = OPUS_AUTO; |
| 199 | st->force_channels = OPUS_AUTO; |
| 200 | st->user_forced_mode = OPUS_AUTO; |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 201 | st->voice_ratio = -1; |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 202 | st->encoder_buffer = st->Fs/100; |
Jean-Marc Valin | 927488b | 2011-08-30 20:09:22 -0400 | [diff] [blame] | 203 | |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 204 | st->delay_compensation = st->Fs/400; |
Jean-Marc Valin | 073c4e1 | 2011-08-26 13:01:42 -0400 | [diff] [blame] | 205 | /* This part is meant to compensate for the resampler delay as a function |
| 206 | of the API sampling rate */ |
| 207 | if (st->Fs == 48000) |
| 208 | st->delay_compensation += 23; |
| 209 | else if (st->Fs == 24000) |
| 210 | st->delay_compensation += 15; |
| 211 | else |
| 212 | st->delay_compensation += 2; |
| 213 | |
Jean-Marc Valin | 927488b | 2011-08-30 20:09:22 -0400 | [diff] [blame] | 214 | st->hybrid_stereo_width_Q14 = 1 << 14; |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 215 | st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); |
Jean-Marc Valin | 927488b | 2011-08-30 20:09:22 -0400 | [diff] [blame] | 216 | st->first = 1; |
| 217 | st->mode = MODE_HYBRID; |
| 218 | st->bandwidth = OPUS_BANDWIDTH_FULLBAND; |
| 219 | |
Jean-Marc Valin | 9d8dc3a | 2011-08-29 09:40:57 -0400 | [diff] [blame] | 220 | return OPUS_OK; |
Jean-Marc Valin | 3e66e91 | 2010-06-30 14:16:14 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Jean-Marc Valin | 69bfc67 | 2011-08-29 21:46:17 -0400 | [diff] [blame] | 223 | static unsigned char gen_toc(int mode, int framerate, int bandwidth, int silk_bandwidth, int channels) |
Jean-Marc Valin | 421a628 | 2011-08-18 14:32:16 -0400 | [diff] [blame] | 224 | { |
| 225 | int period; |
| 226 | unsigned char toc; |
| 227 | period = 0; |
| 228 | while (framerate < 400) |
| 229 | { |
| 230 | framerate <<= 1; |
| 231 | period++; |
| 232 | } |
| 233 | if (mode == MODE_SILK_ONLY) |
| 234 | { |
Jean-Marc Valin | 69bfc67 | 2011-08-29 21:46:17 -0400 | [diff] [blame] | 235 | toc = (silk_bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5; |
Jean-Marc Valin | 421a628 | 2011-08-18 14:32:16 -0400 | [diff] [blame] | 236 | toc |= (period-2)<<3; |
| 237 | } else if (mode == MODE_CELT_ONLY) |
| 238 | { |
| 239 | int tmp = bandwidth-OPUS_BANDWIDTH_MEDIUMBAND; |
| 240 | if (tmp < 0) |
| 241 | tmp = 0; |
| 242 | toc = 0x80; |
| 243 | toc |= tmp << 5; |
| 244 | toc |= period<<3; |
| 245 | } else /* Hybrid */ |
| 246 | { |
| 247 | toc = 0x60; |
| 248 | toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4; |
| 249 | toc |= (period-2)<<3; |
| 250 | } |
| 251 | toc |= (channels==2)<<2; |
| 252 | return toc; |
| 253 | } |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 254 | |
| 255 | #ifndef FIXED_POINT |
| 256 | void silk_biquad_float( |
| 257 | const opus_val16 *in, /* I: Input signal */ |
| 258 | const opus_int32 *B_Q28, /* I: MA coefficients [3] */ |
| 259 | const opus_int32 *A_Q28, /* I: AR coefficients [2] */ |
| 260 | opus_val32 *S, /* I/O: State vector [2] */ |
| 261 | opus_val16 *out, /* O: Output signal */ |
| 262 | const opus_int32 len, /* I: Signal length (must be even) */ |
| 263 | int stride |
| 264 | ) |
| 265 | { |
| 266 | /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */ |
| 267 | opus_int k; |
| 268 | opus_val32 vout; |
| 269 | opus_val32 inval; |
| 270 | opus_val32 A[2], B[3]; |
| 271 | |
| 272 | A[0] = A_Q28[0] * (1./((opus_int32)1<<28)); |
| 273 | A[1] = A_Q28[1] * (1./((opus_int32)1<<28)); |
| 274 | B[0] = B_Q28[0] * (1./((opus_int32)1<<28)); |
| 275 | B[1] = B_Q28[1] * (1./((opus_int32)1<<28)); |
| 276 | B[2] = B_Q28[2] * (1./((opus_int32)1<<28)); |
| 277 | |
| 278 | /* Negate A_Q28 values and split in two parts */ |
| 279 | |
| 280 | for( k = 0; k < len; k++ ) { |
| 281 | /* S[ 0 ], S[ 1 ]: Q12 */ |
| 282 | inval = in[ k*stride ]; |
| 283 | vout = S[ 0 ] + B[0]*inval; |
| 284 | |
| 285 | S[ 0 ] = S[1] - vout*A[0] + B[1]*inval; |
| 286 | |
| 287 | S[ 1 ] = - vout*A[1] + B[2]*inval; |
| 288 | |
| 289 | /* Scale back to Q0 and saturate */ |
| 290 | out[ k*stride ] = vout; |
| 291 | } |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | static void hp_cutoff(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs) |
| 296 | { |
| 297 | opus_int32 B_Q28[ 3 ], A_Q28[ 2 ]; |
| 298 | opus_int32 Fc_Q19, r_Q28, r_Q22; |
| 299 | |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 300 | silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) ); |
| 301 | Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 ); |
| 302 | silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 ); |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 303 | |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 304 | r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q19 ); |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 305 | |
| 306 | /* b = r * [ 1; -2; 1 ]; */ |
| 307 | /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */ |
| 308 | B_Q28[ 0 ] = r_Q28; |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 309 | B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 ); |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 310 | B_Q28[ 2 ] = r_Q28; |
| 311 | |
| 312 | /* -r * ( 2 - Fc * Fc ); */ |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 313 | r_Q22 = silk_RSHIFT( r_Q28, 6 ); |
| 314 | A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0, 22 ) ); |
| 315 | A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 ); |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 316 | |
| 317 | #ifdef FIXED_POINT |
| 318 | silk_biquad_alt( in, B_Q28, A_Q28, hp_mem, out, len, channels ); |
| 319 | if( channels == 2 ) { |
| 320 | silk_biquad_alt( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels ); |
| 321 | } |
| 322 | #else |
| 323 | silk_biquad_float( in, B_Q28, A_Q28, hp_mem, out, len, channels ); |
| 324 | if( channels == 2 ) { |
| 325 | silk_biquad_float( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels ); |
| 326 | } |
| 327 | #endif |
| 328 | } |
| 329 | |
Jean-Marc Valin | de3e16c | 2011-10-03 00:39:29 -0400 | [diff] [blame] | 330 | static void stereo_fade(const opus_val16 *in, opus_val16 *out, opus_val16 g1, opus_val16 g2, |
| 331 | int overlap, int frame_size, int channels, const opus_val16 *window, opus_int32 Fs) |
| 332 | { |
| 333 | int i; |
| 334 | int inc = 48000/Fs; |
| 335 | g1 = Q15ONE-g1; |
| 336 | g2 = Q15ONE-g2; |
| 337 | for (i=0;i<overlap;i++) |
| 338 | { |
| 339 | opus_val32 diff; |
| 340 | opus_val16 g, w; |
| 341 | w = MULT16_16_Q15(window[i*inc], window[i*inc]); |
| 342 | g = SHR32(MAC16_16(MULT16_16(w,g2), |
| 343 | Q15ONE-w, g1), 15); |
| 344 | diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1])); |
| 345 | diff = MULT16_16_Q15(g, diff); |
| 346 | out[i*channels] = out[i*channels] - diff; |
| 347 | out[i*channels+1] = out[i*channels+1] + diff; |
| 348 | } |
| 349 | for (;i<frame_size;i++) |
| 350 | { |
| 351 | opus_val32 diff; |
| 352 | diff = EXTRACT16(HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1])); |
| 353 | diff = MULT16_16_Q15(g2, diff); |
| 354 | out[i*channels] = out[i*channels] - diff; |
| 355 | out[i*channels+1] = out[i*channels+1] + diff; |
| 356 | } |
| 357 | } |
| 358 | |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 359 | OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error) |
Jean-Marc Valin | 280c060 | 2011-05-05 20:47:42 -0400 | [diff] [blame] | 360 | { |
Jean-Marc Valin | 9d8dc3a | 2011-08-29 09:40:57 -0400 | [diff] [blame] | 361 | int ret; |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 362 | if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)|| |
| 363 | (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO |
| 364 | && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)) |
| 365 | { |
| 366 | if (error) |
| 367 | *error = OPUS_BAD_ARG; |
| 368 | return NULL; |
| 369 | } |
Jean-Marc Valin | 85b8e62 | 2011-08-29 16:10:08 -0400 | [diff] [blame] | 370 | OpusEncoder *st = (OpusEncoder *)opus_alloc(opus_encoder_get_size(channels)); |
| 371 | if (st == NULL) |
Jean-Marc Valin | 9d8dc3a | 2011-08-29 09:40:57 -0400 | [diff] [blame] | 372 | { |
| 373 | if (error) |
| 374 | *error = OPUS_ALLOC_FAIL; |
| 375 | return NULL; |
| 376 | } |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 377 | ret = opus_encoder_init(st, Fs, channels, application); |
Jean-Marc Valin | 9d8dc3a | 2011-08-29 09:40:57 -0400 | [diff] [blame] | 378 | if (error) |
| 379 | *error = ret; |
| 380 | if (ret != OPUS_OK) |
| 381 | { |
Jean-Marc Valin | 85b8e62 | 2011-08-29 16:10:08 -0400 | [diff] [blame] | 382 | opus_free(st); |
| 383 | st = NULL; |
Jean-Marc Valin | 9d8dc3a | 2011-08-29 09:40:57 -0400 | [diff] [blame] | 384 | } |
Jean-Marc Valin | 85b8e62 | 2011-08-29 16:10:08 -0400 | [diff] [blame] | 385 | return st; |
Jean-Marc Valin | 280c060 | 2011-05-05 20:47:42 -0400 | [diff] [blame] | 386 | } |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 387 | |
| 388 | static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int max_data_bytes) |
| 389 | { |
| 390 | if(!frame_size)frame_size=st->Fs/400; |
| 391 | if (st->user_bitrate_bps==OPUS_AUTO) |
| 392 | return 60*st->Fs/frame_size + st->Fs*st->channels; |
| 393 | else if (st->user_bitrate_bps==OPUS_BITRATE_MAX) |
| 394 | return max_data_bytes*8*st->Fs/frame_size; |
| 395 | else |
| 396 | return st->user_bitrate_bps; |
| 397 | } |
| 398 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 399 | #ifdef FIXED_POINT |
Jean-Marc Valin | e4336a5 | 2011-09-08 18:19:50 -0400 | [diff] [blame] | 400 | #define opus_encode_native opus_encode |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 401 | int opus_encode(OpusEncoder *st, const opus_val16 *pcm, int frame_size, |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 402 | unsigned char *data, int max_data_bytes) |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 403 | #else |
Jean-Marc Valin | e4336a5 | 2011-09-08 18:19:50 -0400 | [diff] [blame] | 404 | #define opus_encode_native opus_encode_float |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 405 | int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size, |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 406 | unsigned char *data, int max_data_bytes) |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 407 | #endif |
Jean-Marc Valin | 3e66e91 | 2010-06-30 14:16:14 -0400 | [diff] [blame] | 408 | { |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 409 | void *silk_enc; |
| 410 | CELTEncoder *celt_enc; |
Jean-Marc Valin | e2d1ef1 | 2010-07-06 15:39:19 -0400 | [diff] [blame] | 411 | int i; |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 412 | int ret=0; |
| 413 | int nBytes; |
| 414 | ec_enc enc; |
Jean-Marc Valin | 69bfc67 | 2011-08-29 21:46:17 -0400 | [diff] [blame] | 415 | int silk_internal_bandwidth=-1; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 416 | int bytes_target; |
Jean-Marc Valin | e3de505 | 2011-02-20 12:23:48 -0500 | [diff] [blame] | 417 | int prefill=0; |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 418 | int start_band = 0; |
Jean-Marc Valin | e2a09db | 2011-03-02 17:54:43 -0500 | [diff] [blame] | 419 | int redundancy = 0; |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 420 | int redundancy_bytes = 0; |
| 421 | int celt_to_silk = 0; |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 422 | VARDECL(opus_val16, pcm_buf); |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 423 | int nb_compr_bytes; |
| 424 | int to_celt = 0; |
Jean-Marc Valin | 06cee2b | 2011-08-19 16:11:41 -0400 | [diff] [blame] | 425 | opus_uint32 redundant_rng = 0; |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 426 | int cutoff_Hz, hp_freq_smth1; |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 427 | int voice_est; |
| 428 | opus_int32 equiv_rate; |
Jean-Marc Valin | d186c91 | 2011-09-08 15:13:46 -0400 | [diff] [blame] | 429 | int delay_compensation; |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 430 | ALLOC_STACK; |
Jean-Marc Valin | f9bc460 | 2011-03-08 14:57:46 -0500 | [diff] [blame] | 431 | |
Jean-Marc Valin | 904e7a8 | 2011-09-11 14:29:46 -0400 | [diff] [blame] | 432 | max_data_bytes = IMIN(1276, max_data_bytes); |
| 433 | |
Jean-Marc Valin | 2b98bdf | 2011-08-20 00:21:46 -0400 | [diff] [blame] | 434 | st->rangeFinal = 0; |
Jean-Marc Valin | 23a900c | 2011-06-14 14:34:02 -0400 | [diff] [blame] | 435 | if (400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size != st->Fs && |
| 436 | 50*frame_size != st->Fs && 25*frame_size != st->Fs && 50*frame_size != 3*st->Fs) |
Jean-Marc Valin | 44e27dd | 2011-08-29 23:35:17 -0400 | [diff] [blame] | 437 | { |
| 438 | RESTORE_STACK; |
| 439 | return OPUS_BAD_ARG; |
| 440 | } |
Jean-Marc Valin | 5095c47 | 2011-05-05 19:47:48 -0400 | [diff] [blame] | 441 | silk_enc = (char*)st+st->silk_enc_offset; |
| 442 | celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); |
Jean-Marc Valin | ba54746 | 2011-05-10 17:54:41 -0400 | [diff] [blame] | 443 | |
Jean-Marc Valin | 68bc8c0 | 2011-09-09 11:10:48 -0400 | [diff] [blame] | 444 | if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) |
Jean-Marc Valin | d186c91 | 2011-09-08 15:13:46 -0400 | [diff] [blame] | 445 | delay_compensation = 0; |
| 446 | else |
| 447 | delay_compensation = st->delay_compensation; |
| 448 | |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 449 | st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes); |
Jean-Marc Valin | ba54746 | 2011-05-10 17:54:41 -0400 | [diff] [blame] | 450 | |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 451 | /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */ |
| 452 | equiv_rate = st->bitrate_bps - 60*(st->Fs/frame_size - 50); |
| 453 | |
| 454 | if (st->signal_type == OPUS_SIGNAL_VOICE) |
| 455 | voice_est = 127; |
| 456 | else if (st->signal_type == OPUS_SIGNAL_MUSIC) |
| 457 | voice_est = 0; |
| 458 | else if (st->voice_ratio >= 0) |
| 459 | voice_est = st->voice_ratio*327>>8; |
| 460 | else if (st->application == OPUS_APPLICATION_VOIP) |
| 461 | voice_est = 115; |
| 462 | else |
| 463 | voice_est = 64; |
| 464 | |
| 465 | #ifdef FUZZING |
| 466 | /* Random mono/stereo decision */ |
| 467 | if (st->channels == 2 && (rand()&0x1F)==0) |
| 468 | st->stream_channels = 3-st->stream_channels; |
| 469 | #else |
Jean-Marc Valin | 103302b | 2011-05-19 17:17:44 -0400 | [diff] [blame] | 470 | /* Rate-dependent mono-stereo decision */ |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 471 | if (st->force_channels!=OPUS_AUTO && st->channels == 2) |
Koen Vos | 479e18b | 2011-05-25 23:09:52 -0400 | [diff] [blame] | 472 | { |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 473 | st->stream_channels = st->force_channels; |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 474 | } else if (st->channels == 2) |
Jean-Marc Valin | f9bc460 | 2011-03-08 14:57:46 -0500 | [diff] [blame] | 475 | { |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 476 | opus_int32 stereo_threshold; |
| 477 | stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14); |
| 478 | if (st->stream_channels == 2) |
| 479 | stereo_threshold -= 4000; |
| 480 | else |
| 481 | stereo_threshold += 4000; |
| 482 | st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1; |
Jean-Marc Valin | f9bc460 | 2011-03-08 14:57:46 -0500 | [diff] [blame] | 483 | } else { |
Jean-Marc Valin | 103302b | 2011-05-19 17:17:44 -0400 | [diff] [blame] | 484 | st->stream_channels = st->channels; |
Jean-Marc Valin | f9bc460 | 2011-03-08 14:57:46 -0500 | [diff] [blame] | 485 | } |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 486 | #endif |
| 487 | |
Jean-Marc Valin | de3e16c | 2011-10-03 00:39:29 -0400 | [diff] [blame] | 488 | if (st->silk_mode.toMono==1 && st->stream_channels==2) |
| 489 | { |
| 490 | /* In case the encoder changes its mind on stereo->mono transition */ |
| 491 | st->silk_mode.toMono = -1; |
Jean-Marc Valin | 1e0805d | 2011-10-03 13:52:18 -0400 | [diff] [blame] | 492 | } else if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0) |
Jean-Marc Valin | de3e16c | 2011-10-03 00:39:29 -0400 | [diff] [blame] | 493 | { |
Jean-Marc Valin | 1e0805d | 2011-10-03 13:52:18 -0400 | [diff] [blame] | 494 | /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */ |
Jean-Marc Valin | de3e16c | 2011-10-03 00:39:29 -0400 | [diff] [blame] | 495 | st->silk_mode.toMono=1; |
| 496 | st->stream_channels = 2; |
Jean-Marc Valin | 1e0805d | 2011-10-03 13:52:18 -0400 | [diff] [blame] | 497 | } else if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==1) |
| 498 | { |
| 499 | st->silk_mode.toMono=2; |
| 500 | st->stream_channels = 2; |
Jean-Marc Valin | de3e16c | 2011-10-03 00:39:29 -0400 | [diff] [blame] | 501 | } else { |
| 502 | st->silk_mode.toMono=0; |
| 503 | } |
| 504 | |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 505 | #ifdef FUZZING |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 506 | /* Random mode switching */ |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 507 | if ((rand()&0xF)==0) |
| 508 | { |
| 509 | if ((rand()&0x1)==0) |
| 510 | st->mode = MODE_CELT_ONLY; |
| 511 | else |
| 512 | st->mode = MODE_SILK_ONLY; |
| 513 | } else { |
| 514 | if (st->prev_mode==MODE_CELT_ONLY) |
| 515 | st->mode = MODE_CELT_ONLY; |
| 516 | else |
| 517 | st->mode = MODE_SILK_ONLY; |
| 518 | } |
| 519 | #else |
Jean-Marc Valin | 00cb6f7 | 2011-05-26 15:06:30 -0400 | [diff] [blame] | 520 | /* Mode selection depending on application and signal type */ |
Jean-Marc Valin | 68bc8c0 | 2011-09-09 11:10:48 -0400 | [diff] [blame] | 521 | if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) |
Jean-Marc Valin | d186c91 | 2011-09-08 15:13:46 -0400 | [diff] [blame] | 522 | { |
| 523 | st->mode = MODE_CELT_ONLY; |
| 524 | } else if (st->user_forced_mode == OPUS_AUTO) |
Jean-Marc Valin | f9bc460 | 2011-03-08 14:57:46 -0500 | [diff] [blame] | 525 | { |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 526 | int chan; |
| 527 | opus_int32 mode_voice, mode_music; |
| 528 | opus_int32 threshold; |
Jean-Marc Valin | f9bc460 | 2011-03-08 14:57:46 -0500 | [diff] [blame] | 529 | |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 530 | chan = (st->channels==2) && st->force_channels!=1; |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 531 | mode_voice = mode_thresholds[chan][0]; |
| 532 | mode_music = mode_thresholds[chan][1]; |
| 533 | threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14); |
Jean-Marc Valin | 00cb6f7 | 2011-05-26 15:06:30 -0400 | [diff] [blame] | 534 | |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 535 | /* Hysteresis */ |
| 536 | if (st->prev_mode == MODE_CELT_ONLY) |
| 537 | threshold -= 4000; |
| 538 | else if (st->prev_mode>0) |
| 539 | threshold += 4000; |
Jean-Marc Valin | 00cb6f7 | 2011-05-26 15:06:30 -0400 | [diff] [blame] | 540 | |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 541 | st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY; |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 542 | } else { |
| 543 | st->mode = st->user_forced_mode; |
Jean-Marc Valin | 00cb6f7 | 2011-05-26 15:06:30 -0400 | [diff] [blame] | 544 | } |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 545 | #endif |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 546 | |
Jean-Marc Valin | 81936d5 | 2011-08-13 01:44:39 -0400 | [diff] [blame] | 547 | /* Override the chosen mode to make sure we meet the requested frame size */ |
Jean-Marc Valin | 81936d5 | 2011-08-13 01:44:39 -0400 | [diff] [blame] | 548 | if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100) |
| 549 | st->mode = MODE_CELT_ONLY; |
| 550 | |
Jean-Marc Valin | 6854556 | 2011-08-12 00:30:47 -0400 | [diff] [blame] | 551 | if (st->prev_mode > 0 && |
| 552 | ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) || |
| 553 | (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY))) |
| 554 | { |
| 555 | redundancy = 1; |
| 556 | celt_to_silk = (st->mode != MODE_CELT_ONLY); |
| 557 | if (!celt_to_silk) |
| 558 | { |
| 559 | /* Switch to SILK/hybrid if frame size is 10 ms or more*/ |
| 560 | if (frame_size >= st->Fs/100) |
| 561 | { |
| 562 | st->mode = st->prev_mode; |
| 563 | to_celt = 1; |
| 564 | } else { |
| 565 | redundancy=0; |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) |
| 570 | { |
| 571 | silk_EncControlStruct dummy; |
| 572 | silk_InitEncoder( silk_enc, &dummy); |
| 573 | prefill=1; |
| 574 | } |
| 575 | |
Jean-Marc Valin | 243ff0c | 2011-05-03 17:06:54 -0400 | [diff] [blame] | 576 | /* Automatic (rate-dependent) bandwidth selection */ |
Jean-Marc Valin | 09c6766 | 2011-05-04 22:34:53 -0400 | [diff] [blame] | 577 | if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch) |
Jean-Marc Valin | f9bc460 | 2011-03-08 14:57:46 -0500 | [diff] [blame] | 578 | { |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 579 | const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds; |
| 580 | opus_int32 bandwidth_thresholds[8]; |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 581 | int bandwidth = OPUS_BANDWIDTH_FULLBAND; |
Jean-Marc Valin | 09c6766 | 2011-05-04 22:34:53 -0400 | [diff] [blame] | 582 | |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 583 | if (st->channels==2 && st->force_channels!=1) |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 584 | { |
| 585 | voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds; |
| 586 | music_bandwidth_thresholds = stereo_music_bandwidth_thresholds; |
| 587 | } else { |
| 588 | voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds; |
| 589 | music_bandwidth_thresholds = mono_music_bandwidth_thresholds; |
| 590 | } |
| 591 | /* Interpolate bandwidth thresholds depending on voice estimation */ |
| 592 | for (i=0;i<8;i++) |
| 593 | { |
| 594 | bandwidth_thresholds[i] = music_bandwidth_thresholds[i] |
| 595 | + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14); |
| 596 | } |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 597 | do { |
| 598 | int threshold, hysteresis; |
| 599 | threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)]; |
| 600 | hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1]; |
| 601 | if (!st->first) |
| 602 | { |
| 603 | if (st->bandwidth >= bandwidth) |
| 604 | threshold -= hysteresis; |
| 605 | else |
| 606 | threshold += hysteresis; |
| 607 | } |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 608 | if (equiv_rate >= threshold) |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 609 | break; |
| 610 | } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND); |
| 611 | st->bandwidth = bandwidth; |
| 612 | /* Prevents any transition to SWB/FB until the SILK layer has fully |
| 613 | switched to WB mode and turned the variable LP filter off */ |
| 614 | if (st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) |
| 615 | st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
Jean-Marc Valin | f9bc460 | 2011-03-08 14:57:46 -0500 | [diff] [blame] | 616 | } |
Jean-Marc Valin | 955f94c | 2011-03-08 22:12:43 -0500 | [diff] [blame] | 617 | |
Jean-Marc Valin | efe16c5 | 2011-09-29 22:02:37 -0400 | [diff] [blame] | 618 | if (st->user_bandwidth != OPUS_AUTO) |
| 619 | st->bandwidth = st->user_bandwidth; |
| 620 | |
Jean-Marc Valin | 243ff0c | 2011-05-03 17:06:54 -0400 | [diff] [blame] | 621 | /* Prevents Opus from wasting bits on frequencies that are above |
| 622 | the Nyquist rate of the input signal */ |
Jean-Marc Valin | 541df0a | 2011-05-26 00:22:58 -0400 | [diff] [blame] | 623 | if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND) |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 624 | st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; |
Jean-Marc Valin | 541df0a | 2011-05-26 00:22:58 -0400 | [diff] [blame] | 625 | if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 626 | st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
Jean-Marc Valin | 541df0a | 2011-05-26 00:22:58 -0400 | [diff] [blame] | 627 | if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND) |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 628 | st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; |
Jean-Marc Valin | 541df0a | 2011-05-26 00:22:58 -0400 | [diff] [blame] | 629 | if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND) |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 630 | st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
Jean-Marc Valin | 955f94c | 2011-03-08 22:12:43 -0500 | [diff] [blame] | 631 | |
Jean-Marc Valin | 955f94c | 2011-03-08 22:12:43 -0500 | [diff] [blame] | 632 | |
Jean-Marc Valin | 81936d5 | 2011-08-13 01:44:39 -0400 | [diff] [blame] | 633 | /* Can't support higher than wideband for >20 ms frames */ |
Jean-Marc Valin | e4336a5 | 2011-09-08 18:19:50 -0400 | [diff] [blame] | 634 | if (frame_size > st->Fs/50 && (st->mode == MODE_CELT_ONLY || st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)) |
| 635 | { |
Jean-Marc Valin | d970a85 | 2011-09-08 21:41:29 -0400 | [diff] [blame] | 636 | VARDECL(unsigned char, tmp_data); |
| 637 | VARDECL(unsigned char, rp_); |
Jean-Marc Valin | e4336a5 | 2011-09-08 18:19:50 -0400 | [diff] [blame] | 638 | int nb_frames; |
| 639 | int bak_mode, bak_bandwidth, bak_channels; |
Jean-Marc Valin | d970a85 | 2011-09-08 21:41:29 -0400 | [diff] [blame] | 640 | OpusRepacketizer *rp; |
| 641 | int bytes_per_frame; |
| 642 | |
| 643 | nb_frames = frame_size > st->Fs/25 ? 3 : 2; |
| 644 | bytes_per_frame = max_data_bytes/nb_frames-3; |
| 645 | |
| 646 | ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char); |
| 647 | ALLOC(rp_, opus_repacketizer_get_size(), unsigned char); |
| 648 | |
| 649 | rp = opus_repacketizer_init((OpusRepacketizer*)rp_); |
Jean-Marc Valin | d3358b1 | 2011-06-14 14:48:53 -0400 | [diff] [blame] | 650 | |
Jean-Marc Valin | e4336a5 | 2011-09-08 18:19:50 -0400 | [diff] [blame] | 651 | bak_mode = st->user_forced_mode; |
| 652 | bak_bandwidth = st->user_bandwidth; |
| 653 | bak_channels = st->force_channels; |
| 654 | |
| 655 | st->user_forced_mode = st->mode; |
| 656 | st->user_bandwidth = st->bandwidth; |
| 657 | st->force_channels = st->stream_channels; |
| 658 | |
Jean-Marc Valin | e4336a5 | 2011-09-08 18:19:50 -0400 | [diff] [blame] | 659 | for (i=0;i<nb_frames;i++) |
| 660 | { |
| 661 | int tmp_len; |
Jean-Marc Valin | d970a85 | 2011-09-08 21:41:29 -0400 | [diff] [blame] | 662 | tmp_len = opus_encode_native(st, pcm+i*(st->channels*st->Fs/50), st->Fs/50, tmp_data+i*bytes_per_frame, bytes_per_frame); |
| 663 | ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len); |
Jean-Marc Valin | e4336a5 | 2011-09-08 18:19:50 -0400 | [diff] [blame] | 664 | } |
| 665 | ret = opus_repacketizer_out(rp, data, max_data_bytes); |
| 666 | |
Jean-Marc Valin | e4336a5 | 2011-09-08 18:19:50 -0400 | [diff] [blame] | 667 | st->user_forced_mode = bak_mode; |
| 668 | st->user_bandwidth = bak_bandwidth; |
| 669 | st->force_channels = bak_channels; |
| 670 | RESTORE_STACK; |
| 671 | return ret; |
| 672 | } |
Jean-Marc Valin | 81936d5 | 2011-08-13 01:44:39 -0400 | [diff] [blame] | 673 | /* CELT mode doesn't support mediumband, use wideband instead */ |
Jean-Marc Valin | 541df0a | 2011-05-26 00:22:58 -0400 | [diff] [blame] | 674 | if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) |
| 675 | st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
Jean-Marc Valin | 81936d5 | 2011-08-13 01:44:39 -0400 | [diff] [blame] | 676 | |
| 677 | /* Chooses the appropriate mode for speech |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 678 | *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */ |
Jean-Marc Valin | 541df0a | 2011-05-26 00:22:58 -0400 | [diff] [blame] | 679 | if (st->mode == MODE_SILK_ONLY && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) |
Jean-Marc Valin | f9bc460 | 2011-03-08 14:57:46 -0500 | [diff] [blame] | 680 | st->mode = MODE_HYBRID; |
Jean-Marc Valin | 541df0a | 2011-05-26 00:22:58 -0400 | [diff] [blame] | 681 | if (st->mode == MODE_HYBRID && st->bandwidth <= OPUS_BANDWIDTH_WIDEBAND) |
Jean-Marc Valin | f9bc460 | 2011-03-08 14:57:46 -0500 | [diff] [blame] | 682 | st->mode = MODE_SILK_ONLY; |
| 683 | |
Jean-Marc Valin | 904e7a8 | 2011-09-11 14:29:46 -0400 | [diff] [blame] | 684 | /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode */ |
| 685 | if (max_data_bytes < 8000*frame_size / (st->Fs * 8)) |
| 686 | st->mode = MODE_CELT_ONLY; |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 687 | /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, st->bandwidth); */ |
Jean-Marc Valin | 904e7a8 | 2011-09-11 14:29:46 -0400 | [diff] [blame] | 688 | bytes_target = IMIN(max_data_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 689 | |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 690 | data += 1; |
Jean-Marc Valin | 606250a | 2011-02-15 14:31:21 -0500 | [diff] [blame] | 691 | |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 692 | ec_enc_init(&enc, data, max_data_bytes-1); |
Jean-Marc Valin | 24af303 | 2010-06-30 14:29:45 -0400 | [diff] [blame] | 693 | |
Jean-Marc Valin | d186c91 | 2011-09-08 15:13:46 -0400 | [diff] [blame] | 694 | ALLOC(pcm_buf, (delay_compensation+frame_size)*st->channels, opus_val16); |
| 695 | for (i=0;i<delay_compensation*st->channels;i++) |
| 696 | pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-delay_compensation)*st->channels+i]; |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 697 | |
| 698 | if (st->mode == MODE_CELT_ONLY) |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 699 | hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 700 | else |
| 701 | hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15; |
| 702 | |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 703 | st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15, |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 704 | hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) ); |
| 705 | |
| 706 | /* convert from log scale to Hertz */ |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 707 | cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) ); |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 708 | |
| 709 | if (st->application == OPUS_APPLICATION_VOIP) |
| 710 | { |
Jean-Marc Valin | d186c91 | 2011-09-08 15:13:46 -0400 | [diff] [blame] | 711 | hp_cutoff(pcm, cutoff_Hz, &pcm_buf[delay_compensation*st->channels], st->hp_mem, frame_size, st->channels, st->Fs); |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 712 | } else { |
| 713 | for (i=0;i<frame_size*st->channels;i++) |
Jean-Marc Valin | d186c91 | 2011-09-08 15:13:46 -0400 | [diff] [blame] | 714 | pcm_buf[delay_compensation*st->channels + i] = pcm[i]; |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 715 | } |
Jean-Marc Valin | 2f0ca76 | 2011-09-01 16:56:40 -0400 | [diff] [blame] | 716 | |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 717 | /* SILK processing */ |
Jean-Marc Valin | 606250a | 2011-02-15 14:31:21 -0500 | [diff] [blame] | 718 | if (st->mode != MODE_CELT_ONLY) |
| 719 | { |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 720 | #ifdef FIXED_POINT |
| 721 | const opus_int16 *pcm_silk; |
| 722 | #else |
| 723 | VARDECL(opus_int16, pcm_silk); |
| 724 | ALLOC(pcm_silk, st->channels*frame_size, opus_int16); |
| 725 | #endif |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 726 | st->silk_mode.bitRate = st->bitrate_bps - 8*st->Fs/frame_size; |
| 727 | if( st->mode == MODE_HYBRID ) { |
Jean-Marc Valin | 103302b | 2011-05-19 17:17:44 -0400 | [diff] [blame] | 728 | st->silk_mode.bitRate /= st->stream_channels; |
Jean-Marc Valin | 541df0a | 2011-05-26 00:22:58 -0400 | [diff] [blame] | 729 | if( st->bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND ) { |
Karsten Vandborg Sorensen | 7805475 | 2011-02-03 09:36:59 -0500 | [diff] [blame] | 730 | if( st->Fs == 100 * frame_size ) { |
| 731 | /* 24 kHz, 10 ms */ |
Koen Vos | 5ad41a3 | 2011-02-04 00:49:48 -0500 | [diff] [blame] | 732 | st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 2000 + st->use_vbr * 1000 ) * 2 ) / 3; |
Karsten Vandborg Sorensen | 7805475 | 2011-02-03 09:36:59 -0500 | [diff] [blame] | 733 | } else { |
| 734 | /* 24 kHz, 20 ms */ |
Koen Vos | 5ad41a3 | 2011-02-04 00:49:48 -0500 | [diff] [blame] | 735 | st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 1000 + st->use_vbr * 1000 ) * 2 ) / 3; |
Karsten Vandborg Sorensen | 7805475 | 2011-02-03 09:36:59 -0500 | [diff] [blame] | 736 | } |
| 737 | } else { |
| 738 | if( st->Fs == 100 * frame_size ) { |
| 739 | /* 48 kHz, 10 ms */ |
Koen Vos | 5ad41a3 | 2011-02-04 00:49:48 -0500 | [diff] [blame] | 740 | st->silk_mode.bitRate = ( st->silk_mode.bitRate + 8000 + st->use_vbr * 3000 ) / 2; |
Karsten Vandborg Sorensen | 7805475 | 2011-02-03 09:36:59 -0500 | [diff] [blame] | 741 | } else { |
| 742 | /* 48 kHz, 20 ms */ |
Koen Vos | 5ad41a3 | 2011-02-04 00:49:48 -0500 | [diff] [blame] | 743 | st->silk_mode.bitRate = ( st->silk_mode.bitRate + 9000 + st->use_vbr * 1000 ) / 2; |
Karsten Vandborg Sorensen | 7805475 | 2011-02-03 09:36:59 -0500 | [diff] [blame] | 744 | } |
| 745 | } |
Jean-Marc Valin | 103302b | 2011-05-19 17:17:44 -0400 | [diff] [blame] | 746 | st->silk_mode.bitRate *= st->stream_channels; |
Koen Vos | 03c1d66 | 2011-02-17 16:54:44 -0500 | [diff] [blame] | 747 | /* don't let SILK use more than 80% */ |
| 748 | if( st->silk_mode.bitRate > ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5 ) { |
| 749 | st->silk_mode.bitRate = ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5; |
| 750 | } |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 751 | } |
Jean-Marc Valin | 904e7a8 | 2011-09-11 14:29:46 -0400 | [diff] [blame] | 752 | /* SILK is not allow to use more than 50% of max_data_bytes */ |
| 753 | if (max_data_bytes < 2*st->silk_mode.bitRate*frame_size / (st->Fs * 8)) |
| 754 | st->silk_mode.bitRate = max_data_bytes*st->Fs*4/frame_size; |
Jean-Marc Valin | 862e182 | 2010-07-20 12:19:00 -0400 | [diff] [blame] | 755 | |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 756 | st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs; |
Jean-Marc Valin | 103302b | 2011-05-19 17:17:44 -0400 | [diff] [blame] | 757 | st->silk_mode.nChannelsAPI = st->channels; |
| 758 | st->silk_mode.nChannelsInternal = st->stream_channels; |
Jean-Marc Valin | 541df0a | 2011-05-26 00:22:58 -0400 | [diff] [blame] | 759 | if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND) { |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 760 | st->silk_mode.desiredInternalSampleRate = 8000; |
Jean-Marc Valin | 541df0a | 2011-05-26 00:22:58 -0400 | [diff] [blame] | 761 | } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 762 | st->silk_mode.desiredInternalSampleRate = 12000; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 763 | } else { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 764 | silk_assert( st->mode == MODE_HYBRID || st->bandwidth == OPUS_BANDWIDTH_WIDEBAND ); |
Jean-Marc Valin | d8f9097 | 2011-04-27 10:06:53 -0400 | [diff] [blame] | 765 | st->silk_mode.desiredInternalSampleRate = 16000; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 766 | } |
| 767 | if( st->mode == MODE_HYBRID ) { |
| 768 | /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */ |
Koen Vos | 1e1562c | 2011-02-14 15:04:59 -0500 | [diff] [blame] | 769 | st->silk_mode.minInternalSampleRate = 16000; |
| 770 | } else { |
| 771 | st->silk_mode.minInternalSampleRate = 8000; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 772 | } |
Jean-Marc Valin | d8f9097 | 2011-04-27 10:06:53 -0400 | [diff] [blame] | 773 | st->silk_mode.maxInternalSampleRate = 16000; |
Jean-Marc Valin | b5be826 | 2010-11-12 06:47:46 +0800 | [diff] [blame] | 774 | |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 775 | /* Call SILK encoder for the low band */ |
Jean-Marc Valin | 66c612e | 2011-08-15 14:08:57 -0400 | [diff] [blame] | 776 | nBytes = IMIN(1275, max_data_bytes-1); |
Jean-Marc Valin | e3de505 | 2011-02-20 12:23:48 -0500 | [diff] [blame] | 777 | if (prefill) |
Jean-Marc Valin | a93f501 | 2011-03-03 15:50:08 -0500 | [diff] [blame] | 778 | { |
| 779 | int zero=0; |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 780 | #ifdef FIXED_POINT |
| 781 | pcm_silk = st->delay_buffer; |
| 782 | #else |
| 783 | for (i=0;i<st->encoder_buffer*st->channels;i++) |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 784 | pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]); |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 785 | #endif |
| 786 | silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, 1 ); |
Jean-Marc Valin | a93f501 | 2011-03-03 15:50:08 -0500 | [diff] [blame] | 787 | } |
Jean-Marc Valin | e3de505 | 2011-02-20 12:23:48 -0500 | [diff] [blame] | 788 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 789 | #ifdef FIXED_POINT |
Jean-Marc Valin | d186c91 | 2011-09-08 15:13:46 -0400 | [diff] [blame] | 790 | pcm_silk = pcm_buf+delay_compensation*st->channels; |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 791 | #else |
| 792 | for (i=0;i<frame_size*st->channels;i++) |
Jean-Marc Valin | d186c91 | 2011-09-08 15:13:46 -0400 | [diff] [blame] | 793 | pcm_silk[i] = FLOAT2INT16(pcm_buf[delay_compensation*st->channels + i]); |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 794 | #endif |
| 795 | ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 ); |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 796 | if( ret ) { |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 797 | /*fprintf (stderr, "SILK encode error: %d\n", ret);*/ |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 798 | /* Handle error */ |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 799 | return OPUS_INTERNAL_ERROR; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 800 | } |
Jean-Marc Valin | 617fcd2 | 2011-03-16 22:11:53 -0400 | [diff] [blame] | 801 | if (nBytes==0) |
Jean-Marc Valin | 421a628 | 2011-08-18 14:32:16 -0400 | [diff] [blame] | 802 | { |
Jean-Marc Valin | 69bfc67 | 2011-08-29 21:46:17 -0400 | [diff] [blame] | 803 | data[-1] = gen_toc(st->mode, st->Fs/frame_size, st->bandwidth, silk_internal_bandwidth, st->stream_channels); |
Jean-Marc Valin | 44e27dd | 2011-08-29 23:35:17 -0400 | [diff] [blame] | 804 | RESTORE_STACK; |
Jean-Marc Valin | 421a628 | 2011-08-18 14:32:16 -0400 | [diff] [blame] | 805 | return 1; |
| 806 | } |
Jean-Marc Valin | 69bfc67 | 2011-08-29 21:46:17 -0400 | [diff] [blame] | 807 | /* Extract SILK internal bandwidth for signaling in first byte */ |
| 808 | if( st->mode == MODE_SILK_ONLY ) { |
| 809 | if( st->silk_mode.internalSampleRate == 8000 ) { |
| 810 | silk_internal_bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
| 811 | } else if( st->silk_mode.internalSampleRate == 12000 ) { |
| 812 | silk_internal_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; |
| 813 | } else if( st->silk_mode.internalSampleRate == 16000 ) { |
| 814 | silk_internal_bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
| 815 | } |
| 816 | } else { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 817 | silk_assert( st->silk_mode.internalSampleRate == 16000 ); |
Jean-Marc Valin | 69bfc67 | 2011-08-29 21:46:17 -0400 | [diff] [blame] | 818 | } |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 819 | } |
Jean-Marc Valin | 67008d2 | 2010-07-20 11:13:30 -0400 | [diff] [blame] | 820 | |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 821 | /* CELT processing */ |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 822 | { |
| 823 | int endband=21; |
Jean-Marc Valin | e2d1ef1 | 2010-07-06 15:39:19 -0400 | [diff] [blame] | 824 | |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 825 | switch(st->bandwidth) |
| 826 | { |
| 827 | case OPUS_BANDWIDTH_NARROWBAND: |
| 828 | endband = 13; |
| 829 | break; |
| 830 | case OPUS_BANDWIDTH_MEDIUMBAND: |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 831 | case OPUS_BANDWIDTH_WIDEBAND: |
| 832 | endband = 17; |
| 833 | break; |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 834 | case OPUS_BANDWIDTH_SUPERWIDEBAND: |
| 835 | endband = 19; |
| 836 | break; |
| 837 | case OPUS_BANDWIDTH_FULLBAND: |
| 838 | endband = 21; |
| 839 | break; |
| 840 | } |
| 841 | celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband)); |
| 842 | celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels)); |
| 843 | } |
| 844 | if (st->mode != MODE_SILK_ONLY) |
| 845 | { |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 846 | celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); |
Jean-Marc Valin | 663a7fe | 2011-09-06 14:05:19 -0400 | [diff] [blame] | 847 | celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX)); |
Jean-Marc Valin | 606250a | 2011-02-15 14:31:21 -0500 | [diff] [blame] | 848 | if (st->prev_mode == MODE_SILK_ONLY) |
| 849 | { |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 850 | unsigned char dummy[10]; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 851 | celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 852 | celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); |
| 853 | celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)); |
Jean-Marc Valin | 9267506 | 2011-09-11 22:00:46 -0400 | [diff] [blame] | 854 | /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */ |
Jean-Marc Valin | d186c91 | 2011-09-08 15:13:46 -0400 | [diff] [blame] | 855 | celt_encode_with_ec(celt_enc, &st->delay_buffer[(st->encoder_buffer-delay_compensation-st->Fs/400)*st->channels], st->Fs/400, dummy, 10, NULL); |
Jean-Marc Valin | 606250a | 2011-02-15 14:31:21 -0500 | [diff] [blame] | 856 | } else { |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 857 | celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(2)); |
Jean-Marc Valin | 606250a | 2011-02-15 14:31:21 -0500 | [diff] [blame] | 858 | } |
Jean-Marc Valin | e2a09db | 2011-03-02 17:54:43 -0500 | [diff] [blame] | 859 | |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 860 | if (st->mode == MODE_HYBRID) |
| 861 | { |
| 862 | int len; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 863 | |
Timothy B. Terriberry | a1dd0fc | 2011-02-03 14:52:50 -0800 | [diff] [blame] | 864 | len = (ec_tell(&enc)+7)>>3; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 865 | if( st->use_vbr ) { |
Koen Vos | 5ad41a3 | 2011-02-04 00:49:48 -0500 | [diff] [blame] | 866 | nb_compr_bytes = len + bytes_target - (st->silk_mode.bitRate * frame_size) / (8 * st->Fs); |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 867 | } else { |
| 868 | /* check if SILK used up too much */ |
| 869 | nb_compr_bytes = len > bytes_target ? len : bytes_target; |
| 870 | } |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 871 | } else { |
Jean-Marc Valin | d9ad6e1 | 2011-02-01 18:08:41 -0500 | [diff] [blame] | 872 | if (st->use_vbr) |
| 873 | { |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 874 | celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1)); |
| 875 | celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint)); |
| 876 | celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps)); |
Jean-Marc Valin | d9ad6e1 | 2011-02-01 18:08:41 -0500 | [diff] [blame] | 877 | nb_compr_bytes = max_data_bytes-1; |
| 878 | } else { |
| 879 | nb_compr_bytes = bytes_target; |
| 880 | } |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 881 | } |
| 882 | |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 883 | } else { |
| 884 | nb_compr_bytes = 0; |
| 885 | } |
Jean-Marc Valin | c8a20b7 | 2010-07-21 14:07:11 -0400 | [diff] [blame] | 886 | |
Jean-Marc Valin | d186c91 | 2011-09-08 15:13:46 -0400 | [diff] [blame] | 887 | for (i=0;i<st->channels*(st->encoder_buffer-(frame_size+delay_compensation));i++) |
Jean-Marc Valin | 12c3992 | 2011-09-04 11:00:29 -0400 | [diff] [blame] | 888 | st->delay_buffer[i] = st->delay_buffer[i+st->channels*frame_size]; |
| 889 | for (;i<st->encoder_buffer*st->channels;i++) |
Jean-Marc Valin | d186c91 | 2011-09-08 15:13:46 -0400 | [diff] [blame] | 890 | st->delay_buffer[i] = pcm_buf[(frame_size+delay_compensation-st->encoder_buffer)*st->channels+i]; |
Jean-Marc Valin | 2f0ca76 | 2011-09-01 16:56:40 -0400 | [diff] [blame] | 891 | |
Jean-Marc Valin | 103302b | 2011-05-19 17:17:44 -0400 | [diff] [blame] | 892 | |
Jean-Marc Valin | de3e16c | 2011-10-03 00:39:29 -0400 | [diff] [blame] | 893 | if (st->mode != MODE_HYBRID || st->stream_channels==1) |
Jean-Marc Valin | bc2eebd | 2011-10-04 11:59:32 -0400 | [diff] [blame] | 894 | st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14 = 1<<14; |
Jean-Marc Valin | de3e16c | 2011-10-03 00:39:29 -0400 | [diff] [blame] | 895 | if( st->channels == 2 ) { |
Jean-Marc Valin | 103302b | 2011-05-19 17:17:44 -0400 | [diff] [blame] | 896 | /* Apply stereo width reduction (at low bitrates) */ |
| 897 | if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) { |
Jean-Marc Valin | de3e16c | 2011-10-03 00:39:29 -0400 | [diff] [blame] | 898 | opus_val16 g1, g2; |
| 899 | const CELTMode *celt_mode; |
| 900 | |
| 901 | celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode)); |
| 902 | g1 = st->hybrid_stereo_width_Q14; |
| 903 | g2 = st->silk_mode.stereoWidth_Q14; |
| 904 | #ifdef FIXED_POINT |
| 905 | g1 *= (1./16384); |
| 906 | g2 *= (1./16384); |
| 907 | #else |
| 908 | g1 = g1==16384 ? Q15ONE : SHL16(g1,1); |
| 909 | g2 = g2==16384 ? Q15ONE : SHL16(g2,1); |
| 910 | #endif |
| 911 | stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap, |
| 912 | frame_size, st->channels, celt_mode->window, st->Fs); |
Jean-Marc Valin | 103302b | 2011-05-19 17:17:44 -0400 | [diff] [blame] | 913 | st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14; |
| 914 | } |
| 915 | } |
| 916 | |
Jean-Marc Valin | e5736ae | 2011-09-05 10:28:27 -0400 | [diff] [blame] | 917 | if (st->mode != MODE_CELT_ONLY && ec_tell(&enc)+29+8*(st->mode == MODE_HYBRID) < 8*nb_compr_bytes) |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 918 | { |
| 919 | /* Check if we have a redundant 0-8 kHz band */ |
| 920 | ec_enc_bit_logp(&enc, redundancy, 12); |
| 921 | if (redundancy) |
| 922 | { |
Jean-Marc Valin | e5736ae | 2011-09-05 10:28:27 -0400 | [diff] [blame] | 923 | int max_redundancy; |
| 924 | ec_enc_bit_logp(&enc, celt_to_silk, 1); |
| 925 | max_redundancy = nb_compr_bytes-((ec_tell(&enc)+7)>>3)-(st->mode == MODE_HYBRID); |
Jean-Marc Valin | 51ac8e6 | 2011-08-15 22:06:02 -0400 | [diff] [blame] | 926 | /* Target the same bit-rate for redundancy as for the rest, |
| 927 | up to a max of 257 bytes */ |
Jean-Marc Valin | e5736ae | 2011-09-05 10:28:27 -0400 | [diff] [blame] | 928 | redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600); |
| 929 | redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes)); |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 930 | if (st->mode == MODE_HYBRID) |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 931 | ec_enc_uint(&enc, redundancy_bytes-2, 256); |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 932 | } |
Jean-Marc Valin | e5736ae | 2011-09-05 10:28:27 -0400 | [diff] [blame] | 933 | } else { |
| 934 | redundancy = 0; |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 935 | } |
| 936 | |
Gregory Maxwell | 7d1df73 | 2011-09-05 11:28:28 -0400 | [diff] [blame] | 937 | if (st->mode != MODE_CELT_ONLY)start_band=17; |
| 938 | |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 939 | if (st->mode == MODE_SILK_ONLY) |
| 940 | { |
| 941 | ret = (ec_tell(&enc)+7)>>3; |
| 942 | ec_enc_done(&enc); |
Gregory Maxwell | 03f7de7 | 2011-08-15 17:25:59 -0400 | [diff] [blame] | 943 | /*When in LPC only mode it's perfectly |
| 944 | reasonable to strip off trailing zero bytes as |
| 945 | the required range decoder behavior is to |
| 946 | fill these in. This can't be done when the MDCT |
| 947 | modes are used because the decoder needs to know |
| 948 | the actual length for allocation purposes.*/ |
| 949 | if(!redundancy) |
| 950 | while(ret>2&&data[ret-1]==0)ret--; |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 951 | nb_compr_bytes = ret; |
Jean-Marc Valin | 51ac8e6 | 2011-08-15 22:06:02 -0400 | [diff] [blame] | 952 | } else { |
| 953 | nb_compr_bytes = IMIN(1275-redundancy_bytes, nb_compr_bytes); |
| 954 | ec_enc_shrink(&enc, nb_compr_bytes); |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 955 | } |
| 956 | |
Jean-Marc Valin | 51ac8e6 | 2011-08-15 22:06:02 -0400 | [diff] [blame] | 957 | |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 958 | /* 5 ms redundant frame for CELT->SILK */ |
| 959 | if (redundancy && celt_to_silk) |
| 960 | { |
Jean-Marc Valin | 5095c47 | 2011-05-05 19:47:48 -0400 | [diff] [blame] | 961 | celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 962 | celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); |
Jean-Marc Valin | 58686e6 | 2011-08-31 15:08:12 -0400 | [diff] [blame] | 963 | celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 964 | celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)); |
| 965 | celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 966 | } |
| 967 | |
Jean-Marc Valin | 5095c47 | 2011-05-05 19:47:48 -0400 | [diff] [blame] | 968 | celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band)); |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 969 | |
| 970 | if (st->mode != MODE_SILK_ONLY) |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 971 | { |
| 972 | ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc); |
Jean-Marc Valin | 69f44c5 | 2011-09-07 02:43:23 -0400 | [diff] [blame] | 973 | if (ret < 0) |
| 974 | return OPUS_INTERNAL_ERROR; |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 975 | } |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 976 | |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 977 | /* 5 ms redundant frame for SILK->CELT */ |
| 978 | if (redundancy && !celt_to_silk) |
| 979 | { |
| 980 | int N2, N4; |
| 981 | N2 = st->Fs/200; |
| 982 | N4 = st->Fs/400; |
| 983 | |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 984 | celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); |
Jean-Marc Valin | 5095c47 | 2011-05-05 19:47:48 -0400 | [diff] [blame] | 985 | celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)); |
| 986 | celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)); |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 987 | |
Jean-Marc Valin | 9267506 | 2011-09-11 22:00:46 -0400 | [diff] [blame] | 988 | /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */ |
Jean-Marc Valin | 58686e6 | 2011-08-31 15:08:12 -0400 | [diff] [blame] | 989 | celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, data+nb_compr_bytes, redundancy_bytes, NULL); |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 990 | |
Jean-Marc Valin | 58686e6 | 2011-08-31 15:08:12 -0400 | [diff] [blame] | 991 | celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 992 | celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)); |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 996 | |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 997 | /* Signalling the mode in the first byte */ |
| 998 | data--; |
Jean-Marc Valin | 69bfc67 | 2011-08-29 21:46:17 -0400 | [diff] [blame] | 999 | data[0] = gen_toc(st->mode, st->Fs/frame_size, st->bandwidth, silk_internal_bandwidth, st->stream_channels); |
Jean-Marc Valin | 57de8a3 | 2010-09-07 17:37:56 -0400 | [diff] [blame] | 1000 | |
Jean-Marc Valin | 06cee2b | 2011-08-19 16:11:41 -0400 | [diff] [blame] | 1001 | st->rangeFinal = enc.rng ^ redundant_rng; |
Ralph Giles | 3f0962c | 2011-07-29 14:01:54 -0700 | [diff] [blame] | 1002 | |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 1003 | if (to_celt) |
| 1004 | st->prev_mode = MODE_CELT_ONLY; |
| 1005 | else |
| 1006 | st->prev_mode = st->mode; |
Jean-Marc Valin | de3e16c | 2011-10-03 00:39:29 -0400 | [diff] [blame] | 1007 | st->prev_channels = st->stream_channels; |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 1008 | st->prev_framesize = frame_size; |
Jean-Marc Valin | de3e16c | 2011-10-03 00:39:29 -0400 | [diff] [blame] | 1009 | |
Jean-Marc Valin | c1673fa | 2011-04-28 08:04:20 -0400 | [diff] [blame] | 1010 | st->first = 0; |
Jean-Marc Valin | 44e27dd | 2011-08-29 23:35:17 -0400 | [diff] [blame] | 1011 | RESTORE_STACK; |
Jean-Marc Valin | 0c0c5f9 | 2011-03-07 20:54:33 -0500 | [diff] [blame] | 1012 | return ret+1+redundancy_bytes; |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1013 | } |
| 1014 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1015 | #ifdef FIXED_POINT |
| 1016 | |
| 1017 | #ifndef DISABLE_FLOAT_API |
| 1018 | int opus_encode_float(OpusEncoder *st, const float *pcm, int frame_size, |
| 1019 | unsigned char *data, int max_data_bytes) |
| 1020 | { |
| 1021 | int i, ret; |
| 1022 | VARDECL(opus_int16, in); |
| 1023 | ALLOC_STACK; |
| 1024 | |
| 1025 | ALLOC(in, frame_size*st->channels, opus_int16); |
| 1026 | |
| 1027 | for (i=0;i<frame_size*st->channels;i++) |
| 1028 | in[i] = FLOAT2INT16(pcm[i]); |
| 1029 | ret = opus_encode(st, in, frame_size, data, max_data_bytes); |
| 1030 | RESTORE_STACK; |
| 1031 | return ret; |
| 1032 | } |
| 1033 | #endif |
| 1034 | |
| 1035 | #else |
| 1036 | int opus_encode(OpusEncoder *st, const opus_int16 *pcm, int frame_size, |
| 1037 | unsigned char *data, int max_data_bytes) |
| 1038 | { |
| 1039 | int i, ret; |
| 1040 | VARDECL(float, in); |
| 1041 | ALLOC_STACK; |
| 1042 | |
| 1043 | ALLOC(in, frame_size*st->channels, float); |
| 1044 | |
| 1045 | for (i=0;i<frame_size*st->channels;i++) |
| 1046 | in[i] = (1./32768)*pcm[i]; |
| 1047 | ret = opus_encode_float(st, in, frame_size, data, max_data_bytes); |
| 1048 | RESTORE_STACK; |
| 1049 | return ret; |
| 1050 | } |
| 1051 | #endif |
| 1052 | |
| 1053 | |
Jean-Marc Valin | 955f94c | 2011-03-08 22:12:43 -0500 | [diff] [blame] | 1054 | int opus_encoder_ctl(OpusEncoder *st, int request, ...) |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1055 | { |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1056 | int ret; |
Ralph Giles | 641eea8 | 2011-08-02 10:06:59 -0700 | [diff] [blame] | 1057 | CELTEncoder *celt_enc; |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1058 | va_list ap; |
| 1059 | |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1060 | ret = OPUS_OK; |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1061 | va_start(ap, request); |
| 1062 | |
Jean-Marc Valin | 5095c47 | 2011-05-05 19:47:48 -0400 | [diff] [blame] | 1063 | celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); |
| 1064 | |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1065 | switch (request) |
| 1066 | { |
Jean-Marc Valin | 69549ac | 2011-08-18 17:28:28 -0400 | [diff] [blame] | 1067 | case OPUS_SET_APPLICATION_REQUEST: |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1068 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1069 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 68bc8c0 | 2011-09-09 11:10:48 -0400 | [diff] [blame] | 1070 | if ( (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO |
| 1071 | && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY) |
| 1072 | || (!st->first && st->application != value)) |
| 1073 | { |
| 1074 | ret = OPUS_BAD_ARG; |
| 1075 | break; |
| 1076 | } |
Jean-Marc Valin | 69549ac | 2011-08-18 17:28:28 -0400 | [diff] [blame] | 1077 | st->application = value; |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1078 | } |
| 1079 | break; |
Jean-Marc Valin | 69549ac | 2011-08-18 17:28:28 -0400 | [diff] [blame] | 1080 | case OPUS_GET_APPLICATION_REQUEST: |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1081 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1082 | opus_int32 *value = va_arg(ap, opus_int32*); |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 1083 | *value = st->application; |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1084 | } |
| 1085 | break; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1086 | case OPUS_SET_BITRATE_REQUEST: |
| 1087 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1088 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 1089 | if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX) |
Jean-Marc Valin | ba54746 | 2011-05-10 17:54:41 -0400 | [diff] [blame] | 1090 | { |
| 1091 | if (value <= 0) |
| 1092 | goto bad_arg; |
| 1093 | else if (value <= 500) |
| 1094 | value = 500; |
Jean-Marc Valin | 69f44c5 | 2011-09-07 02:43:23 -0400 | [diff] [blame] | 1095 | else if (value > (opus_int32)300000*st->channels) |
| 1096 | value = (opus_int32)300000*st->channels; |
Jean-Marc Valin | ba54746 | 2011-05-10 17:54:41 -0400 | [diff] [blame] | 1097 | } |
| 1098 | st->user_bitrate_bps = value; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1099 | } |
| 1100 | break; |
| 1101 | case OPUS_GET_BITRATE_REQUEST: |
| 1102 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1103 | opus_int32 *value = va_arg(ap, opus_int32*); |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 1104 | *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276); |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1105 | } |
| 1106 | break; |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 1107 | case OPUS_SET_FORCE_CHANNELS_REQUEST: |
Jean-Marc Valin | 103302b | 2011-05-19 17:17:44 -0400 | [diff] [blame] | 1108 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1109 | opus_int32 value = va_arg(ap, opus_int32); |
Gregory Maxwell | b9f443d | 2011-10-04 01:20:39 -0400 | [diff] [blame^] | 1110 | if((value<1 || value>st->channels) && value != OPUS_AUTO) |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 1111 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 1112 | st->force_channels = value; |
Jean-Marc Valin | 103302b | 2011-05-19 17:17:44 -0400 | [diff] [blame] | 1113 | } |
| 1114 | break; |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 1115 | case OPUS_GET_FORCE_CHANNELS_REQUEST: |
Jean-Marc Valin | 103302b | 2011-05-19 17:17:44 -0400 | [diff] [blame] | 1116 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1117 | opus_int32 *value = va_arg(ap, opus_int32*); |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 1118 | *value = st->force_channels; |
Jean-Marc Valin | 103302b | 2011-05-19 17:17:44 -0400 | [diff] [blame] | 1119 | } |
| 1120 | break; |
Jean-Marc Valin | 05dd36a | 2010-10-18 12:50:49 -0400 | [diff] [blame] | 1121 | case OPUS_SET_BANDWIDTH_REQUEST: |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1122 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1123 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 1124 | if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO) |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1125 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 955f94c | 2011-03-08 22:12:43 -0500 | [diff] [blame] | 1126 | st->user_bandwidth = value; |
Jean-Marc Valin | 541df0a | 2011-05-26 00:22:58 -0400 | [diff] [blame] | 1127 | if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1128 | st->silk_mode.maxInternalSampleRate = 8000; |
Gregory Maxwell | 8714189 | 2011-10-02 03:19:15 -0400 | [diff] [blame] | 1129 | } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1130 | st->silk_mode.maxInternalSampleRate = 12000; |
| 1131 | } else { |
| 1132 | st->silk_mode.maxInternalSampleRate = 16000; |
| 1133 | } |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1134 | } |
| 1135 | break; |
Jean-Marc Valin | 05dd36a | 2010-10-18 12:50:49 -0400 | [diff] [blame] | 1136 | case OPUS_GET_BANDWIDTH_REQUEST: |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1137 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1138 | opus_int32 *value = va_arg(ap, opus_int32*); |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1139 | *value = st->bandwidth; |
| 1140 | } |
| 1141 | break; |
Jean-Marc Valin | d482773 | 2011-08-19 17:07:16 -0400 | [diff] [blame] | 1142 | case OPUS_SET_DTX_REQUEST: |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1143 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1144 | opus_int32 value = va_arg(ap, opus_int32); |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 1145 | if(value<0 || value>1) |
| 1146 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1147 | st->silk_mode.useDTX = value; |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1148 | } |
| 1149 | break; |
Jean-Marc Valin | d482773 | 2011-08-19 17:07:16 -0400 | [diff] [blame] | 1150 | case OPUS_GET_DTX_REQUEST: |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1151 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1152 | opus_int32 *value = va_arg(ap, opus_int32*); |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1153 | *value = st->silk_mode.useDTX; |
| 1154 | } |
| 1155 | break; |
| 1156 | case OPUS_SET_COMPLEXITY_REQUEST: |
| 1157 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1158 | opus_int32 value = va_arg(ap, opus_int32); |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 1159 | if(value<0 || value>10) |
| 1160 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1161 | st->silk_mode.complexity = value; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1162 | celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value)); |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1163 | } |
| 1164 | break; |
| 1165 | case OPUS_GET_COMPLEXITY_REQUEST: |
| 1166 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1167 | opus_int32 *value = va_arg(ap, opus_int32*); |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1168 | *value = st->silk_mode.complexity; |
| 1169 | } |
| 1170 | break; |
Jean-Marc Valin | e766d9f | 2011-08-28 20:27:18 -0400 | [diff] [blame] | 1171 | case OPUS_SET_INBAND_FEC_REQUEST: |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1172 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1173 | opus_int32 value = va_arg(ap, opus_int32); |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 1174 | if(value<0 || value>1) |
| 1175 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1176 | st->silk_mode.useInBandFEC = value; |
| 1177 | } |
| 1178 | break; |
Jean-Marc Valin | e766d9f | 2011-08-28 20:27:18 -0400 | [diff] [blame] | 1179 | case OPUS_GET_INBAND_FEC_REQUEST: |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1180 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1181 | opus_int32 *value = va_arg(ap, opus_int32*); |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1182 | *value = st->silk_mode.useInBandFEC; |
| 1183 | } |
| 1184 | break; |
| 1185 | case OPUS_SET_PACKET_LOSS_PERC_REQUEST: |
| 1186 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1187 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | d2124cd | 2011-04-25 13:36:14 -0400 | [diff] [blame] | 1188 | if (value < 0 || value > 100) |
| 1189 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1190 | st->silk_mode.packetLossPercentage = value; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1191 | celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value)); |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1192 | } |
| 1193 | break; |
| 1194 | case OPUS_GET_PACKET_LOSS_PERC_REQUEST: |
| 1195 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1196 | opus_int32 *value = va_arg(ap, opus_int32*); |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1197 | *value = st->silk_mode.packetLossPercentage; |
| 1198 | } |
| 1199 | break; |
Jean-Marc Valin | d7f6700 | 2011-08-18 16:55:24 -0400 | [diff] [blame] | 1200 | case OPUS_SET_VBR_REQUEST: |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1201 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1202 | opus_int32 value = va_arg(ap, opus_int32); |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 1203 | if(value<0 || value>1) |
| 1204 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1205 | st->use_vbr = value; |
| 1206 | st->silk_mode.useCBR = 1-value; |
| 1207 | } |
| 1208 | break; |
Jean-Marc Valin | d7f6700 | 2011-08-18 16:55:24 -0400 | [diff] [blame] | 1209 | case OPUS_GET_VBR_REQUEST: |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1210 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1211 | opus_int32 *value = va_arg(ap, opus_int32*); |
Jean-Marc Valin | 2b68ea9 | 2011-02-01 10:35:23 -0500 | [diff] [blame] | 1212 | *value = st->use_vbr; |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1213 | } |
| 1214 | break; |
Jean-Marc Valin | f9bc460 | 2011-03-08 14:57:46 -0500 | [diff] [blame] | 1215 | case OPUS_SET_VOICE_RATIO_REQUEST: |
| 1216 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1217 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | c681bd0 | 2011-09-02 14:47:26 -0400 | [diff] [blame] | 1218 | if (value>100 || value<-1) |
Jean-Marc Valin | d2124cd | 2011-04-25 13:36:14 -0400 | [diff] [blame] | 1219 | goto bad_arg; |
Jean-Marc Valin | f9bc460 | 2011-03-08 14:57:46 -0500 | [diff] [blame] | 1220 | st->voice_ratio = value; |
| 1221 | } |
| 1222 | break; |
| 1223 | case OPUS_GET_VOICE_RATIO_REQUEST: |
| 1224 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1225 | opus_int32 *value = va_arg(ap, opus_int32*); |
Jean-Marc Valin | f9bc460 | 2011-03-08 14:57:46 -0500 | [diff] [blame] | 1226 | *value = st->voice_ratio; |
| 1227 | } |
| 1228 | break; |
Jean-Marc Valin | b55b661 | 2011-03-14 15:11:44 -0400 | [diff] [blame] | 1229 | case OPUS_SET_VBR_CONSTRAINT_REQUEST: |
| 1230 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1231 | opus_int32 value = va_arg(ap, opus_int32); |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 1232 | if(value<0 || value>1) |
| 1233 | return OPUS_BAD_ARG; |
Jean-Marc Valin | b55b661 | 2011-03-14 15:11:44 -0400 | [diff] [blame] | 1234 | st->vbr_constraint = value; |
| 1235 | } |
| 1236 | break; |
| 1237 | case OPUS_GET_VBR_CONSTRAINT_REQUEST: |
| 1238 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1239 | opus_int32 *value = va_arg(ap, opus_int32*); |
Jean-Marc Valin | b55b661 | 2011-03-14 15:11:44 -0400 | [diff] [blame] | 1240 | *value = st->vbr_constraint; |
| 1241 | } |
| 1242 | break; |
Jean-Marc Valin | 00cb6f7 | 2011-05-26 15:06:30 -0400 | [diff] [blame] | 1243 | case OPUS_SET_SIGNAL_REQUEST: |
| 1244 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1245 | opus_int32 value = va_arg(ap, opus_int32); |
Gregory Maxwell | 220a7d4 | 2011-10-01 20:30:16 -0400 | [diff] [blame] | 1246 | if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC) |
| 1247 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 00cb6f7 | 2011-05-26 15:06:30 -0400 | [diff] [blame] | 1248 | st->signal_type = value; |
| 1249 | } |
| 1250 | break; |
| 1251 | case OPUS_GET_SIGNAL_REQUEST: |
| 1252 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1253 | opus_int32 *value = va_arg(ap, opus_int32*); |
Jean-Marc Valin | 00cb6f7 | 2011-05-26 15:06:30 -0400 | [diff] [blame] | 1254 | *value = st->signal_type; |
| 1255 | } |
| 1256 | break; |
Jean-Marc Valin | 9d8b519 | 2011-08-03 12:44:37 -0400 | [diff] [blame] | 1257 | case OPUS_GET_LOOKAHEAD_REQUEST: |
| 1258 | { |
Ralph Giles | 04cca28 | 2011-08-24 00:29:29 -0400 | [diff] [blame] | 1259 | opus_int32 *value = va_arg(ap, opus_int32*); |
Jean-Marc Valin | d186c91 | 2011-09-08 15:13:46 -0400 | [diff] [blame] | 1260 | *value = st->Fs/400; |
Jean-Marc Valin | 68bc8c0 | 2011-09-09 11:10:48 -0400 | [diff] [blame] | 1261 | if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY) |
Jean-Marc Valin | d186c91 | 2011-09-08 15:13:46 -0400 | [diff] [blame] | 1262 | *value += st->delay_compensation; |
Jean-Marc Valin | 9d8b519 | 2011-08-03 12:44:37 -0400 | [diff] [blame] | 1263 | } |
| 1264 | break; |
Jean-Marc Valin | d482773 | 2011-08-19 17:07:16 -0400 | [diff] [blame] | 1265 | case OPUS_GET_FINAL_RANGE_REQUEST: |
| 1266 | { |
| 1267 | opus_uint32 *value = va_arg(ap, opus_uint32*); |
| 1268 | *value = st->rangeFinal; |
| 1269 | } |
| 1270 | break; |
Jean-Marc Valin | 927488b | 2011-08-30 20:09:22 -0400 | [diff] [blame] | 1271 | case OPUS_RESET_STATE: |
| 1272 | { |
| 1273 | void *silk_enc; |
| 1274 | silk_EncControlStruct dummy; |
| 1275 | silk_enc = (char*)st+st->silk_enc_offset; |
| 1276 | |
| 1277 | OPUS_CLEAR((char*)&st->OPUS_ENCODER_RESET_START, |
Gregory Maxwell | d32f948 | 2011-09-04 07:48:20 -0400 | [diff] [blame] | 1278 | sizeof(OpusEncoder)- |
Jean-Marc Valin | 927488b | 2011-08-30 20:09:22 -0400 | [diff] [blame] | 1279 | ((char*)&st->OPUS_ENCODER_RESET_START - (char*)st)); |
| 1280 | |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1281 | celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); |
Jean-Marc Valin | 927488b | 2011-08-30 20:09:22 -0400 | [diff] [blame] | 1282 | silk_InitEncoder( silk_enc, &dummy ); |
| 1283 | st->stream_channels = st->channels; |
| 1284 | st->hybrid_stereo_width_Q14 = 1 << 14; |
| 1285 | st->first = 1; |
| 1286 | st->mode = MODE_HYBRID; |
| 1287 | st->bandwidth = OPUS_BANDWIDTH_FULLBAND; |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 1288 | st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); |
Jean-Marc Valin | 927488b | 2011-08-30 20:09:22 -0400 | [diff] [blame] | 1289 | } |
| 1290 | break; |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 1291 | case OPUS_SET_FORCE_MODE_REQUEST: |
| 1292 | { |
| 1293 | opus_int32 value = va_arg(ap, opus_int32); |
Gregory Maxwell | ed921b0 | 2011-10-02 20:04:28 -0400 | [diff] [blame] | 1294 | if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO) |
Jean-Marc Valin | 07dceb7 | 2011-09-08 13:53:20 -0400 | [diff] [blame] | 1295 | goto bad_arg; |
| 1296 | st->user_forced_mode = value; |
| 1297 | } |
| 1298 | break; |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1299 | default: |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1300 | /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/ |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1301 | ret = OPUS_UNIMPLEMENTED; |
Jean-Marc Valin | 24f36e0 | 2010-07-06 14:41:20 -0400 | [diff] [blame] | 1302 | break; |
Jean-Marc Valin | c5ea074 | 2010-07-05 11:11:54 -0400 | [diff] [blame] | 1303 | } |
Alfred E. Heggestad | 3d31d70 | 2010-07-27 16:38:04 +0200 | [diff] [blame] | 1304 | va_end(ap); |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1305 | return ret; |
Jean-Marc Valin | d2124cd | 2011-04-25 13:36:14 -0400 | [diff] [blame] | 1306 | bad_arg: |
| 1307 | va_end(ap); |
| 1308 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 3e66e91 | 2010-06-30 14:16:14 -0400 | [diff] [blame] | 1309 | } |
| 1310 | |
Jean-Marc Valin | 05dd36a | 2010-10-18 12:50:49 -0400 | [diff] [blame] | 1311 | void opus_encoder_destroy(OpusEncoder *st) |
Jean-Marc Valin | 3e66e91 | 2010-06-30 14:16:14 -0400 | [diff] [blame] | 1312 | { |
Jean-Marc Valin | 07f8840 | 2011-08-29 15:08:51 -0400 | [diff] [blame] | 1313 | opus_free(st); |
Jean-Marc Valin | 3e66e91 | 2010-06-30 14:16:14 -0400 | [diff] [blame] | 1314 | } |