-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSoyAvf.mm
executable file
·1157 lines (962 loc) · 41.1 KB
/
SoyAvf.mm
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
#include "SoyAvf.h"
#include "SoyH264.h"
#include "SoyFilesystem.h"
#include "SoyFourcc.h"
#include <CoreMedia/CMBase.h>
#include <VideoToolbox/VTBase.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreVideo/CoreVideo.h>
#include <CoreMedia/CMSampleBuffer.h>
#include <CoreMedia/CMFormatDescription.h>
#include <CoreMedia/CMTime.h>
#include <VideoToolbox/VTSession.h>
#include <VideoToolbox/VTCompressionProperties.h>
#include <VideoToolbox/VTCompressionSession.h>
#include <VideoToolbox/VTDecompressionSession.h>
#include <VideoToolbox/VTErrors.h>
#include "magic_enum/include/magic_enum.hpp"
#include "AvfPixelBuffer.h"
#if defined(__OBJC__)
SoyTime Soy::Platform::GetTime(CMTime Time)
{
if ( CMTIME_IS_INVALID( Time ) )
return SoyTime();
// missing CMTimeGetSeconds ? link to the CoreMedia framework :)
Float64 TimeSec = CMTimeGetSeconds(Time);
UInt64 TimeMs = 1000.f*TimeSec;
return SoyTime( std::chrono::milliseconds(TimeMs) );
}
#endif
#if defined(__OBJC__)
CMTime Soy::Platform::GetTime(SoyTime Time)
{
return CMTimeMake( Time.mTime, 1000 );
}
#endif
#define CV_VIDEO_TYPE_META(Enum,SoyFormat) TCvVideoTypeMeta( Enum, #Enum, SoyFormat )
#define CV_VIDEO_INVALID_ENUM 0
class TCvVideoTypeMeta
{
public:
TCvVideoTypeMeta(OSType Enum,const char* EnumName,SoyPixelsFormat::Type SoyFormat) :
mPlatformFormat ( Enum ),
mName ( EnumName ),
mSoyFormat ( SoyFormat )
{
if ( !IsValid() )
throw Soy::AssertException("Expected valid enum - or invalid enum is bad" );
}
TCvVideoTypeMeta() :
mPlatformFormat ( CV_VIDEO_INVALID_ENUM ),
mName ( "Invalid enum" ),
mSoyFormat ( SoyPixelsFormat::Invalid )
{
}
bool IsValid() const { return mPlatformFormat != CV_VIDEO_INVALID_ENUM; }
bool operator==(const OSType& Enum) const { return mPlatformFormat == Enum; }
bool operator==(const SoyPixelsFormat::Type& Format) const { return mSoyFormat == Format; }
public:
OSType mPlatformFormat;
SoyPixelsFormat::Type mSoyFormat;
std::string mName;
};
std::ostream& operator<<(std::ostream& out,const AVAssetExportSessionStatus& in)
{
out << magic_enum::enum_name(in);
return out;
}
static TCvVideoTypeMeta Cv_PixelFormatMap[] =
{
/*
// from avfDecoder ResolveFormat(id)
// gr: RGBA never seems to decode, but with no error[on osx]
case SoyPixelsFormat::RGBA:
// BGRA is internal format on IOS so return that as default
case SoyPixelsFormat::BGRA:
default:
*/
CV_VIDEO_TYPE_META( kCVPixelFormatType_OneComponent8, SoyPixelsFormat::Luma ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_24RGB, SoyPixelsFormat::RGB ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_24BGR, SoyPixelsFormat::BGR ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_32BGRA, SoyPixelsFormat::BGRA ),
// gr: PopFace creating a pixel buffer from a unity "argb" texture, failed as RGBA is unsupported...
// gr: ARGB is accepted, but channels are wrong
CV_VIDEO_TYPE_META( kCVPixelFormatType_32RGBA, SoyPixelsFormat::RGBA ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_32ARGB, SoyPixelsFormat::ARGB ),
// gr: no colourspace distinction!
CV_VIDEO_TYPE_META( kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, SoyPixelsFormat::Yuv_8_88 ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_420YpCbCr8BiPlanarFullRange, SoyPixelsFormat::Yuv_8_88 ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_422YpCbCr_4A_8BiPlanar, SoyPixelsFormat::Invalid ), // YuvAlpha_8888_8
// gr: this is CHROMA|YUV! not YUV, this is why the fourcc is 2vuy
// kCVPixelFormatType_422YpCbCr8 = '2vuy', /* Component Y'CbCr 8-bit 4:2:2, ordered Cb Y'0 Cr Y'1 */
CV_VIDEO_TYPE_META( kCVPixelFormatType_422YpCbCr8, SoyPixelsFormat::Uvy_8_88 ),
// todo: check these
// gr: no colourspace distinction!
// no planar = interlaced
CV_VIDEO_TYPE_META( kCVPixelFormatType_422YpCbCr8FullRange, SoyPixelsFormat::YYuv_8888 ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_422YpCbCr8_yuvs, SoyPixelsFormat::YYuv_8888 ),
// gr: planar = 3 planes
CV_VIDEO_TYPE_META( kCVPixelFormatType_420YpCbCr8Planar, SoyPixelsFormat::Yuv_8_8_8 ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_420YpCbCr8PlanarFullRange, SoyPixelsFormat::Yuv_8_8_8 ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_1Monochrome, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_2Indexed, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_4Indexed, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_8Indexed, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_1IndexedGray_WhiteIsZero, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_2IndexedGray_WhiteIsZero, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_4IndexedGray_WhiteIsZero, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_8IndexedGray_WhiteIsZero, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_16BE555, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_16LE555, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_16LE5551, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_16BE565, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_16LE565, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_32ABGR, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_64ARGB, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_48RGB, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_32AlphaGray, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_16Gray, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_4444YpCbCrA8, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_4444YpCbCrA8R, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_444YpCbCr8, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_422YpCbCr16, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_422YpCbCr10, SoyPixelsFormat::Invalid ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_444YpCbCr10, SoyPixelsFormat::Invalid ),
// the logitech C22 has this format, which apparently might be a kind of motion jpeg
CV_VIDEO_TYPE_META( 'dmb1', SoyPixelsFormat::Invalid ),
// hdis, fdis
CV_VIDEO_TYPE_META( kCVPixelFormatType_DisparityFloat32, SoyPixelsFormat::DepthDisparityFloat ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_DisparityFloat16, SoyPixelsFormat::DepthDisparityHalf ),
// hdep, fdep
CV_VIDEO_TYPE_META( kCVPixelFormatType_DepthFloat32, SoyPixelsFormat::DepthFloatMetres ),
CV_VIDEO_TYPE_META( kCVPixelFormatType_DepthFloat16, SoyPixelsFormat::DepthHalfMetres ),
};
/*
std::shared_ptr<TMediaPacket> Avf::GetFormatDescriptionPacket(CMSampleBufferRef SampleBuffer,size_t ParamIndex,SoyMediaFormat::Type Format,size_t StreamIndex)
{
auto Desc = CMSampleBufferGetFormatDescription( SampleBuffer );
std::shared_ptr<TMediaPacket> pPacket( new TMediaPacket() );
auto& Packet = *pPacket;
Packet.mMeta = GetStreamMeta( Desc );
Packet.mMeta.mStreamIndex = StreamIndex;
Packet.mMeta.mCodec = Format;
auto& Data = Packet.mData;
Data.PushBack(0);
Data.PushBack(0);
Data.PushBack(0);
Data.PushBack(1);
// http://stackoverflow.com/questions/24884827/possible-locations-for-sequence-picture-parameter-sets-for-h-264-stream
if ( Format == SoyMediaFormat::H264_SPS_ES )
{
// https://cardinalpeak.com/blog/the-h-264-sequence-parameter-set/
// https://tools.ietf.org/html/rfc6184#section-7.4.1
auto Byte = H264::EncodeNaluByte( H264NaluContent::SequenceParameterSet,H264NaluPriority::Important );
Data.PushBack( Byte );
}
if ( Format == SoyMediaFormat::H264_PPS_ES )
{
auto Byte = H264::EncodeNaluByte(H264NaluContent::PictureParameterSet, H264NaluPriority::Important );
Data.PushBack( Byte );
}
GetFormatDescriptionData( GetArrayBridge( Data ), Desc, ParamIndex );
return pPacket;
}
*/
SoyPixelsMeta Avf::GetPixelMeta(CVPixelBufferRef PixelBuffer)
{
auto Height = CVPixelBufferGetHeight( PixelBuffer );
auto Width = CVPixelBufferGetWidth( PixelBuffer );
auto Format = CVPixelBufferGetPixelFormatType( PixelBuffer );
auto SoyFormat = Avf::GetPixelFormat( Format );
return SoyPixelsMeta( Width, Height, SoyFormat );
}
void Avf::GetFormatDescriptionData(ArrayBridge<uint8>&& Data,CMFormatDescriptionRef FormatDesc,size_t ParamIndex)
{
size_t ParamCount = 0;
auto Result = CMVideoFormatDescriptionGetH264ParameterSetAtIndex( FormatDesc, 0, nullptr, nullptr, &ParamCount, nullptr );
Avf::IsOkay( Result, "Get H264 param 0");
/*
// known bug on ios?
if (status ==
CoreMediaGlue::kCMFormatDescriptionBridgeError_InvalidParameter) {
DLOG(WARNING) << " assuming 2 parameter sets and 4 bytes NAL length header";
pset_count = 2;
nal_size_field_bytes = 4;
*/
if ( ParamIndex >= ParamCount )
throw Soy::AssertException("SPS missing");
const uint8_t* ParamsData = nullptr;;
size_t ParamsSize = 0;
Result = CMVideoFormatDescriptionGetH264ParameterSetAtIndex( FormatDesc, ParamIndex, &ParamsData, &ParamsSize, nullptr, nullptr);
Avf::IsOkay( Result, "Failed to get H264 param X" );
Data.PushBackArray( GetRemoteArray( ParamsData, ParamsSize ) );
}
// lots of errors in macerrors.h with no string conversion :/
#define TESTENUMERROR(e,Enum) if ( (e) == (Enum) ) return #Enum ;
// http://stackoverflow.com/questions/2196869/how-do-you-convert-an-iphone-osstatus-code-to-something-useful
std::string Avf::GetString(OSStatus Status)
{
//[NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil]);
TESTENUMERROR(Status,kVTPropertyNotSupportedErr);
TESTENUMERROR(Status,kVTPropertyReadOnlyErr);
TESTENUMERROR(Status,kVTParameterErr);
TESTENUMERROR(Status,kVTInvalidSessionErr);
TESTENUMERROR(Status,kVTAllocationFailedErr);
TESTENUMERROR(Status,kVTPixelTransferNotSupportedErr);
TESTENUMERROR(Status,kVTCouldNotFindVideoDecoderErr);
TESTENUMERROR(Status,kVTCouldNotCreateInstanceErr);
TESTENUMERROR(Status,kVTCouldNotFindVideoEncoderErr);
TESTENUMERROR(Status,kVTVideoDecoderBadDataErr);
TESTENUMERROR(Status,kVTVideoDecoderUnsupportedDataFormatErr);
TESTENUMERROR(Status,kVTVideoDecoderMalfunctionErr);
TESTENUMERROR(Status,kVTVideoEncoderMalfunctionErr);
TESTENUMERROR(Status,kVTVideoDecoderNotAvailableNowErr);
TESTENUMERROR(Status,kVTImageRotationNotSupportedErr);
TESTENUMERROR(Status,kVTVideoEncoderNotAvailableNowErr);
TESTENUMERROR(Status,kVTFormatDescriptionChangeNotSupportedErr);
TESTENUMERROR(Status,kVTInsufficientSourceColorDataErr);
TESTENUMERROR(Status,kVTCouldNotCreateColorCorrectionDataErr);
TESTENUMERROR(Status,kVTColorSyncTransformConvertFailedErr);
TESTENUMERROR(Status,kVTVideoDecoderAuthorizationErr);
TESTENUMERROR(Status,kVTVideoEncoderAuthorizationErr);
TESTENUMERROR(Status,kVTColorCorrectionPixelTransferFailedErr);
TESTENUMERROR(Status,kVTMultiPassStorageIdentifierMismatchErr);
TESTENUMERROR(Status,kVTMultiPassStorageInvalidErr);
TESTENUMERROR(Status,kVTFrameSiloInvalidTimeStampErr);
TESTENUMERROR(Status,kVTFrameSiloInvalidTimeRangeErr);
TESTENUMERROR(Status,kVTCouldNotFindTemporalFilterErr);
TESTENUMERROR(Status,kVTPixelTransferNotPermittedErr);
TESTENUMERROR(Status,kVTColorCorrectionImageRotationFailedErr);
TESTENUMERROR(Status,kVTVideoDecoderRemovedErr);
TESTENUMERROR(Status,kCVReturnInvalidArgument);
TESTENUMERROR(Status,kCMBlockBufferStructureAllocationFailedErr);
TESTENUMERROR(Status,kCMBlockBufferBlockAllocationFailedErr);
TESTENUMERROR(Status,kCMBlockBufferBadCustomBlockSourceErr);
TESTENUMERROR(Status,kCMBlockBufferBadOffsetParameterErr);
TESTENUMERROR(Status,kCMBlockBufferBadLengthParameterErr);
TESTENUMERROR(Status,kCMBlockBufferBadPointerParameterErr);
TESTENUMERROR(Status,kCMBlockBufferEmptyBBufErr);
TESTENUMERROR(Status,kCMBlockBufferUnallocatedBlockErr);
TESTENUMERROR(Status,kCMBlockBufferInsufficientSpaceErr);
TESTENUMERROR(Status,kCVReturnInvalidArgument);
TESTENUMERROR(Status,kCVReturnAllocationFailed);
#if defined(AVAILABLE_MAC_OS_X_VERSION_10_11_AND_LATER)
TESTENUMERROR(Status,kCVReturnUnsupported);
#endif
TESTENUMERROR(Status,kCVReturnInvalidDisplay);
TESTENUMERROR(Status,kCVReturnDisplayLinkAlreadyRunning);
TESTENUMERROR(Status,kCVReturnDisplayLinkNotRunning);
TESTENUMERROR(Status,kCVReturnDisplayLinkCallbacksNotSet);
TESTENUMERROR(Status,kCVReturnInvalidPixelFormat);
TESTENUMERROR(Status,kCVReturnInvalidSize);
TESTENUMERROR(Status,kCVReturnInvalidPixelBufferAttributes);
TESTENUMERROR(Status,kCVReturnPixelBufferNotOpenGLCompatible);
TESTENUMERROR(Status,kCVReturnPixelBufferNotMetalCompatible);
TESTENUMERROR(Status,kCVReturnWouldExceedAllocationThreshold);
TESTENUMERROR(Status,kCVReturnPoolAllocationFailed);
TESTENUMERROR(Status,kCVReturnInvalidPoolAttributes);
// decompression gives us this
TESTENUMERROR(Status,MACH_RCV_TIMED_OUT);
TESTENUMERROR(Status,kCVReturnInvalidPoolAttributes);
// corefoundation versions of VT errors
switch ( static_cast<sint32>(Status) )
{
case -8961: return "kVTPixelTransferNotSupportedErr -8961";
case -8969: return "kVTVideoDecoderBadDataErr -8969";
case -8970: return "kVTVideoDecoderUnsupportedDataFormatErr -8970";
case -8960: return "kVTVideoDecoderMalfunctionErr -8960";
default:
break;
}
// gr: can't find any documentation on this value.
if ( static_cast<sint32>(Status) == -12349 )
return "Unknown VTDecodeFrame error -12349";
// gr: can't find any documentation on this value.
if ( static_cast<sint32>(Status) == -12780 )
return "Unknown VTCompressionSessionEncodeFrame error -12780. On IOS this seems fail with a YUV format but not BGRA32";
// as integer..
std::stringstream Error;
Error << "OSStatus = " << static_cast<sint32>(Status);
return Error.str();
// could be fourcc?
Soy::TFourcc Fourcc( CFSwapInt32HostToBig(Status) );
return Fourcc.GetString();
/*
// example with specific bundles...
NSBundle *bundle = [NSBundle bundleWithIdentifier:@"com.apple.security"];
NSString *key = [NSString stringWithFormat:@"%d", (int)Status];
auto* StringNs = [bundle localizedStringForKey:key value:key table:@"SecErrorMessages"];
return Soy::NSStringToString( StringNs );
*/
}
/*
std::shared_ptr<AvfCompressor::TInstance> AvfCompressor::Allocate(const TCasterParams& Params)
{
return std::shared_ptr<AvfCompressor::TInstance>( new AvfCompressor::TInstance(Params) );
}
*/
CFStringRef Avf::GetProfile(H264Profile::Type Profile)
{
switch ( Profile )
{
case H264Profile::Baseline: return kVTProfileLevel_H264_Baseline_AutoLevel;
default:
std::Debug << "Unhandled profile type " << Profile << " using baseline" << std::endl;
return kVTProfileLevel_H264_Baseline_AutoLevel;
}
}
/*
CMFormatDescriptionRef Avf::GetFormatDescription(const TStreamMeta& Stream)
{
CFAllocatorRef Allocator = nil;
CMFormatDescriptionRef FormatDesc = nullptr;
if ( SoyMediaFormat::IsH264( Stream.mCodec ) )
{
auto& Sps = Stream.mSps;
auto& Pps = Stream.mPps;
if ( Sps.IsEmpty() ) throw Soy::AssertException("H264 encoder requires SPS beforehand" );
if ( Pps.IsEmpty() ) throw Soy::AssertException("H264 encoder requires PPS beforehand" );
BufferArray<const uint8_t*,2> Params;
BufferArray<size_t,2> ParamSizes;
Params.PushBack( Sps.GetArray() );
ParamSizes.PushBack( Sps.GetDataSize() );
Params.PushBack( Pps.GetArray() );
ParamSizes.PushBack( Pps.GetDataSize() );
auto NaluLengthSize = H264::GetNaluLengthSize( Stream.mCodec );
// don't think ios supports annexb, so we will have to convert, assume we can do this later...
if ( NaluLengthSize == 0 )
{
// gr: save this somewhere for when we convert frames from annexb
NaluLengthSize = H264::GetNaluLengthSize( SoyMediaFormat::H264_32 );
std::Debug << "Avf compressor doesn't support annexb/h264 elementry streams, forcing " << NaluLengthSize*8 << "bit NALU header size" << std::endl;
}
// -12712 http://stackoverflow.com/questions/25078364/cmvideoformatdescriptioncreatefromh264parametersets-issues
auto Result = CMVideoFormatDescriptionCreateFromH264ParameterSets( Allocator, Params.GetSize(), Params.GetArray(), ParamSizes.GetArray(), size_cast<int>(NaluLengthSize), &FormatDesc );
Avf::IsOkay( Result, "CMVideoFormatDescriptionCreateFromH264ParameterSets" );
}
else
{
CFDictionaryRef Extensions = nullptr;
CMMediaType MediaType;
FourCharCode MediaCodec;
GetMediaType( MediaType, MediaCodec, Stream.mCodec );
/ *
// extensions to dictionary
if ( !Stream.mExtensions.IsEmpty() )
{
auto Data = ArrayToNSData( GetArrayBridge(Stream.mExtensions) );
NSError* Error = nullptr;
NSPropertyListReadOptions Options;
NSDictionary* ExtensionsDict = [NSPropertyListSerialization propertyListWithData:Data options:Options format:nullptr error:&Error];
Extensions = (__bridge CFDictionaryRef)ExtensionsDict;
}
*//*
auto Result = CMFormatDescriptionCreate( Allocator, MediaType, MediaCodec, Extensions, &FormatDesc );
Avf::IsOkay( Result, "CMFormatDescriptionCreate" );
}
return FormatDesc;
}
*/
// gr: speed this up! (or reduce usage) all the obj-c calls are expensive.
TStreamMeta Avf::GetStreamMeta(CMFormatDescriptionRef FormatDesc)
{
TStreamMeta Meta;
auto FourccOrig = CMFormatDescriptionGetMediaSubType(FormatDesc);
Soy::TFourcc Fourcc(FourccOrig);
size_t H264LengthSize = 0;
//if ( SoyMediaFormat::IsH264Fourcc(Fourcc) )
{
// gr: this is okay if it goes wrong, probably just doesn't apply to this format
//auto Result =
int NalUnitLength = 0;
CMVideoFormatDescriptionGetH264ParameterSetAtIndex( FormatDesc, 0, nullptr, nullptr, nullptr, &NalUnitLength );
if ( NalUnitLength < 0 )
NalUnitLength = 0;
H264LengthSize = size_cast<size_t>( NalUnitLength );
//AvfCompressor::IsOkay( Result, "Get H264 param NAL size", false );
}
// PCM needs a number too
{
auto* pAudioFormat = CMAudioFormatDescriptionGetStreamBasicDescription( FormatDesc );
if ( pAudioFormat )
{
auto& AudioFormat = *pAudioFormat;
Meta.mAudioSampleRate = AudioFormat.mSampleRate;
Meta.mAudioBytesPerPacket = AudioFormat.mBytesPerPacket;
Meta.mAudioBytesPerFrame = AudioFormat.mBytesPerFrame;
Meta.mAudioFramesPerPacket = AudioFormat.mFramesPerPacket;
Meta.mChannelCount = AudioFormat.mChannelsPerFrame;
// change this to be like H264 different formats; AAC_8, AAC_16, AAC_float etc
Meta.mAudioBitsPerChannel = AudioFormat.mBitsPerChannel;
H264LengthSize = size_cast<size_t>(Meta.mAudioBitsPerChannel);
// gr: if auto bits per channel is zero, it's a compressed format
if ( H264LengthSize == 0 )
Meta.mCompressed = true;
}
}
Meta.mCodecFourcc = Fourcc;
if ( SoyMediaFormat::IsH264( Meta.mCodecFourcc ) )
{
Avf::GetFormatDescriptionData( GetArrayBridge(Meta.mSps), FormatDesc, 0 );
Avf::GetFormatDescriptionData( GetArrayBridge(Meta.mPps), FormatDesc, 1 );
}
Boolean usePixelAspectRatio = false;
Boolean useCleanAperture = false;
auto Dim = CMVideoFormatDescriptionGetPresentationDimensions( FormatDesc, usePixelAspectRatio, useCleanAperture );
Meta.mPixelMeta.DumbSetWidth( Dim.width );
Meta.mPixelMeta.DumbSetHeight( Dim.height );
Meta.mPixelMeta.DumbSetFormat( GetPixelFormat( Meta.mCodecFourcc ) );
//std::stringstream Debug;
//Debug << "Extensions=" << Soy::Platform::GetExtensions( FormatDesc ) << "; ";
// get specific bits
//GetExtension( Desc, "FullRangeVideo", mMeta.mFullRangeYuv );
// pull out audio meta
{
auto* pAudioFormat = CMAudioFormatDescriptionGetStreamBasicDescription( FormatDesc );
if ( pAudioFormat )
{
auto& AudioFormat = *pAudioFormat;
Meta.mAudioSampleRate = AudioFormat.mSampleRate;
Meta.mAudioBytesPerPacket = AudioFormat.mBytesPerPacket;
Meta.mAudioBytesPerFrame = AudioFormat.mBytesPerFrame;
Meta.mAudioFramesPerPacket = AudioFormat.mFramesPerPacket;
Meta.mChannelCount = AudioFormat.mChannelsPerFrame;
// change this to be like H264 different formats; AAC_8, AAC_16, AAC_float etc
Meta.mAudioBitsPerChannel = AudioFormat.mBitsPerChannel;
// flags per format
static bool DebugFlags = false;
if ( DebugFlags )
std::Debug << Meta.mCodec << " sample flags: " << AudioFormat.mFormatFlags << std::endl;
}
else if ( SoyMediaFormat::IsAudio(Meta.mCodec) )
{
std::Debug << "Warning, format has audio data, but we've detected non-audio format (" << Meta.mCodec << ")" << std::endl;
}
}
/*
// test validity of the conversion (slow!)
static bool DoCompareFormatIntegrity = false;
if ( DoCompareFormatIntegrity )
{
auto TestFormat = GetFormatDescription( Meta );
if ( !CMFormatDescriptionEqual ( TestFormat, FormatDesc ) )
{
/*
auto ExtensionsOld = CMFormatDescriptionGetExtensions( FormatDesc );
auto MediaSubTypeOld = CMFormatDescriptionGetMediaSubType( FormatDesc );
auto MediaTypeOld = CMFormatDescriptionGetMediaType( FormatDesc );
auto PresentationDimOld = CMVideoFormatDescriptionGetPresentationDimensions( FormatDesc, true, true );
auto DimOld = CMVideoFormatDescriptionGetDimensions( FormatDesc );
auto ExtensionsStringOld = Soy::NSDictionaryToString( ExtensionsOld );
auto ExtensionsNew = CMFormatDescriptionGetExtensions( TestFormat );
auto MediaSubTypeNew = CMFormatDescriptionGetMediaSubType( TestFormat );
auto MediaTypeNew = CMFormatDescriptionGetMediaType( TestFormat );
auto PresentationDimNew = CMVideoFormatDescriptionGetPresentationDimensions( TestFormat, true, true );
auto DimNew = CMVideoFormatDescriptionGetDimensions( TestFormat );
auto ExtensionsStringNew = Soy::NSDictionaryToString( ExtensionsNew );
*//*
std::Debug << "Warning: generated meta from description, but not equal when converted back again. " << Meta << std::endl;
}
}
*/
return Meta;
}
/*
void Avf::GetMediaType(CMMediaType& MediaType,FourCharCode& MediaCodec,SoyMediaFormat::Type Format)
{
if ( SoyMediaFormat::IsVideo(Format) )
{
MediaType = kCMMediaType_Video;
}
else if ( SoyMediaFormat::IsAudio(Format) )
{
MediaType = kCMMediaType_Audio;
}
else
{
// throw?
std::stringstream Error;
Error << __func__ << " don't know what CMMediaType to use for " << Format;
throw Soy::AssertException( Error.str() );
}
// using cross platform code instead of AVF definitions like
// kCMVideoCodecType_H264
MediaCodec = SoyMediaFormat::ToFourcc( Format );
}
*/
/*
Avf::TAsset::TAsset(const std::string& Filename)
{
// alloc asset
auto Url = GetUrl( Filename );
NSDictionary *Options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset* Asset = [[AVURLAsset alloc] initWithURL:Url options:Options];
if ( !Asset )
throw Soy::AssertException("Failed to create asset");
if ( !Asset.readable )
std::Debug << "Warning: Asset " << Filename << " reported as not-readable" << std::endl;
mAsset.Retain( Asset );
LoadTracks();
}
void Avf::TAsset::LoadTracks()
{
AVAsset* Asset = mAsset.mObject;
if ( !Asset )
throw Soy::AssertException("Asset expected");
Soy::TSemaphore LoadTracksSemaphore;
__block Soy::TSemaphore& Semaphore = LoadTracksSemaphore;
auto OnCompleted = ^
{
NSError* err = nil;
auto Status = [Asset statusOfValueForKey:@"tracks" error:&err];
if ( Status != AVKeyValueStatusLoaded)
{
std::stringstream Error;
Error << "Error loading tracks: " << Soy::NSErrorToString(err);
Semaphore.OnFailed( Error.str().c_str() );
return;
}
Semaphore.OnCompleted();
};
// load tracks
NSArray* Keys = [NSArray arrayWithObjects:@"tracks",@"playable",@"hasProtectedContent",@"duration",@"preferredTransform",nil];
[Asset loadValuesAsynchronouslyForKeys:Keys completionHandler:OnCompleted];
LoadTracksSemaphore.Wait();
// grab duration
// gr: not valid here with AVPlayer!
// http://stackoverflow.com/a/7052147/355753
auto Duration = Soy::Platform::GetTime( Asset.duration );
// get meta for each track
NSArray* Tracks = [Asset tracks];
for ( int t=0; t<[Tracks count]; t++ )
{
// get and retain track
AVAssetTrack* Track = [Tracks objectAtIndex:t];
if ( !Track )
continue;
TStreamMeta TrackMeta;
TrackMeta.mStreamIndex = t;
TrackMeta.mEncodingBitRate = Track.estimatedDataRate;
// extract meta from track
NSArray* FormatDescriptions = [Track formatDescriptions];
std::stringstream MetaDebug;
if ( !FormatDescriptions )
{
MetaDebug << "Format descriptions missing";
}
else if ( FormatDescriptions.count == 0 )
{
MetaDebug << "Format description count=0";
}
else
{
// use first description, warn if more
if ( FormatDescriptions.count > 1 )
{
MetaDebug << "Found mulitple(" << FormatDescriptions.count << ") format descriptions. ";
}
id DescElement = FormatDescriptions[0];
CMFormatDescriptionRef Desc = (__bridge CMFormatDescriptionRef)DescElement;
TrackMeta = Avf::GetStreamMeta( Desc );
// save format
mStreamFormats[t].reset( new Platform::TMediaFormat( Desc ) );
}
// grab the transform from the video track
TrackMeta.mPixelMeta.DumbSetWidth( Track.naturalSize.width );
TrackMeta.mPixelMeta.DumbSetHeight( Track.naturalSize.height );
TrackMeta.mTransform = Soy::MatrixToVector( Track.preferredTransform, vec2f(TrackMeta.mPixelMeta.GetWidth(),TrackMeta.mPixelMeta.GetHeight()) );
TrackMeta.mDuration = Duration;
mStreams.PushBack( TrackMeta );
}
}
*/
CFStringRef Avf::GetProfile(H264Profile::Type Profile,Soy::TVersion Level)
{
std::map<size_t,CFStringRef> BaselineLevels;
BaselineLevels[0] = kVTProfileLevel_H264_Baseline_AutoLevel;
BaselineLevels[103] = kVTProfileLevel_H264_Baseline_1_3;
BaselineLevels[300] = kVTProfileLevel_H264_Baseline_3_0;
BaselineLevels[301] = kVTProfileLevel_H264_Baseline_3_1;
BaselineLevels[302] = kVTProfileLevel_H264_Baseline_3_2;
BaselineLevels[400] = kVTProfileLevel_H264_Baseline_4_0;
BaselineLevels[401] = kVTProfileLevel_H264_Baseline_4_1;
BaselineLevels[402] = kVTProfileLevel_H264_Baseline_4_2;
BaselineLevels[500] = kVTProfileLevel_H264_Baseline_5_0;
BaselineLevels[501] = kVTProfileLevel_H264_Baseline_5_1;
BaselineLevels[502] = kVTProfileLevel_H264_Baseline_5_2;
std::map<size_t,CFStringRef> MainLevels;
MainLevels[0] = kVTProfileLevel_H264_Main_AutoLevel;
MainLevels[300] = kVTProfileLevel_H264_Main_3_0;
MainLevels[301] = kVTProfileLevel_H264_Main_3_1;
MainLevels[302] = kVTProfileLevel_H264_Main_3_2;
MainLevels[400] = kVTProfileLevel_H264_Main_4_0;
MainLevels[401] = kVTProfileLevel_H264_Main_4_1;
MainLevels[402] = kVTProfileLevel_H264_Main_4_2;
MainLevels[500] = kVTProfileLevel_H264_Main_5_0;
MainLevels[501] = kVTProfileLevel_H264_Main_5_1;
MainLevels[502] = kVTProfileLevel_H264_Main_5_2;
std::map<size_t,CFStringRef> HighLevels;
HighLevels[0] = kVTProfileLevel_H264_High_AutoLevel;
HighLevels[300] = kVTProfileLevel_H264_High_3_0;
HighLevels[301] = kVTProfileLevel_H264_High_3_1;
HighLevels[302] = kVTProfileLevel_H264_High_3_2;
HighLevels[400] = kVTProfileLevel_H264_High_4_0;
HighLevels[401] = kVTProfileLevel_H264_High_4_1;
HighLevels[402] = kVTProfileLevel_H264_High_4_2;
HighLevels[500] = kVTProfileLevel_H264_High_5_0;
HighLevels[501] = kVTProfileLevel_H264_High_5_1;
HighLevels[502] = kVTProfileLevel_H264_High_5_2;
std::map<size_t,CFStringRef> ExtendedLevels;
ExtendedLevels[0] = kVTProfileLevel_H264_Extended_AutoLevel;
ExtendedLevels[500] = kVTProfileLevel_H264_Extended_5_0;
switch ( Profile )
{
case H264Profile::Baseline: return BaselineLevels[Level.GetHundred()];
case H264Profile::Main: return MainLevels[Level.GetHundred()];
case H264Profile::High: return HighLevels[Level.GetHundred()];
case H264Profile::Extended: return ExtendedLevels[Level.GetHundred()];
default:
std::Debug << "Unhandled profile type " << Profile << " using baseline" << std::endl;
return kVTProfileLevel_H264_Baseline_AutoLevel;
}
}
/*
NSString* const Avf::GetFormatType(SoyMediaFormat::Type Format)
{
if ( SoyMediaFormat::IsVideo( Format ) )
return AVMediaTypeVideo;
if ( SoyMediaFormat::IsAudio( Format ) )
return AVMediaTypeAudio;
std::stringstream Error;
Error << "Cannot convert " << Format << " to AVMediaType";
throw Soy::AssertException( Error.str() );
}
*/
const std::map<std::string,NSString *>& GetFileExtensionTypeConversion()
{
static std::map<std::string,NSString *> FileExtensionToType;
if ( FileExtensionToType.empty() )
{
FileExtensionToType["mov"] = AVFileTypeQuickTimeMovie;
FileExtensionToType["qt"] = AVFileTypeQuickTimeMovie;
FileExtensionToType["mp4"] = AVFileTypeMPEG4;
FileExtensionToType["m4v"] = AVFileTypeAppleM4V;
FileExtensionToType["m4a"] = AVFileTypeAppleM4A;
#if defined(AVAILABLE_MAC_OS_X_VERSION_10_11_AND_LATER)
FileExtensionToType["3gp"] = AVFileType3GPP;
FileExtensionToType["3gpp"] = AVFileType3GPP;
FileExtensionToType["sdv"] = AVFileType3GPP;
FileExtensionToType["3g2"] = AVFileType3GPP2;
FileExtensionToType["3gp2"] = AVFileType3GPP2;
#endif
FileExtensionToType["caf"] = AVFileTypeCoreAudioFormat;
FileExtensionToType["wav"] = AVFileTypeWAVE;
FileExtensionToType["wave"] = AVFileTypeWAVE;
FileExtensionToType["aif"] = AVFileTypeAIFF;
FileExtensionToType["aiff"] = AVFileTypeAIFF;
FileExtensionToType["aifc"] = AVFileTypeAIFC;
FileExtensionToType["cdda"] = AVFileTypeAIFC;
FileExtensionToType["amr"] = AVFileTypeAMR;
FileExtensionToType["mp3"] = AVFileTypeMPEGLayer3;
FileExtensionToType["au"] = AVFileTypeSunAU;
FileExtensionToType["ac3"] = AVFileTypeAC3;
#if defined(AVAILABLE_MAC_OS_X_VERSION_10_11_AND_LATER)
FileExtensionToType["eac3"] = AVFileTypeEnhancedAC3;
#endif
}
return FileExtensionToType;
}
NSString* const Avf::GetFileExtensionType(const std::string& Extension)
{
auto& FileExtensionToType = GetFileExtensionTypeConversion();
auto FileTypeIt = FileExtensionToType.find( Extension );
if ( FileTypeIt == FileExtensionToType.end() )
{
std::stringstream Error;
Error << "Failed to match filename extension (" << Extension << ") to file type";
throw Soy::AssertException( Error.str() );
}
return FileTypeIt->second;
}
void Avf::GetFileExtensions(ArrayBridge<std::string>&& Extensions)
{
auto& FileExtensionToType = GetFileExtensionTypeConversion();
for ( auto& it : FileExtensionToType )
{
Extensions.PushBack( it.first );
}
}
bool Avf::IsFormatCompressed(SoyMediaFormat::Type Format)
{
switch ( Format )
{
case SoyMediaFormat::H264_8:
case SoyMediaFormat::H264_16:
case SoyMediaFormat::H264_32:
case SoyMediaFormat::H264_ES:
case SoyMediaFormat::Aac:
return true;
default:
return false;
}
}
bool Avf::IsKeyframe(CMSampleBufferRef SampleBuffer,bool DefaultValue)
{
if ( !SampleBuffer )
return DefaultValue;
// code based on chromium
// https://chromium.googlesource.com/chromium/src/media/+/cea1808de66191f7f1eb48b5579e602c0c781146/cast/sender/h264_vt_encoder.cc
auto Attachments = CMSampleBufferGetSampleAttachmentsArray(SampleBuffer,false);
if ( !Attachments )
return DefaultValue;
// gr: why 0? more documentation please chromium
if ( CFArrayGetCount(Attachments) < 1 )
return DefaultValue;
auto sample_attachments = static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(Attachments, 0));
if ( !sample_attachments )
return DefaultValue;
// If the NotSync key is not present, it implies Sync, which indicates a
// keyframe (at least I think, VT documentation is, erm, sparse). Could
// alternatively use kCMSampleAttachmentKey_DependsOnOthers == false.
//bool IsDependent = CFDictionaryContainsKey(sample_attachments,kCMSampleAttachmentKey_DependsOnOthers);
bool NotSync = CFDictionaryContainsKey(sample_attachments,kCMSampleAttachmentKey_NotSync);
//bool PartialSync = CFDictionaryContainsKey(sample_attachments,kCMSampleAttachmentKey_PartialSync);
//bool IsDependedOnByOthers = CFDictionaryContainsKey(sample_attachments,kCMSampleAttachmentKey_IsDependedOnByOthers);
// bool Keyframe = (!NotSync) || (!IsDependent);
bool Keyframe = (!NotSync);
return Keyframe;
}
std::string Avf::GetPixelFormatString(OSType Format)
{
auto Table = GetRemoteArray( Cv_PixelFormatMap );
auto* Meta = GetArrayBridge(Table).Find( Format );
if ( !Meta )
{
Soy::TFourcc Fourcc(Format);
std::stringstream Output;
Output << "<Unknown format " << Fourcc << ">";
return Output.str();
}
return Meta->mName;
}
OSType Avf::GetPlatformPixelFormat(SoyPixelsFormat::Type Format)
{
auto Table = GetRemoteArray( Cv_PixelFormatMap );
auto* Meta = GetArrayBridge(Table).Find( Format );
if ( !Meta )
return CV_VIDEO_INVALID_ENUM;
return Meta->mPlatformFormat;
}
SoyPixelsFormat::Type Avf::GetPixelFormat(OSType Format)
{
auto Table = GetRemoteArray( Cv_PixelFormatMap );
auto* Meta = GetArrayBridge(Table).Find( Format );
if ( !Meta )
{
Soy::TFourcc Fourcc(Format);
std::Debug << "Unknown Avf CV pixel format (" << Fourcc << " 0x" << std::hex << Format << ")" << std::dec << std::endl;
return SoyPixelsFormat::Invalid;
}
return Meta->mSoyFormat;
}
SoyPixelsFormat::Type Avf::GetPixelFormat(const Soy::TFourcc& Fourcc)
{
return GetPixelFormat(Fourcc.mFourcc32);
}
SoyPixelsFormat::Type Avf::GetPixelFormat(NSNumber* Format)
{
auto FormatInt = [Format integerValue];
return GetPixelFormat( static_cast<OSType>(FormatInt) );
}
std::string Avf::GetPixelFormatString(NSNumber* Format)
{
auto FormatInt = [Format integerValue];
return GetPixelFormatString( static_cast<OSType>(FormatInt) );
}
std::string Avf::GetPixelFormatString(id Format)
{
auto FormatInt = [Format integerValue];
return GetPixelFormatString( static_cast<OSType>(FormatInt) );
}
vec2x<uint32_t> Avf::GetSize(CVImageBufferRef Image)
{
auto Size = CVImageBufferGetDisplaySize( Image );
// todo: check non integer sizes
if ( Size.width < 0 )
throw Soy::AssertException("Not expecting negative width for image");
if ( Size.height < 0 )
throw Soy::AssertException("Not expecting negative width for image");
return vec2x<uint32_t>( Size.width, Size.height );
}
void Avf::IsOkay(OSStatus Error,const std::string& Context)
{
IsOkay( Error, Context.c_str() );
}
void Avf::IsOkay(OSStatus Error,const char* Context)
{
// kCVReturnSuccess
if ( Error == noErr )
return;
std::stringstream ErrorString;
ErrorString << "OSStatus/CVReturn error in " << Context << ": " << GetString(Error);
throw Soy::AssertException( ErrorString.str() );
}
void PixelReleaseCallback(void *releaseRefCon, const void *baseAddress)
{
//std::Debug << __func__ << std::endl;
// this page says we need to release
// http://codefromabove.com/2015/01/av-foundation-saving-a-sequence-of-raw-rgb-frames-to-a-movie/
if ( releaseRefCon != nullptr )
{
CFDataRef bufferData = (CFDataRef)releaseRefCon;
CFRelease(bufferData);
}
}
// gr: these create buffers that hold their lifetime AFTER this function, so the Image and the PixelBuffer ref must be kept together
// todo: pass in a lambda that gets called when image is released
CVPixelBufferRef Avf::PixelsToPixelBuffer(const SoyPixelsImpl& Image)
{
CFAllocatorRef PixelBufferAllocator = nullptr;
OSType PixelFormatType = GetPlatformPixelFormat( Image.GetFormat() );
void* ReleaseContext = nullptr;
// gr: hack, cannot create RGBA pixel buffer on OSX. do a last-min conversion here, but ideally it's done beforehand
// REALLY ideally we can go from texture to CVPixelBuffer
// gr: this is the same case on ios. Now I think higher level should fix it, so add an annoying warning