2 * MPEG-2 HW acceleration.
4 * copyright (c) 2010 Laurent Aimar
6 * This file is part of Libav.
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.
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.
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
23 #include "libavutil/log.h"
25 #include "dxva2_internal.h"
26 #include "mpegutils.h"
27 #include "mpegvideo.h"
29 #define MAX_SLICES 1024
30 struct dxva2_picture_context {
31 DXVA_PictureParameters pp;
34 DXVA_SliceInfo slice[MAX_SLICES];
36 const uint8_t *bitstream;
37 unsigned bitstream_size;
40 static void fill_picture_parameters(AVCodecContext *avctx,
42 const struct MpegEncContext *s,
43 DXVA_PictureParameters *pp)
45 const Picture *current_picture = s->current_picture_ptr;
46 int is_field = s->picture_structure != PICT_FRAME;
48 memset(pp, 0, sizeof(*pp));
49 pp->wDecodedPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, current_picture->f);
50 pp->wDeblockedPictureIndex = 0;
51 if (s->pict_type != AV_PICTURE_TYPE_I)
52 pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->last_picture.f);
54 pp->wForwardRefPictureIndex = 0xffff;
55 if (s->pict_type == AV_PICTURE_TYPE_B)
56 pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->next_picture.f);
58 pp->wBackwardRefPictureIndex = 0xffff;
59 pp->wPicWidthInMBminus1 = s->mb_width - 1;
60 pp->wPicHeightInMBminus1 = (s->mb_height >> is_field) - 1;
61 pp->bMacroblockWidthMinus1 = 15;
62 pp->bMacroblockHeightMinus1 = 15;
63 pp->bBlockWidthMinus1 = 7;
64 pp->bBlockHeightMinus1 = 7;
66 pp->bPicStructure = s->picture_structure;
67 pp->bSecondField = is_field && !s->first_field;
68 pp->bPicIntra = s->pict_type == AV_PICTURE_TYPE_I;
69 pp->bPicBackwardPrediction = s->pict_type == AV_PICTURE_TYPE_B;
70 pp->bBidirectionalAveragingMode = 0;
71 pp->bMVprecisionAndChromaRelation= 0; /* FIXME */
72 pp->bChromaFormat = s->chroma_format;
73 pp->bPicScanFixed = 1;
74 pp->bPicScanMethod = s->alternate_scan ? 1 : 0;
75 pp->bPicReadbackRequests = 0;
77 pp->bPicSpatialResid8 = 0;
78 pp->bPicOverflowBlocks = 0;
79 pp->bPicExtrapolation = 0;
80 pp->bPicDeblocked = 0;
81 pp->bPicDeblockConfined = 0;
82 pp->bPic4MVallowed = 0;
86 pp->bReservedBits = 0;
87 pp->wBitstreamFcodes = (s->mpeg_f_code[0][0] << 12) |
88 (s->mpeg_f_code[0][1] << 8) |
89 (s->mpeg_f_code[1][0] << 4) |
90 (s->mpeg_f_code[1][1] );
91 pp->wBitstreamPCEelements = (s->intra_dc_precision << 14) |
92 (s->picture_structure << 12) |
93 (s->top_field_first << 11) |
94 (s->frame_pred_frame_dct << 10) |
95 (s->concealment_motion_vectors << 9) |
96 (s->q_scale_type << 8) |
97 (s->intra_vlc_format << 7) |
98 (s->alternate_scan << 6) |
99 (s->repeat_first_field << 5) |
100 (s->chroma_420_type << 4) |
101 (s->progressive_frame << 3);
102 pp->bBitstreamConcealmentNeed = 0;
103 pp->bBitstreamConcealmentMethod = 0;
106 static void fill_quantization_matrices(AVCodecContext *avctx,
108 const struct MpegEncContext *s,
109 DXVA_QmatrixData *qm)
112 for (i = 0; i < 4; i++)
113 qm->bNewQmatrix[i] = 1;
114 for (i = 0; i < 64; i++) {
115 int n = s->idsp.idct_permutation[ff_zigzag_direct[i]];
116 qm->Qmatrix[0][i] = s->intra_matrix[n];;
117 qm->Qmatrix[1][i] = s->inter_matrix[n];;
118 qm->Qmatrix[2][i] = s->chroma_intra_matrix[n];;
119 qm->Qmatrix[3][i] = s->chroma_inter_matrix[n];;
123 static void fill_slice(AVCodecContext *avctx,
124 const struct MpegEncContext *s,
125 DXVA_SliceInfo *slice,
127 const uint8_t *buffer, unsigned size)
129 int is_field = s->picture_structure != PICT_FRAME;
132 memset(slice, 0, sizeof(*slice));
133 slice->wHorizontalPosition = s->mb_x;
134 slice->wVerticalPosition = s->mb_y >> is_field;
135 slice->dwSliceBitsInBuffer = 8 * size;
136 slice->dwSliceDataLocation = position;
137 slice->bStartCodeBitOffset = 0;
138 slice->bReservedBits = 0;
139 /* XXX We store the index of the first MB and it will be fixed later */
140 slice->wNumberMBsInSlice = (s->mb_y >> is_field) * s->mb_width + s->mb_x;
141 slice->wBadSliceChopping = 0;
143 init_get_bits(&gb, &buffer[4], 8 * (size - 4));
145 slice->wQuantizerScaleCode = get_bits(&gb, 5);
146 while (get_bits1(&gb))
149 slice->wMBbitOffset = 4 * 8 + get_bits_count(&gb);
151 static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
152 DECODER_BUFFER_DESC *bs,
153 DECODER_BUFFER_DESC *sc)
155 const struct MpegEncContext *s = avctx->priv_data;
156 AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
157 struct dxva2_picture_context *ctx_pic =
158 s->current_picture_ptr->hwaccel_picture_private;
159 const int is_field = s->picture_structure != PICT_FRAME;
160 const unsigned mb_count = s->mb_width * (s->mb_height >> is_field);
162 uint8_t *dxva_data, *current, *end;
168 if (ff_dxva2_is_d3d11(avctx)) {
169 type = D3D11_VIDEO_DECODER_BUFFER_BITSTREAM;
170 if (FAILED(ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
171 D3D11VA_CONTEXT(ctx)->decoder,
173 &dxva_size, &dxva_data_ptr)))
178 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
179 type = DXVA2_BitStreamDateBufferType;
180 if (FAILED(IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder,
182 &dxva_data_ptr, &dxva_size)))
187 dxva_data = dxva_data_ptr;
189 end = dxva_data + dxva_size;
191 for (i = 0; i < ctx_pic->slice_count; i++) {
192 DXVA_SliceInfo *slice = &ctx_pic->slice[i];
193 unsigned position = slice->dwSliceDataLocation;
194 unsigned size = slice->dwSliceBitsInBuffer / 8;
195 if (size > end - current) {
196 av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
199 slice->dwSliceDataLocation = current - dxva_data;
201 if (i < ctx_pic->slice_count - 1)
202 slice->wNumberMBsInSlice =
203 slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice;
205 slice->wNumberMBsInSlice =
206 mb_count - slice[0].wNumberMBsInSlice;
208 memcpy(current, &ctx_pic->bitstream[position], size);
212 if (ff_dxva2_is_d3d11(avctx))
213 if (FAILED(ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type)))
217 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
218 if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type)))
221 if (i < ctx_pic->slice_count)
225 if (ff_dxva2_is_d3d11(avctx)) {
226 D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = bs;
227 memset(dsc11, 0, sizeof(*dsc11));
228 dsc11->BufferType = type;
229 dsc11->DataSize = current - dxva_data;
230 dsc11->NumMBsInBuffer = mb_count;
232 type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
236 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
237 DXVA2_DecodeBufferDesc *dsc2 = bs;
238 memset(dsc2, 0, sizeof(*dsc2));
239 dsc2->CompressedBufferType = type;
240 dsc2->DataSize = current - dxva_data;
241 dsc2->NumMBsInBuffer = mb_count;
243 type = DXVA2_SliceControlBufferType;
247 return ff_dxva2_commit_buffer(avctx, ctx, sc,
250 ctx_pic->slice_count * sizeof(*ctx_pic->slice),
254 static int dxva2_mpeg2_start_frame(AVCodecContext *avctx,
255 av_unused const uint8_t *buffer,
256 av_unused uint32_t size)
258 const struct MpegEncContext *s = avctx->priv_data;
259 AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
260 struct dxva2_picture_context *ctx_pic =
261 s->current_picture_ptr->hwaccel_picture_private;
263 if (!DXVA_CONTEXT_VALID(avctx, ctx))
267 fill_picture_parameters(avctx, ctx, s, &ctx_pic->pp);
268 fill_quantization_matrices(avctx, ctx, s, &ctx_pic->qm);
270 ctx_pic->slice_count = 0;
271 ctx_pic->bitstream_size = 0;
272 ctx_pic->bitstream = NULL;
276 static int dxva2_mpeg2_decode_slice(AVCodecContext *avctx,
277 const uint8_t *buffer, uint32_t size)
279 const struct MpegEncContext *s = avctx->priv_data;
280 struct dxva2_picture_context *ctx_pic =
281 s->current_picture_ptr->hwaccel_picture_private;
284 if (ctx_pic->slice_count >= MAX_SLICES) {
285 avpriv_request_sample(avctx, "%d slices in dxva2",
286 ctx_pic->slice_count);
289 if (!ctx_pic->bitstream)
290 ctx_pic->bitstream = buffer;
291 ctx_pic->bitstream_size += size;
293 position = buffer - ctx_pic->bitstream;
294 fill_slice(avctx, s, &ctx_pic->slice[ctx_pic->slice_count++], position,
299 static int dxva2_mpeg2_end_frame(AVCodecContext *avctx)
301 struct MpegEncContext *s = avctx->priv_data;
302 struct dxva2_picture_context *ctx_pic =
303 s->current_picture_ptr->hwaccel_picture_private;
306 if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
308 ret = ff_dxva2_common_end_frame(avctx, s->current_picture_ptr->f,
309 &ctx_pic->pp, sizeof(ctx_pic->pp),
310 &ctx_pic->qm, sizeof(ctx_pic->qm),
311 commit_bitstream_and_slice_buffer);
313 ff_mpeg_draw_horiz_band(s, 0, avctx->height);
317 #if CONFIG_MPEG2_DXVA2_HWACCEL
318 const AVHWAccel ff_mpeg2_dxva2_hwaccel = {
319 .name = "mpeg2_dxva2",
320 .type = AVMEDIA_TYPE_VIDEO,
321 .id = AV_CODEC_ID_MPEG2VIDEO,
322 .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
323 .init = ff_dxva2_decode_init,
324 .uninit = ff_dxva2_decode_uninit,
325 .start_frame = dxva2_mpeg2_start_frame,
326 .decode_slice = dxva2_mpeg2_decode_slice,
327 .end_frame = dxva2_mpeg2_end_frame,
328 .frame_params = ff_dxva2_common_frame_params,
329 .frame_priv_data_size = sizeof(struct dxva2_picture_context),
330 .priv_data_size = sizeof(FFDXVASharedContext),
334 #if CONFIG_MPEG2_D3D11VA_HWACCEL
335 const AVHWAccel ff_mpeg2_d3d11va_hwaccel = {
336 .name = "mpeg2_d3d11va",
337 .type = AVMEDIA_TYPE_VIDEO,
338 .id = AV_CODEC_ID_MPEG2VIDEO,
339 .pix_fmt = AV_PIX_FMT_D3D11VA_VLD,
340 .init = ff_dxva2_decode_init,
341 .uninit = ff_dxva2_decode_uninit,
342 .start_frame = dxva2_mpeg2_start_frame,
343 .decode_slice = dxva2_mpeg2_decode_slice,
344 .end_frame = dxva2_mpeg2_end_frame,
345 .frame_params = ff_dxva2_common_frame_params,
346 .frame_priv_data_size = sizeof(struct dxva2_picture_context),
347 .priv_data_size = sizeof(FFDXVASharedContext),
351 #if CONFIG_MPEG2_D3D11VA2_HWACCEL
352 const AVHWAccel ff_mpeg2_d3d11va2_hwaccel = {
353 .name = "mpeg2_d3d11va2",
354 .type = AVMEDIA_TYPE_VIDEO,
355 .id = AV_CODEC_ID_MPEG2VIDEO,
356 .pix_fmt = AV_PIX_FMT_D3D11,
357 .init = ff_dxva2_decode_init,
358 .uninit = ff_dxva2_decode_uninit,
359 .start_frame = dxva2_mpeg2_start_frame,
360 .decode_slice = dxva2_mpeg2_decode_slice,
361 .end_frame = dxva2_mpeg2_end_frame,
362 .frame_params = ff_dxva2_common_frame_params,
363 .frame_priv_data_size = sizeof(struct dxva2_picture_context),
364 .priv_data_size = sizeof(FFDXVASharedContext),