qsvencoder: Move common property to baseclass
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / qsv / gstqsvencoder.cpp
1 /* GStreamer
2  * Copyright (C) 2021 Seungha Yang <seungha@centricular.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "gstqsvencoder.h"
25 #include <mfxvideo++.h>
26 #include <string.h>
27 #include <string>
28
29 #ifdef G_OS_WIN32
30 #include "gstqsvallocator_d3d11.h"
31
32 #include <wrl.h>
33
34 /* *INDENT-OFF* */
35 using namespace Microsoft::WRL;
36 /* *INDENT-ON* */
37 #else
38 #include "gstqsvallocator_va.h"
39 #endif /* G_OS_WIN32 */
40
41 GST_DEBUG_CATEGORY_STATIC (gst_qsv_encoder_debug);
42 #define GST_CAT_DEFAULT gst_qsv_encoder_debug
43
44 GType
45 gst_qsv_coding_option_get_type (void)
46 {
47   static GType coding_opt_type = 0;
48   static const GEnumValue coding_opts[] = {
49     {MFX_CODINGOPTION_UNKNOWN, "Unknown", "unknown"},
50     {MFX_CODINGOPTION_ON, "On", "on"},
51     {MFX_CODINGOPTION_OFF, "Off", "off"},
52     {0, nullptr, nullptr}
53   };
54
55   if (g_once_init_enter (&coding_opt_type)) {
56     GType type = g_enum_register_static ("GstQsvCodingOption",
57         coding_opts);
58     g_once_init_leave (&coding_opt_type, type);
59   }
60
61   return coding_opt_type;
62 }
63
64 enum
65 {
66   PROP_0,
67   PROP_ADAPTER_LUID,
68   PROP_DEVICE_PATH,
69   PROP_TARGET_USAGE,
70   PROP_LOW_LATENCY,
71 };
72
73 #define DEFAULT_TARGET_USAGE MFX_TARGETUSAGE_BALANCED
74 #define DEFAULT_LOW_LATENCY  FALSE
75
76 typedef struct _GstQsvEncoderSurface
77 {
78   mfxFrameSurface1 surface;
79   mfxEncodeCtrl encode_control;
80
81   /* array of mfxPayload (e.g., SEI data) associated with this surface */
82   GPtrArray *payload;
83
84   /* holds ownership */
85   GstQsvFrame *qsv_frame;
86 } GstQsvEncoderSurface;
87
88 typedef struct _GstQsvEncoderTask
89 {
90   mfxSyncPoint sync_point;
91   mfxBitstream bitstream;
92 } GstQsvEncoderTask;
93
94 struct _GstQsvEncoderPrivate
95 {
96   GstObject *device;
97
98   GstVideoCodecState *input_state;
99   GstQsvAllocator *allocator;
100
101   /* API specific alignment requirement (multiple of 16 or 32) */
102   GstVideoInfo aligned_info;
103
104   mfxSession session;
105   mfxVideoParam video_param;
106
107   /* List of mfxExtBuffer configured by subclass, subclass will hold
108    * allocated memory for each mfxExtBuffer */
109   GPtrArray *extra_params;
110
111   MFXVideoENCODE *encoder;
112   GstQsvMemoryType mem_type;
113
114   /* Internal buffer pool used to allocate fallback buffer when input buffer
115    * is not compatible with expected format/type/resolution etc */
116   GstBufferPool *internal_pool;
117
118   /* Array of GstQsvEncoderSurface, holding ownership */
119   GArray *surface_pool;
120   guint next_surface_index;
121
122   /* Array of GstQsvEncoderTask, holding ownership */
123   GArray *task_pool;
124
125   GQueue free_tasks;
126   GQueue pending_tasks;
127
128   /* Properties */
129   guint target_usage;
130   gboolean low_latency;
131 };
132
133 #define gst_qsv_encoder_parent_class parent_class
134 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstQsvEncoder, gst_qsv_encoder,
135     GST_TYPE_VIDEO_ENCODER, G_ADD_PRIVATE (GstQsvEncoder);
136     GST_DEBUG_CATEGORY_INIT (gst_qsv_encoder_debug,
137         "qsvencoder", 0, "qsvencoder"));
138
139 static void gst_qsv_encoder_dispose (GObject * object);
140 static void gst_qsv_encoder_finalize (GObject * object);
141 static void gst_qsv_encoder_set_property (GObject * object, guint prop_id,
142     const GValue * value, GParamSpec * pspec);
143 static void gst_qsv_encoder_get_property (GObject * object, guint prop_id,
144     GValue * value, GParamSpec * pspec);
145
146 static void gst_qsv_encoder_set_context (GstElement * element,
147     GstContext * context);
148
149 static gboolean gst_qsv_encoder_open (GstVideoEncoder * encoder);
150 static gboolean gst_qsv_encoder_stop (GstVideoEncoder * encoder);
151 static gboolean gst_qsv_encoder_close (GstVideoEncoder * encoder);
152 static gboolean gst_qsv_encoder_set_format (GstVideoEncoder * encoder,
153     GstVideoCodecState * state);
154 static GstFlowReturn gst_qsv_encoder_handle_frame (GstVideoEncoder * encoder,
155     GstVideoCodecFrame * frame);
156 static GstFlowReturn gst_qsv_encoder_finish (GstVideoEncoder * encoder);
157 static gboolean gst_qsv_encoder_flush (GstVideoEncoder * encoder);
158 static gboolean gst_qsv_encoder_sink_query (GstVideoEncoder * encoder,
159     GstQuery * query);
160 static gboolean gst_qsv_encoder_src_query (GstVideoEncoder * encoder,
161     GstQuery * query);
162 static gboolean gst_qsv_encoder_propose_allocation (GstVideoEncoder * encoder,
163     GstQuery * query);
164
165 static void gst_qsv_encoder_surface_clear (GstQsvEncoderSurface * task);
166 static void gst_qsv_encoder_task_clear (GstQsvEncoderTask * task);
167
168 static void
169 gst_qsv_encoder_class_init (GstQsvEncoderClass * klass)
170 {
171   GObjectClass *object_class = G_OBJECT_CLASS (klass);
172   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
173   GstVideoEncoderClass *videoenc_class = GST_VIDEO_ENCODER_CLASS (klass);
174
175   object_class->dispose = gst_qsv_encoder_dispose;
176   object_class->finalize = gst_qsv_encoder_finalize;
177   object_class->set_property = gst_qsv_encoder_set_property;
178   object_class->get_property = gst_qsv_encoder_get_property;
179
180 #ifdef G_OS_WIN32
181   g_object_class_install_property (object_class, PROP_ADAPTER_LUID,
182       g_param_spec_int64 ("adapter-luid", "Adapter LUID",
183           "DXGI Adapter LUID (Locally Unique Identifier) of created device",
184           G_MININT64, G_MAXINT64, 0,
185           (GParamFlags) (GST_PARAM_CONDITIONALLY_AVAILABLE | G_PARAM_READABLE |
186               G_PARAM_STATIC_STRINGS)));
187 #else
188   g_object_class_install_property (object_class, PROP_DEVICE_PATH,
189       g_param_spec_string ("device-path", "Device Path",
190           "DRM device path", NULL,
191           (GParamFlags) (GST_PARAM_CONDITIONALLY_AVAILABLE |
192               G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
193 #endif
194
195   g_object_class_install_property (object_class, PROP_TARGET_USAGE,
196       g_param_spec_uint ("target-usage", "Target Usage",
197           "1: Best quality, 4: Balanced, 7: Best speed",
198           1, 7, DEFAULT_TARGET_USAGE,
199           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
200
201   g_object_class_install_property (object_class, PROP_LOW_LATENCY,
202       g_param_spec_boolean ("low-latency", "Low Latency",
203           "Enables low-latency encoding", DEFAULT_LOW_LATENCY,
204           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
205
206   element_class->set_context = GST_DEBUG_FUNCPTR (gst_qsv_encoder_set_context);
207
208   videoenc_class->open = GST_DEBUG_FUNCPTR (gst_qsv_encoder_open);
209   videoenc_class->stop = GST_DEBUG_FUNCPTR (gst_qsv_encoder_stop);
210   videoenc_class->close = GST_DEBUG_FUNCPTR (gst_qsv_encoder_close);
211   videoenc_class->set_format = GST_DEBUG_FUNCPTR (gst_qsv_encoder_set_format);
212   videoenc_class->handle_frame =
213       GST_DEBUG_FUNCPTR (gst_qsv_encoder_handle_frame);
214   videoenc_class->finish = GST_DEBUG_FUNCPTR (gst_qsv_encoder_finish);
215   videoenc_class->flush = GST_DEBUG_FUNCPTR (gst_qsv_encoder_flush);
216   videoenc_class->sink_query = GST_DEBUG_FUNCPTR (gst_qsv_encoder_sink_query);
217   videoenc_class->src_query = GST_DEBUG_FUNCPTR (gst_qsv_encoder_src_query);
218   videoenc_class->propose_allocation =
219       GST_DEBUG_FUNCPTR (gst_qsv_encoder_propose_allocation);
220 }
221
222 static void
223 gst_qsv_encoder_init (GstQsvEncoder * self)
224 {
225   GstQsvEncoderPrivate *priv;
226
227   priv = self->priv =
228       (GstQsvEncoderPrivate *) gst_qsv_encoder_get_instance_private (self);
229
230   priv->extra_params = g_ptr_array_sized_new (8);
231
232   priv->surface_pool = g_array_new (FALSE, TRUE, sizeof (GstQsvEncoderSurface));
233   g_array_set_clear_func (priv->surface_pool,
234       (GDestroyNotify) gst_qsv_encoder_surface_clear);
235
236   priv->task_pool = g_array_new (FALSE, TRUE, sizeof (GstQsvEncoderTask));
237   g_array_set_clear_func (priv->task_pool,
238       (GDestroyNotify) gst_qsv_encoder_task_clear);
239
240   g_queue_init (&priv->free_tasks);
241   g_queue_init (&priv->pending_tasks);
242
243   priv->target_usage = DEFAULT_TARGET_USAGE;
244   priv->low_latency = DEFAULT_LOW_LATENCY;
245 }
246
247 static void
248 gst_qsv_encoder_dispose (GObject * object)
249 {
250   GstQsvEncoder *self = GST_QSV_ENCODER (object);
251   GstQsvEncoderPrivate *priv = self->priv;
252
253   gst_clear_object (&priv->device);
254
255   G_OBJECT_CLASS (parent_class)->dispose (object);
256 }
257
258 static void
259 gst_qsv_encoder_finalize (GObject * object)
260 {
261   GstQsvEncoder *self = GST_QSV_ENCODER (object);
262   GstQsvEncoderPrivate *priv = self->priv;
263
264   g_ptr_array_unref (priv->extra_params);
265   g_array_unref (priv->task_pool);
266   g_array_unref (priv->surface_pool);
267
268   G_OBJECT_CLASS (parent_class)->finalize (object);
269 }
270
271 static void
272 gst_qsv_encoder_set_property (GObject * object, guint prop_id,
273     const GValue * value, GParamSpec * pspec)
274 {
275   GstQsvEncoder *self = GST_QSV_ENCODER (object);
276   GstQsvEncoderPrivate *priv = self->priv;
277
278   switch (prop_id) {
279     case PROP_TARGET_USAGE:
280       priv->target_usage = g_value_get_uint (value);
281       break;
282     case PROP_LOW_LATENCY:
283       priv->low_latency = g_value_get_boolean (value);
284       break;
285     default:
286       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
287       break;
288   }
289 }
290
291 static void
292 gst_qsv_encoder_get_property (GObject * object, guint prop_id, GValue * value,
293     GParamSpec * pspec)
294 {
295   GstQsvEncoder *self = GST_QSV_ENCODER (object);
296   GstQsvEncoderPrivate *priv = self->priv;
297   GstQsvEncoderClass *klass = GST_QSV_ENCODER_GET_CLASS (self);
298
299   switch (prop_id) {
300     case PROP_ADAPTER_LUID:
301       g_value_set_int64 (value, klass->adapter_luid);
302       break;
303     case PROP_DEVICE_PATH:
304       g_value_set_string (value, klass->display_path);
305       break;
306     case PROP_TARGET_USAGE:
307       g_value_set_uint (value, priv->target_usage);
308       break;
309     case PROP_LOW_LATENCY:
310       g_value_set_boolean (value, priv->low_latency);
311       break;
312     default:
313       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
314       break;
315   }
316 }
317
318 static void
319 gst_qsv_encoder_set_context (GstElement * element, GstContext * context)
320 {
321   GstQsvEncoder *self = GST_QSV_ENCODER (element);
322   GstQsvEncoderClass *klass = GST_QSV_ENCODER_GET_CLASS (element);
323   GstQsvEncoderPrivate *priv = self->priv;
324
325 #ifdef G_OS_WIN32
326   gst_d3d11_handle_set_context_for_adapter_luid (element,
327       context, klass->adapter_luid, (GstD3D11Device **) & priv->device);
328 #else
329   gst_va_handle_set_context (element, context, klass->display_path,
330       (GstVaDisplay **) & priv->device);
331 #endif
332
333   GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
334 }
335
336 #ifdef G_OS_WIN32
337 static gboolean
338 gst_qsv_encoder_open_platform_device (GstQsvEncoder * self)
339 {
340   GstQsvEncoderPrivate *priv = self->priv;
341   GstQsvEncoderClass *klass = GST_QSV_ENCODER_GET_CLASS (self);
342   ComPtr < ID3D10Multithread > multi_thread;
343   HRESULT hr;
344   ID3D11Device *device_handle;
345   mfxStatus status;
346   GstD3D11Device *device;
347
348   if (!gst_d3d11_ensure_element_data_for_adapter_luid (GST_ELEMENT (self),
349           klass->adapter_luid, (GstD3D11Device **) & priv->device)) {
350     GST_ERROR_OBJECT (self, "d3d11 device is unavailable");
351     return FALSE;
352   }
353
354   device = GST_D3D11_DEVICE_CAST (priv->device);
355   priv->allocator = gst_qsv_d3d11_allocator_new (device);
356
357   /* For D3D11 device handle to be used by QSV, multithread protection layer
358    * must be enabled before the MFXVideoCORE_SetHandle() call.
359    *
360    * TODO: Need to check performance impact by this mutithread protection layer,
361    * since it may have a negative impact on overall pipeline performance.
362    * If so, we should create encoding session dedicated d3d11 device and
363    * make use of shared resource */
364   device_handle = gst_d3d11_device_get_device_handle (device);
365   hr = device_handle->QueryInterface (IID_PPV_ARGS (&multi_thread));
366   if (!gst_d3d11_result (hr, device)) {
367     GST_ERROR_OBJECT (self, "ID3D10Multithread interface is unavailable");
368     return FALSE;
369   }
370
371   multi_thread->SetMultithreadProtected (TRUE);
372   status = MFXVideoCORE_SetHandle (priv->session, MFX_HANDLE_D3D11_DEVICE,
373       device_handle);
374   if (status != MFX_ERR_NONE) {
375     GST_ERROR_OBJECT (self, "Failed to set d3d11 device handle");
376     return FALSE;
377   }
378
379   /* NOTE: We never use this mfxFrameAllocator to allocate memory from our side,
380    * but required for QSV because:
381    * 1) QSV may request memory allocation for encoder's internal usage,
382    *   MFX_FOURCC_P8 for example
383    * 2) Our mfxFrameAllocator provides bridge layer for
384    *   gst_video_frame_{map,unmap} and mfxFrameAllocator::{Lock,Unlock},
385    *   including mfxFrameAllocator::GetHDL.
386    * 3) GstQsvAllocator provides GstQsvFrame pool, and therefore allocated
387    *   GstQsvFrame struct can be re-used without per-frame malloc/free
388    */
389   status = MFXVideoCORE_SetFrameAllocator (priv->session,
390       gst_qsv_allocator_get_allocator_handle (priv->allocator));
391   if (status != MFX_ERR_NONE) {
392     GST_ERROR_OBJECT (self, "Failed to set frame allocator %d", status);
393     return FALSE;
394   }
395
396   return TRUE;
397 }
398 #else
399 static gboolean
400 gst_qsv_encoder_open_platform_device (GstQsvEncoder * self)
401 {
402   GstQsvEncoderPrivate *priv = self->priv;
403   GstQsvEncoderClass *klass = GST_QSV_ENCODER_GET_CLASS (self);
404   mfxStatus status;
405   GstVaDisplay *display;
406
407   if (!gst_va_ensure_element_data (GST_ELEMENT (self), klass->display_path,
408           (GstVaDisplay **) & priv->device)) {
409     GST_ERROR_OBJECT (self, "VA display is unavailable");
410     return FALSE;
411   }
412
413   display = GST_VA_DISPLAY (priv->device);
414
415   priv->allocator = gst_qsv_va_allocator_new (display);
416
417   status = MFXVideoCORE_SetHandle (priv->session, MFX_HANDLE_VA_DISPLAY,
418       gst_va_display_get_va_dpy (display));
419   if (status != MFX_ERR_NONE) {
420     GST_ERROR_OBJECT (self, "Failed to set VA display handle");
421     return FALSE;
422   }
423
424   status = MFXVideoCORE_SetFrameAllocator (priv->session,
425       gst_qsv_allocator_get_allocator_handle (priv->allocator));
426   if (status != MFX_ERR_NONE) {
427     GST_ERROR_OBJECT (self, "Failed to set frame allocator %d", status);
428     return FALSE;
429   }
430
431   return TRUE;
432 }
433 #endif
434
435 static gboolean
436 gst_qsv_encoder_open (GstVideoEncoder * encoder)
437 {
438   GstQsvEncoder *self = GST_QSV_ENCODER (encoder);
439   GstQsvEncoderPrivate *priv = self->priv;
440   GstQsvEncoderClass *klass = GST_QSV_ENCODER_GET_CLASS (self);
441   mfxStatus status;
442
443   status = MFXCreateSession (gst_qsv_get_loader (), klass->impl_index,
444       &priv->session);
445   if (status != MFX_ERR_NONE) {
446     GST_ERROR_OBJECT (self, "Failed to create session");
447     return FALSE;
448   }
449
450   if (!gst_qsv_encoder_open_platform_device (self)) {
451     g_clear_pointer (&priv->session, MFXClose);
452     gst_clear_object (&priv->allocator);
453     gst_clear_object (&priv->device);
454
455     return FALSE;
456   }
457
458   return TRUE;
459 }
460
461 static gboolean
462 gst_qsv_encoder_reset (GstQsvEncoder * self)
463 {
464   GstQsvEncoderPrivate *priv = self->priv;
465
466   if (priv->encoder) {
467     delete priv->encoder;
468     priv->encoder = nullptr;
469   }
470
471   if (priv->internal_pool) {
472     gst_buffer_pool_set_active (priv->internal_pool, FALSE);
473     gst_clear_object (&priv->internal_pool);
474   }
475
476   g_array_set_size (priv->surface_pool, 0);
477   g_array_set_size (priv->task_pool, 0);
478   g_queue_clear (&priv->free_tasks);
479   g_queue_clear (&priv->pending_tasks);
480
481   return TRUE;
482 }
483
484 static gboolean
485 gst_qsv_encoder_stop (GstVideoEncoder * encoder)
486 {
487   GstQsvEncoder *self = GST_QSV_ENCODER (encoder);
488   GstQsvEncoderPrivate *priv = self->priv;
489
490   gst_qsv_encoder_reset (self);
491   g_clear_pointer (&priv->input_state, gst_video_codec_state_unref);
492
493   return TRUE;
494 }
495
496 static gboolean
497 gst_qsv_encoder_close (GstVideoEncoder * encoder)
498 {
499   GstQsvEncoder *self = GST_QSV_ENCODER (encoder);
500   GstQsvEncoderPrivate *priv = self->priv;
501
502   g_clear_pointer (&priv->session, MFXClose);
503   gst_clear_object (&priv->allocator);
504   gst_clear_object (&priv->device);
505
506   return TRUE;
507 }
508
509 static void
510 gst_qsv_encoder_payload_clear (mfxPayload * payload)
511 {
512   if (!payload)
513     return;
514
515   g_free (payload->Data);
516   g_free (payload);
517 }
518
519 static void
520 gst_qsv_encoder_surface_reset (GstQsvEncoderSurface * surface)
521 {
522   if (!surface)
523     return;
524
525   gst_clear_qsv_frame (&surface->qsv_frame);
526   g_ptr_array_set_size (surface->payload, 0);
527   memset (&surface->encode_control, 0, sizeof (mfxEncodeCtrl));
528 }
529
530 static void
531 gst_qsv_encoder_surface_clear (GstQsvEncoderSurface * surface)
532 {
533   if (!surface)
534     return;
535
536   gst_qsv_encoder_surface_reset (surface);
537   g_clear_pointer (&surface->payload, g_ptr_array_unref);
538   memset (&surface->surface, 0, sizeof (mfxFrameSurface1));
539 }
540
541 static void
542 gst_qsv_encoder_task_reset (GstQsvEncoder * self, GstQsvEncoderTask * task)
543 {
544   GstQsvEncoderPrivate *priv = self->priv;
545
546   if (!task)
547     return;
548
549   task->sync_point = nullptr;
550   task->bitstream.DataLength = 0;
551   g_queue_push_head (&priv->free_tasks, task);
552 }
553
554 static void
555 gst_qsv_encoder_task_clear (GstQsvEncoderTask * task)
556 {
557   if (!task)
558     return;
559
560   g_clear_pointer (&task->bitstream.Data, g_free);
561   memset (&task->bitstream, 0, sizeof (mfxBitstream));
562 }
563
564 static GstQsvEncoderSurface *
565 gst_qsv_encoder_get_next_surface (GstQsvEncoder * self)
566 {
567   GstQsvEncoderPrivate *priv = self->priv;
568   GstQsvEncoderSurface *surface = nullptr;
569
570   for (guint i = priv->next_surface_index; i < priv->surface_pool->len; i++) {
571     GstQsvEncoderSurface *iter =
572         &g_array_index (priv->surface_pool, GstQsvEncoderSurface, i);
573
574     /* This means surface is still being used by QSV */
575     if (iter->surface.Data.Locked > 0)
576       continue;
577
578     surface = iter;
579     priv->next_surface_index = i;
580     goto out;
581   }
582
583   for (guint i = 0; i < priv->next_surface_index; i++) {
584     GstQsvEncoderSurface *iter =
585         &g_array_index (priv->surface_pool, GstQsvEncoderSurface, i);
586
587     /* This means surface is still being used by QSV */
588     if (iter->surface.Data.Locked > 0)
589       continue;
590
591     surface = iter;
592     priv->next_surface_index = i;
593     goto out;
594   }
595
596   /* Magic number to avoid too large pool size */
597   if (priv->surface_pool->len > 64) {
598     GST_ERROR_OBJECT (self,
599         "No availble surface but pool size is too large already");
600     return nullptr;
601   }
602
603   /* Something went wrong, increase surface pool size */
604   GST_INFO_OBJECT (self, "No useable surfaces, increasing pool size to %d",
605       priv->surface_pool->len + 1);
606
607   g_array_set_size (priv->surface_pool, priv->surface_pool->len + 1);
608   surface = &g_array_index (priv->surface_pool, GstQsvEncoderSurface,
609       priv->surface_pool->len - 1);
610
611   memset (surface, 0, sizeof (GstQsvEncoderSurface));
612   surface->surface.Info =
613       g_array_index (priv->surface_pool, GstQsvEncoderSurface, 0).surface.Info;
614   surface->payload = g_ptr_array_new_with_free_func ((GDestroyNotify)
615       gst_qsv_encoder_payload_clear);
616
617 out:
618   priv->next_surface_index++;
619   priv->next_surface_index %= priv->surface_pool->len;
620
621   gst_qsv_encoder_surface_reset (surface);
622   return surface;
623 }
624
625 static mfxStatus
626 gst_qsv_encoder_encode_frame (GstQsvEncoder * self,
627     GstQsvEncoderSurface * surface, GstQsvEncoderTask * task, mfxU64 timestamp)
628 {
629   mfxFrameSurface1 *s;
630   GstQsvEncoderPrivate *priv = self->priv;
631   mfxStatus status;
632   guint retry_count = 0;
633   /* magic number */
634   const guint retry_threshold = 100;
635   mfxEncodeCtrl *encode_ctrl;
636
637   if (surface) {
638     s = &surface->surface;
639     s->Data.MemId = (mfxMemId) surface->qsv_frame;
640     s->Data.TimeStamp = timestamp;
641     encode_ctrl = &surface->encode_control;
642   } else {
643     /* draining */
644     s = nullptr;
645     encode_ctrl = nullptr;
646   }
647
648   do {
649     status = priv->encoder->EncodeFrameAsync (encode_ctrl,
650         s, &task->bitstream, &task->sync_point);
651
652     /* XXX: probably we should try to drain pending tasks if any in this case
653      * as documented? */
654     if (status == MFX_WRN_DEVICE_BUSY && retry_count < retry_threshold) {
655       GST_INFO_OBJECT (self, "GPU is busy, retry count (%d/%d)",
656           retry_count, retry_threshold);
657       retry_count++;
658
659       /* Magic number 10ms */
660       g_usleep (10000);
661       continue;
662     }
663
664     break;
665   } while (TRUE);
666
667   return status;
668 }
669
670 static GstVideoCodecFrame *
671 gst_qsv_encoder_find_output_frame (GstQsvEncoder * self, GstClockTime pts)
672 {
673   GList *frames, *iter;
674   GstVideoCodecFrame *ret = nullptr;
675   GstVideoCodecFrame *closest = nullptr;
676   guint64 min_pts_abs_diff = 0;
677
678   /* give up, just returns the oldest frame */
679   if (!GST_CLOCK_TIME_IS_VALID (pts))
680     return gst_video_encoder_get_oldest_frame (GST_VIDEO_ENCODER (self));
681
682   frames = gst_video_encoder_get_frames (GST_VIDEO_ENCODER (self));
683
684   for (iter = frames; iter; iter = g_list_next (iter)) {
685     GstVideoCodecFrame *frame = (GstVideoCodecFrame *) iter->data;
686     guint64 abs_diff;
687
688     if (!GST_CLOCK_TIME_IS_VALID (frame->pts))
689       continue;
690
691     if (pts == frame->pts) {
692       ret = frame;
693       break;
694     }
695
696     if (pts >= frame->pts)
697       abs_diff = pts - frame->pts;
698     else
699       abs_diff = frame->pts - pts;
700
701     if (!closest || abs_diff < min_pts_abs_diff) {
702       closest = frame;
703       min_pts_abs_diff = abs_diff;
704     }
705   }
706
707   if (!ret && closest)
708     ret = closest;
709
710   if (ret) {
711     gst_video_codec_frame_ref (ret);
712   } else {
713     ret = gst_video_encoder_get_oldest_frame (GST_VIDEO_ENCODER (self));
714   }
715
716   if (frames)
717     g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
718
719   return ret;
720 }
721
722 static GstFlowReturn
723 gst_qsv_encoder_finish_frame (GstQsvEncoder * self, GstQsvEncoderTask * task,
724     gboolean discard)
725 {
726   GstQsvEncoderPrivate *priv = self->priv;
727   GstQsvEncoderClass *klass = GST_QSV_ENCODER_GET_CLASS (self);
728   mfxStatus status;
729   mfxBitstream *bs;
730   GstVideoCodecFrame *frame;
731   GstClockTime qsv_pts = GST_CLOCK_TIME_NONE;
732   GstClockTime qsv_dts = GST_CLOCK_TIME_NONE;
733   GstBuffer *buffer;
734   gboolean keyframe = FALSE;
735   guint retry_count = 0;
736   /* magic number */
737   const guint retry_threshold = 100;
738
739   status = MFX_ERR_NONE;
740   do {
741     /* magic number 100 ms */
742     status = MFXVideoCORE_SyncOperation (priv->session, task->sync_point, 100);
743
744     /* Retry up to 10 sec (100 ms x 100 times), that should be enough time for
745      * encoding a frame using hardware */
746     if (status == MFX_WRN_IN_EXECUTION && retry_count < retry_threshold) {
747       GST_DEBUG_OBJECT (self,
748           "Operation is still in execution, retry count (%d/%d)",
749           retry_count, retry_threshold);
750       retry_count++;
751       continue;
752     }
753
754     break;
755   } while (TRUE);
756
757   if (discard) {
758     gst_qsv_encoder_task_reset (self, task);
759     return GST_FLOW_OK;
760   }
761
762   if (status != MFX_ERR_NONE && status != MFX_ERR_NONE_PARTIAL_OUTPUT) {
763     gst_qsv_encoder_task_reset (self, task);
764
765     if (status == MFX_ERR_ABORTED) {
766       GST_INFO_OBJECT (self, "Operation was aborted");
767       return GST_FLOW_FLUSHING;
768     }
769
770     GST_WARNING_OBJECT (self, "SyncOperation returned %d (%s)",
771         QSV_STATUS_ARGS (status));
772
773     return GST_FLOW_ERROR;
774   }
775
776   bs = &task->bitstream;
777   qsv_pts = gst_qsv_timestamp_to_gst (bs->TimeStamp);
778
779   /* SDK runtime seems to report zero DTS for all fraems in case of VP9.
780    * It sounds SDK bug, but we can workaround it safely because VP9 B-frame is
781    * not supported in this implementation.
782    *
783    * Also we perfer our nanoseconds timestamp instead of QSV's timescale.
784    * So let' ignore QSV's timescale for non-{h264,h265} cases.
785    *
786    * TODO: We may need to use DTS for MPEG2 (not implemented yet)
787    */
788   if (klass->codec_id == MFX_CODEC_AVC || klass->codec_id == MFX_CODEC_HEVC)
789     qsv_dts = gst_qsv_timestamp_to_gst ((mfxU64) bs->DecodeTimeStamp);
790
791   if ((bs->FrameType & MFX_FRAMETYPE_IDR) != 0)
792     keyframe = TRUE;
793
794   if (klass->create_output_buffer) {
795     buffer = klass->create_output_buffer (self, bs);
796   } else {
797     buffer = gst_buffer_new_memdup (bs->Data + bs->DataOffset, bs->DataLength);
798   }
799   gst_qsv_encoder_task_reset (self, task);
800
801   if (!buffer) {
802     GST_ERROR_OBJECT (self, "No output buffer");
803     return GST_FLOW_ERROR;
804   }
805
806   frame = gst_qsv_encoder_find_output_frame (self, qsv_pts);
807   if (frame) {
808     if (GST_CLOCK_TIME_IS_VALID (qsv_dts)) {
809       frame->pts = qsv_pts;
810       frame->dts = qsv_dts;
811     } else {
812       frame->dts = frame->pts;
813     }
814
815     frame->output_buffer = buffer;
816
817     if (keyframe)
818       GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
819
820     return gst_video_encoder_finish_frame (GST_VIDEO_ENCODER (self), frame);
821   }
822
823   /* Empty available frame, something went wrong but we can just push this
824    * buffer */
825   GST_WARNING_OBJECT (self, "Failed to find corresponding frame");
826   GST_BUFFER_PTS (buffer) = qsv_pts;
827   if (GST_CLOCK_TIME_IS_VALID (qsv_dts))
828     GST_BUFFER_DTS (buffer) = qsv_dts;
829   else
830     GST_BUFFER_DTS (buffer) = qsv_pts;
831
832   if (!keyframe)
833     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
834
835   return gst_pad_push (GST_VIDEO_ENCODER_SRC_PAD (self), buffer);
836 }
837
838 static GstFlowReturn
839 gst_qsv_encoder_drain (GstQsvEncoder * self, gboolean discard)
840 {
841   GstQsvEncoderPrivate *priv = self->priv;
842   mfxStatus status = MFX_ERR_NONE;
843   GstFlowReturn ret = GST_FLOW_OK;
844   GstQsvEncoderTask *task;
845
846   if (!priv->session || !priv->encoder)
847     return GST_FLOW_OK;
848
849   GST_DEBUG_OBJECT (self, "Drain");
850
851   /* Drain pending tasks first if any */
852   while (g_queue_get_length (&priv->pending_tasks) > 0) {
853     task = (GstQsvEncoderTask *) g_queue_pop_tail (&priv->pending_tasks);
854     ret = gst_qsv_encoder_finish_frame (self, task, discard);
855   }
856
857   while (status == MFX_ERR_NONE) {
858     task = (GstQsvEncoderTask *) g_queue_pop_tail (&priv->free_tasks);
859     status = gst_qsv_encoder_encode_frame (self,
860         nullptr, task, MFX_TIMESTAMP_UNKNOWN);
861
862     /* once it's fully drained, then driver will return more data */
863     if (status == MFX_ERR_NONE && task->sync_point) {
864       ret = gst_qsv_encoder_finish_frame (self, task, discard);
865       continue;
866     }
867
868     if (status != MFX_ERR_MORE_DATA)
869       GST_WARNING_OBJECT (self, "Unexpected status return %d (%s)",
870           QSV_STATUS_ARGS (status));
871
872     g_queue_push_head (&priv->free_tasks, task);
873   }
874
875   /* Release GstQsvFrame objects */
876   for (guint i = 0; i < priv->surface_pool->len; i++) {
877     GstQsvEncoderSurface *iter =
878         &g_array_index (priv->surface_pool, GstQsvEncoderSurface, i);
879
880     if (iter->surface.Data.Locked > 0) {
881       GST_WARNING_OBJECT (self,
882           "Encoder was drained but QSV is holding surface %d", i);
883       continue;
884     }
885
886     gst_qsv_encoder_surface_reset (iter);
887   }
888
889   return ret;
890 }
891
892 #ifdef G_OS_WIN32
893 static gboolean
894 gst_qsv_encoder_prepare_d3d11_pool (GstQsvEncoder * self,
895     GstCaps * caps, GstVideoInfo * aligned_info)
896 {
897   GstQsvEncoderPrivate *priv = self->priv;
898   GstStructure *config;
899   GstD3D11AllocationParams *params;
900   GstD3D11Device *device = GST_D3D11_DEVICE_CAST (priv->device);
901
902   priv->internal_pool = gst_d3d11_buffer_pool_new (device);
903   config = gst_buffer_pool_get_config (priv->internal_pool);
904   params = gst_d3d11_allocation_params_new (device, aligned_info,
905       (GstD3D11AllocationFlags) 0, 0);
906
907   gst_buffer_pool_config_set_d3d11_allocation_params (config, params);
908   gst_d3d11_allocation_params_free (params);
909   gst_buffer_pool_config_set_params (config, caps,
910       GST_VIDEO_INFO_SIZE (aligned_info), 0, 0);
911   gst_buffer_pool_set_config (priv->internal_pool, config);
912   gst_buffer_pool_set_active (priv->internal_pool, TRUE);
913
914   return TRUE;
915 }
916 #else
917 static gboolean
918 gst_qsv_encoder_prepare_va_pool (GstQsvEncoder * self,
919     GstCaps * caps, GstVideoInfo * aligned_info)
920 {
921   GstQsvEncoderPrivate *priv = self->priv;
922   GstAllocator *allocator;
923   GstStructure *config;
924   GArray *formats;
925   GstAllocationParams params;
926   GstVaDisplay *display = GST_VA_DISPLAY (priv->device);
927
928   formats = g_array_new (FALSE, FALSE, sizeof (GstVideoFormat));
929   g_array_append_val (formats, GST_VIDEO_INFO_FORMAT (aligned_info));
930
931   allocator = gst_va_allocator_new (display, formats);
932   if (!allocator) {
933     GST_ERROR_OBJECT (self, "Failed to create allocator");
934     return FALSE;
935   }
936
937   gst_allocation_params_init (&params);
938
939   priv->internal_pool = gst_va_pool_new_with_config (caps,
940       GST_VIDEO_INFO_SIZE (aligned_info), 0, 0,
941       VA_SURFACE_ATTRIB_USAGE_HINT_GENERIC, GST_VA_FEATURE_AUTO,
942       allocator, &params);
943   gst_object_unref (allocator);
944
945
946   if (!priv->internal_pool) {
947     GST_ERROR_OBJECT (self, "Failed to create va pool");
948     return FALSE;
949   }
950
951   config = gst_buffer_pool_get_config (priv->internal_pool);
952   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
953   gst_buffer_pool_config_set_params (config, caps,
954       GST_VIDEO_INFO_SIZE (aligned_info), 0, 0);
955   gst_buffer_pool_set_config (priv->internal_pool, config);
956   gst_buffer_pool_set_active (priv->internal_pool, TRUE);
957
958   return TRUE;
959 }
960 #endif
961
962 /* Prepare internal pool, which is used to allocate fallback buffer
963  * when upstream buffer is not directly accessible by QSV */
964 static gboolean
965 gst_qsv_encoder_prepare_pool (GstQsvEncoder * self, GstCaps * caps,
966     GstVideoInfo * aligned_info)
967 {
968   GstQsvEncoderPrivate *priv = self->priv;
969   gboolean ret = FALSE;
970   GstCaps *aligned_caps;
971
972   if (priv->internal_pool) {
973     gst_buffer_pool_set_active (priv->internal_pool, FALSE);
974     gst_clear_object (&priv->internal_pool);
975   }
976
977   aligned_caps = gst_video_info_to_caps (aligned_info);
978
979 #ifdef G_OS_WIN32
980   ret = gst_qsv_encoder_prepare_d3d11_pool (self, aligned_caps, aligned_info);
981 #else
982   ret = gst_qsv_encoder_prepare_va_pool (self, aligned_caps, aligned_info);
983 #endif
984
985   gst_caps_unref (aligned_caps);
986
987   return ret;
988 }
989
990 static gboolean
991 gst_qsv_encoder_init_encode_session (GstQsvEncoder * self)
992 {
993   GstQsvEncoderPrivate *priv = self->priv;
994   GstQsvEncoderClass *klass = GST_QSV_ENCODER_GET_CLASS (self);
995   GstVideoInfo *info = &priv->input_state->info;
996   GstCaps *caps = priv->input_state->caps;
997   mfxVideoParam param;
998   mfxFrameInfo *frame_info;
999   mfxFrameAllocRequest alloc_request;
1000   mfxStatus status;
1001   MFXVideoENCODE *encoder_handle = nullptr;
1002   guint bitstream_size;
1003   gboolean ret;
1004   guint64 min_delay_frames, max_delay_frames;
1005   GstClockTime min_latency, max_latency;
1006
1007   gst_qsv_encoder_drain (self, FALSE);
1008   gst_qsv_encoder_reset (self);
1009
1010   encoder_handle = new MFXVideoENCODE (priv->session);
1011
1012   memset (&param, 0, sizeof (mfxVideoParam));
1013
1014   g_ptr_array_set_size (priv->extra_params, 0);
1015   g_assert (klass->set_format);
1016   if (!klass->set_format (self, priv->input_state, &param, priv->extra_params)) {
1017     GST_ERROR_OBJECT (self, "Subclass failed to set format");
1018     goto error;
1019   }
1020
1021   /* LowPower mode supports smaller set of features, don't enable it for now */
1022   param.mfx.LowPower = MFX_CODINGOPTION_OFF;
1023   if (priv->low_latency)
1024     param.AsyncDepth = 1;
1025   else
1026     param.AsyncDepth = 4;
1027
1028   param.mfx.TargetUsage = priv->target_usage;
1029
1030   frame_info = &param.mfx.FrameInfo;
1031
1032   gst_video_info_set_interlaced_format (&priv->aligned_info,
1033       GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_INTERLACE_MODE (info),
1034       frame_info->Width, frame_info->Height);
1035
1036   /* Always video memory, even when upstream is non-hardware element */
1037   priv->mem_type = GST_QSV_VIDEO_MEMORY | GST_QSV_ENCODER_IN_MEMORY;
1038   param.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
1039   if (!gst_qsv_encoder_prepare_pool (self, caps, &priv->aligned_info)) {
1040     GST_ERROR_OBJECT (self, "Failed to prepare pool");
1041     goto error;
1042   }
1043
1044   status = encoder_handle->Query (&param, &param);
1045   /* If device is unhappy with LowPower = OFF, try again with unknown */
1046   if (status < MFX_ERR_NONE) {
1047     GST_INFO_OBJECT (self, "LowPower - OFF returned %d (%s)",
1048         QSV_STATUS_ARGS (status));
1049     param.mfx.LowPower = MFX_CODINGOPTION_UNKNOWN;
1050   }
1051
1052   status = encoder_handle->Query (&param, &param);
1053   QSV_CHECK_STATUS (self, status, MFXVideoENCODE::Query);
1054
1055   status = encoder_handle->QueryIOSurf (&param, &alloc_request);
1056   QSV_CHECK_STATUS (self, status, MFXVideoENCODE::QueryIOSurf);
1057
1058   status = encoder_handle->Init (&param);
1059   QSV_CHECK_STATUS (self, status, MFXVideoENCODE::Init);
1060
1061   status = encoder_handle->GetVideoParam (&param);
1062   QSV_CHECK_STATUS (self, status, MFXVideoENCODE::GetVideoParam);
1063
1064   GST_DEBUG_OBJECT (self, "NumFrameSuggested: %d, AsyncDepth %d",
1065       alloc_request.NumFrameSuggested, param.AsyncDepth);
1066
1067   g_assert (klass->set_output_state);
1068   ret = klass->set_output_state (self, priv->input_state, priv->session);
1069   if (!ret) {
1070     GST_ERROR_OBJECT (self, "Subclass failed to set output state");
1071     goto error;
1072   }
1073
1074   /* Prepare surface pool with size NumFrameSuggested, then if it's not
1075    * sufficient while encoding, we can increse the pool size dynamically
1076    * if needed */
1077   g_array_set_size (priv->surface_pool, alloc_request.NumFrameSuggested);
1078   for (guint i = 0; i < priv->surface_pool->len; i++) {
1079     GstQsvEncoderSurface *surface = &g_array_index (priv->surface_pool,
1080         GstQsvEncoderSurface, i);
1081
1082     surface->surface.Info = param.mfx.FrameInfo;
1083     surface->payload = g_ptr_array_new_with_free_func ((GDestroyNotify)
1084         gst_qsv_encoder_payload_clear);
1085   }
1086   priv->next_surface_index = 0;
1087
1088   g_array_set_size (priv->task_pool, param.AsyncDepth);
1089   if (klass->codec_id == MFX_CODEC_JPEG) {
1090     gdouble factor = 4.0;
1091
1092     /* jpeg zero returns buffer size */
1093     switch (GST_VIDEO_INFO_FORMAT (info)) {
1094       case GST_VIDEO_FORMAT_NV12:
1095         factor = 1.5;
1096         break;
1097       case GST_VIDEO_FORMAT_YUY2:
1098         factor = 2.0;
1099         break;
1100       default:
1101         break;
1102     }
1103     bitstream_size = (guint)
1104         (factor * GST_VIDEO_INFO_WIDTH (info) * GST_VIDEO_INFO_HEIGHT (info));
1105   } else {
1106     bitstream_size =
1107         (guint) param.mfx.BufferSizeInKB * param.mfx.BRCParamMultiplier * 1024;
1108   }
1109
1110   for (guint i = 0; i < priv->task_pool->len; i++) {
1111     GstQsvEncoderTask *task = &g_array_index (priv->task_pool,
1112         GstQsvEncoderTask, i);
1113
1114     task->bitstream.Data = (mfxU8 *) g_malloc (bitstream_size);
1115     task->bitstream.MaxLength = bitstream_size;
1116
1117     g_queue_push_head (&priv->free_tasks, task);
1118   }
1119
1120   min_delay_frames = priv->task_pool->len;
1121   max_delay_frames = priv->surface_pool->len + min_delay_frames;
1122
1123   min_latency = gst_util_uint64_scale (min_delay_frames * GST_SECOND,
1124       param.mfx.FrameInfo.FrameRateExtD, param.mfx.FrameInfo.FrameRateExtN);
1125   max_latency = gst_util_uint64_scale (max_delay_frames * GST_SECOND,
1126       param.mfx.FrameInfo.FrameRateExtD, param.mfx.FrameInfo.FrameRateExtN);
1127   gst_video_encoder_set_latency (GST_VIDEO_ENCODER (self),
1128       min_latency, max_latency);
1129
1130   priv->video_param = param;
1131   priv->encoder = encoder_handle;
1132
1133   return TRUE;
1134
1135 error:
1136   if (encoder_handle)
1137     delete encoder_handle;
1138
1139   gst_qsv_encoder_reset (self);
1140
1141   return FALSE;
1142 }
1143
1144 static gboolean
1145 gst_qsv_encoder_reset_encode_session (GstQsvEncoder * self)
1146 {
1147   GstQsvEncoderPrivate *priv = self->priv;
1148   GPtrArray *extra_params = priv->extra_params;
1149   mfxStatus status;
1150   mfxExtEncoderResetOption reset_opt;
1151
1152   if (!priv->encoder) {
1153     GST_WARNING_OBJECT (self, "Encoder was not configured");
1154     return gst_qsv_encoder_init_encode_session (self);
1155   }
1156
1157   reset_opt.Header.BufferId = MFX_EXTBUFF_ENCODER_RESET_OPTION;
1158   reset_opt.Header.BufferSz = sizeof (mfxExtEncoderResetOption);
1159   reset_opt.StartNewSequence = MFX_CODINGOPTION_OFF;
1160
1161   gst_qsv_encoder_drain (self, FALSE);
1162
1163   g_ptr_array_add (extra_params, &reset_opt);
1164   priv->video_param.ExtParam = (mfxExtBuffer **) extra_params->pdata;
1165   priv->video_param.NumExtParam = extra_params->len;
1166
1167   status = priv->encoder->Reset (&priv->video_param);
1168   g_ptr_array_remove_index (extra_params, extra_params->len - 1);
1169   priv->video_param.NumExtParam = extra_params->len;
1170
1171   if (status != MFX_ERR_NONE) {
1172     GST_WARNING_OBJECT (self, "MFXVideoENCODE_Reset returned %d (%s)",
1173         QSV_STATUS_ARGS (status));
1174     return gst_qsv_encoder_init_encode_session (self);
1175   }
1176
1177   GST_DEBUG_OBJECT (self, "Encode session reset done");
1178
1179   return TRUE;
1180 }
1181
1182 static gboolean
1183 gst_qsv_encoder_set_format (GstVideoEncoder * encoder,
1184     GstVideoCodecState * state)
1185 {
1186   GstQsvEncoder *self = GST_QSV_ENCODER (encoder);
1187   GstQsvEncoderPrivate *priv = self->priv;
1188
1189   g_clear_pointer (&priv->input_state, gst_video_codec_state_unref);
1190   priv->input_state = gst_video_codec_state_ref (state);
1191
1192   return gst_qsv_encoder_init_encode_session (self);
1193 }
1194
1195 static mfxU16
1196 gst_qsv_encoder_get_pic_struct (GstQsvEncoder * self,
1197     GstVideoCodecFrame * frame)
1198 {
1199   GstQsvEncoderClass *klass = GST_QSV_ENCODER_GET_CLASS (self);
1200   GstQsvEncoderPrivate *priv = self->priv;
1201   GstVideoInfo *info = &priv->input_state->info;
1202
1203   if (klass->codec_id != MFX_CODEC_AVC)
1204     return MFX_PICSTRUCT_PROGRESSIVE;
1205
1206   if (!GST_VIDEO_INFO_IS_INTERLACED (info))
1207     return MFX_PICSTRUCT_PROGRESSIVE;
1208
1209   if (GST_VIDEO_INFO_INTERLACE_MODE (info) == GST_VIDEO_INTERLACE_MODE_MIXED) {
1210     if (!GST_BUFFER_FLAG_IS_SET (frame->input_buffer,
1211             GST_VIDEO_BUFFER_FLAG_INTERLACED)) {
1212       return MFX_PICSTRUCT_PROGRESSIVE;
1213     }
1214
1215     if (GST_BUFFER_FLAG_IS_SET (frame->input_buffer, GST_VIDEO_BUFFER_FLAG_TFF))
1216       return MFX_PICSTRUCT_FIELD_TFF;
1217
1218     return MFX_PICSTRUCT_FIELD_BFF;
1219   }
1220
1221   switch (GST_VIDEO_INFO_FIELD_ORDER (info)) {
1222     case GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST:
1223       return MFX_PICSTRUCT_FIELD_TFF;
1224       break;
1225     case GST_VIDEO_FIELD_ORDER_BOTTOM_FIELD_FIRST:
1226       return MFX_PICSTRUCT_FIELD_BFF;
1227       break;
1228     default:
1229       break;
1230   }
1231
1232   if (GST_BUFFER_FLAG_IS_SET (frame->input_buffer, GST_VIDEO_BUFFER_FLAG_TFF))
1233     return MFX_PICSTRUCT_FIELD_TFF;
1234
1235   return MFX_PICSTRUCT_FIELD_BFF;
1236 }
1237
1238 static GstFlowReturn
1239 gst_qsv_encoder_handle_frame (GstVideoEncoder * encoder,
1240     GstVideoCodecFrame * frame)
1241 {
1242   GstQsvEncoder *self = GST_QSV_ENCODER (encoder);
1243   GstQsvEncoderPrivate *priv = self->priv;
1244   GstQsvEncoderClass *klass = GST_QSV_ENCODER_GET_CLASS (self);
1245   GstFlowReturn ret = GST_FLOW_ERROR;
1246   GstQsvEncoderSurface *surface;
1247   GstQsvEncoderTask *task;
1248   mfxU64 timestamp;
1249   mfxStatus status;
1250
1251   if (klass->check_reconfigure && priv->encoder) {
1252     GstQsvEncoderReconfigure reconfigure;
1253
1254     reconfigure = klass->check_reconfigure (self, priv->session,
1255         &priv->video_param, priv->extra_params);
1256
1257     switch (reconfigure) {
1258       case GST_QSV_ENCODER_RECONFIGURE_BITRATE:
1259         if (!gst_qsv_encoder_reset_encode_session (self)) {
1260           GST_ERROR_OBJECT (self, "Failed to reset session");
1261           gst_video_encoder_finish_frame (encoder, frame);
1262
1263           return GST_FLOW_ERROR;
1264         }
1265         break;
1266       case GST_QSV_ENCODER_RECONFIGURE_FULL:
1267         if (!gst_qsv_encoder_init_encode_session (self)) {
1268           GST_ERROR_OBJECT (self, "Failed to init session");
1269           gst_video_encoder_finish_frame (encoder, frame);
1270
1271           return GST_FLOW_ERROR;
1272         }
1273         break;
1274       default:
1275         break;
1276     }
1277   }
1278
1279   if (!priv->encoder) {
1280     GST_ERROR_OBJECT (self, "Encoder object was not configured");
1281     gst_video_encoder_finish_frame (encoder, frame);
1282
1283     return GST_FLOW_NOT_NEGOTIATED;
1284   }
1285
1286   surface = gst_qsv_encoder_get_next_surface (self);
1287   if (!surface) {
1288     GST_ERROR_OBJECT (self, "No available surface");
1289     goto out;
1290   }
1291
1292   task = (GstQsvEncoderTask *) g_queue_pop_tail (&priv->free_tasks);
1293   g_assert (task);
1294
1295   surface->qsv_frame =
1296       gst_qsv_allocator_acquire_frame (priv->allocator, priv->mem_type,
1297       &priv->input_state->info, gst_buffer_ref (frame->input_buffer),
1298       priv->internal_pool);
1299
1300   if (!surface->qsv_frame) {
1301     GST_ERROR_OBJECT (self, "Failed to wrap buffer with qsv frame");
1302     gst_qsv_encoder_task_reset (self, task);
1303     goto out;
1304   }
1305
1306   surface->surface.Info.PicStruct =
1307       gst_qsv_encoder_get_pic_struct (self, frame);
1308
1309   if (GST_VIDEO_CODEC_FRAME_IS_FORCE_KEYFRAME (frame)) {
1310     surface->encode_control.FrameType =
1311         MFX_FRAMETYPE_IDR | MFX_FRAMETYPE_I | MFX_FRAMETYPE_REF;
1312   } else {
1313     surface->encode_control.FrameType = MFX_FRAMETYPE_UNKNOWN;
1314   }
1315
1316   if (klass->attach_payload) {
1317     klass->attach_payload (self, frame, surface->payload);
1318     if (surface->payload->len > 0) {
1319       surface->encode_control.NumPayload = surface->payload->len;
1320       surface->encode_control.Payload = (mfxPayload **) surface->payload->pdata;
1321     }
1322   }
1323
1324   timestamp = gst_qsv_timestamp_from_gst (frame->pts);
1325   status = gst_qsv_encoder_encode_frame (self, surface, task, timestamp);
1326   if (status != MFX_ERR_NONE && status != MFX_ERR_MORE_DATA) {
1327     GST_ERROR_OBJECT (self, "Failed to encode frame, ret %d (%s)",
1328         QSV_STATUS_ARGS (status));
1329     gst_qsv_encoder_task_reset (self, task);
1330     goto out;
1331   }
1332
1333   if (status == MFX_ERR_NONE && task->sync_point) {
1334     g_queue_push_head (&priv->pending_tasks, task);
1335   } else {
1336     gst_qsv_encoder_task_reset (self, task);
1337   }
1338
1339   ret = GST_FLOW_OK;
1340   /* Do not sync immediately, but record tasks which have output buffer here
1341    * to improve throughput.
1342    * In this way, hardware may be able to run encoding job from its background
1343    * threads (if any). We will do sync only when there's no more free task item
1344    */
1345   while (g_queue_get_length (&priv->pending_tasks) >= priv->task_pool->len) {
1346     GstQsvEncoderTask *task =
1347         (GstQsvEncoderTask *) g_queue_pop_tail (&priv->pending_tasks);
1348     ret = gst_qsv_encoder_finish_frame (self, task, FALSE);
1349   }
1350
1351 out:
1352   gst_video_codec_frame_unref (frame);
1353
1354   return ret;
1355 }
1356
1357 static GstFlowReturn
1358 gst_qsv_encoder_finish (GstVideoEncoder * encoder)
1359 {
1360   GstQsvEncoder *self = GST_QSV_ENCODER (encoder);
1361
1362   return gst_qsv_encoder_drain (self, FALSE);
1363 }
1364
1365 static gboolean
1366 gst_qsv_encoder_flush (GstVideoEncoder * encoder)
1367 {
1368   GstQsvEncoder *self = GST_QSV_ENCODER (encoder);
1369
1370   gst_qsv_encoder_drain (self, TRUE);
1371
1372   return TRUE;
1373 }
1374
1375 static gboolean
1376 gst_qsv_encoder_handle_context_query (GstQsvEncoder * self, GstQuery * query)
1377 {
1378   GstQsvEncoderPrivate *priv = self->priv;
1379
1380 #ifdef G_OS_WIN32
1381   return gst_d3d11_handle_context_query (GST_ELEMENT (self), query,
1382       (GstD3D11Device *) priv->device);
1383 #else
1384   return gst_va_handle_context_query (GST_ELEMENT (self), query,
1385       (GstVaDisplay *) priv->device);
1386 #endif
1387 }
1388
1389 static gboolean
1390 gst_qsv_encoder_sink_query (GstVideoEncoder * encoder, GstQuery * query)
1391 {
1392   GstQsvEncoder *self = GST_QSV_ENCODER (encoder);
1393
1394   switch (GST_QUERY_TYPE (query)) {
1395     case GST_QUERY_CONTEXT:
1396       if (gst_qsv_encoder_handle_context_query (self, query))
1397         return TRUE;
1398       break;
1399     default:
1400       break;
1401   }
1402
1403   return GST_VIDEO_ENCODER_CLASS (parent_class)->sink_query (encoder, query);
1404 }
1405
1406 static gboolean
1407 gst_qsv_encoder_src_query (GstVideoEncoder * encoder, GstQuery * query)
1408 {
1409   GstQsvEncoder *self = GST_QSV_ENCODER (encoder);
1410
1411   switch (GST_QUERY_TYPE (query)) {
1412     case GST_QUERY_CONTEXT:
1413       if (gst_qsv_encoder_handle_context_query (self, query))
1414         return TRUE;
1415       break;
1416     default:
1417       break;
1418   }
1419
1420   return GST_VIDEO_ENCODER_CLASS (parent_class)->src_query (encoder, query);
1421 }
1422
1423 #ifdef G_OS_WIN32
1424 static gboolean
1425 gst_qsv_encoder_propose_allocation (GstVideoEncoder * encoder, GstQuery * query)
1426 {
1427   GstQsvEncoder *self = GST_QSV_ENCODER (encoder);
1428   GstQsvEncoderPrivate *priv = self->priv;
1429   GstD3D11Device *device = GST_D3D11_DEVICE (priv->device);
1430   GstVideoInfo info;
1431   GstBufferPool *pool;
1432   GstCaps *caps;
1433   guint size;
1434   GstStructure *config;
1435   GstCapsFeatures *features;
1436   gboolean is_d3d11 = FALSE;
1437
1438   gst_query_parse_allocation (query, &caps, nullptr);
1439   if (!caps) {
1440     GST_WARNING_OBJECT (self, "null caps in query");
1441     return FALSE;
1442   }
1443
1444   if (!gst_video_info_from_caps (&info, caps)) {
1445     GST_WARNING_OBJECT (self, "Failed to convert caps into info");
1446     return FALSE;
1447   }
1448
1449   features = gst_caps_get_features (caps, 0);
1450   if (features && gst_caps_features_contains (features,
1451           GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY)) {
1452     GST_DEBUG_OBJECT (self, "upstream support d3d11 memory");
1453     pool = gst_d3d11_buffer_pool_new (device);
1454     is_d3d11 = TRUE;
1455   } else {
1456     pool = gst_d3d11_staging_buffer_pool_new (device);
1457   }
1458
1459   config = gst_buffer_pool_get_config (pool);
1460   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
1461
1462   if (is_d3d11) {
1463     GstD3D11AllocationParams *d3d11_params;
1464     GstVideoAlignment align;
1465
1466     /* d3d11 buffer pool doesn't support generic video alignment
1467      * because memory layout of CPU accessible staging texture is uncontrollable.
1468      * Do D3D11 specific handling */
1469     gst_video_alignment_reset (&align);
1470
1471     align.padding_right = GST_VIDEO_INFO_WIDTH (&priv->aligned_info) -
1472         GST_VIDEO_INFO_WIDTH (&info);
1473     align.padding_bottom = GST_VIDEO_INFO_HEIGHT (&priv->aligned_info) -
1474         GST_VIDEO_INFO_HEIGHT (&info);
1475
1476     d3d11_params = gst_d3d11_allocation_params_new (device, &info,
1477         (GstD3D11AllocationFlags) 0, 0);
1478
1479     gst_d3d11_allocation_params_alignment (d3d11_params, &align);
1480     gst_buffer_pool_config_set_d3d11_allocation_params (config, d3d11_params);
1481     gst_d3d11_allocation_params_free (d3d11_params);
1482   }
1483
1484   size = GST_VIDEO_INFO_SIZE (&info);
1485   gst_buffer_pool_config_set_params (config,
1486       caps, size, priv->surface_pool->len, 0);
1487
1488   if (!gst_buffer_pool_set_config (pool, config)) {
1489     GST_WARNING_OBJECT (self, "Failed to set pool config");
1490     gst_object_unref (pool);
1491     return FALSE;
1492   }
1493
1494   /* d3d11 buffer pool will update actual CPU accessible buffer size based on
1495    * allocated staging texture per gst_buffer_pool_set_config() call,
1496    * need query again to get the size */
1497   config = gst_buffer_pool_get_config (pool);
1498   gst_buffer_pool_config_get_params (config, nullptr, &size, nullptr, nullptr);
1499   gst_structure_free (config);
1500
1501   gst_query_add_allocation_pool (query, pool, size, priv->surface_pool->len, 0);
1502   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, nullptr);
1503   gst_object_unref (pool);
1504
1505   return TRUE;
1506 }
1507 #else
1508 static gboolean
1509 gst_qsv_encoder_propose_allocation (GstVideoEncoder * encoder, GstQuery * query)
1510 {
1511   GstQsvEncoder *self = GST_QSV_ENCODER (encoder);
1512   GstQsvEncoderPrivate *priv = self->priv;
1513   GstVideoInfo info;
1514   GstAllocator *allocator = nullptr;
1515   GstBufferPool *pool;
1516   GstCaps *caps;
1517   guint size;
1518   GstStructure *config;
1519   GstVideoAlignment align;
1520   GstAllocationParams params;
1521   GArray *formats;
1522
1523   gst_query_parse_allocation (query, &caps, nullptr);
1524   if (!caps) {
1525     GST_WARNING_OBJECT (self, "null caps in query");
1526     return FALSE;
1527   }
1528
1529   if (!gst_video_info_from_caps (&info, caps)) {
1530     GST_WARNING_OBJECT (self, "Failed to convert caps into info");
1531     return FALSE;
1532   }
1533
1534   gst_allocation_params_init (&params);
1535
1536   formats = g_array_new (FALSE, FALSE, sizeof (GstVideoFormat));
1537   g_array_append_val (formats, GST_VIDEO_INFO_FORMAT (&info));
1538
1539   allocator = gst_va_allocator_new (GST_VA_DISPLAY (priv->device), formats);
1540   if (!allocator) {
1541     GST_ERROR_OBJECT (self, "Failed to create allocator");
1542     return FALSE;
1543   }
1544
1545   pool = gst_va_pool_new_with_config (caps,
1546       GST_VIDEO_INFO_SIZE (&info), priv->surface_pool->len, 0,
1547       VA_SURFACE_ATTRIB_USAGE_HINT_GENERIC, GST_VA_FEATURE_AUTO,
1548       allocator, &params);
1549
1550   if (!pool) {
1551     GST_ERROR_OBJECT (self, "Failed to create va pool");
1552     gst_object_unref (allocator);
1553
1554     return FALSE;
1555   }
1556
1557   gst_video_alignment_reset (&align);
1558   align.padding_right = GST_VIDEO_INFO_WIDTH (&priv->aligned_info) -
1559       GST_VIDEO_INFO_WIDTH (&info);
1560   align.padding_bottom = GST_VIDEO_INFO_HEIGHT (&priv->aligned_info) -
1561       GST_VIDEO_INFO_HEIGHT (&info);
1562
1563   config = gst_buffer_pool_get_config (pool);
1564   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
1565   gst_buffer_pool_config_add_option (config,
1566       GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
1567   gst_buffer_pool_config_set_video_alignment (config, &align);
1568
1569   gst_buffer_pool_config_set_params (config,
1570       caps, GST_VIDEO_INFO_SIZE (&info), priv->surface_pool->len, 0);
1571
1572   if (!gst_buffer_pool_set_config (pool, config)) {
1573     GST_ERROR_OBJECT (self, "Failed to set pool config");
1574     gst_clear_object (&allocator);
1575     gst_object_unref (pool);
1576     return FALSE;
1577   }
1578
1579   if (allocator)
1580     gst_query_add_allocation_param (query, allocator, &params);
1581
1582   config = gst_buffer_pool_get_config (pool);
1583   gst_buffer_pool_config_get_params (config, nullptr, &size, nullptr, nullptr);
1584   gst_structure_free (config);
1585
1586   gst_query_add_allocation_pool (query, pool, size, priv->surface_pool->len, 0);
1587   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, nullptr);
1588
1589   gst_clear_object (&allocator);
1590   gst_object_unref (pool);
1591
1592   return TRUE;
1593 }
1594 #endif