opus_encoder.c 8.08 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
33
34
35
36
/*
   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.

   - Neither the name of the Xiph.org Foundation nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

   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>
37
#include <stdio.h>
38
#include <stdarg.h>
Jean-Marc Valin's avatar
Jean-Marc Valin committed
39
#include "opus_encoder.h"
Jean-Marc Valin's avatar
Jean-Marc Valin committed
40
41
#include "entenc.h"
#include "modes.h"
42
#include "SKP_Silk_SDK_API.h"
43

Jean-Marc Valin's avatar
Jean-Marc Valin committed
44
OpusEncoder *opus_encoder_create(int Fs)
45
{
46
    char *raw_state;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
47
	OpusEncoder *st;
48
	int ret, silkEncSizeBytes, celtEncSizeBytes;
49
    SKP_SILK_SDK_EncControlStruct encControl;
50

51
    /* Create SILK encoder */
52
    ret = SKP_Silk_SDK_Get_Encoder_Size( &silkEncSizeBytes );
53
54
55
    if( ret ) {
    	/* Handle error */
    }
Jean-Marc Valin's avatar
Jean-Marc Valin committed
56
    celtEncSizeBytes = celt_encoder_get_size(1);
Jean-Marc Valin's avatar
Jean-Marc Valin committed
57
58
59
60
    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);
61
62

    st->Fs = Fs;
63

64
65
66
67
68
69
    /*encControl.API_sampleRate        = st->Fs;
    encControl.packetLossPercentage  = 0;
    encControl.useInBandFEC          = 0;
    encControl.useDTX                = 0;
    encControl.complexity            = 2;*/
    ret = SKP_Silk_SDK_InitEncoder( st->silk_enc, &encControl );
70
71
72
    if( ret ) {
        /* Handle error */
    }
73

74
    /* Create CELT encoder */
75
	/* Initialize CELT encoder */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
76
	st->celt_enc = celt_encoder_init(st->celt_enc, Fs, 1, NULL);
77

78
79
	st->mode = MODE_HYBRID;
	st->bandwidth = BANDWIDTH_FULLBAND;
80
	st->vbr_rate = 0;
81

82
83
84
	return st;
}

Jean-Marc Valin's avatar
Jean-Marc Valin committed
85
int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
86
87
		unsigned char *data, int bytes_per_packet)
{
88
    int i;
89
	int ret=0;
90
	SKP_int32 nBytes;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
91
	ec_enc enc;
92
	ec_byte_buffer buf;
93
	SKP_SILK_SDK_EncControlStruct encControl;
94
	int framerate, period;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
95

96
97
	bytes_per_packet -= 1;
	data += 1;
98
99
	ec_byte_writeinit_buffer(&buf, data, bytes_per_packet);
	ec_enc_init(&enc,&buf);
Jean-Marc Valin's avatar
Jean-Marc Valin committed
100

101
102
	if (st->mode != MODE_CELT_ONLY)
	{
103
104
	    /* Set Encoder parameters */
	    encControl.API_sampleRate        = st->Fs;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
105
	    encControl.packetLossPercentage  = 2;
106
107
108
109
	    encControl.useInBandFEC          = 0;
	    encControl.useDTX                = 0;
	    encControl.complexity            = 2;

Jean-Marc Valin's avatar
Jean-Marc Valin committed
110
111
112
113
114
115
116
	    if (st->vbr_rate != 0)
            encControl.bitRate = (st->vbr_rate+6000)/2;
	    else {
	        encControl.bitRate = (bytes_per_packet*8*(celt_int32)st->Fs/frame_size+6000)/2;
	        if (st->Fs  == 100 * frame_size)
	            encControl.bitRate -= 5000;
	    }
Jean-Marc Valin's avatar
Jean-Marc Valin committed
117
	    encControl.payloadSize_ms = 1000 * frame_size / st->Fs;
118

Jean-Marc Valin's avatar
Jean-Marc Valin committed
119
120
121
122
	    if (st->mode == MODE_HYBRID)
	    	encControl.minInternalSampleRate = 16000;
	    else
	    	encControl.minInternalSampleRate = 8000;
123
124

	    if (st->bandwidth == BANDWIDTH_NARROWBAND)
125
	        encControl.maxInternalSampleRate = 8000;
126
	    else if (st->bandwidth == BANDWIDTH_MEDIUMBAND)
Jean-Marc Valin's avatar
Jean-Marc Valin committed
127
	    	encControl.maxInternalSampleRate = 12000;
128
	    else
Jean-Marc Valin's avatar
Jean-Marc Valin committed
129
	    	encControl.maxInternalSampleRate = 16000;
130

131
132
	    /* Call SILK encoder for the low band */
	    nBytes = bytes_per_packet;
133
	    ret = SKP_Silk_SDK_Encode( st->silk_enc, &encControl, pcm, frame_size, &enc, &nBytes );
134
	    if( ret ) {
135
	        fprintf (stderr, "SILK encode error %d\n", ret);
136
137
138
139
140
141
142
143
	        /* Handle error */
	    }
	    ret = (ec_enc_tell(&enc, 0)+7)>>3;
	}

	if (st->mode == MODE_HYBRID)
	{
	    /* This should be adjusted based on the SILK bandwidth */
144
	    celt_encoder_ctl(st->celt_enc, CELT_SET_START_BAND(17));
145
146
147
148
	} else {
        celt_encoder_ctl(st->celt_enc, CELT_SET_START_BAND(0));
	}

149
	if (st->mode != MODE_SILK_ONLY && st->bandwidth > BANDWIDTH_WIDEBAND)
150
	{
Jean-Marc Valin's avatar
Jean-Marc Valin committed
151
		int endband;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
152
	    short pcm_buf[960];
153

Jean-Marc Valin's avatar
Jean-Marc Valin committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
	    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));
170

171
	    for (i=0;i<ENCODER_DELAY_COMPENSATION;i++)
Jean-Marc Valin's avatar
Jean-Marc Valin committed
172
	        pcm_buf[i] = st->delay_buffer[i];
Jean-Marc Valin's avatar
Jean-Marc Valin committed
173
        for (;i<frame_size;i++)
Jean-Marc Valin's avatar
Jean-Marc Valin committed
174
            pcm_buf[i] = pcm[i-ENCODER_DELAY_COMPENSATION];
175
176

        celt_encoder_ctl(st->celt_enc, CELT_SET_PREDICTION(1));
Jean-Marc Valin's avatar
Jean-Marc Valin committed
177
178
179
180
181
182
183
184
185

        if (st->vbr_rate != 0)
        {
            int tmp = (st->vbr_rate-6000)/2;
            tmp = ((ec_enc_tell(&enc, 0)+4)>>3) + tmp * frame_size/(8*st->Fs);
            if (tmp <= bytes_per_packet)
                bytes_per_packet = tmp;
            ec_byte_shrink(&buf, bytes_per_packet);
        }
186
	    /* Encode high band with CELT */
187
	    ret = celt_encode_with_ec(st->celt_enc, pcm_buf, frame_size, NULL, bytes_per_packet, &enc);
188
	    for (i=0;i<ENCODER_DELAY_COMPENSATION;i++)
Jean-Marc Valin's avatar
Jean-Marc Valin committed
189
	        st->delay_buffer[i] = pcm[frame_size-ENCODER_DELAY_COMPENSATION+i];
190
191
192
193
	} else {
	    ec_enc_done(&enc);
	}

194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
	/* 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)
    {
        data[0] = (st->bandwidth-BANDWIDTH_NARROWBAND)<<5;
        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;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
215
    } else /* Opus */
216
217
218
219
220
221
222
223
    {
        data[0] = 0x60;
        data[0] |= (st->bandwidth-BANDWIDTH_SUPERWIDEBAND)<<4;
        data[0] |= (period-2)<<3;
    }
    /*printf ("%x\n", (int)data[0]);*/

    return ret+1;
224
}
225

Jean-Marc Valin's avatar
Jean-Marc Valin committed
226
void opus_encoder_ctl(OpusEncoder *st, int request, ...)
227
228
229
230
231
232
233
{
    va_list ap;

    va_start(ap, request);

    switch (request)
    {
Jean-Marc Valin's avatar
Jean-Marc Valin committed
234
        case OPUS_SET_MODE_REQUEST:
235
236
237
238
239
        {
            int value = va_arg(ap, int);
            st->mode = value;
        }
        break;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
240
        case OPUS_GET_MODE_REQUEST:
241
242
243
244
245
        {
            int *value = va_arg(ap, int*);
            *value = st->mode;
        }
        break;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
246
        case OPUS_SET_BANDWIDTH_REQUEST:
247
248
249
250
251
        {
            int value = va_arg(ap, int);
            st->bandwidth = value;
        }
        break;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
252
        case OPUS_GET_BANDWIDTH_REQUEST:
253
254
255
256
257
        {
            int *value = va_arg(ap, int*);
            *value = st->bandwidth;
        }
        break;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
258
        case OPUS_SET_VBR_RATE_REQUEST:
259
260
261
262
263
        {
            int value = va_arg(ap, int);
            st->vbr_rate = value;
        }
        break;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
264
        case OPUS_GET_VBR_RATE_REQUEST:
265
266
267
268
269
270
        {
            int *value = va_arg(ap, int*);
            *value = st->vbr_rate;
        }
        break;
        default:
Jean-Marc Valin's avatar
Jean-Marc Valin committed
271
            fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);
272
            break;
273
    }
Alfred E. Heggestad's avatar
Alfred E. Heggestad committed
274
275

    va_end(ap);
276
277
}

Jean-Marc Valin's avatar
Jean-Marc Valin committed
278
void opus_encoder_destroy(OpusEncoder *st)
279
280
281
{
	free(st);
}
282