opus_encoder.c 22.2 KB
Newer Older
1
2
/* Copyright (c) 2010 Xiph.Org Foundation, Skype Limited
   Written by Jean-Marc Valin and Koen Vos */
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
33
#include <stdio.h>
34
#include <stdarg.h>
35
#include "celt.h"
Jean-Marc Valin's avatar
Jean-Marc Valin committed
36
#include "opus_encoder.h"
Jean-Marc Valin's avatar
Jean-Marc Valin committed
37
38
#include "entenc.h"
#include "modes.h"
39
#include "SKP_Silk_SDK_API.h"
40

41
OpusEncoder *opus_encoder_create(int Fs, int channels)
42
{
Jean-Marc Valin's avatar
Jean-Marc Valin committed
43
    int err;
44
    char *raw_state;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
45
	OpusEncoder *st;
46
	int ret, silkEncSizeBytes, celtEncSizeBytes;
47

48
    /* Create SILK encoder */
49
    ret = SKP_Silk_SDK_Get_Encoder_Size( &silkEncSizeBytes );
50
51
52
    if( ret ) {
    	/* Handle error */
    }
Jean-Marc Valin's avatar
Jean-Marc Valin committed
53
    celtEncSizeBytes = celt_encoder_get_size(channels);
Jean-Marc Valin's avatar
Jean-Marc Valin committed
54
55
56
57
    raw_state = calloc(sizeof(OpusEncoder)+silkEncSizeBytes+celtEncSizeBytes, 1);
    st = (OpusEncoder*)raw_state;
    st->silk_enc = (void*)(raw_state+sizeof(OpusEncoder));
    st->celt_enc = (CELTEncoder*)(raw_state+sizeof(OpusEncoder)+silkEncSizeBytes);
58
    st->stream_channels = st->channels = channels;
59
60

    st->Fs = Fs;
61

62
    ret = SKP_Silk_SDK_InitEncoder( st->silk_enc, &st->silk_mode );
63
64
65
    if( ret ) {
        /* Handle error */
    }
66

67
68
    /* default SILK parameters */
    st->silk_mode.API_sampleRate        = st->Fs;
Koen Vos's avatar
Koen Vos committed
69
    st->silk_mode.nChannels             = channels;
70
71
72
73
74
75
    st->silk_mode.maxInternalSampleRate = 16000;
    st->silk_mode.minInternalSampleRate = 8000;
    st->silk_mode.payloadSize_ms        = 20;
    st->silk_mode.packetLossPercentage  = 0;
    st->silk_mode.useInBandFEC          = 0;
    st->silk_mode.useDTX                = 0;
Koen Vos's avatar
Koen Vos committed
76
    st->silk_mode.complexity            = 10;
77

78
    /* Create CELT encoder */
79
	/* Initialize CELT encoder */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
80
	st->celt_enc = celt_encoder_init(st->celt_enc, Fs, channels, &err);
81
    celt_encoder_ctl(st->celt_enc, CELT_SET_SIGNALLING(0));
82

83
84
	st->mode = MODE_HYBRID;
	st->bandwidth = BANDWIDTH_FULLBAND;
85
86
	st->use_vbr = 0;
	st->bitrate_bps = 32000;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
87
	st->user_mode = OPUS_MODE_AUTO;
88
	st->user_bandwidth = BANDWIDTH_AUTO;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
89
	st->voice_ratio = 90;
90
	st->first = 1;
91

92
93
94
95
	st->encoder_buffer = st->Fs/100;
	st->delay_compensation = st->Fs/400;
	if (st->Fs > 16000)
		st->delay_compensation += 10;
96
97
98
	return st;
}

Jean-Marc Valin's avatar
Jean-Marc Valin committed
99
int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
100
		unsigned char *data, int max_data_bytes)
101
{
102
    int i;
103
	int ret=0;
104
	SKP_int32 nBytes;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
105
	ec_enc enc;
106
	int framerate, period;
107
108
    int silk_internal_bandwidth;
    int bytes_target;
109
    int prefill=0;
110
    int start_band = 0;
111
    int redundancy = 0;
112
113
    int redundancy_bytes = 0;
    int celt_to_silk = 0;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
114
115
116
    /* TODO: This is 60 only so we can handle 60ms speech/audio switching 
       it shouldn't bee too hard to reduce to 20 ms if needed */
    short pcm_buf[3*960*2];
117
118
    int nb_compr_bytes;
    int to_celt = 0;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
119
120
    celt_int32 mono_rate;

Jean-Marc Valin's avatar
Jean-Marc Valin committed
121
    /* Rete-dependent mono-stereo decision */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
122
123
124
125
    if (st->channels == 2)
    {
        celt_int32 decision_rate;
        decision_rate = st->bitrate_bps + st->voice_ratio*st->voice_ratio;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
126
        /* Add some hysteresis */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
        if (st->stream_channels == 2)
            decision_rate += 4000;
        else
            decision_rate -= 4000;
        if (decision_rate>48000)
            st->stream_channels = 2;
        else
            st->stream_channels = 1;
    } else {
        st->stream_channels = 1;
    }
    /* Equivalent bit-rate for mono */
    mono_rate = st->bitrate_bps;
    if (st->stream_channels==2)
        mono_rate = (mono_rate+10000)/2;
142
143
144
    /* Compensate for smaller frame sizes assuming an equivalent overhead
       of 60 bits/frame */
    mono_rate -= 60*(st->Fs/frame_size - 50);
Jean-Marc Valin's avatar
Jean-Marc Valin committed
145
146
147
148
149

    /* Mode selection */
    if (st->user_mode==OPUS_MODE_AUTO)
    {
        celt_int32 decision_rate;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
150
        /* SILK/CELT threshold is higher for voice than for music */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
151
        decision_rate = mono_rate - 3*st->voice_ratio*st->voice_ratio;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
152
        /* Hysteresis */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
        if (st->prev_mode == MODE_CELT_ONLY)
            decision_rate += 4000;
        else if (st->prev_mode>0)
            decision_rate -= 4000;
        if (decision_rate>24000)
            st->mode = MODE_CELT_ONLY;
        else
            st->mode = MODE_SILK_ONLY;
    } else if (st->user_mode==OPUS_MODE_VOICE)
    {
        st->mode = MODE_SILK_ONLY;
    } else {/* OPUS_AUDIO_MODE */
        st->mode = MODE_CELT_ONLY;
    }

Jean-Marc Valin's avatar
Jean-Marc Valin committed
168
    /* Automatic (rate-dependent) bandwidth selection */
169
    if (st->mode == MODE_CELT_ONLY)
Jean-Marc Valin's avatar
Jean-Marc Valin committed
170
    {
171
172
    	if (mono_rate>35000 || (mono_rate>28000 && st->bandwidth==BANDWIDTH_FULLBAND))
    		st->bandwidth = BANDWIDTH_FULLBAND;
173
    	else if (mono_rate>28000 || (mono_rate>24000 && st->bandwidth>=BANDWIDTH_SUPERWIDEBAND))
174
    		st->bandwidth = BANDWIDTH_SUPERWIDEBAND;
175
    	else if (mono_rate>24000 || (mono_rate>18000 && st->bandwidth>=BANDWIDTH_WIDEBAND))
176
177
178
179
180
    		st->bandwidth = BANDWIDTH_WIDEBAND;
    	else
    		st->bandwidth = BANDWIDTH_NARROWBAND;
    } else if (st->first || st->silk_mode.allowBandwidthSwitch)
    {
181
    	if (mono_rate>31000 || (mono_rate>27000 && st->bandwidth==BANDWIDTH_FULLBAND))
182
    		st->bandwidth = BANDWIDTH_FULLBAND;
183
    	else if (mono_rate>23000 || (mono_rate>19000 && st->bandwidth>=BANDWIDTH_SUPERWIDEBAND))
184
    		st->bandwidth = BANDWIDTH_SUPERWIDEBAND;
185
    	else if (mono_rate>16000 || (mono_rate>13000 && st->bandwidth>=BANDWIDTH_WIDEBAND))
186
    		st->bandwidth = BANDWIDTH_WIDEBAND;
187
    	else if (mono_rate>13000 || (mono_rate>10000 && st->bandwidth>=BANDWIDTH_MEDIUMBAND))
188
189
190
    		st->bandwidth = BANDWIDTH_MEDIUMBAND;
    	else
    		st->bandwidth = BANDWIDTH_NARROWBAND;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
191
192
    	/* Prevents any transition to SWB/FB until the SILK layer has fully
    	   switched to WB mode and turned the variable LP filter off */
193
194
    	if (!st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > BANDWIDTH_WIDEBAND)
    		st->bandwidth = BANDWIDTH_WIDEBAND;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
195
    }
196

Jean-Marc Valin's avatar
Jean-Marc Valin committed
197
198
    /* Prevents Opus from wasting bits on frequencies that are above
       the Nyquist rate of the input signal */
199
200
201
202
203
204
205
206
207
208
209
210
    if (st->Fs <= 24000 && st->bandwidth > BANDWIDTH_SUPERWIDEBAND)
    	st->bandwidth = BANDWIDTH_SUPERWIDEBAND;
    if (st->Fs <= 16000 && st->bandwidth > BANDWIDTH_WIDEBAND)
    	st->bandwidth = BANDWIDTH_WIDEBAND;
    if (st->Fs <= 12000 && st->bandwidth > BANDWIDTH_MEDIUMBAND)
    	st->bandwidth = BANDWIDTH_MEDIUMBAND;
    if (st->Fs <= 8000 && st->bandwidth > BANDWIDTH_NARROWBAND)
    	st->bandwidth = BANDWIDTH_NARROWBAND;

    if (st->user_bandwidth != BANDWIDTH_AUTO)
    	st->bandwidth = st->user_bandwidth;

Jean-Marc Valin's avatar
Jean-Marc Valin committed
211
    /* Prevents nonsensical configurations, i.e. modes that don't exist */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
212
213
214
215
216
217
218
219
220
221
222
    if (frame_size < st->Fs/100 && st->mode != MODE_CELT_ONLY)
        st->mode = MODE_CELT_ONLY;
    if (frame_size > st->Fs/50 && st->mode != MODE_SILK_ONLY)
        st->mode = MODE_SILK_ONLY;
    if (st->mode == MODE_CELT_ONLY && st->bandwidth == BANDWIDTH_MEDIUMBAND)
        st->bandwidth = BANDWIDTH_WIDEBAND;
    if (st->mode == MODE_SILK_ONLY && st->bandwidth > BANDWIDTH_WIDEBAND)
        st->mode = MODE_HYBRID;
    if (st->mode == MODE_HYBRID && st->bandwidth <= BANDWIDTH_WIDEBAND)
        st->mode = MODE_SILK_ONLY;

223
	bytes_target = st->bitrate_bps * frame_size / (st->Fs * 8) - 1;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
224

225
	data += 1;
226
227
228
229
	if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
	{
		SKP_SILK_SDK_EncControlStruct dummy;
		SKP_Silk_SDK_InitEncoder( st->silk_enc, &dummy);
230
		prefill=1;
231
	}
232
233
234
235
236
237
238
239
	if (st->prev_mode >0 &&
	        ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
	        || (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
	{
	    redundancy = 1;
	    celt_to_silk = (st->mode != MODE_CELT_ONLY);
	    if (!celt_to_silk)
	    {
240
241
242
243
244
245
246
247
	        /* Switch to SILK/hybrid if frame size is 10 ms or more*/
	        if (frame_size >= st->Fs/100)
	        {
		        st->mode = st->prev_mode;
		        to_celt = 1;
	        } else {
	        	redundancy=0;
	        }
248
249
	    }
	}
250

251
	ec_enc_init(&enc, data, max_data_bytes-1);
Jean-Marc Valin's avatar
Jean-Marc Valin committed
252

253
	/* SILK processing */
254
255
    if (st->mode != MODE_CELT_ONLY)
    {
256
257
        st->silk_mode.bitRate = st->bitrate_bps - 8*st->Fs/frame_size;
        if( st->mode == MODE_HYBRID ) {
Karsten Vandborg Sorensen's avatar
Karsten Vandborg Sorensen committed
258
259
260
            if( st->bandwidth == BANDWIDTH_SUPERWIDEBAND ) {
                if( st->Fs == 100 * frame_size ) {
                    /* 24 kHz, 10 ms */
Koen Vos's avatar
Koen Vos committed
261
                    st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 2000 + st->use_vbr * 1000 ) * 2 ) / 3;
Karsten Vandborg Sorensen's avatar
Karsten Vandborg Sorensen committed
262
263
                } else {
                    /* 24 kHz, 20 ms */
Koen Vos's avatar
Koen Vos committed
264
                    st->silk_mode.bitRate = ( ( st->silk_mode.bitRate + 1000 + st->use_vbr * 1000 ) * 2 ) / 3;
Karsten Vandborg Sorensen's avatar
Karsten Vandborg Sorensen committed
265
266
267
268
                }
            } else {
                if( st->Fs == 100 * frame_size ) {
                    /* 48 kHz, 10 ms */
Koen Vos's avatar
Koen Vos committed
269
                    st->silk_mode.bitRate = ( st->silk_mode.bitRate + 8000 + st->use_vbr * 3000 ) / 2;
Karsten Vandborg Sorensen's avatar
Karsten Vandborg Sorensen committed
270
271
                } else {
                    /* 48 kHz, 20 ms */
Koen Vos's avatar
Koen Vos committed
272
                    st->silk_mode.bitRate = ( st->silk_mode.bitRate + 9000 + st->use_vbr * 1000 ) / 2;
Karsten Vandborg Sorensen's avatar
Karsten Vandborg Sorensen committed
273
274
                }
            }
275
276
277
278
            /* don't let SILK use more than 80% */
            if( st->silk_mode.bitRate > ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5 ) {
                st->silk_mode.bitRate = ( st->bitrate_bps - 8*st->Fs/frame_size ) * 4/5;
            }
279
        }
280

281
282
        st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
        if (st->bandwidth == BANDWIDTH_NARROWBAND) {
283
        	st->silk_mode.desiredInternalSampleRate = 8000;
284
        } else if (st->bandwidth == BANDWIDTH_MEDIUMBAND) {
285
        	st->silk_mode.desiredInternalSampleRate = 12000;
286
287
        } else {
            SKP_assert( st->mode == MODE_HYBRID || st->bandwidth == BANDWIDTH_WIDEBAND );
288
            st->silk_mode.desiredInternalSampleRate = 16000;
289
290
291
        }
        if( st->mode == MODE_HYBRID ) {
            /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
Koen Vos's avatar
Koen Vos committed
292
293
294
            st->silk_mode.minInternalSampleRate = 16000;
        } else {
            st->silk_mode.minInternalSampleRate = 8000;
295
        }
296
        st->silk_mode.maxInternalSampleRate = 16000;
297

298
299
        /* Call SILK encoder for the low band */
        nBytes = max_data_bytes-1;
300
        if (prefill)
301
302
        {
            int zero=0;
303
        	SKP_Silk_SDK_Encode( st->silk_enc, &st->silk_mode, st->delay_buffer, st->encoder_buffer, NULL, &zero, 1 );
304
        }
305

Jean-Marc Valin's avatar
Jean-Marc Valin committed
306
        ret = SKP_Silk_SDK_Encode( st->silk_enc, &st->silk_mode, pcm, frame_size, &enc, &nBytes, 0 );
307
308
309
310
        if( ret ) {
            fprintf (stderr, "SILK encode error: %d\n", ret);
            /* Handle error */
        }
311
312
        if (nBytes==0)
            return 0;
313
314
315
316
317
318
319
320
321
        /* Extract SILK internal bandwidth for signaling in first byte */
        if( st->mode == MODE_SILK_ONLY ) {
            if( st->silk_mode.internalSampleRate == 8000 ) {
                silk_internal_bandwidth = BANDWIDTH_NARROWBAND;
            } else if( st->silk_mode.internalSampleRate == 12000 ) {
                silk_internal_bandwidth = BANDWIDTH_MEDIUMBAND;
            } else if( st->silk_mode.internalSampleRate == 16000 ) {
                silk_internal_bandwidth = BANDWIDTH_WIDEBAND;
            }
322
323
        } else {
            SKP_assert( st->silk_mode.internalSampleRate == 16000 );
324
325
326
327
        }
    }

    /* CELT processing */
328
	{
329
	    int endband=21;
330

Jean-Marc Valin's avatar
Jean-Marc Valin committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
	    switch(st->bandwidth)
	    {
	    case BANDWIDTH_NARROWBAND:
	    	endband = 13;
	    	break;
	    case BANDWIDTH_WIDEBAND:
	    	endband = 17;
	    	break;
	    case BANDWIDTH_SUPERWIDEBAND:
	    	endband = 19;
	    	break;
	    case BANDWIDTH_FULLBAND:
	    	endband = 21;
	    	break;
	    }
	    celt_encoder_ctl(st->celt_enc, CELT_SET_END_BAND(endband));
347
	    celt_encoder_ctl(st->celt_enc, CELT_SET_CHANNELS(st->stream_channels));
348
349
350
	}
	if (st->mode != MODE_SILK_ONLY)
	{
Jean-Marc Valin's avatar
Jean-Marc Valin committed
351
352
        celt_encoder_ctl(st->celt_enc, CELT_SET_VBR(0));
        celt_encoder_ctl(st->celt_enc, CELT_SET_BITRATE(510000));
353
354
        if (st->prev_mode == MODE_SILK_ONLY)
        {
355
        	unsigned char dummy[10];
356
        	celt_encoder_ctl(st->celt_enc, CELT_RESET_STATE);
357
        	celt_encoder_ctl(st->celt_enc, CELT_SET_START_BAND(0));
358
        	celt_encoder_ctl(st->celt_enc, CELT_SET_PREDICTION(0));
359
        	/* FIXME: This wastes CPU a bit compared to just prefilling the buffer */
360
        	celt_encode(st->celt_enc, &st->delay_buffer[(st->encoder_buffer-st->delay_compensation-st->Fs/400)*st->channels], st->Fs/400, dummy, 10);
361
362
363
        } else {
        	celt_encoder_ctl(st->celt_enc, CELT_SET_PREDICTION(2));
        }
364

365
366
367
368
        if (st->mode == MODE_HYBRID)
        {
            int len;

369
            len = (ec_tell(&enc)+7)>>3;
370
            if( st->use_vbr ) {
Koen Vos's avatar
Koen Vos committed
371
                nb_compr_bytes = len + bytes_target - (st->silk_mode.bitRate * frame_size) / (8 * st->Fs);
372
373
374
375
376
            } else {
                /* check if SILK used up too much */
                nb_compr_bytes = len > bytes_target ? len : bytes_target;
            }
        } else {
Jean-Marc Valin's avatar
Jean-Marc Valin committed
377
378
379
            if (st->use_vbr)
            {
                celt_encoder_ctl(st->celt_enc, CELT_SET_VBR(1));
Jean-Marc Valin's avatar
Jean-Marc Valin committed
380
                celt_encoder_ctl(st->celt_enc, CELT_SET_VBR_CONSTRAINT(st->vbr_constraint));
Jean-Marc Valin's avatar
Jean-Marc Valin committed
381
382
383
384
385
                celt_encoder_ctl(st->celt_enc, CELT_SET_BITRATE(st->bitrate_bps));
                nb_compr_bytes = max_data_bytes-1;
            } else {
                nb_compr_bytes = bytes_target;
            }
386
387
        }

388
        ec_enc_shrink(&enc, nb_compr_bytes);
389
390
391
	} else {
	    nb_compr_bytes = 0;
	}
Jean-Marc Valin's avatar
Jean-Marc Valin committed
392

393
394
395
396
397
398
399
400
401
402
    for (i=0;i<IMIN(frame_size, st->delay_compensation)*st->channels;i++)
        pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-st->delay_compensation)*st->channels+i];
    for (;i<frame_size*st->channels;i++)
        pcm_buf[i] = pcm[i-st->delay_compensation*st->channels];
    if (st->mode != MODE_CELT_ONLY)
    {
        /* Check if we have a redundant 0-8 kHz band */
        ec_enc_bit_logp(&enc, redundancy, 12);
        if (redundancy)
        {
Jean-Marc Valin's avatar
Jean-Marc Valin committed
403
            redundancy_bytes = st->stream_channels*st->bitrate_bps/1600;
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
            ec_enc_bit_logp(&enc, celt_to_silk, 1);
            if (st->mode == MODE_HYBRID)
            	ec_enc_uint(&enc, redundancy_bytes-2, 256);
        }
        start_band = 17;
    }

    if (st->mode == MODE_SILK_ONLY)
    {
        ret = (ec_tell(&enc)+7)>>3;
        ec_enc_done(&enc);
        nb_compr_bytes = ret;
    }

    /* 5 ms redundant frame for CELT->SILK */
    if (redundancy && celt_to_silk)
    {
        celt_encoder_ctl(st->celt_enc, CELT_SET_START_BAND(0));
Jean-Marc Valin's avatar
Jean-Marc Valin committed
422
        /* FIXME: That's OK for now, but we need to set the flags properly */
423
424
425
426
427
428
429
430
431
        celt_encoder_ctl(st->celt_enc, CELT_SET_VBR(0));
        celt_encode(st->celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes);
        celt_encoder_ctl(st->celt_enc, CELT_RESET_STATE);
    }

    celt_encoder_ctl(st->celt_enc, CELT_SET_START_BAND(start_band));

    if (st->mode != MODE_SILK_ONLY)
	{
432
	    /* Encode high band with CELT */
433
	    ret = celt_encode_with_ec(st->celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
434
435
	}

436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
    /* 5 ms redundant frame for SILK->CELT */
    if (redundancy && !celt_to_silk)
    {
        int N2, N4;
        N2 = st->Fs/200;
        N4 = st->Fs/400;

        celt_encoder_ctl(st->celt_enc, CELT_RESET_STATE);
        celt_encoder_ctl(st->celt_enc, CELT_SET_START_BAND(0));
        celt_encoder_ctl(st->celt_enc, CELT_SET_PREDICTION(0));

        /* FIXME: Do proper prefilling here */
        celt_encode(st->celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, data+nb_compr_bytes, redundancy_bytes);

        celt_encode(st->celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes);
    }


    if (frame_size>st->encoder_buffer)
    {
    	for (i=0;i<st->encoder_buffer*st->channels;i++)
    		st->delay_buffer[i] = pcm[(frame_size-st->encoder_buffer)*st->channels+i];
    } else {
    	int tmp = st->encoder_buffer-frame_size;
    	for (i=0;i<tmp*st->channels;i++)
    		st->delay_buffer[i] = st->delay_buffer[i+frame_size*st->channels];
    	for (i=0;i<frame_size*st->channels;i++)
    		st->delay_buffer[tmp*st->channels+i] = pcm[i];
    }

466
467
468
469
470
471
472
473
474
475
476
	/* Signalling the mode in the first byte */
	data--;
	framerate = st->Fs/frame_size;
	period = 0;
	while (framerate < 400)
	{
	    framerate <<= 1;
	    period++;
	}
    if (st->mode == MODE_SILK_ONLY)
    {
477
        data[0] = (silk_internal_bandwidth-BANDWIDTH_NARROWBAND)<<5;
478
479
480
481
482
483
484
485
486
        data[0] |= (period-2)<<3;
    } else if (st->mode == MODE_CELT_ONLY)
    {
        int tmp = st->bandwidth-BANDWIDTH_MEDIUMBAND;
        if (tmp < 0)
            tmp = 0;
        data[0] = 0x80;
        data[0] |= tmp << 5;
        data[0] |= period<<3;
487
    } else /* Hybrid */
488
489
490
491
492
    {
        data[0] = 0x60;
        data[0] |= (st->bandwidth-BANDWIDTH_SUPERWIDEBAND)<<4;
        data[0] |= (period-2)<<3;
    }
493
    data[0] |= (st->stream_channels==2)<<2;
494
495
    /*printf ("%x\n", (int)data[0]);*/

Koen Vos's avatar
Koen Vos committed
496
497
498
#if OPUS_TEST_RANGE_CODER_STATE
    st->rangeFinal = enc.rng;
#endif
499
500
501
502
    if (to_celt)
        st->prev_mode = MODE_CELT_ONLY;
    else
        st->prev_mode = st->mode;
503
    st->first = 0;
504
    return ret+1+redundancy_bytes;
505
}
506

507
int opus_encoder_ctl(OpusEncoder *st, int request, ...)
508
509
510
511
512
513
514
{
    va_list ap;

    va_start(ap, request);

    switch (request)
    {
Jean-Marc Valin's avatar
Jean-Marc Valin committed
515
        case OPUS_SET_MODE_REQUEST:
516
517
        {
            int value = va_arg(ap, int);
Jean-Marc Valin's avatar
Jean-Marc Valin committed
518
            st->user_mode = value;
519
520
        }
        break;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
521
        case OPUS_GET_MODE_REQUEST:
522
523
        {
            int *value = va_arg(ap, int*);
524
            *value = st->mode;
525
526
        }
        break;
527
528
529
530
531
532
533
534
535
536
537
538
        case OPUS_SET_BITRATE_REQUEST:
        {
            int value = va_arg(ap, int);
            st->bitrate_bps = value;
        }
        break;
        case OPUS_GET_BITRATE_REQUEST:
        {
            int *value = va_arg(ap, int*);
            *value = st->bitrate_bps;
        }
        break;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
539
        case OPUS_SET_BANDWIDTH_REQUEST:
540
541
        {
            int value = va_arg(ap, int);
542
543
544
545
            if (value < BANDWIDTH_AUTO || value > BANDWIDTH_FULLBAND)
            	return OPUS_BAD_ARG;
            st->user_bandwidth = value;
            if (st->user_bandwidth == BANDWIDTH_NARROWBAND) {
546
547
548
549
550
551
                st->silk_mode.maxInternalSampleRate = 8000;
            } else if (st->bandwidth == BANDWIDTH_MEDIUMBAND) {
                st->silk_mode.maxInternalSampleRate = 12000;
            } else {
                st->silk_mode.maxInternalSampleRate = 16000;
            }
552
553
        }
        break;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
554
        case OPUS_GET_BANDWIDTH_REQUEST:
555
556
557
558
559
        {
            int *value = va_arg(ap, int*);
            *value = st->bandwidth;
        }
        break;
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
        case OPUS_SET_DTX_FLAG_REQUEST:
        {
            int value = va_arg(ap, int);
            st->silk_mode.useDTX = value;
        }
        break;
        case OPUS_GET_DTX_FLAG_REQUEST:
        {
            int *value = va_arg(ap, int*);
            *value = st->silk_mode.useDTX;
        }
        break;
        case OPUS_SET_COMPLEXITY_REQUEST:
        {
            int value = va_arg(ap, int);
            st->silk_mode.complexity = value;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
576
            celt_encoder_ctl(st->celt_enc, CELT_SET_COMPLEXITY(value));
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
        }
        break;
        case OPUS_GET_COMPLEXITY_REQUEST:
        {
            int *value = va_arg(ap, int*);
            *value = st->silk_mode.complexity;
        }
        break;
        case OPUS_SET_INBAND_FEC_FLAG_REQUEST:
        {
            int value = va_arg(ap, int);
            st->silk_mode.useInBandFEC = value;
        }
        break;
        case OPUS_GET_INBAND_FEC_FLAG_REQUEST:
        {
            int *value = va_arg(ap, int*);
            *value = st->silk_mode.useInBandFEC;
        }
        break;
        case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
        {
            int value = va_arg(ap, int);
600
601
            if (value < 0 || value > 100)
                return OPUS_BAD_ARG;
602
            st->silk_mode.packetLossPercentage = value;
603
            celt_encoder_ctl(st->celt_enc, CELT_SET_LOSS_PERC(value));
604
605
606
607
608
609
610
611
612
        }
        break;
        case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
        {
            int *value = va_arg(ap, int*);
            *value = st->silk_mode.packetLossPercentage;
        }
        break;
        case OPUS_SET_VBR_FLAG_REQUEST:
613
614
        {
            int value = va_arg(ap, int);
615
616
            st->use_vbr = value;
            st->silk_mode.useCBR = 1-value;
617
618
        }
        break;
619
        case OPUS_GET_VBR_FLAG_REQUEST:
620
621
        {
            int *value = va_arg(ap, int*);
622
            *value = st->use_vbr;
623
624
        }
        break;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
625
626
627
        case OPUS_SET_VOICE_RATIO_REQUEST:
        {
            int value = va_arg(ap, int);
Jean-Marc Valin's avatar
Cleanup    
Jean-Marc Valin committed
628
            if (value>100 || value<0)
629
                goto bad_arg;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
630
631
632
633
634
635
636
637
638
            st->voice_ratio = value;
        }
        break;
        case OPUS_GET_VOICE_RATIO_REQUEST:
        {
            int *value = va_arg(ap, int*);
            *value = st->voice_ratio;
        }
        break;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
639
640
641
642
643
644
645
646
647
648
649
650
        case OPUS_SET_VBR_CONSTRAINT_REQUEST:
        {
            int value = va_arg(ap, int);
            st->vbr_constraint = value;
        }
        break;
        case OPUS_GET_VBR_CONSTRAINT_REQUEST:
        {
            int *value = va_arg(ap, int*);
            *value = st->vbr_constraint;
        }
        break;
651
        default:
Jean-Marc Valin's avatar
Jean-Marc Valin committed
652
            fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);
653
            break;
654
    }
Alfred E. Heggestad's avatar
Alfred E. Heggestad committed
655
    va_end(ap);
656
    return OPUS_OK;
657
658
659
bad_arg:
    va_end(ap);
    return OPUS_BAD_ARG;
660
661
}

Jean-Marc Valin's avatar
Jean-Marc Valin committed
662
void opus_encoder_destroy(OpusEncoder *st)
663
664
665
{
	free(st);
}
666

Koen Vos's avatar
Koen Vos committed
667
668
669
670
671
672
#if OPUS_TEST_RANGE_CODER_STATE
int opus_encoder_get_final_range(OpusEncoder *st)
{
    return st->rangeFinal;
}
#endif