-
Notifications
You must be signed in to change notification settings - Fork 37
/
wcap_encoder.h
1005 lines (843 loc) · 31.3 KB
/
wcap_encoder.h
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
#pragma once
#include "wcap.h"
#include "wcap_config.h"
#include "wcap_tex_resize.h"
#include "wcap_yuv_convert.h"
#include <d3d11_4.h>
#include <mfidl.h>
#include <mfreadwrite.h>
//
// interface
//
#define ENCODER_VIDEO_BUFFER_COUNT 8
#define ENCODER_AUDIO_BUFFER_COUNT 16
typedef struct
{
DWORD InputWidth; // width to what input will be cropped
DWORD InputHeight; // height to what input will be cropped
DWORD OutputWidth; // width of video output
DWORD OutputHeight; // height of video output
DWORD FramerateNum; // video output framerate numerator
DWORD FramerateDen; // video output framerate denumerator
UINT64 StartTime; // time in QPC ticks since first call of NewFrame
IMFAsyncCallback VideoSampleCallback;
IMFAsyncCallback AudioSampleCallback;
ID3D11DeviceContext* Context;
ID3D11Multithread* Multithread;
IMFSinkWriter* Writer;
int VideoStreamIndex;
int AudioStreamIndex;
ID3D11RenderTargetView* InputView;
TexResize Resize;
YuvConvert Convert;
YuvConvertOutput ConvertOutput[ENCODER_VIDEO_BUFFER_COUNT];
IMFSample* VideoSample[ENCODER_VIDEO_BUFFER_COUNT];
_Atomic(uint64_t) VideoSampleAvailable;
BOOL VideoDiscontinuity;
UINT64 VideoLastTime;
IMFTransform* Resampler;
IMFSample* AudioSample[ENCODER_AUDIO_BUFFER_COUNT];
_Atomic(uint64_t) AudioSampleAvailable;
IMFSample* AudioInputSample;
DWORD AudioFrameSize;
DWORD AudioSampleRate;
}
Encoder;
typedef struct
{
DWORD Width;
DWORD Height;
DWORD FramerateNum;
DWORD FramerateDen;
WAVEFORMATEX* AudioFormat;
Config* Config;
}
EncoderConfig;
static void Encoder_Init(Encoder* Encoder);
static BOOL Encoder_Start(Encoder* Encoder, ID3D11Device* Device, LPWSTR FileName, const EncoderConfig* Config);
static void Encoder_Stop(Encoder* Encoder);
static BOOL Encoder_NewFrame(Encoder* Encoder, ID3D11Texture2D* Texture, RECT Rect, UINT64 Time, UINT64 TimePeriod);
static void Encoder_NewSamples(Encoder* Encoder, LPCVOID Samples, DWORD FrameCount, UINT64 Time, UINT64 TimePeriod);
static void Encoder_Update(Encoder* Encoder, UINT64 Time, UINT64 TimePeriod);
static void Encoder_GetStats(Encoder* Encoder, DWORD* Bitrate, DWORD* LengthMsec, UINT64* FileSize);
//
// implementation
//
#include <evr.h>
#include <cguid.h>
#include <mfapi.h>
#include <mferror.h>
#include <codecapi.h>
#include <wmcodecdsp.h>
#define MFT64(high, low) (((UINT64)high << 32) | (low))
static HRESULT STDMETHODCALLTYPE Encoder__QueryInterface(IMFAsyncCallback* this, REFIID riid, void** Object)
{
if (Object == NULL)
{
return E_POINTER;
}
if (IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IMFAsyncCallback, riid))
{
*Object = this;
return S_OK;
}
return E_NOINTERFACE;
}
static ULONG STDMETHODCALLTYPE Encoder__AddRef(IMFAsyncCallback* this)
{
return 1;
}
static ULONG STDMETHODCALLTYPE Encoder__Release(IMFAsyncCallback* this)
{
return 1;
}
static HRESULT STDMETHODCALLTYPE Encoder__GetParameters(IMFAsyncCallback* this, DWORD* Flags, DWORD* Queue)
{
*Flags = 0;
*Queue = MFASYNC_CALLBACK_QUEUE_MULTITHREADED;
return S_OK;
}
static HRESULT STDMETHODCALLTYPE Encoder__VideoInvoke(IMFAsyncCallback* this, IMFAsyncResult* Result)
{
Encoder* Enc = CONTAINING_RECORD(this, Encoder, VideoSampleCallback);
IUnknown* Object;
IMFSample* Sample;
HR(IMFAsyncResult_GetObject(Result, &Object));
HR(IUnknown_QueryInterface(Object, &IID_IMFSample, (LPVOID*)&Sample));
IUnknown_Release(Object);
// keep Sample object reference count incremented to reuse for new frame submission
for (size_t Index = 0; Index < ARRAYSIZE(Enc->VideoSample); Index++)
{
if (Sample == Enc->VideoSample[Index])
{
atomic_fetch_or(&Enc->VideoSampleAvailable, 1ULL << Index);
WakeByAddressSingle((PVOID)&Enc->VideoSampleAvailable);
break;
}
}
return S_OK;
}
static HRESULT STDMETHODCALLTYPE Encoder__AudioInvoke(IMFAsyncCallback* this, IMFAsyncResult* Result)
{
Encoder* Enc = CONTAINING_RECORD(this, Encoder, AudioSampleCallback);
IUnknown* Object;
IMFSample* Sample;
HR(IMFAsyncResult_GetObject(Result, &Object));
HR(IUnknown_QueryInterface(Object, &IID_IMFSample, (LPVOID*)&Sample));
IUnknown_Release(Object);
// keep Sample object reference count incremented to reuse for new sample submission
for (size_t Index = 0; Index < ARRAYSIZE(Enc->AudioSample); Index++)
{
if (Sample == Enc->AudioSample[Index])
{
atomic_fetch_or(&Enc->AudioSampleAvailable, 1ULL << Index);
WakeByAddressSingle((PVOID)&Enc->AudioSampleAvailable);
break;
}
}
return S_OK;
}
static IMFAsyncCallbackVtbl Encoder__VideoSampleCallbackVtbl =
{
.QueryInterface = &Encoder__QueryInterface,
.AddRef = &Encoder__AddRef,
.Release = &Encoder__Release,
.GetParameters = &Encoder__GetParameters,
.Invoke = &Encoder__VideoInvoke,
};
static IMFAsyncCallbackVtbl Encoder__AudioSampleCallbackVtbl =
{
.QueryInterface = &Encoder__QueryInterface,
.AddRef = &Encoder__AddRef,
.Release = &Encoder__Release,
.GetParameters = &Encoder__GetParameters,
.Invoke = &Encoder__AudioInvoke,
};
static void Encoder__OutputAudioSamples(Encoder* Encoder)
{
for (;;)
{
// we don't want to drop any audio frames, so wait for available sample/buffer
uint64_t Available = atomic_load(&Encoder->AudioSampleAvailable);
while (Available == 0)
{
uint64_t Zero = 0;
WaitOnAddress((PVOID)&Encoder->AudioSampleAvailable, &Zero, sizeof(Zero), INFINITE);
Available = atomic_load(&Encoder->AudioSampleAvailable);
}
DWORD Index;
_BitScanForward64(&Index, Available);
IMFSample* Sample = Encoder->AudioSample[Index];
DWORD Status;
MFT_OUTPUT_DATA_BUFFER Output = { .dwStreamID = 0, .pSample = Sample };
HRESULT hr = IMFTransform_ProcessOutput(Encoder->Resampler, 0, 1, &Output, &Status);
if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT)
{
// no output is available
break;
}
Assert(SUCCEEDED(hr));
atomic_fetch_and(&Encoder->AudioSampleAvailable, ~(1ULL << Index));
IMFTrackedSample* Tracked;
HR(IMFSample_QueryInterface(Sample, &IID_IMFTrackedSample, (LPVOID*)&Tracked));
HR(IMFTrackedSample_SetAllocator(Tracked, &Encoder->AudioSampleCallback, (IUnknown*)Tracked));
HR(IMFSinkWriter_WriteSample(Encoder->Writer, Encoder->AudioStreamIndex, Sample));
IMFSample_Release(Sample);
IMFTrackedSample_Release(Tracked);
}
}
void Encoder_Init(Encoder* Encoder)
{
HR(MFStartup(MF_VERSION, MFSTARTUP_LITE));
Encoder->VideoSampleCallback.lpVtbl = &Encoder__VideoSampleCallbackVtbl;
Encoder->AudioSampleCallback.lpVtbl = &Encoder__AudioSampleCallbackVtbl;
}
BOOL Encoder_Start(Encoder* Encoder, ID3D11Device* Device, LPWSTR FileName, const EncoderConfig* Config)
{
ID3D11DeviceContext* Context;
ID3D11Device_GetImmediateContext(Device, &Context);
ID3D11Multithread* Multithread;
// it is very unclear if this is needed or not, it used to be that D3D11 debug runtime
// was complaining if this is not done, but nowadays it doesn't complain anymore? very confusing
HR(ID3D11DeviceContext_QueryInterface(Context, &IID_ID3D11Multithread, &Multithread));
ID3D11Multithread_SetMultithreadProtected(Multithread, TRUE);
DWORD InputWidth = Config->Width;
DWORD InputHeight = Config->Height;
DWORD OutputWidth = Config->Config->VideoMaxWidth;
DWORD OutputHeight = Config->Config->VideoMaxHeight;
if (OutputWidth != 0 && OutputHeight == 0)
{
// limit max width
if (OutputWidth < InputWidth)
{
OutputHeight = InputHeight * OutputWidth / InputWidth;
}
else
{
OutputWidth = InputWidth;
OutputHeight = InputHeight;
}
}
else if (OutputWidth == 0 && OutputHeight != 0)
{
// limit max height
if (OutputHeight < InputHeight)
{
OutputWidth = InputWidth * OutputHeight / InputHeight;
}
else
{
OutputWidth = InputWidth;
OutputHeight = InputHeight;
}
}
else if (OutputWidth != 0 && OutputHeight != 0)
{
// limit max width and max height
if (OutputWidth * InputHeight < OutputHeight * InputWidth)
{
if (OutputWidth < InputWidth)
{
OutputHeight = InputHeight * OutputWidth / InputWidth;
}
else
{
OutputWidth = InputWidth;
OutputHeight = InputHeight;
}
}
else
{
if (OutputHeight < InputHeight)
{
OutputWidth = InputWidth * OutputHeight / InputHeight;
}
else
{
OutputWidth = InputWidth;
OutputHeight = InputHeight;
}
}
}
else
{
// use original size
OutputWidth = InputWidth;
OutputHeight = InputHeight;
}
// must be multiple of 2, round upwards
InputWidth = (InputWidth + 1) & ~1;
InputHeight = (InputHeight + 1) & ~1;
OutputWidth = (OutputWidth + 1) & ~1;
OutputHeight = (OutputHeight + 1) & ~1;
BOOL Result = FALSE;
IMFSinkWriter* Writer = NULL;
IMFTransform* Resampler = NULL;
HRESULT hr;
Encoder->VideoStreamIndex = -1;
Encoder->AudioStreamIndex = -1;
const GUID* Container;
const GUID* Codec;
UINT32 Profile;
const GUID* VideoInputFormat;
if (Config->Config->VideoCodec == CONFIG_VIDEO_H264)
{
VideoInputFormat = &MFVideoFormat_NV12;
Container = Config->Config->FragmentedOutput ? &MFTranscodeContainerType_FMPEG4 : &MFTranscodeContainerType_MPEG4;
Codec = &MFVideoFormat_H264;
Profile = ((UINT32[]) { eAVEncH264VProfile_Base, eAVEncH264VProfile_Main, eAVEncH264VProfile_High })[Config->Config->VideoProfile];
}
else if (Config->Config->VideoCodec == CONFIG_VIDEO_H265 && Config->Config->VideoProfile == CONFIG_VIDEO_MAIN)
{
VideoInputFormat = &MFVideoFormat_NV12;
Container = &MFTranscodeContainerType_MPEG4;
Codec = &MFVideoFormat_HEVC;
Profile = eAVEncH265VProfile_Main_420_8;
}
else if (Config->Config->VideoCodec == CONFIG_VIDEO_H265 && Config->Config->VideoProfile == CONFIG_VIDEO_MAIN_10)
{
VideoInputFormat = &MFVideoFormat_P010;
Container = &MFTranscodeContainerType_MPEG4;
Codec = &MFVideoFormat_HEVC;
Profile = eAVEncH265VProfile_Main_420_10;
}
else if (Config->Config->VideoCodec == CONFIG_VIDEO_AV1 && Config->Config->VideoProfile == CONFIG_VIDEO_MAIN)
{
VideoInputFormat = &MFVideoFormat_NV12;
Container = &MFTranscodeContainerType_MPEG4;
Codec = &MFVideoFormat_AV1;
Profile = eAVEncAV1VProfile_Main_420_8;
}
else if (Config->Config->VideoCodec == CONFIG_VIDEO_AV1 && Config->Config->VideoProfile == CONFIG_VIDEO_MAIN_10)
{
VideoInputFormat = &MFVideoFormat_P010;
Container = &MFTranscodeContainerType_MPEG4;
Codec = &MFVideoFormat_AV1;
Profile = eAVEncAV1VProfile_Main_420_10;
}
else
{
Assert(0);
}
// make sure MFT video encoder exists, some vendors wrongly allow SinkWriter to be created for invalid configuration
{
bool Ok = false;
MFT_REGISTER_TYPE_INFO InputType = { MFMediaType_Video, *VideoInputFormat };
MFT_REGISTER_TYPE_INFO OutputType = { MFMediaType_Video, *Codec };
IMFAttributes* EnumAttributes;
HR(MFCreateAttributes(&EnumAttributes, 1));
UINT32 Flags = MFT_ENUM_FLAG_SORTANDFILTER;
if (Config->Config->HardwareEncoder)
{
IDXGIDevice* DxgiDevice;
HR(ID3D11Device_QueryInterface(Device, &IID_IDXGIDevice, (void**)&DxgiDevice));
IDXGIAdapter* DxgiAdapter;
HR(IDXGIDevice_GetAdapter(DxgiDevice, &DxgiAdapter));
IDXGIDevice_Release(DxgiDevice);
DXGI_ADAPTER_DESC AdapterDesc;
IDXGIAdapter_GetDesc(DxgiAdapter, &AdapterDesc);
IDXGIAdapter_Release(DxgiAdapter);
HR(IMFAttributes_SetBlob(EnumAttributes, &MFT_ENUM_ADAPTER_LUID, (UINT8*)&AdapterDesc.AdapterLuid, sizeof(AdapterDesc.AdapterLuid)));
Flags |= MFT_ENUM_FLAG_ASYNCMFT | MFT_ENUM_FLAG_HARDWARE;
}
else
{
Flags |= MFT_ENUM_FLAG_SYNCMFT;
}
UINT32 ActivateCount = 0;
IMFActivate** Activate = NULL;
if (SUCCEEDED(MFTEnum2(MFT_CATEGORY_VIDEO_ENCODER, Flags, &InputType, &OutputType, EnumAttributes, &Activate, &ActivateCount)) && ActivateCount != 0)
{
Ok = true;
}
if (Activate)
{
for (size_t ActivateIndex = 0; ActivateIndex != ActivateCount; ActivateIndex++)
{
IMFActivate_Release(Activate[ActivateIndex]);
}
CoTaskMemFree(Activate);
}
IMFAttributes_Release(EnumAttributes);
if (!Ok)
{
MessageBoxW(NULL, L"Cannot find video encoder!", WCAP_TITLE, MB_ICONERROR);
goto bail;
}
}
// https://github.com/mpv-player/mpv/blob/release/0.38/video/csputils.c#L150-L153
bool IsHD = (OutputWidth >= 1280) || (OutputHeight > 576)
|| Config->Config->VideoCodec == CONFIG_VIDEO_H265
|| Config->Config->VideoCodec == CONFIG_VIDEO_AV1;
// output file
{
IMFAttributes* Attributes;
HR(MFCreateAttributes(&Attributes, 4));
if (Config->Config->HardwareEncoder)
{
HR(IMFAttributes_SetUINT32(Attributes, &MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, TRUE));
UINT Token;
IMFDXGIDeviceManager* Manager;
HR(MFCreateDXGIDeviceManager(&Token, &Manager));
HR(IMFDXGIDeviceManager_ResetDevice(Manager, (IUnknown*)Device, Token));
HR(IMFAttributes_SetUnknown(Attributes, &MF_SINK_WRITER_D3D_MANAGER, (IUnknown*)Manager));
IMFDXGIDeviceManager_Release(Manager);
}
HR(IMFAttributes_SetUINT32(Attributes, &MF_SINK_WRITER_DISABLE_THROTTLING, TRUE));
HR(IMFAttributes_SetGUID(Attributes, &MF_TRANSCODE_CONTAINERTYPE, Container));
hr = MFCreateSinkWriterFromURL(FileName, NULL, Attributes, &Writer);
IMFAttributes_Release(Attributes);
if (FAILED(hr))
{
MessageBoxW(NULL, L"Cannot create output mp4 file!", WCAP_TITLE, MB_ICONERROR);
goto bail;
}
}
// video output type
{
IMFMediaType* Type;
HR(MFCreateMediaType(&Type));
HR(IMFMediaType_SetGUID(Type, &MF_MT_MAJOR_TYPE, &MFMediaType_Video));
HR(IMFMediaType_SetGUID(Type, &MF_MT_SUBTYPE, Codec));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_MPEG2_PROFILE, Profile));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_VIDEO_CHROMA_SITING, MFVideoChromaSubsampling_MPEG2));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_VIDEO_NOMINAL_RANGE, MFNominalRange_Wide));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_VIDEO_PRIMARIES, IsHD ? MFVideoPrimaries_BT709 : MFVideoPrimaries_SMPTE170M));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_YUV_MATRIX, IsHD ? MFVideoTransferMatrix_BT709 : MFVideoTransferMatrix_BT601));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_TRANSFER_FUNCTION, MFVideoPrimaries_BT709));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive));
HR(IMFMediaType_SetUINT64(Type, &MF_MT_FRAME_RATE, MFT64(Config->FramerateNum, Config->FramerateDen)));
HR(IMFMediaType_SetUINT64(Type, &MF_MT_FRAME_SIZE, MFT64(OutputWidth, OutputHeight)));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_AVG_BITRATE, Config->Config->VideoBitrate * 1000));
hr = IMFSinkWriter_AddStream(Writer, Type, &Encoder->VideoStreamIndex);
IMFMediaType_Release(Type);
if (FAILED(hr))
{
MessageBoxW(NULL, L"Cannot configure video encoder!", WCAP_TITLE, MB_ICONERROR);
goto bail;
}
}
// video input type, NV12 or P010 format
{
IMFMediaType* Type;
HR(MFCreateMediaType(&Type));
HR(IMFMediaType_SetGUID(Type, &MF_MT_MAJOR_TYPE, &MFMediaType_Video));
HR(IMFMediaType_SetGUID(Type, &MF_MT_SUBTYPE, VideoInputFormat));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive));
HR(IMFMediaType_SetUINT64(Type, &MF_MT_FRAME_RATE, MFT64(Config->FramerateNum, Config->FramerateDen)));
HR(IMFMediaType_SetUINT64(Type, &MF_MT_FRAME_SIZE, MFT64(OutputWidth, OutputHeight)));
hr = IMFSinkWriter_SetInputMediaType(Writer, Encoder->VideoStreamIndex, Type, NULL);
IMFMediaType_Release(Type);
if (FAILED(hr))
{
MessageBoxW(NULL, L"Cannot configure video encoder input!", WCAP_TITLE, MB_ICONERROR);
goto bail;
}
}
// video encoder parameters
{
ICodecAPI* Codec;
HR(IMFSinkWriter_GetServiceForStream(Writer, 0, &GUID_NULL, &IID_ICodecAPI, (LPVOID*)&Codec));
// VBR rate control
VARIANT RateControl = { .vt = VT_UI4, .ulVal = eAVEncCommonRateControlMode_UnconstrainedVBR };
ICodecAPI_SetValue(Codec, &CODECAPI_AVEncCommonRateControlMode, &RateControl);
// VBR bitrate to use, some MFT encoders override MF_MT_AVG_BITRATE setting with this one
VARIANT Bitrate = { .vt = VT_UI4, .ulVal = Config->Config->VideoBitrate * 1000 };
ICodecAPI_SetValue(Codec, &CODECAPI_AVEncCommonMeanBitRate, &Bitrate);
// set GOP size to 4 seconds
VARIANT GopSize = { .vt = VT_UI4, .ulVal = MUL_DIV_ROUND_UP(4, Config->FramerateNum, Config->FramerateDen) };
ICodecAPI_SetValue(Codec, &CODECAPI_AVEncMPVGOPSize, &GopSize);
// disable low latency, for higher quality & better performance
VARIANT LowLatency = { .vt = VT_BOOL, .boolVal = VARIANT_FALSE };
ICodecAPI_SetValue(Codec, &CODECAPI_AVLowLatencyMode, &LowLatency);
// enable 2 B-frames, for better compression
VARIANT Bframes = { .vt = VT_UI4, .ulVal = 2 };
ICodecAPI_SetValue(Codec, &CODECAPI_AVEncMPVDefaultBPictureCount, &Bframes);
ICodecAPI_Release(Codec);
}
if (Config->AudioFormat)
{
HR(CoCreateInstance(&CLSID_CResamplerMediaObject, NULL, CLSCTX_INPROC_SERVER, &IID_IMFTransform, (LPVOID*)&Resampler));
// audio resampler input
{
IMFMediaType* Type;
HR(MFCreateMediaType(&Type));
HR(MFInitMediaTypeFromWaveFormatEx(Type, Config->AudioFormat, sizeof(*Config->AudioFormat) + Config->AudioFormat->cbSize));
HR(IMFTransform_SetInputType(Resampler, 0, Type, 0));
IMFMediaType_Release(Type);
}
// audio resampler output
{
WAVEFORMATEX Format =
{
.wFormatTag = WAVE_FORMAT_PCM,
.nChannels = (WORD)Config->Config->AudioChannels,
.nSamplesPerSec = Config->Config->AudioSamplerate,
.wBitsPerSample = sizeof(short) * 8,
};
Format.nBlockAlign = Format.nChannels * Format.wBitsPerSample / 8;
Format.nAvgBytesPerSec = Format.nSamplesPerSec * Format.nBlockAlign;
IMFMediaType* Type;
HR(MFCreateMediaType(&Type));
HR(MFInitMediaTypeFromWaveFormatEx(Type, &Format, sizeof(Format)));
HR(IMFTransform_SetOutputType(Resampler, 0, Type, 0));
IMFMediaType_Release(Type);
}
HR(IMFTransform_ProcessMessage(Resampler, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0));
// audio output type
{
const GUID* Codec = &((GUID[]){ MFAudioFormat_AAC, MFAudioFormat_FLAC })[Config->Config->AudioCodec];
IMFMediaType* Type;
HR(MFCreateMediaType(&Type));
HR(IMFMediaType_SetGUID(Type, &MF_MT_MAJOR_TYPE, &MFMediaType_Audio));
HR(IMFMediaType_SetGUID(Type, &MF_MT_SUBTYPE, Codec));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_AUDIO_BITS_PER_SAMPLE, 16));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, Config->Config->AudioSamplerate));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_AUDIO_NUM_CHANNELS, Config->Config->AudioChannels));
if (Config->Config->AudioCodec == CONFIG_AUDIO_AAC)
{
HR(IMFMediaType_SetUINT32(Type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, Config->Config->AudioBitrate * 1000 / 8));
}
hr = IMFSinkWriter_AddStream(Writer, Type, &Encoder->AudioStreamIndex);
IMFMediaType_Release(Type);
if (FAILED(hr))
{
MessageBoxW(NULL, L"Cannot configure audio encoder output!", WCAP_TITLE, MB_ICONERROR);
goto bail;
}
}
// audio input type
{
IMFMediaType* Type;
HR(MFCreateMediaType(&Type));
HR(IMFMediaType_SetGUID(Type, &MF_MT_MAJOR_TYPE, &MFMediaType_Audio));
HR(IMFMediaType_SetGUID(Type, &MF_MT_SUBTYPE, &MFAudioFormat_PCM));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_AUDIO_BITS_PER_SAMPLE, 16));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, Config->Config->AudioSamplerate));
HR(IMFMediaType_SetUINT32(Type, &MF_MT_AUDIO_NUM_CHANNELS, Config->Config->AudioChannels));
hr = IMFSinkWriter_SetInputMediaType(Writer, Encoder->AudioStreamIndex, Type, NULL);
IMFMediaType_Release(Type);
if (FAILED(hr))
{
MessageBoxW(NULL, L"Cannot configure audio encoder input!", WCAP_TITLE, MB_ICONERROR);
goto bail;
}
}
}
hr = IMFSinkWriter_BeginWriting(Writer);
if (FAILED(hr))
{
MessageBoxW(NULL, L"Cannot start writing to mp4 file!", WCAP_TITLE, MB_ICONERROR);
goto bail;
}
// input texture
{
TexResize_Create(&Encoder->Resize, Device, InputWidth, InputHeight, OutputWidth, OutputHeight, Config->Config->GammaCorrectResize, D3D11_BIND_RENDER_TARGET);
D3D11_RENDER_TARGET_VIEW_DESC InputViewDesc =
{
.Format = DXGI_FORMAT_B8G8R8A8_UNORM,
.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D,
};
ID3D11Device_CreateRenderTargetView(Device, (ID3D11Resource*)Encoder->Resize.InputTexture, &InputViewDesc, &Encoder->InputView);
FLOAT Black[] = { 0, 0, 0, 0 };
ID3D11DeviceContext_ClearRenderTargetView(Context, Encoder->InputView, Black);
}
// yuv converter
{
YuvColorSpace ColorSpace = IsHD ? YuvColorSpace_BT709 : YuvColorSpace_BT601;
YuvConvert_Create(&Encoder->Convert, Device, Encoder->Resize.OutputTexture, OutputWidth, OutputHeight, ColorSpace, Config->Config->ImprovedColorConversion);
DXGI_FORMAT ConvertFormat = IsEqualGUID(VideoInputFormat, &MFVideoFormat_NV12) ? DXGI_FORMAT_NV12 : DXGI_FORMAT_P010;
for (size_t OutputIndex = 0; OutputIndex < ENCODER_VIDEO_BUFFER_COUNT; OutputIndex++)
{
YuvConvertOutput_Create(&Encoder->ConvertOutput[OutputIndex], Device, OutputWidth, OutputHeight, ConvertFormat);
ID3D11Texture2D* ConvertOutputTexture = Encoder->ConvertOutput[OutputIndex].Texture;
IMFSample* VideoSample;
HR(MFCreateVideoSampleFromSurface(NULL, &VideoSample));
IMFMediaBuffer* Buffer;
HR(MFCreateDXGISurfaceBuffer(&IID_ID3D11Texture2D, (IUnknown*)ConvertOutputTexture, 0, FALSE, &Buffer));
UINT32 MaxLength;
HR(IMFMediaBuffer_GetMaxLength(Buffer, &MaxLength));
HR(IMFMediaBuffer_SetCurrentLength(Buffer, MaxLength));
HR(IMFSample_AddBuffer(VideoSample, Buffer));
IMFMediaBuffer_Release(Buffer);
Encoder->VideoSample[OutputIndex] = VideoSample;
}
}
Encoder->InputWidth = Config->Width;
Encoder->InputHeight = Config->Height;
Encoder->OutputWidth = OutputWidth;
Encoder->OutputHeight = OutputHeight;
Encoder->FramerateNum = Config->FramerateNum;
Encoder->FramerateDen = Config->FramerateDen;
Encoder->VideoDiscontinuity = FALSE;
Encoder->VideoLastTime = 0x8000000000000000ULL; // some large time in future
Assert(ENCODER_VIDEO_BUFFER_COUNT <= 64);
atomic_init(&Encoder->VideoSampleAvailable, (1ULL << ENCODER_VIDEO_BUFFER_COUNT) - 1);
if (Encoder->AudioStreamIndex >= 0)
{
// resampler input sample
HR(MFCreateSample(&Encoder->AudioInputSample));
// resampler output & audio encoding input buffer/samples
for (int i = 0; i < ENCODER_AUDIO_BUFFER_COUNT; i++)
{
IMFSample* Sample;
IMFMediaBuffer* Buffer;
IMFTrackedSample* Tracked;
HR(MFCreateTrackedSample(&Tracked));
HR(IMFTrackedSample_QueryInterface(Tracked, &IID_IMFSample, (LPVOID*)&Sample));
HR(MFCreateMemoryBuffer(Config->Config->AudioSamplerate * Config->Config->AudioChannels * sizeof(short), &Buffer));
HR(IMFSample_AddBuffer(Sample, Buffer));
IMFMediaBuffer_Release(Buffer);
IMFTrackedSample_Release(Tracked);
Encoder->AudioSample[i] = Sample;
}
Encoder->AudioFrameSize = Config->AudioFormat->nBlockAlign;
Encoder->AudioSampleRate = Config->AudioFormat->nSamplesPerSec;
Encoder->Resampler = Resampler;
Assert(ENCODER_AUDIO_BUFFER_COUNT <= 64);
atomic_init(&Encoder->AudioSampleAvailable, (1ULL << ENCODER_AUDIO_BUFFER_COUNT) - 1);
}
ID3D11DeviceContext_AddRef(Context);
ID3D11Multithread_AddRef(Multithread);
Encoder->Context = Context;
Encoder->Multithread = Multithread;
Encoder->StartTime = 0;
Encoder->Writer = Writer;
Writer = NULL;
Resampler = NULL;
Result = TRUE;
bail:
if (Resampler)
{
IMFTransform_Release(Resampler);
}
if (Writer)
{
IMFSinkWriter_Release(Writer);
DeleteFileW(FileName);
}
ID3D11Multithread_Release(Multithread);
ID3D11DeviceContext_Release(Context);
return Result;
}
void Encoder_Stop(Encoder* Encoder)
{
if (Encoder->AudioStreamIndex >= 0)
{
HR(IMFTransform_ProcessMessage(Encoder->Resampler, MFT_MESSAGE_COMMAND_DRAIN, 0));
Encoder__OutputAudioSamples(Encoder);
IMFTransform_Release(Encoder->Resampler);
}
IMFSinkWriter_Finalize(Encoder->Writer);
IMFSinkWriter_Release(Encoder->Writer);
if (Encoder->AudioStreamIndex >= 0)
{
for (int i = 0; i < ENCODER_AUDIO_BUFFER_COUNT; i++)
{
IMFSample_Release(Encoder->AudioSample[i]);
}
IMFSample_Release(Encoder->AudioInputSample);
}
for (size_t OutputIndex = 0; OutputIndex < ENCODER_VIDEO_BUFFER_COUNT; OutputIndex++)
{
YuvConvertOutput_Release(&Encoder->ConvertOutput[OutputIndex]);
IMFSample_Release(Encoder->VideoSample[OutputIndex]);
}
YuvConvert_Release(&Encoder->Convert);
TexResize_Release(&Encoder->Resize);
ID3D11RenderTargetView_Release(Encoder->InputView);
ID3D11Multithread_Release(Encoder->Multithread);
ID3D11DeviceContext_Release(Encoder->Context);
}
BOOL Encoder_NewFrame(Encoder* Encoder, ID3D11Texture2D* Texture, RECT Rect, UINT64 Time, UINT64 TimePeriod)
{
Encoder->VideoLastTime = Time;
uint64_t Available = atomic_load(&Encoder->VideoSampleAvailable);
if (Available == 0)
{
// dropped frame
LONGLONG Timestamp = MFllMulDiv(Time - Encoder->StartTime, MF_UNITS_PER_SECOND, TimePeriod, 0);
HR(IMFSinkWriter_SendStreamTick(Encoder->Writer, Encoder->VideoStreamIndex, Timestamp));
Encoder->VideoDiscontinuity = TRUE;
return FALSE;
}
DWORD Index;
_BitScanForward64(&Index, Available);
atomic_fetch_and(&Encoder->VideoSampleAvailable, ~(1ULL << Index));
ID3D11DeviceContext* Context = Encoder->Context;
ID3D11Multithread_Enter(Encoder->Multithread);
// copy to input texture
{
D3D11_BOX Box =
{
.left = Rect.left,
.top = Rect.top,
.right = Rect.right,
.bottom = Rect.bottom,
.front = 0,
.back = 1,
};
DWORD Width = Box.right - Box.left;
DWORD Height = Box.bottom - Box.top;
if (Width < Encoder->InputWidth || Height < Encoder->InputHeight)
{
FLOAT Black[] = { 0, 0, 0, 0 };
ID3D11DeviceContext_ClearRenderTargetView(Context, Encoder->InputView, Black);
Box.right = Box.left + min(Encoder->InputWidth, Box.right);
Box.bottom = Box.top + min(Encoder->InputHeight, Box.bottom);
}
ID3D11DeviceContext_CopySubresourceRegion(Context, (ID3D11Resource*)Encoder->Resize.InputTexture, 0, 0, 0, 0, (ID3D11Resource*)Texture, 0, &Box);
}
// resize if needed
TexResize_Dispatch(&Encoder->Resize, Context);
// convert to YUV
YuvConvert_Dispatch(&Encoder->Convert, Context, &Encoder->ConvertOutput[Index]);
ID3D11DeviceContext_Flush(Context);
ID3D11Multithread_Leave(Encoder->Multithread);
// setup input time & duration
if (Encoder->StartTime == 0)
{
Encoder->StartTime = Time;
}
IMFSample* Sample = Encoder->VideoSample[Index];
HR(IMFSample_SetSampleDuration(Sample, MFllMulDiv(Encoder->FramerateDen, MF_UNITS_PER_SECOND, Encoder->FramerateNum, 0)));
HR(IMFSample_SetSampleTime(Sample, MFllMulDiv(Time - Encoder->StartTime, MF_UNITS_PER_SECOND, TimePeriod, 0)));
if (Encoder->VideoDiscontinuity)
{
HR(IMFSample_SetUINT32(Sample, &MFSampleExtension_Discontinuity, TRUE));
Encoder->VideoDiscontinuity = FALSE;
}
else
{
// don't care about success or no, we just don't want this attribute set at all
IMFSample_DeleteItem(Sample, &MFSampleExtension_Discontinuity);
}
IMFTrackedSample* Tracked;
HR(IMFSample_QueryInterface(Sample, &IID_IMFTrackedSample, (LPVOID*)&Tracked));
IMFTrackedSample_SetAllocator(Tracked, &Encoder->VideoSampleCallback, NULL);
IMFTrackedSample_Release(Tracked);
// submit to encoder which will happen in background
HR(IMFSinkWriter_WriteSample(Encoder->Writer, Encoder->VideoStreamIndex, Sample));
IMFSample_Release(Sample);
return TRUE;
}
typedef struct
{
IMFMediaBuffer Buffer;
uint8_t* SampleData;
uint32_t SampleByteCount;
uint32_t References;
}
EncoderAudioBuffer;
static HRESULT STDMETHODCALLTYPE EncoderAudioBuffer__QueryInterface(IMFMediaBuffer* This, REFIID Riid, void** Object)
{
if (Object == NULL)
{
return E_POINTER;
}
if (IsEqualGUID(Riid, &IID_IMFMediaBuffer) || IsEqualGUID(Riid, &IID_IUnknown))
{
*Object = This;
return S_OK;
}
return E_NOINTERFACE;
}
static ULONG STDMETHODCALLTYPE EncoderAudioBuffer__AddRef(IMFMediaBuffer* This)
{
EncoderAudioBuffer* Buffer = CONTAINING_RECORD(This, EncoderAudioBuffer, Buffer);
return Buffer->References += 1;
}
static ULONG STDMETHODCALLTYPE EncoderAudioBuffer__Release(IMFMediaBuffer* This)
{
EncoderAudioBuffer* Buffer = CONTAINING_RECORD(This, EncoderAudioBuffer, Buffer);
return Buffer->References -= 1;
}
static HRESULT STDMETHODCALLTYPE EncoderAudioBuffer__Lock(IMFMediaBuffer* This, BYTE** OutBuffer, DWORD* OutMaxLength, DWORD* OutCurrentLength)
{
EncoderAudioBuffer* Buffer = CONTAINING_RECORD(This, EncoderAudioBuffer, Buffer);
*OutBuffer = Buffer->SampleData;
if (OutMaxLength)
{
*OutMaxLength = Buffer->SampleByteCount;
}
if (OutCurrentLength)
{
*OutCurrentLength = Buffer->SampleByteCount;
}
return S_OK;
}
static HRESULT STDMETHODCALLTYPE EncoderAudioBuffer__Unlock(IMFMediaBuffer* This)
{
return S_OK;
}
static HRESULT STDMETHODCALLTYPE EncoderAudioBuffer__GetCurrentLength(IMFMediaBuffer* This, DWORD* OutCurrentLength)
{
EncoderAudioBuffer* Buffer = CONTAINING_RECORD(This, EncoderAudioBuffer, Buffer);
*OutCurrentLength = Buffer->SampleByteCount;
return S_OK;
}
static HRESULT STDMETHODCALLTYPE EncoderAudioBuffer__SetCurrentLength(IMFMediaBuffer* This, DWORD InCurrentLength)
{
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE EncoderAudioBuffer__GetMaxLength(IMFMediaBuffer* This, DWORD* OutMaxLength)
{
EncoderAudioBuffer* Buffer = CONTAINING_RECORD(This, EncoderAudioBuffer, Buffer);
*OutMaxLength = Buffer->SampleByteCount;
return S_OK;
}
static IMFMediaBufferVtbl EncoderAudioBufferVtbl =
{
.QueryInterface = &EncoderAudioBuffer__QueryInterface,
.AddRef = &EncoderAudioBuffer__AddRef,
.Release = &EncoderAudioBuffer__Release,
.Lock = &EncoderAudioBuffer__Lock,
.Unlock = &EncoderAudioBuffer__Unlock,
.GetCurrentLength = &EncoderAudioBuffer__GetCurrentLength,
.SetCurrentLength = &EncoderAudioBuffer__SetCurrentLength,
.GetMaxLength = &EncoderAudioBuffer__GetMaxLength,
};
void Encoder_NewSamples(Encoder* Encoder, LPCVOID Samples, DWORD FrameCount, UINT64 Time, UINT64 TimePeriod)
{
EncoderAudioBuffer Input =
{
.Buffer.lpVtbl = &EncoderAudioBufferVtbl,
.SampleData = (BYTE*)Samples,
.SampleByteCount = FrameCount * Encoder->AudioFrameSize,
};
IMFSample* AudioSample = Encoder->AudioInputSample;
HR(IMFSample_AddBuffer(AudioSample, &Input.Buffer));
// setup input time & duration
Assert(Encoder->StartTime != 0);
HR(IMFSample_SetSampleDuration(AudioSample, MFllMulDiv(FrameCount, MF_UNITS_PER_SECOND, Encoder->AudioSampleRate, 0)));
HR(IMFSample_SetSampleTime(AudioSample, MFllMulDiv(Time - Encoder->StartTime, MF_UNITS_PER_SECOND, TimePeriod, 0)));
HR(IMFTransform_ProcessInput(Encoder->Resampler, 0, AudioSample, 0));
Encoder__OutputAudioSamples(Encoder);
HR(IMFSample_RemoveAllBuffers(AudioSample));
Assert(Input.References == 0);
}
void Encoder_Update(Encoder* Encoder, UINT64 Time, UINT64 TimePeriod)
{
// if there was no frame during last second, add discontinuity
if ((int64_t)(Time - Encoder->VideoLastTime) >= (int64_t)TimePeriod)
{
Encoder->VideoLastTime = Time;
LONGLONG Timestamp = MFllMulDiv(Time - Encoder->StartTime, MF_UNITS_PER_SECOND, TimePeriod, 0);
HR(IMFSinkWriter_SendStreamTick(Encoder->Writer, Encoder->VideoStreamIndex, Timestamp));
Encoder->VideoDiscontinuity = TRUE;
}
}
void Encoder_GetStats(Encoder* Encoder, DWORD* Bitrate, DWORD* LengthMsec, UINT64* FileSize)
{
MF_SINK_WRITER_STATISTICS Stats = { .cb = sizeof(Stats) };
HR(IMFSinkWriter_GetStatistics(Encoder->Writer, Encoder->VideoStreamIndex, &Stats));
*Bitrate = (DWORD)MFllMulDiv(8 * Stats.qwByteCountProcessed, MF_UNITS_PER_SECOND, 1000 * Stats.llLastTimestampProcessed, 0);
*LengthMsec = (DWORD)(Stats.llLastTimestampProcessed / 10000);
*FileSize = Stats.qwByteCountProcessed;
if (Encoder->AudioStreamIndex >= 0)
{