diff options
author | mannol <eniz_vukovic@hotmail.com> | 2014-11-18 00:46:46 +0100 |
---|---|---|
committer | mannol <eniz_vukovic@hotmail.com> | 2014-11-18 00:46:46 +0100 |
commit | 386c9748d48d3bb4513e8e5c32e2b30a4d6a00d4 (patch) | |
tree | 55d0fb2e9fb6e1149317b9de355c28dd86c57014 /toxav/codec.h | |
parent | 4e6f993e7d22865ee2ac90bd7dd3ff25b078c55c (diff) |
av refactor
Diffstat (limited to 'toxav/codec.h')
-rw-r--r-- | toxav/codec.h | 128 |
1 files changed, 86 insertions, 42 deletions
diff --git a/toxav/codec.h b/toxav/codec.h index db4fbea0..90a74ce8 100644 --- a/toxav/codec.h +++ b/toxav/codec.h | |||
@@ -24,6 +24,9 @@ | |||
24 | #ifndef _CODEC_H_ | 24 | #ifndef _CODEC_H_ |
25 | #define _CODEC_H_ | 25 | #define _CODEC_H_ |
26 | 26 | ||
27 | #include "toxav.h" | ||
28 | #include "rtp.h" | ||
29 | |||
27 | #include <stdio.h> | 30 | #include <stdio.h> |
28 | #include <math.h> | 31 | #include <math.h> |
29 | #include <pthread.h> | 32 | #include <pthread.h> |
@@ -32,85 +35,126 @@ | |||
32 | #include <vpx/vpx_encoder.h> | 35 | #include <vpx/vpx_encoder.h> |
33 | #include <vpx/vp8dx.h> | 36 | #include <vpx/vp8dx.h> |
34 | #include <vpx/vp8cx.h> | 37 | #include <vpx/vp8cx.h> |
38 | #include <vpx/vpx_image.h> | ||
35 | #define VIDEO_CODEC_DECODER_INTERFACE (vpx_codec_vp8_dx()) | 39 | #define VIDEO_CODEC_DECODER_INTERFACE (vpx_codec_vp8_dx()) |
36 | #define VIDEO_CODEC_ENCODER_INTERFACE (vpx_codec_vp8_cx()) | 40 | #define VIDEO_CODEC_ENCODER_INTERFACE (vpx_codec_vp8_cx()) |
37 | 41 | ||
38 | /* Audio encoding/decoding */ | 42 | /* Audio encoding/decoding */ |
39 | #include <opus.h> | 43 | #include <opus.h> |
40 | 44 | ||
41 | typedef enum _Capabilities { | 45 | typedef void (*CSAudioCallback) (void* agent, int32_t call_idx, const int16_t* PCM, uint16_t size, void* data); |
42 | none, | 46 | typedef void (*CSVideoCallback) (void* agent, int32_t call_idx, const vpx_image_t* img, void* data); |
47 | |||
48 | typedef enum _CsCapabilities { | ||
43 | a_encoding = 1 << 0, | 49 | a_encoding = 1 << 0, |
44 | a_decoding = 1 << 1, | 50 | a_decoding = 1 << 1, |
45 | v_encoding = 1 << 2, | 51 | v_encoding = 1 << 2, |
46 | v_decoding = 1 << 3 | 52 | v_decoding = 1 << 3 |
47 | } Capabilities; | 53 | } CsCapabilities; |
48 | |||
49 | extern const uint16_t min_jbuf_size; | ||
50 | 54 | ||
51 | typedef struct _CodecState { | 55 | typedef struct _CSSession { |
52 | 56 | ||
57 | /* VIDEO | ||
58 | * | ||
59 | * | ||
60 | */ | ||
61 | int support_video; | ||
62 | |||
53 | /* video encoding */ | 63 | /* video encoding */ |
54 | vpx_codec_ctx_t v_encoder; | 64 | vpx_codec_ctx_t v_encoder; |
55 | uint32_t frame_counter; | 65 | uint32_t frame_counter; |
56 | 66 | ||
57 | /* video decoding */ | 67 | /* video decoding */ |
58 | vpx_codec_ctx_t v_decoder; | 68 | vpx_codec_ctx_t v_decoder; |
59 | int bitrate; | ||
60 | int max_width; | 69 | int max_width; |
61 | int max_height; | 70 | int max_height; |
62 | 71 | ||
72 | |||
73 | /* Data handling */ | ||
74 | uint8_t *frame_buf; /* buffer for split video payloads */ | ||
75 | uint32_t frame_size; /* largest address written to in frame_buf for current input frame*/ | ||
76 | uint8_t frameid_in, frameid_out; /* id of input and output video frame */ | ||
77 | uint32_t last_timestamp; /* calculating cycles */ | ||
78 | |||
79 | /* Limits */ | ||
80 | uint32_t video_frame_piece_size; | ||
81 | uint32_t max_video_frame_size; | ||
82 | |||
83 | /* Reassembling */ | ||
84 | uint8_t *split_video_frame; | ||
85 | const uint8_t *processing_video_frame; | ||
86 | uint16_t processing_video_frame_size; | ||
87 | |||
88 | |||
89 | |||
90 | /* AUDIO | ||
91 | * | ||
92 | * | ||
93 | */ | ||
94 | |||
63 | /* audio encoding */ | 95 | /* audio encoding */ |
64 | OpusEncoder *audio_encoder; | 96 | OpusEncoder *audio_encoder; |
65 | int audio_bitrate; | 97 | int audio_encoder_bitrate; |
66 | int audio_sample_rate; | 98 | int audio_encoder_sample_rate; |
99 | int audio_encoder_frame_duration; | ||
67 | int audio_encoder_channels; | 100 | int audio_encoder_channels; |
68 | 101 | ||
69 | /* audio decoding */ | 102 | /* audio decoding */ |
70 | OpusDecoder *audio_decoder; | 103 | OpusDecoder *audio_decoder; |
104 | int audio_decoder_bitrate; | ||
105 | int audio_decoder_sample_rate; | ||
106 | int audio_decoder_frame_duration; | ||
71 | int audio_decoder_channels; | 107 | int audio_decoder_channels; |
72 | 108 | ||
73 | uint64_t capabilities; /* supports*/ | 109 | struct _JitterBuffer* j_buf; |
110 | |||
74 | 111 | ||
75 | /* Voice activity detection */ | 112 | /* Voice activity detection */ |
76 | uint32_t EVAD_tolerance; /* In frames */ | 113 | uint32_t EVAD_tolerance; /* In frames */ |
77 | uint32_t EVAD_tolerance_cr; | 114 | uint32_t EVAD_tolerance_cr; |
78 | } CodecState; | 115 | |
79 | 116 | ||
80 | 117 | ||
81 | typedef struct _JitterBuffer { | 118 | /* OTHER |
82 | RTPMessage **queue; | 119 | * |
83 | uint32_t size; | 120 | * |
84 | uint32_t capacity; | 121 | */ |
85 | uint16_t bottom; | 122 | |
86 | uint16_t top; | 123 | uint64_t capabilities; /* supports*/ |
87 | } JitterBuffer; | 124 | |
88 | 125 | /* Buffering */ | |
89 | JitterBuffer *create_queue(unsigned int capacity); | 126 | void* abuf_raw, *vbuf_raw; /* Un-decoded data */ |
90 | void terminate_queue(JitterBuffer *q); | 127 | _Bool active; |
91 | void queue(JitterBuffer *q, RTPMessage *pk); | 128 | pthread_mutex_t queue_mutex[1]; |
92 | RTPMessage *dequeue(JitterBuffer *q, int *success); | 129 | |
93 | 130 | void* agent; /* Pointer to ToxAv */ | |
131 | int32_t call_idx; | ||
132 | } CSSession; | ||
133 | |||
134 | CSSession *cs_new(const ToxAvCSettings* cs_self, const ToxAvCSettings* cs_peer, uint32_t jbuf_size, int has_video); | ||
135 | void cs_kill(CSSession *cs); | ||
136 | |||
137 | int cs_split_video_payload(CSSession* cs, const uint8_t* payload, uint16_t length); | ||
138 | const uint8_t* cs_get_split_video_frame(CSSession* cs, uint16_t* size); | ||
139 | |||
140 | /** | ||
141 | * Call playback callbacks | ||
142 | */ | ||
143 | void cs_do(CSSession* cs); | ||
94 | 144 | ||
95 | CodecState *codec_init_session ( uint32_t audio_bitrate, | 145 | void cs_register_audio_callback(CSAudioCallback cb, void* data); |
96 | uint16_t audio_frame_duration, | 146 | void cs_register_video_callback(CSVideoCallback cb, void* data); |
97 | uint32_t audio_sample_rate, | ||
98 | uint32_t encoder_audio_channels, | ||
99 | uint32_t decoder_audio_channels, | ||
100 | uint32_t audio_VAD_tolerance_ms, | ||
101 | uint16_t max_video_width, | ||
102 | uint16_t max_video_height, | ||
103 | uint32_t video_bitrate ); | ||
104 | 147 | ||
105 | void codec_terminate_session(CodecState *cs); | 148 | /* Reconfigure video encoder; return 0 on success or -1 on failure. */ |
149 | int cs_set_video_encoder_resolution(CSSession *cs, uint16_t width, uint16_t height); | ||
150 | int cs_set_video_encoder_bitrate(CSSession *cs, uint32_t video_bitrate); | ||
106 | 151 | ||
107 | /* Reconfigure video encoder | ||
108 | return 0 on success. | ||
109 | return -1 on failure. */ | ||
110 | int reconfigure_video_encoder_resolution(CodecState *cs, uint16_t width, uint16_t height); | ||
111 | int reconfigure_video_encoder_bitrate(CodecState *cs, uint32_t video_bitrate); | ||
112 | 152 | ||
113 | /* Calculate energy and return 1 if has voice, 0 if not */ | 153 | /* Calculate energy and return 1 if has voice, 0 if not */ |
114 | int energy_VAD(CodecState *cs, int16_t *PCM, uint16_t frame_size, float energy); | 154 | int cs_calculate_vad(CSSession *cs, int16_t *PCM, uint16_t frame_size, float energy); |
155 | void cs_set_vad_treshold(CSSession *cs, uint32_t treshold, uint16_t frame_duration); | ||
156 | |||
115 | 157 | ||
158 | /* Internal. Called from rtp_handle_message */ | ||
159 | void queue_message(RTPSession *session, RTPMessage *msg); | ||
116 | #endif /* _CODEC_H_ */ | 160 | #endif /* _CODEC_H_ */ |