-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcommon.cpp
More file actions
197 lines (158 loc) · 5.45 KB
/
common.cpp
File metadata and controls
197 lines (158 loc) · 5.45 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "common.h"
#include "config.h"
#include "memory.h"
#include "version.h"
namespace evx {
evx_status initialize_header(uint32 width, uint32 height, evx_header *header)
{
// Configure our header with default values.
header->magic[0] = 'E';
header->magic[1] = 'V';
header->magic[2] = 'X';
header->magic[3] = '1';
header->ref_count = EVX_REFERENCE_FRAME_COUNT;
header->version = EVX_VERSION_WORD(EVX_VERSION_MAJOR, EVX_VERSION_MINOR);
header->frame_width = width;
header->frame_height = height;
header->size = sizeof(evx_header);
return EVX_SUCCESS;
}
evx_status verify_header(const evx_header &header)
{
uint16 version = EVX_VERSION_WORD(EVX_VERSION_MAJOR, EVX_VERSION_MINOR);
if (header.magic[0] != 'E' || header.magic[1] != 'V' ||
header.magic[2] != 'X' || header.magic[3] != '1')
{
return EVX_ERROR_INVALID_RESOURCE;
}
if (header.version != version ||
header.ref_count != EVX_REFERENCE_FRAME_COUNT ||
header.size != sizeof(header))
{
return EVX_ERROR_INVALID_RESOURCE;
}
return EVX_SUCCESS;
}
evx_status clear_header(evx_header *header)
{
return initialize_header(0, 0, header);
}
evx_status clear_frame(evx_frame *frame)
{
if (EVX_PARAM_CHECK)
{
if (!frame)
{
return evx_post_error(EVX_ERROR_INVALIDARG);
}
}
frame->type = EVX_FRAME_INTRA;
frame->index = 0;
frame->quality = clip_range(EVX_DEFAULT_QUALITY_LEVEL, 1, 100);
return EVX_SUCCESS;
}
evx_status clear_block_desc(evx_block_desc *block_desc)
{
aligned_zero_memory(block_desc, sizeof(block_desc));
block_desc->block_type = EVX_BLOCK_INTRA_DEFAULT;
return EVX_SUCCESS;
}
evx_context::evx_context() : block_table(NULL)
{
}
evx_status initialize_context(uint32 width, uint32 height, evx_context *context)
{
if (EVX_PARAM_CHECK)
{
if (!context)
{
return evx_post_error(EVX_ERROR_INVALIDARG);
}
}
context->width_in_blocks = (width >> EVX_MACROBLOCK_SHIFT);
context->height_in_blocks = (height >> EVX_MACROBLOCK_SHIFT);
uint32 block_count = (context->width_in_blocks) * (context->height_in_blocks);
if (EVX_SUCCESS != context->cache_bank.input_cache.initialize(EVX_IMAGE_FORMAT_R16S, width, height))
{
clear_context(context);
return evx_post_error(EVX_ERROR_EXECUTION_FAILURE);
}
if (EVX_SUCCESS != context->cache_bank.transform_cache.initialize(EVX_IMAGE_FORMAT_R16S, EVX_MACROBLOCK_SIZE, EVX_MACROBLOCK_SIZE))
{
clear_context(context);
return evx_post_error(EVX_ERROR_EXECUTION_FAILURE);
}
if (EVX_SUCCESS != context->cache_bank.motion_cache.initialize(EVX_IMAGE_FORMAT_R16S, EVX_MACROBLOCK_SIZE, EVX_MACROBLOCK_SIZE))
{
clear_context(context);
return evx_post_error(EVX_ERROR_EXECUTION_FAILURE);
}
if (EVX_SUCCESS != context->cache_bank.output_cache.initialize(EVX_IMAGE_FORMAT_R16S, width, height))
{
clear_context(context);
return evx_post_error(EVX_ERROR_EXECUTION_FAILURE);
}
if (EVX_SUCCESS != context->cache_bank.staging_cache.initialize(EVX_IMAGE_FORMAT_R16S, EVX_MACROBLOCK_SIZE, EVX_MACROBLOCK_SIZE))
{
clear_context(context);
return evx_post_error(EVX_ERROR_EXECUTION_FAILURE);
}
for (uint32 i = 0; i < EVX_REFERENCE_FRAME_COUNT; ++i)
{
if (EVX_SUCCESS != context->cache_bank.prediction_cache[i].initialize(EVX_IMAGE_FORMAT_R16S, width, height))
{
clear_context(context);
return evx_post_error(EVX_ERROR_EXECUTION_FAILURE);
}
}
// Create block caches.
create_macroblock(context->cache_bank.transform_cache, 0, 0, &context->cache_bank.transform_block);
create_macroblock(context->cache_bank.motion_cache, 0, 0, &context->cache_bank.motion_block);
create_macroblock(context->cache_bank.staging_cache, 0, 0, &context->cache_bank.staging_block);
context->block_table = new evx_block_desc[block_count];
if (!context->block_table)
{
clear_context(context);
return evx_post_error(EVX_ERROR_EXECUTION_FAILURE);
}
aligned_zero_memory(context->block_table, sizeof(evx_block_desc) * block_count);
context->feed_stream.resize_capacity(32 * EVX_MB);
return EVX_SUCCESS;
}
evx_status clear_context(evx_context *context)
{
if (EVX_PARAM_CHECK)
{
if (!context)
{
return evx_post_error(EVX_ERROR_INVALIDARG);
}
}
context->cache_bank.input_cache.deinitialize();
context->cache_bank.output_cache.deinitialize();
context->cache_bank.transform_cache.deinitialize();
context->cache_bank.motion_cache.deinitialize();
context->cache_bank.staging_cache.deinitialize();
for (uint32 i = 0; i < EVX_REFERENCE_FRAME_COUNT; ++i)
{
context->cache_bank.prediction_cache[i].deinitialize();
}
delete [] context->block_table;
context->block_table = NULL;
context->feed_stream.clear();
context->arith_coder.clear();
return EVX_SUCCESS;
}
uint32 query_context_width(const evx_context &context)
{
return context.width_in_blocks << EVX_MACROBLOCK_SHIFT;
}
uint32 query_context_height(const evx_context &context)
{
return context.height_in_blocks << EVX_MACROBLOCK_SHIFT;
}
uint32 query_prediction_index_by_offset(const evx_frame &frame, uint8 offset)
{
return (frame.index + EVX_REFERENCE_FRAME_COUNT - offset) % EVX_REFERENCE_FRAME_COUNT;
}
} // namespace evx