UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
video_decompress.cpp
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2011-2019 CESNET, z. s. p. o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifdef HAVE_CONFIG_H
39#include "config.h"
40#include "config_unix.h"
41#include "config_win32.h"
42#endif // HAVE_CONFIG_H
43
44#include <stdio.h>
45#include <string.h>
46#include <string>
47#include "debug.h"
48#include "video_codec.h"
49#include "video_decompress.h"
50#include "lib_common.h"
51
52#define DECOMPRESS_MAGIC 0xdff34f21u
53
54using std::string;
55
60 uint32_t magic;
62 void *state;
63};
64
65ADD_TO_PARAM("decompress", "* decompress=<name>[:<codec>]\n"
66 " Forces specified decompress module (will fail if not able to decompress\n"
67 " the received compression). Optionally also force codec to decode to."
68 " See 'uv --list-modules' to see available decompress modules.\n");
78static int find_best_decompress(codec_t compression, struct pixfmt_desc internal_prop, codec_t out_pixfmt,
79 int prio_min, int prio_max, const struct video_decompress_info **vdi, string & name) {
81
82 int best_priority = prio_max + 1;
83 string force_module;
84
85 if (commandline_params.find("decompress") != commandline_params.end()) {
86 char *tmp = strdup(commandline_params.at("decompress").c_str());
87 if (strchr(tmp, ':')) {
88 // if out_pixfmt specified and doesn't match, return
89 if (out_pixfmt != get_codec_from_name(strchr(tmp, ':') + 1)) {
90 free(tmp);
91 return -1;
92 }
93 *strchr(tmp, ':') = '\0';
94 }
95 force_module = tmp;
96 free(tmp);
97 }
98
99 for (const auto & d : decomps) {
100 // if user has explicitly requested decoder, skip all others
101 if (!force_module.empty() && d.first != force_module) {
102 continue;
103 }
104 // first pass - find the one with best priority (least)
105 int priority = static_cast<const video_decompress_info *>(d.second)->get_decompress_priority(compression, internal_prop, out_pixfmt);
106 if (priority < 0) { // decoder not valid for given properties combination
107 continue;
108 }
109 if (priority <= prio_max && priority >= prio_min && priority < best_priority) {
110 best_priority = priority;
111 *vdi = static_cast<const video_decompress_info *>(d.second);
112 name = d.first;
113 }
114 }
115
116 if(best_priority == prio_max + 1)
117 return -1;
118 return best_priority;
119}
120
128{
129 struct state_decompress *s;
130
131 s = (struct state_decompress *) calloc(1, sizeof(struct state_decompress));
133 s->functions = vdi;
134 s->state = s->functions->init();
135 if (s->state == NULL) {
136 free(s);
137 return NULL;
138 }
139 return s;
140}
141
151 struct state_decompress **decompress_state, int substreams)
152{
153 for(int i = 0; i < substreams; ++i) {
154 decompress_state[i] = decompress_init(vdi);
155
156 if (!decompress_state[i]) {
157 for(int j = 0; j < i; ++j) {
158 if (decompress_state[j] != nullptr) {
159 decompress_done(decompress_state[j]);
160 }
161 decompress_state[j] = nullptr;
162 }
163 return false;
164 }
165 }
166
167 return true;
168}
169
184bool decompress_init_multi(codec_t compression, struct pixfmt_desc internal_prop, codec_t out_codec, struct state_decompress **out, int count)
185{
186 int prio_max = 1000;
187 int prio_min = 0;
188 int prio_cur;
189 const video_decompress_info *vdi = nullptr;
190
191 while(1) {
192 string name;
193 prio_cur = find_best_decompress(compression, internal_prop, out_codec,
194 prio_min, prio_max, &vdi, name);
195 // if found, init decoder
196 if(prio_cur != -1) {
197 if (try_initialize_decompress(vdi, out, count)) {
198 LOG(LOG_LEVEL_VERBOSE) << "Decompressor \"" << name << "\" initialized successfully.\n";
199 return true;
200 } else {
201 // failed, try to find another one
202 LOG(LOG_LEVEL_VERBOSE) << "Cannot initialize decompressor \"" << name << "\"!\n";
203 prio_min = prio_cur + 1;
204 continue;
205 }
206 } else {
207 break;
208 }
209 }
210 return false;
211}
212
214int decompress_reconfigure(struct state_decompress *s, struct video_desc desc, int rshift, int gshift, int bshift, int pitch, codec_t out_codec)
215{
216 assert(s->magic == DECOMPRESS_MAGIC);
217
218 return s->functions->reconfigure(s->state, desc, rshift, gshift, bshift, pitch, out_codec);
219}
220
223 struct state_decompress *s,
224 unsigned char *dst,
225 unsigned char *compressed,
226 unsigned int compressed_len,
227 int frame_seq,
228 struct video_frame_callbacks *callbacks,
229 struct pixfmt_desc *internal_prop)
230{
231 assert(s->magic == DECOMPRESS_MAGIC);
232
233 return s->functions->decompress(s->state,
234 dst,
235 compressed,
236 compressed_len,
237 frame_seq,
238 callbacks,
239 internal_prop);
240}
241
243int decompress_get_property(struct state_decompress *s, int property, void *val, size_t *len)
244{
245 return s->functions->get_property(s->state, property, val, len);
246}
247
250{
251 assert(s->magic == DECOMPRESS_MAGIC);
252
253 s->functions->done(s->state);
254 free(s);
255}
256
int void * val
struct video_desc desc
int void size_t * len
int property
codec_t get_codec_from_name(const char *name)
#define LOG_LEVEL_VERBOSE
Definition debug.h:63
#define LOG(level)
Definition debug.h:313
decompress_status
decompress_status decompress_frame(struct state_decompress *s, unsigned char *dst, unsigned char *compressed, unsigned int compressed_len, int frame_seq, struct video_frame_callbacks *callbacks, struct pixfmt_desc *internal_prop)
Decompresses video frame.
int decompress_get_property(struct state_decompress *s, int property, void *val, size_t *len)
void decompress_done(struct state_decompress *s)
int decompress_reconfigure(struct state_decompress *s, struct video_desc desc, int rshift, int gshift, int bshift, int pitch, codec_t out_codec)
bool decompress_init_multi(codec_t compression, struct pixfmt_desc internal_prop, codec_t out_codec, struct state_decompress **out, int count)
Initializes (multiple) decompress states.
int count
Definition hd-rum.c:44
std::unordered_map< std::string, std::string > commandline_params
Definition host.cpp:184
#define ADD_TO_PARAM(param, doc)
Definition host.h:203
map< string, const void * > get_libraries_for_class(enum library_class cls, int abi_version, bool include_hidden)
@ LIBRARY_CLASS_VIDEO_DECOMPRESS
Definition lib_common.h:81
const char * name
Definition settings.cpp:193
const struct video_decompress_info * functions
pointer to selected decoder functions
void * state
decoder driver state
uint32_t magic
selected decoder magic
decompress_get_priority_t get_decompress_priority
decompress_get_property_t get_property
decompress_reconfigure_t reconfigure
decompress_init_t init
decompress_done_t done
decompress_decompress_t decompress
Struct containing callbacks of a video_frame.
Definition types.h:230
codec_t
Definition types.h:65
static int find_best_decompress(codec_t compression, struct pixfmt_desc internal_prop, codec_t out_pixfmt, int prio_min, int prio_max, const struct video_decompress_info **vdi, string &name)
static bool try_initialize_decompress(const video_decompress_info *vdi, struct state_decompress **decompress_state, int substreams)
static struct state_decompress * decompress_init(const video_decompress_info *vdi)
#define DECOMPRESS_MAGIC
API for video decompress drivers.
#define VIDEO_DECOMPRESS_ABI_VERSION