dxva2_hevc: fix 32x32 scaling lists
[ffmpeg.git] / libavcodec / dxva2_hevc.c
1 /*
2  * DXVA2 HEVC HW acceleration.
3  *
4  * copyright (c) 2014 - 2015 Hendrik Leppkes
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavutil/avassert.h"
24
25 #include "dxva2_internal.h"
26 #include "hevc.h"
27
28 #define MAX_SLICES 256
29
30 struct hevc_dxva2_picture_context {
31     DXVA_PicParams_HEVC   pp;
32     DXVA_Qmatrix_HEVC     qm;
33     unsigned              slice_count;
34     DXVA_Slice_HEVC_Short slice_short[MAX_SLICES];
35     const uint8_t         *bitstream;
36     unsigned              bitstream_size;
37 };
38
39 static void fill_picture_entry(DXVA_PicEntry_HEVC *pic,
40                                unsigned index, unsigned flag)
41 {
42     av_assert0((index & 0x7f) == index && (flag & 0x01) == flag);
43     pic->bPicEntry = index | (flag << 7);
44 }
45
46 static int get_refpic_index(const DXVA_PicParams_HEVC *pp, int surface_index)
47 {
48     int i;
49     for (i = 0; i < FF_ARRAY_ELEMS(pp->RefPicList); i++) {
50         if ((pp->RefPicList[i].bPicEntry & 0x7f) == surface_index)
51           return i;
52     }
53     return 0xff;
54 }
55
56 static void fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, const HEVCContext *h,
57                                     DXVA_PicParams_HEVC *pp)
58 {
59     const HEVCFrame *current_picture = h->ref;
60     int i, j, k;
61
62     memset(pp, 0, sizeof(*pp));
63
64     pp->PicWidthInMinCbsY  = h->sps->min_cb_width;
65     pp->PicHeightInMinCbsY = h->sps->min_cb_height;
66
67     pp->wFormatAndSequenceInfoFlags = (h->sps->chroma_format_idc          <<  0) |
68                                       (h->sps->separate_colour_plane_flag <<  2) |
69                                       ((h->sps->bit_depth - 8)            <<  3) |
70                                       ((h->sps->bit_depth - 8)            <<  6) |
71                                       ((h->sps->log2_max_poc_lsb - 4)     <<  9) |
72                                       (0                                  << 13) |
73                                       (0                                  << 14) |
74                                       (0                                  << 15);
75
76     fill_picture_entry(&pp->CurrPic, ff_dxva2_get_surface_index(avctx, ctx, current_picture->frame), 0);
77
78     pp->sps_max_dec_pic_buffering_minus1         = h->sps->temporal_layer[h->sps->max_sub_layers - 1].max_dec_pic_buffering - 1;
79     pp->log2_min_luma_coding_block_size_minus3   = h->sps->log2_min_cb_size - 3;
80     pp->log2_diff_max_min_luma_coding_block_size = h->sps->log2_diff_max_min_coding_block_size;
81     pp->log2_min_transform_block_size_minus2     = h->sps->log2_min_tb_size - 2;
82     pp->log2_diff_max_min_transform_block_size   = h->sps->log2_max_trafo_size  - h->sps->log2_min_tb_size;
83     pp->max_transform_hierarchy_depth_inter      = h->sps->max_transform_hierarchy_depth_inter;
84     pp->max_transform_hierarchy_depth_intra      = h->sps->max_transform_hierarchy_depth_intra;
85     pp->num_short_term_ref_pic_sets              = h->sps->nb_st_rps;
86     pp->num_long_term_ref_pics_sps               = h->sps->num_long_term_ref_pics_sps;
87
88     pp->num_ref_idx_l0_default_active_minus1     = h->pps->num_ref_idx_l0_default_active - 1;
89     pp->num_ref_idx_l1_default_active_minus1     = h->pps->num_ref_idx_l1_default_active - 1;
90     pp->init_qp_minus26                          = h->pps->pic_init_qp_minus26;
91
92     if (h->sh.short_term_ref_pic_set_sps_flag == 0 && h->sh.short_term_rps) {
93         pp->ucNumDeltaPocsOfRefRpsIdx            = h->sh.short_term_rps->num_delta_pocs;
94         pp->wNumBitsForShortTermRPSInSlice       = h->sh.short_term_ref_pic_set_size;
95     }
96
97     pp->dwCodingParamToolFlags = (h->sps->scaling_list_enable_flag               <<  0) |
98                                  (h->sps->amp_enabled_flag                       <<  1) |
99                                  (h->sps->sao_enabled                            <<  2) |
100                                  (h->sps->pcm_enabled_flag                       <<  3) |
101                                  ((h->sps->pcm_enabled_flag ? (h->sps->pcm.bit_depth - 1) : 0)            <<  4) |
102                                  ((h->sps->pcm_enabled_flag ? (h->sps->pcm.bit_depth_chroma - 1) : 0)     <<  8) |
103                                  ((h->sps->pcm_enabled_flag ? (h->sps->pcm.log2_min_pcm_cb_size - 3) : 0) << 12) |
104                                  ((h->sps->pcm_enabled_flag ? (h->sps->pcm.log2_max_pcm_cb_size - h->sps->pcm.log2_min_pcm_cb_size) : 0) << 14) |
105                                  (h->sps->pcm.loop_filter_disable_flag           << 16) |
106                                  (h->sps->long_term_ref_pics_present_flag        << 17) |
107                                  (h->sps->sps_temporal_mvp_enabled_flag          << 18) |
108                                  (h->sps->sps_strong_intra_smoothing_enable_flag << 19) |
109                                  (h->pps->dependent_slice_segments_enabled_flag  << 20) |
110                                  (h->pps->output_flag_present_flag               << 21) |
111                                  (h->pps->num_extra_slice_header_bits            << 22) |
112                                  (h->pps->sign_data_hiding_flag                  << 25) |
113                                  (h->pps->cabac_init_present_flag                << 26) |
114                                  (0                                              << 27);
115
116     pp->dwCodingSettingPicturePropertyFlags = (h->pps->constrained_intra_pred_flag                <<  0) |
117                                               (h->pps->transform_skip_enabled_flag                <<  1) |
118                                               (h->pps->cu_qp_delta_enabled_flag                   <<  2) |
119                                               (h->pps->pic_slice_level_chroma_qp_offsets_present_flag <<  3) |
120                                               (h->pps->weighted_pred_flag                         <<  4) |
121                                               (h->pps->weighted_bipred_flag                       <<  5) |
122                                               (h->pps->transquant_bypass_enable_flag              <<  6) |
123                                               (h->pps->tiles_enabled_flag                         <<  7) |
124                                               (h->pps->entropy_coding_sync_enabled_flag           <<  8) |
125                                               (h->pps->uniform_spacing_flag                       <<  9) |
126                                               ((h->pps->tiles_enabled_flag ? h->pps->loop_filter_across_tiles_enabled_flag : 0) << 10) |
127                                               (h->pps->seq_loop_filter_across_slices_enabled_flag << 11) |
128                                               (h->pps->deblocking_filter_override_enabled_flag    << 12) |
129                                               (h->pps->disable_dbf                                << 13) |
130                                               (h->pps->lists_modification_present_flag            << 14) |
131                                               (h->pps->slice_header_extension_present_flag        << 15) |
132                                               (IS_IRAP(h)                                         << 16) |
133                                               (IS_IDR(h)                                          << 17) |
134                                               /* IntraPicFlag */
135                                               (IS_IRAP(h)                                         << 18) |
136                                               (0                                                  << 19);
137     pp->pps_cb_qp_offset            = h->pps->cb_qp_offset;
138     pp->pps_cr_qp_offset            = h->pps->cr_qp_offset;
139     if (h->pps->tiles_enabled_flag) {
140         pp->num_tile_columns_minus1 = h->pps->num_tile_columns - 1;
141         pp->num_tile_rows_minus1    = h->pps->num_tile_rows - 1;
142
143         if (!h->pps->uniform_spacing_flag) {
144             for (i = 0; i < h->pps->num_tile_columns; i++)
145                 pp->column_width_minus1[i] = h->pps->column_width[i] - 1;
146
147             for (i = 0; i < h->pps->num_tile_rows; i++)
148                 pp->row_height_minus1[i] = h->pps->row_height[i] - 1;
149         }
150     }
151
152     pp->diff_cu_qp_delta_depth           = h->pps->diff_cu_qp_delta_depth;
153     pp->pps_beta_offset_div2             = h->pps->beta_offset / 2;
154     pp->pps_tc_offset_div2               = h->pps->tc_offset / 2;
155     pp->log2_parallel_merge_level_minus2 = h->pps->log2_parallel_merge_level - 2;
156     pp->CurrPicOrderCntVal               = h->poc;
157
158     // empty the lists
159     memset(&pp->RefPicList, 0xff, sizeof(pp->RefPicList));
160     memset(&pp->RefPicSetStCurrBefore, 0xff, sizeof(pp->RefPicSetStCurrBefore));
161     memset(&pp->RefPicSetStCurrAfter, 0xff, sizeof(pp->RefPicSetStCurrAfter));
162     memset(&pp->RefPicSetLtCurr, 0xff, sizeof(pp->RefPicSetLtCurr));
163
164     // fill RefPicList from the DPB
165     for (i = 0, j = 0; i < FF_ARRAY_ELEMS(h->DPB); i++) {
166         const HEVCFrame *frame = &h->DPB[i];
167         if (frame != current_picture && (frame->flags & (HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF))) {
168             fill_picture_entry(&pp->RefPicList[j], ff_dxva2_get_surface_index(avctx, ctx, frame->frame), !!(frame->flags & HEVC_FRAME_FLAG_LONG_REF));
169             pp->PicOrderCntValList[j] = frame->poc;
170             j++;
171         }
172     }
173
174     #define DO_REF_LIST(ref_idx, ref_list) { \
175         const RefPicList *rpl = &h->rps[ref_idx]; \
176         av_assert0(rpl->nb_refs <= FF_ARRAY_ELEMS(pp->ref_list)); \
177         for (j = 0, k = 0; j < rpl->nb_refs; j++) { \
178             if (rpl->ref[j]) { \
179                 pp->ref_list[k] = get_refpic_index(pp, ff_dxva2_get_surface_index(avctx, ctx, rpl->ref[j]->frame)); \
180                 k++; \
181             } \
182         } \
183     }
184
185     // Fill short term and long term lists
186     DO_REF_LIST(ST_CURR_BEF, RefPicSetStCurrBefore);
187     DO_REF_LIST(ST_CURR_AFT, RefPicSetStCurrAfter);
188     DO_REF_LIST(LT_CURR, RefPicSetLtCurr);
189
190     pp->StatusReportFeedbackNumber = 1 + DXVA_CONTEXT_REPORT_ID(avctx, ctx)++;
191 }
192
193 static void fill_scaling_lists(AVDXVAContext *ctx, const HEVCContext *h, DXVA_Qmatrix_HEVC *qm)
194 {
195     unsigned i, j, pos;
196     const ScalingList *sl = h->pps->scaling_list_data_present_flag ?
197                             &h->pps->scaling_list : &h->sps->scaling_list;
198
199     memset(qm, 0, sizeof(*qm));
200     for (i = 0; i < 6; i++) {
201         for (j = 0; j < 16; j++) {
202             pos = 4 * ff_hevc_diag_scan4x4_y[j] + ff_hevc_diag_scan4x4_x[j];
203             qm->ucScalingLists0[i][j] = sl->sl[0][i][pos];
204         }
205
206         for (j = 0; j < 64; j++) {
207             pos = 8 * ff_hevc_diag_scan8x8_y[j] + ff_hevc_diag_scan8x8_x[j];
208             qm->ucScalingLists1[i][j] = sl->sl[1][i][pos];
209             qm->ucScalingLists2[i][j] = sl->sl[2][i][pos];
210
211             if (i < 2)
212                 qm->ucScalingLists3[i][j] = sl->sl[3][i * 3][pos];
213         }
214
215         qm->ucScalingListDCCoefSizeID2[i] = sl->sl_dc[0][i];
216         if (i < 2)
217             qm->ucScalingListDCCoefSizeID3[i] = sl->sl_dc[1][i * 3];
218     }
219 }
220
221 static void fill_slice_short(DXVA_Slice_HEVC_Short *slice,
222                              unsigned position, unsigned size)
223 {
224     memset(slice, 0, sizeof(*slice));
225     slice->BSNALunitDataLocation = position;
226     slice->SliceBytesInBuffer    = size;
227     slice->wBadSliceChopping     = 0;
228 }
229
230 static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
231                                              DECODER_BUFFER_DESC *bs,
232                                              DECODER_BUFFER_DESC *sc)
233 {
234     const HEVCContext *h = avctx->priv_data;
235     AVDXVAContext *ctx = avctx->hwaccel_context;
236     const HEVCFrame *current_picture = h->ref;
237     struct hevc_dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
238     DXVA_Slice_HEVC_Short *slice = NULL;
239     void     *dxva_data_ptr;
240     uint8_t  *dxva_data, *current, *end;
241     unsigned dxva_size;
242     void     *slice_data;
243     unsigned slice_size;
244     unsigned padding;
245     unsigned i;
246     unsigned type;
247
248     /* Create an annex B bitstream buffer with only slice NAL and finalize slice */
249 #if CONFIG_D3D11VA
250     if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
251         type = D3D11_VIDEO_DECODER_BUFFER_BITSTREAM;
252         if (FAILED(ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
253                                                        D3D11VA_CONTEXT(ctx)->decoder,
254                                                        type,
255                                                        &dxva_size, &dxva_data_ptr)))
256             return -1;
257     }
258 #endif
259 #if CONFIG_DXVA2
260     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
261         type = DXVA2_BitStreamDateBufferType;
262         if (FAILED(IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder,
263                                                   type,
264                                                   &dxva_data_ptr, &dxva_size)))
265             return -1;
266     }
267 #endif
268
269     dxva_data = dxva_data_ptr;
270     current = dxva_data;
271     end = dxva_data + dxva_size;
272
273     for (i = 0; i < ctx_pic->slice_count; i++) {
274         static const uint8_t start_code[] = { 0, 0, 1 };
275         static const unsigned start_code_size = sizeof(start_code);
276         unsigned position, size;
277
278         slice = &ctx_pic->slice_short[i];
279
280         position = slice->BSNALunitDataLocation;
281         size     = slice->SliceBytesInBuffer;
282         if (start_code_size + size > end - current) {
283             av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
284             break;
285         }
286
287         slice->BSNALunitDataLocation = current - dxva_data;
288         slice->SliceBytesInBuffer    = start_code_size + size;
289
290         memcpy(current, start_code, start_code_size);
291         current += start_code_size;
292
293         memcpy(current, &ctx_pic->bitstream[position], size);
294         current += size;
295     }
296     padding = FFMIN(128 - ((current - dxva_data) & 127), end - current);
297     if (slice && padding > 0) {
298         memset(current, 0, padding);
299         current += padding;
300
301         slice->SliceBytesInBuffer += padding;
302     }
303 #if CONFIG_D3D11VA
304     if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
305         if (FAILED(ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type)))
306             return -1;
307 #endif
308 #if CONFIG_DXVA2
309     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
310         if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type)))
311             return -1;
312 #endif
313     if (i < ctx_pic->slice_count)
314         return -1;
315
316 #if CONFIG_D3D11VA
317     if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
318         D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = bs;
319         memset(dsc11, 0, sizeof(*dsc11));
320         dsc11->BufferType           = type;
321         dsc11->DataSize             = current - dxva_data;
322         dsc11->NumMBsInBuffer       = 0;
323
324         type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
325     }
326 #endif
327 #if CONFIG_DXVA2
328     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
329         DXVA2_DecodeBufferDesc *dsc2 = bs;
330         memset(dsc2, 0, sizeof(*dsc2));
331         dsc2->CompressedBufferType = type;
332         dsc2->DataSize             = current - dxva_data;
333         dsc2->NumMBsInBuffer       = 0;
334
335         type = DXVA2_SliceControlBufferType;
336     }
337 #endif
338
339     slice_data = ctx_pic->slice_short;
340     slice_size = ctx_pic->slice_count * sizeof(*ctx_pic->slice_short);
341
342     av_assert0(((current - dxva_data) & 127) == 0);
343     return ff_dxva2_commit_buffer(avctx, ctx, sc,
344                                   type,
345                                   slice_data, slice_size, 0);
346 }
347
348
349 static int dxva2_hevc_start_frame(AVCodecContext *avctx,
350                                   av_unused const uint8_t *buffer,
351                                   av_unused uint32_t size)
352 {
353     const HEVCContext *h = avctx->priv_data;
354     AVDXVAContext *ctx = avctx->hwaccel_context;
355     struct hevc_dxva2_picture_context *ctx_pic = h->ref->hwaccel_picture_private;
356
357     if (DXVA_CONTEXT_DECODER(avctx, ctx) == NULL ||
358         DXVA_CONTEXT_CFG(avctx, ctx) == NULL ||
359         DXVA_CONTEXT_COUNT(avctx, ctx) <= 0)
360         return -1;
361     av_assert0(ctx_pic);
362
363     /* Fill up DXVA_PicParams_HEVC */
364     fill_picture_parameters(avctx, ctx, h, &ctx_pic->pp);
365
366     /* Fill up DXVA_Qmatrix_HEVC */
367     fill_scaling_lists(ctx, h, &ctx_pic->qm);
368
369     ctx_pic->slice_count    = 0;
370     ctx_pic->bitstream_size = 0;
371     ctx_pic->bitstream      = NULL;
372     return 0;
373 }
374
375 static int dxva2_hevc_decode_slice(AVCodecContext *avctx,
376                                    const uint8_t *buffer,
377                                    uint32_t size)
378 {
379     const HEVCContext *h = avctx->priv_data;
380     const HEVCFrame *current_picture = h->ref;
381     struct hevc_dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
382     unsigned position;
383
384     if (ctx_pic->slice_count >= MAX_SLICES)
385         return -1;
386
387     if (!ctx_pic->bitstream)
388         ctx_pic->bitstream = buffer;
389     ctx_pic->bitstream_size += size;
390
391     position = buffer - ctx_pic->bitstream;
392     fill_slice_short(&ctx_pic->slice_short[ctx_pic->slice_count], position, size);
393     ctx_pic->slice_count++;
394
395     return 0;
396 }
397
398 static int dxva2_hevc_end_frame(AVCodecContext *avctx)
399 {
400     HEVCContext *h = avctx->priv_data;
401     struct hevc_dxva2_picture_context *ctx_pic = h->ref->hwaccel_picture_private;
402     int scale = ctx_pic->pp.dwCodingParamToolFlags & 1;
403     int ret;
404
405     if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
406         return -1;
407
408     ret = ff_dxva2_common_end_frame(avctx, h->ref->frame,
409                                     &ctx_pic->pp, sizeof(ctx_pic->pp),
410                                     scale ? &ctx_pic->qm : NULL, scale ? sizeof(ctx_pic->qm) : 0,
411                                     commit_bitstream_and_slice_buffer);
412     return ret;
413 }
414
415 #if CONFIG_HEVC_DXVA2_HWACCEL
416 AVHWAccel ff_hevc_dxva2_hwaccel = {
417     .name           = "hevc_dxva2",
418     .type           = AVMEDIA_TYPE_VIDEO,
419     .id             = AV_CODEC_ID_HEVC,
420     .pix_fmt        = AV_PIX_FMT_DXVA2_VLD,
421     .start_frame    = dxva2_hevc_start_frame,
422     .decode_slice   = dxva2_hevc_decode_slice,
423     .end_frame      = dxva2_hevc_end_frame,
424     .frame_priv_data_size = sizeof(struct hevc_dxva2_picture_context),
425 };
426 #endif
427
428 #if CONFIG_HEVC_D3D11VA_HWACCEL
429 AVHWAccel ff_hevc_d3d11va_hwaccel = {
430     .name           = "hevc_d3d11va",
431     .type           = AVMEDIA_TYPE_VIDEO,
432     .id             = AV_CODEC_ID_HEVC,
433     .pix_fmt        = AV_PIX_FMT_D3D11VA_VLD,
434     .start_frame    = dxva2_hevc_start_frame,
435     .decode_slice   = dxva2_hevc_decode_slice,
436     .end_frame      = dxva2_hevc_end_frame,
437     .frame_priv_data_size = sizeof(struct hevc_dxva2_picture_context),
438 };
439 #endif