annotate ffmpeg/libavcodec/dxva2_mpeg2.c @ 13:844d341cf643 tip

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