2 * Copyright (C) 2021 Intel Corporation
3 * Author: He Junyan <junyan.he@intel.com>
4 * Author: Víctor Jáquez <vjaquez@igalia.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
26 #include "gstvaencoder.h"
28 #include "gstvacaps.h"
29 #include "gstvadisplay_priv.h"
32 typedef struct _GstVaProfileConfig GstVaProfileConfig;
34 struct _GstVaProfileConfig
44 GArray *available_profiles;
46 GstCaps *sinkpad_caps;
47 GstVaDisplay *display;
51 VAEntrypoint entrypoint;
57 GstBufferPool *recon_pool;
60 GST_DEBUG_CATEGORY_STATIC (gst_va_encoder_debug);
61 #define GST_CAT_DEFAULT gst_va_encoder_debug
63 #define gst_va_encoder_parent_class parent_class
64 G_DEFINE_TYPE_WITH_CODE (GstVaEncoder, gst_va_encoder, GST_TYPE_OBJECT,
65 GST_DEBUG_CATEGORY_INIT (gst_va_encoder_debug, "vaencoder", 0,
80 static GParamSpec *g_properties[N_PROPERTIES];
83 _destroy_buffer (GstVaDisplay * display, VABufferID buffer)
87 VADisplay dpy = gst_va_display_get_va_dpy (display);
89 status = vaDestroyBuffer (dpy, buffer);
90 if (status != VA_STATUS_SUCCESS) {
92 GST_WARNING ("Failed to destroy the buffer: %s", vaErrorStr (status));
99 _create_buffer (GstVaEncoder * self, gint type, gpointer data, gsize size)
102 VADisplay dpy = gst_va_display_get_va_dpy (self->display);
106 GST_OBJECT_LOCK (self);
107 context = self->context;
108 GST_OBJECT_UNLOCK (self);
110 dpy = gst_va_display_get_va_dpy (self->display);
111 status = vaCreateBuffer (dpy, context, type, size, 1, data, &buffer);
112 if (status != VA_STATUS_SUCCESS) {
113 GST_ERROR_OBJECT (self, "vaCreateBuffer: %s", vaErrorStr (status));
114 return VA_INVALID_ID;
121 gst_va_encoder_set_property (GObject * object, guint prop_id,
122 const GValue * value, GParamSpec * pspec)
124 GstVaEncoder *self = GST_VA_ENCODER (object);
126 GST_OBJECT_LOCK (self);
130 g_assert (!self->display);
131 self->display = g_value_dup_object (value);
134 case PROP_ENTRYPOINT:{
135 self->entrypoint = g_value_get_int (value);
139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143 GST_OBJECT_UNLOCK (self);
147 gst_va_encoder_get_property (GObject * object, guint prop_id, GValue * value,
150 GstVaEncoder *self = GST_VA_ENCODER (object);
152 GST_OBJECT_LOCK (self);
156 g_value_set_object (value, self->display);
159 g_value_set_int (value, self->profile);
161 case PROP_ENTRYPOINT:
162 g_value_set_int (value, self->entrypoint);
165 g_value_set_uint (value, self->rt_format);
168 g_value_set_int (value, self->coded_width);
171 g_value_set_int (value, self->coded_height);
173 case PROP_CODED_BUF_SIZE:
174 g_value_set_int (value, self->codedbuf_size);
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 GST_OBJECT_UNLOCK (self);
185 gst_va_encoder_init (GstVaEncoder * self)
187 self->profile = VAProfileNone;
188 self->entrypoint = 0;
189 self->config = VA_INVALID_ID;
190 self->context = VA_INVALID_ID;
192 self->coded_width = 0;
193 self->coded_height = 0;
194 self->codedbuf_size = 0;
195 g_clear_pointer (&self->recon_pool, gst_object_unref);
198 static inline gboolean
199 _is_open_unlocked (GstVaEncoder * self)
201 return (self->config != VA_INVALID_ID && self->profile != VAProfileNone);
205 gst_va_encoder_is_open (GstVaEncoder * self)
209 g_return_val_if_fail (GST_IS_VA_ENCODER (self), FALSE);
211 GST_OBJECT_LOCK (self);
212 ret = _is_open_unlocked (self);
213 GST_OBJECT_UNLOCK (self);
218 gst_va_encoder_close (GstVaEncoder * self)
222 VAConfigID config = VA_INVALID_ID;
223 VAContextID context = VA_INVALID_ID;
224 GstBufferPool *recon_pool = NULL;
226 g_return_val_if_fail (GST_IS_VA_ENCODER (self), FALSE);
228 GST_OBJECT_LOCK (self);
229 if (!_is_open_unlocked (self)) {
230 GST_OBJECT_UNLOCK (self);
234 config = self->config;
235 context = self->context;
237 recon_pool = self->recon_pool;
238 self->recon_pool = NULL;
240 gst_va_encoder_init (self);
241 GST_OBJECT_UNLOCK (self);
243 gst_buffer_pool_set_active (recon_pool, FALSE);
244 g_clear_pointer (&recon_pool, gst_object_unref);
246 dpy = gst_va_display_get_va_dpy (self->display);
248 if (context != VA_INVALID_ID) {
249 status = vaDestroyContext (dpy, context);
250 if (status != VA_STATUS_SUCCESS)
251 GST_ERROR_OBJECT (self, "vaDestroyContext: %s", vaErrorStr (status));
254 status = vaDestroyConfig (dpy, config);
255 if (status != VA_STATUS_SUCCESS)
256 GST_ERROR_OBJECT (self, "vaDestroyConfig: %s", vaErrorStr (status));
258 gst_caps_replace (&self->srcpad_caps, NULL);
259 gst_caps_replace (&self->sinkpad_caps, NULL);
265 _get_surface_formats (GstVaDisplay * display, VAConfigID config)
268 GstVideoFormat format;
269 VASurfaceAttrib *attribs;
270 guint i, attrib_count;
272 attribs = gst_va_get_surface_attribs (display, config, &attrib_count);
276 formats = g_array_new (FALSE, FALSE, sizeof (GstVideoFormat));
278 for (i = 0; i < attrib_count; i++) {
279 if (attribs[i].value.type != VAGenericValueTypeInteger)
281 switch (attribs[i].type) {
282 case VASurfaceAttribPixelFormat:
283 format = gst_va_video_format_from_va_fourcc (attribs[i].value.value.i);
284 if (format != GST_VIDEO_FORMAT_UNKNOWN)
285 g_array_append_val (formats, format);
294 if (formats->len == 0) {
295 g_array_unref (formats);
302 static GstBufferPool *
303 _create_reconstruct_pool (GstVaDisplay * display, GArray * surface_formats,
304 GstVideoFormat format, gint coded_width, gint coded_height,
307 GstAllocator *allocator = NULL;
308 guint usage_hint = VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER;
310 GstAllocationParams params = { 0, };
313 GstCaps *caps = NULL;
315 gst_video_info_set_format (&info, format, coded_width, coded_height);
317 size = GST_VIDEO_INFO_SIZE (&info);
319 caps = gst_video_info_to_caps (&info);
320 gst_caps_set_features_simple (caps,
321 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_VA));
323 allocator = gst_va_allocator_new (display, surface_formats);
325 gst_allocation_params_init (¶ms);
327 pool = gst_va_pool_new_with_config (caps, size, 0, max_buffers, usage_hint,
328 GST_VA_FEATURE_AUTO, allocator, ¶ms);
330 gst_clear_object (&allocator);
331 gst_clear_caps (&caps);
337 gst_va_encoder_open (GstVaEncoder * self, VAProfile profile,
338 VAEntrypoint entrypoint, GstVideoFormat video_format, guint rt_format,
339 gint coded_width, gint coded_height, gint codedbuf_size,
340 guint max_reconstruct_surfaces, guint rc_ctrl, guint32 packed_headers)
343 VAConfigAttrib attribs[3] = {
344 { .type = VAConfigAttribRTFormat, .value = rt_format, },
345 { .type = VAConfigAttribRateControl, .value = rc_ctrl, },
348 VAConfigID config = VA_INVALID_ID;
349 VAContextID context = VA_INVALID_ID;
351 GArray *surface_formats = NULL;
353 GstBufferPool *recon_pool = NULL;
354 guint attrib_idx = 2;
356 g_return_val_if_fail (GST_IS_VA_ENCODER (self), FALSE);
357 g_return_val_if_fail (codedbuf_size > 0, FALSE);
359 if (gst_va_encoder_is_open (self))
362 if (!gst_va_encoder_has_profile_and_entrypoint (self, profile, entrypoint)) {
363 GST_ERROR_OBJECT (self, "Unsupported profile: %d, entrypoint: %d",
364 profile, entrypoint);
368 if (packed_headers > 0) {
369 attribs[attrib_idx].type = VAConfigAttribEncPackedHeaders;
370 attribs[attrib_idx].value = packed_headers;
374 dpy = gst_va_display_get_va_dpy (self->display);
376 status = vaCreateConfig (dpy, profile, entrypoint, attribs, attrib_idx,
378 if (status != VA_STATUS_SUCCESS) {
379 GST_ERROR_OBJECT (self, "vaCreateConfig: %s", vaErrorStr (status));
383 surface_formats = _get_surface_formats (self->display, config);
384 if (!surface_formats) {
385 GST_ERROR_OBJECT (self, "Failed to get surface formats");
389 recon_pool = _create_reconstruct_pool (self->display, surface_formats,
390 video_format, coded_width, coded_height, max_reconstruct_surfaces);
392 GST_ERROR_OBJECT (self, "Failed to create reconstruct pool");
395 gst_buffer_pool_set_active (recon_pool, TRUE);
397 status = vaCreateContext (dpy, config, coded_width, coded_height,
398 VA_PROGRESSIVE, NULL, 0, &context);
399 if (status != VA_STATUS_SUCCESS) {
400 GST_ERROR_OBJECT (self, "vaCreateConfig: %s", vaErrorStr (status));
404 GST_OBJECT_LOCK (self);
406 self->config = config;
407 self->context = context;
408 self->profile = profile;
409 self->rt_format = rt_format;
410 self->coded_width = coded_width;
411 self->coded_height = coded_height;
412 self->codedbuf_size = codedbuf_size;
413 gst_object_replace ((GstObject **) & self->recon_pool,
414 (GstObject *) recon_pool);
416 GST_OBJECT_UNLOCK (self);
418 g_clear_pointer (&recon_pool, gst_object_unref);
419 /* now we should return now only this profile's caps */
420 gst_caps_replace (&self->srcpad_caps, NULL);
425 g_clear_pointer (&recon_pool, gst_object_unref);
427 if (config != VA_INVALID_ID)
428 vaDestroyConfig (dpy, config);
430 if (context != VA_INVALID_ID)
431 vaDestroyContext (dpy, context);
437 gst_va_encoder_dispose (GObject * object)
439 GstVaEncoder *self = GST_VA_ENCODER (object);
441 gst_va_encoder_close (self);
443 g_clear_pointer (&self->available_profiles, g_array_unref);
444 gst_clear_object (&self->display);
446 G_OBJECT_CLASS (parent_class)->dispose (object);
450 gst_va_encoder_class_init (GstVaEncoderClass * klass)
452 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
454 gobject_class->set_property = gst_va_encoder_set_property;
455 gobject_class->get_property = gst_va_encoder_get_property;
456 gobject_class->dispose = gst_va_encoder_dispose;
458 g_properties[PROP_DISPLAY] =
459 g_param_spec_object ("display", "GstVaDisplay", "GstVADisplay object",
461 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
463 g_properties[PROP_PROFILE] =
464 g_param_spec_int ("va-profile", "VAProfile", "VA Profile",
465 VAProfileNone, 50, VAProfileNone,
466 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
468 g_properties[PROP_ENTRYPOINT] =
469 g_param_spec_int ("va-entrypoint", "VAEntrypoint", "VA Entrypoint",
471 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
473 g_properties[PROP_CHROMA] =
474 g_param_spec_uint ("va-rt-format", "VARTFormat", "VA RT Fromat or chroma",
475 VA_RT_FORMAT_YUV420, VA_RT_FORMAT_PROTECTED, VA_RT_FORMAT_YUV420,
476 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
478 g_properties[PROP_WIDTH] =
479 g_param_spec_int ("coded-width", "coded-picture-width",
480 "coded picture width", 0, G_MAXINT, 0,
481 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
483 g_properties[PROP_HEIGHT] =
484 g_param_spec_int ("coded-height", "coded-picture-height",
485 "coded picture height", 0, G_MAXINT, 0,
486 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
488 g_properties[PROP_CODED_BUF_SIZE] =
489 g_param_spec_int ("coded-buf-size", "coded-buffer-size",
490 "coded buffer size", 0, G_MAXINT, 0,
491 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
493 g_object_class_install_properties (gobject_class, N_PROPERTIES, g_properties);
497 gst_va_encoder_initialize (GstVaEncoder * self, guint32 codec)
499 GArray *enc_profiles = NULL;
502 if (self->available_profiles)
505 enc_profiles = gst_va_display_get_profiles (self->display, codec,
511 self->available_profiles =
512 g_array_new (FALSE, FALSE, sizeof (GstVaProfileConfig));
515 for (i = 0; i < enc_profiles->len; i++) {
516 GstVaProfileConfig config;
518 config.profile = g_array_index (enc_profiles, VAProfile, i);
519 config.entrypoint = self->entrypoint;
520 g_array_append_val (self->available_profiles, config);
524 g_clear_pointer (&enc_profiles, g_array_unref);
526 if (self->available_profiles->len == 0)
533 gst_va_encoder_new (GstVaDisplay * display, guint32 codec,
534 VAEntrypoint entrypoint)
538 g_return_val_if_fail (GST_IS_VA_DISPLAY (display), NULL);
540 self = g_object_new (GST_TYPE_VA_ENCODER, "display", display,
541 "va-entrypoint", entrypoint, NULL);
542 if (!gst_va_encoder_initialize (self, codec))
543 gst_clear_object (&self);
549 gst_va_encoder_has_profile_and_entrypoint (GstVaEncoder * self,
550 VAProfile profile, VAEntrypoint entrypoint)
552 GstVaProfileConfig *config;
555 g_return_val_if_fail (GST_IS_VA_ENCODER (self), FALSE);
557 if (profile == VAProfileNone)
560 for (i = 0; i < self->available_profiles->len; i++) {
561 config = &g_array_index (self->available_profiles, GstVaProfileConfig, i);
562 if (config->profile == profile) {
566 if (config->entrypoint == entrypoint)
570 if (i == self->available_profiles->len)
577 gst_va_encoder_get_max_slice_num (GstVaEncoder * self,
578 VAProfile profile, VAEntrypoint entrypoint)
582 VAConfigAttrib attrib = {.type = VAConfigAttribEncMaxSlices };
584 g_return_val_if_fail (GST_IS_VA_ENCODER (self), -1);
586 if (profile == VAProfileNone)
589 dpy = gst_va_display_get_va_dpy (self->display);
590 status = vaGetConfigAttributes (dpy, profile, entrypoint, &attrib, 1);
591 if (status != VA_STATUS_SUCCESS) {
592 GST_WARNING_OBJECT (self, "Failed to query encoding slices: %s",
593 vaErrorStr (status));
597 if (attrib.value == VA_ATTRIB_NOT_SUPPORTED) {
598 GST_WARNING_OBJECT (self, "Driver does not support encoding picture as "
607 gst_va_encoder_get_max_num_reference (GstVaEncoder * self,
608 VAProfile profile, VAEntrypoint entrypoint,
609 guint32 * list0, guint32 * list1)
613 VAConfigAttrib attrib = {.type = VAConfigAttribEncMaxRefFrames };
615 g_return_val_if_fail (GST_IS_VA_ENCODER (self), FALSE);
617 if (profile == VAProfileNone)
620 dpy = gst_va_display_get_va_dpy (self->display);
621 status = vaGetConfigAttributes (dpy, profile, entrypoint, &attrib, 1);
622 if (status != VA_STATUS_SUCCESS) {
623 GST_WARNING_OBJECT (self, "Failed to query reference frames: %s",
624 vaErrorStr (status));
628 if (attrib.value == VA_ATTRIB_NOT_SUPPORTED) {
638 *list0 = attrib.value & 0xffff;
640 *list1 = (attrib.value >> 16) & 0xffff;
646 gst_va_encoder_get_rate_control_mode (GstVaEncoder * self,
647 VAProfile profile, VAEntrypoint entrypoint)
651 VAConfigAttrib attrib = {.type = VAConfigAttribRateControl };
653 g_return_val_if_fail (GST_IS_VA_ENCODER (self), 0);
655 if (profile == VAProfileNone)
658 dpy = gst_va_display_get_va_dpy (self->display);
659 status = vaGetConfigAttributes (dpy, profile, entrypoint, &attrib, 1);
660 if (status != VA_STATUS_SUCCESS) {
661 GST_WARNING_OBJECT (self, "Failed to query rate control mode: %s",
662 vaErrorStr (status));
666 if (attrib.value == VA_ATTRIB_NOT_SUPPORTED) {
667 GST_WARNING_OBJECT (self, "Driver does not support any rate control modes");
675 gst_va_encoder_get_quality_level (GstVaEncoder * self,
676 VAProfile profile, VAEntrypoint entrypoint)
680 VAConfigAttrib attrib = {.type = VAConfigAttribEncQualityRange };
682 g_return_val_if_fail (GST_IS_VA_ENCODER (self), 0);
684 if (profile == VAProfileNone)
687 dpy = gst_va_display_get_va_dpy (self->display);
688 status = vaGetConfigAttributes (dpy, profile, entrypoint, &attrib, 1);
689 if (status != VA_STATUS_SUCCESS) {
690 GST_WARNING_OBJECT (self, "Failed to query the quality level: %s",
691 vaErrorStr (status));
695 if (attrib.value == VA_ATTRIB_NOT_SUPPORTED) {
696 GST_WARNING_OBJECT (self, "Driver does not support quality attribute");
704 gst_va_encoder_has_trellis (GstVaEncoder * self,
705 VAProfile profile, VAEntrypoint entrypoint)
709 VAConfigAttrib attrib = {.type = VAConfigAttribEncQuantization };
711 g_return_val_if_fail (GST_IS_VA_ENCODER (self), FALSE);
713 if (profile == VAProfileNone)
716 dpy = gst_va_display_get_va_dpy (self->display);
717 status = vaGetConfigAttributes (dpy, profile, entrypoint, &attrib, 1);
718 if (status != VA_STATUS_SUCCESS) {
719 GST_WARNING_OBJECT (self, "Failed to query the trellis: %s",
720 vaErrorStr (status));
724 if (attrib.value == VA_ATTRIB_NOT_SUPPORTED) {
725 GST_WARNING_OBJECT (self, "Driver does not support trellis");
729 return attrib.value & VA_ENC_QUANTIZATION_TRELLIS_SUPPORTED;
733 gst_va_encoder_get_rtformat (GstVaEncoder * self,
734 VAProfile profile, VAEntrypoint entrypoint)
738 VAConfigAttrib attrib = {.type = VAConfigAttribRTFormat };
740 if (profile == VAProfileNone)
743 dpy = gst_va_display_get_va_dpy (self->display);
744 status = vaGetConfigAttributes (dpy, profile, entrypoint, &attrib, 1);
745 if (status != VA_STATUS_SUCCESS) {
746 GST_ERROR_OBJECT (self, "Failed to query rt format: %s",
747 vaErrorStr (status));
751 if (attrib.value == VA_ATTRIB_NOT_SUPPORTED) {
752 GST_WARNING_OBJECT (self, "Driver does not support any rt format");
760 gst_va_encoder_get_packed_headers (GstVaEncoder * self, VAProfile profile,
761 VAEntrypoint entrypoint)
765 VAConfigAttrib attrib = {.type = VAConfigAttribEncPackedHeaders };
767 if (profile == VAProfileNone)
770 dpy = gst_va_display_get_va_dpy (self->display);
771 status = vaGetConfigAttributes (dpy, profile, entrypoint, &attrib, 1);
772 if (status != VA_STATUS_SUCCESS) {
773 GST_ERROR_OBJECT (self, "Failed to query packed headers: %s",
774 vaErrorStr (status));
778 if (attrib.value == VA_ATTRIB_NOT_SUPPORTED) {
779 GST_WARNING_OBJECT (self, "Driver does not support any packed headers");
786 /* Add packed header such as SPS, PPS, SEI, etc. If adding slice header,
787 it is attached to the last slice parameter. */
789 gst_va_encoder_add_packed_header (GstVaEncoder * self, GstVaEncodePicture * pic,
790 gint type, gpointer data, gsize size_in_bits, gboolean has_emulation_bytes)
793 VAEncPackedHeaderParameterBuffer param = {
795 .bit_length = size_in_bits,
796 .has_emulation_bytes = has_emulation_bytes,
799 g_return_val_if_fail (GST_IS_VA_ENCODER (self), FALSE);
800 g_return_val_if_fail (self->context != VA_INVALID_ID, FALSE);
801 g_return_val_if_fail (pic && data && size_in_bits > 0, FALSE);
802 g_return_val_if_fail (type >= VAEncPackedHeaderSequence
803 && type <= VAEncPackedHeaderRawData, FALSE);
805 if (!gst_va_encoder_is_open (self)) {
806 GST_ERROR_OBJECT (self, "encoder has not been opened yet");
810 buffer = _create_buffer (self, VAEncPackedHeaderParameterBufferType, ¶m,
812 if (buffer == VA_INVALID_ID)
815 g_array_append_val (pic->params, buffer);
817 buffer = _create_buffer (self, VAEncPackedHeaderDataBufferType, data,
818 (size_in_bits + 7) / 8);
819 if (buffer == VA_INVALID_ID)
822 g_array_append_val (pic->params, buffer);
828 gst_va_encoder_add_param (GstVaEncoder * self, GstVaEncodePicture * pic,
829 VABufferType type, gpointer data, gsize size)
833 g_return_val_if_fail (GST_IS_VA_ENCODER (self), FALSE);
834 g_return_val_if_fail (self->context != VA_INVALID_ID, FALSE);
835 g_return_val_if_fail (pic && data && size > 0, FALSE);
837 if (!gst_va_encoder_is_open (self)) {
838 GST_ERROR_OBJECT (self, "encoder has not been opened yet");
842 buffer = _create_buffer (self, type, data, size);
843 if (buffer == VA_INVALID_ID)
846 g_array_append_val (pic->params, buffer);
852 gst_va_encoder_get_surface_formats (GstVaEncoder * self)
854 g_return_val_if_fail (GST_IS_VA_ENCODER (self), NULL);
856 if (!gst_va_encoder_is_open (self))
859 return _get_surface_formats (self->display, self->config);
863 _get_codec_caps (GstVaEncoder * self)
865 GstCaps *sinkpad_caps = NULL, *srcpad_caps = NULL;
867 if (!gst_va_encoder_is_open (self)
868 && GST_IS_VA_DISPLAY_WRAPPED (self->display)) {
869 if (gst_va_caps_from_profiles (self->display, self->available_profiles,
870 self->entrypoint, &sinkpad_caps, &srcpad_caps)) {
871 gst_caps_replace (&self->sinkpad_caps, sinkpad_caps);
872 gst_caps_replace (&self->srcpad_caps, srcpad_caps);
873 gst_caps_unref (srcpad_caps);
874 gst_caps_unref (sinkpad_caps);
884 gst_va_encoder_get_sinkpad_caps (GstVaEncoder * self)
886 GstCaps *sinkpad_caps = NULL;
888 g_return_val_if_fail (GST_IS_VA_ENCODER (self), FALSE);
890 if (g_atomic_pointer_get (&self->sinkpad_caps))
891 return gst_caps_ref (self->sinkpad_caps);
893 if (_get_codec_caps (self))
894 return gst_caps_ref (self->sinkpad_caps);
896 if (gst_va_encoder_is_open (self)) {
897 sinkpad_caps = gst_va_create_raw_caps_from_config (self->display,
899 gst_caps_replace (&self->sinkpad_caps, sinkpad_caps);
900 gst_caps_unref (sinkpad_caps);
902 return gst_caps_ref (self->sinkpad_caps);
909 gst_va_encoder_get_srcpad_caps (GstVaEncoder * self)
911 g_return_val_if_fail (GST_IS_VA_ENCODER (self), FALSE);
913 if (g_atomic_pointer_get (&self->srcpad_caps))
914 return gst_caps_ref (self->srcpad_caps);
916 if (_get_codec_caps (self))
917 return gst_caps_ref (self->srcpad_caps);
919 if (gst_va_encoder_is_open (self)) {
921 VAEntrypoint entrypoint;
924 GST_OBJECT_LOCK (self);
925 profile = self->profile;
926 entrypoint = self->entrypoint;
927 GST_OBJECT_UNLOCK (self);
929 caps = gst_va_create_coded_caps (self->display, profile, entrypoint, NULL);
931 gst_caps_replace (&self->srcpad_caps, caps);
932 return gst_caps_ref (self->srcpad_caps);
940 _destroy_all_buffers (GstVaEncodePicture * pic)
946 g_return_val_if_fail (GST_IS_VA_DISPLAY (pic->display), FALSE);
948 for (i = 0; i < pic->params->len; i++) {
949 buffer = g_array_index (pic->params, VABufferID, i);
950 ret &= _destroy_buffer (pic->display, buffer);
952 pic->params = g_array_set_size (pic->params, 0);
958 gst_va_encoder_encode (GstVaEncoder * self, GstVaEncodePicture * pic)
964 gboolean ret = FALSE;
966 g_return_val_if_fail (pic, FALSE);
968 GST_OBJECT_LOCK (self);
970 if (!_is_open_unlocked (self)) {
971 GST_OBJECT_UNLOCK (self);
972 GST_ERROR_OBJECT (self, "encoder has not been opened yet");
976 context = self->context;
977 GST_OBJECT_UNLOCK (self);
979 surface = gst_va_encode_picture_get_raw_surface (pic);
980 if (surface == VA_INVALID_ID) {
981 GST_ERROR_OBJECT (self, "Encode picture without valid raw surface");
985 GST_TRACE_OBJECT (self, "Encode the surface %#x", surface);
987 dpy = gst_va_display_get_va_dpy (self->display);
989 status = vaBeginPicture (dpy, context, surface);
990 if (status != VA_STATUS_SUCCESS) {
991 GST_WARNING_OBJECT (self, "vaBeginPicture: %s", vaErrorStr (status));
995 if (pic->params->len > 0) {
996 status = vaRenderPicture (dpy, context, (VABufferID *) pic->params->data,
998 if (status != VA_STATUS_SUCCESS) {
999 GST_WARNING_OBJECT (self, "vaRenderPicture: %s", vaErrorStr (status));
1004 status = vaEndPicture (dpy, context);
1005 ret = (status == VA_STATUS_SUCCESS);
1007 GST_WARNING_OBJECT (self, "vaEndPicture: %s", vaErrorStr (status));
1010 _destroy_all_buffers (pic);
1016 _destroy_all_buffers (pic);
1017 status = vaEndPicture (dpy, context);
1024 gst_va_encode_picture_get_reconstruct_surface (GstVaEncodePicture * pic)
1026 g_return_val_if_fail (pic, VA_INVALID_ID);
1027 g_return_val_if_fail (pic->reconstruct_buffer, VA_INVALID_ID);
1029 return gst_va_buffer_get_surface (pic->reconstruct_buffer);
1033 gst_va_encode_picture_get_raw_surface (GstVaEncodePicture * pic)
1035 g_return_val_if_fail (pic, VA_INVALID_ID);
1036 g_return_val_if_fail (pic->raw_buffer, VA_INVALID_ID);
1038 return gst_va_buffer_get_surface (pic->raw_buffer);
1041 GstVaEncodePicture *
1042 gst_va_encode_picture_new (GstVaEncoder * self, GstBuffer * raw_buffer)
1044 GstVaEncodePicture *pic;
1045 VABufferID coded_buffer;
1049 GstBufferPool *recon_pool = NULL;
1050 GstBuffer *reconstruct_buffer = NULL;
1052 GstBufferPoolAcquireParams buffer_pool_params = {
1053 .flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT,
1056 g_return_val_if_fail (self && GST_IS_VA_ENCODER (self), NULL);
1057 g_return_val_if_fail (raw_buffer && GST_IS_BUFFER (raw_buffer), NULL);
1059 GST_OBJECT_LOCK (self);
1061 if (!_is_open_unlocked (self)) {
1062 GST_OBJECT_UNLOCK (self);
1063 GST_ERROR_OBJECT (self, "encoder has not been opened yet");
1067 if (self->codedbuf_size <= 0) {
1068 GST_ERROR_OBJECT (self, "codedbuf_size: %d, is invalid",
1069 self->codedbuf_size);
1070 GST_OBJECT_UNLOCK (self);
1073 codedbuf_size = self->codedbuf_size;
1075 recon_pool = gst_object_ref (self->recon_pool);
1077 GST_OBJECT_UNLOCK (self);
1079 ret = gst_buffer_pool_acquire_buffer (recon_pool, &reconstruct_buffer,
1080 &buffer_pool_params);
1081 gst_clear_object (&recon_pool);
1083 if (ret != GST_FLOW_OK) {
1084 GST_ERROR_OBJECT (self, "Failed to create the reconstruct picture");
1085 gst_clear_buffer (&reconstruct_buffer);
1089 dpy = gst_va_display_get_va_dpy (self->display);
1090 status = vaCreateBuffer (dpy, self->context, VAEncCodedBufferType,
1091 codedbuf_size, 1, NULL, &coded_buffer);
1092 if (status != VA_STATUS_SUCCESS) {
1093 GST_ERROR_OBJECT (self, "vaCreateBuffer: %s", vaErrorStr (status));
1094 gst_clear_buffer (&reconstruct_buffer);
1098 pic = g_slice_new (GstVaEncodePicture);
1099 pic->raw_buffer = gst_buffer_ref (raw_buffer);
1100 pic->reconstruct_buffer = reconstruct_buffer;
1101 pic->display = gst_object_ref (self->display);
1102 pic->coded_buffer = coded_buffer;
1104 pic->params = g_array_sized_new (FALSE, FALSE, sizeof (VABufferID), 8);
1110 gst_va_encode_picture_free (GstVaEncodePicture * pic)
1112 g_return_if_fail (pic);
1114 _destroy_all_buffers (pic);
1116 if (pic->coded_buffer != VA_INVALID_ID)
1117 _destroy_buffer (pic->display, pic->coded_buffer);
1119 gst_buffer_unref (pic->raw_buffer);
1120 gst_buffer_unref (pic->reconstruct_buffer);
1122 g_clear_pointer (&pic->params, g_array_unref);
1123 gst_clear_object (&pic->display);
1125 g_slice_free (GstVaEncodePicture, pic);
1129 * GstVaEncoderRateControl:
1134 gst_va_encoder_rate_control_get_type (void)
1136 static gsize type = 0;
1137 static const GEnumValue values[] = {
1138 {VA_RC_CBR, "Constant Bitrate", "cbr"},
1139 {VA_RC_VBR, "Variable Bitrate", "vbr"},
1140 {VA_RC_VCM, "Video Conferencing Mode (Non HRD compliant)", "vcm"},
1141 {VA_RC_CQP, "Constant Quantizer", "cqp"},
1142 /* {VA_RC_VBR_CONSTRAINED, "VBR with peak rate higher than average bitrate", */
1143 /* "vbr-constrained"}, */
1144 /* {VA_RC_ICQ, "Intelligent Constant Quality", "icq"}, */
1145 /* {VA_RC_MB, "Macroblock based rate control", "mb"}, */
1146 /* {VA_RC_CFS, "Constant Frame Size", "cfs"}, */
1147 /* {VA_RC_PARALLEL, "Parallel BRC", "parallel"}, */
1148 /* {VA_RC_QVBR, "Quality defined VBR", "qvbr"}, */
1149 /* {VA_RC_AVBR, "Average VBR", "avbr"}, */
1153 if (g_once_init_enter (&type)) {
1154 GType _type = g_enum_register_static ("GstVaEncoderRateControl", values);
1155 g_once_init_leave (&type, _type);