]> git.sesse.net Git - ffmpeg/blob - libavcodec/dxva2_h264.c
Reindent the content of one if(){} in ff_dxva2_common_end_frame.
[ffmpeg] / libavcodec / dxva2_h264.c
1 /*
2  * DXVA2 H264 HW acceleration.
3  *
4  * copyright (c) 2009 Laurent Aimar
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
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "dxva2.h"
24 #include "avcodec.h"
25
26 #include "mpegvideo.h"
27 #include "h264.h"
28 #include "h264data.h"
29
30 struct dxva2_picture_context {
31     DXVA_PicParams_H264   pp;
32     DXVA_Qmatrix_H264     qm;
33     unsigned              slice_count;
34     DXVA_Slice_H264_Short slice_short[MAX_SLICES];
35     DXVA_Slice_H264_Long  slice_long[MAX_SLICES];
36     const uint8_t         *bitstream;
37     unsigned              bitstream_size;
38 };
39
40 static void *get_surface(const Picture *picture)
41 {
42     return picture->data[3];
43 }
44 static unsigned get_surface_index(const struct dxva_context *ctx,
45                                   const Picture *picture)
46 {
47     void *surface = get_surface(picture);
48     unsigned i;
49
50     for (i = 0; i < ctx->surface_count; i++)
51         if (ctx->surface[i] == surface)
52             return i;
53
54     assert(0);
55     return 0;
56 }
57
58 static void fill_picture_entry(DXVA_PicEntry_H264 *pic,
59                                unsigned index, unsigned flag)
60 {
61     assert((index&0x7f) == index && (flag&0x01) == flag);
62     pic->bPicEntry = index | (flag << 7);
63 }
64
65 static void fill_picture_parameters(struct dxva_context *ctx, const H264Context *h,
66                                     DXVA_PicParams_H264 *pp)
67 {
68     const MpegEncContext *s = &h->s;
69     const Picture *current_picture = s->current_picture_ptr;
70     int i;
71
72     memset(pp, 0, sizeof(*pp));
73     /* Configure current picture */
74     fill_picture_entry(&pp->CurrPic,
75                        get_surface_index(ctx, current_picture),
76                        s->picture_structure == PICT_BOTTOM_FIELD);
77     /* Configure the set of references */
78     pp->UsedForReferenceFlags  = 0;
79     pp->NonExistingFrameFlags  = 0;
80     for (i = 0; i < FF_ARRAY_ELEMS(pp->RefFrameList); i++) {
81         if (i < h->short_ref_count + h->long_ref_count) {
82             const Picture *r;
83             if (i < h->short_ref_count) {
84                 r = h->short_ref[i];
85                 assert(!r->long_ref);
86             } else {
87                 r = h->long_ref[i - h->short_ref_count];
88                 assert(r->long_ref);
89             }
90             fill_picture_entry(&pp->RefFrameList[i],
91                                get_surface_index(ctx, r),
92                                r->long_ref != 0);
93
94             if ((r->reference & PICT_TOP_FIELD) && r->field_poc[0] != INT_MAX)
95                 pp->FieldOrderCntList[i][0] = r->field_poc[0];
96             if ((r->reference & PICT_BOTTOM_FIELD) && r->field_poc[1] != INT_MAX)
97                 pp->FieldOrderCntList[i][1] = r->field_poc[1];
98
99             pp->FrameNumList[i] = r->long_ref ? r->pic_id : r->frame_num;
100             if (r->reference & PICT_TOP_FIELD)
101                 pp->UsedForReferenceFlags |= 1 << (2*i + 0);
102             if (r->reference & PICT_BOTTOM_FIELD)
103                 pp->UsedForReferenceFlags |= 1 << (2*i + 1);
104         } else {
105             pp->RefFrameList[i].bPicEntry = 0xff;
106             pp->FieldOrderCntList[i][0]   = 0;
107             pp->FieldOrderCntList[i][1]   = 0;
108             pp->FrameNumList[i]           = 0;
109         }
110     }
111
112     pp->wFrameWidthInMbsMinus1        = s->mb_width  - 1;
113     pp->wFrameHeightInMbsMinus1       = s->mb_height - 1;
114     pp->num_ref_frames                = h->sps.ref_frame_count;
115
116     pp->wBitFields                    = ((s->picture_structure != PICT_FRAME) <<  0) |
117                                         (h->sps.mb_aff                        <<  1) |
118                                         (h->sps.residual_color_transform_flag <<  2) |
119                                         /* sp_for_switch_flag (not implemented by FFmpeg) */
120                                         (0                                    <<  3) |
121                                         (h->sps.chroma_format_idc             <<  4) |
122                                         ((h->nal_ref_idc != 0)                <<  6) |
123                                         (h->pps.constrained_intra_pred        <<  7) |
124                                         (h->pps.weighted_pred                 <<  8) |
125                                         (h->pps.weighted_bipred_idc           <<  9) |
126                                         /* MbsConsecutiveFlag */
127                                         (1                                    << 11) |
128                                         (h->sps.frame_mbs_only_flag           << 12) |
129                                         (h->pps.transform_8x8_mode            << 13) |
130                                         ((h->sps.level_idc >= 31)             << 14) |
131                                         /* IntraPicFlag (Modified if we detect a non
132                                          * intra slice in decode_slice) */
133                                         (1                                    << 15);
134
135     pp->bit_depth_luma_minus8         = h->sps.bit_depth_luma - 8;
136     pp->bit_depth_chroma_minus8       = h->sps.bit_depth_chroma - 8;
137     pp->Reserved16Bits                = 3; /* FIXME is there a way to detect the right mode ? */
138     pp->StatusReportFeedbackNumber    = 1 + ctx->report_id++;
139     pp->CurrFieldOrderCnt[0] = 0;
140     if ((s->picture_structure & PICT_TOP_FIELD) &&
141         current_picture->field_poc[0] != INT_MAX)
142         pp->CurrFieldOrderCnt[0] = current_picture->field_poc[0];
143     pp->CurrFieldOrderCnt[1] = 0;
144     if ((s->picture_structure & PICT_BOTTOM_FIELD) &&
145         current_picture->field_poc[1] != INT_MAX)
146         pp->CurrFieldOrderCnt[1] = current_picture->field_poc[1];
147     pp->pic_init_qs_minus26           = h->pps.init_qs - 26;
148     pp->chroma_qp_index_offset        = h->pps.chroma_qp_index_offset[0];
149     pp->second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
150     pp->ContinuationFlag              = 1;
151     pp->pic_init_qp_minus26           = h->pps.init_qp - 26;
152     pp->num_ref_idx_l0_active_minus1  = h->pps.ref_count[0] - 1;
153     pp->num_ref_idx_l1_active_minus1  = h->pps.ref_count[1] - 1;
154     pp->Reserved8BitsA                = 0;
155     pp->frame_num                     = h->frame_num;
156     pp->log2_max_frame_num_minus4     = h->sps.log2_max_frame_num - 4;
157     pp->pic_order_cnt_type            = h->sps.poc_type;
158     if (h->sps.poc_type == 0)
159         pp->log2_max_pic_order_cnt_lsb_minus4 = h->sps.log2_max_poc_lsb - 4;
160     else if (h->sps.poc_type == 1)
161         pp->delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
162     pp->direct_8x8_inference_flag     = h->sps.direct_8x8_inference_flag;
163     pp->entropy_coding_mode_flag      = h->pps.cabac;
164     pp->pic_order_present_flag        = h->pps.pic_order_present;
165     pp->num_slice_groups_minus1       = h->pps.slice_group_count - 1;
166     pp->slice_group_map_type          = h->pps.mb_slice_group_map_type;
167     pp->deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
168     pp->redundant_pic_cnt_present_flag= h->pps.redundant_pic_cnt_present;
169     pp->Reserved8BitsB                = 0;
170     pp->slice_group_change_rate_minus1= 0;  /* XXX not implemented by FFmpeg */
171     //pp->SliceGroupMap[810];               /* XXX not implemented by FFmpeg */
172 }
173
174 static void fill_scaling_lists(const H264Context *h, DXVA_Qmatrix_H264 *qm)
175 {
176     unsigned i, j;
177     memset(qm, 0, sizeof(*qm));
178     for (i = 0; i < 6; i++)
179         for (j = 0; j < 16; j++)
180             qm->bScalingLists4x4[i][j] = h->pps.scaling_matrix4[i][zigzag_scan[j]];
181
182     for (i = 0; i < 2; i++)
183         for (j = 0; j < 64; j++)
184             qm->bScalingLists8x8[i][j] = h->pps.scaling_matrix8[i][ff_zigzag_direct[j]];
185 }
186
187 static int is_slice_short(struct dxva_context *ctx)
188 {
189     assert(ctx->cfg->ConfigBitstreamRaw == 1 ||
190            ctx->cfg->ConfigBitstreamRaw == 2);
191     return ctx->cfg->ConfigBitstreamRaw == 2;
192 }
193
194 static void fill_slice_short(DXVA_Slice_H264_Short *slice,
195                              unsigned position, unsigned size)
196 {
197     memset(slice, 0, sizeof(*slice));
198     slice->BSNALunitDataLocation = position;
199     slice->SliceBytesInBuffer    = size;
200     slice->wBadSliceChopping     = 0;
201 }
202
203 static void fill_slice_long(AVCodecContext *avctx, DXVA_Slice_H264_Long *slice,
204                             unsigned position, unsigned size)
205 {
206     H264Context *h = avctx->priv_data; /* FIXME Can't use const because of get_bits_count */
207     struct dxva_context *ctx = avctx->hwaccel_context;
208     MpegEncContext *s = &h->s;
209     unsigned list;
210
211     memset(slice, 0, sizeof(*slice));
212     slice->BSNALunitDataLocation = position;
213     slice->SliceBytesInBuffer    = size;
214     slice->wBadSliceChopping     = 0;
215
216     slice->first_mb_in_slice     = (s->mb_y >> FIELD_OR_MBAFF_PICTURE) * s->mb_width + s->mb_x;
217     slice->NumMbsForSlice        = 0; /* XXX it is set once we have all slices */
218     slice->BitOffsetToSliceData  = get_bits_count(&s->gb) + 8;
219     slice->slice_type            = ff_h264_get_slice_type(h);
220     if (h->slice_type_fixed)
221         slice->slice_type += 5;
222     slice->luma_log2_weight_denom       = h->luma_log2_weight_denom;
223     slice->chroma_log2_weight_denom     = h->chroma_log2_weight_denom;
224     if (h->list_count > 0)
225         slice->num_ref_idx_l0_active_minus1 = h->ref_count[0] - 1;
226     if (h->list_count > 1)
227         slice->num_ref_idx_l1_active_minus1 = h->ref_count[1] - 1;
228     slice->slice_alpha_c0_offset_div2   = h->slice_alpha_c0_offset / 2;
229     slice->slice_beta_offset_div2       = h->slice_beta_offset / 2;
230     slice->Reserved8Bits                = 0;
231
232     for (list = 0; list < 2; list++) {
233         unsigned i;
234         for (i = 0; i < FF_ARRAY_ELEMS(slice->RefPicList[list]); i++) {
235             if (list < h->list_count && i < h->ref_count[list]) {
236                 const Picture *r = &h->ref_list[list][i];
237                 unsigned plane;
238                 fill_picture_entry(&slice->RefPicList[list][i],
239                                    get_surface_index(ctx, r),
240                                    r->reference == PICT_BOTTOM_FIELD);
241                 for (plane = 0; plane < 3; plane++) {
242                     int w, o;
243                     if (plane == 0 && h->luma_weight_flag[list]) {
244                         w = h->luma_weight[list][i];
245                         o = h->luma_offset[list][i];
246                     } else if (plane >= 1 && h->chroma_weight_flag[list]) {
247                         w = h->chroma_weight[list][i][plane-1];
248                         o = h->chroma_offset[list][i][plane-1];
249                     } else {
250                         w = 1 << (plane == 0 ? h->luma_log2_weight_denom :
251                                                h->chroma_log2_weight_denom);
252                         o = 0;
253                     }
254                     slice->Weights[list][i][plane][0] = w;
255                     slice->Weights[list][i][plane][1] = o;
256                 }
257             } else {
258                 unsigned plane;
259                 slice->RefPicList[list][i].bPicEntry = 0xff;
260                 for (plane = 0; plane < 3; plane++) {
261                     slice->Weights[list][i][plane][0] = 0;
262                     slice->Weights[list][i][plane][1] = 0;
263                 }
264             }
265         }
266     }
267     slice->slice_qs_delta    = 0; /* XXX not implemented by FFmpeg */
268     slice->slice_qp_delta    = s->qscale - h->pps.init_qp;
269     slice->redundant_pic_cnt = h->redundant_pic_count;
270     if (h->slice_type == FF_B_TYPE)
271         slice->direct_spatial_mv_pred_flag = h->direct_spatial_mv_pred;
272     slice->cabac_init_idc = h->pps.cabac ? h->cabac_init_idc : 0;
273     if (h->deblocking_filter < 2)
274         slice->disable_deblocking_filter_idc = 1 - h->deblocking_filter;
275     else
276         slice->disable_deblocking_filter_idc = h->deblocking_filter;
277     slice->slice_id = h->current_slice - 1;
278 }
279
280 static int commit_buffer(AVCodecContext *avctx,
281                          struct dxva_context *ctx,
282                          DXVA2_DecodeBufferDesc *dsc,
283                          unsigned type, const void *data, unsigned size,
284                          unsigned mb_count)
285 {
286     void     *dxva_data;
287     unsigned dxva_size;
288     int      result;
289
290     if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder, type,
291                                               &dxva_data, &dxva_size))) {
292         av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %d\n", type);
293         return -1;
294     }
295     if (size <= dxva_size) {
296         memcpy(dxva_data, data, size);
297
298         memset(dsc, 0, sizeof(*dsc));
299         dsc->CompressedBufferType = type;
300         dsc->DataSize             = size;
301         dsc->NumMBsInBuffer       = mb_count;
302
303         result = 0;
304     } else {
305         av_log(avctx, AV_LOG_ERROR, "Buffer for type %d was too small\n", type);
306         result = -1;
307     }
308     if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder, type))) {
309         av_log(avctx, AV_LOG_ERROR, "Failed to release buffer type %d\n", type);
310         result = -1;
311     }
312     return result;
313 }
314
315 static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
316                                              DXVA2_DecodeBufferDesc *bs,
317                                              DXVA2_DecodeBufferDesc *sc)
318 {
319     H264Context *h = avctx->priv_data;
320     MpegEncContext *s = &h->s;
321     const unsigned mb_count = s->mb_width * s->mb_height;
322     struct dxva_context *ctx = avctx->hwaccel_context;
323     const Picture *current_picture = h->s.current_picture_ptr;
324     struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
325     DXVA_Slice_H264_Short *slice = NULL;
326     uint8_t  *dxva_data, *current, *end;
327     unsigned dxva_size;
328     void     *slice_data;
329     unsigned slice_size;
330     unsigned padding;
331     unsigned i;
332
333     /* Create an annex B bitstream buffer with only slice NAL and finalize slice */
334     if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
335                                                DXVA2_BitStreamDateBufferType,
336                                                &dxva_data, &dxva_size)))
337         return -1;
338     current = dxva_data;
339     end = dxva_data + dxva_size;
340
341     for (i = 0; i < ctx_pic->slice_count; i++) {
342         static const uint8_t start_code[] = { 0, 0, 1 };
343         static const unsigned start_code_size = sizeof(start_code);
344         unsigned position, size;
345
346         assert(offsetof(DXVA_Slice_H264_Short, BSNALunitDataLocation) ==
347                offsetof(DXVA_Slice_H264_Long,  BSNALunitDataLocation));
348         assert(offsetof(DXVA_Slice_H264_Short, SliceBytesInBuffer) ==
349                offsetof(DXVA_Slice_H264_Long,  SliceBytesInBuffer));
350
351         if (is_slice_short(ctx))
352             slice = &ctx_pic->slice_short[i];
353         else
354             slice = (DXVA_Slice_H264_Short*)&ctx_pic->slice_long[i];
355
356         position = slice->BSNALunitDataLocation;
357         size     = slice->SliceBytesInBuffer;
358         if (start_code_size + size > end - current) {
359             av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
360             break;
361         }
362
363         slice->BSNALunitDataLocation = current - dxva_data;
364         slice->SliceBytesInBuffer    = start_code_size + size;
365
366         if (!is_slice_short(ctx)) {
367             DXVA_Slice_H264_Long *slice_long = (DXVA_Slice_H264_Long*)slice;
368             if (i < ctx_pic->slice_count - 1)
369                 slice_long->NumMbsForSlice =
370                     slice_long[1].first_mb_in_slice - slice_long[0].first_mb_in_slice;
371             else
372                 slice_long->NumMbsForSlice = mb_count - slice_long->first_mb_in_slice;
373         }
374
375         memcpy(current, start_code, start_code_size);
376         current += start_code_size;
377
378         memcpy(current, &ctx_pic->bitstream[position], size);
379         current += size;
380     }
381     padding = FFMIN(128 - ((current - dxva_data) & 127), end - current);
382     if (slice && padding > 0) {
383         memset(current, 0, padding);
384         current += padding;
385
386         slice->SliceBytesInBuffer += padding;
387     }
388     if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
389                                                   DXVA2_BitStreamDateBufferType)))
390         return -1;
391     if (i < ctx_pic->slice_count)
392         return -1;
393
394     memset(bs, 0, sizeof(*bs));
395     bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
396     bs->DataSize             = current - dxva_data;
397     bs->NumMBsInBuffer       = mb_count;
398
399     if (is_slice_short(ctx)) {
400         slice_data = ctx_pic->slice_short;
401         slice_size = ctx_pic->slice_count * sizeof(*ctx_pic->slice_short);
402     } else {
403         slice_data = ctx_pic->slice_long;
404         slice_size = ctx_pic->slice_count * sizeof(*ctx_pic->slice_long);
405     }
406     assert((bs->DataSize & 127) == 0);
407     return commit_buffer(avctx, ctx, sc,
408                          DXVA2_SliceControlBufferType,
409                          slice_data, slice_size, mb_count);
410 }
411
412
413 static int start_frame(AVCodecContext *avctx,
414                        av_unused const uint8_t *buffer,
415                        av_unused uint32_t size)
416 {
417     const H264Context *h = avctx->priv_data;
418     struct dxva_context *ctx = avctx->hwaccel_context;
419     struct dxva2_picture_context *ctx_pic = h->s.current_picture_ptr->hwaccel_picture_private;
420
421     if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
422         return -1;
423     assert(ctx_pic);
424
425     /* Fill up DXVA_PicParams_H264 */
426     fill_picture_parameters(ctx, h, &ctx_pic->pp);
427
428     /* Fill up DXVA_Qmatrix_H264 */
429     fill_scaling_lists(h, &ctx_pic->qm);
430
431     ctx_pic->slice_count    = 0;
432     ctx_pic->bitstream_size = 0;
433     ctx_pic->bitstream      = NULL;
434     return 0;
435 }
436
437 static int decode_slice(AVCodecContext *avctx,
438                         const uint8_t *buffer, uint32_t size)
439 {
440     H264Context *h = avctx->priv_data; /* FIXME Can't use const because of get_bits_count */
441     struct dxva_context *ctx = avctx->hwaccel_context;
442     const Picture *current_picture = h->s.current_picture_ptr;
443     struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
444     unsigned position;
445
446     if (ctx_pic->slice_count >= MAX_SLICES)
447         return -1;
448
449     if (!ctx_pic->bitstream)
450         ctx_pic->bitstream = buffer;
451     ctx_pic->bitstream_size += size;
452
453     position = buffer - ctx_pic->bitstream;
454     if (is_slice_short(ctx))
455         fill_slice_short(&ctx_pic->slice_short[ctx_pic->slice_count],
456                          position, size);
457     else
458         fill_slice_long(avctx, &ctx_pic->slice_long[ctx_pic->slice_count],
459                         position, size);
460     ctx_pic->slice_count++;
461
462     if (h->slice_type != FF_I_TYPE && h->slice_type != FF_SI_TYPE)
463         ctx_pic->pp.wBitFields &= ~(1 << 15); /* Set IntraPicFlag to 0 */
464     return 0;
465 }
466
467 static int ff_dxva2_common_end_frame(AVCodecContext *avctx, MpegEncContext *s,
468                                      const void *pp, unsigned pp_size,
469                                      const void *qm, unsigned qm_size,
470                                      int (*commit_bs_si)(AVCodecContext *,
471                                                          DXVA2_DecodeBufferDesc *bs,
472                                                          DXVA2_DecodeBufferDesc *slice))
473 {
474     struct dxva_context *ctx = avctx->hwaccel_context;
475     unsigned               buffer_count = 0;
476     DXVA2_DecodeBufferDesc buffer[4];
477     DXVA2_DecodeExecuteParams exec;
478     int      result;
479
480     if (FAILED(IDirectXVideoDecoder_BeginFrame(ctx->decoder,
481                                                get_surface(s->current_picture_ptr),
482                                                NULL))) {
483         av_log(avctx, AV_LOG_ERROR, "Failed to begin frame\n");
484         return -1;
485     }
486
487     result = commit_buffer(avctx, ctx, &buffer[buffer_count],
488                            DXVA2_PictureParametersBufferType,
489                            pp, pp_size, 0);
490     if (result) {
491         av_log(avctx, AV_LOG_ERROR,
492                "Failed to add picture parameter buffer\n");
493         goto end;
494     }
495     buffer_count++;
496
497     if (qm_size > 0) {
498         result = commit_buffer(avctx, ctx, &buffer[buffer_count],
499                                DXVA2_InverseQuantizationMatrixBufferType,
500                                qm, qm_size, 0);
501         if (result) {
502             av_log(avctx, AV_LOG_ERROR,
503                    "Failed to add inverse quantization matrix buffer\n");
504             goto end;
505         }
506         buffer_count++;
507     }
508
509     result = commit_bs_si(avctx,
510                           &buffer[buffer_count + 0],
511                           &buffer[buffer_count + 1]);
512     if (result) {
513         av_log(avctx, AV_LOG_ERROR,
514                "Failed to add bitstream or slice control buffer\n");
515         goto end;
516     }
517     buffer_count += 2;
518
519     /* TODO Film Grain when possible */
520
521     assert(buffer_count == 1 + (qm_size > 0) + 2);
522
523     memset(&exec, 0, sizeof(exec));
524     exec.NumCompBuffers      = buffer_count;
525     exec.pCompressedBuffers  = buffer;
526     exec.pExtensionData      = NULL;
527     if (FAILED(IDirectXVideoDecoder_Execute(ctx->decoder, &exec))) {
528         av_log(avctx, AV_LOG_ERROR, "Failed to execute\n");
529         result = -1;
530     }
531
532 end:
533     if (FAILED(IDirectXVideoDecoder_EndFrame(ctx->decoder, NULL))) {
534         av_log(avctx, AV_LOG_ERROR, "Failed to end frame\n");
535         result = -1;
536     }
537
538     if (!result)
539         ff_draw_horiz_band(s, 0, s->avctx->height);
540     return result;
541 }
542
543 static int end_frame(AVCodecContext *avctx)
544 {
545     H264Context *h = avctx->priv_data;
546     MpegEncContext *s = &h->s;
547     struct dxva2_picture_context *ctx_pic =
548         h->s.current_picture_ptr->hwaccel_picture_private;
549
550     if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
551         return -1;
552     return ff_dxva2_common_end_frame(avctx, s,
553                                      &ctx_pic->pp, sizeof(ctx_pic->pp),
554                                      &ctx_pic->qm, sizeof(ctx_pic->qm),
555                                      commit_bitstream_and_slice_buffer);
556 }
557
558 AVHWAccel h264_dxva2_hwaccel = {
559     .name           = "h264_dxva2",
560     .type           = CODEC_TYPE_VIDEO,
561     .id             = CODEC_ID_H264,
562     .pix_fmt        = PIX_FMT_DXVA2_VLD,
563     .capabilities   = 0,
564     .start_frame    = start_frame,
565     .decode_slice   = decode_slice,
566     .end_frame      = end_frame,
567     .priv_data_size = sizeof(struct dxva2_picture_context),
568 };
569