annotate dxva2_mpeg2.c @ 12178:b3c39e9d4d3e libavcodec

aacenc: Template quantize_and_encode_band_cost().
author alexc
date Fri, 16 Jul 2010 20:02:46 +0000
parents d62a89be5458
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11884
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
1 /*
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
2 * MPEG-2 HW acceleration.
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
3 *
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
4 * copyright (c) 2010 Laurent Aimar
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
5 *
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
6 * This file is part of FFmpeg.
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
7 *
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
12 *
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
16 * Lesser General Public License for more details.
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
17 *
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
21 */
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
22
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
23 #include "dxva2_internal.h"
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
24
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
25 #define MAX_SLICES (SLICE_MAX_START_CODE - SLICE_MIN_START_CODE + 1)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
26 struct dxva2_picture_context {
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
27 DXVA_PictureParameters pp;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
28 DXVA_QmatrixData qm;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
29 unsigned slice_count;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
30 DXVA_SliceInfo slice[MAX_SLICES];
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
31
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
32 const uint8_t *bitstream;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
33 unsigned bitstream_size;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
34 };
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
35
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
36 static void fill_picture_parameters(AVCodecContext *avctx,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
37 struct dxva_context *ctx,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
38 const struct MpegEncContext *s,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
39 DXVA_PictureParameters *pp)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
40 {
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
41 const Picture *current_picture = s->current_picture_ptr;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
42 int is_field = s->picture_structure != PICT_FRAME;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
43
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
44 memset(pp, 0, sizeof(*pp));
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
45 pp->wDecodedPictureIndex = ff_dxva2_get_surface_index(ctx, current_picture);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
46 pp->wDeblockedPictureIndex = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
47 if (s->pict_type != FF_I_TYPE)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
48 pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, &s->last_picture);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
49 else
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
50 pp->wForwardRefPictureIndex = 0xffff;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
51 if (s->pict_type == FF_B_TYPE)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
52 pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, &s->next_picture);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
53 else
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
54 pp->wBackwardRefPictureIndex = 0xffff;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
55 pp->wPicWidthInMBminus1 = s->mb_width - 1;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
56 pp->wPicHeightInMBminus1 = (s->mb_height >> is_field) - 1;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
57 pp->bMacroblockWidthMinus1 = 15;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
58 pp->bMacroblockHeightMinus1 = 15;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
59 pp->bBlockWidthMinus1 = 7;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
60 pp->bBlockHeightMinus1 = 7;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
61 pp->bBPPminus1 = 7;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
62 pp->bPicStructure = s->picture_structure;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
63 pp->bSecondField = is_field && !s->first_field;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
64 pp->bPicIntra = s->pict_type == FF_I_TYPE;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
65 pp->bPicBackwardPrediction = s->pict_type == FF_B_TYPE;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
66 pp->bBidirectionalAveragingMode = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
67 pp->bMVprecisionAndChromaRelation= 0; /* FIXME */
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
68 pp->bChromaFormat = s->chroma_format;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
69 pp->bPicScanFixed = 1;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
70 pp->bPicScanMethod = s->alternate_scan ? 1 : 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
71 pp->bPicReadbackRequests = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
72 pp->bRcontrol = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
73 pp->bPicSpatialResid8 = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
74 pp->bPicOverflowBlocks = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
75 pp->bPicExtrapolation = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
76 pp->bPicDeblocked = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
77 pp->bPicDeblockConfined = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
78 pp->bPic4MVallowed = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
79 pp->bPicOBMC = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
80 pp->bPicBinPB = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
81 pp->bMV_RPS = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
82 pp->bReservedBits = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
83 pp->wBitstreamFcodes = (s->mpeg_f_code[0][0] << 12) |
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
84 (s->mpeg_f_code[0][1] << 8) |
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
85 (s->mpeg_f_code[1][0] << 4) |
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
86 (s->mpeg_f_code[1][1] );
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
87 pp->wBitstreamPCEelements = (s->intra_dc_precision << 14) |
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
88 (s->picture_structure << 12) |
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
89 (s->top_field_first << 11) |
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
90 (s->frame_pred_frame_dct << 10) |
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
91 (s->concealment_motion_vectors << 9) |
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
92 (s->q_scale_type << 8) |
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
93 (s->intra_vlc_format << 7) |
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
94 (s->alternate_scan << 6) |
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
95 (s->repeat_first_field << 5) |
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
96 (s->chroma_420_type << 4) |
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
97 (s->progressive_frame << 3);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
98 pp->bBitstreamConcealmentNeed = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
99 pp->bBitstreamConcealmentMethod = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
100 }
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
101
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
102 static void fill_quantization_matrices(AVCodecContext *avctx,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
103 struct dxva_context *ctx,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
104 const struct MpegEncContext *s,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
105 DXVA_QmatrixData *qm)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
106 {
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
107 int i;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
108 for (i = 0; i < 4; i++)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
109 qm->bNewQmatrix[i] = 1;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
110 for (i = 0; i < 64; i++) {
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
111 int n = s->dsp.idct_permutation[ff_zigzag_direct[i]];
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
112 qm->Qmatrix[0][i] = s->intra_matrix[n];;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
113 qm->Qmatrix[1][i] = s->inter_matrix[n];;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
114 qm->Qmatrix[2][i] = s->chroma_intra_matrix[n];;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
115 qm->Qmatrix[3][i] = s->chroma_inter_matrix[n];;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
116 }
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
117 }
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
118
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
119 static void fill_slice(AVCodecContext *avctx,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
120 const struct MpegEncContext *s,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
121 DXVA_SliceInfo *slice,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
122 unsigned position,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
123 const uint8_t *buffer, unsigned size)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
124 {
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
125 int is_field = s->picture_structure != PICT_FRAME;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
126 GetBitContext gb;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
127
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
128 memset(slice, 0, sizeof(*slice));
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
129 slice->wHorizontalPosition = s->mb_x;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
130 slice->wVerticalPosition = s->mb_y >> is_field;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
131 slice->dwSliceBitsInBuffer = 8 * size;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
132 slice->dwSliceDataLocation = position;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
133 slice->bStartCodeBitOffset = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
134 slice->bReservedBits = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
135 /* XXX We store the index of the first MB and it will be fixed later */
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
136 slice->wNumberMBsInSlice = (s->mb_y >> is_field) * s->mb_width + s->mb_x;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
137 slice->wBadSliceChopping = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
138
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
139 init_get_bits(&gb, &buffer[4], 8 * (size - 4));
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
140
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
141 slice->wQuantizerScaleCode = get_bits(&gb, 5);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
142 while (get_bits1(&gb))
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
143 skip_bits(&gb, 8);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
144
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
145 slice->wMBbitOffset = 4 * 8 + get_bits_count(&gb);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
146 }
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
147 static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
148 DXVA2_DecodeBufferDesc *bs,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
149 DXVA2_DecodeBufferDesc *sc)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
150 {
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
151 const struct MpegEncContext *s = avctx->priv_data;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
152 struct dxva_context *ctx = avctx->hwaccel_context;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
153 struct dxva2_picture_context *ctx_pic =
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
154 s->current_picture_ptr->hwaccel_picture_private;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
155 const int is_field = s->picture_structure != PICT_FRAME;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
156 const unsigned mb_count = s->mb_width * (s->mb_height >> is_field);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
157 uint8_t *dxva_data, *current, *end;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
158 unsigned dxva_size;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
159 unsigned i;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
160
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
161 if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
162 DXVA2_BitStreamDateBufferType,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
163 &dxva_data, &dxva_size)))
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
164 return -1;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
165 current = dxva_data;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
166 end = dxva_data + dxva_size;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
167
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
168 for (i = 0; i < ctx_pic->slice_count; i++) {
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
169 DXVA_SliceInfo *slice = &ctx_pic->slice[i];
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
170 unsigned position = slice->dwSliceDataLocation;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
171 unsigned size = slice->dwSliceBitsInBuffer / 8;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
172 if (size > end - current) {
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
173 av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
174 break;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
175 }
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
176 slice->dwSliceDataLocation = current - dxva_data;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
177
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
178 if (i < ctx_pic->slice_count - 1)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
179 slice->wNumberMBsInSlice =
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
180 slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
181 else
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
182 slice->wNumberMBsInSlice =
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
183 mb_count - slice[0].wNumberMBsInSlice;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
184
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
185 memcpy(current, &ctx_pic->bitstream[position], size);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
186 current += size;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
187 }
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
188 if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
189 DXVA2_BitStreamDateBufferType)))
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
190 return -1;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
191 if (i < ctx_pic->slice_count)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
192 return -1;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
193
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
194 memset(bs, 0, sizeof(*bs));
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
195 bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
196 bs->DataSize = current - dxva_data;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
197 bs->NumMBsInBuffer = mb_count;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
198
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
199 return ff_dxva2_commit_buffer(avctx, ctx, sc,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
200 DXVA2_SliceControlBufferType,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
201 ctx_pic->slice,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
202 ctx_pic->slice_count * sizeof(*ctx_pic->slice),
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
203 mb_count);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
204 }
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
205
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
206 static int start_frame(AVCodecContext *avctx,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
207 av_unused const uint8_t *buffer,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
208 av_unused uint32_t size)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
209 {
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
210 const struct MpegEncContext *s = avctx->priv_data;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
211 struct dxva_context *ctx = avctx->hwaccel_context;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
212 struct dxva2_picture_context *ctx_pic =
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
213 s->current_picture_ptr->hwaccel_picture_private;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
214
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
215 if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
216 return -1;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
217 assert(ctx_pic);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
218
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
219 fill_picture_parameters(avctx, ctx, s, &ctx_pic->pp);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
220 fill_quantization_matrices(avctx, ctx, s, &ctx_pic->qm);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
221
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
222 ctx_pic->slice_count = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
223 ctx_pic->bitstream_size = 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
224 ctx_pic->bitstream = NULL;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
225 return 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
226 }
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
227
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
228 static int decode_slice(AVCodecContext *avctx,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
229 const uint8_t *buffer, uint32_t size)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
230 {
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
231 const struct MpegEncContext *s = avctx->priv_data;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
232 struct dxva2_picture_context *ctx_pic =
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
233 s->current_picture_ptr->hwaccel_picture_private;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
234 unsigned position;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
235
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
236 if (ctx_pic->slice_count >= MAX_SLICES)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
237 return -1;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
238
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
239 if (!ctx_pic->bitstream)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
240 ctx_pic->bitstream = buffer;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
241 ctx_pic->bitstream_size += size;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
242
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
243 position = buffer - ctx_pic->bitstream;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
244 fill_slice(avctx, s, &ctx_pic->slice[ctx_pic->slice_count++], position,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
245 buffer, size);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
246 return 0;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
247 }
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
248
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
249 static int end_frame(AVCodecContext *avctx)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
250 {
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
251 struct MpegEncContext *s = avctx->priv_data;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
252 struct dxva2_picture_context *ctx_pic =
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
253 s->current_picture_ptr->hwaccel_picture_private;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
254
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
255 if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
256 return -1;
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
257 return ff_dxva2_common_end_frame(avctx, s,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
258 &ctx_pic->pp, sizeof(ctx_pic->pp),
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
259 &ctx_pic->qm, sizeof(ctx_pic->qm),
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
260 commit_bitstream_and_slice_buffer);
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
261 }
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
262
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
263 AVHWAccel mpeg2_dxva2_hwaccel = {
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
264 .name = "mpeg2_dxva2",
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
265 .type = AVMEDIA_TYPE_VIDEO,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
266 .id = CODEC_ID_MPEG2VIDEO,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
267 .pix_fmt = PIX_FMT_DXVA2_VLD,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
268 .capabilities = 0,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
269 .start_frame = start_frame,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
270 .decode_slice = decode_slice,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
271 .end_frame = end_frame,
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
272 .priv_data_size = sizeof(struct dxva2_picture_context),
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
273 };
d62a89be5458 MPEG-2 DXVA2 implementation
fenrir
parents:
diff changeset
274