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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
|
// Copyright (c) 2008-2020 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.
#ifndef __MFXUTILS_H__
#define __MFXUTILS_H__
#include "mfx_config.h"
#include "mfxstructures.h"
#include "mfxdeprecated.h"
#include "mfxplugin.h"
#include "umc_structures.h"
#include "mfx_trace.h"
#include "mfx_utils_logging.h"
#include "mfx_utils_perf.h"
#include "mfx_decode_dpb_logging.h"
#include "mfx_timing.h"
#include "mfxsurfacepool.h"
#include "mfx_error.h"
#include <va/va.h>
#include <dlfcn.h>
#include <cassert>
#include <cstddef>
#include <algorithm>
#include <chrono>
#include <functional>
#include <atomic>
#include <sstream>
#include <utility>
#include <malloc.h>
#include <cstdlib>
// MFX_LOG output to the console in Debug mode;
// MFX_LTRACE output to vpl log file in Release and Debug modes.
template <
typename T
, typename std::enable_if<!std::is_same<T, mfxStatus>::value, int>::type = 0>
static inline T mfx_sts_trace(const char* fileName, const uint32_t lineNum, const char* funcName, T sts)
{
const int mSts = static_cast<int>(sts);
#if defined(MFX_ENABLE_LOG_UTILITY)
if (mSts)
{
MFX_LOG(LEVEL_ERROR, fileName, lineNum, "%s: returns %d\n", funcName, sts);
}
#endif
if (mSts != 0)
{
std::string mfxSts = (mSts > 0) ? "[warning] Status = " : "[critical] Status = ";
MFX_LTRACE((&_trace_static_handle, fileName, lineNum, funcName, MFX_TRACE_CATEGORY, MFX_TRACE_LEVEL_INTERNAL, mfxSts.c_str(), MFX_TRACE_FORMAT_I, sts));
}
return sts;
}
template <
typename T
, typename = typename std::enable_if<std::is_same<T, mfxStatus>::value>::type>
static inline T mfx_sts_trace(const char* fileName, const uint32_t lineNum, const char* funcName, T sts)
{
const std::string stsString = GetMFXStatusInString(sts);
std::string mfxSts;
if (sts > MFX_ERR_NONE || sts == MFX_ERR_MORE_DATA || sts == MFX_ERR_MORE_SURFACE || sts == MFX_ERR_INCOMPATIBLE_VIDEO_PARAM) //MFX_ERR_MORE_DATA, MFX_ERR_MORE_SURFACE and MFX_ERR_INCOMPATIBLE_VIDEO_PARAM are warning status
{
#if defined(MFX_ENABLE_LOG_UTILITY)
MFX_LOG(LEVEL_WARN, fileName, lineNum, "%s: returns %s\n", funcName, stsString.c_str());
#endif
mfxSts = "[warning] mfxRes = ";
}
else if (sts < MFX_ERR_NONE)
{
#if defined(MFX_ENABLE_LOG_UTILITY)
MFX_LOG(LEVEL_ERROR, fileName, lineNum, "%s: returns %s\n", funcName, stsString.c_str());
#endif
mfxSts = "[critical] mfxRes = ";
}
if(sts != MFX_ERR_NONE)
{
MFX_LTRACE((&_trace_static_handle, fileName, lineNum, funcName, MFX_TRACE_CATEGORY, MFX_TRACE_LEVEL_INTERNAL, mfxSts.c_str(), MFX_TRACE_FORMAT_S, stsString.c_str()));
}
return sts;
}
#define MFX_STS_TRACE(sts) mfx_sts_trace(__FILE__, __LINE__, __FUNCTION__, sts)
#define MFX_SUCCEEDED(sts) (MFX_STS_TRACE(sts) == MFX_ERR_NONE)
#define MFX_FAILED(sts) (MFX_STS_TRACE(sts) != MFX_ERR_NONE)
#define MFX_RETURN(sts) { return MFX_STS_TRACE(sts); }
#define MFX_RETURN_IF_ERR_NONE(sts) { if (MFX_SUCCEEDED(sts)) return MFX_ERR_NONE; }
#define MFX_CHECK(EXPR, ERR) { if (!(EXPR)) MFX_RETURN(ERR); }
#define MFX_CHECK_NO_RET(EXPR, STS, ERR){ if (!(EXPR)) { std::ignore = MFX_STS_TRACE(ERR); STS = ERR; } }
#define MFX_CHECK_STS(sts) MFX_CHECK(MFX_SUCCEEDED(sts), sts)
#define MFX_CHECK_STS_RET(sts, ret) { if (MFX_FAILED(sts)) return ret; }
#define MFX_CHECK_STS_RET_NULL(sts) MFX_CHECK_STS_RET(sts, nullptr)
#define MFX_SAFE_CALL(FUNC) { mfxStatus _sts = FUNC; MFX_CHECK_STS(_sts); }
#define MFX_CHECK_NULL_PTR1(pointer) MFX_CHECK(pointer, MFX_ERR_NULL_PTR)
#define MFX_CHECK_NULL_PTR2(p1, p2) { MFX_CHECK(p1, MFX_ERR_NULL_PTR); MFX_CHECK(p2, MFX_ERR_NULL_PTR); }
#define MFX_CHECK_NULL_PTR3(p1, p2, p3) { MFX_CHECK(p1, MFX_ERR_NULL_PTR); MFX_CHECK(p2, MFX_ERR_NULL_PTR); MFX_CHECK(p3, MFX_ERR_NULL_PTR); }
#define MFX_CHECK_STS_ALLOC(pointer) MFX_CHECK(pointer, MFX_ERR_MEMORY_ALLOC)
#define MFX_CHECK_COND(cond) MFX_CHECK(cond, MFX_ERR_UNSUPPORTED)
#define MFX_CHECK_INIT(InitFlag) MFX_CHECK(InitFlag, MFX_ERR_MORE_DATA)
#define MFX_CHECK_HDL(hdl) MFX_CHECK(hdl, MFX_ERR_INVALID_HANDLE)
#define MFX_CHECK_UMC_ALLOC(err) { if (err != true) {return MFX_ERR_MEMORY_ALLOC;} }
#define MFX_CHECK_EXBUF_INDEX(index) { if (index == -1) {return MFX_ERR_MEMORY_ALLOC;} }
#define MFX_CHECK_WITH_ASSERT(EXPR, ERR) { assert(EXPR); MFX_CHECK(EXPR,ERR); }
#define MFX_CHECK_WITH_THROW(EXPR, ERR, EXP) { if (!(EXPR)) { std::ignore = MFX_STS_TRACE(ERR); throw EXP; } }
#define MFX_CHECK_WITH_THROW_STS(EXPR, ERR) MFX_CHECK_WITH_THROW(EXPR, ERR, std::system_error(mfx::make_error_code(mfxStatus(ERR))))
static const mfxU32 MFX_TIME_STAMP_FREQUENCY = 90000; // will go to mfxdefs.h
static const mfxU64 MFX_TIME_STAMP_INVALID = (mfxU64)-1; // will go to mfxdefs.h
static const mfxU32 NO_INDEX = 0xffffffff;
static const mfxU8 NO_INDEX_U8 = 0xff;
static const mfxU16 NO_INDEX_U16 = 0xffff;
#define MFX_CHECK_UMC_STS(err) { if (err != static_cast<int>(UMC::UMC_OK)) {return ConvertStatusUmc2Mfx(err);} }
inline
mfxStatus ConvertStatusUmc2Mfx(UMC::Status umcStatus)
{
switch (umcStatus)
{
case UMC::UMC_OK: return MFX_ERR_NONE;
case UMC::UMC_ERR_NULL_PTR: return MFX_ERR_NULL_PTR;
case UMC::UMC_ERR_UNSUPPORTED: return MFX_ERR_UNSUPPORTED;
case UMC::UMC_ERR_ALLOC: return MFX_ERR_MEMORY_ALLOC;
case UMC::UMC_ERR_LOCK: return MFX_ERR_LOCK_MEMORY;
case UMC::UMC_ERR_NOT_IMPLEMENTED: return MFX_ERR_NOT_IMPLEMENTED;
case UMC::UMC_ERR_GPU_HANG: return MFX_ERR_GPU_HANG;
case UMC::UMC_ERR_DEVICE_FAIL: return MFX_ERR_DEVICE_FAILED;
case UMC::UMC_ERR_NOT_ENOUGH_BUFFER: return MFX_ERR_NOT_ENOUGH_BUFFER;
case UMC::UMC_ERR_NOT_ENOUGH_DATA: return MFX_ERR_MORE_DATA;
case UMC::UMC_ERR_SYNC: return MFX_ERR_MORE_DATA; // need to skip bad frames
default: return MFX_ERR_UNKNOWN; // need general error code here
}
}
inline
UMC::Status ConvertStatusMfx2Umc(mfxStatus mfxStatus)
{
switch (mfxStatus)
{
case MFX_ERR_NONE: return UMC::UMC_OK;
case MFX_ERR_NULL_PTR: return UMC::UMC_ERR_NULL_PTR;
case MFX_ERR_UNSUPPORTED: return UMC::UMC_ERR_UNSUPPORTED;
case MFX_ERR_MEMORY_ALLOC: return UMC::UMC_ERR_ALLOC;
case MFX_ERR_LOCK_MEMORY: return UMC::UMC_ERR_LOCK;
case MFX_ERR_NOT_ENOUGH_BUFFER: return UMC::UMC_ERR_NOT_ENOUGH_BUFFER;
case MFX_ERR_MORE_DATA: return UMC::UMC_ERR_NOT_ENOUGH_DATA;
case MFX_ERR_NOT_IMPLEMENTED: return UMC::UMC_ERR_NOT_IMPLEMENTED;
case MFX_ERR_GPU_HANG: return UMC::UMC_ERR_GPU_HANG;
case MFX_ERR_UNKNOWN:
default:
return UMC::UMC_ERR_FAILED;
}
}
inline
mfxF64 GetUmcTimeStamp(mfxU64 ts)
{
return ts == MFX_TIME_STAMP_INVALID ? -1.0 : ts / (mfxF64)MFX_TIME_STAMP_FREQUENCY;
}
inline
mfxU64 GetMfxTimeStamp(mfxF64 ts)
{
return ts < 0.0 ? MFX_TIME_STAMP_INVALID : (mfxU64)(ts * MFX_TIME_STAMP_FREQUENCY + .5);
}
inline
bool LumaIsNull(const mfxFrameSurface1 * surf)
{
if (surf->Info.FourCC == MFX_FOURCC_Y410)
{
return !surf->Data.Y410;
}
else
{
return !surf->Data.Y;
}
}
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(PTR) { if (PTR) { PTR->Release(); PTR = NULL; } }
#endif
#define IS_PROTECTION_ANY(val) (false)
#define MFX_COPY_FIELD_NO_LOG(Field) buf_dst.Field = buf_src.Field
#if !defined(MFX_ENABLE_LOG_UTILITY)
#define MFX_COPY_FIELD(Field) buf_dst.Field = buf_src.Field
#define MFX_COPY_ARRAY_FIELD(Array) std::copy(std::begin(buf_src.Array), std::end(buf_src.Array), std::begin(buf_dst.Array))
#else
#define MFX_COPY_FIELD(Field) \
buf_dst.Field = buf_src.Field; \
{ \
const std::string fieldForamt = GetNumberFormat(buf_src.Field); \
const std::string typeName = std::string(typeid(buf_src).name()); \
const std::string format = typeName + ".%s = " + fieldForamt + "\n"; \
MFX_LOG_API_TRACE(format.c_str(), #Field, buf_src.Field); \
}
#define MFX_COPY_ARRAY_FIELD(Array) \
std::copy(std::begin(buf_src.Array), std::end(buf_src.Array), std::begin(buf_dst.Array)); \
{ \
int idx = 0; \
const std::string fieldForamt = GetNumberFormat(buf_src.Array[0]); \
const std::string typeName = std::string(typeid(buf_src).name()); \
const std::string format = typeName + ".%s[%d] = " + fieldForamt + "\n"; \
for (auto it = std::begin(buf_src.Array); it != std::end(buf_src.Array); it++, idx++) { \
MFX_LOG_API_TRACE(format.c_str(), #Array, idx, *it); \
} \
}
#endif
namespace mfx
{
template<typename T>
T GetEnv(const char* name, T defaultVal)
{
#if defined (MFX_ENV_CFG_ENABLE)
if (const char* strVal = std::getenv(name))
{
std::istringstream(strVal) >> defaultVal;
MFX_LTRACE_1(MFX_TRACE_LEVEL_INTERNAL, name, "=%s", strVal);
return defaultVal;
}
#endif
#if defined (MFX_TRACE_ENABLE)
{
std::ostringstream ss;
ss << name << "=" << defaultVal;
MFX_LTRACE_MSG(MFX_TRACE_LEVEL_INTERNAL, ss.str().c_str());
}
#endif
return defaultVal;
}
// switch to std::clamp when C++17 support will be enabled
// Clip value v to range [lo, hi]
template<class T>
constexpr const T& clamp( const T& v, const T& lo, const T& hi )
{
return std::min(hi, std::max(v, lo));
}
// Comp is comparison function object with meaning of 'less' operator (i.e. std::less<> or operator<)
template<class T, class Compare>
constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp )
{
return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
}
// Clip value to range [0, 255]
template<class T>
constexpr uint8_t byte_clamp(T v)
{
return uint8_t(clamp<T>(v, 0, 255));
}
// Aligns value to next power of two
template<class T> inline
T align2_value(T value, size_t alignment = 16)
{
assert((alignment & (alignment - 1)) == 0);
return static_cast<T> ((value + (alignment - 1)) & ~(alignment - 1));
}
template <class T>
constexpr size_t size(const T& c)
{
return (size_t)c.size();
}
template <class T, size_t N>
constexpr size_t size(const T(&)[N])
{
return N;
}
template<class T>
constexpr T CeilDiv(T x, T y)
{
return (x + y - 1) / y;
}
inline mfxU32 CeilLog2(mfxU32 x)
{
mfxU32 l = 0;
while (x > (1U << l))
++l;
return l;
}
template <class F>
struct TupleArgs;
template <typename TRes, typename... TArgs>
struct TupleArgs<TRes(TArgs...)>
{
using type = std::tuple<TArgs...>;
};
template <typename TRes, typename... TArgs>
struct TupleArgs<TRes(*)(TArgs...)>
{
using type = std::tuple<TArgs...>;
};
template <class F>
struct result_of;
template <typename TRes, typename... TArgs>
struct result_of<TRes(TArgs...)> : std::result_of<TRes(TArgs...)> {};
template <typename TRes, typename... TArgs>
struct result_of<TRes(*const&)(TArgs...)>
{
using type = TRes;
};
template<typename TFunc, typename TTuple, size_t ...S >
inline typename mfx::result_of<TFunc>::type
apply_impl(TFunc&& fn, TTuple&& t, std::index_sequence<S...>)
{
return fn(std::get<S>(t) ...);
}
template<typename TFunc, typename TTuple>
inline typename mfx::result_of<TFunc>::type
apply(TFunc&& fn, TTuple&& t)
{
return apply_impl(
std::forward<TFunc>(fn)
, std::forward<TTuple>(t)
, typename std::make_index_sequence<std::tuple_size<typename std::remove_reference<TTuple>::type>::value>());
}
template<class T>
class IterStepWrapper
: public std::iterator_traits<T>
{
public:
using iterator_category = std::forward_iterator_tag;
using iterator_type = IterStepWrapper;
using reference = typename std::iterator_traits<T>::reference;
using pointer = typename std::iterator_traits<T>::pointer;
IterStepWrapper(T ptr, ptrdiff_t step = 1)
: m_ptr(ptr)
, m_step(step)
{}
iterator_type& operator++()
{
std::advance(m_ptr, m_step);
return *this;
}
iterator_type operator++(int)
{
auto i = *this;
++(*this);
return i;
}
reference operator*() { return *m_ptr; }
pointer operator->() { return m_ptr; }
bool operator==(const iterator_type& other)
{
return
m_ptr == other.m_ptr
|| abs(std::distance(m_ptr, other.m_ptr)) < std::max(abs(m_step), abs(other.m_step));
}
bool operator!=(const iterator_type& other)
{
return !((*this) == other);
}
private:
T m_ptr;
ptrdiff_t m_step;
};
template <class T>
inline IterStepWrapper<T> MakeStepIter(T ptr, ptrdiff_t step = 1)
{
return IterStepWrapper<T>(ptr, step);
}
class OnExit
: public std::function<void()>
{
public:
OnExit(const OnExit&) = delete;
template<class... TArg>
OnExit(TArg&& ...arg)
: std::function<void()>(std::forward<TArg>(arg)...)
{}
~OnExit()
{
if (operator bool())
operator()();
}
template<class... TArg>
OnExit& operator=(TArg&& ...arg)
{
std::function<void()> tmp(std::forward<TArg>(arg)...);
swap(tmp);
return *this;
}
};
namespace options //MSDK API options verification utilities
{
//Each Check... function return true if verification failed, false otherwise
template <class T>
inline bool Check(const T&)
{
return true;
}
template <class T, T val, T... other>
inline bool Check(const T & opt)
{
if (opt == val)
return false;
return Check<T, other...>(opt);
}
template <class T, T val>
inline bool CheckGE(T opt)
{
return !(opt >= val);
}
template <class T, class... U>
inline bool Check(T & opt, T next, U... other)
{
if (opt == next)
return false;
return Check(opt, other...);
}
template <class T>
inline bool CheckOrZero(T& opt)
{
opt = T(0);
return true;
}
template <class T, T val, T... other>
inline bool CheckOrZero(T & opt)
{
if (opt == val)
return false;
return CheckOrZero<T, other...>(opt);
}
template <class T, class... U>
inline bool CheckOrZero(T & opt, T next, U... other)
{
if (opt == next)
return false;
return CheckOrZero(opt, (T)other...);
}
template <class T>
inline bool CheckOrSetDefault(T& opt, T dflt)
{
opt = T(dflt);
return true;
}
template <class T, T val, T... other>
inline bool CheckOrSetDefault(T & opt, T dflt)
{
if (opt == val)
return false;
return CheckOrSetDefault<T, other...>(opt, dflt);
}
template <class T, class U>
inline bool CheckMaxOrZero(T & opt, U max)
{
if (opt <= max)
return false;
opt = 0;
return true;
}
template <class T, class U>
inline bool CheckMinOrZero(T & opt, U min)
{
if (opt >= min)
return false;
opt = 0;
return true;
}
template <class T, class U>
inline bool CheckMaxOrClip(T & opt, U max)
{
if (opt <= max)
return false;
opt = T(max);
return true;
}
template <class T, class U>
inline bool CheckMinOrClip(T & opt, U min)
{
if (opt >= min)
return false;
opt = T(min);
return true;
}
template<class T, class I>
inline bool CheckRangeOrClip(T & opt, I min, I max)
{
if (opt < static_cast<T>(min))
{
opt = static_cast<T>(min);
return true;
}
if (opt > static_cast<T>(max))
{
opt = static_cast<T>(max);
return true;
}
return false;
}
template <class T>
inline bool CheckRangeOrSetDefault(T & opt, T min, T max, T dflt)
{
if (opt >= min && opt <= max)
return false;
opt = dflt;
return true;
}
inline bool CheckTriState(mfxU16 opt)
{
return Check<mfxU16
, MFX_CODINGOPTION_UNKNOWN
, MFX_CODINGOPTION_ON
, MFX_CODINGOPTION_OFF>(opt);
}
inline bool CheckTriStateOrZero(mfxU16& opt)
{
return CheckOrZero<mfxU16
, MFX_CODINGOPTION_UNKNOWN
, MFX_CODINGOPTION_ON
, MFX_CODINGOPTION_OFF>(opt);
}
template<class TVal, class TArg, typename std::enable_if<!std::is_constructible<TVal, TArg>::value, int>::type = 0>
inline TVal GetOrCall(TArg val) { return val(); }
template<class TVal, class TArg, typename = typename std::enable_if<std::is_constructible<TVal, TArg>::value>::type>
inline TVal GetOrCall(TArg val) { return TVal(val); }
template<typename T, typename TF>
inline bool SetDefault(T& opt, TF get_dflt)
{
if (opt)
return false;
opt = GetOrCall<T>(get_dflt);
return true;
}
inline void SetDefaultOpt(mfxU16 &opt, bool bCond)
{
SetDefault(opt, bCond ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF);
}
template<typename T, typename TF>
inline bool SetIf(T& opt, bool bSet, TF get)
{
if (!bSet)
return false;
opt = GetOrCall<T>(get);
return true;
}
template<class T, class TF, class... TA>
inline bool SetIf(T& opt, bool bSet, TF&& get, TA&&... arg)
{
if (!bSet)
return false;
opt = get(std::forward<TA>(arg)...);
return true;
}
template <class T>
inline bool InheritOption(T optInit, T & optReset)
{
if (optReset == 0)
{
optReset = optInit;
return true;
}
return false;
}
template<class TSrcIt, class TDstIt>
TDstIt InheritOptions(TSrcIt initFirst, TSrcIt initLast, TDstIt resetFirst)
{
while (initFirst != initLast)
{
InheritOption(*initFirst++, *resetFirst++);
}
return resetFirst;
}
inline mfxU16 Bool2CO(bool bOptON)
{
return mfxU16(MFX_CODINGOPTION_OFF - !!bOptON * MFX_CODINGOPTION_ON);
}
template<class T>
inline bool AlignDown(T& value, mfxU32 alignment)
{
assert((alignment & (alignment - 1)) == 0); // should be 2^n
if (!(value & (alignment - 1))) return false;
value = value & ~(alignment - 1);
return true;
}
template<class T>
inline bool AlignUp(T& value, mfxU32 alignment)
{
assert((alignment & (alignment - 1)) == 0); // should be 2^n
if (!(value & (alignment - 1))) return false;
value = (value + alignment - 1) & ~(alignment - 1);
return true;
}
namespace frametype
{
inline bool IsIdr(mfxU32 type)
{
return !!(type & MFX_FRAMETYPE_IDR);
}
inline bool IsI(mfxU32 type)
{
return !!(type & MFX_FRAMETYPE_I);
}
inline bool IsB(mfxU32 type)
{
return !!(type & MFX_FRAMETYPE_B);
}
inline bool IsP(mfxU32 type)
{
return !!(type & MFX_FRAMETYPE_P);
}
inline bool IsRef(mfxU32 type)
{
return !!(type & MFX_FRAMETYPE_REF);
}
}
}
class PODArraysHolder
{
public:
template<typename T>
T& PushBack(T*& p)
{
auto IsSameData = [p](std::vector<uint8_t>& v) { return (uint8_t*)p == v.data(); };
auto it = std::find_if(std::begin(m_attachedData), std::end(m_attachedData), IsSameData);
if (it == m_attachedData.end())
{
m_attachedData.emplace_back(std::vector<uint8_t>(sizeof(T), 0));
return *(p = (T*)m_attachedData.back().data());
}
auto itNew = it->insert(it->end(), sizeof(T), 0);
p = (T*)it->data();
return *(T*)&*itNew;
}
protected:
std::list<std::vector<uint8_t>> m_attachedData;
};
template <class Duration, class Representation>
class Timer
{
public:
Timer(Representation left)
: m_end(std::chrono::steady_clock::now() + Duration(left))
{}
Timer(std::chrono::steady_clock::time_point end)
: m_end(end)
{}
Representation Left() const
{
auto now = std::chrono::steady_clock::now();
return m_end < now ?
Representation{} :
Representation(std::chrono::duration_cast<Duration>(m_end - now).count());
}
std::chrono::steady_clock::time_point End() const
{
return m_end;
}
static
bool Expired(Representation left)
{
return left == Representation{};
}
bool Expired() const
{
return Expired(Left());
}
protected:
std::chrono::steady_clock::time_point m_end;
};
template <class Duration, class Representation>
class ResettableTimer : public Timer<Duration, Representation>
{
public:
ResettableTimer(Representation left = Representation{})
: Timer<Duration, Representation>(left)
{}
void Reset(Representation left)
{
this->m_end = std::chrono::steady_clock::now() + Duration(left);
m_running = true;
}
bool IsRunnig() const
{
return m_running;
}
void Stop()
{
m_running = false;
}
private:
bool m_running = false;
};
template <typename Representation>
using TimerMs = Timer<std::chrono::milliseconds, Representation>;
using ResettableTimerMs = ResettableTimer<std::chrono::milliseconds, std::chrono::milliseconds>;
inline mfxExtBuffer* GetExtBuffer(mfxExtBuffer** ExtParam, mfxU32 NumExtParam, mfxU32 BufferId, mfxU32 offset = 0)
{
if (!ExtParam)
return nullptr;
mfxU32 count = 0;
for (mfxU32 i = 0; i < NumExtParam; ++i)
{
if (ExtParam[i] && ExtParam[i]->BufferId == BufferId && count++ == offset)
{
return ExtParam[i];
}
}
return nullptr;
}
using mfx_shared_lib_path_string = std::string;
inline mfxHDL shared_lib_load(const mfx_shared_lib_path_string& shared_lib_file_name)
{
if (shared_lib_file_name.empty())
return nullptr;
return dlopen(shared_lib_file_name.c_str(), RTLD_LAZY);
}
inline mfxHDL shared_lib_get_addr(mfxHDL shared_lib_handle, const std::string & shared_lib_func_name)
{
if (!shared_lib_handle)
return nullptr;
return dlsym(shared_lib_handle, shared_lib_func_name.c_str());
}
inline void shared_lib_free(mfxHDL shared_lib_handle)
{
if (!shared_lib_handle)
return;
std::ignore = dlclose(shared_lib_handle);
}
class mfx_shared_lib_holder
{
public:
mfx_shared_lib_holder(const mfx_shared_lib_path_string& path, const char** functions_to_load, size_t n_functions_to_load)
: m_handle(shared_lib_load(path))
{
MFX_CHECK_WITH_THROW_STS(m_handle, MFX_ERR_INVALID_HANDLE);
// We can optionally load some functions from shared library
if (!n_functions_to_load)
return;
MFX_CHECK_WITH_THROW_STS(functions_to_load, MFX_ERR_INVALID_HANDLE);
for (size_t idx = 0; idx < n_functions_to_load; ++idx)
{
mfxHDL loaded_hdl = mfx::shared_lib_get_addr(m_handle, functions_to_load[idx]);
MFX_CHECK_WITH_THROW_STS(loaded_hdl, MFX_ERR_INVALID_HANDLE);
m_loaded_functions[functions_to_load[idx]] = loaded_hdl;
}
}
virtual ~mfx_shared_lib_holder()
{
shared_lib_free(m_handle);
}
mfxHDL get_function_hdl(const std::string& fname)
{
auto it = m_loaded_functions.find(fname);
return it != m_loaded_functions.end() ? it->second : nullptr;
}
protected:
mfxHDL m_handle = nullptr;
std::map<std::string, mfxHDL> m_loaded_functions;
};
inline mfxU32 GetNumDataPlanesFromFourcc(mfxU32 fourcc)
{
switch (fourcc)
{
#if defined (MFX_ENABLE_FOURCC_RGB565)
case MFX_FOURCC_RGB565:
#endif
case MFX_FOURCC_RGB4:
case MFX_FOURCC_BGR4:
case MFX_FOURCC_A2RGB10:
case MFX_FOURCC_ARGB16:
case MFX_FOURCC_ABGR16:
case MFX_FOURCC_R16:
case MFX_FOURCC_P8:
case MFX_FOURCC_P8_TEXTURE:
case MFX_FOURCC_AYUV:
case MFX_FOURCC_AYUV_RGB4:
case MFX_FOURCC_Y210:
case MFX_FOURCC_Y216:
case MFX_FOURCC_Y410:
case MFX_FOURCC_Y416:
case MFX_FOURCC_ABGR16F:
case MFX_FOURCC_XYUV:
return 1u;
case MFX_FOURCC_NV12:
case MFX_FOURCC_NV16:
case MFX_FOURCC_NV21:
case MFX_FOURCC_P010:
case MFX_FOURCC_P016:
case MFX_FOURCC_P210:
case MFX_FOURCC_YUY2:
case MFX_FOURCC_UYVY:
return 2u;
case MFX_FOURCC_YV12:
case MFX_FOURCC_IYUV:
case MFX_FOURCC_I010:
case MFX_FOURCC_I210:
case MFX_FOURCC_I422:
#ifdef MFX_ENABLE_RGBP
case MFX_FOURCC_RGBP:
#endif
case MFX_FOURCC_BGRP:
return 3u;
default:
return 0u;
}
}
} //namespace mfx
inline mfxStatus CheckAndDestroyVAbuffer(VADisplay display, VABufferID & buffer_id)
{
if (buffer_id != VA_INVALID_ID)
{
VAStatus vaSts = vaDestroyBuffer(display, buffer_id);
MFX_CHECK_WITH_ASSERT(VA_STATUS_SUCCESS == vaSts, MFX_ERR_DEVICE_FAILED);
buffer_id = VA_INVALID_ID;
}
return MFX_ERR_NONE;
}
inline mfxStatus AddRefSurface(mfxFrameSurface1 & surf, bool allow_legacy_surface = false)
{
if (allow_legacy_surface && !surf.FrameInterface) { return MFX_ERR_NONE; }
MFX_CHECK(surf.FrameInterface && surf.FrameInterface->AddRef, MFX_ERR_UNSUPPORTED);
return surf.FrameInterface->AddRef(&surf);
}
inline mfxStatus ReleaseSurface(mfxFrameSurface1 & surf, bool allow_legacy_surface = false)
{
if (allow_legacy_surface && !surf.FrameInterface) { return MFX_ERR_NONE; }
MFX_CHECK(surf.FrameInterface && surf.FrameInterface->Release, MFX_ERR_UNSUPPORTED);
return surf.FrameInterface->Release(&surf);
}
struct surface_refcount_scoped_lock : public std::unique_ptr<mfxFrameSurface1, void(*)(mfxFrameSurface1* surface)>
{
surface_refcount_scoped_lock(mfxFrameSurface1* surface)
: std::unique_ptr<mfxFrameSurface1, void(*)(mfxFrameSurface1* surface)>(
surface, [](mfxFrameSurface1* surface)
{
std::ignore = MFX_STS_TRACE(ReleaseSurface(*surface));
})
{}
};
struct mfxRefCountable
{
virtual mfxU32 GetRefCounter() const = 0;
virtual void AddRef() = 0;
virtual mfxStatus Release() = 0;
};
template <typename T>
struct mfxRefCountableInstance
{
static mfxRefCountable* Get(T* object)
{ return static_cast<mfxRefCountable*>(object); }
};
template <typename T, typename U = T>
class mfxRefCountableImpl
: public mfxRefCountable
, public T
{
protected:
template <typename X, typename = void>
struct HasRefInterface : std::false_type { };
template <typename X>
struct HasRefInterface <X, decltype(X::RefInterface, void())> : std::true_type { };
template <typename X, typename std::enable_if<HasRefInterface<X>::value, bool>::type = true>
void AssignFunctionPointers()
{
T::RefInterface.AddRef = _AddRef2;
T::RefInterface.Release = _Release2;
T::RefInterface.GetRefCounter = _GetRefCounter2;
}
template <typename X, typename std::enable_if<!HasRefInterface<X>::value, bool>::type = true>
void AssignFunctionPointers()
{
T::AddRef = _AddRef;
T::Release = _Release;
T::GetRefCounter = _GetRefCounter;
}
public:
mfxRefCountableImpl()
: T()
, m_ref_count(0)
{
#if defined (MFX_DEBUG_REFCOUNT)
g_global_registry.RegisterRefcountObject(this);
#endif
AssignFunctionPointers<T>();
}
virtual ~mfxRefCountableImpl()
{
if (m_ref_count.load(std::memory_order_relaxed) != 0)
{
std::ignore = MFX_STS_TRACE(MFX_ERR_UNKNOWN);
}
#if defined (MFX_DEBUG_REFCOUNT)
g_global_registry.UnregisterRefcountObject(this);
#endif
}
mfxU32 GetRefCounter() const override
{
return m_ref_count.load(std::memory_order_relaxed);
}
void AddRef() override
{
std::ignore = m_ref_count.fetch_add(1, std::memory_order_relaxed);
}
mfxStatus Release() override
{
MFX_CHECK(m_ref_count.load(std::memory_order_relaxed), MFX_ERR_UNDEFINED_BEHAVIOR);
// fetch_sub return value immediately preceding
if (m_ref_count.fetch_sub(1, std::memory_order_relaxed) - 1 == 0)
{
// Refcount is zero
// Update state of parent allocator if set
Close();
// Delete refcounted object, here wrapper is finally destroyed and underlying texture / memory released
delete this;
}
return MFX_ERR_NONE;
}
static mfxStatus _AddRef(U* object)
{
MFX_CHECK_NULL_PTR1(object);
auto instance = mfxRefCountableInstance<U>::Get(object);
MFX_CHECK_HDL(instance);
instance->AddRef();
return MFX_ERR_NONE;
}
static mfxStatus _Release(U* object)
{
MFX_CHECK_NULL_PTR1(object);
auto instance = mfxRefCountableInstance<U>::Get(object);
MFX_CHECK_HDL(instance);
return instance->Release();
}
static mfxStatus _GetRefCounter(U* object, mfxU32* counter)
{
MFX_CHECK_NULL_PTR1(object);
MFX_CHECK_NULL_PTR1(counter);
auto instance = mfxRefCountableInstance<U>::Get(object);
MFX_CHECK_HDL(instance);
*counter = instance->GetRefCounter();
return MFX_ERR_NONE;
}
static mfxStatus _AddRef2(mfxRefInterface* object)
{
MFX_CHECK_NULL_PTR1(object);
auto instance = static_cast<mfxRefCountable*>(object->Context);
MFX_CHECK_HDL(instance);
instance->AddRef();
return MFX_ERR_NONE;
}
static mfxStatus _Release2(mfxRefInterface* object)
{
MFX_CHECK_NULL_PTR1(object);
auto instance = static_cast<mfxRefCountable*>(object->Context);
MFX_CHECK_HDL(instance);
return instance->Release();
}
static mfxStatus _GetRefCounter2(mfxRefInterface* object, mfxU32* counter)
{
MFX_CHECK_NULL_PTR1(object);
MFX_CHECK_NULL_PTR1(counter);
auto instance = static_cast<mfxRefCountable*>(object->Context);
MFX_CHECK_HDL(instance);
*counter = instance->GetRefCounter();
return MFX_ERR_NONE;
}
protected:
virtual void Close() { return; }
private:
std::atomic<uint32_t> m_ref_count;
};
// This class calls release at destruction
template <typename RefCountable>
struct unique_ptr_refcountable : public std::unique_ptr<RefCountable, void(*)(RefCountable* obj)>
{
explicit unique_ptr_refcountable(RefCountable* refcountable = nullptr)
: std::unique_ptr<RefCountable, void(*)(RefCountable* obj)>(
refcountable, [](RefCountable* obj)
{
auto instance = mfxRefCountableInstance<RefCountable>::Get(obj);
instance->Release();
})
{}
};
#define MFX_EQ_FIELD(Field) l.Field == r.Field
#define MFX_EQ_ARRAY(Array, Num) std::equal(l.Array, l.Array + Num, r.Array)
#define MFX_DECL_OPERATOR_NOT_EQ(Name) \
static inline bool operator!=(Name const& l, Name const& r) \
{ \
return !(l == r); \
}
static inline bool operator==(mfxPluginUID const& l, mfxPluginUID const& r)
{
return MFX_EQ_ARRAY(Data, 16);
}
MFX_DECL_OPERATOR_NOT_EQ(mfxPluginUID)
static inline bool operator==(mfxGUID const& l, mfxGUID const& r)
{
return MFX_EQ_ARRAY(Data, 16);
}
MFX_DECL_OPERATOR_NOT_EQ(mfxGUID)
static inline bool operator==(mfxExtBuffer const& l, mfxExtBuffer const& r)
{
return MFX_EQ_FIELD(BufferId) && MFX_EQ_FIELD(BufferSz);
}
static inline bool operator==(mfxExtAllocationHints const& l, mfxExtAllocationHints const& r)
{
return MFX_EQ_FIELD(Header) && MFX_EQ_FIELD(AllocationPolicy) && MFX_EQ_FIELD(NumberToPreAllocate) && MFX_EQ_FIELD(DeltaToAllocateOnTheFly) && MFX_EQ_FIELD(VPPPoolType) && MFX_EQ_FIELD(Wait);
}
inline mfxStatus CheckAllocationHintsBuffer(const mfxExtAllocationHints& allocation_hints, bool is_vpp = false)
{
switch (allocation_hints.AllocationPolicy)
{
case MFX_ALLOCATION_OPTIMAL:
MFX_CHECK(!allocation_hints.NumberToPreAllocate, MFX_WRN_INCOMPATIBLE_VIDEO_PARAM);
MFX_CHECK(!allocation_hints.DeltaToAllocateOnTheFly, MFX_WRN_INCOMPATIBLE_VIDEO_PARAM);
break;
case MFX_ALLOCATION_UNLIMITED:
MFX_CHECK(!allocation_hints.DeltaToAllocateOnTheFly, MFX_WRN_INCOMPATIBLE_VIDEO_PARAM);
break;
case MFX_ALLOCATION_LIMITED:
break;
default:
MFX_RETURN(MFX_ERR_INVALID_VIDEO_PARAM);
}
if (is_vpp)
{
MFX_CHECK(allocation_hints.VPPPoolType == MFX_VPP_POOL_IN || allocation_hints.VPPPoolType == MFX_VPP_POOL_OUT, MFX_ERR_INVALID_VIDEO_PARAM);
}
return MFX_ERR_NONE;
}
enum class ComponentType
{
UNINITIALIZED = 0,
DECODE = 1,
ENCODE = 2,
VPP = 3
};
template <typename CacheType>
struct surface_cache_controller
{
surface_cache_controller(CacheType* cache, ComponentType type, mfxVPPPoolType vpp_pool_type = mfxVPPPoolType(-1))
: m_cache(cache)
, m_type(type)
, m_vpp_pool(vpp_pool_type)
{}
~surface_cache_controller()
{
Close();
}
mfxStatus SetupCache(mfxSession session, const mfxVideoParam& par)
{
auto it = std::find_if(par.ExtParam, par.ExtParam + par.NumExtParam,
[this](mfxExtBuffer* buffer)
{
return buffer->BufferId == MFX_EXTBUFF_ALLOCATION_HINTS &&
(m_type != ComponentType::VPP || reinterpret_cast<mfxExtAllocationHints*>(buffer)->VPPPoolType == m_vpp_pool);
});
mfxExtAllocationHints* allocation_hints = nullptr;
bool optimal_caching_policy_requested = false;
if (it != par.ExtParam + par.NumExtParam)
{
allocation_hints = reinterpret_cast<mfxExtAllocationHints*>(*it);
optimal_caching_policy_requested = allocation_hints->AllocationPolicy == MFX_ALLOCATION_OPTIMAL;
}
if (optimal_caching_policy_requested)
{
// Use temporal copy for possible IOPattern adjustment. Hitorically Init doesn't require setting of IOPatter, but QueryIOSurf does.
// This function is invoked during Init
mfxVideoParam tmp_par = par;
switch (m_type)
{
case ComponentType::DECODE:
{
mfxFrameAllocRequest req = {};
if (!tmp_par.IOPattern)
tmp_par.IOPattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
mfxStatus sts = MFX_STS_TRACE(MFXVideoDECODE_QueryIOSurf(session, &tmp_par, &req));
MFX_CHECK(sts >= MFX_ERR_NONE, sts);
m_required_num_surf = req.NumFrameSuggested;
}
break;
case ComponentType::ENCODE:
{
mfxFrameAllocRequest req = {};
if (!tmp_par.IOPattern)
tmp_par.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
mfxStatus sts = MFX_STS_TRACE(MFXVideoENCODE_QueryIOSurf(session, &tmp_par, &req));
MFX_CHECK(sts >= MFX_ERR_NONE, sts);
m_required_num_surf = req.NumFrameSuggested;
}
break;
case ComponentType::VPP:
{
mfxFrameAllocRequest req[2] = {};
if (!tmp_par.IOPattern)
tmp_par.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY | MFX_IOPATTERN_OUT_VIDEO_MEMORY;
mfxStatus sts = MFX_STS_TRACE(MFXVideoVPP_QueryIOSurf(session, &tmp_par, req));
MFX_CHECK(sts >= MFX_ERR_NONE, sts);
MFX_CHECK(m_vpp_pool == MFX_VPP_POOL_IN || m_vpp_pool == MFX_VPP_POOL_OUT, MFX_ERR_INVALID_VIDEO_PARAM);
m_required_num_surf = req[m_vpp_pool].NumFrameSuggested;
}
break;
default:
MFX_RETURN(MFX_ERR_NOT_IMPLEMENTED);
}
}
MFX_CHECK(m_cache, MFX_ERR_NOT_INITIALIZED);
if (!allocation_hints)
return MFX_ERR_NONE;
MFX_SAFE_CALL(CheckAllocationHintsBuffer(*allocation_hints, m_type == ComponentType::VPP));
MFX_SAFE_CALL(m_cache->SetupPolicy(*allocation_hints));
if (optimal_caching_policy_requested)
{
unique_ptr_refcountable<mfxSurfacePoolInterface> scoped_surface_lock(m_cache.get());
// AddRef here because m_cache is also a smart pointer, so will be decremented twice
MFX_SAFE_CALL(scoped_surface_lock->AddRef(scoped_surface_lock.get()));
MFX_CHECK(m_updated_caches.find(scoped_surface_lock) == std::end(m_updated_caches), MFX_ERR_UNKNOWN);
MFX_SAFE_CALL(m_cache->UpdateLimits(m_required_num_surf));
m_updated_caches[std::move(scoped_surface_lock)] = m_required_num_surf;
}
m_cache_hints_set = *allocation_hints;
return MFX_ERR_NONE;
}
mfxStatus Update(const mfxFrameSurface1& surf)
{
if (!surf.FrameInterface)
return MFX_ERR_NONE;
MFX_CHECK_HDL(surf.FrameInterface->QueryInterface);
mfxSurfacePoolInterface* pool_interface = nullptr;
MFX_SAFE_CALL(surf.FrameInterface->QueryInterface(const_cast<mfxFrameSurface1*>(&surf), MFX_GUID_SURFACE_POOL, reinterpret_cast<mfxHDL*>(&pool_interface)));
MFX_CHECK_HDL(pool_interface);
unique_ptr_refcountable<mfxSurfacePoolInterface> scoped_surface_lock(pool_interface);
// If pool already was updated, do nothing
if (m_updated_caches.find(scoped_surface_lock) != std::end(m_updated_caches))
return MFX_ERR_NONE;
MFX_CHECK_HDL(scoped_surface_lock->GetAllocationPolicy);
mfxPoolAllocationPolicy policy;
MFX_SAFE_CALL(scoped_surface_lock->GetAllocationPolicy(scoped_surface_lock.get(), &policy));
if (policy != MFX_ALLOCATION_OPTIMAL)
return MFX_ERR_NONE;
MFX_SAFE_CALL(scoped_surface_lock->SetNumSurfaces(scoped_surface_lock.get(), m_required_num_surf));
m_updated_caches[std::move(scoped_surface_lock)] = m_required_num_surf;
return MFX_ERR_NONE;
}
mfxStatus ResetCache(const mfxVideoParam& par)
{
return ResetCache(par.ExtParam, par.NumExtParam);
}
mfxStatus ResetCache(mfxExtBuffer** ExtParam, mfxU16 NumExtParam)
{
MFX_CHECK(m_cache, MFX_ERR_NOT_INITIALIZED);
auto allocation_hints = std::find_if(ExtParam, ExtParam + NumExtParam,
[this](mfxExtBuffer* buffer)
{
return buffer->BufferId == MFX_EXTBUFF_ALLOCATION_HINTS
/* According to VPL spec reserved fields should be zeroed, so VPPPoolType is zero for non-VPP components */
&& reinterpret_cast<mfxExtAllocationHints*>(buffer)->VPPPoolType == m_cache_hints_set.VPPPoolType;
});
if (allocation_hints != ExtParam + NumExtParam)
{
// For now it is not allowed to change cache parameters during Reset
MFX_CHECK(*reinterpret_cast<mfxExtAllocationHints*>(*allocation_hints) == m_cache_hints_set, MFX_ERR_INVALID_VIDEO_PARAM);
}
return MFX_ERR_NONE;
}
void Close()
{
if (!m_cache)
{
std::ignore = MFX_STS_TRACE(MFX_ERR_NOT_INITIALIZED);
return;
}
// Revoke in other caches (pools)
for (auto& pool_num_surf : m_updated_caches)
std::ignore = MFX_STS_TRACE(pool_num_surf.first->RevokeSurfaces(pool_num_surf.first.get(), pool_num_surf.second));
m_updated_caches.clear();
}
CacheType* operator->()
{
MFX_CHECK_WITH_THROW_STS(m_cache, MFX_ERR_NOT_INITIALIZED);
return m_cache.get();
}
mfxStatus GetSurface(mfxFrameSurface1*& output_surf, mfxSurfaceHeader* import_surface)
{
MFX_CHECK(m_cache, MFX_ERR_NOT_INITIALIZED);
MFX_RETURN(m_cache->GetSurface(output_surf, false, import_surface));
}
private:
unique_ptr_refcountable<CacheType> m_cache;
ComponentType m_type;
mfxVPPPoolType m_vpp_pool;
mfxU16 m_required_num_surf = 0;
mfxExtAllocationHints m_cache_hints_set = {};
struct ptr_comparator
{
bool operator()(const unique_ptr_refcountable<mfxSurfacePoolInterface>& left, const unique_ptr_refcountable<mfxSurfacePoolInterface>& right) const
{
return left.get() < right.get();
}
};
std::map<unique_ptr_refcountable<mfxSurfacePoolInterface>, mfxU32, ptr_comparator> m_updated_caches;
};
struct GUID
{
size_t GetHashCode() const
{
std::stringstream ss;
ss << Data1 << Data2 << Data3
// Pass Data4 element-wise to allow zeroes in GUID
<< Data4[0] << Data4[1] << Data4[2] << Data4[3] << Data4[4] << Data4[5] << Data4[6] << Data4[7];
return std::hash<std::string>()(ss.str());
}
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];
};
static inline int operator==(const GUID& guidOne, const GUID& guidOther)
{
return
guidOne.Data1 == guidOther.Data1 &&
guidOne.Data2 == guidOther.Data2 &&
guidOne.Data3 == guidOther.Data3 &&
std::equal(guidOne.Data4, guidOne.Data4 + sizeof(guidOne.Data4), guidOther.Data4);
}
inline bool IsOn(mfxU32 opt)
{
return opt == MFX_CODINGOPTION_ON;
}
inline bool IsOff(mfxU32 opt)
{
return opt == MFX_CODINGOPTION_OFF;
}
inline bool IsAdapt(mfxU32 opt)
{
return opt == MFX_CODINGOPTION_ADAPTIVE;
}
inline void* AlignedMalloc(size_t size, size_t alignment)
{
return aligned_alloc(alignment, size);
}
inline void AlignedFree(void* memory)
{
free(memory);
}
inline bool check_import_flags(size_t flags)
{
switch (flags)
{
case MFX_SURFACE_FLAG_DEFAULT:
case MFX_SURFACE_FLAG_IMPORT_SHARED:
case MFX_SURFACE_FLAG_IMPORT_COPY:
case (MFX_SURFACE_FLAG_IMPORT_SHARED | MFX_SURFACE_FLAG_IMPORT_COPY):
return true;
default:
return false;
}
}
inline bool check_export_flags(size_t flags)
{
switch (flags)
{
case MFX_SURFACE_FLAG_DEFAULT:
case MFX_SURFACE_FLAG_EXPORT_SHARED:
case MFX_SURFACE_FLAG_EXPORT_COPY:
case (MFX_SURFACE_FLAG_EXPORT_SHARED | MFX_SURFACE_FLAG_EXPORT_COPY):
return true;
default:
return false;
}
}
#endif // __MFXUTILS_H__
|