OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 24 matching lines...) Loading... |
35 kZeroBreakCount = 157, | 35 kZeroBreakCount = 157, |
36 | 36 |
37 #if defined(OPUS_FIXED_POINT) | 37 #if defined(OPUS_FIXED_POINT) |
38 kZeroBreakValue = 10, | 38 kZeroBreakValue = 10, |
39 #else | 39 #else |
40 kZeroBreakValue = 1, | 40 kZeroBreakValue = 1, |
41 #endif | 41 #endif |
42 }; | 42 }; |
43 | 43 |
44 int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst, | 44 int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst, |
45 int32_t channels, | 45 size_t channels, |
46 int32_t application) { | 46 int32_t application) { |
47 int opus_app; | 47 int opus_app; |
48 if (!inst) | 48 if (!inst) |
49 return -1; | 49 return -1; |
50 | 50 |
51 switch (application) { | 51 switch (application) { |
52 case 0: | 52 case 0: |
53 opus_app = OPUS_APPLICATION_VOIP; | 53 opus_app = OPUS_APPLICATION_VOIP; |
54 break; | 54 break; |
55 case 1: | 55 case 1: |
56 opus_app = OPUS_APPLICATION_AUDIO; | 56 opus_app = OPUS_APPLICATION_AUDIO; |
57 break; | 57 break; |
58 default: | 58 default: |
59 return -1; | 59 return -1; |
60 } | 60 } |
61 | 61 |
62 OpusEncInst* state = calloc(1, sizeof(OpusEncInst)); | 62 OpusEncInst* state = calloc(1, sizeof(OpusEncInst)); |
63 assert(state); | 63 assert(state); |
64 | 64 |
65 // Allocate zero counters. | 65 // Allocate zero counters. |
66 state->zero_counts = calloc(channels, sizeof(size_t)); | 66 state->zero_counts = calloc(channels, sizeof(size_t)); |
67 assert(state->zero_counts); | 67 assert(state->zero_counts); |
68 | 68 |
69 int error; | 69 int error; |
70 state->encoder = opus_encoder_create(48000, channels, opus_app, | 70 state->encoder = opus_encoder_create(48000, (int)channels, opus_app, |
71 &error); | 71 &error); |
72 if (error != OPUS_OK || !state->encoder) { | 72 if (error != OPUS_OK || !state->encoder) { |
73 WebRtcOpus_EncoderFree(state); | 73 WebRtcOpus_EncoderFree(state); |
74 return -1; | 74 return -1; |
75 } | 75 } |
76 | 76 |
77 state->in_dtx_mode = 0; | 77 state->in_dtx_mode = 0; |
78 state->channels = channels; | 78 state->channels = channels; |
79 | 79 |
80 *inst = state; | 80 *inst = state; |
(...skipping 11 matching lines...) Loading... |
92 } | 92 } |
93 } | 93 } |
94 | 94 |
95 int WebRtcOpus_Encode(OpusEncInst* inst, | 95 int WebRtcOpus_Encode(OpusEncInst* inst, |
96 const int16_t* audio_in, | 96 const int16_t* audio_in, |
97 size_t samples, | 97 size_t samples, |
98 size_t length_encoded_buffer, | 98 size_t length_encoded_buffer, |
99 uint8_t* encoded) { | 99 uint8_t* encoded) { |
100 int res; | 100 int res; |
101 size_t i; | 101 size_t i; |
102 int c; | 102 size_t c; |
103 | 103 |
104 int16_t buffer[2 * 48 * kWebRtcOpusMaxEncodeFrameSizeMs]; | 104 int16_t buffer[2 * 48 * kWebRtcOpusMaxEncodeFrameSizeMs]; |
105 | 105 |
106 if (samples > 48 * kWebRtcOpusMaxEncodeFrameSizeMs) { | 106 if (samples > 48 * kWebRtcOpusMaxEncodeFrameSizeMs) { |
107 return -1; | 107 return -1; |
108 } | 108 } |
109 | 109 |
110 const int channels = inst->channels; | 110 const size_t channels = inst->channels; |
111 int use_buffer = 0; | 111 int use_buffer = 0; |
112 | 112 |
113 // Break long consecutive zeros by forcing a "1" every |kZeroBreakCount| | 113 // Break long consecutive zeros by forcing a "1" every |kZeroBreakCount| |
114 // samples. | 114 // samples. |
115 if (inst->in_dtx_mode) { | 115 if (inst->in_dtx_mode) { |
116 for (i = 0; i < samples; ++i) { | 116 for (i = 0; i < samples; ++i) { |
117 for (c = 0; c < channels; ++c) { | 117 for (c = 0; c < channels; ++c) { |
118 if (audio_in[i * channels + c] == 0) { | 118 if (audio_in[i * channels + c] == 0) { |
119 ++inst->zero_counts[c]; | 119 ++inst->zero_counts[c]; |
120 if (inst->zero_counts[c] == kZeroBreakCount) { | 120 if (inst->zero_counts[c] == kZeroBreakCount) { |
(...skipping 120 matching lines...) Loading... |
241 } | 241 } |
242 | 242 |
243 int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity) { | 243 int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity) { |
244 if (inst) { | 244 if (inst) { |
245 return opus_encoder_ctl(inst->encoder, OPUS_SET_COMPLEXITY(complexity)); | 245 return opus_encoder_ctl(inst->encoder, OPUS_SET_COMPLEXITY(complexity)); |
246 } else { | 246 } else { |
247 return -1; | 247 return -1; |
248 } | 248 } |
249 } | 249 } |
250 | 250 |
251 int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst, int channels) { | 251 int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst, size_t channels) { |
252 int error; | 252 int error; |
253 OpusDecInst* state; | 253 OpusDecInst* state; |
254 | 254 |
255 if (inst != NULL) { | 255 if (inst != NULL) { |
256 /* Create Opus decoder state. */ | 256 /* Create Opus decoder state. */ |
257 state = (OpusDecInst*) calloc(1, sizeof(OpusDecInst)); | 257 state = (OpusDecInst*) calloc(1, sizeof(OpusDecInst)); |
258 if (state == NULL) { | 258 if (state == NULL) { |
259 return -1; | 259 return -1; |
260 } | 260 } |
261 | 261 |
262 /* Create new memory, always at 48000 Hz. */ | 262 /* Create new memory, always at 48000 Hz. */ |
263 state->decoder = opus_decoder_create(48000, channels, &error); | 263 state->decoder = opus_decoder_create(48000, (int)channels, &error); |
264 if (error == OPUS_OK && state->decoder != NULL) { | 264 if (error == OPUS_OK && state->decoder != NULL) { |
265 /* Creation of memory all ok. */ | 265 /* Creation of memory all ok. */ |
266 state->channels = channels; | 266 state->channels = channels; |
267 state->prev_decoded_samples = kWebRtcOpusDefaultFrameSize; | 267 state->prev_decoded_samples = kWebRtcOpusDefaultFrameSize; |
268 state->in_dtx_mode = 0; | 268 state->in_dtx_mode = 0; |
269 *inst = state; | 269 *inst = state; |
270 return 0; | 270 return 0; |
271 } | 271 } |
272 | 272 |
273 /* If memory allocation was unsuccessful, free the entire state. */ | 273 /* If memory allocation was unsuccessful, free the entire state. */ |
274 if (state->decoder) { | 274 if (state->decoder) { |
275 opus_decoder_destroy(state->decoder); | 275 opus_decoder_destroy(state->decoder); |
276 } | 276 } |
277 free(state); | 277 free(state); |
278 } | 278 } |
279 return -1; | 279 return -1; |
280 } | 280 } |
281 | 281 |
282 int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst) { | 282 int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst) { |
283 if (inst) { | 283 if (inst) { |
284 opus_decoder_destroy(inst->decoder); | 284 opus_decoder_destroy(inst->decoder); |
285 free(inst); | 285 free(inst); |
286 return 0; | 286 return 0; |
287 } else { | 287 } else { |
288 return -1; | 288 return -1; |
289 } | 289 } |
290 } | 290 } |
291 | 291 |
292 int WebRtcOpus_DecoderChannels(OpusDecInst* inst) { | 292 size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst) { |
293 return inst->channels; | 293 return inst->channels; |
294 } | 294 } |
295 | 295 |
296 void WebRtcOpus_DecoderInit(OpusDecInst* inst) { | 296 void WebRtcOpus_DecoderInit(OpusDecInst* inst) { |
297 opus_decoder_ctl(inst->decoder, OPUS_RESET_STATE); | 297 opus_decoder_ctl(inst->decoder, OPUS_RESET_STATE); |
298 inst->in_dtx_mode = 0; | 298 inst->in_dtx_mode = 0; |
299 } | 299 } |
300 | 300 |
301 /* For decoder to determine if it is to output speech or comfort noise. */ | 301 /* For decoder to determine if it is to output speech or comfort noise. */ |
302 static int16_t DetermineAudioType(OpusDecInst* inst, size_t encoded_bytes) { | 302 static int16_t DetermineAudioType(OpusDecInst* inst, size_t encoded_bytes) { |
(...skipping 192 matching lines...) Loading... |
495 return 0; | 495 return 0; |
496 } | 496 } |
497 | 497 |
498 for (n = 0; n < channels; n++) { | 498 for (n = 0; n < channels; n++) { |
499 if (frame_data[0][0] & (0x80 >> ((n + 1) * (frames + 1) - 1))) | 499 if (frame_data[0][0] & (0x80 >> ((n + 1) * (frames + 1) - 1))) |
500 return 1; | 500 return 1; |
501 } | 501 } |
502 | 502 |
503 return 0; | 503 return 0; |
504 } | 504 } |
OLD | NEW |