FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vdpau_h264.c
Go to the documentation of this file.
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 "internal.h"
28 #include "h264.h"
29 #include "mpegutils.h"
30 #include "vdpau.h"
31 #include "vdpau_internal.h"
32 
33 static int32_t h264_foc(int foc)
34 {
35  if (foc == INT_MAX)
36  foc = 0;
37  return foc;
38 }
39 
40 static void vdpau_h264_clear_rf(VdpReferenceFrameH264 *rf)
41 {
42  rf->surface = VDP_INVALID_HANDLE;
43  rf->is_long_term = VDP_FALSE;
44  rf->top_is_reference = VDP_FALSE;
45  rf->bottom_is_reference = VDP_FALSE;
46  rf->field_order_cnt[0] = 0;
47  rf->field_order_cnt[1] = 0;
48  rf->frame_idx = 0;
49 }
50 
51 static void vdpau_h264_set_rf(VdpReferenceFrameH264 *rf, H264Picture *pic,
52  int pic_structure)
53 {
54  VdpVideoSurface surface = ff_vdpau_get_surface_id(pic->f);
55 
56  if (pic_structure == 0)
57  pic_structure = pic->reference;
58 
59  rf->surface = surface;
60  rf->is_long_term = pic->reference && pic->long_ref;
61  rf->top_is_reference = (pic_structure & PICT_TOP_FIELD) != 0;
62  rf->bottom_is_reference = (pic_structure & PICT_BOTTOM_FIELD) != 0;
63  rf->field_order_cnt[0] = h264_foc(pic->field_poc[0]);
64  rf->field_order_cnt[1] = h264_foc(pic->field_poc[1]);
65  rf->frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
66 }
67 
69 {
70  H264Context * const h = avctx->priv_data;
71  struct vdpau_picture_context *pic_ctx = h->cur_pic_ptr->hwaccel_picture_private;
72  VdpPictureInfoH264 *info = &pic_ctx->info.h264;
73  int list;
74 
75  VdpReferenceFrameH264 *rf = &info->referenceFrames[0];
76 #define H264_RF_COUNT FF_ARRAY_ELEMS(info->referenceFrames)
77 
78  for (list = 0; list < 2; ++list) {
79  H264Picture **lp = list ? h->long_ref : h->short_ref;
80  int i, ls = list ? 16 : h->short_ref_count;
81 
82  for (i = 0; i < ls; ++i) {
83  H264Picture *pic = lp[i];
84  VdpReferenceFrameH264 *rf2;
85  VdpVideoSurface surface_ref;
86  int pic_frame_idx;
87 
88  if (!pic || !pic->reference)
89  continue;
90  pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
91  surface_ref = ff_vdpau_get_surface_id(pic->f);
92 
93  rf2 = &info->referenceFrames[0];
94  while (rf2 != rf) {
95  if ((rf2->surface == surface_ref) &&
96  (rf2->is_long_term == pic->long_ref) &&
97  (rf2->frame_idx == pic_frame_idx))
98  break;
99  ++rf2;
100  }
101  if (rf2 != rf) {
102  rf2->top_is_reference |= (pic->reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
103  rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
104  continue;
105  }
106 
107  if (rf >= &info->referenceFrames[H264_RF_COUNT])
108  continue;
109 
110  vdpau_h264_set_rf(rf, pic, pic->reference);
111  ++rf;
112  }
113  }
114 
115  for (; rf < &info->referenceFrames[H264_RF_COUNT]; ++rf)
117 }
118 
120  const uint8_t *buffer, uint32_t size)
121 {
122  H264Context * const h = avctx->priv_data;
123  const PPS *pps = h->ps.pps;
124  const SPS *sps = h->ps.sps;
125  H264Picture *pic = h->cur_pic_ptr;
126  struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
127  VdpPictureInfoH264 *info = &pic_ctx->info.h264;
128 #ifdef VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE
129  VdpPictureInfoH264Predictive *info2 = &pic_ctx->info.h264_predictive;
130 #endif
131 
132  /* init VdpPictureInfoH264 */
133  info->slice_count = 0;
134  info->field_order_cnt[0] = h264_foc(pic->field_poc[0]);
135  info->field_order_cnt[1] = h264_foc(pic->field_poc[1]);
136  info->is_reference = h->nal_ref_idc != 0;
137  info->frame_num = h->poc.frame_num;
138  info->field_pic_flag = h->picture_structure != PICT_FRAME;
139  info->bottom_field_flag = h->picture_structure == PICT_BOTTOM_FIELD;
140  info->num_ref_frames = sps->ref_frame_count;
141  info->mb_adaptive_frame_field_flag = sps->mb_aff && !info->field_pic_flag;
142  info->constrained_intra_pred_flag = pps->constrained_intra_pred;
143  info->weighted_pred_flag = pps->weighted_pred;
144  info->weighted_bipred_idc = pps->weighted_bipred_idc;
145  info->frame_mbs_only_flag = sps->frame_mbs_only_flag;
146  info->transform_8x8_mode_flag = pps->transform_8x8_mode;
147  info->chroma_qp_index_offset = pps->chroma_qp_index_offset[0];
148  info->second_chroma_qp_index_offset = pps->chroma_qp_index_offset[1];
149  info->pic_init_qp_minus26 = pps->init_qp - 26;
150  info->num_ref_idx_l0_active_minus1 = pps->ref_count[0] - 1;
151  info->num_ref_idx_l1_active_minus1 = pps->ref_count[1] - 1;
152  info->log2_max_frame_num_minus4 = sps->log2_max_frame_num - 4;
153  info->pic_order_cnt_type = sps->poc_type;
154  info->log2_max_pic_order_cnt_lsb_minus4 = sps->poc_type ? 0 : sps->log2_max_poc_lsb - 4;
155  info->delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag;
156  info->direct_8x8_inference_flag = sps->direct_8x8_inference_flag;
157 #ifdef VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE
158  info2->qpprime_y_zero_transform_bypass_flag = sps->transform_bypass;
159  info2->separate_colour_plane_flag = sps->residual_color_transform_flag;
160 #endif
161  info->entropy_coding_mode_flag = pps->cabac;
162  info->pic_order_present_flag = pps->pic_order_present;
163  info->deblocking_filter_control_present_flag = pps->deblocking_filter_parameters_present;
164  info->redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present;
165 
166  memcpy(info->scaling_lists_4x4, pps->scaling_matrix4,
167  sizeof(info->scaling_lists_4x4));
168  memcpy(info->scaling_lists_8x8[0], pps->scaling_matrix8[0],
169  sizeof(info->scaling_lists_8x8[0]));
170  memcpy(info->scaling_lists_8x8[1], pps->scaling_matrix8[3],
171  sizeof(info->scaling_lists_8x8[1]));
172 
174 
175  return ff_vdpau_common_start_frame(pic_ctx, buffer, size);
176 }
177 
178 static const uint8_t start_code_prefix[3] = { 0x00, 0x00, 0x01 };
179 
181  const uint8_t *buffer, uint32_t size)
182 {
183  H264Context *h = avctx->priv_data;
184  H264Picture *pic = h->cur_pic_ptr;
185  struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
186  int val;
187 
188  val = ff_vdpau_add_buffer(pic_ctx, start_code_prefix, 3);
189  if (val)
190  return val;
191 
192  val = ff_vdpau_add_buffer(pic_ctx, buffer, size);
193  if (val)
194  return val;
195 
196  pic_ctx->info.h264.slice_count++;
197  return 0;
198 }
199 
201 {
202  H264Context *h = avctx->priv_data;
203  H264SliceContext *sl = &h->slice_ctx[0];
204  H264Picture *pic = h->cur_pic_ptr;
205  struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
206  int val;
207 
208  val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
209  if (val < 0)
210  return val;
211 
212  ff_h264_draw_horiz_band(h, sl, 0, h->avctx->height);
213  return 0;
214 }
215 
216 static int vdpau_h264_init(AVCodecContext *avctx)
217 {
218  VdpDecoderProfile profile;
219  uint32_t level = avctx->level;
220 
221  switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
223  profile = VDP_DECODER_PROFILE_H264_BASELINE;
224  break;
226 #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
227  profile = VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE;
228  break;
229 #endif
231  profile = VDP_DECODER_PROFILE_H264_MAIN;
232  break;
234  profile = VDP_DECODER_PROFILE_H264_HIGH;
235  break;
236 #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
238  profile = VDP_DECODER_PROFILE_H264_EXTENDED;
239  break;
240 #endif
242  /* XXX: High 10 can be treated as High so long as only 8 bits per
243  * format are supported. */
244  profile = VDP_DECODER_PROFILE_H264_HIGH;
245  break;
246 #ifdef VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE
250  profile = VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE;
251  break;
252 #endif
253  default:
254  return AVERROR(ENOTSUP);
255  }
256 
257  if ((avctx->profile & FF_PROFILE_H264_INTRA) && avctx->level == 11)
258  level = VDP_DECODER_LEVEL_H264_1b;
259 
260  return ff_vdpau_common_init(avctx, profile, level);
261 }
262 
264  .name = "h264_vdpau",
265  .type = AVMEDIA_TYPE_VIDEO,
266  .id = AV_CODEC_ID_H264,
267  .pix_fmt = AV_PIX_FMT_VDPAU,
268  .start_frame = vdpau_h264_start_frame,
269  .end_frame = vdpau_h264_end_frame,
270  .decode_slice = vdpau_h264_decode_slice,
271  .frame_priv_data_size = sizeof(struct vdpau_picture_context),
272  .init = vdpau_h264_init,
273  .uninit = ff_vdpau_common_uninit,
274  .priv_data_size = sizeof(VDPAUContext),
275 };
#define FF_PROFILE_H264_MAIN
Definition: avcodec.h:3187
const char const char void * val
Definition: avisynth_c.h:634
int long_ref
1->long term reference 0->short term reference
Definition: h264.h:289
#define FF_PROFILE_H264_CAVLC_444
Definition: avcodec.h:3197
H264POCContext poc
Definition: h264.h:578
int weighted_bipred_idc
Definition: h264.h:208
int chroma_qp_index_offset[2]
Definition: h264.h:211
int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition: vdpau.c:273
Sequence parameter set.
Definition: h264.h:136
#define FF_PROFILE_H264_INTRA
Definition: avcodec.h:3183
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264.h:206
Picture parameter set.
Definition: h264.h:200
void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl, int y, int height)
Definition: h264.c:102
int frame_mbs_only_flag
Definition: h264.h:153
H264Context.
Definition: h264.h:456
AVFrame * f
Definition: h264.h:264
Public libavcodec VDPAU header.
static void vdpau_h264_clear_rf(VdpReferenceFrameH264 *rf)
Definition: vdpau_h264.c:40
H264Picture * long_ref[32]
Definition: h264.h:592
int profile
profile
Definition: avcodec.h:3153
int picture_structure
Definition: h264.h:528
AVHWAccel ff_h264_vdpau_hwaccel
Definition: vdpau_h264.c:263
#define FF_PROFILE_H264_HIGH_444_PREDICTIVE
Definition: avcodec.h:3195
uint8_t scaling_matrix4[6][16]
Definition: h264.h:216
int deblocking_filter_parameters_present
deblocking_filter_parameters_present_flag
Definition: h264.h:212
#define FF_PROFILE_H264_BASELINE
Definition: avcodec.h:3185
const PPS * pps
Definition: h264.h:236
int ff_vdpau_common_uninit(AVCodecContext *avctx)
Definition: vdpau.c:235
uint8_t
int cabac
entropy_coding_mode_flag
Definition: h264.h:202
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:38
int transform_bypass
qpprime_y_zero_transform_bypass_flag
Definition: h264.h:141
static int vdpau_h264_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: vdpau_h264.c:119
int redundant_pic_cnt_present
redundant_pic_cnt_present_flag
Definition: h264.h:214
#define FF_PROFILE_H264_EXTENDED
Definition: avcodec.h:3188
ptrdiff_t size
Definition: opengl_enc.c:101
static void vdpau_h264_set_rf(VdpReferenceFrameH264 *rf, H264Picture *pic, int pic_structure)
Definition: vdpau_h264.c:51
H.264 / AVC / MPEG-4 part10 codec.
static int32_t h264_foc(int foc)
Definition: vdpau_h264.c:33
int mb_aff
mb_adaptive_frame_field_flag
Definition: h264.h:154
int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile, int level)
Definition: vdpau.c:122
int poc_type
pic_order_cnt_type
Definition: h264.h:143
int constrained_intra_pred
constrained_intra_pred_flag
Definition: h264.h:213
#define AVERROR(e)
Definition: error.h:43
void * hwaccel_picture_private
hardware accelerator private data
Definition: h264.h:277
#define FF_PROFILE_H264_HIGH_422
Definition: avcodec.h:3192
#define FF_PROFILE_H264_HIGH
Definition: avcodec.h:3189
int weighted_pred
weighted_pred_flag
Definition: h264.h:207
#define PICT_TOP_FIELD
Definition: mpegutils.h:37
int frame_num
frame_num (raw frame_num from slice header)
Definition: h264.h:284
int residual_color_transform_flag
residual_colour_transform_flag
Definition: h264.h:191
int delta_pic_order_always_zero_flag
Definition: h264.h:145
uint8_t scaling_matrix8[6][64]
Definition: h264.h:217
int ref_frame_count
num_ref_frames
Definition: h264.h:149
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3668
int reference
Definition: h264.h:295
int32_t
static int vdpau_h264_end_frame(AVCodecContext *avctx)
Definition: vdpau_h264.c:200
static int vdpau_h264_init(AVCodecContext *avctx)
Definition: vdpau_h264.c:216
int level
level
Definition: avcodec.h:3242
static void vdpau_h264_set_reference_frames(AVCodecContext *avctx)
Definition: vdpau_h264.c:68
int init_qp
pic_init_qp_minus26 + 26
Definition: h264.h:209
H264SliceContext * slice_ctx
Definition: h264.h:470
int direct_8x8_inference_flag
Definition: h264.h:155
int pic_order_present
pic_order_present_flag
Definition: h264.h:203
AVCodecContext * avctx
Definition: h264.h:458
Libavcodec external API header.
static const uint8_t start_code_prefix[3]
Definition: vdpau_h264.c:178
H264Picture * short_ref[32]
Definition: h264.h:591
int field_poc[2]
top/bottom POC
Definition: h264.h:282
main external API structure.
Definition: avcodec.h:1649
#define H264_RF_COUNT
HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface.
Definition: pixfmt.h:208
H264Picture * cur_pic_ptr
Definition: h264.h:466
int log2_max_poc_lsb
log2_max_pic_order_cnt_lsb_minus4
Definition: h264.h:144
mfxU16 profile
Definition: qsvenc.c:42
int transform_8x8_mode
transform_8x8_mode_flag
Definition: h264.h:215
uint8_t level
Definition: svq3.c:193
int pic_id
pic_num (short -> no wrap version of pic_num, pic_num & max_pic_num; long -> long_pic_num) ...
Definition: h264.h:287
common internal api header.
int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx, const uint8_t *buf, uint32_t size)
Definition: vdpau.c:346
int log2_max_frame_num
log2_max_frame_num_minus4 + 4
Definition: h264.h:142
H264ParamSets ps
Definition: h264.h:574
int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame, struct vdpau_picture_context *pic_ctx)
Definition: vdpau.c:283
static int vdpau_h264_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: vdpau_h264.c:180
void * priv_data
Definition: avcodec.h:1691
#define PICT_FRAME
Definition: mpegutils.h:39
SPS * sps
Definition: h264.h:238
#define FF_PROFILE_H264_CONSTRAINED_BASELINE
Definition: avcodec.h:3186
#define FF_PROFILE_H264_HIGH_10
Definition: avcodec.h:3190
int nal_ref_idc
Definition: h264.h:562
GLuint buffer
Definition: opengl_enc.c:102
static uintptr_t ff_vdpau_get_surface_id(AVFrame *pic)
Extract VdpVideoSurface from an AVFrame.
int short_ref_count
number of actual short term references
Definition: h264.h:606