| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * MPEG-4 Part 10 / AVC / H.264 HW decode acceleration through VDPAU | ||
| 3 | * | ||
| 4 | * Copyright (c) 2008 NVIDIA | ||
| 5 | * Copyright (c) 2013 Rémi Denis-Courmont | ||
| 6 | * | ||
| 7 | * This file is part of FFmpeg. | ||
| 8 | * | ||
| 9 | * FFmpeg is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU Lesser General Public | ||
| 11 | * License as published by the Free Software Foundation; either | ||
| 12 | * version 2.1 of the License, or (at your option) any later version. | ||
| 13 | * | ||
| 14 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | * Lesser General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU Lesser General Public | ||
| 20 | * License along with FFmpeg; if not, write to the Free Software Foundation, | ||
| 21 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include <vdpau/vdpau.h> | ||
| 25 | |||
| 26 | #include "avcodec.h" | ||
| 27 | #include "h264dec.h" | ||
| 28 | #include "h264_ps.h" | ||
| 29 | #include "hwaccel_internal.h" | ||
| 30 | #include "mpegutils.h" | ||
| 31 | #include "vdpau.h" | ||
| 32 | #include "vdpau_internal.h" | ||
| 33 | |||
| 34 | ✗ | static int32_t h264_foc(int foc) | |
| 35 | { | ||
| 36 | ✗ | if (foc == INT_MAX) | |
| 37 | ✗ | foc = 0; | |
| 38 | ✗ | return foc; | |
| 39 | } | ||
| 40 | |||
| 41 | ✗ | static void vdpau_h264_clear_rf(VdpReferenceFrameH264 *rf) | |
| 42 | { | ||
| 43 | ✗ | rf->surface = VDP_INVALID_HANDLE; | |
| 44 | ✗ | rf->is_long_term = VDP_FALSE; | |
| 45 | ✗ | rf->top_is_reference = VDP_FALSE; | |
| 46 | ✗ | rf->bottom_is_reference = VDP_FALSE; | |
| 47 | ✗ | rf->field_order_cnt[0] = 0; | |
| 48 | ✗ | rf->field_order_cnt[1] = 0; | |
| 49 | ✗ | rf->frame_idx = 0; | |
| 50 | ✗ | } | |
| 51 | |||
| 52 | ✗ | static void vdpau_h264_set_rf(VdpReferenceFrameH264 *rf, H264Picture *pic, | |
| 53 | int pic_structure) | ||
| 54 | { | ||
| 55 | ✗ | VdpVideoSurface surface = ff_vdpau_get_surface_id(pic->f); | |
| 56 | |||
| 57 | ✗ | if (pic_structure == 0) | |
| 58 | ✗ | pic_structure = pic->reference; | |
| 59 | |||
| 60 | ✗ | rf->surface = surface; | |
| 61 | ✗ | rf->is_long_term = pic->reference && pic->long_ref; | |
| 62 | ✗ | rf->top_is_reference = (pic_structure & PICT_TOP_FIELD) != 0; | |
| 63 | ✗ | rf->bottom_is_reference = (pic_structure & PICT_BOTTOM_FIELD) != 0; | |
| 64 | ✗ | rf->field_order_cnt[0] = h264_foc(pic->field_poc[0]); | |
| 65 | ✗ | rf->field_order_cnt[1] = h264_foc(pic->field_poc[1]); | |
| 66 | ✗ | rf->frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num; | |
| 67 | ✗ | } | |
| 68 | |||
| 69 | ✗ | static void vdpau_h264_set_reference_frames(AVCodecContext *avctx) | |
| 70 | { | ||
| 71 | ✗ | H264Context * const h = avctx->priv_data; | |
| 72 | ✗ | struct vdpau_picture_context *pic_ctx = h->cur_pic_ptr->hwaccel_picture_private; | |
| 73 | ✗ | VdpPictureInfoH264 *info = &pic_ctx->info.h264; | |
| 74 | int list; | ||
| 75 | |||
| 76 | ✗ | VdpReferenceFrameH264 *rf = &info->referenceFrames[0]; | |
| 77 | #define H264_RF_COUNT FF_ARRAY_ELEMS(info->referenceFrames) | ||
| 78 | |||
| 79 | ✗ | for (list = 0; list < 2; ++list) { | |
| 80 | ✗ | H264Picture **lp = list ? h->long_ref : h->short_ref; | |
| 81 | ✗ | int i, ls = list ? 16 : h->short_ref_count; | |
| 82 | |||
| 83 | ✗ | for (i = 0; i < ls; ++i) { | |
| 84 | ✗ | H264Picture *pic = lp[i]; | |
| 85 | VdpReferenceFrameH264 *rf2; | ||
| 86 | VdpVideoSurface surface_ref; | ||
| 87 | int pic_frame_idx; | ||
| 88 | |||
| 89 | ✗ | if (!pic || !pic->reference) | |
| 90 | ✗ | continue; | |
| 91 | ✗ | pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num; | |
| 92 | ✗ | surface_ref = ff_vdpau_get_surface_id(pic->f); | |
| 93 | |||
| 94 | ✗ | rf2 = &info->referenceFrames[0]; | |
| 95 | ✗ | while (rf2 != rf) { | |
| 96 | ✗ | if ((rf2->surface == surface_ref) && | |
| 97 | ✗ | (rf2->is_long_term == pic->long_ref) && | |
| 98 | ✗ | (rf2->frame_idx == pic_frame_idx)) | |
| 99 | ✗ | break; | |
| 100 | ✗ | ++rf2; | |
| 101 | } | ||
| 102 | ✗ | if (rf2 != rf) { | |
| 103 | ✗ | rf2->top_is_reference |= (pic->reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE; | |
| 104 | ✗ | rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE; | |
| 105 | ✗ | continue; | |
| 106 | } | ||
| 107 | |||
| 108 | ✗ | if (rf >= &info->referenceFrames[H264_RF_COUNT]) | |
| 109 | ✗ | continue; | |
| 110 | |||
| 111 | ✗ | vdpau_h264_set_rf(rf, pic, pic->reference); | |
| 112 | ✗ | ++rf; | |
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | ✗ | for (; rf < &info->referenceFrames[H264_RF_COUNT]; ++rf) | |
| 117 | ✗ | vdpau_h264_clear_rf(rf); | |
| 118 | ✗ | } | |
| 119 | |||
| 120 | ✗ | static int vdpau_h264_start_frame(AVCodecContext *avctx, | |
| 121 | const AVBufferRef *buffer_ref, | ||
| 122 | const uint8_t *buffer, uint32_t size) | ||
| 123 | { | ||
| 124 | ✗ | H264Context * const h = avctx->priv_data; | |
| 125 | ✗ | const PPS *pps = h->ps.pps; | |
| 126 | ✗ | const SPS *sps = h->ps.sps; | |
| 127 | ✗ | H264Picture *pic = h->cur_pic_ptr; | |
| 128 | ✗ | struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private; | |
| 129 | ✗ | VdpPictureInfoH264 *info = &pic_ctx->info.h264; | |
| 130 | #ifdef VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE | ||
| 131 | ✗ | VdpPictureInfoH264Predictive *info2 = &pic_ctx->info.h264_predictive; | |
| 132 | #endif | ||
| 133 | |||
| 134 | /* init VdpPictureInfoH264 */ | ||
| 135 | ✗ | info->slice_count = 0; | |
| 136 | ✗ | info->field_order_cnt[0] = h264_foc(pic->field_poc[0]); | |
| 137 | ✗ | info->field_order_cnt[1] = h264_foc(pic->field_poc[1]); | |
| 138 | ✗ | info->is_reference = h->nal_ref_idc != 0; | |
| 139 | ✗ | info->frame_num = h->poc.frame_num; | |
| 140 | ✗ | info->field_pic_flag = h->picture_structure != PICT_FRAME; | |
| 141 | ✗ | info->bottom_field_flag = h->picture_structure == PICT_BOTTOM_FIELD; | |
| 142 | ✗ | info->num_ref_frames = sps->ref_frame_count; | |
| 143 | ✗ | info->mb_adaptive_frame_field_flag = sps->mb_aff && !info->field_pic_flag; | |
| 144 | ✗ | info->constrained_intra_pred_flag = pps->constrained_intra_pred; | |
| 145 | ✗ | info->weighted_pred_flag = pps->weighted_pred; | |
| 146 | ✗ | info->weighted_bipred_idc = pps->weighted_bipred_idc; | |
| 147 | ✗ | info->frame_mbs_only_flag = sps->frame_mbs_only_flag; | |
| 148 | ✗ | info->transform_8x8_mode_flag = pps->transform_8x8_mode; | |
| 149 | ✗ | info->chroma_qp_index_offset = pps->chroma_qp_index_offset[0]; | |
| 150 | ✗ | info->second_chroma_qp_index_offset = pps->chroma_qp_index_offset[1]; | |
| 151 | ✗ | info->pic_init_qp_minus26 = pps->init_qp - 26; | |
| 152 | ✗ | info->num_ref_idx_l0_active_minus1 = pps->ref_count[0] - 1; | |
| 153 | ✗ | info->num_ref_idx_l1_active_minus1 = pps->ref_count[1] - 1; | |
| 154 | ✗ | info->log2_max_frame_num_minus4 = sps->log2_max_frame_num - 4; | |
| 155 | ✗ | info->pic_order_cnt_type = sps->poc_type; | |
| 156 | ✗ | info->log2_max_pic_order_cnt_lsb_minus4 = sps->poc_type ? 0 : sps->log2_max_poc_lsb - 4; | |
| 157 | ✗ | info->delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag; | |
| 158 | ✗ | info->direct_8x8_inference_flag = sps->direct_8x8_inference_flag; | |
| 159 | #ifdef VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE | ||
| 160 | ✗ | info2->qpprime_y_zero_transform_bypass_flag = sps->transform_bypass; | |
| 161 | ✗ | info2->separate_colour_plane_flag = sps->residual_color_transform_flag; | |
| 162 | #endif | ||
| 163 | ✗ | info->entropy_coding_mode_flag = pps->cabac; | |
| 164 | ✗ | info->pic_order_present_flag = pps->pic_order_present; | |
| 165 | ✗ | info->deblocking_filter_control_present_flag = pps->deblocking_filter_parameters_present; | |
| 166 | ✗ | info->redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present; | |
| 167 | |||
| 168 | ✗ | memcpy(info->scaling_lists_4x4, pps->scaling_matrix4, | |
| 169 | sizeof(info->scaling_lists_4x4)); | ||
| 170 | ✗ | memcpy(info->scaling_lists_8x8[0], pps->scaling_matrix8[0], | |
| 171 | sizeof(info->scaling_lists_8x8[0])); | ||
| 172 | ✗ | memcpy(info->scaling_lists_8x8[1], pps->scaling_matrix8[3], | |
| 173 | sizeof(info->scaling_lists_8x8[1])); | ||
| 174 | |||
| 175 | ✗ | vdpau_h264_set_reference_frames(avctx); | |
| 176 | |||
| 177 | ✗ | return ff_vdpau_common_start_frame(pic_ctx, buffer, size); | |
| 178 | } | ||
| 179 | |||
| 180 | static const uint8_t start_code_prefix[3] = { 0x00, 0x00, 0x01 }; | ||
| 181 | |||
| 182 | ✗ | static int vdpau_h264_decode_slice(AVCodecContext *avctx, | |
| 183 | const uint8_t *buffer, uint32_t size) | ||
| 184 | { | ||
| 185 | ✗ | H264Context *h = avctx->priv_data; | |
| 186 | ✗ | H264Picture *pic = h->cur_pic_ptr; | |
| 187 | ✗ | struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private; | |
| 188 | int val; | ||
| 189 | |||
| 190 | ✗ | val = ff_vdpau_add_buffer(pic_ctx, start_code_prefix, 3); | |
| 191 | ✗ | if (val) | |
| 192 | ✗ | return val; | |
| 193 | |||
| 194 | ✗ | val = ff_vdpau_add_buffer(pic_ctx, buffer, size); | |
| 195 | ✗ | if (val) | |
| 196 | ✗ | return val; | |
| 197 | |||
| 198 | ✗ | pic_ctx->info.h264.slice_count++; | |
| 199 | ✗ | return 0; | |
| 200 | } | ||
| 201 | |||
| 202 | ✗ | static int vdpau_h264_end_frame(AVCodecContext *avctx) | |
| 203 | { | ||
| 204 | ✗ | H264Context *h = avctx->priv_data; | |
| 205 | ✗ | H264SliceContext *sl = &h->slice_ctx[0]; | |
| 206 | ✗ | H264Picture *pic = h->cur_pic_ptr; | |
| 207 | ✗ | struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private; | |
| 208 | int val; | ||
| 209 | |||
| 210 | ✗ | val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx); | |
| 211 | ✗ | if (val < 0) | |
| 212 | ✗ | return val; | |
| 213 | |||
| 214 | ✗ | ff_h264_draw_horiz_band(h, sl, 0, h->avctx->height); | |
| 215 | ✗ | return 0; | |
| 216 | } | ||
| 217 | |||
| 218 | ✗ | static av_cold int vdpau_h264_init(AVCodecContext *avctx) | |
| 219 | { | ||
| 220 | VdpDecoderProfile profile; | ||
| 221 | ✗ | uint32_t level = avctx->level; | |
| 222 | |||
| 223 | ✗ | switch (avctx->profile & ~AV_PROFILE_H264_INTRA) { | |
| 224 | ✗ | case AV_PROFILE_H264_BASELINE: | |
| 225 | ✗ | profile = VDP_DECODER_PROFILE_H264_BASELINE; | |
| 226 | ✗ | break; | |
| 227 | ✗ | case AV_PROFILE_H264_CONSTRAINED_BASELINE: | |
| 228 | #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE | ||
| 229 | ✗ | profile = VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE; | |
| 230 | ✗ | break; | |
| 231 | #endif | ||
| 232 | ✗ | case AV_PROFILE_H264_MAIN: | |
| 233 | ✗ | profile = VDP_DECODER_PROFILE_H264_MAIN; | |
| 234 | ✗ | break; | |
| 235 | ✗ | case AV_PROFILE_H264_HIGH: | |
| 236 | ✗ | profile = VDP_DECODER_PROFILE_H264_HIGH; | |
| 237 | ✗ | break; | |
| 238 | #ifdef VDP_DECODER_PROFILE_H264_EXTENDED | ||
| 239 | ✗ | case AV_PROFILE_H264_EXTENDED: | |
| 240 | ✗ | profile = VDP_DECODER_PROFILE_H264_EXTENDED; | |
| 241 | ✗ | break; | |
| 242 | #endif | ||
| 243 | ✗ | case AV_PROFILE_H264_HIGH_10: | |
| 244 | /* XXX: High 10 can be treated as High so long as only 8 bits per | ||
| 245 | * format are supported. */ | ||
| 246 | ✗ | profile = VDP_DECODER_PROFILE_H264_HIGH; | |
| 247 | ✗ | break; | |
| 248 | #ifdef VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE | ||
| 249 | ✗ | case AV_PROFILE_H264_HIGH_422: | |
| 250 | case AV_PROFILE_H264_HIGH_444_PREDICTIVE: | ||
| 251 | case AV_PROFILE_H264_CAVLC_444: | ||
| 252 | ✗ | profile = VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE; | |
| 253 | ✗ | break; | |
| 254 | #endif | ||
| 255 | ✗ | default: | |
| 256 | ✗ | return AVERROR(ENOTSUP); | |
| 257 | } | ||
| 258 | |||
| 259 | ✗ | if ((avctx->profile & AV_PROFILE_H264_INTRA) && avctx->level == 11) | |
| 260 | ✗ | level = VDP_DECODER_LEVEL_H264_1b; | |
| 261 | |||
| 262 | ✗ | return ff_vdpau_common_init(avctx, profile, level); | |
| 263 | } | ||
| 264 | |||
| 265 | const FFHWAccel ff_h264_vdpau_hwaccel = { | ||
| 266 | .p.name = "h264_vdpau", | ||
| 267 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 268 | .p.id = AV_CODEC_ID_H264, | ||
| 269 | .p.pix_fmt = AV_PIX_FMT_VDPAU, | ||
| 270 | .start_frame = vdpau_h264_start_frame, | ||
| 271 | .end_frame = vdpau_h264_end_frame, | ||
| 272 | .decode_slice = vdpau_h264_decode_slice, | ||
| 273 | .frame_priv_data_size = sizeof(struct vdpau_picture_context), | ||
| 274 | .init = vdpau_h264_init, | ||
| 275 | .uninit = ff_vdpau_common_uninit, | ||
| 276 | .frame_params = ff_vdpau_common_frame_params, | ||
| 277 | .priv_data_size = sizeof(VDPAUContext), | ||
| 278 | .caps_internal = HWACCEL_CAP_ASYNC_SAFE, | ||
| 279 | }; | ||
| 280 |