UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
gpujpeg_test.cpp
Go to the documentation of this file.
1#ifdef HAVE_CONFIG_H
2#include "config.h"
3#include "config_unix.h"
4#include "config_win32.h"
5#endif
6
7extern "C" int gpujpeg_test_simple();
8
9#if defined HAVE_GPUJPEG
10#include <algorithm>
11#include <iostream>
12#include <memory>
13#include <string>
14#include <vector>
15
16#include "host.h"
17#include "module.h"
18#include "unit_common.h"
19#include "video.h"
20#include "video_compress.h"
21#include "video_decompress.h"
22
23using std::clog;
24using std::for_each;
25using std::max;
26using std::shared_ptr;
27using std::to_string;
28using std::vector;
29
30struct compress_state;
31struct state_decompress;
32
33static bool no_gpu = false;
34static compress_state *compress{nullptr};
35static state_decompress *decompress{nullptr};
36
37static void gpujpeg_test_teardown()
38{
39 module_done(CAST_MODULE(compress));
40 if (decompress != nullptr) {
41 decompress_done(decompress);
42 }
43}
44
45static pthread_once_t set_up = PTHREAD_ONCE_INIT;
46
47static void gpujpeg_test_setup()
48{
49 struct compress_state *compression;
50 int ret = compress_init(nullptr, "GPUJPEG:check", &compression);
51 if(ret >= 0) {
52 if(ret == 0) {
53 module_done(CAST_MODULE(compression));
54 }
55 } else {
56 clog << "Either GPUJPEG not compiled in or no CUDA-capable devices found - skipping GPUJPEG tests.\n";
57 no_gpu = true;
58 return;
59 }
60
61 ret = compress_init(nullptr, "GPUJPEG", &compress);
62 assert(ret >= 0 && "Compression initialization failed");
63
64 commandline_params["decompress"] = "gpujpeg";
65 atexit(gpujpeg_test_teardown);
66}
67
69{
70 using namespace std::string_literals;
71 pthread_once(&set_up, gpujpeg_test_setup);
72 if (no_gpu) {
73 return 1;
74 }
75
76 struct video_desc desc{1920, 1080, RGB, 1, PROGRESSIVE, 1};
77 auto in = shared_ptr<video_frame>(vf_alloc_desc_data(desc), vf_free);
78 memset(in->tiles[0].data, 127, in->tiles[0].data_len);
79
80 compress_frame(compress, in);
81 auto compressed = compress_pop(compress);
82 ASSERT_MESSAGE("Compression failed", compressed);
83
84 vector<unsigned char> decompressed(in->tiles[0].data_len);
85 auto comp_desc = desc;
86 comp_desc.color_spec = JPEG;
87 if (bool ret = decompress_init_multi(JPEG, pixfmt_desc{}, RGB, &decompress, 1)) {
88 ASSERT_MESSAGE("Decompression init failed", ret);
89 }
90 auto ret = decompress_reconfigure(decompress, comp_desc, 0, 8, 16, vc_get_linesize(desc.width, desc.color_spec), desc.color_spec);
91 ASSERT_MESSAGE("Decompression reconfiguration failed", ret == TRUE);
92 auto status = decompress_frame(decompress,
93 decompressed.data(),
94 reinterpret_cast<unsigned char *>(compressed->tiles[0].data),
95 compressed->tiles[0].data_len,
96 0,
97 nullptr,
98 nullptr);
99 ASSERT_MESSAGE("Decompression failed", status == DECODER_GOT_FRAME);
100
101 int i = 0;
102 int max_diff = 0;
103 for_each(decompressed.begin(), decompressed.end(), [&](unsigned char &x) {max_diff = max(abs(x - (unsigned char) in->tiles[0].data[i++]), max_diff);});
104 ASSERT_MESSAGE("Maximal allowed difference 1, found "s + to_string(max_diff), max_diff <= 1);
105 return 0;
106}
107#else
109{
110 return 1;
111}
112#endif // defined HAVE_GPUJPEG
struct video_desc desc
void vf_free(struct video_frame *buf)
Frees video_frame structure.
int vc_get_linesize(unsigned int width, codec_t codec)
struct video_frame * vf_alloc_desc_data(struct video_desc desc)
#define TRUE
int gpujpeg_test_simple()
void module_done(struct module *module_data)
Definition module.c:93
#define CAST_MODULE(x)
Definition module.h:197
shared_ptr< video_frame > compress_pop(struct compress_state *proxy)
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.
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.
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.
@ DECODER_GOT_FRAME
std::unordered_map< std::string, std::string > commandline_params
Definition host.cpp:184
Video compress state.
codec_t color_spec
Definition types.h:182
unsigned int width
width of every tile (!)
Definition types.h:179
@ PROGRESSIVE
progressive frame
Definition types.h:164
@ RGB
RGB 8-bit (packed into 24-bit word)
Definition types.h:80
@ JPEG
JPEG image, restart intervals may be used. Compatible with GPUJPEG.
Definition types.h:81
#define ASSERT_MESSAGE(msg, expr)
Definition unit_common.h:20
This is an umbrella header for video functions.
API for video compress drivers.
API for video decompress drivers.