2 * Copyright (C) 2022 Intel Corporation
3 * Author: He Junyan <junyan.he@intel.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the0
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #include "gstvabaseenc.h"
23 #include <gst/va/gstva.h>
24 #include <gst/va/vasurfaceimage.h>
27 #include "gstvacaps.h"
29 #define GST_CAT_DEFAULT gst_va_base_enc_debug
30 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
32 struct _GstVaBaseEncPrivate
34 GstVideoInfo sinkpad_info;
35 GstBufferPool *raw_pool;
44 static GParamSpec *properties[N_PROPERTIES];
49 * A base class implementation for VA-API Encoders.
54 #define gst_va_base_enc_parent_class parent_class
55 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstVaBaseEnc, gst_va_base_enc,
56 GST_TYPE_VIDEO_ENCODER, G_ADD_PRIVATE (GstVaBaseEnc)
57 GST_DEBUG_CATEGORY_INIT (gst_va_base_enc_debug,
58 "vabaseenc", 0, "vabaseenc element"););
62 gst_va_base_enc_reset_state (GstVaBaseEnc * base)
64 GstVaBaseEncClass *klass = GST_VA_BASE_ENC_GET_CLASS (base);
66 base->frame_duration = GST_CLOCK_TIME_NONE;
70 base->profile = VAProfileNone;
71 base->entrypoint = klass->entrypoint;
73 base->codedbuf_size = 0;
77 _flush_all_frames (GstVaBaseEnc * base)
79 g_queue_clear_full (&base->reorder_list,
80 (GDestroyNotify) gst_video_codec_frame_unref);
81 g_queue_clear_full (&base->output_list,
82 (GDestroyNotify) gst_video_codec_frame_unref);
83 g_queue_clear_full (&base->ref_list,
84 (GDestroyNotify) gst_video_codec_frame_unref);
88 gst_va_base_enc_open (GstVideoEncoder * venc)
90 GstVaBaseEnc *base = GST_VA_BASE_ENC (venc);
91 GstVaBaseEncClass *klass = GST_VA_BASE_ENC_GET_CLASS (venc);
94 if (!gst_va_ensure_element_data (venc, klass->render_device_path,
98 g_object_notify (G_OBJECT (base), "device-path");
100 if (!g_atomic_pointer_get (&base->encoder)) {
101 GstVaEncoder *va_encoder;
103 va_encoder = gst_va_encoder_new (base->display, klass->codec,
108 gst_object_replace ((GstObject **) (&base->encoder),
109 (GstObject *) va_encoder);
110 gst_clear_object (&va_encoder);
119 gst_va_base_enc_start (GstVideoEncoder * venc)
121 GstVaBaseEnc *base = GST_VA_BASE_ENC (venc);
122 GstVaBaseEncClass *klass = GST_VA_BASE_ENC_GET_CLASS (base);
124 klass->reset_state (base);
126 base->input_frame_count = 0;
127 base->output_frame_count = 0;
129 base->input_state = NULL;
131 /* Set the minimum pts to some huge value (1000 hours). This keeps
132 * the dts at the start of the stream from needing to be
134 base->start_pts = GST_SECOND * 60 * 60 * 1000;
135 gst_video_encoder_set_min_pts (venc, base->start_pts);
141 gst_va_base_enc_close (GstVideoEncoder * venc)
143 GstVaBaseEnc *base = GST_VA_BASE_ENC (venc);
145 gst_clear_object (&base->encoder);
146 gst_clear_object (&base->display);
152 gst_va_base_enc_stop (GstVideoEncoder * venc)
154 GstVaBaseEnc *base = GST_VA_BASE_ENC (venc);
156 _flush_all_frames (base);
158 if (!gst_va_encoder_close (base->encoder)) {
159 GST_ERROR_OBJECT (base, "Failed to close the VA encoder");
163 if (base->priv->raw_pool)
164 gst_buffer_pool_set_active (base->priv->raw_pool, FALSE);
165 gst_clear_object (&base->priv->raw_pool);
167 if (base->input_state)
168 gst_video_codec_state_unref (base->input_state);
174 gst_va_base_enc_get_caps (GstVideoEncoder * venc, GstCaps * filter)
176 GstVaBaseEnc *base = GST_VA_BASE_ENC (venc);
177 GstCaps *caps = NULL, *tmp;
180 caps = gst_va_encoder_get_sinkpad_caps (base->encoder);
184 tmp = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
185 gst_caps_unref (caps);
189 caps = gst_video_encoder_proxy_getcaps (venc, NULL, filter);
192 GST_LOG_OBJECT (base, "Returning caps %" GST_PTR_FORMAT, caps);
196 static GstBufferPool *
197 _get_sinkpad_pool (GstVaBaseEnc * base)
199 GstAllocator *allocator;
200 GstAllocationParams params = { 0, };
201 guint size, usage_hint = 0;
202 GArray *surface_formats = NULL;
205 if (base->priv->raw_pool)
206 return base->priv->raw_pool;
208 g_assert (base->input_state);
209 caps = gst_caps_copy (base->input_state->caps);
210 gst_caps_set_features_simple (caps,
211 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_VA));
213 gst_allocation_params_init (¶ms);
215 size = GST_VIDEO_INFO_SIZE (&base->input_state->info);
217 surface_formats = gst_va_encoder_get_surface_formats (base->encoder);
219 allocator = gst_va_allocator_new (base->display, surface_formats);
221 base->priv->raw_pool = gst_va_pool_new_with_config (caps, size, 1, 0,
222 usage_hint, GST_VA_FEATURE_AUTO, allocator, ¶ms);
223 if (!base->priv->raw_pool) {
224 gst_object_unref (allocator);
228 gst_va_allocator_get_format (allocator, &base->priv->sinkpad_info, NULL,
231 gst_object_unref (allocator);
233 gst_buffer_pool_set_active (base->priv->raw_pool, TRUE);
235 return base->priv->raw_pool;
239 _try_import_buffer (GstVaBaseEnc * base, GstBuffer * inbuf)
244 surface = gst_va_buffer_get_surface (inbuf);
245 if (surface != VA_INVALID_ID)
248 /* TODO: DMA buffer. */
254 gst_va_base_enc_import_input_buffer (GstVaBaseEnc * base,
255 GstBuffer * inbuf, GstBuffer ** buf)
257 GstBuffer *buffer = NULL;
260 GstVideoFrame in_frame, out_frame;
261 gboolean imported, copied;
263 imported = _try_import_buffer (base, inbuf);
265 *buf = gst_buffer_ref (inbuf);
269 /* input buffer doesn't come from a vapool, thus it is required to
270 * have a pool, grab from it a new buffer and copy the input
271 * buffer to the new one */
272 if (!(pool = _get_sinkpad_pool (base)))
273 return GST_FLOW_ERROR;
275 ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
276 if (ret != GST_FLOW_OK)
279 GST_LOG_OBJECT (base, "copying input frame");
281 if (!gst_video_frame_map (&in_frame, &base->input_state->info,
282 inbuf, GST_MAP_READ))
284 if (!gst_video_frame_map (&out_frame, &base->priv->sinkpad_info, buffer,
286 gst_video_frame_unmap (&in_frame);
290 copied = gst_video_frame_copy (&out_frame, &in_frame);
292 gst_video_frame_unmap (&out_frame);
293 gst_video_frame_unmap (&in_frame);
298 /* strictly speaking this is not needed but let's play safe */
299 if (!gst_buffer_copy_into (buffer, inbuf, GST_BUFFER_COPY_FLAGS |
300 GST_BUFFER_COPY_TIMESTAMPS, 0, -1))
301 return GST_FLOW_ERROR;
309 GST_ELEMENT_WARNING (base, CORE, NOT_IMPLEMENTED, (NULL),
310 ("invalid video buffer received"));
312 gst_buffer_unref (buffer);
313 return GST_FLOW_ERROR;
318 gst_va_base_enc_create_output_buffer (GstVaBaseEnc * base,
319 GstVaEncodePicture * picture)
323 GstBuffer *buf = NULL;
325 VACodedBufferSegment *seg, *seg_list;
327 /* Wait for encoding to finish */
328 surface = gst_va_encode_picture_get_raw_surface (picture);
329 if (!va_sync_surface (base->display, surface))
333 if (!va_map_buffer (base->display, picture->coded_buffer,
334 (gpointer *) & seg_list))
338 va_unmap_buffer (base->display, picture->coded_buffer);
339 GST_WARNING_OBJECT (base, "coded buffer has no segment list");
344 for (seg = seg_list; seg; seg = seg->next)
345 coded_size += seg->size;
347 buf = gst_video_encoder_allocate_output_buffer (GST_VIDEO_ENCODER_CAST (base),
350 va_unmap_buffer (base->display, picture->coded_buffer);
351 GST_ERROR_OBJECT (base, "Failed to allocate output buffer, size %d",
357 for (seg = seg_list; seg; seg = seg->next) {
360 write_size = gst_buffer_fill (buf, offset, seg->buf, seg->size);
361 if (write_size != seg->size) {
362 GST_WARNING_OBJECT (base, "Segment size is %d, but copied %"
363 G_GSIZE_FORMAT, seg->size, write_size);
369 va_unmap_buffer (base->display, picture->coded_buffer);
374 gst_clear_buffer (&buf);
378 static GstAllocator *
379 _allocator_from_caps (GstVaBaseEnc * base, GstCaps * caps)
381 GstAllocator *allocator = NULL;
383 if (gst_caps_is_dmabuf (caps)) {
384 allocator = gst_va_dmabuf_allocator_new (base->display);
386 GArray *surface_formats =
387 gst_va_encoder_get_surface_formats (base->encoder);
388 allocator = gst_va_allocator_new (base->display, surface_formats);
395 gst_va_base_enc_propose_allocation (GstVideoEncoder * venc, GstQuery * query)
397 GstVaBaseEnc *base = GST_VA_BASE_ENC (venc);
398 GstAllocator *allocator = NULL;
399 GstAllocationParams params = { 0, };
403 gboolean need_pool = FALSE;
404 guint size, usage_hint = 0;
406 gst_query_parse_allocation (query, &caps, &need_pool);
410 if (!gst_video_info_from_caps (&info, caps)) {
411 GST_ERROR_OBJECT (base, "Cannot parse caps %" GST_PTR_FORMAT, caps);
415 size = GST_VIDEO_INFO_SIZE (&info);
417 gst_allocation_params_init (¶ms);
419 if (!(allocator = _allocator_from_caps (base, caps)))
422 pool = gst_va_pool_new_with_config (caps, size, 1, 0, usage_hint,
423 GST_VA_FEATURE_AUTO, allocator, ¶ms);
425 gst_object_unref (allocator);
429 gst_query_add_allocation_param (query, allocator, ¶ms);
430 gst_query_add_allocation_pool (query, pool, size, 0, 0);
432 GST_DEBUG_OBJECT (base,
433 "proposing %" GST_PTR_FORMAT " with allocator %" GST_PTR_FORMAT,
436 gst_object_unref (allocator);
437 gst_object_unref (pool);
439 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
446 GST_ERROR_OBJECT (base, "failed to set config");
452 _push_buffer_to_downstream (GstVaBaseEnc * base, GstVideoCodecFrame * frame)
454 GstVaEncodePicture *enc_picture;
455 GstVaBaseEncClass *base_class = GST_VA_BASE_ENC_GET_CLASS (base);
459 if (base_class->prepare_output)
460 base_class->prepare_output (base, frame);
463 *((GstVaEncodePicture **) gst_video_codec_frame_get_user_data (frame));
465 buf = gst_va_base_enc_create_output_buffer (base, enc_picture);
467 GST_ERROR_OBJECT (base, "Failed to create output buffer");
471 gst_buffer_replace (&frame->output_buffer, buf);
472 gst_clear_buffer (&buf);
474 GST_LOG_OBJECT (base, "Push to downstream: frame system_frame_number: %d,"
475 " pts: %" GST_TIME_FORMAT ", dts: %" GST_TIME_FORMAT
476 " duration: %" GST_TIME_FORMAT ", buffer size: %" G_GSIZE_FORMAT,
477 frame->system_frame_number, GST_TIME_ARGS (frame->pts),
478 GST_TIME_ARGS (frame->dts), GST_TIME_ARGS (frame->duration),
479 gst_buffer_get_size (frame->output_buffer));
481 ret = gst_video_encoder_finish_frame (GST_VIDEO_ENCODER (base), frame);
485 gst_clear_buffer (&frame->output_buffer);
486 gst_clear_buffer (&buf);
487 gst_video_encoder_finish_frame (GST_VIDEO_ENCODER (base), frame);
489 return GST_FLOW_ERROR;
493 _push_out_one_buffer (GstVaBaseEnc * base)
495 GstVideoCodecFrame *frame_out;
497 guint32 system_frame_number;
499 frame_out = g_queue_pop_head (&base->output_list);
500 gst_video_codec_frame_unref (frame_out);
502 system_frame_number = frame_out->system_frame_number;
504 ret = _push_buffer_to_downstream (base, frame_out);
506 if (ret != GST_FLOW_OK)
507 GST_ERROR_OBJECT (base, "fails to push one buffer, "
508 "system_frame_number %d", system_frame_number);
514 gst_va_base_enc_handle_frame (GstVideoEncoder * venc,
515 GstVideoCodecFrame * frame)
517 GstVaBaseEnc *base = GST_VA_BASE_ENC (venc);
518 GstVaBaseEncClass *base_class = GST_VA_BASE_ENC_GET_CLASS (base);
520 GstBuffer *in_buf = NULL;
521 GstVideoCodecFrame *frame_encode = NULL;
523 GST_LOG_OBJECT (venc,
524 "handle frame id %d, dts %" GST_TIME_FORMAT ", pts %" GST_TIME_FORMAT,
525 frame->system_frame_number,
526 GST_TIME_ARGS (GST_BUFFER_DTS (frame->input_buffer)),
527 GST_TIME_ARGS (GST_BUFFER_PTS (frame->input_buffer)));
529 ret = gst_va_base_enc_import_input_buffer (base,
530 frame->input_buffer, &in_buf);
531 if (ret != GST_FLOW_OK)
532 goto error_buffer_invalid;
534 gst_buffer_replace (&frame->input_buffer, in_buf);
535 gst_clear_buffer (&in_buf);
537 if (!base_class->new_frame (base, frame))
538 goto error_new_frame;
540 if (!base_class->reorder_frame (base, frame, FALSE, &frame_encode))
543 /* pass it to reorder list and we should not use it again. */
546 while (frame_encode) {
547 ret = base_class->encode_frame (base, frame_encode, FALSE);
548 if (ret != GST_FLOW_OK)
551 while (g_queue_get_length (&base->output_list) > 0) {
552 ret = _push_out_one_buffer (base);
553 if (ret != GST_FLOW_OK)
554 goto error_push_buffer;
558 if (!base_class->reorder_frame (base, NULL, FALSE, &frame_encode))
564 error_buffer_invalid:
566 GST_ELEMENT_ERROR (venc, STREAM, ENCODE,
567 ("Failed to import the input frame."), (NULL));
568 gst_clear_buffer (&in_buf);
569 gst_clear_buffer (&frame->output_buffer);
570 gst_video_encoder_finish_frame (venc, frame);
575 GST_ELEMENT_ERROR (venc, STREAM, ENCODE,
576 ("Failed to create the input frame."), (NULL));
577 gst_clear_buffer (&frame->output_buffer);
578 gst_video_encoder_finish_frame (venc, frame);
579 return GST_FLOW_ERROR;
583 GST_ELEMENT_ERROR (venc, STREAM, ENCODE,
584 ("Failed to reorder the input frame."), (NULL));
585 gst_clear_buffer (&frame->output_buffer);
586 gst_video_encoder_finish_frame (venc, frame);
587 return GST_FLOW_ERROR;
591 GST_ELEMENT_ERROR (venc, STREAM, ENCODE,
592 ("Failed to encode the frame."), (NULL));
593 gst_clear_buffer (&frame_encode->output_buffer);
594 gst_video_encoder_finish_frame (venc, frame_encode);
598 GST_ELEMENT_ERROR (venc, STREAM, ENCODE,
599 ("Failed to push the buffer."), (NULL));
604 gst_va_base_enc_drain (GstVideoEncoder * venc)
606 GstVaBaseEnc *base = GST_VA_BASE_ENC (venc);
607 GstVaBaseEncClass *base_class = GST_VA_BASE_ENC_GET_CLASS (base);
608 GstFlowReturn ret = GST_FLOW_OK;
609 GstVideoCodecFrame *frame_enc = NULL;
610 gboolean is_last = FALSE;
612 GST_DEBUG_OBJECT (base, "Encoder is draining");
614 /* Kickout all cached frames */
615 if (!base_class->reorder_frame (base, NULL, TRUE, &frame_enc)) {
616 ret = GST_FLOW_ERROR;
617 goto error_and_purge_all;
623 if (g_queue_is_empty (&base->reorder_list))
626 ret = base_class->encode_frame (base, frame_enc, is_last);
627 if (ret != GST_FLOW_OK)
628 goto error_and_purge_all;
632 ret = _push_out_one_buffer (base);
633 if (ret != GST_FLOW_OK)
634 goto error_and_purge_all;
636 if (!base_class->reorder_frame (base, NULL, TRUE, &frame_enc)) {
637 ret = GST_FLOW_ERROR;
638 goto error_and_purge_all;
642 g_assert (g_queue_is_empty (&base->reorder_list));
644 /* Output all frames. */
645 while (!g_queue_is_empty (&base->output_list)) {
646 ret = _push_out_one_buffer (base);
647 if (ret != GST_FLOW_OK)
648 goto error_and_purge_all;
651 /* Also clear the reference list. */
652 g_queue_clear_full (&base->ref_list,
653 (GDestroyNotify) gst_video_codec_frame_unref);
659 gst_clear_buffer (&frame_enc->output_buffer);
660 gst_video_encoder_finish_frame (venc, frame_enc);
663 if (!g_queue_is_empty (&base->output_list)) {
664 GST_WARNING_OBJECT (base, "Still %d frame in the output list"
665 " after drain", g_queue_get_length (&base->output_list));
666 while (!g_queue_is_empty (&base->output_list)) {
667 frame_enc = g_queue_pop_head (&base->output_list);
668 gst_video_codec_frame_unref (frame_enc);
669 gst_clear_buffer (&frame_enc->output_buffer);
670 gst_video_encoder_finish_frame (venc, frame_enc);
674 if (!g_queue_is_empty (&base->reorder_list)) {
675 GST_WARNING_OBJECT (base, "Still %d frame in the reorder list"
676 " after drain", g_queue_get_length (&base->reorder_list));
677 while (!g_queue_is_empty (&base->reorder_list)) {
678 frame_enc = g_queue_pop_head (&base->reorder_list);
679 gst_video_codec_frame_unref (frame_enc);
680 gst_clear_buffer (&frame_enc->output_buffer);
681 gst_video_encoder_finish_frame (venc, frame_enc);
685 /* Also clear the reference list. */
686 g_queue_clear_full (&base->ref_list,
687 (GDestroyNotify) gst_video_codec_frame_unref);
693 gst_va_base_enc_finish (GstVideoEncoder * venc)
695 return gst_va_base_enc_drain (venc);
699 gst_va_base_enc_set_format (GstVideoEncoder * venc, GstVideoCodecState * state)
701 GstVaBaseEnc *base = GST_VA_BASE_ENC (venc);
702 GstVaBaseEncClass *base_class = GST_VA_BASE_ENC_GET_CLASS (base);
704 g_return_val_if_fail (state->caps != NULL, FALSE);
706 if (base->input_state)
707 gst_video_codec_state_unref (base->input_state);
708 base->input_state = gst_video_codec_state_ref (state);
710 if (gst_va_base_enc_drain (venc) != GST_FLOW_OK)
713 if (!gst_va_encoder_close (base->encoder)) {
714 GST_ERROR_OBJECT (base, "Failed to close the VA encoder");
718 if (!base_class->reconfig (base)) {
719 GST_ERROR_OBJECT (base, "Reconfig the encoder error");
723 /* Sub class should open the encoder if reconfig succeeds. */
724 g_assert (gst_va_encoder_is_open (base->encoder));
730 gst_va_base_enc_flush (GstVideoEncoder * venc)
732 _flush_all_frames (GST_VA_BASE_ENC (venc));
737 _query_context (GstVaBaseEnc * base, GstQuery * query)
739 GstVaDisplay *display = NULL;
742 gst_object_replace ((GstObject **) & display, (GstObject *) base->display);
743 ret = gst_va_handle_context_query (GST_ELEMENT_CAST (base), query, display);
744 gst_clear_object (&display);
750 gst_va_base_enc_src_query (GstVideoEncoder * venc, GstQuery * query)
752 GstVaBaseEnc *base = GST_VA_BASE_ENC (venc);
753 gboolean ret = FALSE;
755 switch (GST_QUERY_TYPE (query)) {
756 case GST_QUERY_CONTEXT:{
757 ret = _query_context (base, query);
760 case GST_QUERY_CAPS:{
761 GstCaps *caps = NULL, *tmp, *filter = NULL;
762 GstVaEncoder *va_encoder = NULL;
765 gst_object_replace ((GstObject **) & va_encoder,
766 (GstObject *) base->encoder);
768 gst_query_parse_caps (query, &filter);
770 fixed_caps = GST_PAD_IS_FIXED_CAPS (GST_VIDEO_ENCODER_SRC_PAD (venc));
772 if (!fixed_caps && va_encoder)
773 caps = gst_va_encoder_get_srcpad_caps (va_encoder);
775 gst_clear_object (&va_encoder);
779 tmp = gst_caps_intersect_full (filter, caps,
780 GST_CAPS_INTERSECT_FIRST);
781 gst_caps_unref (caps);
785 GST_LOG_OBJECT (base, "Returning caps %" GST_PTR_FORMAT, caps);
786 gst_query_set_caps_result (query, caps);
787 gst_caps_unref (caps);
791 /* else jump to default */
794 ret = GST_VIDEO_ENCODER_CLASS (parent_class)->src_query (venc, query);
802 gst_va_base_enc_sink_query (GstVideoEncoder * venc, GstQuery * query)
804 GstVaBaseEnc *base = GST_VA_BASE_ENC (venc);
806 if (GST_QUERY_TYPE (query) == GST_QUERY_CONTEXT)
807 return _query_context (base, query);
809 return GST_VIDEO_ENCODER_CLASS (parent_class)->sink_query (venc, query);
813 gst_va_base_enc_set_context (GstElement * element, GstContext * context)
815 GstVaDisplay *old_display, *new_display;
816 GstVaBaseEnc *base = GST_VA_BASE_ENC (element);
817 GstVaBaseEncClass *klass = GST_VA_BASE_ENC_GET_CLASS (base);
820 old_display = base->display ? gst_object_ref (base->display) : NULL;
822 ret = gst_va_handle_set_context (element, context, klass->render_device_path,
825 new_display = base->display ? gst_object_ref (base->display) : NULL;
827 if (!ret || (old_display && new_display && old_display != new_display
829 GST_ELEMENT_WARNING (element, RESOURCE, BUSY,
830 ("Can't replace VA display while operating"), (NULL));
833 gst_clear_object (&old_display);
834 gst_clear_object (&new_display);
836 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
840 gst_va_base_enc_get_property (GObject * object, guint prop_id,
841 GValue * value, GParamSpec * pspec)
843 GstVaBaseEnc *base = GST_VA_BASE_ENC (object);
846 case PROP_DEVICE_PATH:{
847 if (!(base->display && GST_IS_VA_DISPLAY_DRM (base->display))) {
848 g_value_set_string (value, NULL);
851 g_object_get_property (G_OBJECT (base->display), "path", value);
855 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
860 gst_va_base_enc_init (GstVaBaseEnc * self)
862 g_queue_init (&self->reorder_list);
863 g_queue_init (&self->ref_list);
864 g_queue_init (&self->output_list);
866 self->priv = gst_va_base_enc_get_instance_private (self);
870 gst_va_base_enc_dispose (GObject * object)
872 _flush_all_frames (GST_VA_BASE_ENC (object));
873 gst_va_base_enc_close (GST_VIDEO_ENCODER (object));
875 G_OBJECT_CLASS (parent_class)->dispose (object);
879 gst_va_base_enc_class_init (GstVaBaseEncClass * klass)
881 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
882 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
883 GstVideoEncoderClass *encoder_class = GST_VIDEO_ENCODER_CLASS (klass);
885 gobject_class->get_property = gst_va_base_enc_get_property;
886 gobject_class->dispose = gst_va_base_enc_dispose;
888 element_class->set_context = GST_DEBUG_FUNCPTR (gst_va_base_enc_set_context);
890 encoder_class->open = GST_DEBUG_FUNCPTR (gst_va_base_enc_open);
891 encoder_class->close = GST_DEBUG_FUNCPTR (gst_va_base_enc_close);
892 encoder_class->start = GST_DEBUG_FUNCPTR (gst_va_base_enc_start);
893 encoder_class->stop = GST_DEBUG_FUNCPTR (gst_va_base_enc_stop);
894 encoder_class->getcaps = GST_DEBUG_FUNCPTR (gst_va_base_enc_get_caps);
895 encoder_class->src_query = GST_DEBUG_FUNCPTR (gst_va_base_enc_src_query);
896 encoder_class->sink_query = GST_DEBUG_FUNCPTR (gst_va_base_enc_sink_query);
897 encoder_class->propose_allocation =
898 GST_DEBUG_FUNCPTR (gst_va_base_enc_propose_allocation);
899 encoder_class->handle_frame =
900 GST_DEBUG_FUNCPTR (gst_va_base_enc_handle_frame);
901 encoder_class->set_format = GST_DEBUG_FUNCPTR (gst_va_base_enc_set_format);
902 encoder_class->finish = GST_DEBUG_FUNCPTR (gst_va_base_enc_finish);
903 encoder_class->flush = GST_DEBUG_FUNCPTR (gst_va_base_enc_flush);
905 klass->reset_state = GST_DEBUG_FUNCPTR (gst_va_base_enc_reset_state);
907 properties[PROP_DEVICE_PATH] = g_param_spec_string ("device-path",
908 "Device Path", "DRM device path", NULL,
909 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
911 g_object_class_install_properties (gobject_class, N_PROPERTIES, properties);
913 gst_type_mark_as_plugin_api (GST_TYPE_VA_BASE_ENC, 0);
916 /*********************** Helper Functions ****************************/
918 gst_va_base_enc_add_rate_control_parameter (GstVaBaseEnc * base,
919 GstVaEncodePicture * picture, guint32 rc_mode,
920 guint max_bitrate_bits, guint target_percentage,
921 guint32 qp_i, guint32 min_qp, guint32 max_qp, guint32 mbbrc)
923 uint32_t window_size;
924 struct VAEncMiscParameterRateControlWrap
926 VAEncMiscParameterType type;
927 VAEncMiscParameterRateControl rate_control;
930 if (rc_mode == VA_RC_NONE || rc_mode == VA_RC_CQP)
933 window_size = rc_mode == VA_RC_VBR ? max_bitrate_bits / 2 : max_bitrate_bits;
936 rate_control = (struct VAEncMiscParameterRateControlWrap) {
937 .type = VAEncMiscParameterTypeRateControl,
939 .bits_per_second = max_bitrate_bits,
940 .target_percentage = target_percentage,
941 .window_size = window_size,
945 .rc_flags.bits.mb_rate_control = mbbrc,
951 if (!gst_va_encoder_add_param (base->encoder, picture,
952 VAEncMiscParameterBufferType, &rate_control, sizeof (rate_control))) {
953 GST_ERROR_OBJECT (base, "Failed to create the race control parameter");
961 gst_va_base_enc_add_quality_level_parameter (GstVaBaseEnc * base,
962 GstVaEncodePicture * picture, guint target_usage)
967 VAEncMiscParameterType type;
968 VAEncMiscParameterBufferQualityLevel ql;
970 .type = VAEncMiscParameterTypeQualityLevel,
971 .ql.quality_level = target_usage,
975 if (target_usage == 0)
978 if (!gst_va_encoder_add_param (base->encoder, picture,
979 VAEncMiscParameterBufferType, &quality_level,
980 sizeof (quality_level))) {
981 GST_ERROR_OBJECT (base, "Failed to create the quality level parameter");
989 gst_va_base_enc_add_frame_rate_parameter (GstVaBaseEnc * base,
990 GstVaEncodePicture * picture)
995 VAEncMiscParameterType type;
996 VAEncMiscParameterFrameRate fr;
998 .type = VAEncMiscParameterTypeFrameRate,
999 /* denominator = framerate >> 16 & 0xffff;
1000 * numerator = framerate & 0xffff; */
1002 (GST_VIDEO_INFO_FPS_N (&base->input_state->info) & 0xffff) |
1003 ((GST_VIDEO_INFO_FPS_D (&base->input_state->info) & 0xffff) << 16)
1007 if (!gst_va_encoder_add_param (base->encoder, picture,
1008 VAEncMiscParameterBufferType, &framerate, sizeof (framerate))) {
1009 GST_ERROR_OBJECT (base, "Failed to create the frame rate parameter");
1017 gst_va_base_enc_add_hrd_parameter (GstVaBaseEnc * base,
1018 GstVaEncodePicture * picture, guint32 rc_mode, guint cpb_length_bits)
1023 VAEncMiscParameterType type;
1024 VAEncMiscParameterHRD hrd;
1026 .type = VAEncMiscParameterTypeHRD,
1028 .buffer_size = cpb_length_bits,
1029 .initial_buffer_fullness = cpb_length_bits / 2,
1034 if (rc_mode == VA_RC_NONE || rc_mode == VA_RC_CQP || rc_mode == VA_RC_VCM)
1037 if (!gst_va_encoder_add_param (base->encoder, picture,
1038 VAEncMiscParameterBufferType, &hrd, sizeof (hrd))) {
1039 GST_ERROR_OBJECT (base, "Failed to create the HRD parameter");
1047 gst_va_base_enc_add_trellis_parameter (GstVaBaseEnc * base,
1048 GstVaEncodePicture * picture, gboolean use_trellis)
1053 VAEncMiscParameterType type;
1054 VAEncMiscParameterQuantization tr;
1056 .type = VAEncMiscParameterTypeQuantization,
1057 .tr.quantization_flags.bits = {
1058 .disable_trellis = 0,
1059 .enable_trellis_I = 1,
1060 .enable_trellis_B = 1,
1061 .enable_trellis_P = 1,
1069 if (!gst_va_encoder_add_param (base->encoder, picture,
1070 VAEncMiscParameterBufferType, &trellis, sizeof (trellis))) {
1071 GST_ERROR_OBJECT (base, "Failed to create the trellis parameter");
1079 gst_va_base_enc_add_codec_tag (GstVaBaseEnc * base, const gchar * codec_name)
1081 GstVideoEncoder *venc = GST_VIDEO_ENCODER (base);
1082 GstTagList *tags = gst_tag_list_new_empty ();
1083 const gchar *encoder_name;
1086 g_object_get (venc, "bitrate", &bitrate, NULL);
1088 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_NOMINAL_BITRATE,
1092 gst_element_class_get_metadata (GST_ELEMENT_GET_CLASS (venc),
1093 GST_ELEMENT_METADATA_LONGNAME)))
1094 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_ENCODER,
1095 encoder_name, NULL);
1097 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_CODEC, codec_name,
1100 gst_video_encoder_merge_tags (venc, tags, GST_TAG_MERGE_REPLACE);
1101 gst_tag_list_unref (tags);