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