-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathvf_split.cpp
More file actions
160 lines (138 loc) · 6.66 KB
/
Copy pathvf_split.cpp
File metadata and controls
160 lines (138 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* @file src/utils/vf_split.cpp
* @author Martin Pulec <pulec@cesnet.cz>
*/
/*
* Copyright (c) 2011-2025 CESNET
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of CESNET nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "utils/vf_split.h"
#include <cassert> // for assert
#include <cstdint> // for int64_t, INT64_MAX
#include <cstdlib> // for malloc
#include <cstring> // for memcpy
#include <type_traits> // for decay
#include "types.h" // for tile, video_frame, video_desc
#include "video_codec.h" // for get_bpp, vc_get_linesize
#include "video_frame.h" // for vf_get_tile, vf_alloc_desc, vf_copy_metadata
void vf_split(struct video_frame *out, struct video_frame *src,
unsigned int x_count, unsigned int y_count, int preallocate)
{
unsigned int tile_idx, line_idx;
struct tile *cur_tiles;
unsigned int tile_line = 0;
int out_linesize = 0;
int src_linesize;
out->color_spec = src->color_spec;
out->fps = src->fps;
//out->aux = src->aux | AUX_TILED;
assert(vf_get_tile(src, 0)->width % x_count == 0u && vf_get_tile(src, 0)->height % y_count == 0u);
src_linesize = vc_get_linesize(src->tiles[0].width,
src->color_spec);
assert(x_count * y_count > 0);
for(tile_idx = 0u; tile_idx < x_count * y_count; ++tile_idx) {
out->tiles[tile_idx].width = vf_get_tile(src, 0)->width / x_count;
out->tiles[tile_idx].height = vf_get_tile(src, 0)->height / y_count;
out_linesize = vc_get_linesize(out->tiles[tile_idx].width,
src->color_spec);
out->tiles[tile_idx].data_len = out_linesize * out->tiles[tile_idx].height;
}
cur_tiles = &out->tiles[0];
for(line_idx = 0u; line_idx < vf_get_tile(src, 0)->height; ++line_idx, ++tile_line) {
unsigned int cur_tile_idx;
unsigned int byte = 0u;
if (line_idx % (vf_get_tile(src, 0)->height / y_count) == 0u) /* next tiles*/
{
tile_line = 0u;
if (line_idx != 0u)
cur_tiles += x_count;
if (preallocate) {
for (cur_tile_idx = 0u; cur_tile_idx < x_count;
++cur_tile_idx) {
cur_tiles[cur_tile_idx].data = (char *)
malloc(cur_tiles[cur_tile_idx].
data_len);
}
}
}
for(cur_tile_idx = 0u; cur_tile_idx < x_count; ++cur_tile_idx) {
memcpy((void *) &cur_tiles[cur_tile_idx].data[
tile_line *
out_linesize],
(void *) &src->tiles[0].data[line_idx *
src_linesize + byte],
cur_tiles[cur_tile_idx].width *
get_bpp(src->color_spec));
byte += cur_tiles[cur_tile_idx].width * get_bpp(src->color_spec);
}
}
}
using namespace std;
vector<shared_ptr<video_frame>> vf_separate_tiles(shared_ptr<video_frame> frame)
{
vector<shared_ptr<video_frame>> ret(frame->tile_count);
struct video_desc desc = video_desc_from_frame(frame.get());
desc.tile_count = 1;
for (unsigned int i = 0; i < frame->tile_count; ++i) {
auto holder = new shared_ptr<video_frame>(frame);
ret[i] = shared_ptr<video_frame>(vf_alloc_desc(desc), [holder](struct video_frame *frame) {
delete holder;
vf_free(frame);
});
ret[i]->tiles[0].data_len = frame->tiles[i].data_len;
ret[i]->tiles[0].data = frame->tiles[i].data;
vf_copy_metadata(ret[i].get(), frame.get());
}
return ret;
}
shared_ptr<video_frame> vf_merge_tiles(std::vector<shared_ptr<video_frame>> const & tiles)
{
struct video_desc desc = video_desc_from_frame(tiles[0].get());
desc.tile_count = tiles.size();
auto holder = new decay<decltype(tiles)>::type(tiles);
shared_ptr<video_frame> ret(vf_alloc_desc(desc),
[holder](struct video_frame *frame) {
delete holder;
vf_free(frame);
});
int64_t t0 = INT64_MAX;
int64_t t1 = 0;
for (unsigned int i = 0; i < tiles.size(); ++i) {
ret->tiles[i].data = tiles[i]->tiles[0].data;
ret->tiles[i].data_len = tiles[i]->tiles[0].data_len;
t0 = tiles[i]->compress_start < t0 ? tiles[i]->compress_start : t0;
t1 = tiles[i]->compress_end > t1 ? tiles[i]->compress_end : t1;
}
vf_copy_metadata(ret.get(), tiles.at(0).get());
ret->compress_start = t0;
ret->compress_end = t1;
return ret;
}