1 /* GStreamer
2 * Copyright (C) 2020 Igalia, S.L.
3 * Author: Víctor Jáquez <vjaquez@igalia.com>
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 /**
22 * SECTION:element-vah264dec
23 * @title: vah264dec
24 * @short_description: A VA-API based H264 video decoder
25 *
26 * vah264dec decodes H264 bitstreams to VA surfaces using the
27 * installed and chosen [VA-API](https://01.org/linuxmedia/vaapi)
28 * driver.
29 *
30 * The decoding surfaces can be mapped onto main memory as video
31 * frames.
32 *
33 * ## Example launch line
34 * ```
35 * gst-launch-1.0 filesrc location=big_buck_bunny.mov ! parsebin ! vah264dec ! autovideosink
36 * ```
37 *
38 * Since: 1.18
39 *
40 */
41
42 /* ToDo:
43 *
44 * + mutiview and stereo profiles
45 */
46
47 #ifdef HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50
51 #include "gstvah264dec.h"
52
53 #include "gstvabasedec.h"
54
55 GST_DEBUG_CATEGORY_STATIC (gst_va_h264dec_debug);
56 #ifndef GST_DISABLE_GST_DEBUG
57 #define GST_CAT_DEFAULT gst_va_h264dec_debug
58 #else
59 #define GST_CAT_DEFAULT NULL
60 #endif
61
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)
65
66 typedef struct _GstVaH264Dec GstVaH264Dec;
67 typedef struct _GstVaH264DecClass GstVaH264DecClass;
68
69 struct _GstVaH264DecClass
70 {
71 GstVaBaseDecClass parent_class;
72 };
73
74 struct _GstVaH264Dec
75 {
76 GstVaBaseDec parent;
77
78 GstFlowReturn last_ret;
79
80 gint coded_width;
81 gint coded_height;
82 gint dpb_size;
83
84 /* Used to fill VAPictureParameterBufferH264.ReferenceFrames */
85 GArray *ref_list;
86
87 gboolean interlaced;
88 };
89
90 static GstElementClass *parent_class = NULL;
91
92 /* *INDENT-OFF* */
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 }");
97 /* *INDENT-ON* */
98
99 static const gchar *sink_caps_str = "video/x-h264";
100
101 static GstFlowReturn
gst_va_h264_dec_end_picture(GstH264Decoder * decoder,GstH264Picture * picture)102 gst_va_h264_dec_end_picture (GstH264Decoder * decoder, GstH264Picture * picture)
103 {
104 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
105 GstVaDecodePicture *va_pic;
106
107 GST_LOG_OBJECT (base, "end picture %p, (poc %d)",
108 picture, picture->pic_order_cnt);
109
110 va_pic = gst_h264_picture_get_user_data (picture);
111
112 if (!gst_va_decoder_decode (base->decoder, va_pic))
113 return GST_FLOW_ERROR;
114
115 return GST_FLOW_OK;
116 }
117
118 static GstFlowReturn
gst_va_h264_dec_output_picture(GstH264Decoder * decoder,GstVideoCodecFrame * frame,GstH264Picture * picture)119 gst_va_h264_dec_output_picture (GstH264Decoder * decoder,
120 GstVideoCodecFrame * frame, GstH264Picture * picture)
121 {
122 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
123 GstVaH264Dec *self = GST_VA_H264_DEC (decoder);
124
125 GST_LOG_OBJECT (self,
126 "Outputting picture %p (poc %d)", picture, picture->pic_order_cnt);
127
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;
132 }
133
134 if (base->copy_frames)
135 gst_va_base_dec_copy_output_buffer (base, frame);
136
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;
141
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);
146 }
147
148 gst_h264_picture_unref (picture);
149
150 return gst_video_decoder_finish_frame (GST_VIDEO_DECODER (self), frame);
151 }
152
153 static void
_init_vaapi_pic(VAPictureH264 * va_picture)154 _init_vaapi_pic (VAPictureH264 * va_picture)
155 {
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;
161 }
162
163 static void
_fill_vaapi_pic(VAPictureH264 * va_picture,GstH264Picture * picture,gboolean merge_other_field)164 _fill_vaapi_pic (VAPictureH264 * va_picture, GstH264Picture * picture,
165 gboolean merge_other_field)
166 {
167 GstVaDecodePicture *va_pic;
168
169 va_pic = gst_h264_picture_get_user_data (picture);
170
171 if (!va_pic) {
172 _init_vaapi_pic (va_picture);
173 return;
174 }
175
176 va_picture->picture_id = gst_va_decode_picture_get_surface (va_pic);
177 va_picture->flags = 0;
178
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;
182 } else {
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;
186 }
187
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;
192 break;
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;
197 } else {
198 va_picture->flags |= VA_PICTURE_H264_TOP_FIELD;
199 va_picture->BottomFieldOrderCnt = 0;
200 }
201 va_picture->TopFieldOrderCnt = picture->top_field_order_cnt;
202 break;
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;
207 } else {
208 va_picture->flags |= VA_PICTURE_H264_BOTTOM_FIELD;
209 va_picture->TopFieldOrderCnt = 0;
210 }
211 va_picture->BottomFieldOrderCnt = picture->bottom_field_order_cnt;
212 break;
213 default:
214 va_picture->TopFieldOrderCnt = 0;
215 va_picture->BottomFieldOrderCnt = 0;
216 break;
217 }
218 }
219
220 /* fill the VA API reference picture lists from the GstCodec reference
221 * picture list */
222 static void
_fill_ref_pic_list(VAPictureH264 va_reflist[32],GArray * reflist,GstH264Picture * current_picture)223 _fill_ref_pic_list (VAPictureH264 va_reflist[32], GArray * reflist,
224 GstH264Picture * current_picture)
225 {
226 guint i;
227
228 for (i = 0; i < reflist->len; i++) {
229 GstH264Picture *picture = g_array_index (reflist, GstH264Picture *, i);
230
231 if (picture) {
232 _fill_vaapi_pic (&va_reflist[i], picture,
233 GST_H264_PICTURE_IS_FRAME (current_picture));
234 } else {
235 /* list might include null picture if reference picture was missing */
236 _init_vaapi_pic (&va_reflist[i]);
237 }
238 }
239
240 for (; i < 32; i++)
241 _init_vaapi_pic (&va_reflist[i]);
242 }
243
244 static void
_fill_pred_weight_table(GstH264SliceHdr * header,VASliceParameterBufferH264 * slice_param)245 _fill_pred_weight_table (GstH264SliceHdr * header,
246 VASliceParameterBufferH264 * slice_param)
247 {
248 GstH264PPS *pps;
249 GstH264SPS *sps;
250 guint num_weight_tables = 0;
251 gint i, j;
252
253 pps = header->pps;
254 sps = pps->sequence;
255
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;
261
262 if (num_weight_tables == 0)
263 return;
264
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;
269
270 /* VA API also wants the inferred (default) values, not only what is
271 * available in the bitstream (7.4.3.2). */
272
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];
279 }
280
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];
289 }
290 }
291 }
292
293 if (num_weight_tables == 1)
294 return;
295
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];
302 }
303
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];
312 }
313 }
314 }
315 }
316
317 static inline guint
_get_slice_data_bit_offset(GstH264SliceHdr * header,guint nal_header_bytes)318 _get_slice_data_bit_offset (GstH264SliceHdr * header, guint nal_header_bytes)
319 {
320 guint epb_count;
321
322 epb_count = header->n_emulation_prevention_bytes;
323 return 8 * nal_header_bytes + header->header_size - epb_count * 8;
324 }
325
326 static GstFlowReturn
gst_va_h264_dec_decode_slice(GstH264Decoder * decoder,GstH264Picture * picture,GstH264Slice * slice,GArray * ref_pic_list0,GArray * ref_pic_list1)327 gst_va_h264_dec_decode_slice (GstH264Decoder * decoder,
328 GstH264Picture * picture, GstH264Slice * slice, GArray * ref_pic_list0,
329 GArray * ref_pic_list1)
330 {
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;
336
337 /* *INDENT-OFF* */
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,
354 };
355 /* *INDENT-ON* */
356
357 _fill_ref_pic_list (slice_param.RefPicList0, ref_pic_list0, picture);
358 _fill_ref_pic_list (slice_param.RefPicList1, ref_pic_list1, picture);
359
360 _fill_pred_weight_table (header, &slice_param);
361
362 va_pic = gst_h264_picture_get_user_data (picture);
363
364 if (!gst_va_decoder_add_slice_buffer (base->decoder, va_pic, &slice_param,
365 sizeof (slice_param), slice->nalu.data + slice->nalu.offset,
366 slice->nalu.size)) {
367 return GST_FLOW_ERROR;
368 }
369
370 return GST_FLOW_OK;
371 }
372
373 static GstFlowReturn
gst_va_h264_dec_start_picture(GstH264Decoder * decoder,GstH264Picture * picture,GstH264Slice * slice,GstH264Dpb * dpb)374 gst_va_h264_dec_start_picture (GstH264Decoder * decoder,
375 GstH264Picture * picture, GstH264Slice * slice, GstH264Dpb * dpb)
376 {
377 GstVaH264Dec *self = GST_VA_H264_DEC (decoder);
378 GstH264PPS *pps;
379 GstH264SPS *sps;
380 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
381 GstVaDecodePicture *va_pic;
382 VAIQMatrixBufferH264 iq_matrix = { 0, };
383 VAPictureParameterBufferH264 pic_param;
384 guint i, n;
385 GArray *ref_list = self->ref_list;
386
387 va_pic = gst_h264_picture_get_user_data (picture);
388
389 pps = slice->header.pps;
390 sps = pps->sequence;
391
392 /* *INDENT-OFF* */
393 pic_param = (VAPictureParameterBufferH264) {
394 /* .CurrPic */
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,
403 .seq_fields.bits = {
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,
416 },
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,
421 .pic_fields.bits = {
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,
433 },
434 .frame_num = slice->header.frame_num,
435 };
436 /* *INDENT-ON* */
437
438 _fill_vaapi_pic (&pic_param.CurrPic, picture, FALSE);
439
440 /* reference frames */
441 {
442 guint ref_frame_idx = 0;
443 g_array_set_size (ref_list, 0);
444
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);
449 }
450 g_array_set_size (ref_list, 0);
451
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);
456 }
457 g_array_set_size (ref_list, 0);
458
459 for (; ref_frame_idx < 16; ref_frame_idx++)
460 _init_vaapi_pic (&pic_param.ReferenceFrames[ref_frame_idx]);
461 }
462
463 if (!gst_va_decoder_add_param_buffer (base->decoder, va_pic,
464 VAPictureParameterBufferType, &pic_param, sizeof (pic_param)))
465 return GST_FLOW_ERROR;
466
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]);
471 }
472
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
475 * more details */
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]);
480 }
481
482 if (!gst_va_decoder_add_param_buffer (base->decoder, va_pic,
483 VAIQMatrixBufferType, &iq_matrix, sizeof (iq_matrix)))
484 return GST_FLOW_ERROR;
485
486 return GST_FLOW_OK;
487 }
488
489 static GstFlowReturn
gst_va_h264_dec_new_picture(GstH264Decoder * decoder,GstVideoCodecFrame * frame,GstH264Picture * picture)490 gst_va_h264_dec_new_picture (GstH264Decoder * decoder,
491 GstVideoCodecFrame * frame, GstH264Picture * picture)
492 {
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);
497
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;
502 }
503 }
504
505 self->last_ret = gst_video_decoder_allocate_output_frame (vdec, frame);
506 if (self->last_ret != GST_FLOW_OK)
507 goto error;
508
509 pic = gst_va_decode_picture_new (base->decoder, frame->output_buffer);
510
511 gst_h264_picture_set_user_data (picture, pic,
512 (GDestroyNotify) gst_va_decode_picture_free);
513
514 GST_LOG_OBJECT (self, "New va decode picture %p - %#x", pic,
515 gst_va_decode_picture_get_surface (pic));
516
517 return GST_FLOW_OK;
518
519 error:
520 {
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;
525 }
526 }
527
528 static GstFlowReturn
gst_va_h264_dec_new_field_picture(GstH264Decoder * decoder,const GstH264Picture * first_field,GstH264Picture * second_field)529 gst_va_h264_dec_new_field_picture (GstH264Decoder * decoder,
530 const GstH264Picture * first_field, GstH264Picture * second_field)
531 {
532 GstVaDecodePicture *first_pic, *second_pic;
533 GstVaH264Dec *self = GST_VA_H264_DEC (decoder);
534 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
535
536 first_pic = gst_h264_picture_get_user_data ((GstH264Picture *) first_field);
537 if (!first_pic)
538 return GST_FLOW_ERROR;
539
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);
543
544 GST_LOG_OBJECT (self, "New va decode picture %p - %#x", second_pic,
545 gst_va_decode_picture_get_surface (second_pic));
546
547 return GST_FLOW_OK;
548 }
549
550 static inline guint
_get_num_views(const GstH264SPS * sps)551 _get_num_views (const GstH264SPS * sps)
552 {
553 return 1 + (sps->extension_type == GST_H264_NAL_EXTENSION_MVC ?
554 sps->extension.mvc.num_views_minus1 : 0);
555 }
556
557 static guint
_get_rtformat(GstVaH264Dec * self,guint8 bit_depth_luma,guint8 chroma_format_idc)558 _get_rtformat (GstVaH264Dec * self, guint8 bit_depth_luma,
559 guint8 chroma_format_idc)
560 {
561 switch (bit_depth_luma) {
562 case 10:
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;
567 else
568 return VA_RT_FORMAT_YUV420_10;
569 break;
570 case 8:
571 if (chroma_format_idc == 3)
572 return VA_RT_FORMAT_YUV444;
573 if (chroma_format_idc == 2)
574 return VA_RT_FORMAT_YUV422;
575 else
576 return VA_RT_FORMAT_YUV420;
577 break;
578 default:
579 GST_ERROR_OBJECT (self, "Unsupported chroma format: %d "
580 "(with depth luma: %d)", chroma_format_idc, bit_depth_luma);
581 return 0;
582 }
583 }
584
585 /* *INDENT-OFF* */
586 static const struct
587 {
588 GstH264Profile profile_idc;
589 VAProfile va_profile;
590 } profile_map[] = {
591 #define P(idc, va) { G_PASTE (GST_H264_PROFILE_, idc), G_PASTE (VAProfileH264, va) }
592 /* P (BASELINE, ), */
593 P (MAIN, Main),
594 /* P (EXTENDED, ), */
595 P (HIGH, High),
596 /* P (HIGH10, ), */
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, ), */
603 #undef P
604 };
605 /* *INDENT-ON* */
606
607 static VAProfile
_get_profile(GstVaH264Dec * self,const GstH264SPS * sps,gint max_dpb_size)608 _get_profile (GstVaH264Dec * self, const GstH264SPS * sps, gint max_dpb_size)
609 {
610 GstVaBaseDec *base = GST_VA_BASE_DEC (self);
611 VAProfile profiles[4];
612 gint i = 0, j;
613
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;
617 break;
618 }
619 }
620
621 switch (sps->profile_idc) {
622 case GST_H264_PROFILE_BASELINE:
623 {
624 GstH264DecoderCompliance compliance = GST_H264_DECODER_COMPLIANCE_STRICT;
625
626 g_object_get (G_OBJECT (self), "compliance", &compliance, NULL);
627
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;
634 }
635
636 break;
637 }
638 case GST_H264_PROFILE_EXTENDED:
639 if (sps->constraint_set1_flag) { /* A.2.2 (main profile) */
640 profiles[i++] = VAProfileH264Main;
641 }
642 break;
643 case GST_H264_PROFILE_MULTIVIEW_HIGH:
644 if (_get_num_views (sps) == 2) {
645 profiles[i++] = VAProfileH264StereoHigh;
646 }
647 if (max_dpb_size <= 16 /* && i965 driver */ ) {
648 profiles[i++] = VAProfileH264MultiviewHigh;
649 }
650 default:
651 break;
652 }
653
654 for (j = 0; j < i && j < G_N_ELEMENTS (profiles); j++) {
655 if (gst_va_decoder_has_profile (base->decoder, profiles[j]))
656 return profiles[j];
657 }
658
659 GST_ERROR_OBJECT (self, "Unsupported profile: %d", sps->profile_idc);
660
661 return VAProfileNone;
662 }
663
664 static GstFlowReturn
gst_va_h264_dec_new_sequence(GstH264Decoder * decoder,const GstH264SPS * sps,gint max_dpb_size)665 gst_va_h264_dec_new_sequence (GstH264Decoder * decoder, const GstH264SPS * sps,
666 gint max_dpb_size)
667 {
668 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
669 GstVaH264Dec *self = GST_VA_H264_DEC (decoder);
670 VAProfile profile;
671 gint display_width;
672 gint display_height;
673 gint padding_left, padding_right, padding_top, padding_bottom;
674 guint rt_format;
675 gboolean negotiation_needed = FALSE;
676 gboolean interlaced;
677
678 if (self->dpb_size < max_dpb_size)
679 self->dpb_size = max_dpb_size;
680
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;
688 } else {
689 display_width = sps->width;
690 display_height = sps->height;
691 padding_left = padding_right = padding_top = padding_bottom = 0;
692 }
693
694 profile = _get_profile (self, sps, max_dpb_size);
695 if (profile == VAProfileNone)
696 return GST_FLOW_NOT_NEGOTIATED;
697
698 rt_format = _get_rtformat (self, sps->bit_depth_luma_minus8 + 8,
699 sps->chroma_format_idc);
700 if (rt_format == 0)
701 return GST_FLOW_NOT_NEGOTIATED;
702
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;
709
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,
713 self->coded_height);
714 }
715
716 if (base->width != display_width || base->height != display_height) {
717 base->width = display_width;
718 base->height = display_height;
719
720 negotiation_needed = TRUE;
721 GST_INFO_OBJECT (self, "Resolution changed to %dx%d", base->width,
722 base->height);
723 }
724
725 interlaced = !sps->frame_mbs_only_flag;
726 if (self->interlaced != interlaced) {
727 self->interlaced = interlaced;
728
729 negotiation_needed = TRUE;
730 GST_INFO_OBJECT (self, "Interlaced mode changed to %d", interlaced);
731 }
732
733 base->need_valign = base->width < self->coded_width
734 || base->height < self->coded_height;
735 if (base->need_valign) {
736 /* *INDENT-OFF* */
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);
744 }
745 base->valign = (GstVideoAlignment) {
746 .padding_left = padding_left,
747 .padding_right = padding_right,
748 .padding_top = padding_top,
749 .padding_bottom = padding_bottom,
750 };
751 /* *INDENT-ON* */
752 }
753
754 base->min_buffers = self->dpb_size + 4; /* dpb size + scratch surfaces */
755
756 base->need_negotiation = negotiation_needed;
757
758 return GST_FLOW_OK;
759 }
760
761 static GstCaps *
_complete_sink_caps(GstCaps * sinkcaps)762 _complete_sink_caps (GstCaps * sinkcaps)
763 {
764 GstCaps *caps = gst_caps_copy (sinkcaps);
765 GValue val = G_VALUE_INIT;
766 const gchar *streamformat[] = { "avc", "avc3", "byte-stream" };
767 gint i;
768
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);
773
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;
777
778 g_value_init (&v, G_TYPE_STRING);
779 g_value_set_string (&v, streamformat[i]);
780 gst_value_list_append_value (&val, &v);
781 g_value_unset (&v);
782 }
783 gst_caps_set_value (caps, "stream-format", &val);
784 g_value_unset (&val);
785
786 return caps;
787 }
788
789 static GstCaps *
gst_va_h264_dec_getcaps(GstVideoDecoder * decoder,GstCaps * filter)790 gst_va_h264_dec_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
791 {
792 GstCaps *sinkcaps, *caps = NULL, *tmp;
793 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
794
795 if (base->decoder)
796 caps = gst_va_decoder_get_sinkpad_caps (base->decoder);
797
798 if (caps) {
799 sinkcaps = _complete_sink_caps (caps);
800 gst_caps_unref (caps);
801 if (filter) {
802 tmp = gst_caps_intersect_full (filter, sinkcaps,
803 GST_CAPS_INTERSECT_FIRST);
804 gst_caps_unref (sinkcaps);
805 caps = tmp;
806 } else {
807 caps = sinkcaps;
808 }
809 GST_LOG_OBJECT (base, "Returning caps %" GST_PTR_FORMAT, caps);
810 } else {
811 caps = gst_video_decoder_proxy_getcaps (decoder, NULL, filter);
812 }
813
814 return caps;
815 }
816
817 static gboolean
gst_va_h264_dec_negotiate(GstVideoDecoder * decoder)818 gst_va_h264_dec_negotiate (GstVideoDecoder * decoder)
819 {
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);
825
826 /* Ignore downstream renegotiation request. */
827 if (!base->need_negotiation)
828 return TRUE;
829
830 base->need_negotiation = FALSE;
831
832 if (gst_va_decoder_is_open (base->decoder)
833 && !gst_va_decoder_close (base->decoder))
834 return FALSE;
835
836 if (!gst_va_decoder_open (base->decoder, base->profile, base->rt_format))
837 return FALSE;
838
839 if (!gst_va_decoder_set_frame_size (base->decoder, self->coded_width,
840 self->coded_height))
841 return FALSE;
842
843 if (base->output_state)
844 gst_video_codec_state_unref (base->output_state);
845
846 gst_va_base_dec_get_preferred_format_and_caps_features (base, &format,
847 &capsfeatures);
848
849 base->output_state =
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;
854
855 base->output_state->caps = gst_video_info_to_caps (&base->output_state->info);
856 if (capsfeatures)
857 gst_caps_set_features_simple (base->output_state->caps, capsfeatures);
858
859 GST_INFO_OBJECT (self, "Negotiated caps %" GST_PTR_FORMAT,
860 base->output_state->caps);
861
862 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
863 }
864
865 static void
gst_va_h264_dec_dispose(GObject * object)866 gst_va_h264_dec_dispose (GObject * object)
867 {
868 GstVaH264Dec *self = GST_VA_H264_DEC (object);
869
870 gst_va_base_dec_close (GST_VIDEO_DECODER (object));
871 g_clear_pointer (&self->ref_list, g_array_unref);
872
873 G_OBJECT_CLASS (parent_class)->dispose (object);
874 }
875
876 static void
gst_va_h264_dec_class_init(gpointer g_class,gpointer class_data)877 gst_va_h264_dec_class_init (gpointer g_class, gpointer class_data)
878 {
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;
885 gchar *long_name;
886
887 if (cdata->description) {
888 long_name = g_strdup_printf ("VA-API H.264 Decoder in %s",
889 cdata->description);
890 } else {
891 long_name = g_strdup ("VA-API H.264 Decoder");
892 }
893
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>");
898
899 sink_doc_caps = gst_caps_from_string (sink_caps_str);
900 src_doc_caps = gst_caps_from_string (src_caps_str);
901
902 parent_class = g_type_class_peek_parent (g_class);
903
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);
907
908 gobject_class->dispose = gst_va_h264_dec_dispose;
909
910 decoder_class->getcaps = GST_DEBUG_FUNCPTR (gst_va_h264_dec_getcaps);
911 decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_va_h264_dec_negotiate);
912
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);
917
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);
928
929 g_free (long_name);
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);
934 g_free (cdata);
935 }
936
937 static void
gst_va_h264_dec_init(GTypeInstance * instance,gpointer g_class)938 gst_va_h264_dec_init (GTypeInstance * instance, gpointer g_class)
939 {
940 GstVaH264Dec *self = GST_VA_H264_DEC (instance);
941
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),
944 TRUE);
945
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);
950 }
951
952 static gpointer
_register_debug_category(gpointer data)953 _register_debug_category (gpointer data)
954 {
955 GST_DEBUG_CATEGORY_INIT (gst_va_h264dec_debug, "vah264dec", 0,
956 "VA h264 decoder");
957
958 return NULL;
959 }
960
961 gboolean
gst_va_h264_dec_register(GstPlugin * plugin,GstVaDevice * device,GstCaps * sink_caps,GstCaps * src_caps,guint rank)962 gst_va_h264_dec_register (GstPlugin * plugin, GstVaDevice * device,
963 GstCaps * sink_caps, GstCaps * src_caps, guint rank)
964 {
965 static GOnce debug_once = G_ONCE_INIT;
966 GType type;
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,
972 };
973 struct CData *cdata;
974 gboolean ret;
975 gchar *type_name, *feature_name;
976
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);
981
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);
987
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);
992
993 type_info.class_data = cdata;
994
995 type_name = g_strdup ("GstVaH264Dec");
996 feature_name = g_strdup ("vah264dec");
997
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);
1003 g_free (type_name);
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;
1008
1009 /* lower rank for non-first device */
1010 if (rank > 0)
1011 rank--;
1012 }
1013
1014 g_once (&debug_once, _register_debug_category, NULL);
1015
1016 type = g_type_register_static (GST_TYPE_H264_DECODER,
1017 type_name, &type_info, 0);
1018
1019 ret = gst_element_register (plugin, feature_name, rank, type);
1020
1021 g_free (type_name);
1022 g_free (feature_name);
1023
1024 return ret;
1025 }
1026