2 * MPEG-4 Part 10 / AVC / H.264 HW decode acceleration through VDPAU
4 * Copyright (c) 2008 NVIDIA
5 * Copyright (c) 2013 RĂ©mi Denis-Courmont
7 * This file is part of FFmpeg.
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.
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.
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
24 #include <vdpau/vdpau.h>
29 #include "vdpau_internal.h"
31 static int32_t h264_foc(int foc)
38 static void vdpau_h264_clear_rf(VdpReferenceFrameH264 *rf)
40 rf->surface = VDP_INVALID_HANDLE;
41 rf->is_long_term = VDP_FALSE;
42 rf->top_is_reference = VDP_FALSE;
43 rf->bottom_is_reference = VDP_FALSE;
44 rf->field_order_cnt[0] = 0;
45 rf->field_order_cnt[1] = 0;
49 static void vdpau_h264_set_rf(VdpReferenceFrameH264 *rf, Picture *pic,
52 VdpVideoSurface surface = ff_vdpau_get_surface_id(pic);
54 if (pic_structure == 0)
55 pic_structure = pic->reference;
57 rf->surface = surface;
58 rf->is_long_term = pic->reference && pic->long_ref;
59 rf->top_is_reference = (pic_structure & PICT_TOP_FIELD) != 0;
60 rf->bottom_is_reference = (pic_structure & PICT_BOTTOM_FIELD) != 0;
61 rf->field_order_cnt[0] = h264_foc(pic->field_poc[0]);
62 rf->field_order_cnt[1] = h264_foc(pic->field_poc[1]);
63 rf->frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
66 static void vdpau_h264_set_reference_frames(AVCodecContext *avctx)
68 H264Context * const h = avctx->priv_data;
69 AVVDPAUContext *hwctx = avctx->hwaccel_context;
70 VdpPictureInfoH264 *info = &hwctx->info.h264;
73 VdpReferenceFrameH264 *rf = &info->referenceFrames[0];
74 #define H264_RF_COUNT FF_ARRAY_ELEMS(info->referenceFrames)
76 for (list = 0; list < 2; ++list) {
77 Picture **lp = list ? h->long_ref : h->short_ref;
78 int i, ls = list ? 16 : h->short_ref_count;
80 for (i = 0; i < ls; ++i) {
82 VdpReferenceFrameH264 *rf2;
83 VdpVideoSurface surface_ref;
86 if (!pic || !pic->reference)
88 pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
89 surface_ref = ff_vdpau_get_surface_id(pic);
91 rf2 = &info->referenceFrames[0];
93 if ((rf2->surface == surface_ref) &&
94 (rf2->is_long_term == pic->long_ref) &&
95 (rf2->frame_idx == pic_frame_idx))
100 rf2->top_is_reference |= (pic->reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
101 rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
105 if (rf >= &info->referenceFrames[H264_RF_COUNT])
108 vdpau_h264_set_rf(rf, pic, pic->reference);
113 for (; rf < &info->referenceFrames[H264_RF_COUNT]; ++rf)
114 vdpau_h264_clear_rf(rf);
117 static int vdpau_h264_start_frame(AVCodecContext *avctx,
118 const uint8_t *buffer, uint32_t size)
120 H264Context * const h = avctx->priv_data;
121 AVVDPAUContext *hwctx = avctx->hwaccel_context;
122 VdpPictureInfoH264 *info = &hwctx->info.h264;
123 Picture *pic = h->cur_pic_ptr;
125 /* init VdpPictureInfoH264 */
126 info->slice_count = 0;
127 info->field_order_cnt[0] = h264_foc(pic->field_poc[0]);
128 info->field_order_cnt[1] = h264_foc(pic->field_poc[1]);
129 info->is_reference = h->nal_ref_idc != 0;
130 info->frame_num = h->frame_num;
131 info->field_pic_flag = h->picture_structure != PICT_FRAME;
132 info->bottom_field_flag = h->picture_structure == PICT_BOTTOM_FIELD;
133 info->num_ref_frames = h->sps.ref_frame_count;
134 info->mb_adaptive_frame_field_flag = h->sps.mb_aff && !info->field_pic_flag;
135 info->constrained_intra_pred_flag = h->pps.constrained_intra_pred;
136 info->weighted_pred_flag = h->pps.weighted_pred;
137 info->weighted_bipred_idc = h->pps.weighted_bipred_idc;
138 info->frame_mbs_only_flag = h->sps.frame_mbs_only_flag;
139 info->transform_8x8_mode_flag = h->pps.transform_8x8_mode;
140 info->chroma_qp_index_offset = h->pps.chroma_qp_index_offset[0];
141 info->second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
142 info->pic_init_qp_minus26 = h->pps.init_qp - 26;
143 info->num_ref_idx_l0_active_minus1 = h->pps.ref_count[0] - 1;
144 info->num_ref_idx_l1_active_minus1 = h->pps.ref_count[1] - 1;
145 info->log2_max_frame_num_minus4 = h->sps.log2_max_frame_num - 4;
146 info->pic_order_cnt_type = h->sps.poc_type;
147 info->log2_max_pic_order_cnt_lsb_minus4 = h->sps.poc_type ? 0 : h->sps.log2_max_poc_lsb - 4;
148 info->delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
149 info->direct_8x8_inference_flag = h->sps.direct_8x8_inference_flag;
150 info->entropy_coding_mode_flag = h->pps.cabac;
151 info->pic_order_present_flag = h->pps.pic_order_present;
152 info->deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
153 info->redundant_pic_cnt_present_flag = h->pps.redundant_pic_cnt_present;
155 memcpy(info->scaling_lists_4x4, h->pps.scaling_matrix4,
156 sizeof(info->scaling_lists_4x4));
157 memcpy(info->scaling_lists_8x8[0], h->pps.scaling_matrix8[0],
158 sizeof(info->scaling_lists_8x8[0]));
159 memcpy(info->scaling_lists_8x8[1], h->pps.scaling_matrix8[3],
160 sizeof(info->scaling_lists_8x8[1]));
162 vdpau_h264_set_reference_frames(avctx);
164 return ff_vdpau_common_start_frame(avctx, buffer, size);
167 static const uint8_t start_code_prefix[3] = { 0x00, 0x00, 0x01 };
169 static int vdpau_h264_decode_slice(AVCodecContext *avctx,
170 const uint8_t *buffer, uint32_t size)
172 AVVDPAUContext *hwctx = avctx->hwaccel_context;
175 val = ff_vdpau_add_buffer(avctx, start_code_prefix, 3);
179 val = ff_vdpau_add_buffer(avctx, buffer, size);
183 hwctx->info.h264.slice_count++;
187 static int vdpau_h264_end_frame(AVCodecContext *avctx)
189 AVVDPAUContext *hwctx = avctx->hwaccel_context;
190 H264Context *h = avctx->priv_data;
191 VdpVideoSurface surf = ff_vdpau_get_surface_id(h->cur_pic_ptr);
193 hwctx->render(hwctx->decoder, surf, (void *)&hwctx->info,
194 hwctx->bitstream_buffers_used, hwctx->bitstream_buffers);
196 ff_h264_draw_horiz_band(h, 0, h->avctx->height);
197 hwctx->bitstream_buffers_used = 0;
202 AVHWAccel ff_h264_vdpau_hwaccel = {
203 .name = "h264_vdpau",
204 .type = AVMEDIA_TYPE_VIDEO,
205 .id = AV_CODEC_ID_H264,
206 .pix_fmt = AV_PIX_FMT_VDPAU,
207 .start_frame = vdpau_h264_start_frame,
208 .end_frame = vdpau_h264_end_frame,
209 .decode_slice = vdpau_h264_decode_slice,