File: libmfx_allocator.h

package info (click to toggle)
onevpl-intel-gpu 25.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 58,880 kB
  • sloc: cpp: 1,443,659; ansic: 29,676; asm: 17,754; makefile: 6
file content (1506 lines) | stat: -rw-r--r-- 51,434 bytes parent folder | download | duplicates (2)
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
// Copyright (c) 2007-2024 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 _LIBMFX_ALLOCATOR_H_
#define _LIBMFX_ALLOCATOR_H_

#include "mfxvideo.h"
#include "mfx_common_int.h"

// It is only needed for Synchronize
#include "mfx_session.h"

#include "mfxsurfacepool.h"

#include "vm_interlocked.h"

#include <shared_mutex>
#include <vector>
#include <mutex>
#include <condition_variable>
#include <algorithm>

#ifdef MFX_ENABLE_ENCODE_STATS
#include "mfxencodestats.h"
#endif // MFX_ENABLE_ENCODE_STATS

static const size_t BASE_ADDR_ALIGN = 0x1000; // 4k page size alignment
static const size_t BASE_SIZE_ALIGN = 0x1000; // 4k page size alignment

// Internal Allocators
namespace mfxDefaultAllocator
{
    mfxStatus AllocBuffer(mfxHDL pthis, mfxU32 nbytes, mfxU16 type, mfxMemId *mid);
    mfxStatus LockBuffer(mfxHDL pthis, mfxMemId mid, mfxU8 **ptr);
    mfxStatus UnlockBuffer(mfxHDL pthis, mfxMemId mid);
    mfxStatus FreeBuffer(mfxHDL pthis, mfxMemId mid);

    mfxStatus AllocFrames(mfxHDL pthis, mfxFrameAllocRequest *request, mfxFrameAllocResponse *response);
    mfxStatus LockFrame(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr);
    mfxStatus GetHDL(mfxHDL pthis, mfxMemId mid, mfxHDL *handle);
    mfxStatus UnlockFrame(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr=0);
    mfxStatus FreeFrames(mfxHDL pthis, mfxFrameAllocResponse *response);

    mfxStatus GetSurfaceSizeInBytes(mfxU32 pitch, mfxU32 height, mfxU32 fourCC, mfxU32& nBytes);
    mfxStatus GetNumBytesRequired(const mfxFrameInfo& Info, mfxU32& nbytes, size_t power_of_2_alignment = BASE_SIZE_ALIGN);

    struct BufferStruct
    {
        mfxHDL      allocator;
        mfxU32      id;
        mfxU32      nbytes;
        mfxU16      type;
    };
    struct FrameStruct
    {
        mfxU32          id;
        mfxFrameInfo    info;
    };
}

class mfxWideBufferAllocator
{
public:
    std::vector<mfxDefaultAllocator::BufferStruct*> m_bufHdl;
    mfxWideBufferAllocator(void);
    ~mfxWideBufferAllocator(void);
    mfxBufferAllocator bufferAllocator;
};

class mfxBaseWideFrameAllocator
{
public:
    mfxBaseWideFrameAllocator(mfxU16 type = 0);
    virtual ~mfxBaseWideFrameAllocator();
    mfxFrameAllocator       frameAllocator;
    mfxWideBufferAllocator  wbufferAllocator;
    mfxU32                  NumFrames;
    std::vector<mfxHDL>     m_frameHandles;
    // Type of this allocator
    mfxU16                  type;
};
class mfxWideSWFrameAllocator : public  mfxBaseWideFrameAllocator
{
public:
    mfxWideSWFrameAllocator(mfxU16 type);
    virtual ~mfxWideSWFrameAllocator(void) {};
};

inline void clear_frame_data(mfxFrameData& frame_data) noexcept
{
    frame_data.PitchHigh = frame_data.PitchLow = 0;

    frame_data.U = frame_data.V = frame_data.Y = frame_data.A = nullptr;
}

#if defined (MFX_DEBUG_REFCOUNT)

static class RefcountGlobalRegistry
{
public:
    ~RefcountGlobalRegistry()
    {
        for (auto id : m_object_id)
        {
            printf("\n\nREFCOUNT ERROR: NOT DELETED OBJECT %p\n\n", id);
        }
    }

    void RegisterRefcountObject(void* id)
    {
        std::lock_guard<std::mutex> lock(m_mutex);

        if (std::find(std::begin(m_object_id), std::end(m_object_id), id) != std::end(m_object_id))
        {
            printf("\n\nREFCOUNT ERROR: CANNOT RE-REGISTER OBJECT %p\n\n", id);
            return;
        }

        printf("\n\nREFCOUNT NOTIFY: REGISTER OBJECT %p\n\n", id);

        m_object_id.push_back(id);
    }

    void UnregisterRefcountObject(void* id)
    {
        std::lock_guard<std::mutex> lock(m_mutex);

        auto it = std::find(std::begin(m_object_id), std::end(m_object_id), id);
        if (it == std::end(m_object_id))
        {
            printf("\n\nREFCOUNT ERROR: CANNOT DELETE NON-REGISTERED OBJECT %p\n\n", id);
            return;
        }

        printf("\n\nREFCOUNT NOTIFY: UNREGISTER OBJECT %p\n\n", id);

        m_object_id.erase(it);
    }

private:
    std::mutex         m_mutex;
    std::vector<void*> m_object_id;
} g_global_registry;
#endif

class FrameAllocatorWrapper;

class FrameAllocatorBase
{
public:
    FrameAllocatorBase(mfxSession session = nullptr): m_session(session) {}
    virtual ~FrameAllocatorBase() {}
    virtual mfxStatus Alloc(mfxFrameAllocRequest& request, mfxFrameAllocResponse& response)                 = 0;
    virtual mfxStatus Lock(mfxMemId mid, mfxFrameData* frame_data, mfxU32 flags = MFX_MAP_READ_WRITE)       = 0;
    virtual mfxStatus Unlock(mfxMemId mid, mfxFrameData* frame_data)                                        = 0;
    virtual mfxStatus GetHDL(mfxMemId mid, mfxHDL& handle)                                            const = 0;
    virtual mfxStatus Free(mfxFrameAllocResponse& response)                                                 = 0;
    virtual mfxStatus CreateSurface(mfxU16 type, const mfxFrameInfo& info, mfxFrameSurface1* & output_surf,
                                                                          mfxSurfaceHeader* import_surface) = 0;
    virtual mfxStatus ReallocSurface(const mfxFrameInfo& info, mfxMemId id)                                 = 0;
    virtual void      SetDevice(mfxHDL device)                                                              = 0;

    // Currently Synchronize implemented through mfxSyncPoint mechanism
    mfxStatus Synchronize(mfxSyncPoint, mfxU32 /*timeout*/);

    static bool CheckMemoryFlags(mfxU32 flags)
    {
        switch (flags & 0xf)
        {
        case MFX_MAP_READ:
        case MFX_MAP_WRITE:
        case MFX_MAP_READ_WRITE:
            break;
        default:
            return false;
        }

        return ((flags & 0xf0) & ~MFX_MAP_NOWAIT) == 0;
    }

    static std::atomic<std::uint32_t> m_allocator_num;

protected:

    // Surface need access to Remove method from destructor, for allocator state update
    friend class mfxFrameSurfaceBaseInterface;
    virtual void Remove(mfxMemId mid) = 0;

    friend class FrameAllocatorWrapper;
    void SetWrapper(FrameAllocatorWrapper* wrapper)
    {
        // It is assumed that there is only one possible wrapper, so it is ok to reassign without a check here
        m_frame_allocator_wrapper = wrapper;
    }

    FrameAllocatorWrapper*            m_frame_allocator_wrapper = nullptr;
    mfxSession                        m_session;
};

class FrameAllocatorExternal : public FrameAllocatorBase
{
public:
    FrameAllocatorExternal(const mfxFrameAllocator& ext_allocator)
        : allocator(ext_allocator)
    {}

    mfxStatus Alloc(mfxFrameAllocRequest& request, mfxFrameAllocResponse& response) override
    {
        return allocator.Alloc(allocator.pthis, &request, &response);
    }

    mfxStatus Lock(mfxMemId mid, mfxFrameData* frame_data, mfxU32 = MFX_MAP_READ) override
    {
        return allocator.Lock(allocator.pthis, mid, frame_data);
    }

    mfxStatus Unlock(mfxMemId mid, mfxFrameData* frame_data) override
    {
        return allocator.Unlock(allocator.pthis, mid, frame_data);
    }

    mfxStatus GetHDL(mfxMemId mid, mfxHDL& handle) const override
    {
        return allocator.GetHDL(allocator.pthis, mid, &handle);
    }

    mfxStatus Free(mfxFrameAllocResponse& response) override
    {
        return allocator.Free(allocator.pthis, &response);
    }

    mfxStatus CreateSurface(mfxU16, const mfxFrameInfo &, mfxFrameSurface1* &,
                                                            mfxSurfaceHeader*) override { return MFX_ERR_UNSUPPORTED; }
    mfxStatus ReallocSurface(const mfxFrameInfo &, mfxMemId )                  override { return MFX_ERR_UNSUPPORTED; }
    void      SetDevice(mfxHDL )                                               override { return; }
    void      Remove(mfxMemId)                                                 override { return; }

    mfxFrameAllocator* GetExtAllocator() { return &allocator; }

private:
    mfxFrameAllocator allocator;
};

static bool RequiredHWallocator(mfxU16 memtype)
{
    constexpr mfxU16 MFX_MEMTYPE_VIDEO_MASK = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_ENCODER_TARGET;

    return memtype & MFX_MEMTYPE_VIDEO_MASK;
}

class FrameAllocatorWrapper
{
public:
    FrameAllocatorWrapper(bool delayed_allocation)
        : m_delayed_allocation(delayed_allocation)
    {}

    mfxStatus Alloc(mfxFrameAllocRequest& request, mfxFrameAllocResponse& response, bool ext_alloc_hint = false)
    {
        try
        {
            // external allocator
            if (IsExtAllocatorSet() &&
                // Only external frames can go to external allocator
                 ( ((request.Type & MFX_MEMTYPE_EXTERNAL_FRAME)
                // Make 'fake' Alloc call to retrieve memId's of surfaces already allocated by app
                && (request.Type & MFX_MEMTYPE_FROM_DECODE))
                // Some possible forcing of external allocator
                || ext_alloc_hint))
            {

               MFX_SAFE_CALL(allocator_ext->Alloc(request, response));
               // In delay allocate mode, response frame num only need >= 0.
               // Delay allocate mode not work with D3D9, D3D9 will use legacy allocator logical
                if(!m_delayed_allocation && (response.NumFrameActual < request.NumFrameMin))
                {
                    std::ignore = MFX_STS_TRACE(allocator_ext->Free(response));
                    MFX_RETURN(MFX_ERR_MEMORY_ALLOC);
                }

               CacheMids(response, *allocator_ext);
               return MFX_ERR_NONE;
            }
            else
            {
                // Default Allocator is used for internal memory allocation only
                MFX_CHECK(!(request.Type & MFX_MEMTYPE_EXTERNAL_FRAME), MFX_ERR_MEMORY_ALLOC);

                return DefaultAllocFrames(request, response);
            }
        }
        catch (...)
        {
            MFX_RETURN(MFX_ERR_MEMORY_ALLOC);
        }
    }

    mfxStatus Lock(mfxMemId mid, mfxFrameData* frame_data, mfxU32 flags = MFX_MAP_READ_WRITE)
    {
        auto allocator = GetAllocatorByMid(mid);
        MFX_CHECK(allocator, MFX_ERR_LOCK_MEMORY);

        return allocator->Lock(mid, frame_data, flags);
    }

    mfxStatus Unlock(mfxMemId mid, mfxFrameData* frame_data)
    {
        auto allocator = GetAllocatorByMid(mid);
        MFX_CHECK(allocator, MFX_ERR_UNKNOWN);

        return allocator->Unlock(mid, frame_data);
    }

    mfxStatus GetHDL(mfxMemId mid, mfxHDL& handle)
    {
        auto allocator = GetAllocatorByMid(mid);
        MFX_CHECK(allocator, MFX_ERR_UNDEFINED_BEHAVIOR);

        return allocator->GetHDL(mid, handle);
    }

    mfxStatus Free(mfxFrameAllocResponse& response)
    {
        MFX_CHECK_NULL_PTR1(response.mids);

        // We can only Free those surfaces which were allocated inside library, so
        // we shouldn't default to ext allocator
        auto allocator = GetAllocatorByMid(response.mids[0], false);
        MFX_CHECK(allocator, MFX_ERR_UNKNOWN);

        std::unique_lock<std::shared_timed_mutex> lock(m_mutex);

        for (mfxU32 i = 0; i < response.NumFrameActual; ++i)
        {
            m_mid_to_allocator.erase(response.mids[i]);
        }

        lock.unlock();

        return allocator->Free(response);
    }

    mfxStatus CreateSurface(mfxU16 type, const mfxFrameInfo& info, mfxFrameSurface1* & output_surf, mfxSurfaceHeader* import_surface)
    {
        if (RequiredHWallocator(type))
        {
            MFX_CHECK(allocator_hw, MFX_ERR_UNSUPPORTED);
        }

        FrameAllocatorBase* allocator = ((type & MFX_MEMTYPE_SYSTEM_MEMORY) || !allocator_hw) ? allocator_sw.get() : allocator_hw.get();
        MFX_CHECK_HDL(allocator);
        MFX_SAFE_CALL(allocator->CreateSurface(type, info, output_surf, import_surface));

        CacheMid(output_surf->Data.MemId, *allocator);
        // it is required to clean up m_mid_to_allocator when output_surf will be completely deleted
        allocator->SetWrapper(this);

        return MFX_ERR_NONE;
    }

    mfxStatus ReallocSurface(const mfxFrameInfo & info, mfxMemId id)
    {
        // We can't realloc surface from ext allocator, so let's not even try default to it
        auto allocator = GetAllocatorByMid(id, false);
        MFX_CHECK(allocator, MFX_ERR_UNKNOWN);

        return allocator->ReallocSurface(info, id);
    }

    void SetDevice(mfxHDL device)
    {
        if (allocator_hw)
            allocator_hw->SetDevice(device);
    }

    mfxStatus SetFrameAllocator(const mfxFrameAllocator& allocator)
    {
        MFX_CHECK(!IsExtAllocatorSet(), MFX_ERR_UNDEFINED_BEHAVIOR);
        allocator_ext.reset(new FrameAllocatorExternal(allocator));
        return MFX_ERR_NONE;
    }

    bool IsExtAllocatorSet() const
    {
        return !!allocator_ext;
    }

    mfxFrameAllocator* GetExtAllocator()
    {
        if (!IsExtAllocatorSet())
            return nullptr;

        auto ext_alloc = dynamic_cast<FrameAllocatorExternal*>(allocator_ext.get());

        return ext_alloc ? ext_alloc->GetExtAllocator() : nullptr;
    }

    std::unique_ptr<FrameAllocatorBase> allocator_sw;
    std::unique_ptr<FrameAllocatorBase> allocator_hw;
    std::unique_ptr<FrameAllocatorBase> allocator_ext;

private:
    mfxStatus DefaultAllocFrames(mfxFrameAllocRequest& request, mfxFrameAllocResponse& response)
    {
        FrameAllocatorBase* allocator;

        if ((request.Type & MFX_MEMTYPE_SYSTEM_MEMORY) || !allocator_hw)
        {
            allocator = allocator_sw.get();
        }
        else
        {
            allocator = allocator_hw.get();
        }

        mfxStatus sts = allocator->Alloc(request, response);
        MFX_CHECK_STS(sts);

        if (response.NumFrameActual < request.NumFrameMin)
        {
            std::ignore = Free(response);
            MFX_RETURN(MFX_ERR_MEMORY_ALLOC);
        }

        CacheMids(response, *allocator);

        return MFX_ERR_NONE;
    }

    FrameAllocatorBase* GetAllocatorByMid(mfxMemId mid, bool default_to_external = true)
    {
        std::shared_lock<std::shared_timed_mutex> guard(m_mutex);

        if (m_mid_to_allocator.find(mid) == std::end(m_mid_to_allocator))
        {
            // If this surface wasn't allocated through Alloc call (cache miss), it might be external surface passed by user,
            // so it should be handled by external allocator
            return default_to_external ? allocator_ext.get() : nullptr;
        }

        return m_mid_to_allocator[mid];
    }

    void CacheMids(const mfxFrameAllocResponse & response, FrameAllocatorBase& allocator)
    {
        std::lock_guard<std::shared_timed_mutex> guard(m_mutex);
        for (mfxU32 i = 0; i < response.NumFrameActual; ++i)
        {
            m_mid_to_allocator[response.mids[i]] = &allocator;
        }
    }

    void CacheMid(mfxMemId mid, FrameAllocatorBase& allocator)
    {
        std::lock_guard<std::shared_timed_mutex> guard(m_mutex);

        m_mid_to_allocator[mid] = &allocator;
    }

    // To allow access to Remove function
    template <class T, class U> friend class FlexibleFrameAllocator;

    // This function is called when surface is deleted by reducing refcount to zero
    void Remove(mfxMemId mid)
    {
        std::unique_lock<std::shared_timed_mutex> lock(m_mutex);

        m_mid_to_allocator.erase(mid);
    }

    std::shared_timed_mutex                 m_mutex;
    std::map<mfxMemId, FrameAllocatorBase*> m_mid_to_allocator;
    bool                                    m_delayed_allocation;
};

inline mfxU16 AdjustTypeInternal(mfxU16 type)
{
    return (type & ~MFX_MEMTYPE_EXTERNAL_FRAME) | MFX_MEMTYPE_INTERNAL_FRAME;
}
template <class T, class U>
class FlexibleFrameAllocator : public FrameAllocatorBase
{
#define MFX_DETACH_FRAME                                                                         \
    [](T* surface)                                                                               \
    {                                                                                            \
        /* We already removed from allocator's list, no need to update it through destructor */  \
        surface->DetachParentAllocator();                                                        \
                                                                                                 \
        std::ignore = MFX_STS_TRACE(surface->Release());                                         \
    }

    using pT = std::unique_ptr<T, void(*)(T* surface)>;

public:
    FlexibleFrameAllocator(mfxHDL device = nullptr, mfxSession session = nullptr)
        // ids across different allocators (SW / HW in one core and in different cores (for simplicity)) shouldn't overlap
        : FrameAllocatorBase(session)
        // fetch_add returns value prior the increment
        , m_mid_high_part(size_t(m_allocator_num.fetch_add(1, std::memory_order_relaxed) + 1) << m_bits_n_surf)
        , m_mid_low_part_modulo((size_t(1) << m_bits_n_surf) - 1)
        , m_device(device)
        , m_staging_adapter(std::make_shared<U>(device))
    {
    }

    mfxStatus Alloc(mfxFrameAllocRequest& request, mfxFrameAllocResponse& response) override
    {
        response = {};

        if (!request.NumFrameSuggested)
            return MFX_ERR_NONE;

        MFX_CHECK(!(request.Type & MFX_MEMTYPE_EXTERNAL_FRAME), MFX_ERR_UNSUPPORTED);

        mfxU16 type = AdjustTypeInternal(request.Type);

        try
        {
            std::vector<mfxMemId> mids(request.NumFrameSuggested);

            std::list<pT> alloc_list;
            for (mfxU16 i = 0; i < request.NumFrameSuggested; ++i)
            {
                mids[i] = GenerateMid();

                alloc_list.emplace_back(pT(T::Create(request.Info, type, mids[i], m_staging_adapter, m_device, request.AllocId, *this, nullptr), MFX_DETACH_FRAME));
            }

            std::lock_guard<std::shared_timed_mutex> guard(m_mutex);

            m_allocated_pool.splice(m_allocated_pool.end(), alloc_list);

            m_returned_mids.emplace_back(std::move(mids));

            response.AllocId        = request.AllocId;
            response.mids           = m_returned_mids.back().data();
            response.NumFrameActual = request.NumFrameSuggested;
        }
        catch (const std::system_error& ex)
        {
            MFX_CHECK_STS(mfxStatus(ex.code().value()));
        }
        catch (...)
        {
            MFX_RETURN(MFX_ERR_MEMORY_ALLOC);
        }

        return MFX_ERR_NONE;
    }

    mfxStatus Lock(mfxMemId mid, mfxFrameData* frame_data, mfxU32 flags = MFX_MAP_READ) override
    {
        MFX_CHECK_HDL(mid);
        MFX_CHECK(CheckMemoryFlags(flags), MFX_ERR_LOCK_MEMORY);

        std::shared_lock<std::shared_timed_mutex> guard(m_mutex);

        auto it = std::find_if(std::begin(m_allocated_pool), std::end(m_allocated_pool),
            [mid](const pT& surf) { return surf->GetMid() == mid; });

        MFX_CHECK(it != std::end(m_allocated_pool), MFX_ERR_NOT_FOUND);

        MFX_SAFE_CALL((*it)->Lock(flags));

        (*it)->CopyPointers(frame_data);

        return MFX_ERR_NONE;
    }

    mfxStatus Unlock(mfxMemId mid, mfxFrameData* frame_data) override
    {
        MFX_CHECK_HDL(mid);

        std::shared_lock<std::shared_timed_mutex> guard(m_mutex);

        auto it = std::find_if(std::begin(m_allocated_pool), std::end(m_allocated_pool),
            [mid](const pT& surf) { return surf->GetMid() == mid; });

        MFX_CHECK(it != std::end(m_allocated_pool), MFX_ERR_NOT_FOUND);

        MFX_SAFE_CALL((*it)->Unlock());

        (*it)->CopyPointers(frame_data);

        return MFX_ERR_NONE;
    }

    mfxStatus GetHDL(mfxMemId mid, mfxHDL& handle) const override
    {
        MFX_CHECK_HDL(mid);

        std::shared_lock<std::shared_timed_mutex> guard(m_mutex);

        auto it = std::find_if(std::begin(m_allocated_pool), std::end(m_allocated_pool),
            [mid](const pT& surf) { return surf->GetMid() == mid; });

        MFX_CHECK(it != std::end(m_allocated_pool), MFX_ERR_INVALID_HANDLE);

        return (*it)->GetHDL(handle);
    }

    mfxStatus Free(mfxFrameAllocResponse& response) override
    {
        std::list<pT> frames_to_erase;

        std::lock_guard<std::shared_timed_mutex> guard(m_mutex);

        // Clear mids
        auto it_mids = std::find_if(std::begin(m_returned_mids), std::end(m_returned_mids),
            [this, response](const std::vector<mfxMemId> & mids)
                {
                    return mids.size() == response.NumFrameActual
                    && std::equal(response.mids, response.mids + response.NumFrameActual, std::begin(mids),
                        [this](mfxMemId l, mfxMemId r) { return l == r || l == ALREADY_REMOVED_MID || r == ALREADY_REMOVED_MID; });
                });

        MFX_CHECK(it_mids != std::end(m_returned_mids),       MFX_ERR_NOT_FOUND);
        // Partial freeing is not allowed
        MFX_CHECK(it_mids->size() == response.NumFrameActual, MFX_ERR_INVALID_HANDLE);

        mfxStatus sts = MFX_ERR_NONE;

        for (mfxU32 i = 0; i < response.NumFrameActual; ++i)
        {
            // Clear exported pool (if surface is there, MSDK lib might be doing something wrong)
            auto mid = response.mids[i];

            // This mid was already deleted by calling Release (object is deleted when it's refcounter reaches zero)
            if (mid == ALREADY_REMOVED_MID) continue;

            auto it_alloc = std::find_if(std::begin(m_allocated_pool), std::end(m_allocated_pool), [mid](const pT& surf) { return surf->GetMid() == mid; });

            if (it_alloc != std::end(m_allocated_pool))
            {
                frames_to_erase.splice(frames_to_erase.end(), m_allocated_pool, it_alloc);
            }
            else
            {
                sts = MFX_ERR_NOT_FOUND;
            }
        }

        m_returned_mids.erase(it_mids);

        return sts;
    }

    mfxStatus CreateSurface(mfxU16 type, const mfxFrameInfo& info, mfxFrameSurface1* & output_surf, mfxSurfaceHeader* import_surface) override
    {
        MFX_CHECK(!(type & MFX_MEMTYPE_EXTERNAL_FRAME), MFX_ERR_UNSUPPORTED);

        try
        {
            std::list<pT> alloc_list;

            alloc_list.emplace_back(pT(T::Create(info, T::AdjustType(type), GenerateMid(), m_staging_adapter, m_device, 0u, *this, import_surface), MFX_DETACH_FRAME));

            std::lock_guard<std::shared_timed_mutex> guard(m_mutex);

            m_allocated_pool.splice(m_allocated_pool.end(), alloc_list);

            // Fill mfxFrameSurface1 object and return to user
            output_surf = &(m_allocated_pool.back()->m_exported_surface);
        }
        catch (const std::system_error& ex)
        {
            MFX_CHECK_STS(mfxStatus(ex.code().value()));
        }
        catch (...)
        {
            MFX_RETURN(MFX_ERR_MEMORY_ALLOC);
        }

        return MFX_ERR_NONE;
    }

    mfxStatus ReallocSurface(const mfxFrameInfo & info, mfxMemId mid) override
    {
        MFX_CHECK_HDL(mid);

        std::shared_lock<std::shared_timed_mutex> guard(m_mutex);

        auto it = std::find_if(std::begin(m_allocated_pool), std::end(m_allocated_pool),
            [mid](const pT& surf) { return surf->GetMid() == mid; });
        MFX_CHECK(it != std::end(m_allocated_pool), MFX_ERR_NOT_FOUND);

        // Will not reallocate surface which is locked by someone
        MFX_CHECK(!(*it)->Locked(),                 MFX_ERR_LOCK_MEMORY);

        MFX_CHECK((*it)->ReallocAllowed(info),      MFX_ERR_INVALID_VIDEO_PARAM);

        return (*it)->Realloc(info);
    }

    void SetDevice(mfxHDL device) override
    {
        m_device = device;

        m_staging_adapter->SetDevice(device);
    }

protected:
    void Remove(mfxMemId mid) override
    {
        std::lock_guard<std::shared_timed_mutex> guard(m_mutex);

        auto it = std::find_if(std::begin(m_allocated_pool), std::end(m_allocated_pool),
            [mid](const pT& surf) { return surf->GetMid() == mid; });

        if (it == std::end(m_allocated_pool))
        {
            std::ignore = MFX_STS_TRACE(MFX_ERR_NOT_FOUND);
            return;
        }

        // Surface is being deleted after decreasing refcount to zero, no need decrease refcount in destructor of holder
        it->release();

        // Remove surface from mid <-> allocator binding table
        if (m_frame_allocator_wrapper)
            m_frame_allocator_wrapper->Remove(mid);

        m_allocated_pool.erase(it);

        std::ignore = std::find_if(std::begin(m_returned_mids), std::end(m_returned_mids),
            [this, mid](std::vector<mfxMemId>& v_mid)
            {
                auto it = std::find(std::begin(v_mid), std::end(v_mid), mid);

                if (it == std::end(v_mid)) return false;

                *it = ALREADY_REMOVED_MID;

                return true;
            });
    }

private:
    const size_t                           m_bits_n_surf      = 16; // One session can't have more than 2^16 surfaces simultaneously
    const size_t                           m_mid_high_part;
    const size_t                           m_mid_low_part_modulo;
    size_t                                 m_mid_low_part     = 0;
    mfxHDL                                 m_device           = nullptr;

    mutable std::shared_timed_mutex        m_mutex;

    // Do not change order of m_staging_adapter and m_allocated_pool (surfaces destruction has side effect on staging adapter)
    std::shared_ptr<U>                     m_staging_adapter;

    std::list<pT>                          m_allocated_pool;  // Pool of allocated surfaces

    std::list<std::vector<mfxMemId>>       m_returned_mids;   // Storage of memory for mids returned to MSDK lib

    const mfxMemId ALREADY_REMOVED_MID = mfxMemId(std::numeric_limits<size_t>::max());

    // This method always called without m_mutex being locked
    mfxMemId GenerateMid()
    {
        std::lock_guard<std::shared_timed_mutex> guard(m_mutex);

        // Check that pool is not already full
        MFX_CHECK_WITH_THROW_STS(m_allocated_pool.size() <= (m_mid_low_part_modulo + 1), MFX_ERR_MEMORY_ALLOC);

        mfxMemId new_memid;
        // There is only m_mid_low_part_modulo + 1 possible mids within one allocator
        for (size_t i = 0; i < m_mid_low_part_modulo + 1; ++i)
        {
            new_memid = mfxMemId(m_mid_high_part | ((++m_mid_low_part) & m_mid_low_part_modulo));

            if (
                // Check if current mid is already in pool
                std::find_if(std::begin(m_allocated_pool), std::end(m_allocated_pool),
                    [new_memid](const pT& surf) { return surf->GetMid() == new_memid; }) == std::end(m_allocated_pool)
                )
                return new_memid;
        }

        // Couldn't find suitable mid
        MFX_CHECK_WITH_THROW_STS(false, MFX_ERR_MEMORY_ALLOC);
    }

#undef MFX_DETACH_FRAME
};

class mfxFrameSurfaceBaseInterface
    : public mfxRefCountableImpl<mfxFrameSurfaceInterface, mfxFrameSurface1>
{
public:
    mfxFrameSurfaceBaseInterface(mfxMemId mid, FrameAllocatorBase& allocator)
        : m_allocator(&allocator)
        , m_mid(mid)
    {}

    virtual mfxStatus                          Lock(mfxU32 flags)      = 0;
    virtual mfxStatus                          Unlock()                = 0;
    virtual std::pair<mfxHDL, mfxResourceType> GetNativeHandle() const = 0;
    virtual std::pair<mfxHDL, mfxHandleType>   GetDeviceHandle() const = 0;
    virtual mfxStatus Export(const mfxSurfaceHeader& export_header,
                                  mfxSurfaceHeader** exported_surface) = 0;

    mfxMemId GetMid() const { return m_mid; }

    mfxStatus Synchronize(mfxU32 timeout)
    {
        // If allocator is detached, no need to sychronize surface. It is already synchronized
        return m_allocator ? m_allocator->Synchronize(m_sp, timeout) : MFX_ERR_NONE;
    }

    void SetSyncPoint(mfxSyncPoint Sync)
    {
        m_sp = Sync;
    }

    void DetachParentAllocator()
    {
        m_allocator = nullptr;
    }

    mfxSurfacePoolInterface* QueryParentPool()
    {
        if (!m_parent_pool)
            return nullptr;

        if (MFX_STS_TRACE(m_parent_pool->AddRef(m_parent_pool)) != MFX_ERR_NONE)
            return nullptr;

        return m_parent_pool;
    }

    void SetParentPool(mfxSurfacePoolInterface* pool)
    {
        m_parent_pool = pool;
    }

protected:

    void Close() override
    {
        if (m_allocator)
            m_allocator->Remove(m_mid);
    }

private:
    FrameAllocatorBase*      m_allocator;
    mfxMemId                 m_mid;
    mfxSyncPoint             m_sp          = nullptr;
    mfxSurfacePoolInterface* m_parent_pool = nullptr;
};

template <>
struct mfxRefCountableInstance<mfxFrameSurface1>
{
    static mfxRefCountable* Get(mfxFrameSurface1* object)
    { return reinterpret_cast<mfxFrameSurfaceBaseInterface*>(object->FrameInterface->Context); }
};

inline void copy_frame_surface_pixel_pointers(mfxFrameData& buf_dst, const mfxFrameData& buf_src)
{
    MFX_COPY_FIELD_NO_LOG(PitchLow);
    MFX_COPY_FIELD_NO_LOG(PitchHigh);
    MFX_COPY_FIELD_NO_LOG(Y);
    MFX_COPY_FIELD_NO_LOG(U);
    MFX_COPY_FIELD_NO_LOG(V);
    MFX_COPY_FIELD_NO_LOG(A);
}

class mfxFrameSurfaceInterfaceImpl;

class mfxSurfaceBase
    : public mfxRefCountableImpl<mfxSurfaceInterface>
{
public:
    mfxSurfaceBase(const mfxSurfaceHeader& export_header, mfxFrameSurfaceInterfaceImpl* p_base_surface)
        : m_p_base_surface(p_base_surface)
    {
        MFX_CHECK_WITH_THROW_STS(CheckExportFlags(export_header.SurfaceFlags), MFX_ERR_INVALID_VIDEO_PARAM);

        // Surface interface level
        Context         = this;
        Version.Version = MFX_SURFACEINTERFACE_VERSION;
        Header          = export_header;

        mfxSurfaceInterface::Synchronize = &mfxSurfaceBase::Synchronize_impl;
    }

    static mfxStatus Synchronize_impl(mfxSurfaceInterface* ext_surface, mfxU32 timeout)
    {
        MFX_CHECK_NULL_PTR1(ext_surface);
        MFX_CHECK_HDL(ext_surface->Context);

        return
            reinterpret_cast<mfxSurfaceBase*>(ext_surface->Context)->Synchronize(timeout);
    }

    mfxStatus Synchronize(mfxU32 timeout);

    void DetachBaseSurface()
    {
        m_p_base_surface = nullptr;
    }

    mfxFrameSurfaceInterfaceImpl* GetParentSurface()
    {
        return m_p_base_surface;
    }

    // Here we return memory which will be transferred outside, it will be copy of internal fields,
    // so we will be on safe side if user accidently memset this memory, so it protects internal state from accidental change
    virtual mfxSurfaceHeader* GetExport() = 0;

private:

    static bool CheckExportFlags(mfxU32 export_flags)
    {
        return (export_flags == MFX_SURFACE_FLAG_DEFAULT) || (export_flags & (MFX_SURFACE_FLAG_EXPORT_SHARED | MFX_SURFACE_FLAG_EXPORT_COPY));
    }

    void Close() override;

    mfxFrameSurfaceInterfaceImpl* m_p_base_surface = nullptr;

};

class mfxFrameSurfaceInterfaceImpl : public mfxFrameSurfaceBaseInterface
{
public:
    mfxFrameSurfaceInterfaceImpl(const mfxFrameInfo& info, mfxU16 type, mfxMemId mid, FrameAllocatorBase& allocator)
        : mfxFrameSurfaceBaseInterface(mid, allocator)
    {
        // Surface interface level
        Context         = static_cast<mfxFrameSurfaceBaseInterface*>(this);
        Version.Version = MFX_FRAMESURFACEINTERFACE_VERSION;

        mfxFrameSurfaceInterface::Map             = &mfxFrameSurfaceInterfaceImpl::Map_impl;
        mfxFrameSurfaceInterface::Unmap           = &mfxFrameSurfaceInterfaceImpl::Unmap_impl;
        mfxFrameSurfaceInterface::GetNativeHandle = &mfxFrameSurfaceInterfaceImpl::GetNativeHandle_impl;
        mfxFrameSurfaceInterface::GetDeviceHandle = &mfxFrameSurfaceInterfaceImpl::GetDeviceHandle_impl;
        mfxFrameSurfaceInterface::Synchronize     = &mfxFrameSurfaceInterfaceImpl::Synchronize_impl;
        mfxFrameSurfaceInterface::QueryInterface  = &mfxFrameSurfaceInterfaceImpl::QueryInterface_impl;
        mfxFrameSurfaceInterface::Export          = &mfxFrameSurfaceInterfaceImpl::Export_impl;

        // Surface representation
        m_internal_surface.Version.Version = MFX_FRAMESURFACE1_VERSION;

        m_internal_surface.Info           = info;
        m_internal_surface.Data.MemId     = mid;
        m_internal_surface.Data.MemType   = type;
        m_internal_surface.FrameInterface = static_cast<mfxFrameSurfaceInterface*>(this);

        if (!m_internal_surface.Info.BitDepthLuma)
            m_internal_surface.Info.BitDepthLuma = BitDepthFromFourcc(m_internal_surface.Info.FourCC);

        if (!m_internal_surface.Info.BitDepthChroma)
            m_internal_surface.Info.BitDepthChroma = m_internal_surface.Info.BitDepthLuma ? m_internal_surface.Info.BitDepthLuma : BitDepthFromFourcc(m_internal_surface.Info.FourCC);

        if (!m_internal_surface.Info.ChromaFormat)
            m_internal_surface.Info.ChromaFormat = ChromaFormatFromFourcc(m_internal_surface.Info.FourCC);

        m_exported_surface = m_internal_surface;
    }

    static mfxStatus Map_impl(mfxFrameSurface1* surface, mfxU32 flags)
    {
        MFX_CHECK_NULL_PTR1(surface);
        MFX_CHECK_HDL(surface->FrameInterface);
        MFX_CHECK_HDL(surface->FrameInterface->Context);

        MFX_SAFE_CALL(reinterpret_cast<mfxFrameSurfaceBaseInterface*>(surface->FrameInterface->Context)->Lock(flags));

        // User may try to Map surface using copy of mfxFrameSurface1 object, so we have to copy internally set pointers
        reinterpret_cast<mfxFrameSurfaceInterfaceImpl*>(surface->FrameInterface->Context)->CopyPointers(&surface->Data);

        return MFX_ERR_NONE;
    }

    static mfxStatus Unmap_impl(mfxFrameSurface1* surface)
    {
        MFX_CHECK_NULL_PTR1(surface);
        MFX_CHECK_HDL(surface->FrameInterface);
        MFX_CHECK_HDL(surface->FrameInterface->Context);

        MFX_SAFE_CALL(reinterpret_cast<mfxFrameSurfaceBaseInterface*>(surface->FrameInterface->Context)->Unlock());

        // User may try to Map surface using copy of mfxFrameSurface1 object, so we have to copy internally set pointers
        // (in Unmap case it means that data pointers will be zeroed if surface becomes unmapped)
        reinterpret_cast<mfxFrameSurfaceInterfaceImpl*>(surface->FrameInterface->Context)->CopyPointers(&surface->Data);

        return MFX_ERR_NONE;
    }

    static mfxStatus GetNativeHandle_impl(mfxFrameSurface1* surface, mfxHDL* resource, mfxResourceType* resource_type)
    {
        MFX_CHECK_NULL_PTR3(surface, resource, resource_type);
        MFX_CHECK_HDL(surface->FrameInterface);
        MFX_CHECK_HDL(surface->FrameInterface->Context);

        std::tie(*resource, *resource_type) = reinterpret_cast<mfxFrameSurfaceBaseInterface*>(surface->FrameInterface->Context)->GetNativeHandle();
        MFX_CHECK(*resource, MFX_ERR_UNSUPPORTED);

        return MFX_ERR_NONE;
    }

    static mfxStatus GetDeviceHandle_impl(mfxFrameSurface1* surface, mfxHDL* device_handle, mfxHandleType* device_type)
    {
        MFX_CHECK_NULL_PTR3(surface, device_handle, device_type);
        MFX_CHECK_HDL(surface->FrameInterface);
        MFX_CHECK_HDL(surface->FrameInterface->Context);

        std::tie(*device_handle, *device_type) = reinterpret_cast<mfxFrameSurfaceBaseInterface*>(surface->FrameInterface->Context)->GetDeviceHandle();
        MFX_CHECK(*device_handle, MFX_ERR_UNSUPPORTED);

        return MFX_ERR_NONE;
    }

    static mfxStatus Synchronize_impl(mfxFrameSurface1* surface, mfxU32 timeout)
    {
        MFX_CHECK_NULL_PTR1(surface);
        MFX_CHECK_HDL(surface->FrameInterface);
        MFX_CHECK_HDL(surface->FrameInterface->Context);

        return
            reinterpret_cast<mfxFrameSurfaceBaseInterface*>(surface->FrameInterface->Context)->Synchronize(timeout);
    }

    static mfxStatus QueryInterface_impl(mfxFrameSurface1* surface, mfxGUID guid, mfxHDL* iface)
    {
        MFX_CHECK_NULL_PTR2(surface, iface);
        MFX_CHECK_HDL(surface->FrameInterface);
        MFX_CHECK_HDL(surface->FrameInterface->Context);

        if (guid == MFX_GUID_SURFACE_POOL)
        {
            *iface = reinterpret_cast<mfxFrameSurfaceBaseInterface*>(surface->FrameInterface->Context)->QueryParentPool();

            MFX_CHECK(*iface, MFX_ERR_NOT_INITIALIZED);

            return MFX_ERR_NONE;
        }

        MFX_RETURN(MFX_ERR_UNSUPPORTED);
    }

    static mfxStatus Export_impl(mfxFrameSurface1* surface, mfxSurfaceHeader export_header, mfxSurfaceHeader** exported_surface)
    {
        MFX_CHECK_NULL_PTR1(surface);
        MFX_CHECK_HDL(surface->FrameInterface);
        MFX_CHECK_HDL(surface->FrameInterface->Context);

        // This is virtual function call, so it will be dispatched to Export function defined in current child class
        return reinterpret_cast<mfxFrameSurfaceBaseInterface*>(surface->FrameInterface->Context)->Export(export_header, exported_surface);
    }

    virtual mfxStatus CreateExportSurface(const mfxSurfaceHeader& /*export_header*/, mfxSurfaceBase*& /*exported_surface*/)
    {
        MFX_RETURN(MFX_ERR_NOT_IMPLEMENTED);
    }

    bool ReallocAllowed(const mfxFrameInfo& frame_info) const
    {
        mfxU16 bitdepth_luma   = frame_info.BitDepthLuma   ? frame_info.BitDepthLuma   : BitDepthFromFourcc(frame_info.FourCC);
        mfxU16 bitdepth_chroma = frame_info.BitDepthChroma ? frame_info.BitDepthChroma : BitDepthFromFourcc(frame_info.FourCC);
        mfxU16 chroma_format   = frame_info.ChromaFormat   ? frame_info.ChromaFormat   : ChromaFormatFromFourcc(frame_info.FourCC);

        bool realloc_allowed = frame_info.FourCC == m_internal_surface.Info.FourCC
                            && bitdepth_luma     == m_internal_surface.Info.BitDepthLuma
                            && bitdepth_chroma   == m_internal_surface.Info.BitDepthChroma
                            && frame_info.Shift  == m_internal_surface.Info.Shift
                            && chroma_format     == m_internal_surface.Info.ChromaFormat;

        return realloc_allowed;
    }

    void CopyPointers(mfxFrameData* frame_data) const
    {
        if (!frame_data)
            return;

        copy_frame_surface_pixel_pointers(*frame_data, m_internal_surface.Data);
    }

    void DetachExported(mfxSurfaceBase* exp_surface)
    {
        std::lock_guard<std::mutex> guard(m_mutex);

        auto it = std::find_if(std::begin(m_converted_surfaces_for_export), std::end(m_converted_surfaces_for_export),
            [exp_surface](const pExpSurf& p_exported_surf)
            {
                return p_exported_surf.get() == exp_surface;
            });

        if (it == std::end(m_converted_surfaces_for_export))
        {
            std::ignore = MFX_STS_TRACE(MFX_WRN_OUT_OF_RANGE);
            return;
        }

        // This function is called from exported surface destructor, so no need to decrease reference here, object is already being deleted
        it->release();

        m_converted_surfaces_for_export.erase(it);
    }

    // Will be returned to user, to protect original fields of mfxFrameSurface1 from zeroing on user side
    mfxFrameSurface1   m_exported_surface = {};

protected:

#define MFX_RELEASE_EXPORTED                                   \
    [](mfxSurfaceBase* surface)                                \
    {                                                          \
        /* Surface is being deleted in destructor of container,
           so no need to recursevely update it's content */    \
        surface->DetachBaseSurface();                          \
                                                               \
        std::ignore = MFX_STS_TRACE(surface->Release());       \
    }

    using pExpSurf = std::unique_ptr<mfxSurfaceBase, void(*)(mfxSurfaceBase* surface)>;

    mfxStatus Export(const mfxSurfaceHeader& export_header, mfxSurfaceHeader** exported_surface) override
    {

        MFX_CHECK_NULL_PTR1(exported_surface);

        std::lock_guard<std::mutex> guard(m_mutex);

        /*
        if ((export_header.SurfaceFlags == MFX_SURFACE_FLAG_DEFAULT) || (export_header.SurfaceFlags & MFX_SURFACE_FLAG_EXPORT_SHARED))
        {
            // First check if we already exported current surface to this type
            auto it = std::find_if(std::begin(m_converted_surfaces_for_export), std::end(m_converted_surfaces_for_export),
                [&export_header](const pExpSurf& p_exported_surf)
                {
                        // Check that surface is of same type
                    return (export_header.SurfaceType == p_exported_surf->Header.SurfaceType)
                        // and export flags are compatible:
                        // only shared (no-copy) export is allowed for reexport (returning same object to user)
                        && (p_exported_surf->Header.SurfaceFlags & MFX_SURFACE_FLAG_EXPORT_SHARED);
                });

            if (it != std::end(m_converted_surfaces_for_export))
            {
                p_exported_surf->AddRef();
                *exported_surface = (*it)->GetExport();
                return MFX_ERR_NONE;
            }
        }

        // Need to create new export surface
        */

        // Here we go into overload of Export in derived class, here actual exported surface is created
        mfxSurfaceBase* tmp_exported_surface = nullptr;
        MFX_SAFE_CALL(this->CreateExportSurface(export_header, tmp_exported_surface));

        MFX_CHECK_NULL_PTR1(tmp_exported_surface);

        m_converted_surfaces_for_export.emplace_back(pExpSurf(tmp_exported_surface, MFX_RELEASE_EXPORTED));

        *exported_surface = tmp_exported_surface->GetExport();

        return MFX_ERR_NONE;
    }

    mfxFrameSurface1    m_internal_surface = {};

    std::list<pExpSurf> m_converted_surfaces_for_export;

    mutable std::mutex  m_mutex;

#undef MFX_RELEASE_EXPORTED
};

class mfxSurfaceArrayImpl;
template <>
struct mfxRefCountableInstance<mfxSurfaceArray>
{
    static mfxRefCountable* Get(mfxSurfaceArray* object)
    { return reinterpret_cast<mfxRefCountable*>(object->Context); }
};

class mfxSurfaceArrayImpl : public mfxRefCountableImpl<mfxSurfaceArray>
{
public:
    static mfxSurfaceArrayImpl* Create()
    {
        mfxSurfaceArrayImpl* surfArr = new mfxSurfaceArrayImpl();
        surfArr->AddRef();
        return surfArr;
    }

    void AddSurface(mfxFrameSurface1* surface)
    {
        std::lock_guard<std::mutex> guard(m_mutex);

        m_surfaces.push_back(surface);

        if (surface)
            std::ignore = MFX_STS_TRACE(AddRefSurface(*surface));

        Surfaces    =         m_surfaces.data();
        NumSurfaces = (mfxU32)m_surfaces.size();
    }

protected:
    void Close() override
    {
        for (auto surface : m_surfaces)
        {
            if (surface)
                std::ignore = MFX_STS_TRACE(ReleaseSurface(*surface));
        }
    }

private:
    mfxSurfaceArrayImpl()
    {
        Context = static_cast<mfxRefCountable*>(this);
        Version.Version = MFX_SURFACEARRAY_VERSION;
    }

    std::vector<mfxFrameSurface1*> m_surfaces;
    std::mutex m_mutex;
};

#ifdef MFX_ENABLE_ENCODE_STATS
class mfxEncodeStatsContainerImpl : public mfxRefCountableImpl<mfxEncodeStatsContainer>
{
public:
    mfxStatus AllocFrameStatsBuf()
    {
        std::lock_guard<std::mutex> guard(m_mutex);

        if (this->EncodeFrameStats == nullptr)
        {
            this->EncodeFrameStats = new mfxEncodeFrameStats{};

            MFX_CHECK(this->EncodeFrameStats, MFX_ERR_MEMORY_ALLOC);
        }
        else
        {
            *this->EncodeFrameStats = {};
        }

        return MFX_ERR_NONE;
    }

    mfxStatus AllocBlkStatsBuf(mfxU32 numBlk)
    {
        MFX_CHECK(numBlk, MFX_ERR_INVALID_VIDEO_PARAM);

        std::lock_guard<std::mutex> guard(m_mutex);

        if (this->EncodeBlkStats == nullptr)
        {
            this->EncodeBlkStats = new mfxEncodeBlkStats{};
            MFX_CHECK(this->EncodeBlkStats, MFX_ERR_MEMORY_ALLOC);
        }

        MFX_CHECK_STS(AllocBlkStatsArray(numBlk));

        return MFX_ERR_NONE;
    }

protected:
    template <typename T>
    static void Delete(T*& p)
    {
        if (p)
        {
            delete p;
            p = nullptr;
        }
    }

    template <typename T>
    static void DeleteArray(T*& p)
    {
        if (p)
        {
            delete[] p;
            p = nullptr;
        }
    }

    template <typename T>
    static mfxStatus AllocBlkStatsBuf(mfxU32 numBlkIn, mfxU32& numBlkOut, T*& buf)
    {
        if (numBlkIn <= numBlkOut && buf)
        {
            for (mfxU32 i = 0; i < numBlkOut; i++)
            {
                buf[i] = {};
            }

            return MFX_ERR_NONE;
        }

        if (numBlkOut)
        {
            DeleteArray(buf);
        }

        buf = new T[numBlkIn]{};
        MFX_CHECK(buf, MFX_ERR_MEMORY_ALLOC);
        numBlkOut = numBlkIn;

        return MFX_ERR_NONE;
    }

    virtual mfxStatus AllocBlkStatsArray(mfxU32 numBlk) = 0;

    virtual void DetroyBlkStatsArray() = 0;

    void Close() override
    {
        if (this->EncodeFrameStats)
        {
            Delete(this->EncodeFrameStats);
        }

        if (this->EncodeBlkStats)
        {
            DetroyBlkStatsArray();
            Delete(this->EncodeBlkStats);
        }
    }

protected:
    mfxEncodeStatsContainerImpl()
    {
        Version.Version      = MFX_ENCODESTATSCONTAINER_VERSION;
        RefInterface.Context = static_cast<mfxRefCountable*>(this);
    }

    std::mutex m_mutex;
};
#endif // MFX_ENABLE_ENCODE_STATS

class RWAcessSurface : public mfxFrameSurfaceInterfaceImpl
{
public:
    RWAcessSurface(const mfxFrameInfo & info, mfxU16 type, mfxMemId mid, FrameAllocatorBase& allocator)
        : mfxFrameSurfaceInterfaceImpl(info, type, mid, allocator)
    {}

    mfxStatus LockRW(std::unique_lock<std::mutex>& guard, bool write, bool nowait);
    mfxStatus UnlockRW();

    // Functions below should be called from thread-safe context
    mfxU32 NumReaders() const { return m_read_locks; }
    bool   Locked()     const { return m_write_lock || m_read_locks != 0; }

private:

    // This class provides shared access to read (with possible wait if write lock was acquired and MFX_MAP_NOWAIT wasn't set)
    // and exclusive write access with immediate return if read lock was acquired

    std::condition_variable m_wait_before_read;
    mfxU32                  m_read_locks = 0u;
    bool                    m_write_lock = false;
};


template <>
struct mfxRefCountableInstance<mfxSurfaceInterface>
{
    static mfxRefCountable* Get(mfxSurfaceInterface* object)
    {
        return reinterpret_cast<mfxRefCountable*>(object->Context);
    }
};

template<class SurfaceType>
class mfxSurfaceImpl : public mfxSurfaceBase, public SurfaceType
{
public:
    mfxSurfaceImpl(const mfxSurfaceHeader& export_header, mfxFrameSurfaceInterfaceImpl* p_base_surface)
        : mfxSurfaceBase(export_header, p_base_surface)
        , SurfaceType()
    {
        mfxSurfaceInterface::Header.StructSize = sizeof(SurfaceType);
        SurfaceType::SurfaceInterface = *(static_cast<mfxSurfaceInterface*>(this));
    }

    mfxSurfaceHeader* GetExport() override
    {
        m_surface_for_export = *(static_cast<SurfaceType*>(this));

        return &m_surface_for_export.SurfaceInterface.Header;
    }

protected:
    void SetResultedExportType(mfxU32 export_type)
    {
        mfxSurfaceInterface::Header.SurfaceFlags = SurfaceType::SurfaceInterface.Header.SurfaceFlags = export_type;
    }

    SurfaceType m_surface_for_export = {};

};

// This stub used for allocators which don't need staging surfaces
class staging_adapter_stub
{
public:
    staging_adapter_stub(mfxHDL = nullptr)
    {}

    operator mfxHDL() const { return nullptr; }

    void SetDevice(mfxHDL)
    {}
};

struct mfxFrameSurface1_sw : public RWAcessSurface
{
    static mfxFrameSurface1_sw* Create(const mfxFrameInfo& info, mfxU16 type, mfxMemId mid, std::shared_ptr<staging_adapter_stub>& staging_adapter, mfxHDL device, mfxU32 context, FrameAllocatorBase& allocator,
        mfxSurfaceHeader* import_surface)
    {
        // Import of SW surfaces is not supported right now
        MFX_CHECK_WITH_THROW_STS(!import_surface, MFX_ERR_UNSUPPORTED);

        auto surface = new mfxFrameSurface1_sw(info, type, mid, staging_adapter, device, context, allocator);
        surface->AddRef();
        return surface;
    }

    ~mfxFrameSurface1_sw()
    {
        // Unmap surface if it is still mapped
        while (Locked())
        {
            if (MFX_FAILED(Unlock()))
                break;
        }
    }

    mfxStatus                          Lock(mfxU32 flags)      override;
    mfxStatus                          Unlock()                override;
    std::pair<mfxHDL, mfxResourceType> GetNativeHandle() const override { return { nullptr, mfxResourceType(0) }; }
    std::pair<mfxHDL, mfxHandleType>   GetDeviceHandle() const override { return { nullptr, mfxHandleType(0)   }; }
    mfxStatus Export(const mfxSurfaceHeader&, mfxSurfaceHeader**) override
    {
        MFX_RETURN(MFX_ERR_NOT_IMPLEMENTED);
    }

    mfxStatus GetHDL(mfxHDL& handle) const
    {
        handle = reinterpret_cast<mfxHDL>(GetMid());
        return MFX_ERR_NONE;
    }

    mfxStatus Realloc(const mfxFrameInfo & info);

    static mfxU16 AdjustType(mfxU16 type)
    {
        return AdjustTypeInternal(type);
    }

protected:
    mfxFrameSurface1_sw(const mfxFrameInfo& info, mfxU16 type, mfxMemId mid, std::shared_ptr<staging_adapter_stub>& staging_adapter, mfxHDL device, mfxU32 context, FrameAllocatorBase& allocator);

    std::unique_ptr<mfxU8, void(*)(void*)> m_data;
};

using FlexibleFrameAllocatorSW = FlexibleFrameAllocator<mfxFrameSurface1_sw, staging_adapter_stub>;

const size_t MFX_MAX_NUM_COLOR_PLANES = 4;

using uniq_ptr_mfx_shared_lib_holder = std::unique_ptr<mfx::mfx_shared_lib_holder>;

class ImportExportHelper : private std::map<mfxSurfaceType, uniq_ptr_mfx_shared_lib_holder>
{
public:

    mfx::mfx_shared_lib_holder* GetHelper(mfxSurfaceType shared_library_type);

    // Write a specialization for desired convertion in dedicated source code file
    template <mfxSurfaceType SharedLibType>
    static uniq_ptr_mfx_shared_lib_holder LoadAndInit();

private:
    std::mutex m_mutex;

};

#endif