This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
/
vaapiencoder_vp9.cpp
401 lines (326 loc) · 11.5 KB
/
vaapiencoder_vp9.cpp
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
/*
* Copyright (C) 2016 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "vaapiencoder_vp9.h"
#include "common/scopedlogger.h"
#include "common/common_def.h"
#include "vaapi/vaapicontext.h"
#include "vaapi/vaapidisplay.h"
#include "vaapicodedbuffer.h"
#include "vaapiencpicture.h"
#include <algorithm>
namespace YamiMediaCodec {
// each frame can have at most 3 reference frames, golden, alt and last
enum maxSizeValues {
kMaxReferenceFrames = 8,
kMaxWidth = 4096,
kMaxHeight = kMaxWidth,
kMaxHeaderSize=kMaxWidth
};
enum VP9QPValues { kMinQPValue = 9, kDefaultQPValue = 60, kMaxQPValue = 127 };
enum VP9FrameType { kKeyFrame, kInterFrame };
enum VP9LevelValues { kDefaultSharpnessLevel, kDefaultFilterLevel = 10 };
class VaapiEncPictureVP9 : public VaapiEncPicture {
public:
VaapiEncPictureVP9(const ContextPtr& context, const SurfacePtr& surface,
int64_t timeStamp)
: VaapiEncPicture(context, surface, timeStamp)
{
return;
}
VAGenericID getCodedBufferID() { return m_codedBuffer->getID(); }
};
VaapiEncoderVP9::VaapiEncoderVP9()
: m_frameCount(0)
{
m_videoParamCommon.profile = VAProfileVP9Profile0;
m_videoParamCommon.rcParams.minQP = kMinQPValue;
m_videoParamCommon.rcParams.maxQP = kMaxQPValue;
m_videoParamCommon.rcParams.initQP = kDefaultQPValue;
// add extra surfaces to operate due to kMaxReferenceFrames
// vaapi_encoder class will create 5 extra surfaces already
m_maxOutputBuffer = kMaxReferenceFrames;
}
VaapiEncoderVP9::~VaapiEncoderVP9() {}
YamiStatus VaapiEncoderVP9::getMaxOutSize(uint32_t* maxSize)
{
FUNC_ENTER();
*maxSize = m_maxCodedbufSize;
return YAMI_SUCCESS;
}
YamiStatus VaapiEncoderVP9::resetParams()
{
// intel driver cannot handle non 8-bit aligned resolutions, once
// fixed this can go away
if (width() != ALIGN8(width()) || height() != ALIGN8(height())) {
ERROR("Input resolution %dx%d is not 8-bit aligned", width(), height());
return YAMI_INVALID_PARAM;
}
m_maxCodedbufSize = width() * height() * 3 / 2;
// adding extra padding. In particular small resolutions require more
// space depending on other quantization parameters during execution. The
// value below is a good compromise
m_maxCodedbufSize += kMaxHeaderSize;
if (ipPeriod() == 0)
m_videoParamCommon.intraPeriod = 1;
return YAMI_SUCCESS;
}
YamiStatus VaapiEncoderVP9::start()
{
YamiStatus status;
FUNC_ENTER();
status = resetParams();
if (status != YAMI_SUCCESS)
return status;
return VaapiEncoderBase::start();
}
void VaapiEncoderVP9::flush()
{
FUNC_ENTER();
m_frameCount = 0;
m_reference.clear();
VaapiEncoderBase::flush();
}
YamiStatus VaapiEncoderVP9::stop()
{
flush();
return VaapiEncoderBase::stop();
}
YamiStatus VaapiEncoderVP9::setParameters(VideoParamConfigType type,
Yami_PTR videoEncParams)
{
YamiStatus status = YAMI_INVALID_PARAM;
if (!videoEncParams)
return YAMI_INVALID_PARAM;
switch (type) {
case VideoParamsTypeVP9: {
VideoParamsVP9* vp9Params = (VideoParamsVP9*)videoEncParams;
PARAMETER_ASSIGN(m_videoParamsVP9, *vp9Params);
status = YAMI_SUCCESS;
} break;
default:
status = VaapiEncoderBase::setParameters(type, videoEncParams);
break;
}
return status;
}
YamiStatus VaapiEncoderVP9::getParameters(VideoParamConfigType type,
Yami_PTR videoEncParams)
{
YamiStatus status = YAMI_INVALID_PARAM;
if (!videoEncParams)
return status;
switch (type) {
case VideoParamsTypeVP9: {
VideoParamsVP9* vp9Params = (VideoParamsVP9*)videoEncParams;
PARAMETER_ASSIGN(*vp9Params, m_videoParamsVP9);
status = YAMI_SUCCESS;
} break;
default:
status = VaapiEncoderBase::getParameters(type, videoEncParams);
break;
}
return status;
}
YamiStatus VaapiEncoderVP9::doEncode(const SurfacePtr& surface,
uint64_t timeStamp, bool forceKeyFrame)
{
YamiStatus ret;
if (!surface)
return YAMI_INVALID_PARAM;
PicturePtr picture(new VaapiEncPictureVP9(m_context, surface, timeStamp));
if (!(m_frameCount % keyFramePeriod()) || forceKeyFrame)
picture->m_type = VAAPI_PICTURE_I;
else
picture->m_type = VAAPI_PICTURE_P;
m_frameCount++;
CodedBufferPtr codedBuffer
= VaapiCodedBuffer::create(m_context, m_maxCodedbufSize);
if (!codedBuffer)
return YAMI_OUT_MEMORY;
picture->m_codedBuffer = codedBuffer;
codedBuffer->setFlag(ENCODE_BUFFERFLAG_ENDOFFRAME);
if (picture->m_type == VAAPI_PICTURE_I) {
codedBuffer->setFlag(ENCODE_BUFFERFLAG_SYNCFRAME);
}
ret = encodePicture(picture);
if (ret != YAMI_SUCCESS) {
return ret;
}
output(picture);
return YAMI_SUCCESS;
}
bool VaapiEncoderVP9::fill(VAEncSequenceParameterBufferVP9* seqParam) const
{
seqParam->max_frame_width = kMaxWidth;
seqParam->max_frame_height = kMaxHeight;
seqParam->bits_per_second = bitRate();
seqParam->intra_period = intraPeriod();
seqParam->kf_min_dist = 1;
seqParam->kf_max_dist = intraPeriod();
return true;
}
// Fills in VA picture parameter buffer
bool VaapiEncoderVP9::fill(VAEncPictureParameterBufferVP9* picParam,
const PicturePtr& picture,
const SurfacePtr& surface)
{
picParam->reconstructed_frame = surface->getID();
picParam->coded_buf = picture->getCodedBufferID();
if (picture->m_type == VAAPI_PICTURE_I) {
for (uint32_t i = 0; i < kMaxReferenceFrames ; i++)
picParam->reference_frames[i] = VA_INVALID_SURFACE;
}
else {
picParam->pic_flags.bits.frame_type = kInterFrame;
ReferenceQueue::const_iterator it = m_reference.begin();
for (uint32_t i = 0; it != m_reference.end(); ++it, i++) {
picParam->reference_frames[i] = (*it)->getID();
DEBUG("reference frame[%d] 0x%x", i, (*it)->getID());
}
picParam->pic_flags.bits.frame_context_idx = 0;
// last/golden/alt is used as reference frame. L0 forward
picParam->ref_flags.bits.ref_frame_ctrl_l0 = 0x7;
// golden and alt are last KeyFrame
// last is last decoded frame
if (getReferenceMode()) {
// intel driver updates a new slot with every new frame and keeps
// the reference frames in a circular buffer, the buffer is defined
// with 8 slots but it is up to the application to use as many as
// desired. This scheme implements the use of 3 slots for
// last/gold/alt refs
m_currentReferenceIndex = (m_currentReferenceIndex + 1) % 3;
// set to refresh next slot with current reconstructed surface
picParam->refresh_frame_flags = 1 << m_currentReferenceIndex;
// assign the references on only 3 slots
picParam->ref_flags.bits.ref_arf_idx = m_currentReferenceIndex;
picParam->ref_flags.bits.ref_gf_idx = m_currentReferenceIndex + 1;
picParam->ref_flags.bits.ref_last_idx = m_currentReferenceIndex - 1;
if (m_currentReferenceIndex == 0) {
picParam->ref_flags.bits.ref_last_idx
= m_currentReferenceIndex + 2;
}
else if (m_currentReferenceIndex == 2) {
picParam->ref_flags.bits.ref_gf_idx
= m_currentReferenceIndex - 2;
}
}
else {
picParam->refresh_frame_flags = 0x01; // refresh last frame
picParam->ref_flags.bits.ref_last_idx = 0;
picParam->ref_flags.bits.ref_gf_idx = 1;
picParam->ref_flags.bits.ref_arf_idx = 2;
}
}
picParam->frame_width_src = width();
picParam->frame_height_src = height();
picParam->frame_width_dst = width();
picParam->frame_height_dst = height();
picParam->pic_flags.bits.show_frame = 1;
picParam->luma_ac_qindex = (initQP() >= minQP() && initQP() <= maxQP()) ? initQP() : kDefaultQPValue;
picParam->luma_dc_qindex_delta = 1;
picParam->chroma_ac_qindex_delta = 1;
picParam->chroma_dc_qindex_delta = 1;
picParam->filter_level = kDefaultFilterLevel;
picParam->sharpness_level = kDefaultSharpnessLevel;
return true;
}
bool VaapiEncoderVP9::fill(
VAEncMiscParameterTypeVP9PerSegmantParam* segParam) const
{
return true;
}
bool VaapiEncoderVP9::ensureSequence(const PicturePtr& picture)
{
if (picture->m_type != VAAPI_PICTURE_I)
return true;
VAEncSequenceParameterBufferVP9* seqParam;
if (!picture->editSequence(seqParam) || !fill(seqParam)) {
ERROR("failed to create sequence parameter buffer (SPS)");
return false;
}
return true;
}
bool VaapiEncoderVP9::ensurePicture(const PicturePtr& picture,
const SurfacePtr& surface)
{
VAEncPictureParameterBufferVP9* picParam;
if (!picture->editPicture(picParam) || !fill(picParam, picture, surface)) {
ERROR("failed to create picture parameter buffer (PPS)");
return false;
}
return true;
}
bool VaapiEncoderVP9::ensureQMatrix(const PicturePtr& picture)
{
VAEncMiscParameterTypeVP9PerSegmantParam* segmentParam;
if (picture->m_type != VAAPI_PICTURE_I)
return true;
if (!picture->editQMatrix(segmentParam) || !fill(segmentParam)) {
ERROR("failed to create qMatrix");
return false;
}
return true;
}
bool VaapiEncoderVP9::referenceListUpdate(const PicturePtr& pic,
const SurfacePtr& recon)
{
if (pic->m_type == VAAPI_PICTURE_I) {
m_reference.clear();
m_reference.insert(m_reference.end(), kMaxReferenceFrames, recon);
if (getReferenceMode())
m_currentReferenceIndex = 0;
}
else {
if (getReferenceMode()) {
ReferenceQueue::iterator it
= m_reference.begin() + m_currentReferenceIndex;
(*it) = recon;
#if __ENABLE_DEBUG__
for (it = m_reference.begin(); it != m_reference.end(); ++it)
DEBUG("Update ref frames 0x%x", (*it)->getID());
#endif
}
else {
m_reference.pop_front();
m_reference.push_front(recon);
}
}
return true;
}
YamiStatus VaapiEncoderVP9::encodePicture(const PicturePtr& picture)
{
YamiStatus ret = YAMI_FAIL;
SurfacePtr reconstruct = createSurface();
if (!reconstruct)
return ret;
if (!ensureSequence(picture))
return ret;
if (!ensureQMatrix(picture))
return ret;
if (!ensureMiscParams(picture.get()))
return ret;
if (!ensurePicture(picture, reconstruct))
return ret;
if (!picture->encode())
return ret;
if (!referenceListUpdate(picture, reconstruct))
return ret;
return YAMI_SUCCESS;
}
}