UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
video_compress.cpp File Reference

Video compress functions. More...

#include <cassert>
#include <cinttypes>
#include <cstdio>
#include <cstring>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include "messaging.h"
#include "module.h"
#include "tv.h"
#include "utils/synchronized_queue.h"
#include "utils/thread.h"
#include "utils/vf_split.h"
#include "utils/worker.h"
#include "video.h"
#include "video_compress.h"
#include "lib_common.h"
#include "debug.h"
Include dependency graph for video_compress.cpp:

Go to the source code of this file.

Classes

struct  compress_state
 Video compress state. More...
 
struct  compress_worker_data
 Auxiliary structure passed to worker thread. More...
 

Macros

#define MOD_NAME   "[vcompress] "
 

Functions

static void async_poison (struct compress_state_real *s)
 
static bool check_state_count (unsigned tile_count, struct compress_state *proxy)
 
void compress_done (struct compress_state *proxy)
 Video compression cleanup function.
 
void compress_frame (struct compress_state *proxy, shared_ptr< video_frame > frame)
 
int compress_init (struct module *parent, const char *config_string, struct compress_state **state)
 This function initializes video compression.
 
shared_ptr< video_frame > compress_pop (struct compress_state *proxy)
 
static void compress_process_message (struct compress_state *proxy, struct msg_change_compress_data *data)
 Processes message.
 
void show_compress_help (bool full)
 Displays list of available compressions.
 
Tile API Routines

The worker callbacks here are optimization - all tiles are processed concurrently.

static shared_ptr< video_frame > compress_frame_tiles (struct compress_state *proxy, shared_ptr< video_frame > frame)
 
static void * compress_tile_callback (void *arg)
 This function is callback passed to a "thread pool".
 

Detailed Description

Video compress functions.

Author
Martin Pulec pulec.nosp@m.@ces.nosp@m.net.c.nosp@m.z

Definition in file video_compress.cpp.

Macro Definition Documentation

◆ MOD_NAME

#define MOD_NAME   "[vcompress] "

Definition at line 62 of file video_compress.cpp.

Function Documentation

◆ async_poison()

static void async_poison ( struct compress_state_real * s)
static

Definition at line 117 of file video_compress.cpp.

117 {
118 if (s->funcs->compress_frame_async_push_func) {
119 s->funcs->compress_frame_async_push_func(s->state[0], {}); // poison
120 } else if (s->funcs->compress_tile_async_push_func){
121 for(size_t i = 0; i < s->state.size(); i++){
122 s->funcs->compress_tile_async_push_func(s->state[i], {}); // poison
123 }
124 }
125}
compress_tile_async_push_t compress_tile_async_push_func
Async tile API.
compress_frame_async_push_t compress_frame_async_push_func
Async API.

References video_compress_info::compress_frame_async_push_func, and video_compress_info::compress_tile_async_push_func.

Referenced by compress_frame(), and compress_process_message().

◆ check_state_count()

static bool check_state_count ( unsigned tile_count,
struct compress_state * proxy )
static

Checks if there are at least as many states as there are tiles. If there are not enough states it initializes new ones.

Parameters
proxycompress state
[in]frameuncompressed frame
Returns
false in case of failure

Definition at line 287 of file video_compress.cpp.

288{
289 struct compress_state_real *s = proxy->ptr;
290
291 if(tile_count != s->state.size()) {
292 size_t old_size = s->state.size();
293 s->state.resize(tile_count);
294 for (unsigned int i = old_size; i < s->state.size(); ++i) {
295 s->state[i] = s->funcs->init_func(&proxy->mod, s->compress_options.c_str());
296 if(!s->state[i]) {
298 "Compression initialization failed\n";
299 return false;
300 }
301 }
302 }
303 return true;
304}
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define LOG(level)
Definition debug.h:313
struct module mod
compress module data
struct compress_state_real * ptr
pointer to real compress state
compress_init_t init_func
compress driver initialization function
#define MOD_NAME

References video_compress_info::init_func, LOG, LOG_LEVEL_ERROR, compress_state::mod, MOD_NAME, and compress_state::ptr.

Referenced by compress_frame(), and compress_frame_tiles().

◆ compress_frame_tiles()

static shared_ptr< video_frame > compress_frame_tiles ( struct compress_state * proxy,
shared_ptr< video_frame > frame )
static

Compresses video frame with tiles API

Parameters
proxycompress state
[in]frameuncompressed frame
Returns
compressed video frame, may be NULL if compression failed

Definition at line 424 of file video_compress.cpp.

426{
427 struct compress_state_real *s = proxy->ptr;
428 const int tile_cnt = (int) proxy->ptr->state.size();
429 vector<shared_ptr<video_frame>> separate_tiles;
430 if (frame) {
431 if (!check_state_count(frame->tile_count, proxy)) {
432 return nullptr;
433 }
434 separate_tiles = vf_separate_tiles(frame);
435 } else {
436 separate_tiles.resize(tile_cnt);
437 }
438
439 // frame pointer may no longer be valid
440 frame = NULL;
441
442 vector<task_result_handle_t> task_handle(tile_cnt);
443
444 vector <compress_worker_data> data_tile(tile_cnt);
445 for (int i = 0; i < tile_cnt; ++i) {
446 struct compress_worker_data *data = &data_tile[i];
447 data->state = s->state[i];
448 data->frame = separate_tiles[i];
449 data->callback = s->funcs->compress_tile_func;
450
451 task_handle[i] = task_run_async(compress_tile_callback, data);
452 }
453
454 vector<shared_ptr<video_frame>> compressed_tiles(separate_tiles.size());
455
456 bool failed = false;
457 for(unsigned int i = 0; i < separate_tiles.size(); ++i) {
458 struct compress_worker_data *data = (struct compress_worker_data *)
459 wait_task(task_handle[i]);
460
461 if(!data->ret) {
462 failed = true;
463 }
464
465 compressed_tiles[i] = data->ret;
466 }
467
468 if (failed) {
469 return NULL;
470 }
471
472 return vf_merge_tiles(compressed_tiles);
473}
struct video_frame * frame
Auxiliary structure passed to worker thread.
shared_ptr< video_frame > frame
uncompressed tile to be compressed
shared_ptr< video_frame > ret
OUT - returned compressed tile, NULL if failed.
struct module * state
compress driver status
compress_tile_t callback
tile compress callback
compress_tile_t compress_tile_func
compress function for Tile API
unsigned int tile_count
Definition types.h:344
shared_ptr< video_frame > vf_merge_tiles(std::vector< shared_ptr< video_frame > > const &tiles)
Definition vf_split.cpp:135
vector< shared_ptr< video_frame > > vf_separate_tiles(shared_ptr< video_frame > frame)
Definition vf_split.cpp:114
static bool check_state_count(unsigned tile_count, struct compress_state *proxy)
static void * compress_tile_callback(void *arg)
This function is callback passed to a "thread pool".
void * wait_task(task_result_handle_t handle)
Definition worker.cpp:280
task_result_handle_t task_run_async(runnable_t task, void *data)
Runs task asynchronously.
Definition worker.cpp:262

References compress_worker_data::callback, check_state_count(), compress_tile_callback(), video_compress_info::compress_tile_func, compress_worker_data::frame, frame, compress_state::ptr, compress_worker_data::ret, compress_worker_data::state, task_run_async(), video_frame::tile_count, vf_merge_tiles(), vf_separate_tiles(), and wait_task().

Referenced by compress_frame().

Here is the call graph for this function:

◆ compress_process_message()

static void compress_process_message ( struct compress_state * proxy,
struct msg_change_compress_data * data )
static

Processes message.

This function is a callback called from control thread to change some parameters of compression.

Parameters
[in]receiverpointer to the compress module
[in]msgmessage to process
Todo
Handle responses more inteligently (eg. aggregate).

Definition at line 136 of file video_compress.cpp.

137{
138 struct response *r = NULL;
139 /* In this case we are only changing some parameter of compression.
140 * This means that we pass the parameter to compress driver. */
141 if(data->what == CHANGE_PARAMS) {
142 for(unsigned int i = 0; i < proxy->ptr->state.size(); ++i) {
143 struct msg_change_compress_data *tmp_data =
144 (struct msg_change_compress_data *)
146 tmp_data->what = data->what;
147 strncpy(tmp_data->config_string, data->config_string,
148 sizeof(tmp_data->config_string));
149 struct response *resp = send_message_to_receiver(proxy->ptr->state[i],
150 (struct message *) tmp_data);
153 free_response(r); // frees previous response
154 r = resp;
155 }
156
157 } else {
158 struct compress_state_real *new_state;
159 char config[1024];
160 strncpy(config, data->config_string, sizeof(config));
161
162 try {
163 new_state = compress_state_real::create(&proxy->mod, config, proxy);
164 } catch (int i) {
165 free_message((struct message *) data,
167 return;
168 }
169
170 struct compress_state_real *old = proxy->ptr;
171 // let the async processing finish
172 old->discard_frames = true;
173 async_poison(old);
174 delete old;
175 proxy->ptr = new_state;
176 r = new_response(RESPONSE_OK, NULL);
177 }
178
179 free_message((struct message *) data, r);
180}
static void new_message(struct module *m)
void free_message(struct message *msg, struct response *r)
struct response * new_response(int status, const char *text)
struct response * send_message_to_receiver(struct module *receiver, struct message *msg)
void free_response(struct response *r)
#define RESPONSE_OK
Definition messaging.h:57
@ CHANGE_PARAMS
Definition messaging.h:130
#define RESPONSE_INT_SERV_ERR
Definition messaging.h:63
enum compress_change_type what
Definition messaging.h:135
static void async_poison(struct compress_state_real *s)

References async_poison(), CHANGE_PARAMS, msg_change_compress_data::config_string, free_message(), free_response(), compress_state::mod, new_message(), new_response(), compress_state::ptr, RESPONSE_INT_SERV_ERR, RESPONSE_OK, send_message_to_receiver(), and msg_change_compress_data::what.

Referenced by compress_frame().

Here is the call graph for this function:

◆ compress_tile_callback()

static void * compress_tile_callback ( void * arg)
static

This function is callback passed to a "thread pool".

Parameters
argcompress_worker_data
Returns
compress_worker_data (same as input)

Definition at line 409 of file video_compress.cpp.

409 {
411
412 s->ret = s->callback(s->state, s->frame);
413
414 return s;
415}

References compress_worker_data::callback, compress_worker_data::frame, compress_worker_data::ret, and compress_worker_data::state.

Referenced by compress_frame_tiles().