1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "config.h"
20 #include "config_components.h"
21
25 #if CONFIG_D3D11VA
27 #endif
28 #if CONFIG_DXVA2
29 #define COBJMACROS
31 #endif
35
39
40 #if CONFIG_D3D11VA
41 #include <d3d11.h>
42 #endif
43
44 #ifdef _WIN32
46 #else
47 #include <dlfcn.h>
48 #endif
49
50 #define FFMPEG_AMF_WRITER_ID L"ffmpeg_amf"
51
52 #define PTS_PROP L"PtsProp"
53
57 #if CONFIG_D3D11VA
59 #endif
60 #if CONFIG_DXVA2
62 #endif
64 };
65
70
72 {
80 };
81
83 {
88 }
89 }
90 return AMF_SURFACE_UNKNOWN;
91 }
92
94 const wchar_t *scope,
const wchar_t *
message)
95 {
98 }
99
101 {
102 }
103
105 {
108 };
109
111 {
113 AMFInit_Fn init_fun;
114 AMFQueryVersion_Fn version_fun;
115 AMF_RESULT res;
116
118 if (!
ctx->delayed_frame) {
120 }
121 // hardcoded to current HW queue size - will auto-realloc if too small
124 if (!
ctx->timestamp_list) {
126 }
128
129
130 ctx->library = dlopen(AMF_DLL_NAMEA, RTLD_NOW | RTLD_LOCAL);
133
134 init_fun = (AMFInit_Fn)dlsym(
ctx->library, AMF_INIT_FUNCTION_NAME);
136
137 version_fun = (AMFQueryVersion_Fn)dlsym(
ctx->library, AMF_QUERY_VERSION_FUNCTION_NAME);
139
140 res = version_fun(&
ctx->version);
142 res = init_fun(AMF_FULL_VERSION, &
ctx->factory);
144 res =
ctx->factory->pVtbl->GetTrace(
ctx->factory, &
ctx->trace);
148 return 0;
149 }
150
151 #if CONFIG_D3D11VA
153 {
155 AMF_RESULT res;
156
157 res =
ctx->context->pVtbl->InitDX11(
ctx->context, hwctx->
device, AMF_DX11_1);
158 if (res != AMF_OK) {
159 if (res == AMF_NOT_SUPPORTED)
161 else
162 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on the given D3D11 device: %d.\n", res);
164 }
165
166 return 0;
167 }
168 #endif
169
170 #if CONFIG_DXVA2
172 {
174 HANDLE device_handle;
175 IDirect3DDevice9 *device;
176 HRESULT hr;
177 AMF_RESULT res;
179
180 hr = IDirect3DDeviceManager9_OpenDeviceHandle(hwctx->
devmgr, &device_handle);
181 if (FAILED(hr)) {
182 av_log(avctx,
AV_LOG_ERROR,
"Failed to open device handle for Direct3D9 device: %lx.\n", (
unsigned long)hr);
184 }
185
186 hr = IDirect3DDeviceManager9_LockDevice(hwctx->
devmgr, device_handle, &device, FALSE);
187 if (SUCCEEDED(hr)) {
188 IDirect3DDeviceManager9_UnlockDevice(hwctx->
devmgr, device_handle, FALSE);
190 } else {
191 av_log(avctx,
AV_LOG_ERROR,
"Failed to lock device handle for Direct3D9 device: %lx.\n", (
unsigned long)hr);
193 }
194
195 IDirect3DDeviceManager9_CloseDeviceHandle(hwctx->
devmgr, device_handle);
196
199
200 res =
ctx->context->pVtbl->InitDX9(
ctx->context, device);
201
202 IDirect3DDevice9_Release(device);
203
204 if (res != AMF_OK) {
205 if (res == AMF_NOT_SUPPORTED)
207 else
208 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on given D3D9 device: %d.\n", res);
210 }
211
212 return 0;
213 }
214 #endif
215
217 {
219 AMFContext1 *context1 =
NULL;
220 AMF_RESULT res;
222
223 ctx->hwsurfaces_in_queue = 0;
224 ctx->hwsurfaces_in_queue_max = 16;
225
226 // configure AMF logger
227 // the return of these functions indicates old state and do not affect behaviour
228 ctx->trace->pVtbl->EnableWriter(
ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT,
ctx->log_to_dbg != 0 );
230 ctx->trace->pVtbl->SetWriterLevel(
ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT, AMF_TRACE_TRACE);
231 ctx->trace->pVtbl->EnableWriter(
ctx->trace, AMF_TRACE_WRITER_CONSOLE, 0);
232 ctx->trace->pVtbl->SetGlobalLevel(
ctx->trace, AMF_TRACE_TRACE);
233
234 // connect AMF logger to av_log
236 ctx->tracer.avctx = avctx;
239
240 res =
ctx->factory->pVtbl->CreateContext(
ctx->factory, &
ctx->context);
242
243 // If a device was passed to the encoder, try to initialise from that.
246
248 av_log(avctx,
AV_LOG_ERROR,
"Format of input frames context (%s) is not supported by AMF.\n",
251 }
252
254 #if CONFIG_D3D11VA
259 break;
260 #endif
261 #if CONFIG_DXVA2
266 break;
267 #endif
268 default:
269 av_log(avctx,
AV_LOG_ERROR,
"AMF initialisation from a %s frames context is not supported.\n",
272 }
273
275 if (!
ctx->hw_frames_ctx)
277
280
283
284 switch (device_ctx->
type) {
285 #if CONFIG_D3D11VA
287 ret = amf_init_from_d3d11_device(avctx, device_ctx->
hwctx);
290 break;
291 #endif
292 #if CONFIG_DXVA2
294 ret = amf_init_from_dxva2_device(avctx, device_ctx->
hwctx);
297 break;
298 #endif
299 default:
300 av_log(avctx,
AV_LOG_ERROR,
"AMF initialisation from a %s device is not supported.\n",
303 }
304
306 if (!
ctx->hw_device_ctx)
308
309 } else {
310 res =
ctx->context->pVtbl->InitDX11(
ctx->context,
NULL, AMF_DX11_1);
311 if (res == AMF_OK) {
313 } else {
314 res =
ctx->context->pVtbl->InitDX9(
ctx->context,
NULL);
315 if (res == AMF_OK) {
317 } else {
318 AMFGuid guid = IID_AMFContext1();
319 res =
ctx->context->pVtbl->QueryInterface(
ctx->context, &guid, (
void**)&context1);
321
322 res = context1->pVtbl->InitVulkan(context1,
NULL);
323 context1->pVtbl->Release(context1);
324 if (res != AMF_OK) {
325 if (res == AMF_NOT_SUPPORTED)
326 av_log(avctx,
AV_LOG_ERROR,
"AMF via Vulkan is not supported on the given device.\n");
327 else
328 av_log(avctx,
AV_LOG_ERROR,
"AMF failed to initialise on the given Vulkan device: %d.\n", res);
330 }
332 }
333 }
334 }
335 return 0;
336 }
337
339 {
342 AMF_RESULT res;
344
348 break;
351 break;
354 break;
355 default:
356 break;
357 }
359
360 if (
ctx->hw_frames_ctx)
362 else
364
368
369 res =
ctx->factory->pVtbl->CreateComponent(
ctx->factory,
ctx->context,
codec_id, &
ctx->encoder);
371
372 return 0;
373 }
374
376 {
378
379 if (
ctx->delayed_surface) {
380 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
382 }
383
385 ctx->encoder->pVtbl->Terminate(
ctx->encoder);
386 ctx->encoder->pVtbl->Release(
ctx->encoder);
388 }
389
391 ctx->context->pVtbl->Terminate(
ctx->context);
392 ctx->context->pVtbl->Release(
ctx->context);
394 }
397
400 }
402 dlclose(
ctx->library);
404 }
409 ctx->delayed_drain = 0;
412
413 return 0;
414 }
415
417 AMFSurface* surface)
418 {
419 AMFPlane *plane;
420 uint8_t *dst_data[4];
421 int dst_linesize[4];
424
425 planes = surface->pVtbl->GetPlanesCount(surface);
427
429 plane = surface->pVtbl->GetPlaneAt(surface,
i);
430 dst_data[
i] = plane->pVtbl->GetNative(plane);
431 dst_linesize[
i] = plane->pVtbl->GetHPitch(plane);
432 }
436
437 return 0;
438 }
439
441 {
444 AMFVariantStruct var = {0};
447
450 }
452
455 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE, &var);
456 if(var.int64Value == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR) {
458 }
459 break;
461 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE, &var);
462 if (var.int64Value == AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE_IDR) {
464 }
465 break;
467 buffer->pVtbl->GetProperty(
buffer, AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE, &var);
468 if (var.int64Value == AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE_KEY) {
470 }
471 default:
472 break;
473 }
474
476
477 pkt->
pts = var.int64Value;
// original pts
478
479
482
483 // calc dts shift if max_b_frames > 0
484 if ((
ctx->max_b_frames > 0 || ((
ctx->pa_adaptive_mini_gop == 1) ?
true :
false)) &&
ctx->dts_delay == 0) {
487
489 "timestamp_list is empty while max_b_frames = %d\n", avctx->
max_b_frames);
493 }
494 ctx->dts_delay = timestamp_last - timestamp;
495 }
497 return 0;
498 }
499
500 // amfenc API implementation
502 {
504
508 return 0;
509 }
510 }
511 }
514 }
515
517 {
518 AMF_RESULT res;
519 AMFVariantStruct var;
520 res = AMFVariantInit(&var);
521 if (res == AMF_OK) {
522 AMFGuid guid_AMFInterface = IID_AMFInterface();
523 AMFInterface *amf_interface;
524 res =
val->pVtbl->QueryInterface(
val, &guid_AMFInterface, (
void**)&amf_interface);
525
526 if (res == AMF_OK) {
527 res = AMFVariantAssignInterface(&var, amf_interface);
528 amf_interface->pVtbl->Release(amf_interface);
529 }
530 if (res == AMF_OK) {
531 res =
object->pVtbl->SetProperty(
object,
name, var);
532 }
533 AMFVariantClear(&var);
534 }
535 return res;
536 }
537
539 {
540 AMF_RESULT res;
541 AMFVariantStruct var;
542 res = AMFVariantInit(&var);
543 if (res == AMF_OK) {
544 res =
object->pVtbl->GetProperty(
object,
name, &var);
545 if (res == AMF_OK) {
546 if (var.type == AMF_VARIANT_INTERFACE) {
547 AMFGuid guid_AMFBuffer = IID_AMFBuffer();
548 AMFInterface *amf_interface = AMFVariantInterface(&var);
549 res = amf_interface->pVtbl->QueryInterface(amf_interface, &guid_AMFBuffer, (
void**)
val);
550 } else {
551 res = AMF_INVALID_DATA_TYPE;
552 }
553 }
554 AMFVariantClear(&var);
555 }
556 return res;
557 }
558
560 {
562 AMFBuffer *frame_ref_storage_buffer =
NULL;
563 AMF_RESULT res;
564
565 res =
context->pVtbl->AllocBuffer(
context, AMF_MEMORY_HOST,
sizeof(frame_ref), &frame_ref_storage_buffer);
566 if (res == AMF_OK) {
568 if (frame_ref) {
569 memcpy(frame_ref_storage_buffer->pVtbl->GetNative(frame_ref_storage_buffer), &frame_ref, sizeof(frame_ref));
570 } else {
571 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
572 frame_ref_storage_buffer =
NULL;
573 }
574 }
575 return frame_ref_storage_buffer;
576 }
577
579 {
581 memcpy(&frame_ref, frame_ref_storage_buffer->pVtbl->GetNative(frame_ref_storage_buffer), sizeof(frame_ref));
583 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
584 }
585
587 {
589 AMFSurface *surface;
590 AMF_RESULT res;
592 AMF_RESULT res_query;
595 int block_and_wait;
596 int query_output_data_flag = 0;
597 AMF_RESULT res_resubmit;
598
601
606 }
607
608 if (!
frame->
buf[0]) {
// submit drain
609 if (!
ctx->eof) {
// submit drain one time only
610 if (
ctx->delayed_surface !=
NULL) {
611 ctx->delayed_drain = 1;
// input queue is full: resubmit Drain() in ff_amf_receive_packet
612 }
else if(!
ctx->delayed_drain) {
613 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
614 if (res == AMF_INPUT_FULL) {
615 ctx->delayed_drain = 1;
// input queue is full: resubmit Drain() in ff_amf_receive_packet
616 } else {
617 if (res == AMF_OK) {
618 ctx->eof = 1;
// drain started
619 }
621 }
622 }
623 }
624 }
else if (!
ctx->delayed_surface) {
// submit frame
625 int hw_surface = 0;
626
627 // prepare surface from frame
629 #if CONFIG_D3D11VA
631 {
632 static const GUID AMFTextureArrayIndexGUID = { 0x28115527, 0xe7c3, 0x4b66, { 0x99, 0xd3, 0x4f, 0x2a, 0xe6, 0xb4, 0x7f, 0xaf } };
633 ID3D11Texture2D *texture = (ID3D11Texture2D*)
frame->
data[0];
// actual texture
634 int index = (intptr_t)
frame->
data[1];
// index is a slice in texture array is - set to tell AMF which slice to use
635
638
639 texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID,
sizeof(
index), &
index);
640
641 res =
ctx->context->pVtbl->CreateSurfaceFromDX11Native(
ctx->context, texture, &surface,
NULL);
// wrap to AMF surface
643
644 hw_surface = 1;
645 }
646 break;
647 #endif
648 #if CONFIG_DXVA2
650 {
651 IDirect3DSurface9 *texture = (IDirect3DSurface9 *)
frame->
data[3];
// actual texture
652
653 res =
ctx->context->pVtbl->CreateSurfaceFromDX9Native(
ctx->context, texture, &surface,
NULL);
// wrap to AMF surface
655
656 hw_surface = 1;
657 }
658 break;
659 #endif
660 default:
661 {
662 res =
ctx->context->pVtbl->AllocSurface(
ctx->context, AMF_MEMORY_HOST,
ctx->format, avctx->
width, avctx->
height, &surface);
665 }
666 break;
667 }
668
669 if (hw_surface) {
670 AMFBuffer *frame_ref_storage_buffer;
671
672 // input HW surfaces can be vertically aligned by 16; tell AMF the real size
674
677
680 ctx->hwsurfaces_in_queue++;
681 frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
682 }
683
684 surface->pVtbl->SetPts(surface,
frame->
pts);
686
689 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_AUD, !!
ctx->aud);
690 break;
692 AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_AUD, !!
ctx->aud);
693 break;
694 //case AV_CODEC_ID_AV1 not supported
695 default:
696 break;
697 }
698
699 // submit surface
700 res =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)surface);
701 if (res == AMF_INPUT_FULL) { // handle full queue
702 //store surface for later submission
703 ctx->delayed_surface = surface;
704 } else {
706 surface->pVtbl->Release(surface);
708
713 }
714 }
715
716
717 do {
718 block_and_wait = 0;
719 // poll data
720 if (!avpkt->
data && !avpkt->
buf) {
721 res_query =
ctx->encoder->pVtbl->QueryOutput(
ctx->encoder, &
data);
723 // copy data to packet
725 AMFGuid guid = IID_AMFBuffer();
726 query_output_data_flag = 1;
727 data->pVtbl->QueryInterface(
data, &guid, (
void**)&
buffer);
// query for buffer interface
729
731
732 if (
data->pVtbl->HasProperty(
data,
L "av_frame_ref")) {
733 AMFBuffer* frame_ref_storage_buffer;
737 ctx->hwsurfaces_in_queue--;
738 }
739
741
743 }
744 }
745 res_resubmit = AMF_OK;
746 if (
ctx->delayed_surface !=
NULL) {
// try to resubmit frame
747 res_resubmit =
ctx->encoder->pVtbl->SubmitInput(
ctx->encoder, (AMFData*)
ctx->delayed_surface);
748 if (res_resubmit != AMF_INPUT_FULL) {
750 ctx->delayed_surface->pVtbl->Release(
ctx->delayed_surface);
754
758 }
759 }
else if (
ctx->delayed_drain) {
// try to resubmit drain
760 res =
ctx->encoder->pVtbl->Drain(
ctx->encoder);
761 if (res != AMF_INPUT_FULL) {
762 ctx->delayed_drain = 0;
763 ctx->eof = 1;
// drain started
765 } else {
766 av_log(avctx,
AV_LOG_WARNING,
"Data acquired but delayed drain submission got AMF_INPUT_FULL- should not happen\n");
767 }
768 }
769
770 if (query_output_data_flag == 0) {
771 if (res_resubmit == AMF_INPUT_FULL ||
ctx->delayed_drain || (
ctx->eof && res_query != AMF_EOF) || (
ctx->hwsurfaces_in_queue >=
ctx->hwsurfaces_in_queue_max)) {
772 block_and_wait = 1;
774 }
775 }
776 } while (block_and_wait);
777
778 if (res_query == AMF_EOF) {
782 } else {
784 }
786 }
787
789 #if CONFIG_D3D11VA
792 #endif
793 #if CONFIG_DXVA2
796 #endif
798 };