62308b403eff3e31c238fac2f18dc796eae2e1ca
[libav.git] / libavcodec / dxva2_mpeg2.c
1 /*
2 * MPEG-2 HW acceleration.
3 *
4 * copyright (c) 2010 Laurent Aimar
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/log.h"
24 #include "dxva2_internal.h"
25 #include "mpegutils.h"
26 #include "mpegvideo.h"
27
28 #define MAX_SLICES 1024
29 struct dxva2_picture_context {
30 DXVA_PictureParameters pp;
31 DXVA_QmatrixData qm;
32 unsigned slice_count;
33 DXVA_SliceInfo slice[MAX_SLICES];
34
35 const uint8_t *bitstream;
36 unsigned bitstream_size;
37 };
38
39 static void fill_picture_parameters(AVCodecContext *avctx,
40 AVDXVAContext *ctx,
41 const struct MpegEncContext *s,
42 DXVA_PictureParameters *pp)
43 {
44 const Picture *current_picture = s->current_picture_ptr;
45 int is_field = s->picture_structure != PICT_FRAME;
46
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);
52 else
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);
56 else
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;
64 pp->bBPPminus1 = 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;
75 pp->bRcontrol = 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;
82 pp->bPicOBMC = 0;
83 pp->bPicBinPB = 0;
84 pp->bMV_RPS = 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;
103 }
104
105 static void fill_quantization_matrices(AVCodecContext *avctx,
106 AVDXVAContext *ctx,
107 const struct MpegEncContext *s,
108 DXVA_QmatrixData *qm)
109 {
110 int i;
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];;
119 }
120 }
121
122 static void fill_slice(AVCodecContext *avctx,
123 const struct MpegEncContext *s,
124 DXVA_SliceInfo *slice,
125 unsigned position,
126 const uint8_t *buffer, unsigned size)
127 {
128 int is_field = s->picture_structure != PICT_FRAME;
129 GetBitContext gb;
130
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;
141
142 init_get_bits(&gb, &buffer[4], 8 * (size - 4));
143
144 slice->wQuantizerScaleCode = get_bits(&gb, 5);
145 while (get_bits1(&gb))
146 skip_bits(&gb, 8);
147
148 slice->wMBbitOffset = 4 * 8 + get_bits_count(&gb);
149 }
150 static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
151 DECODER_BUFFER_DESC *bs,
152 DECODER_BUFFER_DESC *sc)
153 {
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);
160 void *dxva_data_ptr;
161 uint8_t *dxva_data, *current, *end;
162 unsigned dxva_size;
163 unsigned i;
164 unsigned type;
165
166 #if CONFIG_D3D11VA
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,
171 type,
172 &dxva_size, &dxva_data_ptr)))
173 return -1;
174 }
175 #endif
176 #if CONFIG_DXVA2
177 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
178 type = DXVA2_BitStreamDateBufferType;
179 if (FAILED(IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder,
180 type,
181 &dxva_data_ptr, &dxva_size)))
182 return -1;
183 }
184 #endif
185
186 dxva_data = dxva_data_ptr;
187 current = dxva_data;
188 end = dxva_data + dxva_size;
189
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");
196 break;
197 }
198 slice->dwSliceDataLocation = current - dxva_data;
199
200 if (i < ctx_pic->slice_count - 1)
201 slice->wNumberMBsInSlice =
202 slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice;
203 else
204 slice->wNumberMBsInSlice =
205 mb_count - slice[0].wNumberMBsInSlice;
206
207 memcpy(current, &ctx_pic->bitstream[position], size);
208 current += size;
209 }
210 #if CONFIG_D3D11VA
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)))
213 return -1;
214 #endif
215 #if CONFIG_DXVA2
216 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
217 if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type)))
218 return -1;
219 #endif
220 if (i < ctx_pic->slice_count)
221 return -1;
222
223 #if CONFIG_D3D11VA
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;
230
231 type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
232 }
233 #endif
234 #if CONFIG_DXVA2
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;
241
242 type = DXVA2_SliceControlBufferType;
243 }
244 #endif
245
246 return ff_dxva2_commit_buffer(avctx, ctx, sc,
247 type,
248 ctx_pic->slice,
249 ctx_pic->slice_count * sizeof(*ctx_pic->slice),
250 mb_count);
251 }
252
253 static int dxva2_mpeg2_start_frame(AVCodecContext *avctx,
254 av_unused const uint8_t *buffer,
255 av_unused uint32_t size)
256 {
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;
261
262 if (DXVA_CONTEXT_DECODER(avctx, ctx) == NULL ||
263 DXVA_CONTEXT_CFG(avctx, ctx) == NULL ||
264 DXVA_CONTEXT_COUNT(avctx, ctx) <= 0)
265 return -1;
266 assert(ctx_pic);
267
268 fill_picture_parameters(avctx, ctx, s, &ctx_pic->pp);
269 fill_quantization_matrices(avctx, ctx, s, &ctx_pic->qm);
270
271 ctx_pic->slice_count = 0;
272 ctx_pic->bitstream_size = 0;
273 ctx_pic->bitstream = NULL;
274 return 0;
275 }
276
277 static int dxva2_mpeg2_decode_slice(AVCodecContext *avctx,
278 const uint8_t *buffer, uint32_t size)
279 {
280 const struct MpegEncContext *s = avctx->priv_data;
281 struct dxva2_picture_context *ctx_pic =
282 s->current_picture_ptr->hwaccel_picture_private;
283 unsigned position;
284
285 if (ctx_pic->slice_count >= MAX_SLICES) {
286 avpriv_request_sample(avctx, "%d slices in dxva2",
287 ctx_pic->slice_count);
288 return -1;
289 }
290 if (!ctx_pic->bitstream)
291 ctx_pic->bitstream = buffer;
292 ctx_pic->bitstream_size += size;
293
294 position = buffer - ctx_pic->bitstream;
295 fill_slice(avctx, s, &ctx_pic->slice[ctx_pic->slice_count++], position,
296 buffer, size);
297 return 0;
298 }
299
300 static int dxva2_mpeg2_end_frame(AVCodecContext *avctx)
301 {
302 struct MpegEncContext *s = avctx->priv_data;
303 struct dxva2_picture_context *ctx_pic =
304 s->current_picture_ptr->hwaccel_picture_private;
305 int ret;
306
307 if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
308 return -1;
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);
313 if (!ret)
314 ff_mpeg_draw_horiz_band(s, 0, avctx->height);
315 return ret;
316 }
317
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),
328 };
329 #endif
330
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),
341 };
342 #endif