Libav
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 Libav.
8  *
9  * Libav 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  * Libav 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 Libav; 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 "h264.h"
28 #include "mpegutils.h"
29 #include "vdpau.h"
30 #include "vdpau_internal.h"
31 
32 static int32_t h264_foc(int foc)
33 {
34  if (foc == INT_MAX)
35  foc = 0;
36  return foc;
37 }
38 
39 static void vdpau_h264_clear_rf(VdpReferenceFrameH264 *rf)
40 {
41  rf->surface = VDP_INVALID_HANDLE;
42  rf->is_long_term = VDP_FALSE;
43  rf->top_is_reference = VDP_FALSE;
44  rf->bottom_is_reference = VDP_FALSE;
45  rf->field_order_cnt[0] = 0;
46  rf->field_order_cnt[1] = 0;
47  rf->frame_idx = 0;
48 }
49 
50 static void vdpau_h264_set_rf(VdpReferenceFrameH264 *rf, H264Picture *pic,
51  int pic_structure)
52 {
53  VdpVideoSurface surface = ff_vdpau_get_surface_id(&pic->f);
54 
55  if (pic_structure == 0)
56  pic_structure = pic->reference;
57 
58  rf->surface = surface;
59  rf->is_long_term = pic->reference && pic->long_ref;
60  rf->top_is_reference = (pic_structure & PICT_TOP_FIELD) != 0;
61  rf->bottom_is_reference = (pic_structure & PICT_BOTTOM_FIELD) != 0;
62  rf->field_order_cnt[0] = h264_foc(pic->field_poc[0]);
63  rf->field_order_cnt[1] = h264_foc(pic->field_poc[1]);
64  rf->frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
65 }
66 
68 {
69  H264Context * const h = avctx->priv_data;
71  VdpPictureInfoH264 *info = &pic_ctx->info.h264;
72  int list;
73 
74  VdpReferenceFrameH264 *rf = &info->referenceFrames[0];
75 #define H264_RF_COUNT FF_ARRAY_ELEMS(info->referenceFrames)
76 
77  for (list = 0; list < 2; ++list) {
78  H264Picture **lp = list ? h->long_ref : h->short_ref;
79  int i, ls = list ? 16 : h->short_ref_count;
80 
81  for (i = 0; i < ls; ++i) {
82  H264Picture *pic = lp[i];
83  VdpReferenceFrameH264 *rf2;
84  VdpVideoSurface surface_ref;
85  int pic_frame_idx;
86 
87  if (!pic || !pic->reference)
88  continue;
89  pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
90  surface_ref = ff_vdpau_get_surface_id(&pic->f);
91 
92  rf2 = &info->referenceFrames[0];
93  while (rf2 != rf) {
94  if ((rf2->surface == surface_ref) &&
95  (rf2->is_long_term == pic->long_ref) &&
96  (rf2->frame_idx == pic_frame_idx))
97  break;
98  ++rf2;
99  }
100  if (rf2 != rf) {
101  rf2->top_is_reference |= (pic->reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
102  rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
103  continue;
104  }
105 
106  if (rf >= &info->referenceFrames[H264_RF_COUNT])
107  continue;
108 
109  vdpau_h264_set_rf(rf, pic, pic->reference);
110  ++rf;
111  }
112  }
113 
114  for (; rf < &info->referenceFrames[H264_RF_COUNT]; ++rf)
116 }
117 
119  const uint8_t *buffer, uint32_t size)
120 {
121  H264Context * const h = avctx->priv_data;
122  H264Picture *pic = h->cur_pic_ptr;
123  struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
124  VdpPictureInfoH264 *info = &pic_ctx->info.h264;
125 
126  /* init VdpPictureInfoH264 */
127  info->slice_count = 0;
128  info->field_order_cnt[0] = h264_foc(pic->field_poc[0]);
129  info->field_order_cnt[1] = h264_foc(pic->field_poc[1]);
130  info->is_reference = h->nal_ref_idc != 0;
131  info->frame_num = h->frame_num;
132  info->field_pic_flag = h->picture_structure != PICT_FRAME;
133  info->bottom_field_flag = h->picture_structure == PICT_BOTTOM_FIELD;
134  info->num_ref_frames = h->sps.ref_frame_count;
135  info->mb_adaptive_frame_field_flag = h->sps.mb_aff && !info->field_pic_flag;
136  info->constrained_intra_pred_flag = h->pps.constrained_intra_pred;
137  info->weighted_pred_flag = h->pps.weighted_pred;
138  info->weighted_bipred_idc = h->pps.weighted_bipred_idc;
139  info->frame_mbs_only_flag = h->sps.frame_mbs_only_flag;
140  info->transform_8x8_mode_flag = h->pps.transform_8x8_mode;
141  info->chroma_qp_index_offset = h->pps.chroma_qp_index_offset[0];
142  info->second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
143  info->pic_init_qp_minus26 = h->pps.init_qp - 26;
144  info->num_ref_idx_l0_active_minus1 = h->pps.ref_count[0] - 1;
145  info->num_ref_idx_l1_active_minus1 = h->pps.ref_count[1] - 1;
146  info->log2_max_frame_num_minus4 = h->sps.log2_max_frame_num - 4;
147  info->pic_order_cnt_type = h->sps.poc_type;
148  info->log2_max_pic_order_cnt_lsb_minus4 = h->sps.poc_type ? 0 : h->sps.log2_max_poc_lsb - 4;
149  info->delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
150  info->direct_8x8_inference_flag = h->sps.direct_8x8_inference_flag;
151  info->entropy_coding_mode_flag = h->pps.cabac;
152  info->pic_order_present_flag = h->pps.pic_order_present;
153  info->deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
154  info->redundant_pic_cnt_present_flag = h->pps.redundant_pic_cnt_present;
155 
156  memcpy(info->scaling_lists_4x4, h->pps.scaling_matrix4,
157  sizeof(info->scaling_lists_4x4));
158  memcpy(info->scaling_lists_8x8[0], h->pps.scaling_matrix8[0],
159  sizeof(info->scaling_lists_8x8[0]));
160  memcpy(info->scaling_lists_8x8[1], h->pps.scaling_matrix8[3],
161  sizeof(info->scaling_lists_8x8[1]));
162 
164 
165  return ff_vdpau_common_start_frame(pic_ctx, buffer, size);
166 }
167 
168 static const uint8_t start_code_prefix[3] = { 0x00, 0x00, 0x01 };
169 
171  const uint8_t *buffer, uint32_t size)
172 {
173  H264Context *h = avctx->priv_data;
174  H264Picture *pic = h->cur_pic_ptr;
175  struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
176  int val;
177 
178  val = ff_vdpau_add_buffer(pic_ctx, start_code_prefix, 3);
179  if (val)
180  return val;
181 
182  val = ff_vdpau_add_buffer(pic_ctx, buffer, size);
183  if (val)
184  return val;
185 
186  pic_ctx->info.h264.slice_count++;
187  return 0;
188 }
189 
191 {
192  AVVDPAUContext *hwctx = avctx->hwaccel_context;
193  H264Context *h = avctx->priv_data;
194  H264Picture *pic = h->cur_pic_ptr;
195  struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
196  VdpVideoSurface surf = ff_vdpau_get_surface_id(&pic->f);
197 
198  hwctx->render(hwctx->decoder, surf, (void *)&pic_ctx->info,
199  pic_ctx->bitstream_buffers_used, pic_ctx->bitstream_buffers);
200 
202  av_freep(&pic_ctx->bitstream_buffers);
203 
204  return 0;
205 }
206 
208  .name = "h264_vdpau",
209  .type = AVMEDIA_TYPE_VIDEO,
210  .id = AV_CODEC_ID_H264,
211  .pix_fmt = AV_PIX_FMT_VDPAU,
212  .start_frame = vdpau_h264_start_frame,
213  .end_frame = vdpau_h264_end_frame,
214  .decode_slice = vdpau_h264_decode_slice,
215  .frame_priv_data_size = sizeof(struct vdpau_picture_context),
216 };
int long_ref
1->long term reference 0->short term reference
Definition: h264.h:289
int size
union AVVDPAUPictureInfo info
VDPAU picture information.
int weighted_bipred_idc
Definition: h264.h:227
int chroma_qp_index_offset[2]
Definition: h264.h:230
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:41
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264.h:225
int bitstream_buffers_used
Useful bitstream buffers in the bitstream buffers table.
int frame_mbs_only_flag
Definition: h264.h:173
H264Context.
Definition: h264.h:303
Public libavcodec VDPAU header.
struct AVFrame f
Definition: h264.h:264
static void vdpau_h264_clear_rf(VdpReferenceFrameH264 *rf)
Definition: vdpau_h264.c:39
H264Picture * long_ref[32]
Definition: h264.h:565
int picture_structure
Definition: h264.h:419
AVHWAccel ff_h264_vdpau_hwaccel
Definition: vdpau_h264.c:207
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
VdpBitstreamBuffer * bitstream_buffers
Table of bitstream buffers.
uint8_t scaling_matrix4[6][16]
Definition: h264.h:235
int deblocking_filter_parameters_present
deblocking_filter_parameters_present_flag
Definition: h264.h:231
uint8_t
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2438
int cabac
entropy_coding_mode_flag
Definition: h264.h:221
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:34
static int vdpau_h264_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: vdpau_h264.c:118
VdpDecoder decoder
VDPAU decoder handle.
Definition: vdpau.h:88
int redundant_pic_cnt_present
redundant_pic_cnt_present_flag
Definition: h264.h:233
static void vdpau_h264_set_rf(VdpReferenceFrameH264 *rf, H264Picture *pic, int pic_structure)
Definition: vdpau_h264.c:50
H.264 / AVC / MPEG4 part10 codec.
static int32_t h264_foc(int foc)
Definition: vdpau_h264.c:32
int frame_num
Definition: h264.h:544
int mb_aff
mb_adaptive_frame_field_flag
Definition: h264.h:174
This structure is used to share data between the libavcodec library and the client video application...
Definition: vdpau.h:82
int poc_type
pic_order_cnt_type
Definition: h264.h:163
int constrained_intra_pred
constrained_intra_pred_flag
Definition: h264.h:232
void * hwaccel_picture_private
hardware accelerator private data
Definition: h264.h:277
PPS pps
current pps
Definition: h264.h:402
int weighted_pred
weighted_pred_flag
Definition: h264.h:226
#define PICT_TOP_FIELD
Definition: mpegutils.h:33
int frame_num
frame_num (raw frame_num from slice header)
Definition: h264.h:284
int delta_pic_order_always_zero_flag
Definition: h264.h:165
uint8_t scaling_matrix8[6][64]
Definition: h264.h:236
int ref_frame_count
num_ref_frames
Definition: h264.h:169
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:2893
int reference
Definition: h264.h:296
SPS sps
current sps
Definition: h264.h:401
int32_t
static int vdpau_h264_end_frame(AVCodecContext *avctx)
Definition: vdpau_h264.c:190
static char buffer[20]
Definition: seek-test.c:31
static void vdpau_h264_set_reference_frames(AVCodecContext *avctx)
Definition: vdpau_h264.c:67
int init_qp
pic_init_qp_minus26 + 26
Definition: h264.h:228
int direct_8x8_inference_flag
Definition: h264.h:175
int pic_order_present
pic_order_present_flag
Definition: h264.h:222
AVCodecContext * avctx
Definition: h264.h:304
Libavcodec external API header.
AVHWAccel.
Definition: avcodec.h:2887
static const uint8_t start_code_prefix[3]
Definition: vdpau_h264.c:168
H264Picture * short_ref[32]
Definition: h264.h:564
int field_poc[2]
top/bottom POC
Definition: h264.h:282
main external API structure.
Definition: avcodec.h:1044
#define H264_RF_COUNT
HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface.
Definition: pixfmt.h:190
H264Picture * cur_pic_ptr
Definition: h264.h:315
VdpDecoderRender * render
VDPAU decoder render callback.
Definition: vdpau.h:95
int log2_max_poc_lsb
log2_max_pic_order_cnt_lsb_minus4
Definition: h264.h:164
void ff_h264_draw_horiz_band(H264Context *h, int y, int height)
Definition: h264.c:80
int transform_8x8_mode
transform_8x8_mode_flag
Definition: h264.h:234
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
VdpPictureInfoH264 h264
Definition: vdpau.h:62
int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx, const uint8_t *buf, uint32_t size)
Definition: vdpau.c:72
int log2_max_frame_num
log2_max_frame_num_minus4 + 4
Definition: h264.h:162
static int vdpau_h264_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: vdpau_h264.c:170
void * priv_data
Definition: avcodec.h:1086
#define PICT_FRAME
Definition: mpegutils.h:35
int nal_ref_idc
Definition: h264.h:517
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:580