2014-07-03 22:58:00 +08:00
|
|
|
/** codec.c
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-02-16 05:36:15 +08:00
|
|
|
* Audio and video codec intitialization, encoding/decoding and playback
|
2013-10-13 22:14:27 +08:00
|
|
|
*
|
2014-02-16 05:36:15 +08:00
|
|
|
* Copyright (C) 2013 Tox project All Rights Reserved.
|
2013-10-13 22:14:27 +08:00
|
|
|
*
|
2014-02-16 05:36:15 +08:00
|
|
|
* This file is part of Tox.
|
2013-10-13 22:14:27 +08:00
|
|
|
*
|
2014-02-16 05:36:15 +08:00
|
|
|
* Tox is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2013-10-13 22:14:27 +08:00
|
|
|
*
|
2014-02-16 05:36:15 +08:00
|
|
|
* Tox is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2013-10-13 22:14:27 +08:00
|
|
|
*
|
2014-02-16 05:36:15 +08:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
2013-10-13 22:14:27 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
#include "../toxcore/logger.h"
|
|
|
|
|
2013-10-13 22:14:27 +08:00
|
|
|
#include <stdio.h>
|
2014-02-16 03:44:33 +08:00
|
|
|
#include <stdlib.h>
|
2013-10-13 22:14:27 +08:00
|
|
|
#include <math.h>
|
2014-02-01 19:52:48 +08:00
|
|
|
#include <assert.h>
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
#include "rtp.h"
|
2014-06-21 07:58:55 +08:00
|
|
|
#include "codec.h"
|
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
JitterBuffer *create_queue(unsigned int capacity)
|
2014-03-07 10:13:04 +08:00
|
|
|
{
|
2014-07-25 07:45:38 +08:00
|
|
|
unsigned int size = 1;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-28 07:52:43 +08:00
|
|
|
while (size <= capacity) {
|
2014-07-25 07:45:38 +08:00
|
|
|
size *= 2;
|
2014-03-07 10:13:04 +08:00
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-11 00:00:49 +08:00
|
|
|
JitterBuffer *q;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-11 00:00:49 +08:00
|
|
|
if ( !(q = calloc(sizeof(JitterBuffer), 1)) ) return NULL;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
if (!(q->queue = calloc(sizeof(RTPMessage *), size))) {
|
2014-05-31 06:29:43 +08:00
|
|
|
free(q);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
q->size = size;
|
|
|
|
q->capacity = capacity;
|
2013-10-13 22:14:27 +08:00
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2014-07-25 08:56:07 +08:00
|
|
|
static void clear_queue(JitterBuffer *q)
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-07-25 07:45:38 +08:00
|
|
|
for (; q->bottom != q->top; ++q->bottom) {
|
2014-07-25 09:00:42 +08:00
|
|
|
if (q->queue[q->bottom % q->size]) {
|
2014-07-25 07:45:38 +08:00
|
|
|
rtp_free_msg(NULL, q->queue[q->bottom % q->size]);
|
2014-07-25 09:00:42 +08:00
|
|
|
q->queue[q->bottom % q->size] = NULL;
|
|
|
|
}
|
2014-07-25 07:45:38 +08:00
|
|
|
}
|
2014-07-25 08:56:07 +08:00
|
|
|
}
|
2014-07-06 00:25:30 +08:00
|
|
|
|
2014-07-25 08:56:07 +08:00
|
|
|
void terminate_queue(JitterBuffer *q)
|
|
|
|
{
|
|
|
|
if (!q) return;
|
|
|
|
|
|
|
|
clear_queue(q);
|
2014-07-25 07:45:38 +08:00
|
|
|
free(q->queue);
|
2014-05-11 00:00:49 +08:00
|
|
|
free(q);
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
void queue(JitterBuffer *q, RTPMessage *pk)
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-07-25 07:45:38 +08:00
|
|
|
uint16_t sequnum = pk->header->sequnum;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
unsigned int num = sequnum % q->size;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-07-25 08:56:07 +08:00
|
|
|
if (sequnum - q->bottom > q->size) {
|
|
|
|
clear_queue(q);
|
|
|
|
q->bottom = sequnum;
|
|
|
|
q->queue[num] = pk;
|
|
|
|
q->top = sequnum + 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
if (q->queue[num])
|
|
|
|
return;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
q->queue[num] = pk;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
if ((sequnum - q->bottom) >= (q->top - q->bottom))
|
|
|
|
q->top = sequnum + 1;
|
|
|
|
}
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
/* success is 0 when there is nothing to dequeue, 1 when there's a good packet, 2 when there's a lost packet */
|
|
|
|
RTPMessage *dequeue(JitterBuffer *q, int *success)
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-07-25 07:45:38 +08:00
|
|
|
if (q->top == q->bottom) {
|
|
|
|
*success = 0;
|
|
|
|
return NULL;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
unsigned int num = q->bottom % q->size;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
if (q->queue[num]) {
|
|
|
|
RTPMessage *ret = q->queue[num];
|
|
|
|
q->queue[num] = NULL;
|
|
|
|
++q->bottom;
|
|
|
|
*success = 1;
|
|
|
|
return ret;
|
|
|
|
}
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
if (q->top - q->bottom > q->capacity) {
|
|
|
|
++q->bottom;
|
|
|
|
*success = 2;
|
|
|
|
return NULL;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
2014-07-25 07:45:38 +08:00
|
|
|
|
|
|
|
*success = 0;
|
|
|
|
return NULL;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
int init_video_decoder(CodecState *cs)
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-05-03 07:46:03 +08:00
|
|
|
int rc = vpx_codec_dec_init_ver(&cs->v_decoder, VIDEO_CODEC_DECODER_INTERFACE, NULL, 0, VPX_DECODER_ABI_VERSION);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( rc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR("Init video_decoder failed: %s", vpx_codec_err_to_string(rc));
|
2014-02-10 06:06:44 +08:00
|
|
|
return -1;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
2014-02-16 03:44:33 +08:00
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
int init_audio_decoder(CodecState *cs, uint32_t audio_channels)
|
|
|
|
{
|
|
|
|
int rc;
|
2014-02-17 09:01:30 +08:00
|
|
|
cs->audio_decoder = opus_decoder_create(cs->audio_sample_rate, audio_channels, &rc );
|
|
|
|
|
|
|
|
if ( rc != OPUS_OK ) {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_ERROR("Error while starting audio decoder: %s", opus_strerror(rc));
|
2014-02-10 06:06:44 +08:00
|
|
|
return -1;
|
2014-02-17 09:01:30 +08:00
|
|
|
}
|
|
|
|
|
2014-07-28 00:51:20 +08:00
|
|
|
cs->audio_decoder_channels = audio_channels;
|
2014-02-10 06:06:44 +08:00
|
|
|
return 0;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-07-05 05:41:02 +08:00
|
|
|
int reconfigure_video_encoder_resolution(CodecState *cs, uint16_t width, uint16_t height)
|
|
|
|
{
|
|
|
|
vpx_codec_enc_cfg_t cfg = *cs->v_encoder.config.enc;
|
|
|
|
|
|
|
|
if (cfg.g_w == width && cfg.g_h == height)
|
|
|
|
return 0;
|
|
|
|
|
2014-07-06 02:36:19 +08:00
|
|
|
if (width * height > cs->max_width * cs->max_height)
|
|
|
|
return -1;
|
|
|
|
|
2014-07-05 05:41:02 +08:00
|
|
|
LOGGER_DEBUG("New video resolution: %u %u", width, height);
|
|
|
|
cfg.g_w = width;
|
|
|
|
cfg.g_h = height;
|
|
|
|
int rc = vpx_codec_enc_config_set(&cs->v_encoder, &cfg);
|
|
|
|
|
|
|
|
if ( rc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int reconfigure_video_encoder_bitrate(CodecState *cs, uint32_t video_bitrate)
|
|
|
|
{
|
|
|
|
vpx_codec_enc_cfg_t cfg = *cs->v_encoder.config.enc;
|
|
|
|
|
|
|
|
if (cfg.rc_target_bitrate == video_bitrate)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LOGGER_DEBUG("New video bitrate: %u", video_bitrate);
|
|
|
|
cfg.rc_target_bitrate = video_bitrate;
|
|
|
|
int rc = vpx_codec_enc_config_set(&cs->v_encoder, &cfg);
|
|
|
|
|
|
|
|
if ( rc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-10 06:06:44 +08:00
|
|
|
|
2014-07-06 02:36:19 +08:00
|
|
|
int init_video_encoder(CodecState *cs, uint16_t max_width, uint16_t max_height, uint32_t video_bitrate)
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-02-16 03:44:33 +08:00
|
|
|
vpx_codec_enc_cfg_t cfg;
|
2014-05-03 07:46:03 +08:00
|
|
|
int rc = vpx_codec_enc_config_default(VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
if (rc != VPX_CODEC_OK) {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_ERROR("Failed to get config: %s", vpx_codec_err_to_string(rc));
|
2014-02-10 06:06:44 +08:00
|
|
|
return -1;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-16 03:44:33 +08:00
|
|
|
cfg.rc_target_bitrate = video_bitrate;
|
2014-07-06 02:36:19 +08:00
|
|
|
cfg.g_w = max_width;
|
|
|
|
cfg.g_h = max_height;
|
2014-06-25 06:07:31 +08:00
|
|
|
cfg.g_pass = VPX_RC_ONE_PASS;
|
|
|
|
cfg.g_error_resilient = VPX_ERROR_RESILIENT_DEFAULT | VPX_ERROR_RESILIENT_PARTITIONS;
|
|
|
|
cfg.g_lag_in_frames = 0;
|
|
|
|
cfg.kf_min_dist = 0;
|
|
|
|
cfg.kf_max_dist = 300;
|
2014-07-06 02:36:19 +08:00
|
|
|
cfg.kf_mode = VPX_KF_AUTO;
|
|
|
|
|
|
|
|
cs->max_width = max_width;
|
|
|
|
cs->max_height = max_height;
|
|
|
|
cs->bitrate = video_bitrate;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
rc = vpx_codec_enc_init_ver(&cs->v_encoder, VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0, VPX_ENCODER_ABI_VERSION);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( rc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR("Failed to initialize encoder: %s", vpx_codec_err_to_string(rc));
|
2014-02-10 06:06:44 +08:00
|
|
|
return -1;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-06-25 06:07:31 +08:00
|
|
|
rc = vpx_codec_control(&cs->v_encoder, VP8E_SET_CPUUSED, 7);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
if ( rc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
|
|
|
|
return -1;
|
|
|
|
}
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
return 0;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-02-16 03:44:33 +08:00
|
|
|
int init_audio_encoder(CodecState *cs, uint32_t audio_channels)
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-05-03 07:46:03 +08:00
|
|
|
int rc = OPUS_OK;
|
|
|
|
cs->audio_encoder = opus_encoder_create(cs->audio_sample_rate, audio_channels, OPUS_APPLICATION_AUDIO, &rc);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( rc != OPUS_OK ) {
|
|
|
|
LOGGER_ERROR("Error while starting audio encoder: %s", opus_strerror(rc));
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_BITRATE(cs->audio_bitrate));
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( rc != OPUS_OK ) {
|
|
|
|
LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(rc));
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_COMPLEXITY(10));
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( rc != OPUS_OK ) {
|
|
|
|
LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(rc));
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-28 00:51:20 +08:00
|
|
|
cs->audio_encoder_channels = audio_channels;
|
2014-05-03 07:46:03 +08:00
|
|
|
return 0;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
|
2014-02-17 09:01:30 +08:00
|
|
|
CodecState *codec_init_session ( uint32_t audio_bitrate,
|
|
|
|
uint16_t audio_frame_duration,
|
|
|
|
uint32_t audio_sample_rate,
|
2014-07-28 00:51:20 +08:00
|
|
|
uint32_t encoder_audio_channels,
|
|
|
|
uint32_t decoder_audio_channels,
|
2014-06-22 01:04:00 +08:00
|
|
|
uint32_t audio_VAD_tolerance_ms,
|
2014-07-06 02:36:19 +08:00
|
|
|
uint16_t max_video_width,
|
|
|
|
uint16_t max_video_height,
|
2014-02-16 03:44:33 +08:00
|
|
|
uint32_t video_bitrate )
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-03-07 10:13:04 +08:00
|
|
|
CodecState *retu = calloc(sizeof(CodecState), 1);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
|
|
|
if (!retu) return NULL;
|
2014-02-16 03:44:33 +08:00
|
|
|
|
2014-03-07 10:13:04 +08:00
|
|
|
retu->audio_bitrate = audio_bitrate;
|
|
|
|
retu->audio_sample_rate = audio_sample_rate;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
/* Encoders */
|
2014-07-06 02:36:19 +08:00
|
|
|
if (!max_video_width || !max_video_height) { /* Disable video */
|
2014-03-07 10:13:04 +08:00
|
|
|
/*video_width = 320;
|
|
|
|
video_height = 240; */
|
2014-05-26 00:27:48 +08:00
|
|
|
} else {
|
2014-07-06 05:31:06 +08:00
|
|
|
retu->capabilities |= ( 0 == init_video_encoder(retu, max_video_width, max_video_height,
|
|
|
|
video_bitrate) ) ? v_encoding : 0;
|
2014-03-11 07:36:47 +08:00
|
|
|
retu->capabilities |= ( 0 == init_video_decoder(retu) ) ? v_decoding : 0;
|
2014-02-16 03:44:33 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-28 00:51:20 +08:00
|
|
|
retu->capabilities |= ( 0 == init_audio_encoder(retu, encoder_audio_channels) ) ? a_encoding : 0;
|
|
|
|
retu->capabilities |= ( 0 == init_audio_decoder(retu, decoder_audio_channels) ) ? a_decoding : 0;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-11 00:00:49 +08:00
|
|
|
if ( retu->capabilities == 0 ) { /* everything failed */
|
|
|
|
free (retu);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-06-22 20:39:26 +08:00
|
|
|
|
|
|
|
retu->EVAD_tolerance = audio_VAD_tolerance_ms > audio_frame_duration ?
|
|
|
|
audio_VAD_tolerance_ms / audio_frame_duration : audio_frame_duration;
|
|
|
|
|
2014-03-07 10:13:04 +08:00
|
|
|
return retu;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-02-17 09:01:30 +08:00
|
|
|
void codec_terminate_session ( CodecState *cs )
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-07-05 21:11:25 +08:00
|
|
|
if (!cs) return;
|
2014-07-06 00:25:30 +08:00
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
if ( cs->audio_encoder )
|
2014-02-10 06:06:44 +08:00
|
|
|
opus_encoder_destroy(cs->audio_encoder);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
|
|
|
if ( cs->audio_decoder )
|
2014-02-10 06:06:44 +08:00
|
|
|
opus_decoder_destroy(cs->audio_decoder);
|
2014-02-16 03:44:33 +08:00
|
|
|
|
2014-03-11 07:36:47 +08:00
|
|
|
if ( cs->capabilities & v_decoding )
|
2014-03-07 10:13:04 +08:00
|
|
|
vpx_codec_destroy(&cs->v_decoder);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-03-11 07:36:47 +08:00
|
|
|
if ( cs->capabilities & v_encoding )
|
2014-03-07 10:13:04 +08:00
|
|
|
vpx_codec_destroy(&cs->v_encoder);
|
2014-07-06 00:25:30 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
LOGGER_DEBUG("Terminated codec state: %p", cs);
|
|
|
|
free(cs);
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2014-06-21 07:58:55 +08:00
|
|
|
|
2014-07-20 10:29:56 +08:00
|
|
|
static float calculate_sum_sq (int16_t *n, uint16_t k)
|
2014-06-21 07:58:55 +08:00
|
|
|
{
|
|
|
|
float result = 0;
|
2014-06-22 01:04:00 +08:00
|
|
|
uint16_t i = 0;
|
2014-06-22 20:39:26 +08:00
|
|
|
|
2014-06-22 01:04:00 +08:00
|
|
|
for ( ; i < k; i ++) result += (float) (n[i] * n[i]);
|
2014-06-22 20:39:26 +08:00
|
|
|
|
2014-06-21 07:58:55 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-06-22 20:39:26 +08:00
|
|
|
int energy_VAD(CodecState *cs, int16_t *PCM, uint16_t frame_size, float energy)
|
2014-06-21 07:58:55 +08:00
|
|
|
{
|
|
|
|
float frame_energy = sqrt(calculate_sum_sq(PCM, frame_size)) / frame_size;
|
2014-06-22 20:39:26 +08:00
|
|
|
|
|
|
|
if ( frame_energy > energy) {
|
2014-06-22 01:04:00 +08:00
|
|
|
cs->EVAD_tolerance_cr = cs->EVAD_tolerance; /* Reset counter */
|
|
|
|
return 1;
|
|
|
|
}
|
2014-06-22 20:39:26 +08:00
|
|
|
|
|
|
|
if ( cs->EVAD_tolerance_cr ) {
|
|
|
|
cs->EVAD_tolerance_cr --;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-21 07:58:55 +08:00
|
|
|
return 0;
|
2014-06-22 08:41:32 +08:00
|
|
|
}
|