UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
vf_split.cpp
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2011-2021 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
39#ifdef HAVE_CONFIG_H
40#include "config.h"
41#include "config_unix.h"
42#include "config_win32.h"
43#endif // HAVE_CONFIG_H
44
45#include <stdio.h>
46#include <string.h>
47#include "utils/vf_split.h"
48#include "video.h"
49#include "video_codec.h"
50
51void vf_split(struct video_frame *out, struct video_frame *src,
52 unsigned int x_count, unsigned int y_count, int preallocate)
53{
54 unsigned int tile_idx, line_idx;
55 struct tile *cur_tiles;
56 unsigned int tile_line = 0;
57 int out_linesize = 0;
58 int src_linesize;
59
60 out->color_spec = src->color_spec;
61 out->fps = src->fps;
62 //out->aux = src->aux | AUX_TILED;
63
64 assert(vf_get_tile(src, 0)->width % x_count == 0u && vf_get_tile(src, 0)->height % y_count == 0u);
65
66 src_linesize = vc_get_linesize(src->tiles[0].width,
67 src->color_spec);
68
69 assert(x_count * y_count > 0);
70 for(tile_idx = 0u; tile_idx < x_count * y_count; ++tile_idx) {
71 out->tiles[tile_idx].width = vf_get_tile(src, 0)->width / x_count;
72 out->tiles[tile_idx].height = vf_get_tile(src, 0)->height / y_count;
73
74 out_linesize = vc_get_linesize(out->tiles[tile_idx].width,
75 src->color_spec);
76 out->tiles[tile_idx].data_len = out_linesize * out->tiles[tile_idx].height;
77 }
78
79 cur_tiles = &out->tiles[0];
80 for(line_idx = 0u; line_idx < vf_get_tile(src, 0)->height; ++line_idx, ++tile_line) {
81 unsigned int cur_tile_idx;
82 unsigned int byte = 0u;
83
84 if (line_idx % (vf_get_tile(src, 0)->height / y_count) == 0u) /* next tiles*/
85 {
86 tile_line = 0u;
87 if (line_idx != 0u)
88 cur_tiles += x_count;
89 if (preallocate) {
90 for (cur_tile_idx = 0u; cur_tile_idx < x_count;
91 ++cur_tile_idx) {
92 cur_tiles[cur_tile_idx].data = (char *)
93 malloc(cur_tiles[cur_tile_idx].
94 data_len);
95 }
96 }
97 }
98
99 for(cur_tile_idx = 0u; cur_tile_idx < x_count; ++cur_tile_idx) {
100 memcpy((void *) &cur_tiles[cur_tile_idx].data[
101 tile_line *
102 out_linesize],
103 (void *) &src->tiles[0].data[line_idx *
104 src_linesize + byte],
105 cur_tiles[cur_tile_idx].width *
106 get_bpp(src->color_spec));
107 byte += cur_tiles[cur_tile_idx].width * get_bpp(src->color_spec);
108 }
109 }
110}
111
112using namespace std;
113
114vector<shared_ptr<video_frame>> vf_separate_tiles(shared_ptr<video_frame> frame)
115{
116 vector<shared_ptr<video_frame>> ret(frame->tile_count);
118 desc.tile_count = 1;
119
120 for (unsigned int i = 0; i < frame->tile_count; ++i) {
121 auto holder = new shared_ptr<video_frame>(frame);
122 ret[i] = shared_ptr<video_frame>(vf_alloc_desc(desc), [holder](struct video_frame *frame) {
123 delete holder;
124 vf_free(frame);
125 });
126
127 ret[i]->tiles[0].data_len = frame->tiles[i].data_len;
128 ret[i]->tiles[0].data = frame->tiles[i].data;
129 vf_copy_metadata(ret[i].get(), frame.get());
130 }
131
132 return ret;
133}
134
135shared_ptr<video_frame> vf_merge_tiles(std::vector<shared_ptr<video_frame>> const & tiles)
136{
137 struct video_desc desc = video_desc_from_frame(tiles[0].get());
138 desc.tile_count = tiles.size();
139
140 auto holder = new decay<decltype(tiles)>::type(tiles);
141 shared_ptr<video_frame> ret(vf_alloc_desc(desc),
142 [holder](struct video_frame *frame) {
143 delete holder;
144 vf_free(frame);
145 });
146
147 int64_t t0 = INT64_MAX;
148 int64_t t1 = 0;
149 for (unsigned int i = 0; i < tiles.size(); ++i) {
150 ret->tiles[i].data = tiles[i]->tiles[0].data;
151 ret->tiles[i].data_len = tiles[i]->tiles[0].data_len;
152 t0 = tiles[i]->compress_start < t0 ? tiles[i]->compress_start : t0;
153 t1 = tiles[i]->compress_end > t1 ? tiles[i]->compress_end : t1;
154 }
155 vf_copy_metadata(ret.get(), tiles.at(0).get());
156 ret->compress_start = t0;
157 ret->compress_end = t1;
158
159 return ret;
160}
161
struct video_desc desc
struct video_frame * frame
void vf_free(struct video_frame *buf)
Frees video_frame structure.
struct video_desc video_desc_from_frame(const struct video_frame *frame)
Returns struct video_desc from video frame.
int vc_get_linesize(unsigned int width, codec_t codec)
struct video_frame * vf_alloc_desc(struct video_desc desc)
Allocates video frame accordig given desc.
Option::OptType type
Definition settings.cpp:194
Struct tile is an area of video_frame.
Definition types.h:279
unsigned int width
Definition types.h:280
char * data
Raw tile data. Pointer must be at least 4B aligned.
Definition types.h:287
unsigned int data_len
length of the data
Definition types.h:288
unsigned int height
Definition types.h:281
unsigned int tile_count
Definition types.h:185
Struct video_frame represents a video frame and contains video description.
Definition types.h:305
struct tile tiles[FLEXIBLE_ARRAY_MEMBER]
Definition types.h:345
double fps
Definition types.h:308
unsigned int tile_count
Definition types.h:344
codec_t color_spec
Definition types.h:306
void vf_split(struct video_frame *out, struct video_frame *src, unsigned int x_count, unsigned int y_count, int preallocate)
Definition vf_split.cpp:51
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
This is an umbrella header for video functions.
double get_bpp(codec_t codec)
struct tile * vf_get_tile(struct video_frame *buf, int pos)
Returns pointer to n-th tile from video frame Equivalent to &video_frame::tiles[pos].
void vf_copy_metadata(struct video_frame *dest, const struct video_frame *src)