FFmpeg
amfenc.c
Go to the documentation of this file.
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 
22 #include "libavutil/avassert.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/hwcontext.h"
27 #if CONFIG_D3D11VA
29 #endif
30 #if CONFIG_DXVA2
31 #define COBJMACROS
33 #endif
34 #include "libavutil/mem.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/time.h"
37 
38 #include "amfenc.h"
39 #include "encode.h"
40 #include "internal.h"
42 
43 #define AMF_AV_FRAME_REF L"av_frame_ref"
44 #define PTS_PROP L"PtsProp"
45 
46 static int amf_save_hdr_metadata(AVCodecContext *avctx, const AVFrame *frame, AMFHDRMetadata *hdrmeta)
47 {
48  AVFrameSideData *sd_display;
49  AVFrameSideData *sd_light;
50  AVMasteringDisplayMetadata *display_meta;
51  AVContentLightMetadata *light_meta;
52 
54  if (sd_display) {
55  display_meta = (AVMasteringDisplayMetadata *)sd_display->data;
56  if (display_meta->has_luminance) {
57  const unsigned int luma_den = 10000;
58  hdrmeta->maxMasteringLuminance =
59  (amf_uint32)(luma_den * av_q2d(display_meta->max_luminance));
60  hdrmeta->minMasteringLuminance =
61  FFMIN((amf_uint32)(luma_den * av_q2d(display_meta->min_luminance)), hdrmeta->maxMasteringLuminance);
62  }
63  if (display_meta->has_primaries) {
64  const unsigned int chroma_den = 50000;
65  hdrmeta->redPrimary[0] =
66  FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[0][0])), chroma_den);
67  hdrmeta->redPrimary[1] =
68  FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[0][1])), chroma_den);
69  hdrmeta->greenPrimary[0] =
70  FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[1][0])), chroma_den);
71  hdrmeta->greenPrimary[1] =
72  FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[1][1])), chroma_den);
73  hdrmeta->bluePrimary[0] =
74  FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[2][0])), chroma_den);
75  hdrmeta->bluePrimary[1] =
76  FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->display_primaries[2][1])), chroma_den);
77  hdrmeta->whitePoint[0] =
78  FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->white_point[0])), chroma_den);
79  hdrmeta->whitePoint[1] =
80  FFMIN((amf_uint16)(chroma_den * av_q2d(display_meta->white_point[1])), chroma_den);
81  }
82 
84  if (sd_light) {
85  light_meta = (AVContentLightMetadata *)sd_light->data;
86  if (light_meta) {
87  hdrmeta->maxContentLightLevel = (amf_uint16)light_meta->MaxCLL;
88  hdrmeta->maxFrameAverageLightLevel = (amf_uint16)light_meta->MaxFALL;
89  }
90  }
91  return 0;
92  }
93  return 1;
94 }
95 
96 #if CONFIG_D3D11VA
97 #include <d3d11.h>
98 #endif
99 
100 #ifdef _WIN32
101 #include "compat/w32dlfcn.h"
102 #else
103 #include <dlfcn.h>
104 #endif
105 
106 #define FFMPEG_AMF_WRITER_ID L"ffmpeg_amf"
107 
108 
112 #if CONFIG_D3D11VA
114 #endif
115 #if CONFIG_DXVA2
117 #endif
128 };
129 
131 
133 {
134  AMFEncoderContext *ctx = avctx->priv_data;
135  const wchar_t *codec_id = NULL;
136  AMF_RESULT res;
137  enum AVPixelFormat pix_fmt;
138  AVHWDeviceContext *hw_device_ctx = (AVHWDeviceContext*)ctx->device_ctx_ref->data;
139  AVAMFDeviceContext *amf_device_ctx = (AVAMFDeviceContext *)hw_device_ctx->hwctx;
140  int alloc_size;
141  wchar_t name[512];
142 
143 
144  alloc_size = swprintf(name, amf_countof(name), L"%s%lld",PTS_PROP, next_encoder_index) + 1;
145  ctx->pts_property_name = av_memdup(name, alloc_size * sizeof(wchar_t));
146  if(!ctx->pts_property_name)
147  return AVERROR(ENOMEM);
148 
149  alloc_size = swprintf(name, amf_countof(name), L"%s%lld",AMF_AV_FRAME_REF, next_encoder_index) + 1;
150  ctx->av_frame_property_name = av_memdup(name, alloc_size * sizeof(wchar_t));
151  if(!ctx->av_frame_property_name)
152  return AVERROR(ENOMEM);
153 
155 
156  switch (avctx->codec->id) {
157  case AV_CODEC_ID_H264:
158  codec_id = AMFVideoEncoderVCE_AVC;
159  break;
160  case AV_CODEC_ID_HEVC:
161  codec_id = AMFVideoEncoder_HEVC;
162  break;
163  case AV_CODEC_ID_AV1 :
164  codec_id = AMFVideoEncoder_AV1;
165  break;
166  default:
167  break;
168  }
169  AMF_RETURN_IF_FALSE(ctx, codec_id != NULL, AVERROR(EINVAL), "Codec %d is not supported\n", avctx->codec->id);
170 
171  if (avctx->hw_frames_ctx)
172  pix_fmt = ((AVHWFramesContext*)avctx->hw_frames_ctx->data)->sw_format;
173  else
174  pix_fmt = avctx->pix_fmt;
175 
176  if (pix_fmt == AV_PIX_FMT_P010) {
177  AMF_RETURN_IF_FALSE(ctx, amf_device_ctx->version >= AMF_MAKE_FULL_VERSION(1, 4, 32, 0), AVERROR_UNKNOWN, "10-bit encoder is not supported by AMD GPU drivers versions lower than 23.30.\n");
178  }
179 
180  ctx->format = av_av_to_amf_format(pix_fmt);
181  AMF_RETURN_IF_FALSE(ctx, ctx->format != AMF_SURFACE_UNKNOWN, AVERROR(EINVAL),
182  "Format %s is not supported\n", av_get_pix_fmt_name(pix_fmt));
183 
184  res = amf_device_ctx->factory->pVtbl->CreateComponent(amf_device_ctx->factory, amf_device_ctx->context, codec_id, &ctx->encoder);
185  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_ENCODER_NOT_FOUND, "CreateComponent(%ls) failed with error %d\n", codec_id, res);
186 
187  ctx->submitted_frame = 0;
188  ctx->encoded_frame = 0;
189  ctx->eof = 0;
190 
191  return 0;
192 }
193 
195 {
196  AMFEncoderContext *ctx = avctx->priv_data;
197 
198  if (ctx->encoder) {
199  ctx->encoder->pVtbl->Terminate(ctx->encoder);
200  ctx->encoder->pVtbl->Release(ctx->encoder);
201  ctx->encoder = NULL;
202  }
203 
204  av_buffer_unref(&ctx->device_ctx_ref);
205  av_fifo_freep2(&ctx->timestamp_list);
206 
207  av_freep(&ctx->pts_property_name);
208  av_freep(&ctx->av_frame_property_name);
209 
210  return 0;
211 }
212 
213 static int amf_copy_surface(AVCodecContext *avctx, const AVFrame *frame,
214  AMFSurface* surface)
215 {
216  AMFPlane *plane;
217  uint8_t *dst_data[4] = {0};
218  int dst_linesize[4] = {0};
219  int planes;
220  int i;
221 
222  planes = (int)surface->pVtbl->GetPlanesCount(surface);
223  av_assert0(planes < FF_ARRAY_ELEMS(dst_data));
224 
225  for (i = 0; i < planes; i++) {
226  plane = surface->pVtbl->GetPlaneAt(surface, i);
227  dst_data[i] = plane->pVtbl->GetNative(plane);
228  dst_linesize[i] = plane->pVtbl->GetHPitch(plane);
229  }
230  av_image_copy2(dst_data, dst_linesize,
231  frame->data, frame->linesize, frame->format,
232  avctx->width, avctx->height);
233 
234  return 0;
235 }
236 
237 static int amf_copy_buffer(AVCodecContext *avctx, AVPacket *pkt, AMFBuffer *buffer)
238 {
239  AMFEncoderContext *ctx = avctx->priv_data;
240  int ret;
241  AMFVariantStruct var = {0};
242  int64_t timestamp = AV_NOPTS_VALUE;
243  int64_t size = buffer->pVtbl->GetSize(buffer);
244 
245  if ((ret = ff_get_encode_buffer(avctx, pkt, size, 0)) < 0) {
246  return ret;
247  }
248  memcpy(pkt->data, buffer->pVtbl->GetNative(buffer), size);
249 
250  switch (avctx->codec->id) {
251  case AV_CODEC_ID_H264:
252  buffer->pVtbl->GetProperty(buffer, AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE, &var);
253  if(var.int64Value == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR) {
255  }
256  break;
257  case AV_CODEC_ID_HEVC:
258  buffer->pVtbl->GetProperty(buffer, AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE, &var);
259  if (var.int64Value == AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE_IDR) {
261  }
262  break;
263  case AV_CODEC_ID_AV1:
264  buffer->pVtbl->GetProperty(buffer, AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE, &var);
265  if (var.int64Value == AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE_KEY) {
267  }
268  default:
269  break;
270  }
271 
272  buffer->pVtbl->GetProperty(buffer, ctx->pts_property_name, &var);
273 
274  pkt->pts = var.int64Value; // original pts
275 
276  AMF_RETURN_IF_FALSE(ctx, av_fifo_read(ctx->timestamp_list, &timestamp, 1) >= 0,
277  AVERROR_UNKNOWN, "timestamp_list is empty\n");
278 
279  // calc dts shift if max_b_frames > 0
280  if ((ctx->max_b_frames > 0 || ((ctx->pa_adaptive_mini_gop == 1) ? true : false)) && ctx->dts_delay == 0) {
281  int64_t timestamp_last = AV_NOPTS_VALUE;
282  size_t can_read = av_fifo_can_read(ctx->timestamp_list);
283 
284  AMF_RETURN_IF_FALSE(ctx, can_read > 0, AVERROR_UNKNOWN,
285  "timestamp_list is empty while max_b_frames = %d\n", avctx->max_b_frames);
286  av_fifo_peek(ctx->timestamp_list, &timestamp_last, 1, can_read - 1);
287  if (timestamp < 0 || timestamp_last < AV_NOPTS_VALUE) {
288  return AVERROR(ERANGE);
289  }
290  ctx->dts_delay = timestamp_last - timestamp;
291  }
292  pkt->dts = timestamp - ctx->dts_delay;
293  return 0;
294 }
295 
296 // amfenc API implementation
298 {
299  int ret;
300  AMFEncoderContext *ctx = avctx->priv_data;
301  AVHWDeviceContext *hwdev_ctx = NULL;
302 
303  // hardcoded to current HW queue size - will auto-realloc if too small
304  ctx->timestamp_list = av_fifo_alloc2(avctx->max_b_frames + 16, sizeof(int64_t),
306  if (!ctx->timestamp_list) {
307  return AVERROR(ENOMEM);
308  }
309  ctx->dts_delay = 0;
310 
311  ctx->hwsurfaces_in_queue = 0;
312 
313  if (avctx->hw_device_ctx) {
314  hwdev_ctx = (AVHWDeviceContext*)avctx->hw_device_ctx->data;
315  if (hwdev_ctx->type == AV_HWDEVICE_TYPE_AMF)
316  {
317  ctx->device_ctx_ref = av_buffer_ref(avctx->hw_device_ctx);
318  }
319  else {
321  AMF_RETURN_IF_FALSE(ctx, ret == 0, ret, "Failed to create derived AMF device context: %s\n", av_err2str(ret));
322  }
323  } else if (avctx->hw_frames_ctx) {
324  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
325  if (frames_ctx->device_ref ) {
326  if (frames_ctx->format == AV_PIX_FMT_AMF_SURFACE) {
327  ctx->device_ctx_ref = av_buffer_ref(frames_ctx->device_ref);
328  }
329  else {
330  ret = av_hwdevice_ctx_create_derived(&ctx->device_ctx_ref, AV_HWDEVICE_TYPE_AMF, frames_ctx->device_ref, 0);
331  AMF_RETURN_IF_FALSE(ctx, ret == 0, ret, "Failed to create derived AMF device context: %s\n", av_err2str(ret));
332  }
333  }
334  }
335  else {
336  ret = av_hwdevice_ctx_create(&ctx->device_ctx_ref, AV_HWDEVICE_TYPE_AMF, NULL, NULL, 0);
337  AMF_RETURN_IF_FALSE(ctx, ret == 0, ret, "Failed to create hardware device context (AMF) : %s\n", av_err2str(ret));
338  }
339 
340  if ((ret = amf_init_encoder(avctx)) == 0) {
341  return 0;
342  }
343 
344  ff_amf_encode_close(avctx);
345  return ret;
346 }
347 
348 static AMF_RESULT amf_set_property_buffer(AMFSurface *object, const wchar_t *name, AMFBuffer *val)
349 {
350  AMF_RESULT res;
351  AMFVariantStruct var;
352  res = AMFVariantInit(&var);
353  if (res == AMF_OK) {
354  AMFGuid guid_AMFInterface = IID_AMFInterface();
355  AMFInterface *amf_interface;
356  res = val->pVtbl->QueryInterface(val, &guid_AMFInterface, (void**)&amf_interface);
357 
358  if (res == AMF_OK) {
359  res = AMFVariantAssignInterface(&var, amf_interface);
360  amf_interface->pVtbl->Release(amf_interface);
361  }
362  if (res == AMF_OK) {
363  res = object->pVtbl->SetProperty(object, name, var);
364  }
365  AMFVariantClear(&var);
366  }
367  return res;
368 }
369 
370 static AMF_RESULT amf_lock_context(AVCodecContext *avctx)
371 {
372  AMFEncoderContext *ctx = avctx->priv_data;
373  AVHWDeviceContext *hw_device_ctx = (AVHWDeviceContext*)ctx->device_ctx_ref->data;
374  AVAMFDeviceContext *amf_device_ctx = (AVAMFDeviceContext *)hw_device_ctx->hwctx;
375  AMF_RESULT res;
376 
377  switch(amf_device_ctx->memory_type) {
378  case AMF_MEMORY_DX11:
379  res = amf_device_ctx->context->pVtbl->LockDX11(amf_device_ctx->context);
380  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockDX11() failed with error %d\n", res);
381  break;
382  case AMF_MEMORY_DX12:
383  {
384  AMFContext2 *context2 = NULL;
385  AMFGuid guid = IID_AMFContext2();
386  res = amf_device_ctx->context->pVtbl->QueryInterface(amf_device_ctx->context, &guid, (void**)&context2);
387  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "QueryInterface for AMFContext2 failed with error %d\n", res);
388  res = context2->pVtbl->LockDX12(context2);
389  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockDX12() failed with error %d\n", res);
390  context2->pVtbl->Release(context2);
391  }
392  break;
393  case AMF_MEMORY_DX9:
394  res = amf_device_ctx->context->pVtbl->LockDX9(amf_device_ctx->context);
395  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockDX9() failed with error %d\n", res);
396 
397  case AMF_MEMORY_VULKAN:
398  {
399  AMFContext2 *context2 = NULL;
400  AMFGuid guid = IID_AMFContext2();
401  res = amf_device_ctx->context->pVtbl->QueryInterface(amf_device_ctx->context, &guid, (void**)&context2);
402  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "QueryInterface for AMFContext2 failed with error %d\n", res);
403  res = context2->pVtbl->LockVulkan(context2);
404  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockVulkan() failed with error %d\n", res);
405  context2->pVtbl->Release(context2);
406  }
407  break;
408  }
409  return AMF_OK;
410 }
411 static AMF_RESULT amf_unlock_context(AVCodecContext *avctx)
412 {
413  AMFEncoderContext *ctx = avctx->priv_data;
414  AVHWDeviceContext *hw_device_ctx = (AVHWDeviceContext*)ctx->device_ctx_ref->data;
415  AVAMFDeviceContext *amf_device_ctx = (AVAMFDeviceContext *)hw_device_ctx->hwctx;
416  AMF_RESULT res;
417 
418  switch(amf_device_ctx->memory_type) {
419  case AMF_MEMORY_DX11:
420  res = amf_device_ctx->context->pVtbl->UnlockDX11(amf_device_ctx->context);
421  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockDX11() failed with error %d\n", res);
422  break;
423  case AMF_MEMORY_DX12:
424  {
425  AMFContext2 *context2 = NULL;
426  AMFGuid guid = IID_AMFContext2();
427  res = amf_device_ctx->context->pVtbl->QueryInterface(amf_device_ctx->context, &guid, (void**)&context2);
428  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "QueryInterface for AMFContext2 failed with error %d\n", res);
429  res = context2->pVtbl->UnlockDX12(context2);
430  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockDX12() failed with error %d\n", res);
431  context2->pVtbl->Release(context2);
432  }
433  break;
434  case AMF_MEMORY_DX9:
435  res = amf_device_ctx->context->pVtbl->UnlockDX9(amf_device_ctx->context);
436  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockDX9() failed with error %d\n", res);
437 
438  case AMF_MEMORY_VULKAN:
439  {
440  AMFContext2 *context2 = NULL;
441  AMFGuid guid = IID_AMFContext2();
442  res = amf_device_ctx->context->pVtbl->QueryInterface(amf_device_ctx->context, &guid, (void**)&context2);
443  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "QueryInterface for AMFContext2 failed with error %d\n", res);
444  res = context2->pVtbl->UnlockVulkan(context2);
445  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockVulkan() failed with error %d\n", res);
446  context2->pVtbl->Release(context2);
447  }
448  break;
449  }
450  return AMF_OK;
451 }
452 
453 static AMF_RESULT amf_store_attached_frame_ref(AMFEncoderContext *ctx, const AVFrame *frame, AMFSurface *surface)
454 {
455  AMF_RESULT res = AMF_FAIL;
456  int64_t data;
458  if (frame_ref) {
459  memcpy(&data, &frame_ref, sizeof(frame_ref)); // store pointer in 8 bytes
460  AMF_ASSIGN_PROPERTY_INT64(res, surface, ctx->av_frame_property_name, data);
461  }
462  return res;
463 }
464 
466 {
467  AMFVariantStruct var = {0};
468  AMF_RESULT res = buffer->pVtbl->GetProperty(buffer, ctx->av_frame_property_name, &var);
469  if(res == AMF_OK && var.int64Value){
471  memcpy(&frame_ref, &var.int64Value, sizeof(frame_ref));
473  }
474  return res;
475 }
476 
477 static int amf_submit_frame(AVCodecContext *avctx, AVFrame *frame, AMFSurface **surface_resubmit)
478 {
479  AMFEncoderContext *ctx = avctx->priv_data;
480  AVHWDeviceContext *hw_device_ctx = (AVHWDeviceContext*)ctx->device_ctx_ref->data;
481  AVAMFDeviceContext *amf_device_ctx = (AVAMFDeviceContext *)hw_device_ctx->hwctx;
482  AMFSurface *surface;
483  AMF_RESULT res;
484  int ret;
485  int hw_surface = 0;
486  int max_b_frames = ctx->max_b_frames < 0 ? 0 : ctx->max_b_frames;
487 
488 // prepare surface from frame
489  switch (frame->format) {
490 #if CONFIG_D3D11VA
491  case AV_PIX_FMT_D3D11:
492  {
493  static const GUID AMFTextureArrayIndexGUID = { 0x28115527, 0xe7c3, 0x4b66, { 0x99, 0xd3, 0x4f, 0x2a, 0xe6, 0xb4, 0x7f, 0xaf } };
494  ID3D11Texture2D *texture = (ID3D11Texture2D*)frame->data[0]; // actual texture
495  int index = (intptr_t)frame->data[1]; // index is a slice in texture array is - set to tell AMF which slice to use
496  av_assert0(frame->hw_frames_ctx && avctx->hw_frames_ctx &&
497  frame->hw_frames_ctx->data == avctx->hw_frames_ctx->data);
498  texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID, sizeof(index), &index);
499  res = amf_device_ctx->context->pVtbl->CreateSurfaceFromDX11Native(amf_device_ctx->context, texture, &surface, NULL); // wrap to AMF surface
500  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "CreateSurfaceFromDX11Native() failed with error %d\n", res);
501  hw_surface = 1;
502  }
503  break;
504 #endif
505 #if CONFIG_DXVA2
507  {
508  IDirect3DSurface9 *texture = (IDirect3DSurface9 *)frame->data[3]; // actual texture
509  res = amf_device_ctx->context->pVtbl->CreateSurfaceFromDX9Native(amf_device_ctx->context, texture, &surface, NULL); // wrap to AMF surface
510  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "CreateSurfaceFromDX9Native() failed with error %d\n", res);
511  hw_surface = 1;
512  }
513  break;
514 #endif
516  {
517  surface = (AMFSurface*)frame->data[0];
518  surface->pVtbl->Acquire(surface);
519  hw_surface = 1;
520  }
521  break;
522  default:
523  {
524  res = amf_device_ctx->context->pVtbl->AllocSurface(amf_device_ctx->context, AMF_MEMORY_HOST, ctx->format, avctx->width, avctx->height, &surface);
525  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "AllocSurface() failed with error %d\n", res);
526  amf_copy_surface(avctx, frame, surface);
527  }
528  break;
529  }
530  if (hw_surface) {
532  ctx->hwsurfaces_in_queue++;
533  // input HW surfaces can be vertically aligned by 16; tell AMF the real size
534  surface->pVtbl->SetCrop(surface, 0, 0, frame->width, frame->height);
535  }
536  // HDR10 metadata
537  if (frame->color_trc == AVCOL_TRC_SMPTE2084) {
538  AMFBuffer * hdrmeta_buffer = NULL;
539  res = amf_device_ctx->context->pVtbl->AllocBuffer(amf_device_ctx->context, AMF_MEMORY_HOST, sizeof(AMFHDRMetadata), &hdrmeta_buffer);
540  if (res == AMF_OK) {
541  AMFHDRMetadata * hdrmeta = (AMFHDRMetadata*)hdrmeta_buffer->pVtbl->GetNative(hdrmeta_buffer);
542  if (amf_save_hdr_metadata(avctx, frame, hdrmeta) == 0) {
543  switch (avctx->codec->id) {
544  case AV_CODEC_ID_H264:
545  AMF_ASSIGN_PROPERTY_INTERFACE(res, ctx->encoder, AMF_VIDEO_ENCODER_INPUT_HDR_METADATA, hdrmeta_buffer); break;
546  case AV_CODEC_ID_HEVC:
547  AMF_ASSIGN_PROPERTY_INTERFACE(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_INPUT_HDR_METADATA, hdrmeta_buffer); break;
548  case AV_CODEC_ID_AV1:
549  AMF_ASSIGN_PROPERTY_INTERFACE(res, ctx->encoder, AMF_VIDEO_ENCODER_AV1_INPUT_HDR_METADATA, hdrmeta_buffer); break;
550  }
551  res = amf_set_property_buffer(surface, L"av_frame_hdrmeta", hdrmeta_buffer);
552  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "SetProperty failed for \"av_frame_hdrmeta\" with error %d\n", res);
553  }
554  hdrmeta_buffer->pVtbl->Release(hdrmeta_buffer);
555  }
556  }
557  surface->pVtbl->SetPts(surface, frame->pts);
558 
559  AMF_ASSIGN_PROPERTY_INT64(res, surface, ctx->pts_property_name, frame->pts);
560 
561  switch (avctx->codec->id) {
562  case AV_CODEC_ID_H264:
563  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_AUD, !!ctx->aud);
564  switch (frame->pict_type) {
565  case AV_PICTURE_TYPE_I:
566  if (ctx->forced_idr) {
567  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_SPS, 1);
568  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_INSERT_PPS, 1);
569  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_IDR);
570  } else {
571  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_I);
572  }
573  break;
574  case AV_PICTURE_TYPE_P:
575  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_P);
576  break;
577  case AV_PICTURE_TYPE_B:
578  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_B);
579  break;
580  }
581  break;
582  case AV_CODEC_ID_HEVC:
583  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_AUD, !!ctx->aud);
584  switch (frame->pict_type) {
585  case AV_PICTURE_TYPE_I:
586  if (ctx->forced_idr) {
587  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_INSERT_HEADER, 1);
588  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_IDR);
589  } else {
590  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_I);
591  }
592  break;
593  case AV_PICTURE_TYPE_P:
594  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_P);
595  break;
596  }
597  break;
598  case AV_CODEC_ID_AV1:
599  if (frame->pict_type == AV_PICTURE_TYPE_I) {
600  if (ctx->forced_idr) {
601  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_INSERT_SEQUENCE_HEADER, 1);
602  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE_KEY);
603  } else {
604  AMF_ASSIGN_PROPERTY_INT64(res, surface, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE, AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE_INTRA_ONLY);
605  }
606  }
607  break;
608  default:
609  break;
610  }
611  // submit surface
612  res = ctx->encoder->pVtbl->SubmitInput(ctx->encoder, (AMFData*)surface);
613  if (res == AMF_INPUT_FULL) { // handle full queue
614  //store surface for later submission
615  *surface_resubmit = surface;
616  } else {
617  surface->pVtbl->Release(surface);
618  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "SubmitInput() failed with error %d\n", res);
619  ctx->submitted_frame++;
620  ret = av_fifo_write(ctx->timestamp_list, &frame->pts, 1);
621  if (ret < 0)
622  return ret;
623  if(ctx->submitted_frame <= ctx->encoded_frame + max_b_frames + 1)
624  return AVERROR(EAGAIN); // if frame just submiited - don't poll or wait
625  }
626  return 0;
627 }
628 
629 static int amf_submit_frame_locked(AVCodecContext *avctx, AVFrame *frame, AMFSurface **surface_resubmit)
630 {
631  int ret;
632  int locked = amf_lock_context(avctx);
633  if(locked != AMF_OK)
634  av_log(avctx, AV_LOG_WARNING, "amf_lock_context() failed with %d - should not happen\n", locked);
635 
636  ret = amf_submit_frame(avctx, frame, surface_resubmit);
637 
638  if(locked == AMF_OK)
639  amf_unlock_context(avctx);
640  return ret;
641 }
642 
644 {
645  AMFEncoderContext *ctx = avctx->priv_data;
646  AVHWDeviceContext *hw_device_ctx = (AVHWDeviceContext*)ctx->device_ctx_ref->data;
647  AVAMFDeviceContext *amf_device_ctx = (AVAMFDeviceContext *)hw_device_ctx->hwctx;
648  AMFSurface *surface = NULL;
649  AMF_RESULT res;
650  int ret;
651  AMF_RESULT res_query;
652  AMFData *data = NULL;
654  int block_and_wait;
655  int64_t pts = 0;
656  int max_b_frames = ctx->max_b_frames < 0 ? 0 : ctx->max_b_frames;
657 
658  if (!ctx->encoder){
660  return AVERROR(EINVAL);
661  }
662  ret = ff_encode_get_frame(avctx, frame);
663  if(ret < 0){
664  if(ret != AVERROR_EOF){
666  if(ret == AVERROR(EAGAIN)){
667  if(ctx->submitted_frame <= ctx->encoded_frame + max_b_frames + 1) // too soon to poll
668  return ret;
669  }
670  }
671  }
672  if(ret != AVERROR(EAGAIN)){
673  if (!frame->buf[0]) { // submit drain
674  if (!ctx->eof) { // submit drain one time only
675  if(!ctx->delayed_drain) {
676  res = ctx->encoder->pVtbl->Drain(ctx->encoder);
677  if (res == AMF_INPUT_FULL) {
678  ctx->delayed_drain = 1; // input queue is full: resubmit Drain() in receive loop
679  } else {
680  if (res == AMF_OK) {
681  ctx->eof = 1; // drain started
682  }
683  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "Drain() failed with error %d\n", res);
684  }
685  }
686  }
687  } else { // submit frame
688  ret = amf_submit_frame_locked(avctx, frame, &surface);
689  if(ret < 0){
691  return ret;
692  }
693  pts = frame->pts;
694  }
695  }
697 
698  do {
699  block_and_wait = 0;
700  // poll data
701 
702  res_query = ctx->encoder->pVtbl->QueryOutput(ctx->encoder, &data);
703  if (data) {
704  // copy data to packet
705  AMFBuffer *buffer;
706  AMFGuid guid = IID_AMFBuffer();
707  data->pVtbl->QueryInterface(data, &guid, (void**)&buffer); // query for buffer interface
708  ret = amf_copy_buffer(avctx, avpkt, buffer);
709  if (amf_release_attached_frame_ref(ctx, buffer) == AMF_OK) {
710  ctx->hwsurfaces_in_queue--;
711  }
712  ctx->encoded_frame++;
713  buffer->pVtbl->Release(buffer);
714  data->pVtbl->Release(data);
715 
716  AMF_RETURN_IF_FALSE(ctx, ret >= 0, ret, "amf_copy_buffer() failed with error %d\n", ret);
717 
718  if (ctx->delayed_drain) { // try to resubmit drain
719  res = ctx->encoder->pVtbl->Drain(ctx->encoder);
720  if (res != AMF_INPUT_FULL) {
721  ctx->delayed_drain = 0;
722  ctx->eof = 1; // drain started
723  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "Repeated Drain() failed with error %d\n", res);
724  } else {
725  av_log(avctx, AV_LOG_WARNING, "Data acquired but delayed drain submission got AMF_INPUT_FULL- should not happen\n");
726  }
727  }
728  } else if (ctx->delayed_drain || (ctx->eof && res_query != AMF_EOF) || (ctx->hwsurfaces_in_queue >= ctx->hwsurfaces_in_queue_max) || surface) {
729  block_and_wait = 1;
730  // Only sleep if the driver doesn't support waiting in QueryOutput()
731  // or if we already have output data so we will skip calling it.
732  if (!ctx->query_timeout_supported || avpkt->data || avpkt->buf) {
733  av_usleep(1000);
734  }
735  }
736  } while (block_and_wait);
737 
738  if (res_query == AMF_EOF) {
739  ret = AVERROR_EOF;
740  } else if (data == NULL) {
741  ret = AVERROR(EAGAIN);
742  } else {
743  if(surface) {
744  // resubmit surface
745  res = ctx->encoder->pVtbl->SubmitInput(ctx->encoder, (AMFData*)surface);
746  surface->pVtbl->Release(surface);
747  if (res == AMF_INPUT_FULL) {
748  av_log(avctx, AV_LOG_WARNING, "Data acquired but delayed SubmitInput returned AMF_INPUT_FULL- should not happen\n");
749  } else {
750  AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "SubmitInput() failed with error %d\n", res);
751 
752  ret = av_fifo_write(ctx->timestamp_list, &pts, 1);
753 
754  ctx->submitted_frame++;
755 
756  if (ret < 0)
757  return ret;
758  }
759  }
760  ret = 0;
761  }
762  return ret;
763 }
764 
766 {
767  amf_int64 color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN;
768  if (avctx->color_range == AVCOL_RANGE_JPEG) {
769  /// Color Space for Full (JPEG) Range
770  switch (avctx->colorspace) {
771  case AVCOL_SPC_SMPTE170M:
772  color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_601;
773  break;
774  case AVCOL_SPC_BT709:
775  color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_709;
776  break;
778  case AVCOL_SPC_BT2020_CL:
779  color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_2020;
780  break;
781  }
782  } else {
783  /// Color Space for Limited (MPEG) range
784  switch (avctx->colorspace) {
785  case AVCOL_SPC_SMPTE170M:
786  color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_601;
787  break;
788  case AVCOL_SPC_BT709:
789  color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_709;
790  break;
792  case AVCOL_SPC_BT2020_CL:
793  color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_2020;
794  break;
795  }
796  }
797  return color_profile;
798 }
799 
801 #if CONFIG_D3D11VA
802  HW_CONFIG_ENCODER_FRAMES(D3D11, D3D11VA),
803  HW_CONFIG_ENCODER_DEVICE(NONE, D3D11VA),
804 #endif
805 #if CONFIG_DXVA2
806  HW_CONFIG_ENCODER_FRAMES(DXVA2_VLD, DXVA2),
808 #endif
809  HW_CONFIG_ENCODER_FRAMES(AMF_SURFACE, AMF),
811  NULL,
812 };
AVMasteringDisplayMetadata::has_primaries
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
Definition: mastering_display_metadata.h:62
amf_release_attached_frame_ref
static AMF_RESULT amf_release_attached_frame_ref(AMFEncoderContext *ctx, AMFBuffer *buffer)
Definition: amfenc.c:465
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AVMasteringDisplayMetadata::max_luminance
AVRational max_luminance
Max luminance of mastering display (cd/m^2).
Definition: mastering_display_metadata.h:57
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:659
av_frame_get_side_data
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:657
NONE
@ NONE
Definition: af_afade.c:60
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
AVMasteringDisplayMetadata::display_primaries
AVRational display_primaries[3][2]
CIE 1931 xy chromaticity coords of color primaries (r, g, b order).
Definition: mastering_display_metadata.h:42
AVHWFramesContext::format
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:198
AVMasteringDisplayMetadata::has_luminance
int has_luminance
Flag indicating whether the luminance (min_ and max_) have been set.
Definition: mastering_display_metadata.h:67
int64_t
long long int64_t
Definition: coverity.c:34
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:63
AVContentLightMetadata::MaxCLL
unsigned MaxCLL
Max content light level (cd/m^2).
Definition: mastering_display_metadata.h:111
av_fifo_peek
int av_fifo_peek(const AVFifo *f, void *buf, size_t nb_elems, size_t offset)
Read data from a FIFO without modifying FIFO state.
Definition: fifo.c:255
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:410
pixdesc.h
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:750
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:535
encode.h
data
const char data[16]
Definition: mxf.c:149
amf_set_property_buffer
static AMF_RESULT amf_set_property_buffer(AMFSurface *object, const wchar_t *name, AMFBuffer *val)
Definition: amfenc.c:348
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:102
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AMF_RETURN_IF_FALSE
#define AMF_RETURN_IF_FALSE(avctx, exp, ret_value,...)
Error handling helper.
Definition: amfenc.h:168
amf_copy_surface
static int amf_copy_surface(AVCodecContext *avctx, const AVFrame *frame, AMFSurface *surface)
Definition: amfenc.c:213
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
AVCOL_SPC_BT2020_CL
@ AVCOL_SPC_BT2020_CL
ITU-R BT2020 constant luminance system.
Definition: pixfmt.h:685
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:590
AV_PIX_FMT_AMF_SURFACE
@ AV_PIX_FMT_AMF_SURFACE
HW acceleration through AMF.
Definition: pixfmt.h:477
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:304
AVContentLightMetadata
Content light level needed by to transmit HDR over HDMI (CTA-861.3).
Definition: mastering_display_metadata.h:107
AMF_AV_FRAME_REF
#define AMF_AV_FRAME_REF
Definition: amfenc.c:43
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:440
av_fifo_write
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition: fifo.c:188
ff_amf_encode_close
int av_cold ff_amf_encode_close(AVCodecContext *avctx)
Common encoder termination function.
Definition: amfenc.c:194
ff_amf_encode_init
int ff_amf_encode_init(AVCodecContext *avctx)
Common encoder initization function.
Definition: amfenc.c:297
val
static double val(void *priv, double ch)
Definition: aeval.c:77
pts
static int64_t pts
Definition: transcode_aac.c:644
av_av_to_amf_format
enum AMF_SURFACE_FORMAT av_av_to_amf_format(enum AVPixelFormat fmt)
Definition: hwcontext_amf.c:121
AVHWDeviceContext
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:61
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:51
avassert.h
pkt
AVPacket * pkt
Definition: movenc.c:60
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
av_fifo_read
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition: fifo.c:240
AVMasteringDisplayMetadata::white_point
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
Definition: mastering_display_metadata.h:47
AV_PIX_FMT_DXVA2_VLD
@ AV_PIX_FMT_DXVA2_VLD
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer.
Definition: pixfmt.h:134
AMFEncoderContext
AMF encoder context.
Definition: amfenc.h:40
AVCOL_SPC_SMPTE170M
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
Definition: pixfmt.h:680
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demux_decode.c:41
amf_submit_frame
static int amf_submit_frame(AVCodecContext *avctx, AVFrame *frame, AMFSurface **surface_resubmit)
Definition: amfenc.c:477
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
AV_HWDEVICE_TYPE_AMF
@ AV_HWDEVICE_TYPE_AMF
Definition: hwcontext.h:41
amf_init_encoder
static int amf_init_encoder(AVCodecContext *avctx)
Definition: amfenc.c:132
ctx
AVFormatContext * ctx
Definition: movenc.c:49
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:481
hwcontext_amf.h
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:410
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
av_usleep
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
if
if(ret)
Definition: filter_design.txt:179
AVPacket::buf
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:518
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:669
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:284
AVHWFramesContext::device_ref
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition: hwcontext.h:127
ff_amf_receive_packet
int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Ecoding one frame - common function for all AMF encoders.
Definition: amfenc.c:643
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:278
av_fifo_can_read
size_t av_fifo_can_read(const AVFifo *f)
Definition: fifo.c:87
amf_copy_buffer
static int amf_copy_buffer(AVCodecContext *avctx, AVPacket *pkt, AMFBuffer *buffer)
Definition: amfenc.c:237
AV_FRAME_DATA_MASTERING_DISPLAY_METADATA
@ AV_FRAME_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata associated with a video frame.
Definition: frame.h:120
AV_PIX_FMT_BGR0
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:265
time.h
PTS_PROP
#define PTS_PROP
Definition: amfenc.c:44
amf_submit_frame_locked
static int amf_submit_frame_locked(AVCodecContext *avctx, AVFrame *frame, AMFSurface **surface_resubmit)
Definition: amfenc.c:629
index
int index
Definition: gxfenc.c:90
AVCOL_TRC_SMPTE2084
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
Definition: pixfmt.h:661
AV_PIX_FMT_X2BGR10
#define AV_PIX_FMT_X2BGR10
Definition: pixfmt.h:597
hwcontext_dxva2.h
HW_CONFIG_ENCODER_DEVICE
#define HW_CONFIG_ENCODER_DEVICE(format, device_type_)
Definition: hwconfig.h:95
amf_lock_context
static AMF_RESULT amf_lock_context(AVCodecContext *avctx)
Definition: amfenc.c:370
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:122
ff_amf_pix_fmts
enum AVPixelFormat ff_amf_pix_fmts[]
Supported formats.
Definition: amfenc.c:109
size
int size
Definition: twinvq_data.h:10344
AVAMFDeviceContext
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_amf.h:33
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:247
AVFrameSideData::data
uint8_t * data
Definition: frame.h:267
AVCodecHWConfigInternal
Definition: hwconfig.h:25
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:534
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:541
AV_PIX_FMT_RGB0
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:263
AV_PIX_FMT_D3D11
@ AV_PIX_FMT_D3D11
Hardware surfaces for Direct3D11.
Definition: pixfmt.h:336
AVCodec::id
enum AVCodecID id
Definition: codec.h:186
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:99
HW_CONFIG_ENCODER_FRAMES
#define HW_CONFIG_ENCODER_FRAMES(format, device_type_)
Definition: hwconfig.h:98
planes
static const struct @509 planes[]
amf_unlock_context
static AMF_RESULT amf_unlock_context(AVCodecContext *avctx)
Definition: amfenc.c:411
AV_FRAME_DATA_CONTENT_LIGHT_LEVEL
@ AV_FRAME_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
Definition: frame.h:137
av_hwdevice_ctx_create_derived
int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr, enum AVHWDeviceType type, AVBufferRef *src_ref, int flags)
Create a new device of the specified type from an existing device.
Definition: hwcontext.c:714
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:528
AVCOL_SPC_BT2020_NCL
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:684
amf_save_hdr_metadata
static int amf_save_hdr_metadata(AVCodecContext *avctx, const AVFrame *frame, AMFHDRMetadata *hdrmeta)
Definition: amfenc.c:46
hw_device_ctx
static AVBufferRef * hw_device_ctx
Definition: hw_decode.c:45
ff_amfenc_hw_configs
const AVCodecHWConfigInternal *const ff_amfenc_hw_configs[]
Definition: amfenc.c:800
amf_store_attached_frame_ref
static AMF_RESULT amf_store_attached_frame_ref(AMFEncoderContext *ctx, const AVFrame *frame, AMFSurface *surface)
Definition: amfenc.c:453
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:228
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodecContext::hw_device_ctx
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition: avcodec.h:1475
AVMasteringDisplayMetadata
Mastering display metadata capable of representing the color volume of the display used to master the...
Definition: mastering_display_metadata.h:38
AVCodecContext::height
int height
Definition: avcodec.h:592
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:631
AVCodecContext::hw_frames_ctx
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition: avcodec.h:1453
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:116
frame_ref
static int frame_ref(AVFrame *dst, const AVFrame *src)
Definition: swscale.c:1339
ret
ret
Definition: filter_design.txt:187
AVHWDeviceContext::type
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
Definition: hwcontext.h:73
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:96
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
av_hwdevice_ctx_create
int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)
Open a device of the specified type and create an AVHWDeviceContext for it.
Definition: hwcontext.c:611
av_fifo_alloc2
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
Definition: fifo.c:47
AVCodecContext
main external API structure.
Definition: avcodec.h:431
hwcontext_amf_internal.h
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:280
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:106
av_image_copy2
static void av_image_copy2(uint8_t *const dst_data[4], const int dst_linesizes[4], uint8_t *const src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Wrapper around av_image_copy() to workaround the limitation that the conversion from uint8_t * const ...
Definition: imgutils.h:184
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
L
#define L(x)
Definition: vpx_arith.h:36
amfenc.h
AVMasteringDisplayMetadata::min_luminance
AVRational min_luminance
Min luminance of mastering display (cd/m^2).
Definition: mastering_display_metadata.h:52
AV_PIX_FMT_P010
#define AV_PIX_FMT_P010
Definition: pixfmt.h:585
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:279
AVERROR_ENCODER_NOT_FOUND
#define AVERROR_ENCODER_NOT_FOUND
Encoder not found.
Definition: error.h:56
mem.h
AVCodecContext::max_b_frames
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
Definition: avcodec.h:769
ff_encode_get_frame
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
Definition: encode.c:205
mastering_display_metadata.h
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:265
ff_amf_get_color_profile
int ff_amf_get_color_profile(AVCodecContext *avctx)
Definition: amfenc.c:765
AVPacket
This structure stores compressed data.
Definition: packet.h:512
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
next_encoder_index
static int64_t next_encoder_index
Definition: amfenc.c:130
AVContentLightMetadata::MaxFALL
unsigned MaxFALL
Max average light level per frame (cd/m^2).
Definition: mastering_display_metadata.h:116
AV_PIX_FMT_RGBAF16
#define AV_PIX_FMT_RGBAF16
Definition: pixfmt.h:607
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:592
imgutils.h
hwcontext.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
av_fifo_freep2
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
Definition: fifo.c:286
AVCOL_SPC_BT709
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition: pixfmt.h:675
hwcontext_d3d11va.h
AV_FIFO_FLAG_AUTO_GROW
#define AV_FIFO_FLAG_AUTO_GROW
Automatically resize the FIFO on writes, so that the data fits.
Definition: fifo.h:63
w32dlfcn.h
av_get_pix_fmt_name
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:3261