forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mfenc.c
1198 lines (1005 loc) · 39.2 KB
/
mfenc.c
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
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define COBJMACROS
#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0602
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0602
#endif
#include "mf_utils.h"
#include "libavutil/imgutils.h"
#include "libavutil/opt.h"
#include "libavutil/time.h"
#include "internal.h"
typedef struct MFContext {
AVClass *av_class;
int is_video, is_audio;
GUID main_subtype;
IMFTransform *mft;
IMFMediaEventGenerator *async_events;
DWORD in_stream_id, out_stream_id;
MFT_INPUT_STREAM_INFO in_info;
MFT_OUTPUT_STREAM_INFO out_info;
int out_stream_provides_samples;
int draining, draining_done;
int sample_sent;
int async_need_input, async_have_output, async_marker;
int64_t reorder_delay;
ICodecAPI *codec_api;
// set by AVOption
int opt_enc_rc;
int opt_enc_quality;
int opt_enc_scenario;
int opt_enc_hw;
} MFContext;
static int mf_choose_output_type(AVCodecContext *avctx);
static int mf_setup_context(AVCodecContext *avctx);
#define MF_TIMEBASE (AVRational){1, 10000000}
// Sentinel value only used by us.
#define MF_INVALID_TIME AV_NOPTS_VALUE
static int mf_wait_events(AVCodecContext *avctx)
{
MFContext *c = avctx->priv_data;
if (!c->async_events)
return 0;
while (!(c->async_need_input || c->async_have_output || c->draining_done || c->async_marker)) {
IMFMediaEvent *ev = NULL;
MediaEventType ev_id = 0;
HRESULT hr = IMFMediaEventGenerator_GetEvent(c->async_events, 0, &ev);
if (FAILED(hr)) {
av_log(avctx, AV_LOG_ERROR, "IMFMediaEventGenerator_GetEvent() failed: %s\n",
ff_hr_str(hr));
return AVERROR_EXTERNAL;
}
IMFMediaEvent_GetType(ev, &ev_id);
switch (ev_id) {
case ff_METransformNeedInput:
if (!c->draining)
c->async_need_input = 1;
break;
case ff_METransformHaveOutput:
c->async_have_output = 1;
break;
case ff_METransformDrainComplete:
c->draining_done = 1;
break;
case ff_METransformMarker:
c->async_marker = 1;
break;
default: ;
}
IMFMediaEvent_Release(ev);
}
return 0;
}
static AVRational mf_get_tb(AVCodecContext *avctx)
{
if (avctx->pkt_timebase.num > 0 && avctx->pkt_timebase.den > 0)
return avctx->pkt_timebase;
if (avctx->time_base.num > 0 && avctx->time_base.den > 0)
return avctx->time_base;
return MF_TIMEBASE;
}
static LONGLONG mf_to_mf_time(AVCodecContext *avctx, int64_t av_pts)
{
if (av_pts == AV_NOPTS_VALUE)
return MF_INVALID_TIME;
return av_rescale_q(av_pts, mf_get_tb(avctx), MF_TIMEBASE);
}
static void mf_sample_set_pts(AVCodecContext *avctx, IMFSample *sample, int64_t av_pts)
{
LONGLONG stime = mf_to_mf_time(avctx, av_pts);
if (stime != MF_INVALID_TIME)
IMFSample_SetSampleTime(sample, stime);
}
static int64_t mf_from_mf_time(AVCodecContext *avctx, LONGLONG stime)
{
return av_rescale_q(stime, MF_TIMEBASE, mf_get_tb(avctx));
}
static int64_t mf_sample_get_pts(AVCodecContext *avctx, IMFSample *sample)
{
LONGLONG pts;
HRESULT hr = IMFSample_GetSampleTime(sample, &pts);
if (FAILED(hr))
return AV_NOPTS_VALUE;
return mf_from_mf_time(avctx, pts);
}
static int mf_enca_output_type_get(AVCodecContext *avctx, IMFMediaType *type)
{
MFContext *c = avctx->priv_data;
HRESULT hr;
UINT32 sz;
if (avctx->codec_id != AV_CODEC_ID_MP3 && avctx->codec_id != AV_CODEC_ID_AC3) {
hr = IMFAttributes_GetBlobSize(type, &MF_MT_USER_DATA, &sz);
if (!FAILED(hr) && sz > 0) {
avctx->extradata = av_mallocz(sz + AV_INPUT_BUFFER_PADDING_SIZE);
if (!avctx->extradata)
return AVERROR(ENOMEM);
avctx->extradata_size = sz;
hr = IMFAttributes_GetBlob(type, &MF_MT_USER_DATA, avctx->extradata, sz, NULL);
if (FAILED(hr))
return AVERROR_EXTERNAL;
if (avctx->codec_id == AV_CODEC_ID_AAC && avctx->extradata_size >= 12) {
// Get rid of HEAACWAVEINFO (after wfx field, 12 bytes).
avctx->extradata_size = avctx->extradata_size - 12;
memmove(avctx->extradata, avctx->extradata + 12, avctx->extradata_size);
}
}
}
// I don't know where it's documented that we need this. It happens with the
// MS mp3 encoder MFT. The idea for the workaround is taken from NAudio.
// (Certainly any lossy codec will have frames much smaller than 1 second.)
if (!c->out_info.cbSize && !c->out_stream_provides_samples) {
hr = IMFAttributes_GetUINT32(type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &sz);
if (!FAILED(hr)) {
av_log(avctx, AV_LOG_VERBOSE, "MFT_OUTPUT_STREAM_INFO.cbSize set to 0, "
"assuming %d bytes instead.\n", (int)sz);
c->out_info.cbSize = sz;
}
}
return 0;
}
static int mf_encv_output_type_get(AVCodecContext *avctx, IMFMediaType *type)
{
HRESULT hr;
UINT32 sz;
hr = IMFAttributes_GetBlobSize(type, &MF_MT_MPEG_SEQUENCE_HEADER, &sz);
if (!FAILED(hr) && sz > 0) {
uint8_t *extradata = av_mallocz(sz + AV_INPUT_BUFFER_PADDING_SIZE);
if (!extradata)
return AVERROR(ENOMEM);
hr = IMFAttributes_GetBlob(type, &MF_MT_MPEG_SEQUENCE_HEADER, extradata, sz, NULL);
if (FAILED(hr)) {
av_free(extradata);
return AVERROR_EXTERNAL;
}
av_freep(&avctx->extradata);
avctx->extradata = extradata;
avctx->extradata_size = sz;
}
return 0;
}
static int mf_output_type_get(AVCodecContext *avctx)
{
MFContext *c = avctx->priv_data;
HRESULT hr;
IMFMediaType *type;
int ret;
hr = IMFTransform_GetOutputCurrentType(c->mft, c->out_stream_id, &type);
if (FAILED(hr)) {
av_log(avctx, AV_LOG_ERROR, "could not get output type\n");
return AVERROR_EXTERNAL;
}
av_log(avctx, AV_LOG_VERBOSE, "final output type:\n");
ff_media_type_dump(avctx, type);
ret = 0;
if (c->is_video) {
ret = mf_encv_output_type_get(avctx, type);
} else if (c->is_audio) {
ret = mf_enca_output_type_get(avctx, type);
}
if (ret < 0)
av_log(avctx, AV_LOG_ERROR, "output type not supported\n");
IMFMediaType_Release(type);
return ret;
}
static int mf_sample_to_avpacket(AVCodecContext *avctx, IMFSample *sample, AVPacket *avpkt)
{
MFContext *c = avctx->priv_data;
HRESULT hr;
int ret;
DWORD len;
IMFMediaBuffer *buffer;
BYTE *data;
UINT64 t;
UINT32 t32;
hr = IMFSample_GetTotalLength(sample, &len);
if (FAILED(hr))
return AVERROR_EXTERNAL;
if ((ret = av_new_packet(avpkt, len)) < 0)
return ret;
IMFSample_ConvertToContiguousBuffer(sample, &buffer);
if (FAILED(hr))
return AVERROR_EXTERNAL;
hr = IMFMediaBuffer_Lock(buffer, &data, NULL, NULL);
if (FAILED(hr)) {
IMFMediaBuffer_Release(buffer);
return AVERROR_EXTERNAL;
}
memcpy(avpkt->data, data, len);
IMFMediaBuffer_Unlock(buffer);
IMFMediaBuffer_Release(buffer);
avpkt->pts = avpkt->dts = mf_sample_get_pts(avctx, sample);
hr = IMFAttributes_GetUINT32(sample, &MFSampleExtension_CleanPoint, &t32);
if (c->is_audio || (!FAILED(hr) && t32 != 0))
avpkt->flags |= AV_PKT_FLAG_KEY;
hr = IMFAttributes_GetUINT64(sample, &MFSampleExtension_DecodeTimestamp, &t);
if (!FAILED(hr)) {
avpkt->dts = mf_from_mf_time(avctx, t);
// At least on Qualcomm's HEVC encoder on SD 835, the output dts
// starts from the input pts of the first frame, while the output pts
// is shifted forward. Therefore, shift the output values back so that
// the output pts matches the input.
if (c->reorder_delay == AV_NOPTS_VALUE)
c->reorder_delay = avpkt->pts - avpkt->dts;
avpkt->dts -= c->reorder_delay;
avpkt->pts -= c->reorder_delay;
}
return 0;
}
static IMFSample *mf_a_avframe_to_sample(AVCodecContext *avctx, const AVFrame *frame)
{
MFContext *c = avctx->priv_data;
size_t len;
size_t bps;
IMFSample *sample;
bps = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->channels;
len = frame->nb_samples * bps;
sample = ff_create_memory_sample(frame->data[0], len, c->in_info.cbAlignment);
if (sample)
IMFSample_SetSampleDuration(sample, mf_to_mf_time(avctx, frame->nb_samples));
return sample;
}
static IMFSample *mf_v_avframe_to_sample(AVCodecContext *avctx, const AVFrame *frame)
{
MFContext *c = avctx->priv_data;
IMFSample *sample;
IMFMediaBuffer *buffer;
BYTE *data;
HRESULT hr;
int ret;
int size;
size = av_image_get_buffer_size(avctx->pix_fmt, avctx->width, avctx->height, 1);
if (size < 0)
return NULL;
sample = ff_create_memory_sample(NULL, size, c->in_info.cbAlignment);
if (!sample)
return NULL;
hr = IMFSample_GetBufferByIndex(sample, 0, &buffer);
if (FAILED(hr)) {
IMFSample_Release(sample);
return NULL;
}
hr = IMFMediaBuffer_Lock(buffer, &data, NULL, NULL);
if (FAILED(hr)) {
IMFMediaBuffer_Release(buffer);
IMFSample_Release(sample);
return NULL;
}
ret = av_image_copy_to_buffer((uint8_t *)data, size, (void *)frame->data, frame->linesize,
avctx->pix_fmt, avctx->width, avctx->height, 1);
IMFMediaBuffer_SetCurrentLength(buffer, size);
IMFMediaBuffer_Unlock(buffer);
IMFMediaBuffer_Release(buffer);
if (ret < 0) {
IMFSample_Release(sample);
return NULL;
}
IMFSample_SetSampleDuration(sample, mf_to_mf_time(avctx, frame->pkt_duration));
return sample;
}
static IMFSample *mf_avframe_to_sample(AVCodecContext *avctx, const AVFrame *frame)
{
MFContext *c = avctx->priv_data;
IMFSample *sample;
if (c->is_audio) {
sample = mf_a_avframe_to_sample(avctx, frame);
} else {
sample = mf_v_avframe_to_sample(avctx, frame);
}
if (sample)
mf_sample_set_pts(avctx, sample, frame->pts);
return sample;
}
static int mf_send_sample(AVCodecContext *avctx, IMFSample *sample)
{
MFContext *c = avctx->priv_data;
HRESULT hr;
int ret;
if (sample) {
if (c->async_events) {
if ((ret = mf_wait_events(avctx)) < 0)
return ret;
if (!c->async_need_input)
return AVERROR(EAGAIN);
}
if (!c->sample_sent)
IMFSample_SetUINT32(sample, &MFSampleExtension_Discontinuity, TRUE);
c->sample_sent = 1;
hr = IMFTransform_ProcessInput(c->mft, c->in_stream_id, sample, 0);
if (hr == MF_E_NOTACCEPTING) {
return AVERROR(EAGAIN);
} else if (FAILED(hr)) {
av_log(avctx, AV_LOG_ERROR, "failed processing input: %s\n", ff_hr_str(hr));
return AVERROR_EXTERNAL;
}
c->async_need_input = 0;
} else if (!c->draining) {
hr = IMFTransform_ProcessMessage(c->mft, MFT_MESSAGE_COMMAND_DRAIN, 0);
if (FAILED(hr))
av_log(avctx, AV_LOG_ERROR, "failed draining: %s\n", ff_hr_str(hr));
// Some MFTs (AC3) will send a frame after each drain command (???), so
// this is required to make draining actually terminate.
c->draining = 1;
c->async_need_input = 0;
} else {
return AVERROR_EOF;
}
return 0;
}
static int mf_send_frame(AVCodecContext *avctx, const AVFrame *frame)
{
MFContext *c = avctx->priv_data;
int ret;
IMFSample *sample = NULL;
if (frame) {
sample = mf_avframe_to_sample(avctx, frame);
if (!sample)
return AVERROR(ENOMEM);
if (c->is_video && c->codec_api) {
if (frame->pict_type == AV_PICTURE_TYPE_I || !c->sample_sent)
ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncVideoForceKeyFrame, FF_VAL_VT_UI4(1));
}
}
ret = mf_send_sample(avctx, sample);
if (sample)
IMFSample_Release(sample);
return ret;
}
static int mf_receive_sample(AVCodecContext *avctx, IMFSample **out_sample)
{
MFContext *c = avctx->priv_data;
HRESULT hr;
DWORD st;
MFT_OUTPUT_DATA_BUFFER out_buffers;
IMFSample *sample;
int ret = 0;
while (1) {
*out_sample = NULL;
sample = NULL;
if (c->async_events) {
if ((ret = mf_wait_events(avctx)) < 0)
return ret;
if (!c->async_have_output || c->draining_done) {
ret = 0;
break;
}
}
if (!c->out_stream_provides_samples) {
sample = ff_create_memory_sample(NULL, c->out_info.cbSize, c->out_info.cbAlignment);
if (!sample)
return AVERROR(ENOMEM);
}
out_buffers = (MFT_OUTPUT_DATA_BUFFER) {
.dwStreamID = c->out_stream_id,
.pSample = sample,
};
st = 0;
hr = IMFTransform_ProcessOutput(c->mft, 0, 1, &out_buffers, &st);
if (out_buffers.pEvents)
IMFCollection_Release(out_buffers.pEvents);
if (!FAILED(hr)) {
*out_sample = out_buffers.pSample;
ret = 0;
break;
}
if (out_buffers.pSample)
IMFSample_Release(out_buffers.pSample);
if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) {
if (c->draining)
c->draining_done = 1;
ret = 0;
} else if (hr == MF_E_TRANSFORM_STREAM_CHANGE) {
av_log(avctx, AV_LOG_WARNING, "stream format change\n");
ret = mf_choose_output_type(avctx);
if (ret == 0) // we don't expect renegotiating the input type
ret = AVERROR_EXTERNAL;
if (ret > 0) {
ret = mf_setup_context(avctx);
if (ret >= 0) {
c->async_have_output = 0;
continue;
}
}
} else {
av_log(avctx, AV_LOG_ERROR, "failed processing output: %s\n", ff_hr_str(hr));
ret = AVERROR_EXTERNAL;
}
break;
}
c->async_have_output = 0;
if (ret >= 0 && !*out_sample)
ret = c->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
return ret;
}
static int mf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
{
IMFSample *sample;
int ret;
ret = mf_receive_sample(avctx, &sample);
if (ret < 0)
return ret;
ret = mf_sample_to_avpacket(avctx, sample, avpkt);
IMFSample_Release(sample);
return ret;
}
// Most encoders seem to enumerate supported audio formats on the output types,
// at least as far as channel configuration and sample rate is concerned. Pick
// the one which seems to match best.
static int64_t mf_enca_output_score(AVCodecContext *avctx, IMFMediaType *type)
{
MFContext *c = avctx->priv_data;
HRESULT hr;
UINT32 t;
GUID tg;
int64_t score = 0;
hr = IMFAttributes_GetUINT32(type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &t);
if (!FAILED(hr) && t == avctx->sample_rate)
score |= 1LL << 32;
hr = IMFAttributes_GetUINT32(type, &MF_MT_AUDIO_NUM_CHANNELS, &t);
if (!FAILED(hr) && t == avctx->channels)
score |= 2LL << 32;
hr = IMFAttributes_GetGUID(type, &MF_MT_SUBTYPE, &tg);
if (!FAILED(hr)) {
if (IsEqualGUID(&c->main_subtype, &tg))
score |= 4LL << 32;
}
// Select the bitrate (lowest priority).
hr = IMFAttributes_GetUINT32(type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, &t);
if (!FAILED(hr)) {
int diff = (int)t - avctx->bit_rate / 8;
if (diff >= 0) {
score |= (1LL << 31) - diff; // prefer lower bitrate
} else {
score |= (1LL << 30) + diff; // prefer higher bitrate
}
}
hr = IMFAttributes_GetUINT32(type, &MF_MT_AAC_PAYLOAD_TYPE, &t);
if (!FAILED(hr) && t != 0)
return -1;
return score;
}
static int mf_enca_output_adjust(AVCodecContext *avctx, IMFMediaType *type)
{
// (some decoders allow adjusting this freely, but it can also cause failure
// to set the output type - so it's commented for being too fragile)
//IMFAttributes_SetUINT32(type, &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, avctx->bit_rate / 8);
//IMFAttributes_SetUINT32(type, &MF_MT_AVG_BITRATE, avctx->bit_rate);
return 0;
}
static int64_t mf_enca_input_score(AVCodecContext *avctx, IMFMediaType *type)
{
HRESULT hr;
UINT32 t;
int64_t score = 0;
enum AVSampleFormat sformat = ff_media_type_to_sample_fmt((IMFAttributes *)type);
if (sformat == AV_SAMPLE_FMT_NONE)
return -1; // can not use
if (sformat == avctx->sample_fmt)
score |= 1;
hr = IMFAttributes_GetUINT32(type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &t);
if (!FAILED(hr) && t == avctx->sample_rate)
score |= 2;
hr = IMFAttributes_GetUINT32(type, &MF_MT_AUDIO_NUM_CHANNELS, &t);
if (!FAILED(hr) && t == avctx->channels)
score |= 4;
return score;
}
static int mf_enca_input_adjust(AVCodecContext *avctx, IMFMediaType *type)
{
HRESULT hr;
UINT32 t;
enum AVSampleFormat sformat = ff_media_type_to_sample_fmt((IMFAttributes *)type);
if (sformat != avctx->sample_fmt) {
av_log(avctx, AV_LOG_ERROR, "unsupported input sample format set\n");
return AVERROR(EINVAL);
}
hr = IMFAttributes_GetUINT32(type, &MF_MT_AUDIO_SAMPLES_PER_SECOND, &t);
if (FAILED(hr) || t != avctx->sample_rate) {
av_log(avctx, AV_LOG_ERROR, "unsupported input sample rate set\n");
return AVERROR(EINVAL);
}
hr = IMFAttributes_GetUINT32(type, &MF_MT_AUDIO_NUM_CHANNELS, &t);
if (FAILED(hr) || t != avctx->channels) {
av_log(avctx, AV_LOG_ERROR, "unsupported input channel number set\n");
return AVERROR(EINVAL);
}
return 0;
}
static int64_t mf_encv_output_score(AVCodecContext *avctx, IMFMediaType *type)
{
MFContext *c = avctx->priv_data;
GUID tg;
HRESULT hr;
int score = -1;
hr = IMFAttributes_GetGUID(type, &MF_MT_SUBTYPE, &tg);
if (!FAILED(hr)) {
if (IsEqualGUID(&c->main_subtype, &tg))
score = 1;
}
return score;
}
static int mf_encv_output_adjust(AVCodecContext *avctx, IMFMediaType *type)
{
MFContext *c = avctx->priv_data;
AVRational framerate;
ff_MFSetAttributeSize((IMFAttributes *)type, &MF_MT_FRAME_SIZE, avctx->width, avctx->height);
IMFAttributes_SetUINT32(type, &MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive);
if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
framerate = avctx->framerate;
} else {
framerate = av_inv_q(avctx->time_base);
framerate.den *= avctx->ticks_per_frame;
}
ff_MFSetAttributeRatio((IMFAttributes *)type, &MF_MT_FRAME_RATE, framerate.num, framerate.den);
// (MS HEVC supports eAVEncH265VProfile_Main_420_8 only.)
if (avctx->codec_id == AV_CODEC_ID_H264) {
UINT32 profile = ff_eAVEncH264VProfile_Base;
switch (avctx->profile) {
case FF_PROFILE_H264_MAIN:
profile = ff_eAVEncH264VProfile_Main;
break;
case FF_PROFILE_H264_HIGH:
profile = ff_eAVEncH264VProfile_High;
break;
}
IMFAttributes_SetUINT32(type, &MF_MT_MPEG2_PROFILE, profile);
}
IMFAttributes_SetUINT32(type, &MF_MT_AVG_BITRATE, avctx->bit_rate);
// Note that some of the ICodecAPI options must be set before SetOutputType.
if (c->codec_api) {
if (avctx->bit_rate)
ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncCommonMeanBitRate, FF_VAL_VT_UI4(avctx->bit_rate));
if (c->opt_enc_rc >= 0)
ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncCommonRateControlMode, FF_VAL_VT_UI4(c->opt_enc_rc));
if (c->opt_enc_quality >= 0)
ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncCommonQuality, FF_VAL_VT_UI4(c->opt_enc_quality));
// Always set the number of b-frames. Qualcomm's HEVC encoder on SD835
// defaults this to 1, and that setting is buggy with many of the
// rate control modes. (0 or 2 b-frames works fine with most rate
// control modes, but 2 seems buggy with the u_vbr mode.) Setting
// "scenario" to "camera_record" sets it in CFR mode (where the default
// is VFR), which makes the encoder avoid dropping frames.
ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncMPVDefaultBPictureCount, FF_VAL_VT_UI4(avctx->max_b_frames));
avctx->has_b_frames = avctx->max_b_frames > 0;
ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVEncH264CABACEnable, FF_VAL_VT_BOOL(1));
if (c->opt_enc_scenario >= 0)
ICodecAPI_SetValue(c->codec_api, &ff_CODECAPI_AVScenarioInfo, FF_VAL_VT_UI4(c->opt_enc_scenario));
}
return 0;
}
static int64_t mf_encv_input_score(AVCodecContext *avctx, IMFMediaType *type)
{
enum AVPixelFormat pix_fmt = ff_media_type_to_pix_fmt((IMFAttributes *)type);
if (pix_fmt != avctx->pix_fmt)
return -1; // can not use
return 0;
}
static int mf_encv_input_adjust(AVCodecContext *avctx, IMFMediaType *type)
{
enum AVPixelFormat pix_fmt = ff_media_type_to_pix_fmt((IMFAttributes *)type);
if (pix_fmt != avctx->pix_fmt) {
av_log(avctx, AV_LOG_ERROR, "unsupported input pixel format set\n");
return AVERROR(EINVAL);
}
//ff_MFSetAttributeSize((IMFAttributes *)type, &MF_MT_FRAME_SIZE, avctx->width, avctx->height);
return 0;
}
static int mf_choose_output_type(AVCodecContext *avctx)
{
MFContext *c = avctx->priv_data;
HRESULT hr;
int ret;
IMFMediaType *out_type = NULL;
int64_t out_type_score = -1;
int out_type_index = -1;
int n;
av_log(avctx, AV_LOG_VERBOSE, "output types:\n");
for (n = 0; ; n++) {
IMFMediaType *type;
int64_t score = -1;
hr = IMFTransform_GetOutputAvailableType(c->mft, c->out_stream_id, n, &type);
if (hr == MF_E_NO_MORE_TYPES || hr == E_NOTIMPL)
break;
if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
av_log(avctx, AV_LOG_VERBOSE, "(need to set input type)\n");
ret = 0;
goto done;
}
if (FAILED(hr)) {
av_log(avctx, AV_LOG_ERROR, "error getting output type: %s\n", ff_hr_str(hr));
ret = AVERROR_EXTERNAL;
goto done;
}
av_log(avctx, AV_LOG_VERBOSE, "output type %d:\n", n);
ff_media_type_dump(avctx, type);
if (c->is_video) {
score = mf_encv_output_score(avctx, type);
} else if (c->is_audio) {
score = mf_enca_output_score(avctx, type);
}
if (score > out_type_score) {
if (out_type)
IMFMediaType_Release(out_type);
out_type = type;
out_type_score = score;
out_type_index = n;
IMFMediaType_AddRef(out_type);
}
IMFMediaType_Release(type);
}
if (out_type) {
av_log(avctx, AV_LOG_VERBOSE, "picking output type %d.\n", out_type_index);
} else {
hr = MFCreateMediaType(&out_type);
if (FAILED(hr)) {
ret = AVERROR(ENOMEM);
goto done;
}
}
ret = 0;
if (c->is_video) {
ret = mf_encv_output_adjust(avctx, out_type);
} else if (c->is_audio) {
ret = mf_enca_output_adjust(avctx, out_type);
}
if (ret >= 0) {
av_log(avctx, AV_LOG_VERBOSE, "setting output type:\n");
ff_media_type_dump(avctx, out_type);
hr = IMFTransform_SetOutputType(c->mft, c->out_stream_id, out_type, 0);
if (!FAILED(hr)) {
ret = 1;
} else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
av_log(avctx, AV_LOG_VERBOSE, "rejected - need to set input type\n");
ret = 0;
} else {
av_log(avctx, AV_LOG_ERROR, "could not set output type (%s)\n", ff_hr_str(hr));
ret = AVERROR_EXTERNAL;
}
}
done:
if (out_type)
IMFMediaType_Release(out_type);
return ret;
}
static int mf_choose_input_type(AVCodecContext *avctx)
{
MFContext *c = avctx->priv_data;
HRESULT hr;
int ret;
IMFMediaType *in_type = NULL;
int64_t in_type_score = -1;
int in_type_index = -1;
int n;
av_log(avctx, AV_LOG_VERBOSE, "input types:\n");
for (n = 0; ; n++) {
IMFMediaType *type = NULL;
int64_t score = -1;
hr = IMFTransform_GetInputAvailableType(c->mft, c->in_stream_id, n, &type);
if (hr == MF_E_NO_MORE_TYPES || hr == E_NOTIMPL)
break;
if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
av_log(avctx, AV_LOG_VERBOSE, "(need to set output type 1)\n");
ret = 0;
goto done;
}
if (FAILED(hr)) {
av_log(avctx, AV_LOG_ERROR, "error getting input type: %s\n", ff_hr_str(hr));
ret = AVERROR_EXTERNAL;
goto done;
}
av_log(avctx, AV_LOG_VERBOSE, "input type %d:\n", n);
ff_media_type_dump(avctx, type);
if (c->is_video) {
score = mf_encv_input_score(avctx, type);
} else if (c->is_audio) {
score = mf_enca_input_score(avctx, type);
}
if (score > in_type_score) {
if (in_type)
IMFMediaType_Release(in_type);
in_type = type;
in_type_score = score;
in_type_index = n;
IMFMediaType_AddRef(in_type);
}
IMFMediaType_Release(type);
}
if (in_type) {
av_log(avctx, AV_LOG_VERBOSE, "picking input type %d.\n", in_type_index);
} else {
// Some buggy MFTs (WMA encoder) fail to return MF_E_TRANSFORM_TYPE_NOT_SET.
av_log(avctx, AV_LOG_VERBOSE, "(need to set output type 2)\n");
ret = 0;
goto done;
}
ret = 0;
if (c->is_video) {
ret = mf_encv_input_adjust(avctx, in_type);
} else if (c->is_audio) {
ret = mf_enca_input_adjust(avctx, in_type);
}
if (ret >= 0) {
av_log(avctx, AV_LOG_VERBOSE, "setting input type:\n");
ff_media_type_dump(avctx, in_type);
hr = IMFTransform_SetInputType(c->mft, c->in_stream_id, in_type, 0);
if (!FAILED(hr)) {
ret = 1;
} else if (hr == MF_E_TRANSFORM_TYPE_NOT_SET) {
av_log(avctx, AV_LOG_VERBOSE, "rejected - need to set output type\n");
ret = 0;
} else {
av_log(avctx, AV_LOG_ERROR, "could not set input type (%s)\n", ff_hr_str(hr));
ret = AVERROR_EXTERNAL;
}
}
done:
if (in_type)
IMFMediaType_Release(in_type);
return ret;
}
static int mf_negotiate_types(AVCodecContext *avctx)
{
// This follows steps 1-5 on:
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa965264(v=vs.85).aspx
// If every MFT implementer does this correctly, this loop should at worst
// be repeated once.
int need_input = 1, need_output = 1;
int n;
for (n = 0; n < 2 && (need_input || need_output); n++) {
int ret;
ret = mf_choose_input_type(avctx);
if (ret < 0)
return ret;
need_input = ret < 1;
ret = mf_choose_output_type(avctx);
if (ret < 0)
return ret;
need_output = ret < 1;
}
if (need_input || need_output) {
av_log(avctx, AV_LOG_ERROR, "format negotiation failed (%d/%d)\n",
need_input, need_output);
return AVERROR_EXTERNAL;
}
return 0;
}
static int mf_setup_context(AVCodecContext *avctx)
{
MFContext *c = avctx->priv_data;
HRESULT hr;
int ret;
hr = IMFTransform_GetInputStreamInfo(c->mft, c->in_stream_id, &c->in_info);
if (FAILED(hr))
return AVERROR_EXTERNAL;
av_log(avctx, AV_LOG_VERBOSE, "in_info: size=%d, align=%d\n",
(int)c->in_info.cbSize, (int)c->in_info.cbAlignment);
hr = IMFTransform_GetOutputStreamInfo(c->mft, c->out_stream_id, &c->out_info);
if (FAILED(hr))
return AVERROR_EXTERNAL;
c->out_stream_provides_samples =
(c->out_info.dwFlags & MFT_OUTPUT_STREAM_PROVIDES_SAMPLES) ||
(c->out_info.dwFlags & MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES);
av_log(avctx, AV_LOG_VERBOSE, "out_info: size=%d, align=%d%s\n",
(int)c->out_info.cbSize, (int)c->out_info.cbAlignment,
c->out_stream_provides_samples ? " (provides samples)" : "");
if ((ret = mf_output_type_get(avctx)) < 0)
return ret;
return 0;
}
static int mf_unlock_async(AVCodecContext *avctx)
{
MFContext *c = avctx->priv_data;
HRESULT hr;
IMFAttributes *attrs;
UINT32 v;
int res = AVERROR_EXTERNAL;
// For hw encoding we unfortunately need to use async mode, otherwise
// play it safe and avoid it.
if (!(c->is_video && c->opt_enc_hw))
return 0;
hr = IMFTransform_GetAttributes(c->mft, &attrs);
if (FAILED(hr)) {
av_log(avctx, AV_LOG_ERROR, "error retrieving MFT attributes: %s\n", ff_hr_str(hr));
goto err;
}
hr = IMFAttributes_GetUINT32(attrs, &MF_TRANSFORM_ASYNC, &v);
if (FAILED(hr)) {
av_log(avctx, AV_LOG_ERROR, "error querying async: %s\n", ff_hr_str(hr));
goto err;
}
if (!v) {
av_log(avctx, AV_LOG_ERROR, "hardware MFT is not async\n");
goto err;
}
hr = IMFAttributes_SetUINT32(attrs, &MF_TRANSFORM_ASYNC_UNLOCK, TRUE);
if (FAILED(hr)) {
av_log(avctx, AV_LOG_ERROR, "could not set async unlock: %s\n", ff_hr_str(hr));
goto err;
}
hr = IMFTransform_QueryInterface(c->mft, &IID_IMFMediaEventGenerator, (void **)&c->async_events);
if (FAILED(hr)) {
av_log(avctx, AV_LOG_ERROR, "could not get async interface\n");
goto err;
}
res = 0;
err:
IMFAttributes_Release(attrs);
return res;
}
static int mf_create(void *log, IMFTransform **mft, const AVCodec *codec, int use_hw)