| 1 |
/*############################################################################ |
| 2 |
# Copyright (C) 2020 Intel Corporation |
| 3 |
# |
| 4 |
# SPDX-License-Identifier: MIT |
| 5 |
############################################################################*/ |
| 6 |
|
| 7 |
#include <stdlib.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include "mfxdefs.h" |
| 12 |
#include "mfxvideo.h" |
| 13 |
#include "mfxmvc.h" |
| 14 |
|
| 15 |
/* These macro required for code compilation. */ |
| 16 |
#define INFINITE 0x7FFFFFFF |
| 17 |
#define UNUSED_PARAM(x) (void)(x) |
| 18 |
|
| 19 |
mfxVideoParam init_param; |
| 20 |
mfxFrameAllocRequest request; |
| 21 |
mfxStatus sts; |
| 22 |
mfxSession session; |
| 23 |
mfxFrameSurface1 *surface, *surface2; |
| 24 |
mfxBitstream *bits; |
| 25 |
mfxSyncPoint syncp; |
| 26 |
|
| 27 |
static void allocate_pool_of_frame_surfaces(int nFrames) |
| 28 |
{ |
| 29 |
UNUSED_PARAM(nFrames); |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
static int end_of_stream() |
| 34 |
{ |
| 35 |
return 1; |
| 36 |
} |
| 37 |
|
| 38 |
static void find_unlocked_surface_from_the_pool(mfxFrameSurface1 **pool) |
| 39 |
{ |
| 40 |
UNUSED_PARAM(pool); |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
static void fill_content_for_encoding(mfxFrameSurface1 *surface) |
| 45 |
{ |
| 46 |
UNUSED_PARAM(surface); |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
static void do_something_with_encoded_bits(mfxBitstream *bits) |
| 51 |
{ |
| 52 |
UNUSED_PARAM(bits); |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
static void free_pool_of_frame_surfaces() |
| 57 |
{ |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
/* end of internal stuff */ |
| 62 |
|
| 63 |
static void prg_encoding1 () { |
| 64 |
/*beg1*/ |
| 65 |
MFXVideoENCODE_QueryIOSurf(session, &init_param, &request); |
| 66 |
allocate_pool_of_frame_surfaces(request.NumFrameSuggested); |
| 67 |
MFXVideoENCODE_Init(session, &init_param); |
| 68 |
sts=MFX_ERR_MORE_DATA; |
| 69 |
for (;;) { |
| 70 |
if (sts==MFX_ERR_MORE_DATA && !end_of_stream()) { |
| 71 |
find_unlocked_surface_from_the_pool(&surface); |
| 72 |
fill_content_for_encoding(surface); |
| 73 |
} |
| 74 |
surface2=end_of_stream()?NULL:surface; |
| 75 |
sts=MFXVideoENCODE_EncodeFrameAsync(session,NULL,surface2,bits,&syncp); |
| 76 |
if (end_of_stream() && sts==MFX_ERR_MORE_DATA) break; |
| 77 |
// Skipped other error handling |
| 78 |
if (sts==MFX_ERR_NONE) { |
| 79 |
MFXVideoCORE_SyncOperation(session, syncp, INFINITE); |
| 80 |
do_something_with_encoded_bits(bits); |
| 81 |
} |
| 82 |
} |
| 83 |
MFXVideoENCODE_Close(session); |
| 84 |
free_pool_of_frame_surfaces(); |
| 85 |
/*end1*/ |
| 86 |
} |
| 87 |
|
| 88 |
static void prg_encoding2 () { |
| 89 |
/*beg2*/ |
| 90 |
MFXVideoENCODE_Init(session, &init_param); |
| 91 |
sts=MFX_ERR_MORE_DATA; |
| 92 |
for (;;) { |
| 93 |
if (sts==MFX_ERR_MORE_DATA && !end_of_stream()) { |
| 94 |
MFXMemory_GetSurfaceForEncode(session,&surface); |
| 95 |
fill_content_for_encoding(surface); |
| 96 |
} |
| 97 |
surface2=end_of_stream()?NULL:surface; |
| 98 |
sts=MFXVideoENCODE_EncodeFrameAsync(session,NULL,surface2,bits,&syncp); |
| 99 |
if (surface2) surface->FrameInterface->Release(surface2); |
| 100 |
if (end_of_stream() && sts==MFX_ERR_MORE_DATA) break; |
| 101 |
// Skipped other error handling |
| 102 |
if (sts==MFX_ERR_NONE) { |
| 103 |
MFXVideoCORE_SyncOperation(session, syncp, INFINITE); |
| 104 |
do_something_with_encoded_bits(bits); |
| 105 |
} |
| 106 |
} |
| 107 |
MFXVideoENCODE_Close(session); |
| 108 |
/*end2*/ |
| 109 |
} |
| 110 |
|
| 111 |
/*beg3*/ |
| 112 |
#include "mfxvideo.h" |
| 113 |
#include "mfxbrc.h" |
| 114 |
|
| 115 |
typedef struct { |
| 116 |
mfxU32 EncodedOrder; |
| 117 |
mfxI32 QP; |
| 118 |
mfxU32 MaxSize; |
| 119 |
mfxU32 MinSize; |
| 120 |
mfxU16 Status; |
| 121 |
mfxU64 StartTime; |
| 122 |
// ... skipped |
| 123 |
} MyBrcFrame; |
| 124 |
|
| 125 |
typedef struct { |
| 126 |
MyBrcFrame* frame_queue; |
| 127 |
mfxU32 frame_queue_size; |
| 128 |
mfxU32 frame_queue_max_size; |
| 129 |
mfxI32 max_qp[3]; //I,P,B |
| 130 |
mfxI32 min_qp[3]; //I,P,B |
| 131 |
// ... skipped |
| 132 |
} MyBrcContext; |
| 133 |
|
| 134 |
void* GetExtBuffer(mfxExtBuffer** ExtParam, mfxU16 NumExtParam, mfxU32 bufferID) |
| 135 |
{ |
| 136 |
int i=0; |
| 137 |
for(i = 0; i < NumExtParam; i++) { |
| 138 |
if(ExtParam[i]->BufferId == bufferID) return ExtParam[i]; |
| 139 |
} |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
|
| 143 |
static int IsParametersSupported(mfxVideoParam *par) |
| 144 |
{ |
| 145 |
UNUSED_PARAM(par); |
| 146 |
// do some checks |
| 147 |
return 1; |
| 148 |
} |
| 149 |
|
| 150 |
static int IsResetPossible(MyBrcContext* ctx, mfxVideoParam *par) |
| 151 |
{ |
| 152 |
UNUSED_PARAM(ctx); |
| 153 |
UNUSED_PARAM(par); |
| 154 |
// do some checks |
| 155 |
return 1; |
| 156 |
} |
| 157 |
|
| 158 |
static MyBrcFrame* GetFrame(MyBrcFrame *frame_queue, mfxU32 frame_queue_size, mfxU32 EncodedOrder) |
| 159 |
{ |
| 160 |
UNUSED_PARAM(EncodedOrder); |
| 161 |
//do some logic |
| 162 |
if(frame_queue_size) return &frame_queue[0]; |
| 163 |
return NULL; |
| 164 |
} |
| 165 |
|
| 166 |
static mfxU32 GetFrameCost(mfxU16 FrameType, mfxU16 PyramidLayer) |
| 167 |
{ |
| 168 |
UNUSED_PARAM(FrameType); |
| 169 |
UNUSED_PARAM(PyramidLayer); |
| 170 |
// calculate cost |
| 171 |
return 1; |
| 172 |
} |
| 173 |
|
| 174 |
static mfxU32 GetMinSize(MyBrcContext *ctx, mfxU32 cost) |
| 175 |
{ |
| 176 |
UNUSED_PARAM(ctx); |
| 177 |
UNUSED_PARAM(cost); |
| 178 |
// do some logic |
| 179 |
return 1; |
| 180 |
} |
| 181 |
|
| 182 |
static mfxU32 GetMaxSize(MyBrcContext *ctx, mfxU32 cost) |
| 183 |
{ |
| 184 |
UNUSED_PARAM(ctx); |
| 185 |
UNUSED_PARAM(cost); |
| 186 |
// do some logic |
| 187 |
return 1; |
| 188 |
} |
| 189 |
|
| 190 |
static mfxI32 GetInitQP(MyBrcContext *ctx, mfxU32 MinSize, mfxU32 MaxSize, mfxU32 cost) |
| 191 |
{ |
| 192 |
UNUSED_PARAM(ctx); |
| 193 |
UNUSED_PARAM(MinSize); |
| 194 |
UNUSED_PARAM(MaxSize); |
| 195 |
UNUSED_PARAM(cost); |
| 196 |
// do some logic |
| 197 |
return 1; |
| 198 |
} |
| 199 |
|
| 200 |
static mfxU64 GetTime() |
| 201 |
{ |
| 202 |
mfxU64 wallClock = 0xFFFF; |
| 203 |
return wallClock; |
| 204 |
} |
| 205 |
|
| 206 |
static void UpdateBRCState(mfxU32 CodedFrameSize, MyBrcContext *ctx) |
| 207 |
{ |
| 208 |
UNUSED_PARAM(CodedFrameSize); |
| 209 |
UNUSED_PARAM(ctx); |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
static void RemoveFromQueue(MyBrcFrame* frame_queue, mfxU32 frame_queue_size, MyBrcFrame* frame) |
| 214 |
{ |
| 215 |
UNUSED_PARAM(frame_queue); |
| 216 |
UNUSED_PARAM(frame_queue_size); |
| 217 |
UNUSED_PARAM(frame); |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
static mfxU64 GetMaxFrameEncodingTime(MyBrcContext *ctx) |
| 222 |
{ |
| 223 |
UNUSED_PARAM(ctx); |
| 224 |
return 2; |
| 225 |
} |
| 226 |
|
| 227 |
mfxStatus MyBrcInit(mfxHDL pthis, mfxVideoParam* par) { |
| 228 |
MyBrcContext* ctx = (MyBrcContext*)pthis; |
| 229 |
mfxI32 QpBdOffset; |
| 230 |
mfxExtCodingOption2* co2; |
| 231 |
mfxI32 defaultQP = 4; |
| 232 |
|
| 233 |
if (!pthis || !par) |
| 234 |
return MFX_ERR_NULL_PTR; |
| 235 |
|
| 236 |
if (!IsParametersSupported(par)) |
| 237 |
return MFX_ERR_UNSUPPORTED; |
| 238 |
|
| 239 |
ctx->frame_queue_max_size = par->AsyncDepth; |
| 240 |
ctx->frame_queue = (MyBrcFrame*)malloc(sizeof(MyBrcFrame) * ctx->frame_queue_max_size); |
| 241 |
|
| 242 |
if (!ctx->frame_queue) |
| 243 |
return MFX_ERR_MEMORY_ALLOC; |
| 244 |
|
| 245 |
co2 = (mfxExtCodingOption2*)GetExtBuffer(par->ExtParam, par->NumExtParam, MFX_EXTBUFF_CODING_OPTION2); |
| 246 |
QpBdOffset = (par->mfx.FrameInfo.BitDepthLuma > 8) ? (6 * (par->mfx.FrameInfo.BitDepthLuma - 8)) : 0; |
| 247 |
|
| 248 |
ctx->max_qp[0] = (co2 && co2->MaxQPI) ? (co2->MaxQPI - QpBdOffset) : defaultQP; |
| 249 |
ctx->min_qp[0] = (co2 && co2->MinQPI) ? (co2->MinQPI - QpBdOffset) : defaultQP; |
| 250 |
|
| 251 |
ctx->max_qp[1] = (co2 && co2->MaxQPP) ? (co2->MaxQPP - QpBdOffset) : defaultQP; |
| 252 |
ctx->min_qp[1] = (co2 && co2->MinQPP) ? (co2->MinQPP - QpBdOffset) : defaultQP; |
| 253 |
|
| 254 |
ctx->max_qp[2] = (co2 && co2->MaxQPB) ? (co2->MaxQPB - QpBdOffset) : defaultQP; |
| 255 |
ctx->min_qp[2] = (co2 && co2->MinQPB) ? (co2->MinQPB - QpBdOffset) : defaultQP; |
| 256 |
|
| 257 |
// skipped initialization of other other BRC parameters |
| 258 |
|
| 259 |
ctx->frame_queue_size = 0; |
| 260 |
|
| 261 |
return MFX_ERR_NONE; |
| 262 |
} |
| 263 |
|
| 264 |
mfxStatus MyBrcReset(mfxHDL pthis, mfxVideoParam* par) { |
| 265 |
MyBrcContext* ctx = (MyBrcContext*)pthis; |
| 266 |
|
| 267 |
if (!pthis || !par) |
| 268 |
return MFX_ERR_NULL_PTR; |
| 269 |
|
| 270 |
if (!IsParametersSupported(par)) |
| 271 |
return MFX_ERR_UNSUPPORTED; |
| 272 |
|
| 273 |
if (!IsResetPossible(ctx, par)) |
| 274 |
return MFX_ERR_INCOMPATIBLE_VIDEO_PARAM; |
| 275 |
|
| 276 |
// reset here BRC parameters if required |
| 277 |
|
| 278 |
return MFX_ERR_NONE; |
| 279 |
} |
| 280 |
|
| 281 |
mfxStatus MyBrcClose(mfxHDL pthis) { |
| 282 |
MyBrcContext* ctx = (MyBrcContext*)pthis; |
| 283 |
|
| 284 |
if (!pthis) |
| 285 |
return MFX_ERR_NULL_PTR; |
| 286 |
|
| 287 |
if (ctx->frame_queue) { |
| 288 |
free(ctx->frame_queue); |
| 289 |
ctx->frame_queue = NULL; |
| 290 |
ctx->frame_queue_max_size = 0; |
| 291 |
ctx->frame_queue_size = 0; |
| 292 |
} |
| 293 |
|
| 294 |
return MFX_ERR_NONE; |
| 295 |
} |
| 296 |
|
| 297 |
mfxStatus MyBrcGetFrameCtrl(mfxHDL pthis, mfxBRCFrameParam* par, mfxBRCFrameCtrl* ctrl) { |
| 298 |
MyBrcContext* ctx = (MyBrcContext*)pthis; |
| 299 |
MyBrcFrame* frame = NULL; |
| 300 |
mfxU32 cost; |
| 301 |
|
| 302 |
if (!pthis || !par || !ctrl) |
| 303 |
return MFX_ERR_NULL_PTR; |
| 304 |
|
| 305 |
if (par->NumRecode > 0) |
| 306 |
frame = GetFrame(ctx->frame_queue, ctx->frame_queue_size, par->EncodedOrder); |
| 307 |
else if (ctx->frame_queue_size < ctx->frame_queue_max_size) |
| 308 |
frame = &ctx->frame_queue[ctx->frame_queue_size++]; |
| 309 |
|
| 310 |
if (!frame) |
| 311 |
return MFX_ERR_UNDEFINED_BEHAVIOR; |
| 312 |
|
| 313 |
if (par->NumRecode == 0) { |
| 314 |
frame->EncodedOrder = par->EncodedOrder; |
| 315 |
cost = GetFrameCost(par->FrameType, par->PyramidLayer); |
| 316 |
frame->MinSize = GetMinSize(ctx, cost); |
| 317 |
frame->MaxSize = GetMaxSize(ctx, cost); |
| 318 |
frame->QP = GetInitQP(ctx, frame->MinSize, frame->MaxSize, cost); // from QP/size stat |
| 319 |
frame->StartTime = GetTime(); |
| 320 |
} |
| 321 |
|
| 322 |
ctrl->QpY = frame->QP; |
| 323 |
|
| 324 |
return MFX_ERR_NONE; |
| 325 |
} |
| 326 |
|
| 327 |
#define DEFAULT_QP_INC 4 |
| 328 |
#define DEFAULT_QP_DEC 4 |
| 329 |
|
| 330 |
mfxStatus MyBrcUpdate(mfxHDL pthis, mfxBRCFrameParam* par, mfxBRCFrameCtrl* ctrl, mfxBRCFrameStatus* status) { |
| 331 |
MyBrcContext* ctx = (MyBrcContext*)pthis; |
| 332 |
MyBrcFrame* frame = NULL; |
| 333 |
mfxU32 panic = 0; |
| 334 |
|
| 335 |
if (!pthis || !par || !ctrl || !status) |
| 336 |
return MFX_ERR_NULL_PTR; |
| 337 |
|
| 338 |
frame = GetFrame(ctx->frame_queue, ctx->frame_queue_size, par->EncodedOrder); |
| 339 |
if (!frame) |
| 340 |
return MFX_ERR_UNDEFINED_BEHAVIOR; |
| 341 |
|
| 342 |
// update QP/size stat here |
| 343 |
|
| 344 |
if ( frame->Status == MFX_BRC_PANIC_BIG_FRAME |
| 345 |
|| frame->Status == MFX_BRC_PANIC_SMALL_FRAME) |
| 346 |
panic = 1; |
| 347 |
|
| 348 |
if (panic || (par->CodedFrameSize >= frame->MinSize && par->CodedFrameSize <= frame->MaxSize)) { |
| 349 |
UpdateBRCState(par->CodedFrameSize, ctx); |
| 350 |
RemoveFromQueue(ctx->frame_queue, ctx->frame_queue_size, frame); |
| 351 |
ctx->frame_queue_size--; |
| 352 |
status->BRCStatus = MFX_BRC_OK; |
| 353 |
|
| 354 |
// Here update Min/MaxSize for all queued frames |
| 355 |
|
| 356 |
return MFX_ERR_NONE; |
| 357 |
} |
| 358 |
|
| 359 |
panic = ((GetTime() - frame->StartTime) >= GetMaxFrameEncodingTime(ctx)); |
| 360 |
|
| 361 |
if (par->CodedFrameSize > frame->MaxSize) { |
| 362 |
if (panic || (frame->QP >= ctx->max_qp[0])) { |
| 363 |
frame->Status = MFX_BRC_PANIC_BIG_FRAME; |
| 364 |
} else { |
| 365 |
frame->Status = MFX_BRC_BIG_FRAME; |
| 366 |
frame->QP = DEFAULT_QP_INC; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
if (par->CodedFrameSize < frame->MinSize) { |
| 371 |
if (panic || (frame->QP <= ctx->min_qp[0])) { |
| 372 |
frame->Status = MFX_BRC_PANIC_SMALL_FRAME; |
| 373 |
status->MinFrameSize = frame->MinSize; |
| 374 |
} else { |
| 375 |
frame->Status = MFX_BRC_SMALL_FRAME; |
| 376 |
frame->QP = DEFAULT_QP_DEC; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
status->BRCStatus = frame->Status; |
| 381 |
|
| 382 |
return MFX_ERR_NONE; |
| 383 |
} |
| 384 |
|
| 385 |
void EncoderInit() |
| 386 |
{ |
| 387 |
//initialize encoder |
| 388 |
MyBrcContext brc_ctx; |
| 389 |
mfxExtBRC ext_brc; |
| 390 |
mfxExtCodingOption2 co2; |
| 391 |
mfxExtBuffer* ext_buf[2] = {&co2.Header, &ext_brc.Header}; |
| 392 |
mfxVideoParam vpar; |
| 393 |
|
| 394 |
memset(&brc_ctx, 0, sizeof(MyBrcContext)); |
| 395 |
memset(&ext_brc, 0, sizeof(mfxExtBRC)); |
| 396 |
memset(&co2, 0, sizeof(mfxExtCodingOption2)); |
| 397 |
|
| 398 |
vpar.ExtParam = ext_buf; |
| 399 |
vpar.NumExtParam = sizeof(ext_buf) / sizeof(ext_buf[0]); |
| 400 |
|
| 401 |
co2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2; |
| 402 |
co2.Header.BufferSz = sizeof(mfxExtCodingOption2); |
| 403 |
co2.ExtBRC = MFX_CODINGOPTION_ON; |
| 404 |
|
| 405 |
ext_brc.Header.BufferId = MFX_EXTBUFF_BRC; |
| 406 |
ext_brc.Header.BufferSz = sizeof(mfxExtBRC); |
| 407 |
ext_brc.pthis = &brc_ctx; |
| 408 |
ext_brc.Init = MyBrcInit; |
| 409 |
ext_brc.Reset = MyBrcReset; |
| 410 |
ext_brc.Close = MyBrcClose; |
| 411 |
ext_brc.GetFrameCtrl = MyBrcGetFrameCtrl; |
| 412 |
ext_brc.Update = MyBrcUpdate; |
| 413 |
|
| 414 |
sts = MFXVideoENCODE_Query(session, &vpar, &vpar); |
| 415 |
if (sts == MFX_ERR_UNSUPPORTED || co2.ExtBRC != MFX_CODINGOPTION_ON) |
| 416 |
// unsupported case |
| 417 |
sts = sts; |
| 418 |
else |
| 419 |
sts = MFXVideoENCODE_Init(session, &vpar); |
| 420 |
} |
| 421 |
/*end3*/ |
| 422 |
|
| 423 |
static void prg_encoding4 () { |
| 424 |
/*beg4*/ |
| 425 |
mfxExtBuffer *eb; |
| 426 |
mfxExtMVCSeqDesc seq_desc; |
| 427 |
mfxVideoParam init_param; |
| 428 |
|
| 429 |
init_param.ExtParam=(mfxExtBuffer **)&eb; |
| 430 |
init_param.NumExtParam=1; |
| 431 |
eb=(mfxExtBuffer *)&seq_desc; |
| 432 |
|
| 433 |
/* init encoder */ |
| 434 |
MFXVideoENCODE_Init(session, &init_param); |
| 435 |
|
| 436 |
/* perform encoding */ |
| 437 |
for (;;) { |
| 438 |
MFXVideoENCODE_EncodeFrameAsync(session, NULL, surface2, bits, |
| 439 |
&syncp); |
| 440 |
MFXVideoCORE_SyncOperation(session,syncp,INFINITE); |
| 441 |
} |
| 442 |
|
| 443 |
/* close encoder */ |
| 444 |
MFXVideoENCODE_Close(session); |
| 445 |
} |
| 446 |
/*end4*/ |
| 447 |
|
| 448 |
#include "mfxencodestats.h" |
| 449 |
|
| 450 |
static void AttachBuffer(mfxBitstream* bs, mfxExtEncodeStatsOutput* stats) |
| 451 |
{ |
| 452 |
UNUSED_PARAM(bs); |
| 453 |
UNUSED_PARAM(stats); |
| 454 |
return; |
| 455 |
} |
| 456 |
|
| 457 |
static void prg_encoding5() { |
| 458 |
/*beg5*/ |
| 459 |
mfxExtEncodeStatsOutput stats; |
| 460 |
stats.EncodeStatsFlags = MFX_ENCODESTATS_LEVEL_FRAME|MFX_ENCODESTATS_LEVEL_BLK; // or MFX_ENCODESTATS_LEVEL_DEFAULT can be used |
| 461 |
AttachBuffer(bits, &stats); |
| 462 |
|
| 463 |
/* perform encoding and gathering stats. */ |
| 464 |
sts = MFXVideoENCODE_EncodeFrameAsync(session, NULL, surface, bits, &syncp); |
| 465 |
if (sts == MFX_ERR_NONE) { |
| 466 |
/* to synchronize everything. */ |
| 467 |
MFXVideoCORE_SyncOperation(session, syncp, INFINITE); |
| 468 |
/* process stats */ |
| 469 |
|
| 470 |
/* release memory */ |
| 471 |
stats.EncodeStatsContainer->RefInterface.Release(&stats.EncodeStatsContainer->RefInterface); |
| 472 |
|
| 473 |
} |
| 474 |
|
| 475 |
} |
| 476 |
/*end5*/ |
| 477 |
|
| 478 |
static void prg_encoding6() { |
| 479 |
/*beg6*/ |
| 480 |
mfxExtEncodeStatsOutput stats_frame; |
| 481 |
mfxExtEncodeStatsOutput stats_blk; |
| 482 |
stats_frame.EncodeStatsFlags = MFX_ENCODESTATS_LEVEL_FRAME; |
| 483 |
stats_blk.EncodeStatsFlags = MFX_ENCODESTATS_LEVEL_BLK; |
| 484 |
AttachBuffer(bits, &stats_frame); |
| 485 |
AttachBuffer(bits, &stats_blk); |
| 486 |
|
| 487 |
/* perform encoding and gathering stats. */ |
| 488 |
sts = MFXVideoENCODE_EncodeFrameAsync(session, NULL, surface, bits, &syncp); |
| 489 |
|
| 490 |
if (MFX_ERR_NONE == sts) { |
| 491 |
/* to synchronize bitstream. */ |
| 492 |
stats_frame.EncodeStatsContainer->SynchronizeBitstream(&stats_frame.EncodeStatsContainer->RefInterface, INFINITE); |
| 493 |
|
| 494 |
/* to synchronize frame level statistics. */ |
| 495 |
stats_frame.EncodeStatsContainer->SynchronizeStatistics(&stats_frame.EncodeStatsContainer->RefInterface, INFINITE); |
| 496 |
|
| 497 |
/* to synchronize block level statistics. */ |
| 498 |
stats_blk.EncodeStatsContainer->SynchronizeStatistics(&stats_blk.EncodeStatsContainer->RefInterface, INFINITE); |
| 499 |
|
| 500 |
/* process stats */ |
| 501 |
|
| 502 |
stats_frame.EncodeStatsContainer->RefInterface.Release(&stats_frame.EncodeStatsContainer->RefInterface); |
| 503 |
stats_blk.EncodeStatsContainer->RefInterface.Release(&stats_blk.EncodeStatsContainer->RefInterface); |
| 504 |
|
| 505 |
} |
| 506 |
|
| 507 |
} |
| 508 |
/*end6*/ |
| 509 |
|
| 510 |
static void prg_encoding7() { |
| 511 |
/*beg7*/ |
| 512 |
mfxExtEncodeStatsOutput stats; |
| 513 |
stats.EncodeStatsFlags = MFX_ENCODESTATS_LEVEL_FRAME|MFX_ENCODESTATS_LEVEL_BLK; // or MFX_ENCODESTATS_LEVEL_DEFAULT can be used |
| 514 |
AttachBuffer(bits, &stats); |
| 515 |
|
| 516 |
/* perform encoding and gathering stats. */ |
| 517 |
sts = MFXVideoENCODE_EncodeFrameAsync(session, NULL, surface, bits, &syncp); |
| 518 |
if (MFX_ERR_NONE == sts) { |
| 519 |
/* to synchronize bitstream. */ |
| 520 |
stats.EncodeStatsContainer->SynchronizeBitstream(&stats.EncodeStatsContainer->RefInterface, INFINITE); |
| 521 |
|
| 522 |
/* to synchronize frame and block level statistics. */ |
| 523 |
stats.EncodeStatsContainer->SynchronizeStatistics(&stats.EncodeStatsContainer->RefInterface, INFINITE); |
| 524 |
|
| 525 |
/* process stats */ |
| 526 |
|
| 527 |
/* release memory */ |
| 528 |
stats.EncodeStatsContainer->RefInterface.Release(&stats.EncodeStatsContainer->RefInterface); |
| 529 |
} |
| 530 |
|
| 531 |
} |
| 532 |
/*end7*/ |
| 533 |
|
| 534 |
#if defined(LINUX32) || defined(LINUX64) |
| 535 |
|
| 536 |
#include<pthread.h> |
| 537 |
|
| 538 |
pthread_t pt[3]; |
| 539 |
|
| 540 |
static void* SynchronizeBitstream(void *args) |
| 541 |
{ |
| 542 |
mfxExtEncodeStats *stats = (mfxExtEncodeStatsOutput*)args; |
| 543 |
stats->EncodeStatsContainer->SynchronizeBitstream(&stats->EncodeStatsContainer->RefInterface, INFINITE); |
| 544 |
stats->EncodeStatsContainer->RefInterface.Release(&stats->EncodeStatsContainer->RefInterface); |
| 545 |
pthread_exit(NULL); |
| 546 |
return NULL; |
| 547 |
} |
| 548 |
|
| 549 |
static void* SynchronizeStats(void *args) |
| 550 |
{ |
| 551 |
mfxExtEncodeStats *stats = (mfxExtEncodeStats*)args; |
| 552 |
stats->EncodeStatsContainer->SynchronizeStatistics(&stats->EncodeStatsContainer->RefInterface, INFINITE); |
| 553 |
stats->EncodeStatsContainer->RefInterface.Release(&stats->EncodeStatsContainer->RefInterface); |
| 554 |
pthread_exit(NULL); |
| 555 |
return NULL; |
| 556 |
} |
| 557 |
|
| 558 |
static void prg_encoding8() { |
| 559 |
/*beg8*/ |
| 560 |
mfxExtEncodeStats stats_frame; |
| 561 |
mfxExtEncodeStats stats_blk; |
| 562 |
stats_frame.EncodeStatsFlags = MFX_ENCODESTATS_LEVEL_FRAME; |
| 563 |
stats_blk.EncodeStatsFlags = MFX_ENCODESTATS_LEVEL_BLK; |
| 564 |
AttachBuffer(bits, &stats_frame); |
| 565 |
AttachBuffer(bits, &stats_blk); |
| 566 |
|
| 567 |
/* perform encoding and gathering stats. */ |
| 568 |
MFXVideoENCODE_EncodeFrameAsync(session, NULL, surface, bits, &syncp); |
| 569 |
|
| 570 |
if (MFX_ERR_NONE) { |
| 571 |
|
| 572 |
/* thread to synchronize frame level statistics. */ |
| 573 |
pthread_create(&(pt[0]), NULL, &SynchronizeStats, (void*)&stats_frame); |
| 574 |
|
| 575 |
/* thread to synchronize block level statistics. */ |
| 576 |
pthread_create(&(pt[1]), NULL, &SynchronizeStats, (void*)&stats_blk); |
| 577 |
|
| 578 |
/* thread to synchronize bitstream. */ |
| 579 |
pthread_create(&(pt[2]), NULL, &SynchronizeBitstream, (void*)stats_frame); |
| 580 |
|
| 581 |
} |
| 582 |
|
| 583 |
} |
| 584 |
/*end8*/ |
| 585 |
#endif |
| 586 |
|
| 587 |
static void AttachBufferForQualityInfoOutput(mfxBitstream* bs, mfxExtQualityInfoOutput* qualityInfoOutput) |
| 588 |
{ |
| 589 |
UNUSED_PARAM(bs); |
| 590 |
UNUSED_PARAM(qualityInfoOutput); |
| 591 |
return; |
| 592 |
} |
| 593 |
|
| 594 |
static void prg_encoding9() { |
| 595 |
/*beg9*/ |
| 596 |
/* mfxExtQualityInfoMode Init */ |
| 597 |
mfxExtQualityInfoMode qualityInfoMode = {}; |
| 598 |
qualityInfoMode.Header.BufferId = MFX_EXTBUFF_ENCODED_QUALITY_INFO_MODE; |
| 599 |
qualityInfoMode.Header.BufferSz = sizeof(mfxExtQualityInfoMode); |
| 600 |
qualityInfoMode.QualityInfoMode = MFX_QUALITY_INFO_LEVEL_FRAME; |
| 601 |
|
| 602 |
mfxExtBuffer * ExtParam[1] = { (mfxExtBuffer *)&qualityInfoMode }; |
| 603 |
|
| 604 |
mfxSession session = (mfxSession)0; |
| 605 |
mfxVideoParam encodeParams = {}; |
| 606 |
encodeParams.NumExtParam = 1; |
| 607 |
encodeParams.ExtParam = ExtParam; |
| 608 |
|
| 609 |
/* perform check quality report support status */ |
| 610 |
MFXVideoENCODE_Query(session, &encodeParams, &encodeParams); |
| 611 |
|
| 612 |
/* init encode */ |
| 613 |
MFXVideoENCODE_Init(session, &encodeParams); |
| 614 |
|
| 615 |
/* perform encoding and gathering qualityInfo. */ |
| 616 |
mfxExtQualityInfoOutput qualityInfoOutput = {}; |
| 617 |
qualityInfoOutput.Header.BufferId = MFX_EXTBUFF_ENCODED_QUALITY_INFO_OUTPUT; |
| 618 |
qualityInfoOutput.Header.BufferSz = sizeof(mfxExtQualityInfoOutput); |
| 619 |
AttachBufferForQualityInfoOutput(bits, &qualityInfoOutput); |
| 620 |
sts = MFXVideoENCODE_EncodeFrameAsync(session, NULL, surface, bits, &syncp); |
| 621 |
if (sts == MFX_ERR_NONE) { |
| 622 |
/* to synchronize everything. */ |
| 623 |
MFXVideoCORE_SyncOperation(session, syncp, INFINITE); |
| 624 |
|
| 625 |
/* qualityInfo result output such as qualityInfoOutput.FrameOrder, qualityInfoOutput.MSE. */ |
| 626 |
} |
| 627 |
/*end9*/ |
| 628 |
} |
| 629 |
|
| 630 |
static void prg_encoding10() { |
| 631 |
/*beg10*/ |
| 632 |
/* mfxExtScreenContentTools Init */ |
| 633 |
mfxExtAV1ScreenContentTools screenContentTools = {}; |
| 634 |
screenContentTools.Header.BufferId = MFX_EXTBUFF_AV1_SCREEN_CONTENT_TOOLS; |
| 635 |
screenContentTools.Header.BufferSz = sizeof(mfxExtAV1ScreenContentTools); |
| 636 |
screenContentTools.Palette = MFX_CODINGOPTION_ON; |
| 637 |
screenContentTools.IntraBlockCopy = MFX_CODINGOPTION_ON; |
| 638 |
|
| 639 |
mfxExtBuffer * ExtParam[1] = { (mfxExtBuffer *)&screenContentTools }; |
| 640 |
|
| 641 |
mfxSession session = (mfxSession)0; |
| 642 |
mfxVideoParam encodeParams = {}; |
| 643 |
encodeParams.NumExtParam = 1; |
| 644 |
encodeParams.ExtParam = ExtParam; |
| 645 |
|
| 646 |
/* perform check screen content tools support status */ |
| 647 |
MFXVideoENCODE_Query(session, &encodeParams, &encodeParams); |
| 648 |
|
| 649 |
/* init encode */ |
| 650 |
MFXVideoENCODE_Init(session, &encodeParams); |
| 651 |
|
| 652 |
/* perform encoding */ |
| 653 |
for (;;) { |
| 654 |
MFXVideoENCODE_EncodeFrameAsync(session, NULL, surface, bits, &syncp); |
| 655 |
MFXVideoCORE_SyncOperation(session, syncp, INFINITE); |
| 656 |
} |
| 657 |
/*end10*/ |
| 658 |
} |
| 659 |
|
| 660 |
typedef enum {NV12_SYS = 1, NV12_VIDEO, RGBA_SYS, RGBA_VIDEO} UserDataType; |
| 661 |
UserDataType userDataType; |
| 662 |
mfxU8* alphaSysData; |
| 663 |
mfxMemId alphaVidioMemId; |
| 664 |
static void prg_encoding11() { |
| 665 |
/*beg11*/ |
| 666 |
/* mfxExtAlphaChannelEncCtrl Init */ |
| 667 |
mfxExtAlphaChannelEncCtrl alphaEncCtrl = {}; |
| 668 |
alphaEncCtrl.Header.BufferId = MFX_EXTBUFF_ALPHA_CHANNEL_ENC_CTRL; |
| 669 |
alphaEncCtrl.Header.BufferSz = sizeof(mfxExtAlphaChannelEncCtrl); |
| 670 |
alphaEncCtrl.EnableAlphaChannelEncoding = MFX_CODINGOPTION_ON; |
| 671 |
alphaEncCtrl.AlphaChannelBitrateRatio = 30; |
| 672 |
alphaEncCtrl.AlphaChannelMode = MFX_ALPHA_MODE_PREMULTIPLIED; |
| 673 |
|
| 674 |
mfxExtBuffer* ext_buf_init[1] = { (mfxExtBuffer *)&alphaEncCtrl }; |
| 675 |
init_param.NumExtParam = 1; |
| 676 |
init_param.ExtParam = ext_buf_init; |
| 677 |
|
| 678 |
/* query caps */ |
| 679 |
MFXVideoENCODE_Query(session, &init_param, &init_param); |
| 680 |
|
| 681 |
/* query IO surface */ |
| 682 |
MFXVideoENCODE_QueryIOSurf(session, &init_param, &request); |
| 683 |
allocate_pool_of_frame_surfaces(request.NumFrameSuggested); |
| 684 |
|
| 685 |
/* init encoder */ |
| 686 |
MFXVideoENCODE_Init(session, &init_param); |
| 687 |
|
| 688 |
/* perform encoding */ |
| 689 |
for (;;) { |
| 690 |
|
| 691 |
switch (userDataType) |
| 692 |
{ |
| 693 |
case NV12_SYS: |
| 694 |
{ |
| 695 |
|
| 696 |
/* sys mem data for alpha channel */ |
| 697 |
mfxFrameSurface1 alphaSurfSys = {}; |
| 698 |
alphaSurfSys.Info = surface->Info; // same as the surface.Info(base) |
| 699 |
alphaSurfSys.Data.NumExtParam = 0; |
| 700 |
alphaSurfSys.Data.ExtParam = NULL; |
| 701 |
alphaSurfSys.Data.A = alphaSysData; // Other fields(e.g. MemType/TimeStamp/FrameOrder) will be forced to be set to the same as the surface.Data(base) |
| 702 |
|
| 703 |
/* fill mfxExtAlphaChannelSurface */ |
| 704 |
mfxExtAlphaChannelSurface extAlphaSurfSys = {}; |
| 705 |
extAlphaSurfSys.Header.BufferId = MFX_EXTBUFF_ALPHA_CHANNEL_SURFACE; |
| 706 |
extAlphaSurfSys.Header.BufferSz = sizeof(mfxExtAlphaChannelSurface); |
| 707 |
extAlphaSurfSys.AlphaSurface = &alphaSurfSys; |
| 708 |
|
| 709 |
/* attach alpha surface to mfxFrameSurface1.Data.ExtParam */ |
| 710 |
mfxExtBuffer * ext_buf_surf_sys[1] = { (mfxExtBuffer *)&extAlphaSurfSys }; |
| 711 |
surface->Data.NumExtParam = 1; |
| 712 |
surface->Data.ExtParam = ext_buf_surf_sys; |
| 713 |
|
| 714 |
} |
| 715 |
break; |
| 716 |
case NV12_VIDEO: |
| 717 |
{ |
| 718 |
|
| 719 |
/* alomost same as SYS_NV12, need to use AlphaSurf.Data.MemId instead of AlphaSurf.Data.Y/UV */ |
| 720 |
/* user can use external frame allocator or internal memory management to allocate video memory and map MemId */ |
| 721 |
|
| 722 |
mfxFrameSurface1 alphaSurfVideo = {}; |
| 723 |
alphaSurfVideo.Info = surface->Info; // same as the surface.Info(base) |
| 724 |
alphaSurfVideo.Data.NumExtParam = 0; |
| 725 |
alphaSurfVideo.Data.ExtParam = NULL; |
| 726 |
alphaSurfVideo.Data.MemId = alphaVidioMemId; // Other fields(e.g. MemType/TimeStamp/FrameOrder) will be forced to be set to the same as the surface.Data(base) |
| 727 |
|
| 728 |
/* fill mfxExtAlphaChannelSurface */ |
| 729 |
mfxExtAlphaChannelSurface extAlphaSurfVideo = {}; |
| 730 |
extAlphaSurfVideo.Header.BufferId = MFX_EXTBUFF_ALPHA_CHANNEL_SURFACE; |
| 731 |
extAlphaSurfVideo.Header.BufferSz = sizeof(mfxExtAlphaChannelSurface); |
| 732 |
extAlphaSurfVideo.AlphaSurface = &alphaSurfVideo; |
| 733 |
|
| 734 |
/* attach alpha surface to mfxFrameSurface1.Data.ExtParam */ |
| 735 |
mfxExtBuffer * ext_buf_surf_video[1] = { (mfxExtBuffer *)&extAlphaSurfVideo }; |
| 736 |
surface->Data.NumExtParam = 1; |
| 737 |
surface->Data.ExtParam = ext_buf_surf_video; |
| 738 |
|
| 739 |
} |
| 740 |
break; |
| 741 |
case RGBA_SYS: |
| 742 |
{ |
| 743 |
|
| 744 |
/* no need for mfxExtAlphaChannelSurface, put alpha channel data into the mfxFrameSurface1.Data.A */ |
| 745 |
surface->Data.A = alphaSysData; |
| 746 |
|
| 747 |
} |
| 748 |
break; |
| 749 |
case RGBA_VIDEO: |
| 750 |
{ |
| 751 |
|
| 752 |
/* no need for mfxExtAlphaChannelSurface, alpha texture will be extracted internally */ |
| 753 |
/* the source surface must be shared, set this before allocating the source surface */ |
| 754 |
request.Type |= MFX_MEMTYPE_SHARED_RESOURCE; |
| 755 |
|
| 756 |
} |
| 757 |
break; |
| 758 |
} |
| 759 |
|
| 760 |
/* encode base&alpha frame */ |
| 761 |
sts = MFXVideoENCODE_EncodeFrameAsync(session, NULL, surface, bits, &syncp); |
| 762 |
if (end_of_stream() && sts == MFX_ERR_MORE_DATA) break; |
| 763 |
if (sts == MFX_ERR_NONE) { |
| 764 |
/* get base&alpha encoded data */ |
| 765 |
sts = MFXVideoCORE_SyncOperation(session, syncp, INFINITE); |
| 766 |
if (sts == MFX_ERR_NONE) |
| 767 |
do_something_with_encoded_bits(bits); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
/* close encoder */ |
| 772 |
MFXVideoENCODE_Close(session); |
| 773 |
free_pool_of_frame_surfaces(); |
| 774 |
|
| 775 |
/*end11*/ |
| 776 |
} |
| 777 |
|
| 778 |
#ifdef ONEVPL_EXPERIMENTAL |
| 779 |
static void prg_encoding12() { |
| 780 |
/*beg12*/ |
| 781 |
/* mfxExtAIEncCtrl Init */ |
| 782 |
mfxExtAIEncCtrl aiEncCtrl = {}; |
| 783 |
aiEncCtrl.Header.BufferId = MFX_EXTBUFF_AI_ENC_CTRL; |
| 784 |
aiEncCtrl.Header.BufferSz = sizeof(mfxExtAIEncCtrl); |
| 785 |
aiEncCtrl.SaliencyEncoder = MFX_CODINGOPTION_ON; |
| 786 |
aiEncCtrl.AdaptiveTargetUsage = MFX_CODINGOPTION_ON; |
| 787 |
|
| 788 |
mfxExtBuffer * ExtParam[1] = { (mfxExtBuffer *)&aiEncCtrl }; |
| 789 |
|
| 790 |
mfxSession session = (mfxSession)0; |
| 791 |
mfxVideoParam encodeParams = {}; |
| 792 |
encodeParams.NumExtParam = 1; |
| 793 |
encodeParams.ExtParam = ExtParam; |
| 794 |
|
| 795 |
/* perform check screen content tools support status */ |
| 796 |
MFXVideoENCODE_Query(session, &encodeParams, &encodeParams); |
| 797 |
|
| 798 |
/* init encode */ |
| 799 |
MFXVideoENCODE_Init(session, &encodeParams); |
| 800 |
|
| 801 |
/* perform encoding */ |
| 802 |
for (;;) { |
| 803 |
MFXVideoENCODE_EncodeFrameAsync(session, NULL, surface, bits, &syncp); |
| 804 |
MFXVideoCORE_SyncOperation(session, syncp, INFINITE); |
| 805 |
} |
| 806 |
/*end12*/ |
| 807 |
} |
| 808 |
#endif |