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"
24 #include "dxva2_internal.h"
25 #include "mpegutils.h"
26 #include "mpegvideo.h"
28 #define MAX_SLICES 1024
29 struct dxva2_picture_context {
30 DXVA_PictureParameters pp;
33 DXVA_SliceInfo slice[MAX_SLICES];
35 const uint8_t *bitstream;
36 unsigned bitstream_size;
39 static void fill_picture_parameters(AVCodecContext *avctx,
41 const struct MpegEncContext *s,
42 DXVA_PictureParameters *pp)
44 const Picture *current_picture = s->current_picture_ptr;
45 int is_field = s->picture_structure != PICT_FRAME;
47 memset(pp, 0, sizeof(*pp));
48 pp->wDecodedPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, current_picture->f);
49 pp->wDeblockedPictureIndex = 0;
50 if (s->pict_type != AV_PICTURE_TYPE_I)
51 pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->last_picture.f);
53 pp->wForwardRefPictureIndex = 0xffff;
54 if (s->pict_type == AV_PICTURE_TYPE_B)
55 pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->next_picture.f);
57 pp->wBackwardRefPictureIndex = 0xffff;
58 pp->wPicWidthInMBminus1 = s->mb_width - 1;
59 pp->wPicHeightInMBminus1 = (s->mb_height >> is_field) - 1;
60 pp->bMacroblockWidthMinus1 = 15;
61 pp->bMacroblockHeightMinus1 = 15;
62 pp->bBlockWidthMinus1 = 7;
63 pp->bBlockHeightMinus1 = 7;
65 pp->bPicStructure = s->picture_structure;
66 pp->bSecondField = is_field && !s->first_field;
67 pp->bPicIntra = s->pict_type == AV_PICTURE_TYPE_I;
68 pp->bPicBackwardPrediction = s->pict_type == AV_PICTURE_TYPE_B;
69 pp->bBidirectionalAveragingMode = 0;
70 pp->bMVprecisionAndChromaRelation= 0; /* FIXME */
71 pp->bChromaFormat = s->chroma_format;
72 pp->bPicScanFixed = 1;
73 pp->bPicScanMethod = s->alternate_scan ? 1 : 0;
74 pp->bPicReadbackRequests = 0;
76 pp->bPicSpatialResid8 = 0;
77 pp->bPicOverflowBlocks = 0;
78 pp->bPicExtrapolation = 0;
79 pp->bPicDeblocked = 0;
80 pp->bPicDeblockConfined = 0;
81 pp->bPic4MVallowed = 0;
85 pp->bReservedBits = 0;
86 pp->wBitstreamFcodes = (s->mpeg_f_code[0][0] << 12) |
87 (s->mpeg_f_code[0][1] << 8) |
88 (s->mpeg_f_code[1][0] << 4) |
89 (s->mpeg_f_code[1][1] );
90 pp->wBitstreamPCEelements = (s->intra_dc_precision << 14) |
91 (s->picture_structure << 12) |
92 (s->top_field_first << 11) |
93 (s->frame_pred_frame_dct << 10) |
94 (s->concealment_motion_vectors << 9) |
95 (s->q_scale_type << 8) |
96 (s->intra_vlc_format << 7) |
97 (s->alternate_scan << 6) |
98 (s->repeat_first_field << 5) |
99 (s->chroma_420_type << 4) |
100 (s->progressive_frame << 3);
101 pp->bBitstreamConcealmentNeed = 0;
102 pp->bBitstreamConcealmentMethod = 0;
105 static void fill_quantization_matrices(AVCodecContext *avctx,
107 const struct MpegEncContext *s,
108 DXVA_QmatrixData *qm)
111 for (i = 0; i < 4; i++)
112 qm->bNewQmatrix[i] = 1;
113 for (i = 0; i < 64; i++) {
114 int n = s->idsp.idct_permutation[ff_zigzag_direct[i]];
115 qm->Qmatrix[0][i] = s->intra_matrix[n];;
116 qm->Qmatrix[1][i] = s->inter_matrix[n];;
117 qm->Qmatrix[2][i] = s->chroma_intra_matrix[n];;
118 qm->Qmatrix[3][i] = s->chroma_inter_matrix[n];;
122 static void fill_slice(AVCodecContext *avctx,
123 const struct MpegEncContext *s,
124 DXVA_SliceInfo *slice,
126 const uint8_t *buffer, unsigned size)
128 int is_field = s->picture_structure != PICT_FRAME;
131 memset(slice, 0, sizeof(*slice));
132 slice->wHorizontalPosition = s->mb_x;
133 slice->wVerticalPosition = s->mb_y >> is_field;
134 slice->dwSliceBitsInBuffer = 8 * size;
135 slice->dwSliceDataLocation = position;
136 slice->bStartCodeBitOffset = 0;
137 slice->bReservedBits = 0;
138 /* XXX We store the index of the first MB and it will be fixed later */
139 slice->wNumberMBsInSlice = (s->mb_y >> is_field) * s->mb_width + s->mb_x;
140 slice->wBadSliceChopping = 0;
142 init_get_bits(&gb, &buffer[4], 8 * (size - 4));
144 slice->wQuantizerScaleCode = get_bits(&gb, 5);
145 while (get_bits1(&gb))
148 slice->wMBbitOffset = 4 * 8 + get_bits_count(&gb);
150 static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
151 DECODER_BUFFER_DESC *bs,
152 DECODER_BUFFER_DESC *sc)
154 const struct MpegEncContext *s = avctx->priv_data;
155 AVDXVAContext *ctx = avctx->hwaccel_context;
156 struct dxva2_picture_context *ctx_pic =
157 s->current_picture_ptr->hwaccel_picture_private;
158 const int is_field = s->picture_structure != PICT_FRAME;
159 const unsigned mb_count = s->mb_width * (s->mb_height >> is_field);
161 uint8_t *dxva_data, *current, *end;
167 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
168 type = D3D11_VIDEO_DECODER_BUFFER_BITSTREAM;
169 if (FAILED(ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
170 D3D11VA_CONTEXT(ctx)->decoder,
172 &dxva_size, &dxva_data_ptr)))
177 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
178 type = DXVA2_BitStreamDateBufferType;
179 if (FAILED(IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder,
181 &dxva_data_ptr, &dxva_size)))
186 dxva_data = dxva_data_ptr;
188 end = dxva_data + dxva_size;
190 for (i = 0; i < ctx_pic->slice_count; i++) {
191 DXVA_SliceInfo *slice = &ctx_pic->slice[i];
192 unsigned position = slice->dwSliceDataLocation;
193 unsigned size = slice->dwSliceBitsInBuffer / 8;
194 if (size > end - current) {
195 av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
198 slice->dwSliceDataLocation = current - dxva_data;
200 if (i < ctx_pic->slice_count - 1)
201 slice->wNumberMBsInSlice =
202 slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice;
204 slice->wNumberMBsInSlice =
205 mb_count - slice[0].wNumberMBsInSlice;
207 memcpy(current, &ctx_pic->bitstream[position], size);
211 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
212 if (FAILED(ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type)))
216 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
217 if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type)))
220 if (i < ctx_pic->slice_count)
224 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
225 D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = bs;
226 memset(dsc11, 0, sizeof(*dsc11));
227 dsc11->BufferType = type;
228 dsc11->DataSize = current - dxva_data;
229 dsc11->NumMBsInBuffer = mb_count;
231 type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
235 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
236 DXVA2_DecodeBufferDesc *dsc2 = bs;
237 memset(dsc2, 0, sizeof(*dsc2));
238 dsc2->CompressedBufferType = type;
239 dsc2->DataSize = current - dxva_data;
240 dsc2->NumMBsInBuffer = mb_count;
242 type = DXVA2_SliceControlBufferType;
246 return ff_dxva2_commit_buffer(avctx, ctx, sc,
249 ctx_pic->slice_count * sizeof(*ctx_pic->slice),
253 static int dxva2_mpeg2_start_frame(AVCodecContext *avctx,
254 av_unused const uint8_t *buffer,
255 av_unused uint32_t size)
257 const struct MpegEncContext *s = avctx->priv_data;
258 AVDXVAContext *ctx = avctx->hwaccel_context;
259 struct dxva2_picture_context *ctx_pic =
260 s->current_picture_ptr->hwaccel_picture_private;
262 if (DXVA_CONTEXT_DECODER(avctx, ctx) == NULL ||
263 DXVA_CONTEXT_CFG(avctx, ctx) == NULL ||
264 DXVA_CONTEXT_COUNT(avctx, ctx) <= 0)
268 fill_picture_parameters(avctx, ctx, s, &ctx_pic->pp);
269 fill_quantization_matrices(avctx, ctx, s, &ctx_pic->qm);
271 ctx_pic->slice_count = 0;
272 ctx_pic->bitstream_size = 0;
273 ctx_pic->bitstream = NULL;
277 static int dxva2_mpeg2_decode_slice(AVCodecContext *avctx,
278 const uint8_t *buffer, uint32_t size)
280 const struct MpegEncContext *s = avctx->priv_data;
281 struct dxva2_picture_context *ctx_pic =
282 s->current_picture_ptr->hwaccel_picture_private;
285 if (ctx_pic->slice_count >= MAX_SLICES) {
286 avpriv_request_sample(avctx, "%d slices in dxva2",
287 ctx_pic->slice_count);
290 if (!ctx_pic->bitstream)
291 ctx_pic->bitstream = buffer;
292 ctx_pic->bitstream_size += size;
294 position = buffer - ctx_pic->bitstream;
295 fill_slice(avctx, s, &ctx_pic->slice[ctx_pic->slice_count++], position,
300 static int dxva2_mpeg2_end_frame(AVCodecContext *avctx)
302 struct MpegEncContext *s = avctx->priv_data;
303 struct dxva2_picture_context *ctx_pic =
304 s->current_picture_ptr->hwaccel_picture_private;
307 if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
309 ret = ff_dxva2_common_end_frame(avctx, s->current_picture_ptr->f,
310 &ctx_pic->pp, sizeof(ctx_pic->pp),
311 &ctx_pic->qm, sizeof(ctx_pic->qm),
312 commit_bitstream_and_slice_buffer);
314 ff_mpeg_draw_horiz_band(s, 0, avctx->height);
318 #if CONFIG_MPEG2_DXVA2_HWACCEL
319 AVHWAccel ff_mpeg2_dxva2_hwaccel = {
320 .name = "mpeg2_dxva2",
321 .type = AVMEDIA_TYPE_VIDEO,
322 .id = AV_CODEC_ID_MPEG2VIDEO,
323 .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
324 .start_frame = dxva2_mpeg2_start_frame,
325 .decode_slice = dxva2_mpeg2_decode_slice,
326 .end_frame = dxva2_mpeg2_end_frame,
327 .frame_priv_data_size = sizeof(struct dxva2_picture_context),
331 #if CONFIG_MPEG2_D3D11VA_HWACCEL
332 AVHWAccel ff_mpeg2_d3d11va_hwaccel = {
333 .name = "mpeg2_d3d11va",
334 .type = AVMEDIA_TYPE_VIDEO,
335 .id = AV_CODEC_ID_MPEG2VIDEO,
336 .pix_fmt = AV_PIX_FMT_D3D11VA_VLD,
337 .start_frame = dxva2_mpeg2_start_frame,
338 .decode_slice = dxva2_mpeg2_decode_slice,
339 .end_frame = dxva2_mpeg2_end_frame,
340 .frame_priv_data_size = sizeof(struct dxva2_picture_context),