]> git.sesse.net Git - ffmpeg/blob - libavcodec/vdpau_hevc.c
Merge commit '87de6ddb7b7674e329d5c96677bd8685bc7f7855'
[ffmpeg] / libavcodec / vdpau_hevc.c
1 /*
2  * MPEG-H Part 2 / HEVC / H.265 HW decode acceleration through VDPAU
3  *
4  * Copyright (c) 2013 Philip Langdale
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include <vdpau/vdpau.h>
24
25 #include "avcodec.h"
26 #include "internal.h"
27 #include "hevc.h"
28 #include "vdpau.h"
29 #include "vdpau_internal.h"
30
31 static int vdpau_hevc_start_frame(AVCodecContext *avctx,
32                                   const uint8_t *buffer, uint32_t size)
33 {
34     HEVCContext *h = avctx->priv_data;
35     HEVCFrame *pic = h->ref;
36     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
37
38     VdpPictureInfoHEVC *info = &pic_ctx->info.hevc;
39
40     const HEVCSPS *sps = h->ps.sps;
41     const HEVCPPS *pps = h->ps.pps;
42     const SliceHeader *sh = &h->sh;
43     const ScalingList *sl = pps->scaling_list_data_present_flag ?
44                             &pps->scaling_list : &sps->scaling_list;
45
46     /* init VdpPictureInfoHEVC */
47
48     /* SPS */
49     info->chroma_format_idc = sps->chroma_format_idc;
50     info->separate_colour_plane_flag = sps->separate_colour_plane_flag;
51     info->pic_width_in_luma_samples = sps->width;
52     info->pic_height_in_luma_samples = sps->height;
53     info->bit_depth_luma_minus8 = sps->bit_depth - 8;
54     info->bit_depth_chroma_minus8 = sps->bit_depth - 8;
55     info->log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_poc_lsb - 4;
56     /** Provides the value corresponding to the nuh_temporal_id of the frame
57         to be decoded. */
58     info->sps_max_dec_pic_buffering_minus1 = sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering - 1;
59     info->log2_min_luma_coding_block_size_minus3 = sps->log2_min_cb_size - 3;
60     info->log2_diff_max_min_luma_coding_block_size = sps->log2_diff_max_min_coding_block_size;
61     info->log2_min_transform_block_size_minus2 = sps->log2_min_tb_size - 2;
62     info->log2_diff_max_min_transform_block_size = sps->log2_max_trafo_size - sps->log2_min_tb_size;
63     info->max_transform_hierarchy_depth_inter = sps->max_transform_hierarchy_depth_inter;
64     info->max_transform_hierarchy_depth_intra = sps->max_transform_hierarchy_depth_intra;
65     info->scaling_list_enabled_flag = sps->scaling_list_enable_flag;
66     /** Scaling lists, in diagonal order, to be used for this frame. */
67     for (size_t i = 0; i < 6; i++) {
68         for (size_t j = 0; j < 16; j++) {
69             /** Scaling List for 4x4 quantization matrix,
70                 indexed as ScalingList4x4[matrixId][i]. */
71             uint8_t pos = 4 * ff_hevc_diag_scan4x4_y[j] + ff_hevc_diag_scan4x4_x[j];
72             info->ScalingList4x4[i][j] = sl->sl[0][i][pos];
73         }
74         for (size_t j = 0; j < 64; j++) {
75             uint8_t pos = 8 * ff_hevc_diag_scan8x8_y[j] + ff_hevc_diag_scan8x8_x[j];
76             /** Scaling List for 8x8 quantization matrix,
77                 indexed as ScalingList8x8[matrixId][i]. */
78             info->ScalingList8x8[i][j] = sl->sl[1][i][pos];
79             /** Scaling List for 16x16 quantization matrix,
80                 indexed as ScalingList16x16[matrixId][i]. */
81             info->ScalingList16x16[i][j] = sl->sl[2][i][pos];
82             if (i < 2) {
83                 /** Scaling List for 32x32 quantization matrix,
84                     indexed as ScalingList32x32[matrixId][i]. */
85                 info->ScalingList32x32[i][j] = sl->sl[3][i * 3][pos];
86             }
87         }
88         /** Scaling List DC Coefficients for 16x16,
89             indexed as ScalingListDCCoeff16x16[matrixId]. */
90         info->ScalingListDCCoeff16x16[i] = sl->sl_dc[0][i];
91         if (i < 2) {
92             /** Scaling List DC Coefficients for 32x32,
93                 indexed as ScalingListDCCoeff32x32[matrixId]. */
94             info->ScalingListDCCoeff32x32[i] = sl->sl_dc[1][i * 3];
95         }
96     }
97     info->amp_enabled_flag = sps->amp_enabled_flag;
98     info->sample_adaptive_offset_enabled_flag = sps->sao_enabled;
99     info->pcm_enabled_flag = sps->pcm_enabled_flag;
100     if (info->pcm_enabled_flag) {
101         /** Only needs to be set if pcm_enabled_flag is set. Ignored otherwise. */
102         info->pcm_sample_bit_depth_luma_minus1 = sps->pcm.bit_depth - 1;
103         /** Only needs to be set if pcm_enabled_flag is set. Ignored otherwise. */
104         info->pcm_sample_bit_depth_chroma_minus1 = sps->pcm.bit_depth_chroma - 1;
105         /** Only needs to be set if pcm_enabled_flag is set. Ignored otherwise. */
106         info->log2_min_pcm_luma_coding_block_size_minus3 = sps->pcm.log2_min_pcm_cb_size - 3;
107         /** Only needs to be set if pcm_enabled_flag is set. Ignored otherwise. */
108         info->log2_diff_max_min_pcm_luma_coding_block_size = sps->pcm.log2_max_pcm_cb_size - sps->pcm.log2_min_pcm_cb_size;
109         /** Only needs to be set if pcm_enabled_flag is set. Ignored otherwise. */
110         info->pcm_loop_filter_disabled_flag = sps->pcm.loop_filter_disable_flag;
111     }
112     /** Per spec, when zero, assume short_term_ref_pic_set_sps_flag
113         is also zero. */
114     info->num_short_term_ref_pic_sets = sps->nb_st_rps;
115     info->long_term_ref_pics_present_flag = sps->long_term_ref_pics_present_flag;
116     /** Only needed if long_term_ref_pics_present_flag is set. Ignored
117         otherwise. */
118     info->num_long_term_ref_pics_sps = sps->num_long_term_ref_pics_sps;
119     info->sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag;
120     info->strong_intra_smoothing_enabled_flag = sps->sps_strong_intra_smoothing_enable_flag;
121     /** @} */
122
123     /** \name HEVC Picture Parameter Set
124      *
125      * Copies of the HEVC Picture Parameter Set bitstream fields.
126      * @{ */
127     info->dependent_slice_segments_enabled_flag = pps->dependent_slice_segments_enabled_flag;
128     info->output_flag_present_flag = pps->output_flag_present_flag;
129     info->num_extra_slice_header_bits = pps->num_extra_slice_header_bits;
130     info->sign_data_hiding_enabled_flag = pps->sign_data_hiding_flag;
131     info->cabac_init_present_flag = pps->cabac_init_present_flag;
132     info->num_ref_idx_l0_default_active_minus1 = pps->num_ref_idx_l0_default_active - 1;
133     info->num_ref_idx_l1_default_active_minus1 = pps->num_ref_idx_l1_default_active - 1;
134     info->init_qp_minus26 = pps->pic_init_qp_minus26;
135     info->constrained_intra_pred_flag = pps->constrained_intra_pred_flag;
136     info->transform_skip_enabled_flag = pps->transform_skip_enabled_flag;
137     info->cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag;
138     /** Only needed if cu_qp_delta_enabled_flag is set. Ignored otherwise. */
139     info->diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth;
140     info->pps_cb_qp_offset = pps->cb_qp_offset;
141     info->pps_cr_qp_offset = pps->cr_qp_offset;
142     info->pps_slice_chroma_qp_offsets_present_flag = pps->pic_slice_level_chroma_qp_offsets_present_flag;
143     info->weighted_pred_flag = pps->weighted_pred_flag;
144     info->weighted_bipred_flag = pps->weighted_bipred_flag;
145     info->transquant_bypass_enabled_flag = pps->transquant_bypass_enable_flag;
146     info->tiles_enabled_flag = pps->tiles_enabled_flag;
147     info->entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag;
148     if (info->tiles_enabled_flag) {
149         /** Only valid if tiles_enabled_flag is set. Ignored otherwise. */
150         info->num_tile_columns_minus1 = pps->num_tile_columns - 1;
151         /** Only valid if tiles_enabled_flag is set. Ignored otherwise. */
152         info->num_tile_rows_minus1 = pps->num_tile_rows - 1;
153         /** Only valid if tiles_enabled_flag is set. Ignored otherwise. */
154         info->uniform_spacing_flag = pps->uniform_spacing_flag;
155         /** Only need to set 0..num_tile_columns_minus1. The struct
156             definition reserves up to the maximum of 20. Invalid values are
157             ignored. */
158         for (ssize_t i = 0; i < pps->num_tile_columns; i++) {
159             info->column_width_minus1[i] = pps->column_width[i] - 1;
160         }
161         /** Only need to set 0..num_tile_rows_minus1. The struct
162           definition reserves up to the maximum of 22. Invalid values are
163           ignored.*/
164         for (ssize_t i = 0; i < pps->num_tile_rows; i++) {
165             info->row_height_minus1[i] = pps->row_height[i] - 1;
166         }
167         /** Only needed if tiles_enabled_flag is set. Invalid values are
168           ignored. */
169         info->loop_filter_across_tiles_enabled_flag = pps->loop_filter_across_tiles_enabled_flag;
170     }
171     info->pps_loop_filter_across_slices_enabled_flag = pps->seq_loop_filter_across_slices_enabled_flag;
172     info->deblocking_filter_control_present_flag = pps->deblocking_filter_control_present_flag;
173     /** Only valid if deblocking_filter_control_present_flag is set. Ignored
174         otherwise. */
175     info->deblocking_filter_override_enabled_flag = pps->deblocking_filter_override_enabled_flag;
176     /** Only valid if deblocking_filter_control_present_flag is set. Ignored
177         otherwise. */
178     info->pps_deblocking_filter_disabled_flag = pps->disable_dbf;
179     /** Only valid if deblocking_filter_control_present_flag is set and
180         pps_deblocking_filter_disabled_flag is not set. Ignored otherwise.*/
181     info->pps_beta_offset_div2 = pps->beta_offset / 2;
182     /** Only valid if deblocking_filter_control_present_flag is set and
183         pps_deblocking_filter_disabled_flag is not set. Ignored otherwise. */
184     info->pps_tc_offset_div2 = pps->tc_offset / 2;
185     info->lists_modification_present_flag = pps->lists_modification_present_flag;
186     info->log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level - 2;
187     info->slice_segment_header_extension_present_flag = pps->slice_header_extension_present_flag;
188
189     /** \name HEVC Slice Segment Header
190      *
191      * Copies of the HEVC Slice Segment Header bitstream fields and calculated
192      * values detailed in the specification.
193      * @{ */
194     /** Set to 1 if nal_unit_type is equal to IDR_W_RADL or IDR_N_LP.
195         Set to zero otherwise. */
196     info->IDRPicFlag = IS_IDR(h);
197     /** Set to 1 if nal_unit_type in the range of BLA_W_LP to
198         RSV_IRAP_VCL23, inclusive. Set to zero otherwise.*/
199     info->RAPPicFlag = IS_IRAP(h);
200     /** See section 7.4.7.1 of the specification. */
201     info->CurrRpsIdx = sps->nb_st_rps;
202     if (sh->short_term_ref_pic_set_sps_flag == 1) {
203         for (size_t i = 0; i < sps->nb_st_rps; i++) {
204             if (sh->short_term_rps == &sps->st_rps[i]) {
205                 info->CurrRpsIdx = i;
206                 break;
207             }
208         }
209     }
210     /** See section 7.4.7.2 of the specification. */
211     info->NumPocTotalCurr = ff_hevc_frame_nb_refs(h);
212     if (sh->short_term_ref_pic_set_sps_flag == 0 && sh->short_term_rps) {
213         /** Corresponds to specification field, NumDeltaPocs[RefRpsIdx].
214             Only applicable when short_term_ref_pic_set_sps_flag == 0.
215             Implementations will ignore this value in other cases. See 7.4.8. */
216         info->NumDeltaPocsOfRefRpsIdx = sh->short_term_rps->rps_idx_num_delta_pocs;
217     }
218     /** Section 7.6.3.1 of the H.265/HEVC Specification defines the syntax of
219         the slice_segment_header. This header contains information that
220         some VDPAU implementations may choose to skip. The VDPAU API
221         requires client applications to track the number of bits used in the
222         slice header for structures associated with short term and long term
223         reference pictures. First, VDPAU requires the number of bits used by
224         the short_term_ref_pic_set array in the slice_segment_header. */
225     info->NumShortTermPictureSliceHeaderBits = sh->short_term_ref_pic_set_size;
226     /** Second, VDPAU requires the number of bits used for long term reference
227         pictures in the slice_segment_header. This is equal to the number
228         of bits used for the contents of the block beginning with
229         "if(long_term_ref_pics_present_flag)". */
230     info->NumLongTermPictureSliceHeaderBits = sh->long_term_ref_pic_set_size;
231     /** @} */
232
233     /** Slice Decoding Process - Picture Order Count */
234     /** The value of PicOrderCntVal of the picture in the access unit
235         containing the SEI message. The picture being decoded. */
236     info->CurrPicOrderCntVal = h->poc;
237
238     /** Slice Decoding Process - Reference Picture Sets */
239     for (size_t i = 0; i < 16; i++) {
240         info->RefPics[i] = VDP_INVALID_HANDLE;
241         info->PicOrderCntVal[i] = 0;
242         info->IsLongTerm[i] = 0;
243     }
244     for (size_t i = 0, j = 0; i < FF_ARRAY_ELEMS(h->DPB); i++) {
245         const HEVCFrame *frame = &h->DPB[i];
246         if (frame != h->ref && (frame->flags & (HEVC_FRAME_FLAG_LONG_REF |
247                                                 HEVC_FRAME_FLAG_SHORT_REF))) {
248             if (j > 16) {
249                 av_log(avctx, AV_LOG_WARNING,
250                      "VDPAU only supports up to 16 references in the DPB. "
251                      "This frame may not be decoded correctly.\n");
252                 break;
253             }
254             /** Array of video reference surfaces.
255                 Set any unused positions to VDP_INVALID_HANDLE. */
256             info->RefPics[j] = ff_vdpau_get_surface_id(frame->frame);
257             /** Array of picture order counts. These correspond to positions
258                 in the RefPics array. */
259             info->PicOrderCntVal[j] = frame->poc;
260             /** Array used to specify whether a particular RefPic is
261                 a long term reference. A value of "1" indicates a long-term
262                 reference. */
263             // XXX: Setting this caused glitches in the nvidia implementation
264             // Always setting it to zero, produces correct results
265             //info->IsLongTerm[j] = frame->flags & HEVC_FRAME_FLAG_LONG_REF;
266             info->IsLongTerm[j] = 0;
267             j++;
268         }
269     }
270     /** Copy of specification field, see Section 8.3.2 of the
271         H.265/HEVC Specification. */
272     info->NumPocStCurrBefore = h->rps[ST_CURR_BEF].nb_refs;
273     if (info->NumPocStCurrBefore > 8) {
274         av_log(avctx, AV_LOG_WARNING,
275              "VDPAU only supports up to 8 references in StCurrBefore. "
276              "This frame may not be decoded correctly.\n");
277         info->NumPocStCurrBefore = 8;
278     }
279     /** Copy of specification field, see Section 8.3.2 of the
280         H.265/HEVC Specification. */
281     info->NumPocStCurrAfter = h->rps[ST_CURR_AFT].nb_refs;
282     if (info->NumPocStCurrAfter > 8) {
283         av_log(avctx, AV_LOG_WARNING,
284              "VDPAU only supports up to 8 references in StCurrAfter. "
285              "This frame may not be decoded correctly.\n");
286         info->NumPocStCurrAfter = 8;
287     }
288     /** Copy of specification field, see Section 8.3.2 of the
289         H.265/HEVC Specification. */
290     info->NumPocLtCurr = h->rps[LT_CURR].nb_refs;
291     if (info->NumPocLtCurr > 8) {
292         av_log(avctx, AV_LOG_WARNING,
293              "VDPAU only supports up to 8 references in LtCurr. "
294              "This frame may not be decoded correctly.\n");
295         info->NumPocLtCurr = 8;
296     }
297     /** Reference Picture Set list, one of the short-term RPS. These
298         correspond to positions in the RefPics array. */
299     for (ssize_t i = 0, j = 0; i < h->rps[ST_CURR_BEF].nb_refs; i++) {
300         HEVCFrame *frame = h->rps[ST_CURR_BEF].ref[i];
301         if (frame) {
302             uint8_t found = 0;
303             uintptr_t id = ff_vdpau_get_surface_id(frame->frame);
304             for (size_t k = 0; k < 16; k++) {
305                 if (id == info->RefPics[k]) {
306                     info->RefPicSetStCurrBefore[j] = k;
307                     j++;
308                     found = 1;
309                     break;
310                 }
311             }
312             if (!found) {
313                 av_log(avctx, AV_LOG_WARNING, "missing surface: %p\n",
314                        (void *)id);
315             }
316         } else {
317             av_log(avctx, AV_LOG_WARNING, "missing STR Before frame: %zd\n", i);
318         }
319     }
320     /** Reference Picture Set list, one of the short-term RPS. These
321         correspond to positions in the RefPics array. */
322     for (ssize_t i = 0, j = 0; i < h->rps[ST_CURR_AFT].nb_refs; i++) {
323         HEVCFrame *frame = h->rps[ST_CURR_AFT].ref[i];
324         if (frame) {
325             uint8_t found = 0;
326             uintptr_t id = ff_vdpau_get_surface_id(frame->frame);
327             for (size_t k = 0; k < 16; k++) {
328                 if (id == info->RefPics[k]) {
329                     info->RefPicSetStCurrAfter[j] = k;
330                     j++;
331                     found = 1;
332                     break;
333                 }
334             }
335             if (!found) {
336                 av_log(avctx, AV_LOG_WARNING, "missing surface: %p\n",
337                        (void *)id);
338             }
339         } else {
340             av_log(avctx, AV_LOG_WARNING, "missing STR After frame: %zd\n", i);
341         }
342     }
343     /** Reference Picture Set list, one of the long-term RPS. These
344         correspond to positions in the RefPics array. */
345     for (ssize_t i = 0, j = 0; i < h->rps[LT_CURR].nb_refs; i++) {
346         HEVCFrame *frame = h->rps[LT_CURR].ref[i];
347         if (frame) {
348             uint8_t found = 0;
349             uintptr_t id = ff_vdpau_get_surface_id(frame->frame);
350             for (size_t k = 0; k < 16; k++) {
351                 if (id == info->RefPics[k]) {
352                     info->RefPicSetLtCurr[j] = k;
353                     j++;
354                     found = 1;
355                     break;
356                 }
357             }
358             if (!found) {
359                 av_log(avctx, AV_LOG_WARNING, "missing surface: %p\n",
360                        (void *)id);
361             }
362         } else {
363             av_log(avctx, AV_LOG_WARNING, "missing LTR frame: %zd\n", i);
364         }
365     }
366
367     return ff_vdpau_common_start_frame(pic_ctx, buffer, size);
368 }
369
370 static const uint8_t start_code_prefix[3] = { 0x00, 0x00, 0x01 };
371
372 static int vdpau_hevc_decode_slice(AVCodecContext *avctx,
373                                    const uint8_t *buffer, uint32_t size)
374 {
375     HEVCContext *h = avctx->priv_data;
376     struct vdpau_picture_context *pic_ctx = h->ref->hwaccel_picture_private;
377     int val;
378
379     val = ff_vdpau_add_buffer(pic_ctx, start_code_prefix, 3);
380     if (val)
381         return val;
382
383     val = ff_vdpau_add_buffer(pic_ctx, buffer, size);
384     if (val)
385         return val;
386
387     return 0;
388 }
389
390 static int vdpau_hevc_end_frame(AVCodecContext *avctx)
391 {
392     HEVCContext *h = avctx->priv_data;
393     struct vdpau_picture_context *pic_ctx = h->ref->hwaccel_picture_private;
394     int val;
395
396     val = ff_vdpau_common_end_frame(avctx, h->ref->frame, pic_ctx);
397     if (val < 0)
398         return val;
399
400     return 0;
401 }
402
403 static int vdpau_hevc_init(AVCodecContext *avctx)
404 {
405     VdpDecoderProfile profile;
406     uint32_t level = avctx->level;
407
408     switch (avctx->profile) {
409     case FF_PROFILE_HEVC_MAIN:
410         profile = VDP_DECODER_PROFILE_HEVC_MAIN;
411         break;
412     case FF_PROFILE_HEVC_MAIN_10:
413         profile = VDP_DECODER_PROFILE_HEVC_MAIN_10;
414         break;
415     case FF_PROFILE_HEVC_MAIN_STILL_PICTURE:
416         profile = VDP_DECODER_PROFILE_HEVC_MAIN_STILL;
417         break;
418     default:
419         return AVERROR(ENOTSUP);
420     }
421
422     return ff_vdpau_common_init(avctx, profile, level);
423 }
424
425 AVHWAccel ff_hevc_vdpau_hwaccel = {
426     .name           = "hevc_vdpau",
427     .type           = AVMEDIA_TYPE_VIDEO,
428     .id             = AV_CODEC_ID_HEVC,
429     .pix_fmt        = AV_PIX_FMT_VDPAU,
430     .start_frame    = vdpau_hevc_start_frame,
431     .end_frame      = vdpau_hevc_end_frame,
432     .decode_slice   = vdpau_hevc_decode_slice,
433     .frame_priv_data_size = sizeof(struct vdpau_picture_context),
434     .init           = vdpau_hevc_init,
435     .uninit         = ff_vdpau_common_uninit,
436     .priv_data_size = sizeof(VDPAUContext),
437 };