dxva2: Increase maximum number of slices for mpeg2
[ffmpeg.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
26 #define MAX_SLICES 1024
27 struct dxva2_picture_context {
28     DXVA_PictureParameters pp;
29     DXVA_QmatrixData       qm;
30     unsigned               slice_count;
31     DXVA_SliceInfo         slice[MAX_SLICES];
32
33     const uint8_t          *bitstream;
34     unsigned               bitstream_size;
35 };
36
37 static void fill_picture_parameters(AVCodecContext *avctx,
38                                     struct dxva_context *ctx,
39                                     const struct MpegEncContext *s,
40                                     DXVA_PictureParameters *pp)
41 {
42     const Picture *current_picture = s->current_picture_ptr;
43     int is_field = s->picture_structure != PICT_FRAME;
44
45     memset(pp, 0, sizeof(*pp));
46     pp->wDecodedPictureIndex         = ff_dxva2_get_surface_index(ctx, current_picture);
47     pp->wDeblockedPictureIndex       = 0;
48     if (s->pict_type != AV_PICTURE_TYPE_I)
49         pp->wForwardRefPictureIndex  = ff_dxva2_get_surface_index(ctx, &s->last_picture);
50     else
51         pp->wForwardRefPictureIndex  = 0xffff;
52     if (s->pict_type == AV_PICTURE_TYPE_B)
53         pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, &s->next_picture);
54     else
55         pp->wBackwardRefPictureIndex = 0xffff;
56     pp->wPicWidthInMBminus1          = s->mb_width  - 1;
57     pp->wPicHeightInMBminus1         = (s->mb_height >> is_field) - 1;
58     pp->bMacroblockWidthMinus1       = 15;
59     pp->bMacroblockHeightMinus1      = 15;
60     pp->bBlockWidthMinus1            = 7;
61     pp->bBlockHeightMinus1           = 7;
62     pp->bBPPminus1                   = 7;
63     pp->bPicStructure                = s->picture_structure;
64     pp->bSecondField                 = is_field && !s->first_field;
65     pp->bPicIntra                    = s->pict_type == AV_PICTURE_TYPE_I;
66     pp->bPicBackwardPrediction       = s->pict_type == AV_PICTURE_TYPE_B;
67     pp->bBidirectionalAveragingMode  = 0;
68     pp->bMVprecisionAndChromaRelation= 0; /* FIXME */
69     pp->bChromaFormat                = s->chroma_format;
70     pp->bPicScanFixed                = 1;
71     pp->bPicScanMethod               = s->alternate_scan ? 1 : 0;
72     pp->bPicReadbackRequests         = 0;
73     pp->bRcontrol                    = 0;
74     pp->bPicSpatialResid8            = 0;
75     pp->bPicOverflowBlocks           = 0;
76     pp->bPicExtrapolation            = 0;
77     pp->bPicDeblocked                = 0;
78     pp->bPicDeblockConfined          = 0;
79     pp->bPic4MVallowed               = 0;
80     pp->bPicOBMC                     = 0;
81     pp->bPicBinPB                    = 0;
82     pp->bMV_RPS                      = 0;
83     pp->bReservedBits                = 0;
84     pp->wBitstreamFcodes             = (s->mpeg_f_code[0][0] << 12) |
85                                        (s->mpeg_f_code[0][1] <<  8) |
86                                        (s->mpeg_f_code[1][0] <<  4) |
87                                        (s->mpeg_f_code[1][1]      );
88     pp->wBitstreamPCEelements        = (s->intra_dc_precision         << 14) |
89                                        (s->picture_structure          << 12) |
90                                        (s->top_field_first            << 11) |
91                                        (s->frame_pred_frame_dct       << 10) |
92                                        (s->concealment_motion_vectors <<  9) |
93                                        (s->q_scale_type               <<  8) |
94                                        (s->intra_vlc_format           <<  7) |
95                                        (s->alternate_scan             <<  6) |
96                                        (s->repeat_first_field         <<  5) |
97                                        (s->chroma_420_type            <<  4) |
98                                        (s->progressive_frame          <<  3);
99     pp->bBitstreamConcealmentNeed    = 0;
100     pp->bBitstreamConcealmentMethod  = 0;
101 }
102
103 static void fill_quantization_matrices(AVCodecContext *avctx,
104                                        struct dxva_context *ctx,
105                                        const struct MpegEncContext *s,
106                                        DXVA_QmatrixData *qm)
107 {
108     int i;
109     for (i = 0; i < 4; i++)
110         qm->bNewQmatrix[i] = 1;
111     for (i = 0; i < 64; i++) {
112         int n = s->dsp.idct_permutation[ff_zigzag_direct[i]];
113         qm->Qmatrix[0][i] = s->intra_matrix[n];;
114         qm->Qmatrix[1][i] = s->inter_matrix[n];;
115         qm->Qmatrix[2][i] = s->chroma_intra_matrix[n];;
116         qm->Qmatrix[3][i] = s->chroma_inter_matrix[n];;
117     }
118 }
119
120 static void fill_slice(AVCodecContext *avctx,
121                        const struct MpegEncContext *s,
122                        DXVA_SliceInfo *slice,
123                        unsigned position,
124                        const uint8_t *buffer, unsigned size)
125 {
126     int is_field = s->picture_structure != PICT_FRAME;
127     GetBitContext gb;
128
129     memset(slice, 0, sizeof(*slice));
130     slice->wHorizontalPosition = s->mb_x;
131     slice->wVerticalPosition   = s->mb_y >> is_field;
132     slice->dwSliceBitsInBuffer = 8 * size;
133     slice->dwSliceDataLocation = position;
134     slice->bStartCodeBitOffset = 0;
135     slice->bReservedBits       = 0;
136     /* XXX We store the index of the first MB and it will be fixed later */
137     slice->wNumberMBsInSlice   = (s->mb_y >> is_field) * s->mb_width + s->mb_x;
138     slice->wBadSliceChopping   = 0;
139
140     init_get_bits(&gb, &buffer[4], 8 * (size - 4));
141
142     slice->wQuantizerScaleCode = get_bits(&gb, 5);
143     while (get_bits1(&gb))
144         skip_bits(&gb, 8);
145
146     slice->wMBbitOffset        = 4 * 8 + get_bits_count(&gb);
147 }
148 static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
149                                              DXVA2_DecodeBufferDesc *bs,
150                                              DXVA2_DecodeBufferDesc *sc)
151 {
152     const struct MpegEncContext *s = avctx->priv_data;
153     struct dxva_context *ctx = avctx->hwaccel_context;
154     struct dxva2_picture_context *ctx_pic =
155         s->current_picture_ptr->hwaccel_picture_private;
156     const int is_field = s->picture_structure != PICT_FRAME;
157     const unsigned mb_count = s->mb_width * (s->mb_height >> is_field);
158     uint8_t  *dxva_data, *current, *end;
159     unsigned dxva_size;
160     unsigned i;
161
162     if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
163                                               DXVA2_BitStreamDateBufferType,
164                                               &dxva_data, &dxva_size)))
165         return -1;
166     current = dxva_data;
167     end = dxva_data + dxva_size;
168
169     for (i = 0; i < ctx_pic->slice_count; i++) {
170         DXVA_SliceInfo *slice = &ctx_pic->slice[i];
171         unsigned position = slice->dwSliceDataLocation;
172         unsigned size     = slice->dwSliceBitsInBuffer / 8;
173         if (size > end - current) {
174             av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
175             break;
176         }
177         slice->dwSliceDataLocation = current - dxva_data;
178
179         if (i < ctx_pic->slice_count - 1)
180             slice->wNumberMBsInSlice =
181                 slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice;
182         else
183             slice->wNumberMBsInSlice =
184                 mb_count - slice[0].wNumberMBsInSlice;
185
186         memcpy(current, &ctx_pic->bitstream[position], size);
187         current += size;
188     }
189     if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
190                                                   DXVA2_BitStreamDateBufferType)))
191         return -1;
192     if (i < ctx_pic->slice_count)
193         return -1;
194
195     memset(bs, 0, sizeof(*bs));
196     bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
197     bs->DataSize             = current - dxva_data;
198     bs->NumMBsInBuffer       = mb_count;
199
200     return ff_dxva2_commit_buffer(avctx, ctx, sc,
201                                   DXVA2_SliceControlBufferType,
202                                   ctx_pic->slice,
203                                   ctx_pic->slice_count * sizeof(*ctx_pic->slice),
204                                   mb_count);
205 }
206
207 static int dxva2_mpeg2_start_frame(AVCodecContext *avctx,
208                                    av_unused const uint8_t *buffer,
209                                    av_unused uint32_t size)
210 {
211     const struct MpegEncContext *s = avctx->priv_data;
212     struct dxva_context *ctx = avctx->hwaccel_context;
213     struct dxva2_picture_context *ctx_pic =
214         s->current_picture_ptr->hwaccel_picture_private;
215
216     if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
217         return -1;
218     assert(ctx_pic);
219
220     fill_picture_parameters(avctx, ctx, s, &ctx_pic->pp);
221     fill_quantization_matrices(avctx, ctx, s, &ctx_pic->qm);
222
223     ctx_pic->slice_count    = 0;
224     ctx_pic->bitstream_size = 0;
225     ctx_pic->bitstream      = NULL;
226     return 0;
227 }
228
229 static int dxva2_mpeg2_decode_slice(AVCodecContext *avctx,
230                                     const uint8_t *buffer, uint32_t size)
231 {
232     const struct MpegEncContext *s = avctx->priv_data;
233     struct dxva2_picture_context *ctx_pic =
234         s->current_picture_ptr->hwaccel_picture_private;
235     unsigned position;
236
237     if (ctx_pic->slice_count >= MAX_SLICES) {
238         avpriv_request_sample(avctx, "%d slices in dxva2",
239                               ctx_pic->slice_count);
240         return -1;
241     }
242     if (!ctx_pic->bitstream)
243         ctx_pic->bitstream = buffer;
244     ctx_pic->bitstream_size += size;
245
246     position = buffer - ctx_pic->bitstream;
247     fill_slice(avctx, s, &ctx_pic->slice[ctx_pic->slice_count++], position,
248                buffer, size);
249     return 0;
250 }
251
252 static int dxva2_mpeg2_end_frame(AVCodecContext *avctx)
253 {
254     struct MpegEncContext *s = avctx->priv_data;
255     struct dxva2_picture_context *ctx_pic =
256         s->current_picture_ptr->hwaccel_picture_private;
257     int ret;
258
259     if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
260         return -1;
261     ret = ff_dxva2_common_end_frame(avctx, s->current_picture_ptr,
262                                     &ctx_pic->pp, sizeof(ctx_pic->pp),
263                                     &ctx_pic->qm, sizeof(ctx_pic->qm),
264                                     commit_bitstream_and_slice_buffer);
265     if (!ret)
266         ff_mpeg_draw_horiz_band(s, 0, avctx->height);
267     return ret;
268 }
269
270 AVHWAccel ff_mpeg2_dxva2_hwaccel = {
271     .name           = "mpeg2_dxva2",
272     .type           = AVMEDIA_TYPE_VIDEO,
273     .id             = AV_CODEC_ID_MPEG2VIDEO,
274     .pix_fmt        = AV_PIX_FMT_DXVA2_VLD,
275     .start_frame    = dxva2_mpeg2_start_frame,
276     .decode_slice   = dxva2_mpeg2_decode_slice,
277     .end_frame      = dxva2_mpeg2_end_frame,
278     .priv_data_size = sizeof(struct dxva2_picture_context),
279 };