summaryrefslogtreecommitdiff
path: root/libavcodec/dxva2_mpeg2.c (plain)
blob: 14ac48f5ada7e5d128ff5e6e00a284b0b945ada2
1/*
2 * MPEG-2 HW acceleration.
3 *
4 * copyright (c) 2010 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 "libavutil/log.h"
24#include "mpegutils.h"
25#include "mpegvideo.h"
26
27// The headers above may include w32threads.h, which uses the original
28// _WIN32_WINNT define, while dxva2_internal.h redefines it to target a
29// potentially newer version.
30#include "dxva2_internal.h"
31
32#define MAX_SLICES 1024
33struct dxva2_picture_context {
34 DXVA_PictureParameters pp;
35 DXVA_QmatrixData qm;
36 unsigned slice_count;
37 DXVA_SliceInfo slice[MAX_SLICES];
38
39 const uint8_t *bitstream;
40 unsigned bitstream_size;
41};
42
43static void fill_picture_parameters(AVCodecContext *avctx,
44 AVDXVAContext *ctx,
45 const struct MpegEncContext *s,
46 DXVA_PictureParameters *pp)
47{
48 const Picture *current_picture = s->current_picture_ptr;
49 int is_field = s->picture_structure != PICT_FRAME;
50
51 memset(pp, 0, sizeof(*pp));
52 pp->wDecodedPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, current_picture->f);
53 pp->wDeblockedPictureIndex = 0;
54 if (s->pict_type != AV_PICTURE_TYPE_I)
55 pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->last_picture.f);
56 else
57 pp->wForwardRefPictureIndex = 0xffff;
58 if (s->pict_type == AV_PICTURE_TYPE_B)
59 pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->next_picture.f);
60 else
61 pp->wBackwardRefPictureIndex = 0xffff;
62 pp->wPicWidthInMBminus1 = s->mb_width - 1;
63 pp->wPicHeightInMBminus1 = (s->mb_height >> is_field) - 1;
64 pp->bMacroblockWidthMinus1 = 15;
65 pp->bMacroblockHeightMinus1 = 15;
66 pp->bBlockWidthMinus1 = 7;
67 pp->bBlockHeightMinus1 = 7;
68 pp->bBPPminus1 = 7;
69 pp->bPicStructure = s->picture_structure;
70 pp->bSecondField = is_field && !s->first_field;
71 pp->bPicIntra = s->pict_type == AV_PICTURE_TYPE_I;
72 pp->bPicBackwardPrediction = s->pict_type == AV_PICTURE_TYPE_B;
73 pp->bBidirectionalAveragingMode = 0;
74 pp->bMVprecisionAndChromaRelation= 0; /* FIXME */
75 pp->bChromaFormat = s->chroma_format;
76 pp->bPicScanFixed = 1;
77 pp->bPicScanMethod = s->alternate_scan ? 1 : 0;
78 pp->bPicReadbackRequests = 0;
79 pp->bRcontrol = 0;
80 pp->bPicSpatialResid8 = 0;
81 pp->bPicOverflowBlocks = 0;
82 pp->bPicExtrapolation = 0;
83 pp->bPicDeblocked = 0;
84 pp->bPicDeblockConfined = 0;
85 pp->bPic4MVallowed = 0;
86 pp->bPicOBMC = 0;
87 pp->bPicBinPB = 0;
88 pp->bMV_RPS = 0;
89 pp->bReservedBits = 0;
90 pp->wBitstreamFcodes = (s->mpeg_f_code[0][0] << 12) |
91 (s->mpeg_f_code[0][1] << 8) |
92 (s->mpeg_f_code[1][0] << 4) |
93 (s->mpeg_f_code[1][1] );
94 pp->wBitstreamPCEelements = (s->intra_dc_precision << 14) |
95 (s->picture_structure << 12) |
96 (s->top_field_first << 11) |
97 (s->frame_pred_frame_dct << 10) |
98 (s->concealment_motion_vectors << 9) |
99 (s->q_scale_type << 8) |
100 (s->intra_vlc_format << 7) |
101 (s->alternate_scan << 6) |
102 (s->repeat_first_field << 5) |
103 (s->chroma_420_type << 4) |
104 (s->progressive_frame << 3);
105 pp->bBitstreamConcealmentNeed = 0;
106 pp->bBitstreamConcealmentMethod = 0;
107}
108
109static void fill_quantization_matrices(AVCodecContext *avctx,
110 AVDXVAContext *ctx,
111 const struct MpegEncContext *s,
112 DXVA_QmatrixData *qm)
113{
114 int i;
115 for (i = 0; i < 4; i++)
116 qm->bNewQmatrix[i] = 1;
117 for (i = 0; i < 64; i++) {
118 int n = s->idsp.idct_permutation[ff_zigzag_direct[i]];
119 qm->Qmatrix[0][i] = s->intra_matrix[n];
120 qm->Qmatrix[1][i] = s->inter_matrix[n];
121 qm->Qmatrix[2][i] = s->chroma_intra_matrix[n];
122 qm->Qmatrix[3][i] = s->chroma_inter_matrix[n];
123 }
124}
125
126static void fill_slice(AVCodecContext *avctx,
127 const struct MpegEncContext *s,
128 DXVA_SliceInfo *slice,
129 unsigned position,
130 const uint8_t *buffer, unsigned size)
131{
132 int is_field = s->picture_structure != PICT_FRAME;
133 GetBitContext gb;
134
135 memset(slice, 0, sizeof(*slice));
136 slice->wHorizontalPosition = s->mb_x;
137 slice->wVerticalPosition = s->mb_y >> is_field;
138 slice->dwSliceBitsInBuffer = 8 * size;
139 slice->dwSliceDataLocation = position;
140 slice->bStartCodeBitOffset = 0;
141 slice->bReservedBits = 0;
142 /* XXX We store the index of the first MB and it will be fixed later */
143 slice->wNumberMBsInSlice = (s->mb_y >> is_field) * s->mb_width + s->mb_x;
144 slice->wBadSliceChopping = 0;
145
146 init_get_bits(&gb, &buffer[4], 8 * (size - 4));
147
148 slice->wQuantizerScaleCode = get_bits(&gb, 5);
149 skip_1stop_8data_bits(&gb);
150
151 slice->wMBbitOffset = 4 * 8 + get_bits_count(&gb);
152}
153static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
154 DECODER_BUFFER_DESC *bs,
155 DECODER_BUFFER_DESC *sc)
156{
157 const struct MpegEncContext *s = avctx->priv_data;
158 AVDXVAContext *ctx = avctx->hwaccel_context;
159 struct dxva2_picture_context *ctx_pic =
160 s->current_picture_ptr->hwaccel_picture_private;
161 const int is_field = s->picture_structure != PICT_FRAME;
162 const unsigned mb_count = s->mb_width * (s->mb_height >> is_field);
163 void *dxva_data_ptr;
164 uint8_t *dxva_data, *current, *end;
165 unsigned dxva_size;
166 unsigned i;
167 unsigned type;
168
169#if CONFIG_D3D11VA
170 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
171 type = D3D11_VIDEO_DECODER_BUFFER_BITSTREAM;
172 if (FAILED(ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
173 D3D11VA_CONTEXT(ctx)->decoder,
174 type,
175 &dxva_size, &dxva_data_ptr)))
176 return -1;
177 }
178#endif
179#if CONFIG_DXVA2
180 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
181 type = DXVA2_BitStreamDateBufferType;
182 if (FAILED(IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder,
183 type,
184 &dxva_data_ptr, &dxva_size)))
185 return -1;
186 }
187#endif
188
189 dxva_data = dxva_data_ptr;
190 current = dxva_data;
191 end = dxva_data + dxva_size;
192
193 for (i = 0; i < ctx_pic->slice_count; i++) {
194 DXVA_SliceInfo *slice = &ctx_pic->slice[i];
195 unsigned position = slice->dwSliceDataLocation;
196 unsigned size = slice->dwSliceBitsInBuffer / 8;
197 if (size > end - current) {
198 av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
199 break;
200 }
201 slice->dwSliceDataLocation = current - dxva_data;
202
203 if (i < ctx_pic->slice_count - 1)
204 slice->wNumberMBsInSlice =
205 slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice;
206 else
207 slice->wNumberMBsInSlice =
208 mb_count - slice[0].wNumberMBsInSlice;
209
210 memcpy(current, &ctx_pic->bitstream[position], size);
211 current += size;
212 }
213#if CONFIG_D3D11VA
214 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
215 if (FAILED(ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type)))
216 return -1;
217#endif
218#if CONFIG_DXVA2
219 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
220 if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type)))
221 return -1;
222#endif
223 if (i < ctx_pic->slice_count)
224 return -1;
225
226#if CONFIG_D3D11VA
227 if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
228 D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = bs;
229 memset(dsc11, 0, sizeof(*dsc11));
230 dsc11->BufferType = type;
231 dsc11->DataSize = current - dxva_data;
232 dsc11->NumMBsInBuffer = mb_count;
233
234 type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
235 }
236#endif
237#if CONFIG_DXVA2
238 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
239 DXVA2_DecodeBufferDesc *dsc2 = bs;
240 memset(dsc2, 0, sizeof(*dsc2));
241 dsc2->CompressedBufferType = type;
242 dsc2->DataSize = current - dxva_data;
243 dsc2->NumMBsInBuffer = mb_count;
244
245 type = DXVA2_SliceControlBufferType;
246 }
247#endif
248
249 return ff_dxva2_commit_buffer(avctx, ctx, sc,
250 type,
251 ctx_pic->slice,
252 ctx_pic->slice_count * sizeof(*ctx_pic->slice),
253 mb_count);
254}
255
256static int dxva2_mpeg2_start_frame(AVCodecContext *avctx,
257 av_unused const uint8_t *buffer,
258 av_unused uint32_t size)
259{
260 const struct MpegEncContext *s = avctx->priv_data;
261 AVDXVAContext *ctx = avctx->hwaccel_context;
262 struct dxva2_picture_context *ctx_pic =
263 s->current_picture_ptr->hwaccel_picture_private;
264
265 if (!DXVA_CONTEXT_VALID(avctx, ctx))
266 return -1;
267 assert(ctx_pic);
268
269 fill_picture_parameters(avctx, ctx, s, &ctx_pic->pp);
270 fill_quantization_matrices(avctx, ctx, s, &ctx_pic->qm);
271
272 ctx_pic->slice_count = 0;
273 ctx_pic->bitstream_size = 0;
274 ctx_pic->bitstream = NULL;
275 return 0;
276}
277
278static int dxva2_mpeg2_decode_slice(AVCodecContext *avctx,
279 const uint8_t *buffer, uint32_t size)
280{
281 const struct MpegEncContext *s = avctx->priv_data;
282 struct dxva2_picture_context *ctx_pic =
283 s->current_picture_ptr->hwaccel_picture_private;
284 unsigned position;
285
286 if (ctx_pic->slice_count >= MAX_SLICES) {
287 avpriv_request_sample(avctx, "%d slices in dxva2",
288 ctx_pic->slice_count);
289 return -1;
290 }
291 if (!ctx_pic->bitstream)
292 ctx_pic->bitstream = buffer;
293 ctx_pic->bitstream_size += size;
294
295 position = buffer - ctx_pic->bitstream;
296 fill_slice(avctx, s, &ctx_pic->slice[ctx_pic->slice_count++], position,
297 buffer, size);
298 return 0;
299}
300
301static int dxva2_mpeg2_end_frame(AVCodecContext *avctx)
302{
303 struct MpegEncContext *s = avctx->priv_data;
304 struct dxva2_picture_context *ctx_pic =
305 s->current_picture_ptr->hwaccel_picture_private;
306 int ret;
307
308 if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
309 return -1;
310 ret = ff_dxva2_common_end_frame(avctx, s->current_picture_ptr->f,
311 &ctx_pic->pp, sizeof(ctx_pic->pp),
312 &ctx_pic->qm, sizeof(ctx_pic->qm),
313 commit_bitstream_and_slice_buffer);
314 if (!ret)
315 ff_mpeg_draw_horiz_band(s, 0, avctx->height);
316 return ret;
317}
318
319#if CONFIG_MPEG2_DXVA2_HWACCEL
320AVHWAccel ff_mpeg2_dxva2_hwaccel = {
321 .name = "mpeg2_dxva2",
322 .type = AVMEDIA_TYPE_VIDEO,
323 .id = AV_CODEC_ID_MPEG2VIDEO,
324 .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
325 .start_frame = dxva2_mpeg2_start_frame,
326 .decode_slice = dxva2_mpeg2_decode_slice,
327 .end_frame = dxva2_mpeg2_end_frame,
328 .frame_priv_data_size = sizeof(struct dxva2_picture_context),
329};
330#endif
331
332#if CONFIG_MPEG2_D3D11VA_HWACCEL
333AVHWAccel ff_mpeg2_d3d11va_hwaccel = {
334 .name = "mpeg2_d3d11va",
335 .type = AVMEDIA_TYPE_VIDEO,
336 .id = AV_CODEC_ID_MPEG2VIDEO,
337 .pix_fmt = AV_PIX_FMT_D3D11VA_VLD,
338 .start_frame = dxva2_mpeg2_start_frame,
339 .decode_slice = dxva2_mpeg2_decode_slice,
340 .end_frame = dxva2_mpeg2_end_frame,
341 .frame_priv_data_size = sizeof(struct dxva2_picture_context),
342};
343#endif
344