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
30
31 #if CONFIG_D3D11VA
33 #endif
34 #if CONFIG_DXVA2
35 #define COBJMACROS
37 #endif
38
39 #ifdef _WIN32
41 #else
42 #include <dlfcn.h>
43 #endif
44 //will be in public headers soon
45 #define AMF_VIDEO_DECODER_OUTPUT_FORMAT L"OutputDecodeFormat"
46
54 },
56 },
58 };
59
61 {
62 AMFSurface *surface = (AMFSurface*)(
data);
63 surface->pVtbl->Release(surface);
64 }
65
67 {
68 if( AMF_GET_MAJOR_VERSION(amf_device_ctx->
version) <= 1 &&
69 AMF_GET_MINOR_VERSION(amf_device_ctx->
version) <= 4 &&
70 AMF_GET_SUBMINOR_VERSION(amf_device_ctx->
version) < 36)
71 return 1;
72 return 0;
73 }
74
76 {
81 AMF_RESULT res;
83 amf_int64 color_profile;
84 int pool_size = 36;
85 // way-around for older drivers that don't support dynamic bitness detection -
86 // define HEVC and VP9 10-bit based on container info
88
90 ctx->resolution_changed = 0;
91
94 codec_id = AMFVideoDecoderUVD_H264_AVC;
95 break;
97 codec_id = AMFVideoDecoderHW_H265_HEVC;
98 if(no_bitness_detect){
100 codec_id = AMFVideoDecoderHW_H265_MAIN10;
101 }
102 } break;
105 if(no_bitness_detect){
107 codec_id = AMFVideoDecoderHW_VP9_10BIT;
108 }
109 } break;
112 break;
113 default:
114 break;
115 }
117
118 res = amf_device_ctx->factory->pVtbl->CreateComponent(amf_device_ctx->factory, amf_device_ctx->context,
codec_id, &
ctx->decoder);
120
121 // Color Metadata
122 /// Color Range (Support for older Drivers)
124 AMF_ASSIGN_PROPERTY_BOOL(res,
ctx->decoder, AMF_VIDEO_DECODER_FULL_RANGE_COLOR, 1);
126 AMF_ASSIGN_PROPERTY_BOOL(res,
ctx->decoder, AMF_VIDEO_DECODER_FULL_RANGE_COLOR, 0);
127 }
128 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN;
132 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_601;
133 } else {
134 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_601;
135 }
136 break;
139 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_709;
140 } else {
141 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_709;
142 }
143 break;
147 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_2020;
148 } else {
149 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_2020;
150 }
151 break;
152 }
153 if (color_profile != AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN)
154 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_COLOR_PROFILE, color_profile);
156 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_COLOR_TRANSFER_CHARACTERISTIC, (amf_int64)avctx->
color_trc);
157
159 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_COLOR_PRIMARIES, (amf_int64)avctx->
color_primaries);
160
161 if (
ctx->timestamp_mode != -1)
162 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_TIMESTAMP_MODE,
ctx->timestamp_mode);
163 if (
ctx->decoder_mode != -1)
164 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_REORDER_MODE,
ctx->decoder_mode);
165 if (
ctx->dpb_size != -1)
166 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_DPB_SIZE,
ctx->dpb_size);
167 if (
ctx->lowlatency != -1)
168 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_LOW_LATENCY,
ctx->lowlatency);
169 if (
ctx->smart_access_video != -1) {
170 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_ENABLE_SMART_ACCESS_VIDEO,
ctx->smart_access_video != 0);
171 if (res != AMF_OK) {
172 av_log(avctx,
AV_LOG_ERROR,
"The Smart Access Video is not supported by AMF decoder.\n");
174 } else {
176 // Set low latency mode if Smart Access Video is enabled
177 if (
ctx->smart_access_video != 0) {
178 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_LOW_LATENCY,
true);
179 av_log(avctx,
AV_LOG_INFO,
"The Smart Access Video set low latency mode for decoder.\n");
180 }
181 }
182 }
183 if (
ctx->skip_transfer_sav != -1)
184 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_SKIP_TRANSFER_SMART_ACCESS_VIDEO,
ctx->skip_transfer_sav);
185
186 if (
ctx->copy_output != -1)
187 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_SURFACE_COPY,
ctx->copy_output);
188
190 const uint8_t *extradata;
191 int extradata_size;
193 res = amf_device_ctx->context->pVtbl->AllocBuffer(amf_device_ctx->context, AMF_MEMORY_HOST, extradata_size, &
buffer);
194 if (res == AMF_OK) {
195 memcpy(
buffer->pVtbl->GetNative(
buffer), extradata, extradata_size);
196 AMF_ASSIGN_PROPERTY_INTERFACE(res,
ctx->decoder, AMF_VIDEO_DECODER_EXTRADATA,
buffer);
199 }
200 }
201 if (
ctx->surface_pool_size == -1) {
202 ctx->surface_pool_size = pool_size;
207 }
208
209 //at the moment, there is such a restriction in AMF.
210 //when it is possible, I will remove this code
211 if (
ctx->surface_pool_size > 100)
212 ctx->surface_pool_size = 100;
213
214 AMF_ASSIGN_PROPERTY_INT64(res,
ctx->decoder, AMF_VIDEO_DECODER_SURFACE_POOL_SIZE,
ctx->surface_pool_size);
215 res =
ctx->decoder->pVtbl->Init(
ctx->decoder, AMF_SURFACE_UNKNOWN, avctx->
width, avctx->
height);
216 if (res != AMF_OK) {
219 }
220 return 0;
221 }
222
224 {
226
228 ctx->decoder->pVtbl->Terminate(
ctx->decoder);
229 ctx->decoder->pVtbl->Release(
ctx->decoder);
231 }
232
235
236 return 0;
237 }
238
240 {
246 return 0;
250
252 return 0;
253
254 hwframes_ctx->
width = new_width;
255 hwframes_ctx->
height = new_height;
259
265 }
266 return 0;
267 }
268
270 {
276
281 {
284
286 } else {
289 }
290 } else {
293 }
298
300 // if bitness detection is not supported in legacy driver use format from container
307 }
308 }else{
309 AMFVariantStruct format_var = {0};
310
313
315 }
317 {
318 // this values should be set for avcodec_open2
319 // will be updated after header decoded if not true.
328 }
329 else
331
332 return 0;
333 }
337 }
338
340 {
341 AMF_RESULT res;
342 AMFVariantStruct var;
343 res = AMFVariantInit(&var);
344 if (res == AMF_OK) {
345 res =
object->pVtbl->GetProperty(
object,
name, &var);
346 if (res == AMF_OK) {
347 if (var.type == AMF_VARIANT_INTERFACE) {
348 AMFGuid guid_AMFBuffer = IID_AMFBuffer();
349 AMFInterface *amf_interface = AMFVariantInterface(&var);
350 res = amf_interface->pVtbl->QueryInterface(amf_interface, &guid_AMFBuffer, (
void**)
val);
351 } else {
352 res = AMF_INVALID_DATA_TYPE;
353 }
354 }
355 AMFVariantClear(&var);
356 }
357 return res;
358 }
359
361 {
362 AMFVariantStruct var = {0};
363 AMFPlane *plane;
366 int format_amf;
367
369 // prepare frame similar to ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
370
374
376
382
383 ////
388
389 frame->data[0] = (uint8_t *)surface;
391 format_amf = surface->pVtbl->GetFormat(surface);
394 } else {
395 ret = surface->pVtbl->Convert(surface, AMF_MEMORY_HOST);
397
398 for (
i = 0;
i < surface->pVtbl->GetPlanesCount(surface);
i++) {
399 plane = surface->pVtbl->GetPlaneAt(surface,
i);
400 frame->data[
i] = plane->pVtbl->GetNative(plane);
401 frame->linesize[
i] = plane->pVtbl->GetHPitch(plane);
402 }
403
408
409 format_amf = surface->pVtbl->GetFormat(surface);
411 }
412
415
416 frame->pts = surface->pVtbl->GetPts(surface);
417
418 surface->pVtbl->GetProperty(surface,
L "FFMPEG:dts", &var);
419 frame->pkt_dts = var.int64Value;
420
421 frame->duration = surface->pVtbl->GetDuration(surface);
422 if (
frame->duration < 0)
424
429
431 AMFBuffer * hdrmeta_buffer =
NULL;
433 if (hdrmeta_buffer !=
NULL) {
434 AMFHDRMetadata * hdrmeta = (AMFHDRMetadata*)hdrmeta_buffer->pVtbl->GetNative(hdrmeta_buffer);
437 if (hdrmeta !=
NULL) {
439 const int chroma_den = 50000;
440 const int luma_den = 10000;
441
442 if (!mastering)
444
447
450
453
456
459
462 if (hdrmeta->maxContentLightLevel) {
464
465 if (!light)
467
468 light->
MaxCLL = hdrmeta->maxContentLightLevel;
469 light->
MaxFALL = hdrmeta->maxFrameAverageLightLevel;
470 }
471 }
472 }
473 }
474 return 0;
475 }
476
478 {
480 AMF_RESULT
ret = AMF_OK;
481 AMFSurface *surface =
NULL;
482 AMFData *data_out =
NULL;
483
484 ret =
ctx->decoder->pVtbl->QueryOutput(
ctx->decoder, &data_out);
485 if (
ret != AMF_OK &&
ret != AMF_REPEAT) {
487 }
488 if (data_out ==
NULL) {
489 return AMF_REPEAT;
490 }
491
492 if (data_out) {
493 AMFGuid guid = IID_AMFSurface();
494 data_out->pVtbl->QueryInterface(data_out, &guid, (void**)&surface); // query for buffer interface
495 data_out->pVtbl->Release(data_out);
497 }
498
501 return AMF_OK;
503
504 if (surface) {
505 surface->pVtbl->Release(surface);
507 }
509 }
510
512 {
513 AMF_RESULT res;
514
519 AMF_ASSIGN_PROPERTY_INT64(res,
buffer,
L "FFMPEG:dts",
pkt->
dts);
520 if (res != AMF_OK)
522 return AMF_OK;
523 }
524
526 {
530 AMFContext *ctxt = amf_device_ctx->context;
531 void *mem;
532 AMF_RESULT err;
533 AMFBuffer *buf =
NULL;
534
537
541 err = buf->pVtbl->SetSize(buf,
pkt->
size);
542 AMF_RETURN_IF_FALSE(ctxt, err == AMF_OK, err,
"amf_buffer_from_packet() - SetSize failed");
543 // get the memory location and check the buffer was indeed allocated
544 mem = buf->pVtbl->GetNative(buf);
546
547 // copy the packet memory and clear data padding
550
552 }
553
555 {
557 AMFBuffer *buf;
558 AMF_RESULT res;
559 int got_frame = 0;
561
564
565 // get packet if needed
567 if(
ctx->resolution_changed)
568 ctx->resolution_changed = 0;
569 else{
576 //nothing to consume, start external drain
577 ctx->decoder->pVtbl->Drain(
ctx->decoder);
579 }
580 }
581 }
582
584 // submit frame
587 do{
588 res =
ctx->decoder->pVtbl->SubmitInput(
ctx->decoder, (AMFData*) buf);
589 if(res == AMF_DECODER_NO_FREE_SURFACES)
590 {
592 }
593 } while (res == AMF_DECODER_NO_FREE_SURFACES);
594
595 buf->pVtbl->Release(buf);
596
597 if(res == AMF_DECODER_NO_FREE_SURFACES) {
598 // input is not consumed, need to QueryOutput and submit again
599 av_log(avctx,
AV_LOG_VERBOSE,
"SubmitInput() returned NO_FREE_SURFACES and came out of loop - should never happen\n");
600 res = AMF_OK;
601 } else if (res == AMF_RESOLUTION_CHANGED) {
602 //input is not consumed, start internal drain
603 ctx->decoder->pVtbl->Drain(
ctx->decoder);
605 // process resolution_changed when internal drain is complete
606 ctx->resolution_changed = 1;
607 res = AMF_OK;
608 } else if (res != AMF_OK && res != AMF_NEED_MORE_INPUT && res != AMF_REPEAT) {
611 }
612 }
613
615 if (res == AMF_OK)
616 got_frame = 1;
617 else if (res == AMF_REPEAT)
618 // decoder has no output yet
619 res = AMF_OK;
620 else if (res == AMF_EOF) {
621 // drain is complete
623 if(
ctx->resolution_changed){
624 // re-initialze decoder
625 AMFVariantStruct size_var = {0};
626 AMFVariantStruct format_var = {0};
627 res =
ctx->decoder->pVtbl->GetProperty(
ctx->decoder, AMF_VIDEO_DECODER_CURRENT_SIZE, &size_var);
628 if (res != AMF_OK) {
630 }
631
632 avctx->
width = size_var.sizeValue.width;
633 avctx->
height = size_var.sizeValue.height;
637 if (res != AMF_OK) {
640 }
642 if (res == AMF_OK) {
644 }
645
646 if (res < 0)
648 }else
650 } else {
652 }
653 return got_frame ? 0 :
AVERROR(EAGAIN);
654 }
655
657 {
659 ctx->decoder->pVtbl->Flush(
ctx->decoder);
660 }
661
662 #define OFFSET(x) offsetof(AMFDecoderContext, x)
663 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
664
666 // Decoder mode
667 {
"decoder_mode",
"Decoder mode",
OFFSET(decoder_mode),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AMF_VIDEO_DECODER_MODE_LOW_LATENCY,
VD,
"decoder_mode" },
668 {
"regular",
"DPB delay is based on number of reference frames + 1", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_DECODER_MODE_REGULAR }, 0, 0,
VD,
"decoder_mode" },
669 {
"compliant",
"DPB delay is based on profile - up to 16", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_DECODER_MODE_COMPLIANT }, 0, 0,
VD,
"decoder_mode" },
670 {
"low_latency",
"DPB delay is 0", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_DECODER_MODE_LOW_LATENCY }, 0, 0,
VD,
"decoder_mode" },
671
672 // Timestamp mode
673 {
"timestamp_mode",
"Timestamp mode",
OFFSET(timestamp_mode),
AV_OPT_TYPE_INT, { .i64 = AMF_TS_SORT }, -1, AMF_TS_DECODE,
VD,
"timestamp_mode" },
674 {
"presentation",
"Preserve timestamps from input to output", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_TS_PRESENTATION }, 0, 0,
VD,
"timestamp_mode" },
675 {
"sort",
"Resort PTS list", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_TS_SORT }, 0, 0,
VD,
"timestamp_mode" },
676 {
"decode",
"Decode order", 0,
AV_OPT_TYPE_CONST, { .i64 = AMF_TS_DECODE }, 0, 0,
VD,
"timestamp_mode" },
677
678 // Reference frame management
679 {
"surface_pool_size",
"Number of surfaces in the decode pool",
OFFSET(surface_pool_size),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VD,
NULL },
681
684 {
"skip_transfer_sav",
"Skip transfer on another GPU when SAV enabled",
OFFSET(skip_transfer_sav),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VD,
NULL },
686
688 };
689
695 };
696
697 #define DEFINE_AMF_DECODER(x, X, bsf_name) \
698 const FFCodec ff_##x##_amf_decoder = { \
699 .p.name = #x "_amf", \
700 CODEC_LONG_NAME(#X " AMD AMF video decoder"), \
701 .priv_data_size = sizeof(AMFDecoderContext), \
702 .p.type = AVMEDIA_TYPE_VIDEO, \
703 .p.id = AV_CODEC_ID_##X, \
704 .init = amf_decode_init, \
705 FF_CODEC_RECEIVE_FRAME_CB(amf_decode_frame), \
706 .flush = amf_decode_flush, \
707 .close = amf_decode_close, \
708 .bsfs = bsf_name, \
709 .p.capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING, \
710 .p.priv_class = &amf_decode_class, \
711 .hw_configs = amf_hw_configs, \
712 .p.wrapper_name = "amf", \
713 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE, \
714 }; \
715