diff options
author | mannol <eniz_vukovic@hotmail.com> | 2015-04-21 02:31:12 +0200 |
---|---|---|
committer | mannol <eniz_vukovic@hotmail.com> | 2015-04-21 02:31:12 +0200 |
commit | 3fd0ee5f0873924b4881b0e33eb1c17ea877ab4a (patch) | |
tree | 6a4ee64a3ea7f0191ccfd0205a68411cf0b84063 /toxav/video.c | |
parent | cbb8fdd4eab3bae4db1179f1fa04cdaa35957aeb (diff) |
Final touchups
Diffstat (limited to 'toxav/video.c')
-rw-r--r-- | toxav/video.c | 332 |
1 files changed, 332 insertions, 0 deletions
diff --git a/toxav/video.c b/toxav/video.c new file mode 100644 index 00000000..d51cfd4a --- /dev/null +++ b/toxav/video.c | |||
@@ -0,0 +1,332 @@ | |||
1 | /** video.c | ||
2 | * | ||
3 | * Copyright (C) 2013-2015 Tox project All Rights Reserved. | ||
4 | * | ||
5 | * This file is part of Tox. | ||
6 | * | ||
7 | * Tox is free software: you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation, either version 3 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * Tox is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | ||
20 | */ | ||
21 | |||
22 | #include <stdlib.h> | ||
23 | #include <assert.h> | ||
24 | |||
25 | #include "video.h" | ||
26 | #include "msi.h" | ||
27 | |||
28 | #include "../toxcore/logger.h" | ||
29 | #include "../toxcore/network.h" | ||
30 | |||
31 | /* Good quality encode. */ | ||
32 | #define MAX_DECODE_TIME_US 0 | ||
33 | |||
34 | #define MAX_VIDEOFRAME_SIZE 0x40000 /* 256KiB */ | ||
35 | #define VIDEOFRAME_HEADER_SIZE 0x2 | ||
36 | |||
37 | /* FIXME: Might not be enough? NOTE: I think it is enough */ | ||
38 | #define VIDEO_DECODE_BUFFER_SIZE 20 | ||
39 | |||
40 | typedef struct { uint16_t size; uint8_t data[]; } Payload; | ||
41 | |||
42 | bool create_video_encoder (vpx_codec_ctx_t* dest, int32_t bitrate); | ||
43 | |||
44 | |||
45 | VCSession* vc_new(ToxAV* av, uint32_t friend_id, toxav_receive_video_frame_cb* cb, void* cb_data, uint32_t mvfpsz) | ||
46 | { | ||
47 | VCSession *vc = calloc(sizeof(VCSession), 1); | ||
48 | |||
49 | if (!vc) { | ||
50 | LOGGER_WARNING("Allocation failed! Application might misbehave!"); | ||
51 | return NULL; | ||
52 | } | ||
53 | |||
54 | if (create_recursive_mutex(vc->queue_mutex) != 0) { | ||
55 | LOGGER_WARNING("Failed to create recursive mutex!"); | ||
56 | free(vc); | ||
57 | return NULL; | ||
58 | } | ||
59 | |||
60 | if ( !(vc->frame_buf = calloc(MAX_VIDEOFRAME_SIZE, 1)) ) | ||
61 | goto BASE_CLEANUP; | ||
62 | if ( !(vc->split_video_frame = calloc(VIDEOFRAME_PIECE_SIZE + VIDEOFRAME_HEADER_SIZE, 1)) ) | ||
63 | goto BASE_CLEANUP; | ||
64 | if ( !(vc->vbuf_raw = rb_new(VIDEO_DECODE_BUFFER_SIZE)) ) | ||
65 | goto BASE_CLEANUP; | ||
66 | |||
67 | int rc = vpx_codec_dec_init_ver(vc->v_decoder, VIDEO_CODEC_DECODER_INTERFACE, | ||
68 | NULL, 0, VPX_DECODER_ABI_VERSION); | ||
69 | if ( rc != VPX_CODEC_OK) { | ||
70 | LOGGER_ERROR("Init video_decoder failed: %s", vpx_codec_err_to_string(rc)); | ||
71 | goto BASE_CLEANUP; | ||
72 | } | ||
73 | |||
74 | if (!create_video_encoder(vc->v_encoder, 500000)) { | ||
75 | vpx_codec_destroy(vc->v_decoder); | ||
76 | goto BASE_CLEANUP; | ||
77 | } | ||
78 | |||
79 | vc->linfts = current_time_monotonic(); | ||
80 | vc->lcfd = 60; | ||
81 | |||
82 | vc->peer_video_frame_piece_size = mvfpsz; | ||
83 | |||
84 | return vc; | ||
85 | |||
86 | BASE_CLEANUP: | ||
87 | pthread_mutex_destroy(vc->queue_mutex); | ||
88 | rb_free(vc->vbuf_raw); | ||
89 | free(vc->split_video_frame); | ||
90 | free(vc->frame_buf); | ||
91 | free(vc); | ||
92 | return NULL; | ||
93 | } | ||
94 | void vc_kill(VCSession* vc) | ||
95 | { | ||
96 | if (!vc) | ||
97 | return; | ||
98 | |||
99 | vpx_codec_destroy(vc->v_encoder); | ||
100 | vpx_codec_destroy(vc->v_decoder); | ||
101 | rb_free(vc->vbuf_raw); | ||
102 | free(vc->split_video_frame); | ||
103 | free(vc->frame_buf); | ||
104 | |||
105 | pthread_mutex_destroy(vc->queue_mutex); | ||
106 | |||
107 | LOGGER_DEBUG("Terminated video handler: %p", vc); | ||
108 | free(vc); | ||
109 | } | ||
110 | void vc_do(VCSession* vc) | ||
111 | { | ||
112 | if (!vc) | ||
113 | return; | ||
114 | |||
115 | Payload *p; | ||
116 | int rc; | ||
117 | |||
118 | pthread_mutex_lock(vc->queue_mutex); | ||
119 | if (rb_read(vc->vbuf_raw, (void**)&p)) { | ||
120 | pthread_mutex_unlock(vc->queue_mutex); | ||
121 | |||
122 | rc = vpx_codec_decode(vc->v_decoder, p->data, p->size, NULL, MAX_DECODE_TIME_US); | ||
123 | free(p); | ||
124 | |||
125 | if (rc != VPX_CODEC_OK) { | ||
126 | LOGGER_ERROR("Error decoding video: %s", vpx_codec_err_to_string(rc)); | ||
127 | } else { | ||
128 | vpx_codec_iter_t iter = NULL; | ||
129 | vpx_image_t *dest = vpx_codec_get_frame(vc->v_decoder, &iter); | ||
130 | |||
131 | /* Play decoded images */ | ||
132 | for (; dest; dest = vpx_codec_get_frame(vc->v_decoder, &iter)) { | ||
133 | if (vc->vcb.first) | ||
134 | vc->vcb.first(vc->av, vc->friend_id, dest->d_w, dest->d_h, | ||
135 | (const uint8_t*)dest->planes[0], (const uint8_t*)dest->planes[1], (const uint8_t*)dest->planes[2], | ||
136 | dest->stride[0], dest->stride[1], dest->stride[2], vc->vcb.second); | ||
137 | |||
138 | vpx_img_free(dest); | ||
139 | } | ||
140 | } | ||
141 | |||
142 | return; | ||
143 | } | ||
144 | pthread_mutex_unlock(vc->queue_mutex); | ||
145 | } | ||
146 | void vc_init_video_splitter_cycle(VCSession* vc) | ||
147 | { | ||
148 | if (!vc) | ||
149 | return; | ||
150 | |||
151 | vc->split_video_frame[0] = vc->frameid_out++; | ||
152 | vc->split_video_frame[1] = 0; | ||
153 | } | ||
154 | int vc_update_video_splitter_cycle(VCSession* vc, const uint8_t* payload, uint16_t length) | ||
155 | { | ||
156 | if (!vc) | ||
157 | return; | ||
158 | |||
159 | vc->processing_video_frame = payload; | ||
160 | vc->processing_video_frame_size = length; | ||
161 | |||
162 | return ((length - 1) / VIDEOFRAME_PIECE_SIZE) + 1; | ||
163 | } | ||
164 | const uint8_t* vc_iterate_split_video_frame(VCSession* vc, uint16_t* size) | ||
165 | { | ||
166 | if (!vc || !size) | ||
167 | return NULL; | ||
168 | |||
169 | if (vc->processing_video_frame_size > VIDEOFRAME_PIECE_SIZE) { | ||
170 | memcpy(vc->split_video_frame + VIDEOFRAME_HEADER_SIZE, | ||
171 | vc->processing_video_frame, | ||
172 | VIDEOFRAME_PIECE_SIZE); | ||
173 | |||
174 | vc->processing_video_frame += VIDEOFRAME_PIECE_SIZE; | ||
175 | vc->processing_video_frame_size -= VIDEOFRAME_PIECE_SIZE; | ||
176 | |||
177 | *size = VIDEOFRAME_PIECE_SIZE + VIDEOFRAME_HEADER_SIZE; | ||
178 | } else { | ||
179 | memcpy(vc->split_video_frame + VIDEOFRAME_HEADER_SIZE, | ||
180 | vc->processing_video_frame, | ||
181 | vc->processing_video_frame_size); | ||
182 | |||
183 | *size = vc->processing_video_frame_size + VIDEOFRAME_HEADER_SIZE; | ||
184 | } | ||
185 | |||
186 | vc->split_video_frame[1]++; | ||
187 | |||
188 | return vc->split_video_frame; | ||
189 | } | ||
190 | int vc_reconfigure_encoder(VCSession* vc, int32_t bitrate, uint16_t width, uint16_t height) | ||
191 | { | ||
192 | if (!vc) | ||
193 | return; | ||
194 | |||
195 | vpx_codec_enc_cfg_t cfg = *vc->v_encoder[0].config.enc; | ||
196 | if (cfg.rc_target_bitrate == bitrate && cfg.g_w == width && cfg.g_h == height) | ||
197 | return 0; /* Nothing changed */ | ||
198 | |||
199 | cfg.rc_target_bitrate = bitrate; | ||
200 | cfg.g_w = width; | ||
201 | cfg.g_h = height; | ||
202 | |||
203 | int rc = vpx_codec_enc_config_set(vc->v_encoder, &cfg); | ||
204 | if ( rc != VPX_CODEC_OK) { | ||
205 | LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc)); | ||
206 | return -1; | ||
207 | } | ||
208 | |||
209 | return 0; | ||
210 | } | ||
211 | /* Called from RTP */ | ||
212 | void vc_queue_message(void* vcp, RTPMessage *msg) | ||
213 | { | ||
214 | /* This function does the reconstruction of video packets. | ||
215 | * See more info about video splitting in docs | ||
216 | */ | ||
217 | if (!vcp || !msg) | ||
218 | return; | ||
219 | |||
220 | VCSession* vc = vcp; | ||
221 | |||
222 | uint8_t *packet = msg->data; | ||
223 | uint32_t packet_size = msg->length; | ||
224 | |||
225 | if (packet_size < VIDEOFRAME_HEADER_SIZE) | ||
226 | goto end; | ||
227 | |||
228 | uint8_t diff = packet[0] - vc->frameid_in; | ||
229 | |||
230 | if (diff != 0) { | ||
231 | if (diff < 225) { /* New frame */ | ||
232 | /* Flush last frames' data and get ready for this frame */ | ||
233 | Payload *p = malloc(sizeof(Payload) + vc->frame_size); | ||
234 | |||
235 | if (p) { | ||
236 | LOGGED_LOCK(vc->queue_mutex); | ||
237 | |||
238 | if (rb_full(vc->vbuf_raw)) { | ||
239 | LOGGER_DEBUG("Dropped video frame"); | ||
240 | Payload *tp; | ||
241 | rb_read(vc->vbuf_raw, (void**)&tp); | ||
242 | free(tp); | ||
243 | } else { | ||
244 | p->size = vc->frame_size; | ||
245 | memcpy(p->data, vc->frame_buf, vc->frame_size); | ||
246 | } | ||
247 | |||
248 | /* Calculate time took for peer to send us this frame */ | ||
249 | uint32_t t_lcfd = current_time_monotonic() - vc->linfts; | ||
250 | vc->lcfd = t_lcfd > 100 ? vc->lcfd : t_lcfd; | ||
251 | vc->linfts = current_time_monotonic(); | ||
252 | |||
253 | rb_write(vc->vbuf_raw, p); | ||
254 | LOGGED_UNLOCK(vc->queue_mutex); | ||
255 | } else { | ||
256 | LOGGER_WARNING("Allocation failed! Program might misbehave!"); | ||
257 | goto end; | ||
258 | } | ||
259 | |||
260 | vc->frameid_in = packet[0]; | ||
261 | memset(vc->frame_buf, 0, vc->frame_size); | ||
262 | vc->frame_size = 0; | ||
263 | |||
264 | } else { /* Old frame; drop */ | ||
265 | LOGGER_DEBUG("Old packet: %u", packet[0]); | ||
266 | goto end; | ||
267 | } | ||
268 | } | ||
269 | |||
270 | uint8_t piece_number = packet[1]; | ||
271 | |||
272 | uint32_t length_before_piece = ((piece_number - 1) * vc->peer_video_frame_piece_size); | ||
273 | uint32_t framebuf_new_length = length_before_piece + (packet_size - VIDEOFRAME_HEADER_SIZE); | ||
274 | |||
275 | if (framebuf_new_length > MAX_VIDEOFRAME_SIZE) | ||
276 | goto end; | ||
277 | |||
278 | |||
279 | /* Otherwise it's part of the frame so just process */ | ||
280 | /* LOGGER_DEBUG("Video Packet: %u %u", packet[0], packet[1]); */ | ||
281 | |||
282 | memcpy(vc->frame_buf + length_before_piece, | ||
283 | packet + VIDEOFRAME_HEADER_SIZE, | ||
284 | packet_size - VIDEOFRAME_HEADER_SIZE); | ||
285 | |||
286 | if (framebuf_new_length > vc->frame_size) | ||
287 | vc->frame_size = framebuf_new_length; | ||
288 | |||
289 | end: | ||
290 | rtp_free_msg(NULL, msg); | ||
291 | } | ||
292 | |||
293 | |||
294 | bool create_video_encoder (vpx_codec_ctx_t* dest, int32_t bitrate) | ||
295 | { | ||
296 | assert(dest); | ||
297 | |||
298 | vpx_codec_enc_cfg_t cfg; | ||
299 | int rc = vpx_codec_enc_config_default(VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0); | ||
300 | |||
301 | if (rc != VPX_CODEC_OK) { | ||
302 | LOGGER_ERROR("Failed to get config: %s", vpx_codec_err_to_string(rc)); | ||
303 | return false; | ||
304 | } | ||
305 | |||
306 | rc = vpx_codec_enc_init_ver(dest, VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0, | ||
307 | VPX_ENCODER_ABI_VERSION); | ||
308 | |||
309 | if ( rc != VPX_CODEC_OK) { | ||
310 | LOGGER_ERROR("Failed to initialize encoder: %s", vpx_codec_err_to_string(rc)); | ||
311 | return false; | ||
312 | } | ||
313 | |||
314 | cfg.rc_target_bitrate = bitrate; | ||
315 | cfg.g_w = 800; | ||
316 | cfg.g_h = 600; | ||
317 | cfg.g_pass = VPX_RC_ONE_PASS; | ||
318 | cfg.g_error_resilient = VPX_ERROR_RESILIENT_DEFAULT | VPX_ERROR_RESILIENT_PARTITIONS; | ||
319 | cfg.g_lag_in_frames = 0; | ||
320 | cfg.kf_min_dist = 0; | ||
321 | cfg.kf_max_dist = 48; | ||
322 | cfg.kf_mode = VPX_KF_AUTO; | ||
323 | |||
324 | rc = vpx_codec_control(dest, VP8E_SET_CPUUSED, 8); | ||
325 | |||
326 | if ( rc != VPX_CODEC_OK) { | ||
327 | LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc)); | ||
328 | vpx_codec_destroy(dest); | ||
329 | } | ||
330 | |||
331 | return true; | ||
332 | } \ No newline at end of file | ||