2 * Copyright (C) 2020 Igalia, S.L.
3 * Author: Víctor Jáquez <vjaquez@igalia.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.
22 * SECTION:element-vah264dec
24 * @short_description: A VA-API based H264 video decoder
26 * vah264dec decodes H264 bitstreams to VA surfaces using the
27 * installed and chosen [VA-API](https://01.org/linuxmedia/vaapi)
30 * The decoding surfaces can be mapped onto main memory as video
33 * ## Example launch line
35 * gst-launch-1.0 filesrc location=big_buck_bunny.mov ! parsebin ! vah264dec ! autovideosink
44 * + mutiview and stereo profiles
51 #include "gstvah264dec.h"
53 #include "gstvabasedec.h"
55 GST_DEBUG_CATEGORY_STATIC (gst_va_h264dec_debug);
56 #ifndef GST_DISABLE_GST_DEBUG
57 #define GST_CAT_DEFAULT gst_va_h264dec_debug
59 #define GST_CAT_DEFAULT NULL
62 #define GST_VA_H264_DEC(obj) ((GstVaH264Dec *) obj)
63 #define GST_VA_H264_DEC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), G_TYPE_FROM_INSTANCE (obj), GstVaH264DecClass))
64 #define GST_VA_H264_DEC_CLASS(klass) ((GstVaH264DecClass *) klass)
66 typedef struct _GstVaH264Dec GstVaH264Dec;
67 typedef struct _GstVaH264DecClass GstVaH264DecClass;
69 struct _GstVaH264DecClass
71 GstVaBaseDecClass parent_class;
78 GstFlowReturn last_ret;
84 /* Used to fill VAPictureParameterBufferH264.ReferenceFrames */
90 static GstElementClass *parent_class = NULL;
93 static const gchar *src_caps_str =
94 GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_VA,
95 "{ NV12, P010_10LE }") " ;"
96 GST_VIDEO_CAPS_MAKE ("{ NV12, P010_10LE }");
99 static const gchar *sink_caps_str = "video/x-h264";
102 gst_va_h264_dec_end_picture (GstH264Decoder * decoder, GstH264Picture * picture)
104 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
105 GstVaDecodePicture *va_pic;
107 GST_LOG_OBJECT (base, "end picture %p, (poc %d)",
108 picture, picture->pic_order_cnt);
110 va_pic = gst_h264_picture_get_user_data (picture);
112 if (!gst_va_decoder_decode (base->decoder, va_pic))
113 return GST_FLOW_ERROR;
119 gst_va_h264_dec_output_picture (GstH264Decoder * decoder,
120 GstVideoCodecFrame * frame, GstH264Picture * picture)
122 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
123 GstVaH264Dec *self = GST_VA_H264_DEC (decoder);
125 GST_LOG_OBJECT (self,
126 "Outputting picture %p (poc %d)", picture, picture->pic_order_cnt);
128 if (self->last_ret != GST_FLOW_OK) {
129 gst_h264_picture_unref (picture);
130 gst_video_decoder_drop_frame (GST_VIDEO_DECODER (self), frame);
131 return self->last_ret;
134 if (base->copy_frames)
135 gst_va_base_dec_copy_output_buffer (base, frame);
137 if (picture->buffer_flags != 0) {
138 gboolean interlaced =
139 (picture->buffer_flags & GST_VIDEO_BUFFER_FLAG_INTERLACED) != 0;
140 gboolean tff = (picture->buffer_flags & GST_VIDEO_BUFFER_FLAG_TFF) != 0;
142 GST_TRACE_OBJECT (self,
143 "apply buffer flags 0x%x (interlaced %d, top-field-first %d)",
144 picture->buffer_flags, interlaced, tff);
145 GST_BUFFER_FLAG_SET (frame->output_buffer, picture->buffer_flags);
148 gst_h264_picture_unref (picture);
150 return gst_video_decoder_finish_frame (GST_VIDEO_DECODER (self), frame);
154 _init_vaapi_pic (VAPictureH264 * va_picture)
156 va_picture->picture_id = VA_INVALID_ID;
157 va_picture->frame_idx = 0;
158 va_picture->flags = VA_PICTURE_H264_INVALID;
159 va_picture->TopFieldOrderCnt = 0;
160 va_picture->BottomFieldOrderCnt = 0;
164 _fill_vaapi_pic (VAPictureH264 * va_picture, GstH264Picture * picture,
165 gboolean merge_other_field)
167 GstVaDecodePicture *va_pic;
169 va_pic = gst_h264_picture_get_user_data (picture);
172 _init_vaapi_pic (va_picture);
176 va_picture->picture_id = gst_va_decode_picture_get_surface (va_pic);
177 va_picture->flags = 0;
179 if (GST_H264_PICTURE_IS_LONG_TERM_REF (picture)) {
180 va_picture->flags |= VA_PICTURE_H264_LONG_TERM_REFERENCE;
181 va_picture->frame_idx = picture->long_term_frame_idx;
183 if (GST_H264_PICTURE_IS_SHORT_TERM_REF (picture))
184 va_picture->flags |= VA_PICTURE_H264_SHORT_TERM_REFERENCE;
185 va_picture->frame_idx = picture->frame_num;
188 switch (picture->field) {
189 case GST_H264_PICTURE_FIELD_FRAME:
190 va_picture->TopFieldOrderCnt = picture->top_field_order_cnt;
191 va_picture->BottomFieldOrderCnt = picture->bottom_field_order_cnt;
193 case GST_H264_PICTURE_FIELD_TOP_FIELD:
194 if (merge_other_field && picture->other_field) {
195 va_picture->BottomFieldOrderCnt =
196 picture->other_field->bottom_field_order_cnt;
198 va_picture->flags |= VA_PICTURE_H264_TOP_FIELD;
199 va_picture->BottomFieldOrderCnt = 0;
201 va_picture->TopFieldOrderCnt = picture->top_field_order_cnt;
203 case GST_H264_PICTURE_FIELD_BOTTOM_FIELD:
204 if (merge_other_field && picture->other_field) {
205 va_picture->TopFieldOrderCnt =
206 picture->other_field->top_field_order_cnt;
208 va_picture->flags |= VA_PICTURE_H264_BOTTOM_FIELD;
209 va_picture->TopFieldOrderCnt = 0;
211 va_picture->BottomFieldOrderCnt = picture->bottom_field_order_cnt;
214 va_picture->TopFieldOrderCnt = 0;
215 va_picture->BottomFieldOrderCnt = 0;
220 /* fill the VA API reference picture lists from the GstCodec reference
223 _fill_ref_pic_list (VAPictureH264 va_reflist[32], GArray * reflist,
224 GstH264Picture * current_picture)
228 for (i = 0; i < reflist->len; i++) {
229 GstH264Picture *picture = g_array_index (reflist, GstH264Picture *, i);
232 _fill_vaapi_pic (&va_reflist[i], picture,
233 GST_H264_PICTURE_IS_FRAME (current_picture));
235 /* list might include null picture if reference picture was missing */
236 _init_vaapi_pic (&va_reflist[i]);
241 _init_vaapi_pic (&va_reflist[i]);
245 _fill_pred_weight_table (GstH264SliceHdr * header,
246 VASliceParameterBufferH264 * slice_param)
250 guint num_weight_tables = 0;
256 if (pps->weighted_pred_flag
257 && (GST_H264_IS_P_SLICE (header) || GST_H264_IS_SP_SLICE (header)))
258 num_weight_tables = 1;
259 else if (pps->weighted_bipred_idc == 1 && GST_H264_IS_B_SLICE (header))
260 num_weight_tables = 2;
262 if (num_weight_tables == 0)
265 slice_param->luma_log2_weight_denom =
266 header->pred_weight_table.luma_log2_weight_denom;
267 slice_param->chroma_log2_weight_denom =
268 header->pred_weight_table.chroma_log2_weight_denom;
270 /* VA API also wants the inferred (default) values, not only what is
271 * available in the bitstream (7.4.3.2). */
273 slice_param->luma_weight_l0_flag = 1;
274 for (i = 0; i <= slice_param->num_ref_idx_l0_active_minus1; i++) {
275 slice_param->luma_weight_l0[i] =
276 header->pred_weight_table.luma_weight_l0[i];
277 slice_param->luma_offset_l0[i] =
278 header->pred_weight_table.luma_offset_l0[i];
281 slice_param->chroma_weight_l0_flag = sps->chroma_array_type != 0;
282 if (slice_param->chroma_weight_l0_flag) {
283 for (i = 0; i <= slice_param->num_ref_idx_l0_active_minus1; i++) {
284 for (j = 0; j < 2; j++) {
285 slice_param->chroma_weight_l0[i][j] =
286 header->pred_weight_table.chroma_weight_l0[i][j];
287 slice_param->chroma_offset_l0[i][j] =
288 header->pred_weight_table.chroma_offset_l0[i][j];
293 if (num_weight_tables == 1)
296 slice_param->luma_weight_l1_flag = 1;
297 for (i = 0; i <= slice_param->num_ref_idx_l1_active_minus1; i++) {
298 slice_param->luma_weight_l1[i] =
299 header->pred_weight_table.luma_weight_l1[i];
300 slice_param->luma_offset_l1[i] =
301 header->pred_weight_table.luma_offset_l1[i];
304 slice_param->chroma_weight_l1_flag = sps->chroma_array_type != 0;
305 if (slice_param->chroma_weight_l1_flag) {
306 for (i = 0; i <= slice_param->num_ref_idx_l1_active_minus1; i++) {
307 for (j = 0; j < 2; j++) {
308 slice_param->chroma_weight_l1[i][j] =
309 header->pred_weight_table.chroma_weight_l1[i][j];
310 slice_param->chroma_offset_l1[i][j] =
311 header->pred_weight_table.chroma_offset_l1[i][j];
318 _get_slice_data_bit_offset (GstH264SliceHdr * header, guint nal_header_bytes)
322 epb_count = header->n_emulation_prevention_bytes;
323 return 8 * nal_header_bytes + header->header_size - epb_count * 8;
327 gst_va_h264_dec_decode_slice (GstH264Decoder * decoder,
328 GstH264Picture * picture, GstH264Slice * slice, GArray * ref_pic_list0,
329 GArray * ref_pic_list1)
331 GstH264SliceHdr *header = &slice->header;
332 GstH264NalUnit *nalu = &slice->nalu;
333 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
334 GstVaDecodePicture *va_pic;
335 VASliceParameterBufferH264 slice_param;
338 slice_param = (VASliceParameterBufferH264) {
339 .slice_data_size = nalu->size,
340 .slice_data_offset = 0,
341 .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
342 .slice_data_bit_offset =
343 _get_slice_data_bit_offset (header, nalu->header_bytes),
344 .first_mb_in_slice = header->first_mb_in_slice,
345 .slice_type = header->type % 5,
346 .direct_spatial_mv_pred_flag = header->direct_spatial_mv_pred_flag,
347 .cabac_init_idc = header->cabac_init_idc,
348 .slice_qp_delta = header->slice_qp_delta,
349 .disable_deblocking_filter_idc = header->disable_deblocking_filter_idc,
350 .slice_alpha_c0_offset_div2 = header->slice_alpha_c0_offset_div2,
351 .slice_beta_offset_div2 = header->slice_beta_offset_div2,
352 .num_ref_idx_l0_active_minus1 = header->num_ref_idx_l0_active_minus1,
353 .num_ref_idx_l1_active_minus1 = header->num_ref_idx_l1_active_minus1,
357 _fill_ref_pic_list (slice_param.RefPicList0, ref_pic_list0, picture);
358 _fill_ref_pic_list (slice_param.RefPicList1, ref_pic_list1, picture);
360 _fill_pred_weight_table (header, &slice_param);
362 va_pic = gst_h264_picture_get_user_data (picture);
364 if (!gst_va_decoder_add_slice_buffer (base->decoder, va_pic, &slice_param,
365 sizeof (slice_param), slice->nalu.data + slice->nalu.offset,
367 return GST_FLOW_ERROR;
374 gst_va_h264_dec_start_picture (GstH264Decoder * decoder,
375 GstH264Picture * picture, GstH264Slice * slice, GstH264Dpb * dpb)
377 GstVaH264Dec *self = GST_VA_H264_DEC (decoder);
380 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
381 GstVaDecodePicture *va_pic;
382 VAIQMatrixBufferH264 iq_matrix = { 0, };
383 VAPictureParameterBufferH264 pic_param;
385 GArray *ref_list = self->ref_list;
387 va_pic = gst_h264_picture_get_user_data (picture);
389 pps = slice->header.pps;
393 pic_param = (VAPictureParameterBufferH264) {
395 /* .ReferenceFrames */
396 .picture_width_in_mbs_minus1 = sps->pic_width_in_mbs_minus1,
397 .picture_height_in_mbs_minus1 =
398 ((sps->pic_height_in_map_units_minus1 + 1) <<
399 !sps->frame_mbs_only_flag) -1,
400 .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
401 .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
402 .num_ref_frames = sps->num_ref_frames,
404 .chroma_format_idc = sps->chroma_format_idc,
405 .residual_colour_transform_flag = sps->separate_colour_plane_flag,
406 .gaps_in_frame_num_value_allowed_flag =
407 sps->gaps_in_frame_num_value_allowed_flag,
408 .frame_mbs_only_flag = sps->frame_mbs_only_flag,
409 .mb_adaptive_frame_field_flag = sps->mb_adaptive_frame_field_flag,
410 .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
411 .MinLumaBiPredSize8x8 = sps->level_idc >= 31, /* A.3.3.2 */
412 .log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4,
413 .pic_order_cnt_type = sps->pic_order_cnt_type,
414 .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4,
415 .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
417 .pic_init_qp_minus26 = pps->pic_init_qp_minus26,
418 .pic_init_qs_minus26 = pps->pic_init_qs_minus26,
419 .chroma_qp_index_offset = pps->chroma_qp_index_offset,
420 .second_chroma_qp_index_offset = pps->second_chroma_qp_index_offset,
422 .entropy_coding_mode_flag = pps->entropy_coding_mode_flag,
423 .weighted_pred_flag = pps->weighted_pred_flag,
424 .weighted_bipred_idc = pps->weighted_bipred_idc,
425 .transform_8x8_mode_flag = pps->transform_8x8_mode_flag,
426 .field_pic_flag = slice->header.field_pic_flag,
427 .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
428 .pic_order_present_flag = pps->pic_order_present_flag,
429 .deblocking_filter_control_present_flag =
430 pps->deblocking_filter_control_present_flag,
431 .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present_flag,
432 .reference_pic_flag = picture->nal_ref_idc != 0,
434 .frame_num = slice->header.frame_num,
438 _fill_vaapi_pic (&pic_param.CurrPic, picture, FALSE);
440 /* reference frames */
442 guint ref_frame_idx = 0;
443 g_array_set_size (ref_list, 0);
445 gst_h264_dpb_get_pictures_short_term_ref (dpb, FALSE, FALSE, ref_list);
446 for (i = 0; ref_frame_idx < 16 && i < ref_list->len; i++) {
447 GstH264Picture *pic = g_array_index (ref_list, GstH264Picture *, i);
448 _fill_vaapi_pic (&pic_param.ReferenceFrames[ref_frame_idx++], pic, TRUE);
450 g_array_set_size (ref_list, 0);
452 gst_h264_dpb_get_pictures_long_term_ref (dpb, FALSE, ref_list);
453 for (i = 0; ref_frame_idx < 16 && i < ref_list->len; i++) {
454 GstH264Picture *pic = g_array_index (ref_list, GstH264Picture *, i);
455 _fill_vaapi_pic (&pic_param.ReferenceFrames[ref_frame_idx++], pic, TRUE);
457 g_array_set_size (ref_list, 0);
459 for (; ref_frame_idx < 16; ref_frame_idx++)
460 _init_vaapi_pic (&pic_param.ReferenceFrames[ref_frame_idx]);
463 if (!gst_va_decoder_add_param_buffer (base->decoder, va_pic,
464 VAPictureParameterBufferType, &pic_param, sizeof (pic_param)))
465 return GST_FLOW_ERROR;
467 /* there are always 6 4x4 scaling lists */
468 for (i = 0; i < 6; i++) {
469 gst_h264_quant_matrix_4x4_get_raster_from_zigzag (iq_matrix.ScalingList4x4
470 [i], pps->scaling_lists_4x4[i]);
473 /* We need the first 2 entries (Y intra and Y inter for YCbCr 4:2:2 and
474 * less, and the full 6 entries for 4:4:4, see Table 7-2 of the spec for
476 n = (pps->sequence->chroma_format_idc == 3) ? 6 : 2;
477 for (i = 0; i < n; i++) {
478 gst_h264_quant_matrix_8x8_get_raster_from_zigzag (iq_matrix.ScalingList8x8
479 [i], pps->scaling_lists_8x8[i]);
482 if (!gst_va_decoder_add_param_buffer (base->decoder, va_pic,
483 VAIQMatrixBufferType, &iq_matrix, sizeof (iq_matrix)))
484 return GST_FLOW_ERROR;
490 gst_va_h264_dec_new_picture (GstH264Decoder * decoder,
491 GstVideoCodecFrame * frame, GstH264Picture * picture)
493 GstVaH264Dec *self = GST_VA_H264_DEC (decoder);
494 GstVaDecodePicture *pic;
495 GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
496 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
498 if (base->need_negotiation) {
499 if (!gst_video_decoder_negotiate (vdec)) {
500 GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
501 return GST_FLOW_NOT_NEGOTIATED;
505 self->last_ret = gst_video_decoder_allocate_output_frame (vdec, frame);
506 if (self->last_ret != GST_FLOW_OK)
509 pic = gst_va_decode_picture_new (base->decoder, frame->output_buffer);
511 gst_h264_picture_set_user_data (picture, pic,
512 (GDestroyNotify) gst_va_decode_picture_free);
514 GST_LOG_OBJECT (self, "New va decode picture %p - %#x", pic,
515 gst_va_decode_picture_get_surface (pic));
521 GST_WARNING_OBJECT (self,
522 "Failed to allocated output buffer, return %s",
523 gst_flow_get_name (self->last_ret));
524 return self->last_ret;
529 gst_va_h264_dec_new_field_picture (GstH264Decoder * decoder,
530 const GstH264Picture * first_field, GstH264Picture * second_field)
532 GstVaDecodePicture *first_pic, *second_pic;
533 GstVaH264Dec *self = GST_VA_H264_DEC (decoder);
534 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
536 first_pic = gst_h264_picture_get_user_data ((GstH264Picture *) first_field);
538 return GST_FLOW_ERROR;
540 second_pic = gst_va_decode_picture_new (base->decoder, first_pic->gstbuffer);
541 gst_h264_picture_set_user_data (second_field, second_pic,
542 (GDestroyNotify) gst_va_decode_picture_free);
544 GST_LOG_OBJECT (self, "New va decode picture %p - %#x", second_pic,
545 gst_va_decode_picture_get_surface (second_pic));
551 _get_num_views (const GstH264SPS * sps)
553 return 1 + (sps->extension_type == GST_H264_NAL_EXTENSION_MVC ?
554 sps->extension.mvc.num_views_minus1 : 0);
558 _get_rtformat (GstVaH264Dec * self, guint8 bit_depth_luma,
559 guint8 chroma_format_idc)
561 switch (bit_depth_luma) {
563 if (chroma_format_idc == 3)
564 return VA_RT_FORMAT_YUV444_10;
565 if (chroma_format_idc == 2)
566 return VA_RT_FORMAT_YUV422_10;
568 return VA_RT_FORMAT_YUV420_10;
571 if (chroma_format_idc == 3)
572 return VA_RT_FORMAT_YUV444;
573 if (chroma_format_idc == 2)
574 return VA_RT_FORMAT_YUV422;
576 return VA_RT_FORMAT_YUV420;
579 GST_ERROR_OBJECT (self, "Unsupported chroma format: %d "
580 "(with depth luma: %d)", chroma_format_idc, bit_depth_luma);
588 GstH264Profile profile_idc;
589 VAProfile va_profile;
591 #define P(idc, va) { G_PASTE (GST_H264_PROFILE_, idc), G_PASTE (VAProfileH264, va) }
592 /* P (BASELINE, ), */
594 /* P (EXTENDED, ), */
597 /* P (HIGH_422, ), */
598 /* P (HIGH_444, ), */
599 P (MULTIVIEW_HIGH, MultiviewHigh),
600 P (STEREO_HIGH, StereoHigh),
601 /* P (SCALABLE_BASELINE, ), */
602 /* P (SCALABLE_HIGH, ), */
608 _get_profile (GstVaH264Dec * self, const GstH264SPS * sps, gint max_dpb_size)
610 GstVaBaseDec *base = GST_VA_BASE_DEC (self);
611 VAProfile profiles[4];
614 for (j = 0; j < G_N_ELEMENTS (profile_map); j++) {
615 if (profile_map[j].profile_idc == sps->profile_idc) {
616 profiles[i++] = profile_map[j].va_profile;
621 switch (sps->profile_idc) {
622 case GST_H264_PROFILE_BASELINE:
624 GstH264DecoderCompliance compliance = GST_H264_DECODER_COMPLIANCE_STRICT;
626 g_object_get (G_OBJECT (self), "compliance", &compliance, NULL);
628 /* A.2 compliant or not strict */
629 if (sps->constraint_set0_flag || sps->constraint_set1_flag
630 || sps->constraint_set2_flag
631 || compliance != GST_H264_DECODER_COMPLIANCE_STRICT) {
632 profiles[i++] = VAProfileH264ConstrainedBaseline;
633 profiles[i++] = VAProfileH264Main;
638 case GST_H264_PROFILE_EXTENDED:
639 if (sps->constraint_set1_flag) { /* A.2.2 (main profile) */
640 profiles[i++] = VAProfileH264Main;
643 case GST_H264_PROFILE_MULTIVIEW_HIGH:
644 if (_get_num_views (sps) == 2) {
645 profiles[i++] = VAProfileH264StereoHigh;
647 if (max_dpb_size <= 16 /* && i965 driver */ ) {
648 profiles[i++] = VAProfileH264MultiviewHigh;
654 for (j = 0; j < i && j < G_N_ELEMENTS (profiles); j++) {
655 if (gst_va_decoder_has_profile (base->decoder, profiles[j]))
659 GST_ERROR_OBJECT (self, "Unsupported profile: %d", sps->profile_idc);
661 return VAProfileNone;
665 gst_va_h264_dec_new_sequence (GstH264Decoder * decoder, const GstH264SPS * sps,
668 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
669 GstVaH264Dec *self = GST_VA_H264_DEC (decoder);
673 gint padding_left, padding_right, padding_top, padding_bottom;
675 gboolean negotiation_needed = FALSE;
678 if (self->dpb_size < max_dpb_size)
679 self->dpb_size = max_dpb_size;
681 if (sps->frame_cropping_flag) {
682 display_width = sps->crop_rect_width;
683 display_height = sps->crop_rect_height;
684 padding_left = sps->crop_rect_x;
685 padding_right = sps->width - sps->crop_rect_x - display_width;
686 padding_top = sps->crop_rect_y;
687 padding_bottom = sps->height - sps->crop_rect_y - display_height;
689 display_width = sps->width;
690 display_height = sps->height;
691 padding_left = padding_right = padding_top = padding_bottom = 0;
694 profile = _get_profile (self, sps, max_dpb_size);
695 if (profile == VAProfileNone)
696 return GST_FLOW_NOT_NEGOTIATED;
698 rt_format = _get_rtformat (self, sps->bit_depth_luma_minus8 + 8,
699 sps->chroma_format_idc);
701 return GST_FLOW_NOT_NEGOTIATED;
703 if (!gst_va_decoder_config_is_equal (base->decoder, profile,
704 rt_format, sps->width, sps->height)) {
705 base->profile = profile;
706 base->rt_format = rt_format;
707 self->coded_width = sps->width;
708 self->coded_height = sps->height;
710 negotiation_needed = TRUE;
711 GST_INFO_OBJECT (self, "Format changed to %s [%x] (%dx%d)",
712 gst_va_profile_name (profile), rt_format, self->coded_width,
716 if (base->width != display_width || base->height != display_height) {
717 base->width = display_width;
718 base->height = display_height;
720 negotiation_needed = TRUE;
721 GST_INFO_OBJECT (self, "Resolution changed to %dx%d", base->width,
725 interlaced = !sps->frame_mbs_only_flag;
726 if (self->interlaced != interlaced) {
727 self->interlaced = interlaced;
729 negotiation_needed = TRUE;
730 GST_INFO_OBJECT (self, "Interlaced mode changed to %d", interlaced);
733 base->need_valign = base->width < self->coded_width
734 || base->height < self->coded_height;
735 if (base->need_valign) {
737 if (base->valign.padding_left != padding_left ||
738 base->valign.padding_right != padding_right ||
739 base->valign.padding_top != padding_top ||
740 base->valign.padding_bottom != padding_bottom) {
741 negotiation_needed = TRUE;
742 GST_INFO_OBJECT (self, "crop rect changed to (%d,%d)-->(%d,%d)",
743 padding_left, padding_top, padding_right, padding_bottom);
745 base->valign = (GstVideoAlignment) {
746 .padding_left = padding_left,
747 .padding_right = padding_right,
748 .padding_top = padding_top,
749 .padding_bottom = padding_bottom,
754 base->min_buffers = self->dpb_size + 4; /* dpb size + scratch surfaces */
756 base->need_negotiation = negotiation_needed;
762 _complete_sink_caps (GstCaps * sinkcaps)
764 GstCaps *caps = gst_caps_copy (sinkcaps);
765 GValue val = G_VALUE_INIT;
766 const gchar *streamformat[] = { "avc", "avc3", "byte-stream" };
769 g_value_init (&val, G_TYPE_STRING);
770 g_value_set_string (&val, "au");
771 gst_caps_set_value (caps, "alignment", &val);
772 g_value_unset (&val);
774 gst_value_list_init (&val, G_N_ELEMENTS (streamformat));
775 for (i = 0; i < G_N_ELEMENTS (streamformat); i++) {
776 GValue v = G_VALUE_INIT;
778 g_value_init (&v, G_TYPE_STRING);
779 g_value_set_string (&v, streamformat[i]);
780 gst_value_list_append_value (&val, &v);
783 gst_caps_set_value (caps, "stream-format", &val);
784 g_value_unset (&val);
790 gst_va_h264_dec_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
792 GstCaps *sinkcaps, *caps = NULL, *tmp;
793 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
796 caps = gst_va_decoder_get_sinkpad_caps (base->decoder);
799 sinkcaps = _complete_sink_caps (caps);
800 gst_caps_unref (caps);
802 tmp = gst_caps_intersect_full (filter, sinkcaps,
803 GST_CAPS_INTERSECT_FIRST);
804 gst_caps_unref (sinkcaps);
809 GST_LOG_OBJECT (base, "Returning caps %" GST_PTR_FORMAT, caps);
811 caps = gst_video_decoder_proxy_getcaps (decoder, NULL, filter);
818 gst_va_h264_dec_negotiate (GstVideoDecoder * decoder)
820 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
821 GstVaH264Dec *self = GST_VA_H264_DEC (decoder);
822 GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
823 GstCapsFeatures *capsfeatures = NULL;
824 GstH264Decoder *h264dec = GST_H264_DECODER (decoder);
826 /* Ignore downstream renegotiation request. */
827 if (!base->need_negotiation)
830 base->need_negotiation = FALSE;
832 if (gst_va_decoder_is_open (base->decoder)
833 && !gst_va_decoder_close (base->decoder))
836 if (!gst_va_decoder_open (base->decoder, base->profile, base->rt_format))
839 if (!gst_va_decoder_set_frame_size (base->decoder, self->coded_width,
843 if (base->output_state)
844 gst_video_codec_state_unref (base->output_state);
846 gst_va_base_dec_get_preferred_format_and_caps_features (base, &format,
850 gst_video_decoder_set_output_state (decoder, format,
851 base->width, base->height, h264dec->input_state);
852 if (self->interlaced)
853 base->output_state->info.interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
855 base->output_state->caps = gst_video_info_to_caps (&base->output_state->info);
857 gst_caps_set_features_simple (base->output_state->caps, capsfeatures);
859 GST_INFO_OBJECT (self, "Negotiated caps %" GST_PTR_FORMAT,
860 base->output_state->caps);
862 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
866 gst_va_h264_dec_dispose (GObject * object)
868 GstVaH264Dec *self = GST_VA_H264_DEC (object);
870 gst_va_base_dec_close (GST_VIDEO_DECODER (object));
871 g_clear_pointer (&self->ref_list, g_array_unref);
873 G_OBJECT_CLASS (parent_class)->dispose (object);
877 gst_va_h264_dec_class_init (gpointer g_class, gpointer class_data)
879 GstCaps *src_doc_caps, *sink_doc_caps;
880 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
881 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
882 GstH264DecoderClass *h264decoder_class = GST_H264_DECODER_CLASS (g_class);
883 GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (g_class);
884 struct CData *cdata = class_data;
887 if (cdata->description) {
888 long_name = g_strdup_printf ("VA-API H.264 Decoder in %s",
891 long_name = g_strdup ("VA-API H.264 Decoder");
894 gst_element_class_set_metadata (element_class, long_name,
895 "Codec/Decoder/Video/Hardware",
896 "VA-API based H.264 video decoder",
897 "Víctor Jáquez <vjaquez@igalia.com>");
899 sink_doc_caps = gst_caps_from_string (sink_caps_str);
900 src_doc_caps = gst_caps_from_string (src_caps_str);
902 parent_class = g_type_class_peek_parent (g_class);
904 gst_va_base_dec_class_init (GST_VA_BASE_DEC_CLASS (g_class), H264,
905 cdata->render_device_path, cdata->sink_caps, cdata->src_caps,
906 src_doc_caps, sink_doc_caps);
908 gobject_class->dispose = gst_va_h264_dec_dispose;
910 decoder_class->getcaps = GST_DEBUG_FUNCPTR (gst_va_h264_dec_getcaps);
911 decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_va_h264_dec_negotiate);
913 h264decoder_class->new_sequence =
914 GST_DEBUG_FUNCPTR (gst_va_h264_dec_new_sequence);
915 h264decoder_class->decode_slice =
916 GST_DEBUG_FUNCPTR (gst_va_h264_dec_decode_slice);
918 h264decoder_class->new_picture =
919 GST_DEBUG_FUNCPTR (gst_va_h264_dec_new_picture);
920 h264decoder_class->output_picture =
921 GST_DEBUG_FUNCPTR (gst_va_h264_dec_output_picture);
922 h264decoder_class->start_picture =
923 GST_DEBUG_FUNCPTR (gst_va_h264_dec_start_picture);
924 h264decoder_class->end_picture =
925 GST_DEBUG_FUNCPTR (gst_va_h264_dec_end_picture);
926 h264decoder_class->new_field_picture =
927 GST_DEBUG_FUNCPTR (gst_va_h264_dec_new_field_picture);
930 g_free (cdata->description);
931 g_free (cdata->render_device_path);
932 gst_caps_unref (cdata->src_caps);
933 gst_caps_unref (cdata->sink_caps);
938 gst_va_h264_dec_init (GTypeInstance * instance, gpointer g_class)
940 GstVaH264Dec *self = GST_VA_H264_DEC (instance);
942 gst_va_base_dec_init (GST_VA_BASE_DEC (instance), GST_CAT_DEFAULT);
943 gst_h264_decoder_set_process_ref_pic_lists (GST_H264_DECODER (instance),
946 self->ref_list = g_array_sized_new (FALSE, TRUE,
947 sizeof (GstH264Picture *), 16);
948 g_array_set_clear_func (self->ref_list,
949 (GDestroyNotify) gst_h264_picture_clear);
953 _register_debug_category (gpointer data)
955 GST_DEBUG_CATEGORY_INIT (gst_va_h264dec_debug, "vah264dec", 0,
962 gst_va_h264_dec_register (GstPlugin * plugin, GstVaDevice * device,
963 GstCaps * sink_caps, GstCaps * src_caps, guint rank)
965 static GOnce debug_once = G_ONCE_INIT;
967 GTypeInfo type_info = {
968 .class_size = sizeof (GstVaH264DecClass),
969 .class_init = gst_va_h264_dec_class_init,
970 .instance_size = sizeof (GstVaH264Dec),
971 .instance_init = gst_va_h264_dec_init,
975 gchar *type_name, *feature_name;
977 g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
978 g_return_val_if_fail (GST_IS_VA_DEVICE (device), FALSE);
979 g_return_val_if_fail (GST_IS_CAPS (sink_caps), FALSE);
980 g_return_val_if_fail (GST_IS_CAPS (src_caps), FALSE);
982 cdata = g_new (struct CData, 1);
983 cdata->description = NULL;
984 cdata->render_device_path = g_strdup (device->render_device_path);
985 cdata->sink_caps = _complete_sink_caps (sink_caps);
986 cdata->src_caps = gst_caps_ref (src_caps);
988 /* class data will be leaked if the element never gets instantiated */
989 GST_MINI_OBJECT_FLAG_SET (cdata->sink_caps,
990 GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
991 GST_MINI_OBJECT_FLAG_SET (src_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
993 type_info.class_data = cdata;
995 type_name = g_strdup ("GstVaH264Dec");
996 feature_name = g_strdup ("vah264dec");
998 /* The first decoder to be registered should use a constant name,
999 * like vah264dec, for any additional decoders, we create unique
1000 * names, using inserting the render device name. */
1001 if (g_type_from_name (type_name)) {
1002 gchar *basename = g_path_get_basename (device->render_device_path);
1004 g_free (feature_name);
1005 type_name = g_strdup_printf ("GstVa%sH264Dec", basename);
1006 feature_name = g_strdup_printf ("va%sh264dec", basename);
1007 cdata->description = basename;
1009 /* lower rank for non-first device */
1014 g_once (&debug_once, _register_debug_category, NULL);
1016 type = g_type_register_static (GST_TYPE_H264_DECODER,
1017 type_name, &type_info, 0);
1019 ret = gst_element_register (plugin, feature_name, rank, type);
1022 g_free (feature_name);