2014-07-03 22:58:00 +08:00
|
|
|
/** codec.h
|
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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-06-21 07:58:55 +08:00
|
|
|
#ifndef _CODEC_H_
|
|
|
|
#define _CODEC_H_
|
2013-10-13 22:14:27 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
2014-02-16 03:44:33 +08:00
|
|
|
#include <pthread.h>
|
2014-02-01 19:52:48 +08:00
|
|
|
|
2014-02-16 03:44:33 +08:00
|
|
|
#include <vpx/vpx_decoder.h>
|
|
|
|
#include <vpx/vpx_encoder.h>
|
|
|
|
#include <vpx/vp8dx.h>
|
|
|
|
#include <vpx/vp8cx.h>
|
|
|
|
#define VIDEO_CODEC_DECODER_INTERFACE (vpx_codec_vp8_dx())
|
|
|
|
#define VIDEO_CODEC_ENCODER_INTERFACE (vpx_codec_vp8_cx())
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
/* Audio encoding/decoding */
|
2014-04-13 19:27:53 +08:00
|
|
|
#include <opus.h>
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
typedef enum _Capabilities {
|
2014-03-11 07:36:47 +08:00
|
|
|
none,
|
2014-03-07 10:13:04 +08:00
|
|
|
a_encoding = 1 << 0,
|
|
|
|
a_decoding = 1 << 1,
|
|
|
|
v_encoding = 1 << 2,
|
|
|
|
v_decoding = 1 << 3
|
2014-03-11 07:36:47 +08:00
|
|
|
} Capabilities;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-06-22 01:04:00 +08:00
|
|
|
extern const uint16_t min_jbuf_size;
|
2014-06-21 07:58:55 +08:00
|
|
|
|
2014-02-17 09:01:30 +08:00
|
|
|
typedef struct _CodecState {
|
2013-10-13 22:14:27 +08:00
|
|
|
|
|
|
|
/* video encoding */
|
2014-02-16 03:44:33 +08:00
|
|
|
vpx_codec_ctx_t v_encoder;
|
|
|
|
uint32_t frame_counter;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
|
|
|
/* video decoding */
|
2014-02-16 03:44:33 +08:00
|
|
|
vpx_codec_ctx_t v_decoder;
|
2014-07-21 07:10:57 +08:00
|
|
|
int bitrate;
|
|
|
|
int max_width;
|
|
|
|
int max_height;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
|
|
|
/* audio encoding */
|
2014-02-01 19:52:48 +08:00
|
|
|
OpusEncoder *audio_encoder;
|
|
|
|
int audio_bitrate;
|
2014-02-10 06:06:44 +08:00
|
|
|
int audio_sample_rate;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
|
|
|
/* audio decoding */
|
2014-02-01 19:52:48 +08:00
|
|
|
OpusDecoder *audio_decoder;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-03-11 07:36:47 +08:00
|
|
|
uint64_t capabilities; /* supports*/
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-06-21 07:58:55 +08:00
|
|
|
/* Voice activity detection */
|
2014-06-22 01:04:00 +08:00
|
|
|
uint32_t EVAD_tolerance; /* In frames */
|
|
|
|
uint32_t EVAD_tolerance_cr;
|
2014-02-10 06:06:44 +08:00
|
|
|
} CodecState;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
|
2014-05-11 00:00:49 +08:00
|
|
|
typedef struct _JitterBuffer {
|
2014-05-03 07:46:03 +08:00
|
|
|
RTPMessage **queue;
|
|
|
|
uint16_t capacity;
|
|
|
|
uint16_t size;
|
|
|
|
uint16_t front;
|
|
|
|
uint16_t rear;
|
|
|
|
uint8_t queue_ready;
|
|
|
|
uint16_t current_id;
|
|
|
|
uint32_t current_ts;
|
|
|
|
uint8_t id_set;
|
2014-05-11 00:00:49 +08:00
|
|
|
} JitterBuffer;
|
2014-05-03 07:46:03 +08:00
|
|
|
|
2014-05-11 00:00:49 +08:00
|
|
|
JitterBuffer *create_queue(int capacity);
|
|
|
|
void terminate_queue(JitterBuffer *q);
|
|
|
|
void queue(JitterBuffer *q, RTPMessage *pk);
|
|
|
|
RTPMessage *dequeue(JitterBuffer *q, int *success);
|
2014-02-01 19:52:48 +08:00
|
|
|
|
2013-10-13 22:14:27 +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,
|
|
|
|
uint32_t audio_channels,
|
2014-06-22 01:04:00 +08:00
|
|
|
uint32_t audio_VAD_tolerance_ms,
|
2014-02-16 03:44:33 +08:00
|
|
|
uint16_t video_width,
|
|
|
|
uint16_t video_height,
|
2014-06-21 07:58:55 +08:00
|
|
|
uint32_t video_bitrate);
|
2014-02-01 19:52:48 +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 05:41:02 +08:00
|
|
|
/* Reconfigure video encoder
|
|
|
|
return 0 on success.
|
|
|
|
return -1 on failure. */
|
|
|
|
int reconfigure_video_encoder_resolution(CodecState *cs, uint16_t width, uint16_t height);
|
|
|
|
int reconfigure_video_encoder_bitrate(CodecState *cs, uint32_t video_bitrate);
|
2014-06-21 07:58:55 +08:00
|
|
|
|
2014-06-22 01:04:00 +08:00
|
|
|
/* Calculate energy and return 1 if has voice, 0 if not */
|
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
|
|
|
|
2014-06-22 20:39:26 +08:00
|
|
|
#endif /* _CODEC_H_ */
|