-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathmfx_c2_utils.h
More file actions
executable file
·448 lines (362 loc) · 13.5 KB
/
mfx_c2_utils.h
File metadata and controls
executable file
·448 lines (362 loc) · 13.5 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright (c) 2017-2021 Intel Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include "mfx_defs.h"
#include "mfx_c2_defs.h"
#include <C2Buffer.h>
#include <C2Param.h>
#include <fstream>
c2_status_t MfxStatusToC2(mfxStatus mfx_status);
inline mfxU64 TimestampC2ToMfx(uint64_t timestamp)
{
return timestamp * 90000 / MFX_SECOND_NS;
}
c2_status_t GetC2ConstGraphicBlock(
const C2FrameData& buf_pack, std::unique_ptr<C2ConstGraphicBlock>* c_graph_block);
c2_status_t GetC2ConstLinearBlock(
const C2FrameData& buf_pack, std::unique_ptr<C2ConstLinearBlock>* c_lin_block);
c2_status_t MapConstGraphicBlock(const C2ConstGraphicBlock& graph_block, c2_nsecs_t timeout,
std::unique_ptr<const C2GraphicView>* graph_view);
c2_status_t MapGraphicBlock(C2GraphicBlock& graph_block, c2_nsecs_t timeout,
std::unique_ptr<C2GraphicView>* graph_view);
c2_status_t MapConstLinearBlock(const C2ConstLinearBlock& block, c2_nsecs_t timeout,
std::unique_ptr<C2ReadView>* read_view);
c2_status_t MapLinearBlock(C2LinearBlock& block, c2_nsecs_t timeout,
std::unique_ptr<C2WriteView>* write_view);
std::shared_ptr<C2Buffer> CreateGraphicBuffer(
const std::shared_ptr<C2GraphicBlock> &block, const C2Rect &crop);
template<typename ParamType>
C2ParamFieldValues MakeC2ParamField()
{
ParamType p; // have to instantiate param here as C2ParamField constructor demands this
return C2ParamFieldValues { C2ParamField(&p, &p.value), nullptr };
}
std::unique_ptr<C2SettingResult> MakeC2SettingResult(
const C2ParamField& param_field,
C2SettingResult::Failure failure,
std::vector<C2ParamFieldValues>&& conflicting_fields = {},
const C2FieldSupportedValues* supported_values = nullptr);
c2_status_t GetAggregateStatus(std::vector<std::unique_ptr<C2SettingResult>>* const failures);
bool FindC2Param(
const std::vector<std::shared_ptr<C2ParamDescriptor>>& params_desc,
C2Param::Index param_index);
std::unique_ptr<C2SettingResult> FindC2Param(
const std::vector<std::shared_ptr<C2ParamDescriptor>>& params_desc,
const C2Param* param);
template<typename T, typename ...Args>
std::unique_ptr<T> AllocUniqueString(const Args(&... args), const char *value)
{
size_t len = strlen(value) + 1;
std::unique_ptr<T> res = T::AllocUnique(len, args...);
strcpy(res->m.value, value);
return res;
}
bool AvcProfileAndroidToMfx(C2Config::profile_t android_value, mfxU16* mfx_value);
bool AvcProfileMfxToAndroid(mfxU16 mfx_value, C2Config::profile_t* android_value);
bool AvcLevelAndroidToMfx(C2Config::level_t android_value, mfxU16* mfx_value);
bool AvcLevelMfxToAndroid(mfxU16 mfx_value, C2Config::level_t* android_value);
bool HevcProfileAndroidToMfx(C2Config::profile_t android_value, mfxU16* mfx_value);
bool HevcProfileMfxToAndroid(mfxU16 mfx_value, C2Config::profile_t* android_value);
bool HevcLevelAndroidToMfx(C2Config::level_t android_value, mfxU16* mfx_value);
bool HevcLevelMfxToAndroid(mfxU16 mfx_value, C2Config::level_t* android_value);
bool Vp9ProfileAndroidToMfx(C2Config::profile_t android_value, mfxU16* mfx_value);
bool Vp9ProfileMfxToAndroid(mfxU16 mfx_value, C2Config::profile_t* android_value);
void InitNV12PlaneLayout(uint32_t pitches[C2PlanarLayout::MAX_NUM_PLANES], C2PlanarLayout* layout);
void InitNV12PlaneData(int32_t pitch_y, int32_t alloc_height, uint8_t* base, uint8_t** plane_data);
int MfxFourCCToGralloc(mfxU32 fourcc, bool using_video_memory = true);
bool IsYUV420(const C2GraphicView &view);
bool IsNV12(const C2GraphicView &view);
bool IsI420(const C2GraphicView &view);
bool IsYV12(const C2GraphicView &view);
void ParseGop(const std::shared_ptr<C2StreamGopTuning::output> gop,
uint32_t &syncInterval, uint32_t &iInterval, uint32_t &maxBframes);
inline mfxU16 av1_mfx_profile_to_native_profile(mfxU16 profile)
{
switch (profile)
{
case MFX_PROFILE_AV1_MAIN: return 0;
case MFX_PROFILE_AV1_HIGH: return 1;
case MFX_PROFILE_AV1_PRO: return 2;
default: return 0;
}
}
// Gives access to prorected constructors of C2Buffer.
class C2BufferAccessor : public C2Buffer
{
using C2Buffer::C2Buffer;
friend C2Buffer MakeC2Buffer(const std::vector<C2ConstLinearBlock>& blocks);
friend C2Buffer MakeC2Buffer(const std::vector<C2ConstGraphicBlock>& blocks);
};
inline C2Buffer MakeC2Buffer(const std::vector<C2ConstLinearBlock>& blocks)
{
return C2BufferAccessor(blocks);
}
inline C2Buffer MakeC2Buffer(const std::vector<C2ConstGraphicBlock>& blocks)
{
return C2BufferAccessor(blocks);
}
bool operator==(const C2PlaneInfo& p0, const C2PlaneInfo& p1);
bool operator==(const C2PlanarLayout& src, const C2PlanarLayout& dst);
c2_status_t CopyGraphicView(const C2GraphicView* src, C2GraphicView* dst);
std::string FormatHex(const uint8_t* data, size_t len);
// Writes binary buffers to file.
class BinaryWriter
{
public:
// File named <name> is created/overwritten in: dir/<sub_dirs[0]>/.../<sub_dirs[N-1]>
// Folders are created if missing.
// So if a file with path Dir/SubDir1/SubDir2/File.txt is supposed to be written,
// then it should be passed as: BinaryWriter( "Dir", { "SubDir1", "SubDir2" }, "File.txt")
BinaryWriter(const std::string& dir,
const std::vector<std::string>& sub_dirs, const std::string& name);
public:
void Write(const uint8_t* data, size_t length)
{
stream_.write((const char*)data, length);
}
private:
std::ofstream stream_;
};
// Writes YUV format frame to file.It useful for debug.
class YUVWriter
{
public:
YUVWriter(const std::string& dir,
const std::vector<std::string>& sub_dirs, const std::string& name);
//TODO:
//Only support NV12 for now.
void Write(const uint8_t* yuv_data, int stride, int height, int frameIndex/*, int fourCC*/);
private:
std::ofstream stream_;
};
//declare used extension buffers
template<class T>
struct mfx_ext_buffer_id{};
template<>struct mfx_ext_buffer_id<mfxExtCodingOption> {
enum {id = MFX_EXTBUFF_CODING_OPTION};
};
template<>struct mfx_ext_buffer_id<mfxExtCodingOptionSPSPPS> {
enum {id = MFX_EXTBUFF_CODING_OPTION_SPSPPS};
};
template<>struct mfx_ext_buffer_id<mfxExtCodingOptionVPS> {
enum {id = MFX_EXTBUFF_CODING_OPTION_VPS};
};
template<>struct mfx_ext_buffer_id<mfxExtVP9Param> {
enum {id = MFX_EXTBUFF_VP9_PARAM};
};
template<>struct mfx_ext_buffer_id<mfxExtVideoSignalInfo> {
enum {id = MFX_EXTBUFF_VIDEO_SIGNAL_INFO };
};
template<>struct mfx_ext_buffer_id<mfxExtEncoderResetOption> {
enum {id = MFX_EXTBUFF_ENCODER_RESET_OPTION };
};
template <typename R>
struct ExtParamAccessor
{
private:
using mfxExtBufferDoublePtr = mfxExtBuffer**;
public:
mfxU16& NumExtParam;
mfxExtBufferDoublePtr& ExtParam;
ExtParamAccessor(const R& r):
NumExtParam(const_cast<mfxU16&>(r.NumExtParam)),
ExtParam(const_cast<mfxExtBufferDoublePtr&>(r.ExtParam)) {}
};
/** ExtBufHolder is an utility class which
* provide interface for mfxExtBuffer objects management in any mfx structure (e.g. mfxVideoParam)
*/
template<typename T>
class ExtBufHolder : public T
{
public:
ExtBufHolder() : T()
{
m_ext_buf.reserve(g_max_num_ext_buffers);
}
~ExtBufHolder() // only buffers allocated by wrapper can be released
{
for (auto it = m_ext_buf.begin(); it != m_ext_buf.end(); it++ )
{
delete [] (mfxU8*)(*it);
}
}
ExtBufHolder(const ExtBufHolder& ref)
{
m_ext_buf.reserve(g_max_num_ext_buffers);
*this = ref; // call to operator=
}
ExtBufHolder& operator=(const ExtBufHolder& ref)
{
const T* src_base = &ref;
return operator=(*src_base);
}
ExtBufHolder(const T& ref)
{
*this = ref; // call to operator=
}
ExtBufHolder& operator=(const T& ref)
{
// copy content of main structure type T
T* dst_base = this;
const T* src_base = &ref;
*dst_base = *src_base;
//remove all existing extension buffers
ClearBuffers();
const auto ref_ = ExtParamAccessor<T>(ref);
//reproduce list of extension buffers and copy its content
for (size_t i = 0; i < ref_.NumExtParam; ++i)
{
const auto src_buf = ref_.ExtParam[i];
if (!src_buf) throw std::range_error("Null pointer attached to source ExtParam");
if (!IsCopyAllowed(src_buf->BufferId))
{
auto msg = "Deep copy of '" + Fourcc2Str(src_buf->BufferId) + "' extBuffer is not allowed";
throw std::system_error(msg);
}
// 'false' below is because here we just copy extBuffer's one by one
auto dst_buf = AddExtBuffer(src_buf->BufferId, src_buf->BufferSz, false);
// copy buffer content w/o restoring its type
memcpy((void*)dst_buf, (void*)src_buf, src_buf->BufferSz);
}
return *this;
}
ExtBufHolder(ExtBufHolder &&) = default;
ExtBufHolder & operator= (ExtBufHolder&&) = default;
// Always returns a valid pointer or throws an exception
template<typename TB>
TB* AddExtBuffer()
{
mfxExtBuffer* b = AddExtBuffer(mfx_ext_buffer_id<TB>::id, sizeof(TB));
return (TB*)b;
}
template<typename TB>
void RemoveExtBuffer()
{
auto it = std::find_if(m_ext_buf.begin(), m_ext_buf.end(), CmpExtBufById(mfx_ext_buffer_id<TB>::id));
if (it != m_ext_buf.end())
{
delete [] (mfxU8*)(*it);
it = m_ext_buf.erase(it);
RefreshBuffers();
}
}
template <typename TB>
TB* GetExtBuffer(uint32_t fieldId = 0) const
{
return (TB*)FindExtBuffer(mfx_ext_buffer_id<TB>::id, fieldId);
}
template <typename TB>
operator TB*()
{
return (TB*)FindExtBuffer(mfx_ext_buffer_id<TB>::id, 0);
}
template <typename TB>
operator TB*() const
{
return (TB*)FindExtBuffer(mfx_ext_buffer_id<TB>::id, 0);
}
private:
mfxExtBuffer* AddExtBuffer(mfxU32 id, mfxU32 size)
{
if (!size || !id)
throw std::range_error("AddExtBuffer: wrong size or id!");
auto it = std::find_if(m_ext_buf.begin(), m_ext_buf.end(), CmpExtBufById(id));
if (it == m_ext_buf.end())
{
auto buf = (mfxExtBuffer*)new mfxU8[size];
memset(buf, 0, size);
m_ext_buf.push_back(buf);
buf->BufferId = id;
buf->BufferSz = size;
RefreshBuffers();
return m_ext_buf.back();
}
return *it;
}
mfxExtBuffer* FindExtBuffer(mfxU32 id, uint32_t fieldId) const
{
auto it = std::find_if(m_ext_buf.begin(), m_ext_buf.end(), CmpExtBufById(id));
if (fieldId && it != m_ext_buf.end())
{
++it;
return it != m_ext_buf.end() ? *it : nullptr;
}
return it != m_ext_buf.end() ? *it : nullptr;
}
void RefreshBuffers()
{
auto this_ = ExtParamAccessor<T>(*this);
this_.NumExtParam = static_cast<mfxU16>(m_ext_buf.size());
this_.ExtParam = this_.NumExtParam ? m_ext_buf.data() : nullptr;
}
void ClearBuffers()
{
if (m_ext_buf.size())
{
for (auto it = m_ext_buf.begin(); it != m_ext_buf.end(); it++ )
{
delete [] (mfxU8*)(*it);
}
m_ext_buf.clear();
}
RefreshBuffers();
}
bool IsCopyAllowed(mfxU32 id)
{
// TODO: Epand the list when necessery
static const mfxU32 allowed[] = {
MFX_EXTBUFF_CODING_OPTION,
MFX_EXTBUFF_CODING_OPTION2,
MFX_EXTBUFF_CODING_OPTION3,
MFX_EXTBUFF_HEVC_PARAM,
MFX_EXTBUFF_VP9_PARAM,
MFX_EXTBUFF_VIDEO_SIGNAL_INFO,
MFX_EXTBUFF_ENCODER_RESET_OPTION
};
auto it = std::find_if(std::begin(allowed), std::end(allowed),
[&id](const mfxU32 allowed_id)
{
return allowed_id == id;
});
return it != std::end(allowed);
}
struct CmpExtBufById
{
mfxU32 id;
CmpExtBufById(mfxU32 _id)
: id(_id)
{ };
bool operator () (mfxExtBuffer* b)
{
return (b && b->BufferId == id);
};
};
static std::string Fourcc2Str(mfxU32 fourcc)
{
std::string s;
for (size_t i = 0; i < 4; i++)
{
s.push_back(*(i + (char*)&fourcc));
}
return s;
}
std::vector<mfxExtBuffer*> m_ext_buf;
};
using MfxVideoParamsWrapper = ExtBufHolder<mfxVideoParam>;