FFmpeg
Loading...
Searching...
No Matches
dxva2_mpeg2.c
Go to the documentation of this file.
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 "config_components.h"
24
25#include "libavutil/log.h"
26
27#include "dxva2_internal.h"
28#include "hwaccel_internal.h"
29#include "mpegutils.h"
30#include "mpegvideodec.h"
31
32#define MAX_SLICES 1024
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
45 DXVA_PictureParameters *pp)
46{
47 const struct MpegEncContext *s = avctx->priv_data;
48 const MPVPicture *current_picture = s->cur_pic.ptr;
49 int is_field = s->picture_structure != PICT_FRAME;
50
51 memset(pp, 0, sizeof(*pp));
52 pp->wDeblockedPictureIndex = 0;
53 if (s->pict_type != AV_PICTURE_TYPE_I)
54 pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->last_pic.ptr->f, 0);
55 else
56 pp->wForwardRefPictureIndex = 0xffff;
57 if (s->pict_type == AV_PICTURE_TYPE_B)
58 pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->next_pic.ptr->f, 0);
59 else
60 pp->wBackwardRefPictureIndex = 0xffff;
61 pp->wDecodedPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, current_picture->f, 1);
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
111 DXVA_QmatrixData *qm)
112{
113 const struct MpegEncContext *s = avctx->priv_data;
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
127 DXVA_SliceInfo *slice,
128 unsigned position,
129 const uint8_t *buffer, unsigned size)
130{
131 const struct MpegEncContext *s = avctx->priv_data;
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);
150
151 slice->wMBbitOffset = 4 * 8 + get_bits_count(&gb);
152}
156{
157 const struct MpegEncContext *s = avctx->priv_data;
159 struct dxva2_picture_context *ctx_pic =
160 s->cur_pic.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 = NULL;
164 uint8_t *dxva_data, *current, *end;
165 unsigned dxva_size;
166 unsigned i;
167 unsigned type;
168
169#if CONFIG_D3D11VA
170 if (ff_dxva2_is_d3d11(avctx)) {
171 type = D3D11_VIDEO_DECODER_BUFFER_BITSTREAM;
172 if (FAILED(ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
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 if (!dxva_data_ptr)
190 return -1;
191
192 dxva_data = dxva_data_ptr;
193 current = dxva_data;
194 end = dxva_data + dxva_size;
195
196 for (i = 0; i < ctx_pic->slice_count; i++) {
197 DXVA_SliceInfo *slice = &ctx_pic->slice[i];
198 unsigned position = slice->dwSliceDataLocation;
199 unsigned size = slice->dwSliceBitsInBuffer / 8;
200 if (size > end - current) {
201 av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
202 break;
203 }
204 slice->dwSliceDataLocation = current - dxva_data;
205
207 slice->wNumberMBsInSlice =
208 slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice;
209 else
210 slice->wNumberMBsInSlice =
211 mb_count - slice[0].wNumberMBsInSlice;
212
213 memcpy(current, &ctx_pic->bitstream[position], size);
214 current += size;
215 }
216#if CONFIG_D3D11VA
217 if (ff_dxva2_is_d3d11(avctx))
218 if (FAILED(ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type)))
219 return -1;
220#endif
221#if CONFIG_DXVA2
222 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
223 if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type)))
224 return -1;
225#endif
227 return -1;
228
229#if CONFIG_D3D11VA
230 if (ff_dxva2_is_d3d11(avctx)) {
231 D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = bs;
232 memset(dsc11, 0, sizeof(*dsc11));
233 dsc11->BufferType = type;
234 dsc11->DataSize = current - dxva_data;
235 dsc11->NumMBsInBuffer = mb_count;
236
237 type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
238 }
239#endif
240#if CONFIG_DXVA2
241 if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
242 DXVA2_DecodeBufferDesc *dsc2 = bs;
243 memset(dsc2, 0, sizeof(*dsc2));
244 dsc2->CompressedBufferType = type;
245 dsc2->DataSize = current - dxva_data;
246 dsc2->NumMBsInBuffer = mb_count;
247
248 type = DXVA2_SliceControlBufferType;
249 }
250#endif
251
252 return ff_dxva2_commit_buffer(avctx, ctx, sc,
253 type,
254 ctx_pic->slice,
255 ctx_pic->slice_count * sizeof(*ctx_pic->slice),
256 mb_count);
257}
258
260 av_unused const AVBufferRef *buffer_ref,
261 av_unused const uint8_t *buffer,
262 av_unused uint32_t size)
263{
264 const struct MpegEncContext *s = avctx->priv_data;
266 struct dxva2_picture_context *ctx_pic =
267 s->cur_pic.ptr->hwaccel_picture_private;
268
269 if (!DXVA_CONTEXT_VALID(avctx, ctx))
270 return -1;
271 av_assert0(ctx_pic);
272
275
276 ctx_pic->slice_count = 0;
277 ctx_pic->bitstream_size = 0;
278 ctx_pic->bitstream = NULL;
279 return 0;
280}
281
283 const uint8_t *buffer, uint32_t size)
284{
285 const struct MpegEncContext *s = avctx->priv_data;
286 struct dxva2_picture_context *ctx_pic =
287 s->cur_pic.ptr->hwaccel_picture_private;
288 unsigned position;
289
290 if (ctx_pic->slice_count >= MAX_SLICES) {
291 avpriv_request_sample(avctx, "%d slices in dxva2",
292 ctx_pic->slice_count);
293 return -1;
294 }
295 if (!ctx_pic->bitstream)
296 ctx_pic->bitstream = buffer;
297 ctx_pic->bitstream_size += size;
298
299 position = buffer - ctx_pic->bitstream;
300 ff_dxva2_mpeg2_fill_slice(avctx, &ctx_pic->slice[ctx_pic->slice_count++], position,
301 buffer, size);
302 return 0;
303}
304
306{
307 struct MpegEncContext *s = avctx->priv_data;
308 struct dxva2_picture_context *ctx_pic =
309 s->cur_pic.ptr->hwaccel_picture_private;
310 int ret;
311
312 if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
313 return -1;
314 ret = ff_dxva2_common_end_frame(avctx, s->cur_pic.ptr->f,
315 &ctx_pic->pp, sizeof(ctx_pic->pp),
316 &ctx_pic->qm, sizeof(ctx_pic->qm),
318 if (!ret)
319 ff_mpeg_draw_horiz_band(s, 0, avctx->height);
320 return ret;
321}
322
323#if CONFIG_MPEG2_DXVA2_HWACCEL
325 .p.name = "mpeg2_dxva2",
326 .p.type = AVMEDIA_TYPE_VIDEO,
328 .p.pix_fmt = AV_PIX_FMT_DXVA2_VLD,
329 .init = ff_dxva2_decode_init,
330 .uninit = ff_dxva2_decode_uninit,
331 .start_frame = dxva2_mpeg2_start_frame,
332 .decode_slice = dxva2_mpeg2_decode_slice,
333 .end_frame = dxva2_mpeg2_end_frame,
334 .frame_params = ff_dxva2_common_frame_params,
335 .frame_priv_data_size = sizeof(struct dxva2_picture_context),
336 .priv_data_size = sizeof(FFDXVASharedContext),
337};
338#endif
339
340#if CONFIG_MPEG2_D3D11VA_HWACCEL
342 .p.name = "mpeg2_d3d11va",
343 .p.type = AVMEDIA_TYPE_VIDEO,
345 .p.pix_fmt = AV_PIX_FMT_D3D11VA_VLD,
346 .init = ff_dxva2_decode_init,
347 .uninit = ff_dxva2_decode_uninit,
348 .start_frame = dxva2_mpeg2_start_frame,
349 .decode_slice = dxva2_mpeg2_decode_slice,
350 .end_frame = dxva2_mpeg2_end_frame,
351 .frame_params = ff_dxva2_common_frame_params,
352 .frame_priv_data_size = sizeof(struct dxva2_picture_context),
353 .priv_data_size = sizeof(FFDXVASharedContext),
354};
355#endif
356
357#if CONFIG_MPEG2_D3D11VA2_HWACCEL
359 .p.name = "mpeg2_d3d11va2",
360 .p.type = AVMEDIA_TYPE_VIDEO,
362 .p.pix_fmt = AV_PIX_FMT_D3D11,
363 .init = ff_dxva2_decode_init,
364 .uninit = ff_dxva2_decode_uninit,
365 .start_frame = dxva2_mpeg2_start_frame,
366 .decode_slice = dxva2_mpeg2_decode_slice,
367 .end_frame = dxva2_mpeg2_end_frame,
368 .frame_params = ff_dxva2_common_frame_params,
369 .frame_priv_data_size = sizeof(struct dxva2_picture_context),
370 .priv_data_size = sizeof(FFDXVASharedContext),
371};
372#endif
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
#define MAX_SLICES
int ff_dxva2_is_d3d11(const AVCodecContext *avctx)
Definition dxva2.c:1068
unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx, AVDXVAContext *ctx, const AVFrame *frame, int curr)
Definition dxva2.c:783
int ff_dxva2_decode_init(AVCodecContext *avctx)
Definition dxva2.c:668
int ff_dxva2_commit_buffer(AVCodecContext *avctx, AVDXVAContext *ctx, DECODER_BUFFER_DESC *dsc, unsigned type, const void *data, unsigned size, unsigned mb_count)
Definition dxva2.c:814
int ff_dxva2_decode_uninit(AVCodecContext *avctx)
Definition dxva2.c:743
int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame, const void *pp, unsigned pp_size, const void *qm, unsigned qm_size, int(*commit_bs_si)(AVCodecContext *, DECODER_BUFFER_DESC *bs, DECODER_BUFFER_DESC *slice))
Definition dxva2.c:903
int ff_dxva2_common_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
Definition dxva2.c:602
static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx, DECODER_BUFFER_DESC *bs, DECODER_BUFFER_DESC *sc)
Definition dxva2_av1.c:351
#define DXVA_CONTEXT(avctx)
#define DXVA2_CONTEXT(ctx)
#define D3D11VA_CONTEXT(ctx)
#define DXVA_CONTEXT_VALID(avctx, ctx)
void DECODER_BUFFER_DESC
void ff_dxva2_mpeg2_fill_quantization_matrices(AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_QmatrixData *qm)
static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx, DECODER_BUFFER_DESC *bs, DECODER_BUFFER_DESC *sc)
static int dxva2_mpeg2_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
void ff_dxva2_mpeg2_fill_picture_parameters(AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PictureParameters *pp)
Definition dxva2_mpeg2.c:43
static int dxva2_mpeg2_start_frame(AVCodecContext *avctx, av_unused const AVBufferRef *buffer_ref, av_unused const uint8_t *buffer, av_unused uint32_t size)
void ff_dxva2_mpeg2_fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice, unsigned position, const uint8_t *buffer, unsigned size)
static int dxva2_mpeg2_end_frame(AVCodecContext *avctx)
#define MAX_SLICES
Definition dxva2_vc1.c:32
static struct @111144215057303131116103221376075045141373005341 current
static int skip_1stop_8data_bits(GetBitContext *gb)
Definition get_bits.h:693
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition avutil.h:280
const struct FFHWAccel ff_mpeg2_d3d11va_hwaccel
const struct FFHWAccel ff_mpeg2_dxva2_hwaccel
const struct FFHWAccel ff_mpeg2_d3d11va2_hwaccel
cl_device_type type
static const chunk_decoder decoder[8]
Definition dfa.c:331
#define av_unused
Definition attributes.h:164
const uint8_t ff_zigzag_direct[64]
Definition mathtables.c:137
#define PICT_FRAME
Definition mpegutils.h:33
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
mpegvideo decoder header.
@ AV_PIX_FMT_DXVA2_VLD
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer.
Definition pixfmt.h:134
@ AV_PIX_FMT_D3D11
Hardware surfaces for Direct3D11.
Definition pixfmt.h:336
@ AV_PIX_FMT_D3D11VA_VLD
HW decoding through Direct3D11 via old API, Picture.data[3] contains a ID3D11VideoDecoderOutputView p...
Definition pixfmt.h:254
A reference to a data buffer.
Definition buffer.h:82
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
void * priv_data
Definition avcodec.h:470
MPVPicture.
Definition mpegpicture.h:58
struct AVFrame * f
Definition mpegpicture.h:59
MpegEncContext.
Definition mpegvideo.h:67
struct AVCodecContext * avctx
Definition mpegvideo.h:82
ScratchpadContext sc
Definition mpegvideo.h:150
DXVA_SliceInfo slice[MAX_SLICES]
Definition dxva2_mpeg2.c:37
DXVA_PicParams_H264 pp
Definition dxva2_h264.c:35
const uint8_t * bitstream
Definition dxva2_h264.c:40
DXVA_Qmatrix_H264 qm
Definition dxva2_h264.c:36
#define avpriv_request_sample(...)
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
static char buffer[20]
Definition seek.c:32
int size