toxcore/toxav/codec.h

176 lines
4.5 KiB
C
Raw Normal View History

/** 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
*
2014-02-16 05:36:15 +08:00
* Copyright (C) 2013 Tox project All Rights Reserved.
*
2014-02-16 05:36:15 +08:00
* This file is part of Tox.
*
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.
*
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.
*
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/>.
*
*/
2014-06-21 07:58:55 +08:00
#ifndef _CODEC_H_
#define _CODEC_H_
2014-11-18 07:46:46 +08:00
#include "toxav.h"
#include "rtp.h"
#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>
2014-11-18 07:46:46 +08:00
#include <vpx/vpx_image.h>
2014-02-16 03:44:33 +08:00
#define VIDEO_CODEC_DECODER_INTERFACE (vpx_codec_vp8_dx())
#define VIDEO_CODEC_ENCODER_INTERFACE (vpx_codec_vp8_cx())
2014-02-01 19:52:48 +08:00
/* Audio encoding/decoding */
#include <opus.h>
2014-11-29 20:42:19 +08:00
#define PAIR(TYPE1__, TYPE2__) struct { TYPE1__ first; TYPE2__ second; }
typedef void (*CSAudioCallback) (void *agent, int32_t call_idx, const int16_t *PCM, uint16_t size, void *data);
typedef void (*CSVideoCallback) (void *agent, int32_t call_idx, const vpx_image_t *img, void *data);
2014-11-18 07:46:46 +08:00
2014-11-29 20:42:19 +08:00
/**
* Codec capabilities
*/
typedef enum {
cs_AudioEncoding = 1 << 0,
cs_AudioDecoding = 1 << 1,
cs_VideoEncoding = 1 << 2,
cs_VideoDecoding = 1 << 3
} CSCapabilities;
/**
* Codec errors.
*/
typedef enum {
cs_ErrorSettingVideoResolution = -30,
cs_ErrorSettingVideoBitrate = -31,
cs_ErrorSplittingVideoPayload = -32,
} CSError;
2014-11-29 20:42:19 +08:00
/**
* Codec session - controling codec
*/
2014-11-18 07:46:46 +08:00
typedef struct _CSSession {
2014-06-21 07:58:55 +08:00
/* VIDEO
*
*
*/
2014-11-18 07:46:46 +08:00
int support_video;
/* video encoding */
2014-02-16 03:44:33 +08:00
vpx_codec_ctx_t v_encoder;
uint32_t frame_counter;
/* video decoding */
2014-02-16 03:44:33 +08:00
vpx_codec_ctx_t v_decoder;
int max_width;
int max_height;
/* Data handling */
2014-11-18 07:46:46 +08:00
uint8_t *frame_buf; /* buffer for split video payloads */
uint32_t frame_size; /* largest address written to in frame_buf for current input frame*/
uint8_t frameid_in, frameid_out; /* id of input and output video frame */
uint32_t last_timestamp; /* calculating cycles */
2014-11-18 07:46:46 +08:00
/* Limits */
uint32_t video_frame_piece_size;
uint32_t max_video_frame_size;
2014-11-18 07:46:46 +08:00
/* Reassembling */
uint8_t *split_video_frame;
const uint8_t *processing_video_frame;
uint16_t processing_video_frame_size;
/* AUDIO
*
*
*/
2014-11-18 07:46:46 +08:00
/* audio encoding */
2014-02-01 19:52:48 +08:00
OpusEncoder *audio_encoder;
2014-11-18 07:46:46 +08:00
int audio_encoder_bitrate;
int audio_encoder_sample_rate;
int audio_encoder_frame_duration;
int audio_encoder_channels;
/* audio decoding */
2014-02-01 19:52:48 +08:00
OpusDecoder *audio_decoder;
2014-11-18 07:46:46 +08:00
int audio_decoder_bitrate;
int audio_decoder_sample_rate;
int audio_decoder_frame_duration;
int audio_decoder_channels;
struct _JitterBuffer *j_buf;
2014-06-21 07:58:55 +08:00
/* Voice activity detection */
uint32_t EVAD_tolerance; /* In frames */
uint32_t EVAD_tolerance_cr;
2014-02-01 19:52:48 +08:00
2014-05-03 07:46:03 +08:00
/* OTHER
*
*
*/
2014-11-18 07:46:46 +08:00
uint64_t capabilities; /* supports*/
2014-02-01 19:52:48 +08:00
2014-11-29 20:42:19 +08:00
/* Callbacks */
PAIR(CSAudioCallback, void *) acb;
PAIR(CSVideoCallback, void *) vcb;
2014-11-18 07:46:46 +08:00
/* Buffering */
void *abuf_raw, *vbuf_raw; /* Un-decoded data */
2014-11-18 07:46:46 +08:00
pthread_mutex_t queue_mutex[1];
void *agent; /* Pointer to ToxAv */
2014-11-18 07:46:46 +08:00
int32_t call_idx;
} CSSession;
2014-02-01 19:52:48 +08:00
2014-11-29 20:42:19 +08:00
/* Make sure to be called BEFORE corresponding rtp_new */
CSSession *cs_new(const ToxAvCSettings *cs_self, const ToxAvCSettings *cs_peer, uint32_t jbuf_size, int has_video);
2014-11-29 20:42:19 +08:00
/* Make sure to be called AFTER corresponding rtp_kill */
2014-11-18 07:46:46 +08:00
void cs_kill(CSSession *cs);
int cs_split_video_payload(CSSession *cs, const uint8_t *payload, uint16_t length);
const uint8_t *cs_get_split_video_frame(CSSession *cs, uint16_t *size);
2014-11-18 07:46:46 +08:00
/**
* Call playback callbacks
*/
void cs_do(CSSession *cs);
2014-02-01 19:52:48 +08:00
2014-11-18 07:46:46 +08:00
/* Reconfigure video encoder; return 0 on success or -1 on failure. */
int cs_set_video_encoder_resolution(CSSession *cs, uint16_t width, uint16_t height);
int cs_set_video_encoder_bitrate(CSSession *cs, uint32_t video_bitrate);
2014-06-21 07:58:55 +08:00
2014-11-18 07:46:46 +08:00
/* Internal. Called from rtp_handle_message */
void queue_message(RTPSession *session, RTPMessage *msg);
#endif /* _CODEC_H_ */