1 /******************************************************************************
2 Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17
18 #include <assert.h>
19 #include <inttypes.h>
20 #include "../util/bmem.h"
21 #include "../util/platform.h"
22 #include "../util/profiler.h"
23 #include "../util/threading.h"
24 #include "../util/darray.h"
25 #include "../util/util_uint64.h"
26
27 #include "format-conversion.h"
28 #include "video-io.h"
29 #include "video-frame.h"
30 #include "video-scaler.h"
31
32 extern profiler_name_store_t *obs_get_profiler_name_store(void);
33
34 #define MAX_CONVERT_BUFFERS 3
35 #define MAX_CACHE_SIZE 16
36
37 struct cached_frame_info {
38 struct video_data frame;
39 int skipped;
40 int count;
41 };
42
43 struct video_input {
44 struct video_scale_info conversion;
45 video_scaler_t *scaler;
46 struct video_frame frame[MAX_CONVERT_BUFFERS];
47 int cur_frame;
48
49 void (*callback)(void *param, struct video_data *frame);
50 void *param;
51 };
52
video_input_free(struct video_input * input)53 static inline void video_input_free(struct video_input *input)
54 {
55 for (size_t i = 0; i < MAX_CONVERT_BUFFERS; i++)
56 video_frame_free(&input->frame[i]);
57 video_scaler_destroy(input->scaler);
58 }
59
60 struct video_output {
61 struct video_output_info info;
62
63 pthread_t thread;
64 pthread_mutex_t data_mutex;
65 bool stop;
66
67 os_sem_t *update_semaphore;
68 uint64_t frame_time;
69 volatile long skipped_frames;
70 volatile long total_frames;
71
72 bool initialized;
73
74 pthread_mutex_t input_mutex;
75 DARRAY(struct video_input) inputs;
76
77 size_t available_frames;
78 size_t first_added;
79 size_t last_added;
80 struct cached_frame_info cache[MAX_CACHE_SIZE];
81
82 volatile bool raw_active;
83 volatile long gpu_refs;
84 };
85
86 /* ------------------------------------------------------------------------- */
87
scale_video_output(struct video_input * input,struct video_data * data)88 static inline bool scale_video_output(struct video_input *input,
89 struct video_data *data)
90 {
91 bool success = true;
92
93 if (input->scaler) {
94 struct video_frame *frame;
95
96 if (++input->cur_frame == MAX_CONVERT_BUFFERS)
97 input->cur_frame = 0;
98
99 frame = &input->frame[input->cur_frame];
100
101 success = video_scaler_scale(input->scaler, frame->data,
102 frame->linesize,
103 (const uint8_t *const *)data->data,
104 data->linesize);
105
106 if (success) {
107 for (size_t i = 0; i < MAX_AV_PLANES; i++) {
108 data->data[i] = frame->data[i];
109 data->linesize[i] = frame->linesize[i];
110 }
111 } else {
112 blog(LOG_WARNING, "video-io: Could not scale frame!");
113 }
114 }
115
116 return success;
117 }
118
video_output_cur_frame(struct video_output * video)119 static inline bool video_output_cur_frame(struct video_output *video)
120 {
121 struct cached_frame_info *frame_info;
122 bool complete;
123 bool skipped;
124
125 /* -------------------------------- */
126
127 pthread_mutex_lock(&video->data_mutex);
128
129 frame_info = &video->cache[video->first_added];
130
131 pthread_mutex_unlock(&video->data_mutex);
132
133 /* -------------------------------- */
134
135 pthread_mutex_lock(&video->input_mutex);
136
137 for (size_t i = 0; i < video->inputs.num; i++) {
138 struct video_input *input = video->inputs.array + i;
139 struct video_data frame = frame_info->frame;
140
141 if (scale_video_output(input, &frame))
142 input->callback(input->param, &frame);
143 }
144
145 pthread_mutex_unlock(&video->input_mutex);
146
147 /* -------------------------------- */
148
149 pthread_mutex_lock(&video->data_mutex);
150
151 frame_info->frame.timestamp += video->frame_time;
152 complete = --frame_info->count == 0;
153 skipped = frame_info->skipped > 0;
154
155 if (complete) {
156 if (++video->first_added == video->info.cache_size)
157 video->first_added = 0;
158
159 if (++video->available_frames == video->info.cache_size)
160 video->last_added = video->first_added;
161 } else if (skipped) {
162 --frame_info->skipped;
163 os_atomic_inc_long(&video->skipped_frames);
164 }
165
166 pthread_mutex_unlock(&video->data_mutex);
167
168 /* -------------------------------- */
169
170 return complete;
171 }
172
video_thread(void * param)173 static void *video_thread(void *param)
174 {
175 struct video_output *video = param;
176
177 os_set_thread_name("video-io: video thread");
178
179 const char *video_thread_name =
180 profile_store_name(obs_get_profiler_name_store(),
181 "video_thread(%s)", video->info.name);
182
183 while (os_sem_wait(video->update_semaphore) == 0) {
184 if (video->stop)
185 break;
186
187 profile_start(video_thread_name);
188 while (!video->stop && !video_output_cur_frame(video)) {
189 os_atomic_inc_long(&video->total_frames);
190 }
191
192 os_atomic_inc_long(&video->total_frames);
193 profile_end(video_thread_name);
194
195 profile_reenable_thread();
196 }
197
198 return NULL;
199 }
200
201 /* ------------------------------------------------------------------------- */
202
valid_video_params(const struct video_output_info * info)203 static inline bool valid_video_params(const struct video_output_info *info)
204 {
205 return info->height != 0 && info->width != 0 && info->fps_den != 0 &&
206 info->fps_num != 0;
207 }
208
init_cache(struct video_output * video)209 static inline void init_cache(struct video_output *video)
210 {
211 if (video->info.cache_size > MAX_CACHE_SIZE)
212 video->info.cache_size = MAX_CACHE_SIZE;
213
214 for (size_t i = 0; i < video->info.cache_size; i++) {
215 struct video_frame *frame;
216 frame = (struct video_frame *)&video->cache[i];
217
218 video_frame_init(frame, video->info.format, video->info.width,
219 video->info.height);
220 }
221
222 video->available_frames = video->info.cache_size;
223 }
224
video_output_open(video_t ** video,struct video_output_info * info)225 int video_output_open(video_t **video, struct video_output_info *info)
226 {
227 struct video_output *out;
228
229 if (!valid_video_params(info))
230 return VIDEO_OUTPUT_INVALIDPARAM;
231
232 out = bzalloc(sizeof(struct video_output));
233 if (!out)
234 goto fail0;
235
236 memcpy(&out->info, info, sizeof(struct video_output_info));
237 out->frame_time =
238 util_mul_div64(1000000000ULL, info->fps_den, info->fps_num);
239 out->initialized = false;
240
241 if (pthread_mutex_init_recursive(&out->data_mutex) != 0)
242 goto fail0;
243 if (pthread_mutex_init_recursive(&out->input_mutex) != 0)
244 goto fail1;
245 if (os_sem_init(&out->update_semaphore, 0) != 0)
246 goto fail2;
247 if (pthread_create(&out->thread, NULL, video_thread, out) != 0)
248 goto fail3;
249
250 init_cache(out);
251
252 out->initialized = true;
253 *video = out;
254 return VIDEO_OUTPUT_SUCCESS;
255
256 fail3:
257 os_sem_destroy(out->update_semaphore);
258 fail2:
259 pthread_mutex_destroy(&out->input_mutex);
260 fail1:
261 pthread_mutex_destroy(&out->data_mutex);
262 fail0:
263 video_output_close(out);
264 return VIDEO_OUTPUT_FAIL;
265 }
266
video_output_close(video_t * video)267 void video_output_close(video_t *video)
268 {
269 if (!video)
270 return;
271
272 video_output_stop(video);
273
274 for (size_t i = 0; i < video->inputs.num; i++)
275 video_input_free(&video->inputs.array[i]);
276 da_free(video->inputs);
277
278 for (size_t i = 0; i < video->info.cache_size; i++)
279 video_frame_free((struct video_frame *)&video->cache[i]);
280
281 bfree(video);
282 }
283
video_get_input_idx(const video_t * video,void (* callback)(void * param,struct video_data * frame),void * param)284 static size_t video_get_input_idx(const video_t *video,
285 void (*callback)(void *param,
286 struct video_data *frame),
287 void *param)
288 {
289 for (size_t i = 0; i < video->inputs.num; i++) {
290 struct video_input *input = video->inputs.array + i;
291 if (input->callback == callback && input->param == param)
292 return i;
293 }
294
295 return DARRAY_INVALID;
296 }
297
video_input_init(struct video_input * input,struct video_output * video)298 static inline bool video_input_init(struct video_input *input,
299 struct video_output *video)
300 {
301 if (input->conversion.width != video->info.width ||
302 input->conversion.height != video->info.height ||
303 input->conversion.format != video->info.format) {
304 struct video_scale_info from = {.format = video->info.format,
305 .width = video->info.width,
306 .height = video->info.height,
307 .range = video->info.range,
308 .colorspace =
309 video->info.colorspace};
310
311 int ret = video_scaler_create(&input->scaler,
312 &input->conversion, &from,
313 VIDEO_SCALE_FAST_BILINEAR);
314 if (ret != VIDEO_SCALER_SUCCESS) {
315 if (ret == VIDEO_SCALER_BAD_CONVERSION)
316 blog(LOG_ERROR, "video_input_init: Bad "
317 "scale conversion type");
318 else
319 blog(LOG_ERROR, "video_input_init: Failed to "
320 "create scaler");
321
322 return false;
323 }
324
325 for (size_t i = 0; i < MAX_CONVERT_BUFFERS; i++)
326 video_frame_init(&input->frame[i],
327 input->conversion.format,
328 input->conversion.width,
329 input->conversion.height);
330 }
331
332 return true;
333 }
334
reset_frames(video_t * video)335 static inline void reset_frames(video_t *video)
336 {
337 os_atomic_set_long(&video->skipped_frames, 0);
338 os_atomic_set_long(&video->total_frames, 0);
339 }
340
video_output_connect(video_t * video,const struct video_scale_info * conversion,void (* callback)(void * param,struct video_data * frame),void * param)341 bool video_output_connect(
342 video_t *video, const struct video_scale_info *conversion,
343 void (*callback)(void *param, struct video_data *frame), void *param)
344 {
345 bool success = false;
346
347 if (!video || !callback)
348 return false;
349
350 pthread_mutex_lock(&video->input_mutex);
351
352 if (video_get_input_idx(video, callback, param) == DARRAY_INVALID) {
353 struct video_input input;
354 memset(&input, 0, sizeof(input));
355
356 input.callback = callback;
357 input.param = param;
358
359 if (conversion) {
360 input.conversion = *conversion;
361 } else {
362 input.conversion.format = video->info.format;
363 input.conversion.width = video->info.width;
364 input.conversion.height = video->info.height;
365 }
366
367 if (input.conversion.width == 0)
368 input.conversion.width = video->info.width;
369 if (input.conversion.height == 0)
370 input.conversion.height = video->info.height;
371
372 success = video_input_init(&input, video);
373 if (success) {
374 if (video->inputs.num == 0) {
375 if (!os_atomic_load_long(&video->gpu_refs)) {
376 reset_frames(video);
377 }
378 os_atomic_set_bool(&video->raw_active, true);
379 }
380 da_push_back(video->inputs, &input);
381 }
382 }
383
384 pthread_mutex_unlock(&video->input_mutex);
385
386 return success;
387 }
388
log_skipped(video_t * video)389 static void log_skipped(video_t *video)
390 {
391 long skipped = os_atomic_load_long(&video->skipped_frames);
392 double percentage_skipped =
393 (double)skipped /
394 (double)os_atomic_load_long(&video->total_frames) * 100.0;
395
396 if (skipped)
397 blog(LOG_INFO,
398 "Video stopped, number of "
399 "skipped frames due "
400 "to encoding lag: "
401 "%ld/%ld (%0.1f%%)",
402 video->skipped_frames, video->total_frames,
403 percentage_skipped);
404 }
405
video_output_disconnect(video_t * video,void (* callback)(void * param,struct video_data * frame),void * param)406 void video_output_disconnect(video_t *video,
407 void (*callback)(void *param,
408 struct video_data *frame),
409 void *param)
410 {
411 if (!video || !callback)
412 return;
413
414 pthread_mutex_lock(&video->input_mutex);
415
416 size_t idx = video_get_input_idx(video, callback, param);
417 if (idx != DARRAY_INVALID) {
418 video_input_free(video->inputs.array + idx);
419 da_erase(video->inputs, idx);
420
421 if (video->inputs.num == 0) {
422 os_atomic_set_bool(&video->raw_active, false);
423 if (!os_atomic_load_long(&video->gpu_refs)) {
424 log_skipped(video);
425 }
426 }
427 }
428
429 pthread_mutex_unlock(&video->input_mutex);
430 }
431
video_output_active(const video_t * video)432 bool video_output_active(const video_t *video)
433 {
434 if (!video)
435 return false;
436 return os_atomic_load_bool(&video->raw_active);
437 }
438
video_output_get_info(const video_t * video)439 const struct video_output_info *video_output_get_info(const video_t *video)
440 {
441 return video ? &video->info : NULL;
442 }
443
video_output_lock_frame(video_t * video,struct video_frame * frame,int count,uint64_t timestamp)444 bool video_output_lock_frame(video_t *video, struct video_frame *frame,
445 int count, uint64_t timestamp)
446 {
447 struct cached_frame_info *cfi;
448 bool locked;
449
450 if (!video)
451 return false;
452
453 pthread_mutex_lock(&video->data_mutex);
454
455 if (video->available_frames == 0) {
456 video->cache[video->last_added].count += count;
457 video->cache[video->last_added].skipped += count;
458 locked = false;
459
460 } else {
461 if (video->available_frames != video->info.cache_size) {
462 if (++video->last_added == video->info.cache_size)
463 video->last_added = 0;
464 }
465
466 cfi = &video->cache[video->last_added];
467 cfi->frame.timestamp = timestamp;
468 cfi->count = count;
469 cfi->skipped = 0;
470
471 memcpy(frame, &cfi->frame, sizeof(*frame));
472
473 locked = true;
474 }
475
476 pthread_mutex_unlock(&video->data_mutex);
477
478 return locked;
479 }
480
video_output_unlock_frame(video_t * video)481 void video_output_unlock_frame(video_t *video)
482 {
483 if (!video)
484 return;
485
486 pthread_mutex_lock(&video->data_mutex);
487
488 video->available_frames--;
489 os_sem_post(video->update_semaphore);
490
491 pthread_mutex_unlock(&video->data_mutex);
492 }
493
video_output_get_frame_time(const video_t * video)494 uint64_t video_output_get_frame_time(const video_t *video)
495 {
496 return video ? video->frame_time : 0;
497 }
498
video_output_stop(video_t * video)499 void video_output_stop(video_t *video)
500 {
501 void *thread_ret;
502
503 if (!video)
504 return;
505
506 if (video->initialized) {
507 video->initialized = false;
508 video->stop = true;
509 os_sem_post(video->update_semaphore);
510 pthread_join(video->thread, &thread_ret);
511 os_sem_destroy(video->update_semaphore);
512 pthread_mutex_destroy(&video->data_mutex);
513 pthread_mutex_destroy(&video->input_mutex);
514 }
515 }
516
video_output_stopped(video_t * video)517 bool video_output_stopped(video_t *video)
518 {
519 if (!video)
520 return true;
521
522 return video->stop;
523 }
524
video_output_get_format(const video_t * video)525 enum video_format video_output_get_format(const video_t *video)
526 {
527 return video ? video->info.format : VIDEO_FORMAT_NONE;
528 }
529
video_output_get_width(const video_t * video)530 uint32_t video_output_get_width(const video_t *video)
531 {
532 return video ? video->info.width : 0;
533 }
534
video_output_get_height(const video_t * video)535 uint32_t video_output_get_height(const video_t *video)
536 {
537 return video ? video->info.height : 0;
538 }
539
video_output_get_frame_rate(const video_t * video)540 double video_output_get_frame_rate(const video_t *video)
541 {
542 if (!video)
543 return 0.0;
544
545 return (double)video->info.fps_num / (double)video->info.fps_den;
546 }
547
video_output_get_skipped_frames(const video_t * video)548 uint32_t video_output_get_skipped_frames(const video_t *video)
549 {
550 return (uint32_t)os_atomic_load_long(&video->skipped_frames);
551 }
552
video_output_get_total_frames(const video_t * video)553 uint32_t video_output_get_total_frames(const video_t *video)
554 {
555 return (uint32_t)os_atomic_load_long(&video->total_frames);
556 }
557
558 /* Note: These four functions below are a very slight bit of a hack. If the
559 * texture encoder thread is active while the raw encoder thread is active, the
560 * total frame count will just be doubled while they're both active. Which is
561 * fine. What's more important is having a relatively accurate skipped frame
562 * count. */
563
video_output_inc_texture_encoders(video_t * video)564 void video_output_inc_texture_encoders(video_t *video)
565 {
566 if (os_atomic_inc_long(&video->gpu_refs) == 1 &&
567 !os_atomic_load_bool(&video->raw_active)) {
568 reset_frames(video);
569 }
570 }
571
video_output_dec_texture_encoders(video_t * video)572 void video_output_dec_texture_encoders(video_t *video)
573 {
574 if (os_atomic_dec_long(&video->gpu_refs) == 0 &&
575 !os_atomic_load_bool(&video->raw_active)) {
576 log_skipped(video);
577 }
578 }
579
video_output_inc_texture_frames(video_t * video)580 void video_output_inc_texture_frames(video_t *video)
581 {
582 os_atomic_inc_long(&video->total_frames);
583 }
584
video_output_inc_texture_skipped_frames(video_t * video)585 void video_output_inc_texture_skipped_frames(video_t *video)
586 {
587 os_atomic_inc_long(&video->skipped_frames);
588 }
589