FFmpeg  2.1.1
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 "h264.h"
28 #include "vdpau.h"
29 #include "vdpau_internal.h"
30 
31 static int32_t h264_foc(int foc)
32 {
33  if (foc == INT_MAX)
34  foc = 0;
35  return foc;
36 }
37 
38 static void vdpau_h264_clear_rf(VdpReferenceFrameH264 *rf)
39 {
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;
46  rf->frame_idx = 0;
47 }
48 
49 static void vdpau_h264_set_rf(VdpReferenceFrameH264 *rf, Picture *pic,
50  int pic_structure)
51 {
52  VdpVideoSurface surface = ff_vdpau_get_surface_id(pic);
53 
54  if (pic_structure == 0)
55  pic_structure = pic->reference;
56 
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;
64 }
65 
67 {
68  H264Context * const h = avctx->priv_data;
69  struct vdpau_picture_context *pic_ctx = h->cur_pic_ptr->hwaccel_picture_private;
70  VdpPictureInfoH264 *info = &pic_ctx->info.h264;
71  int list;
72 
73  VdpReferenceFrameH264 *rf = &info->referenceFrames[0];
74 #define H264_RF_COUNT FF_ARRAY_ELEMS(info->referenceFrames)
75 
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;
79 
80  for (i = 0; i < ls; ++i) {
81  Picture *pic = lp[i];
82  VdpReferenceFrameH264 *rf2;
83  VdpVideoSurface surface_ref;
84  int pic_frame_idx;
85 
86  if (!pic || !pic->reference)
87  continue;
88  pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
89  surface_ref = ff_vdpau_get_surface_id(pic);
90 
91  rf2 = &info->referenceFrames[0];
92  while (rf2 != rf) {
93  if ((rf2->surface == surface_ref) &&
94  (rf2->is_long_term == pic->long_ref) &&
95  (rf2->frame_idx == pic_frame_idx))
96  break;
97  ++rf2;
98  }
99  if (rf2 != rf) {
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;
102  continue;
103  }
104 
105  if (rf >= &info->referenceFrames[H264_RF_COUNT])
106  continue;
107 
108  vdpau_h264_set_rf(rf, pic, pic->reference);
109  ++rf;
110  }
111  }
112 
113  for (; rf < &info->referenceFrames[H264_RF_COUNT]; ++rf)
115 }
116 
118  const uint8_t *buffer, uint32_t size)
119 {
120  H264Context * const h = avctx->priv_data;
121  Picture *pic = h->cur_pic_ptr;
122  struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
123  VdpPictureInfoH264 *info = &pic_ctx->info.h264;
124 
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;
154 
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]));
161 
163 
164  return ff_vdpau_common_start_frame(pic, buffer, size);
165 }
166 
167 static const uint8_t start_code_prefix[3] = { 0x00, 0x00, 0x01 };
168 
170  const uint8_t *buffer, uint32_t size)
171 {
172  H264Context *h = avctx->priv_data;
173  Picture *pic = h->cur_pic_ptr;
174  struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
175  int val;
176 
177  val = ff_vdpau_add_buffer(pic, start_code_prefix, 3);
178  if (val)
179  return val;
180 
181  val = ff_vdpau_add_buffer(pic, buffer, size);
182  if (val)
183  return val;
184 
185  pic_ctx->info.h264.slice_count++;
186  return 0;
187 }
188 
190 {
191  int res = 0;
192  AVVDPAUContext *hwctx = avctx->hwaccel_context;
193  H264Context *h = avctx->priv_data;
194  Picture *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);
197 
198  if (!hwctx->render) {
199  res = hwctx->render2(avctx, &pic->f, (void *)&pic_ctx->info,
200  pic_ctx->bitstream_buffers_used, pic_ctx->bitstream_buffers);
201  } else
202  hwctx->render(hwctx->decoder, surf, (void *)&pic_ctx->info,
203  pic_ctx->bitstream_buffers_used, pic_ctx->bitstream_buffers);
204 
206  av_freep(&pic_ctx->bitstream_buffers);
207 
208  return res;
209 }
210 
212  .name = "h264_vdpau",
213  .type = AVMEDIA_TYPE_VIDEO,
214  .id = AV_CODEC_ID_H264,
215  .pix_fmt = AV_PIX_FMT_VDPAU,
216  .start_frame = vdpau_h264_start_frame,
217  .end_frame = vdpau_h264_end_frame,
218  .decode_slice = vdpau_h264_decode_slice,
219  .priv_data_size = sizeof(struct vdpau_picture_context),
220 };
#define PICT_BOTTOM_FIELD
Definition: mpegvideo.h:668
const char const char void * val
Definition: avisynth_c.h:671
#define PICT_TOP_FIELD
Definition: mpegvideo.h:667
VdpDecoderRender * render
VDPAU decoder render callback.
Definition: vdpau.h:103
int size
static void vdpau_h264_set_rf(VdpReferenceFrameH264 *rf, Picture *pic, int pic_structure)
Definition: vdpau_h264.c:49
int weighted_bipred_idc
Definition: h264.h:235
int chroma_qp_index_offset[2]
Definition: h264.h:238
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264.h:233
int frame_mbs_only_flag
Definition: h264.h:181
H264Context.
Definition: h264.h:286
static void vdpau_h264_clear_rf(VdpReferenceFrameH264 *rf)
Definition: vdpau_h264.c:38
int picture_structure
Definition: h264.h:408
AVHWAccel ff_h264_vdpau_hwaccel
Definition: vdpau_h264.c:211
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
int long_ref
1-&gt;long term reference 0-&gt;short term reference
Definition: mpegvideo.h:165
static uint8_t * res
Definition: ffhash.c:43
uint8_t scaling_matrix4[6][16]
Definition: h264.h:243
int deblocking_filter_parameters_present
deblocking_filter_parameters_present_flag
Definition: h264.h:239
uint8_t
#define PICT_FRAME
Definition: mpegvideo.h:669
int cabac
entropy_coding_mode_flag
Definition: h264.h:229
static int vdpau_h264_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: vdpau_h264.c:117
VdpDecoder decoder
VDPAU decoder handle.
Definition: vdpau.h:96
int redundant_pic_cnt_present
redundant_pic_cnt_present_flag
Definition: h264.h:241
int ff_vdpau_common_start_frame(Picture *pic, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition: vdpau.c:48
static uintptr_t ff_vdpau_get_surface_id(Picture *pic)
Extract VdpVideoSurface from a Picture.
H.264 / AVC / MPEG4 part10 codec.
static int32_t h264_foc(int foc)
Definition: vdpau_h264.c:31
int frame_num
Definition: h264.h:533
int mb_aff
mb_adaptive_frame_field_flag
Definition: h264.h:182
This structure is used to share data between the libavcodec library and the client video application...
Definition: vdpau.h:90
int poc_type
pic_order_cnt_type
Definition: h264.h:171
int constrained_intra_pred
constrained_intra_pred_flag
Definition: h264.h:240
int reference
Definition: mpegvideo.h:178
PPS pps
current pps
Definition: h264.h:391
int weighted_pred
weighted_pred_flag
Definition: h264.h:234
Libavcodec external API header.
int delta_pic_order_always_zero_flag
Definition: h264.h:173
uint8_t scaling_matrix8[6][64]
Definition: h264.h:244
int ref_frame_count
num_ref_frames
Definition: h264.h:177
Picture * long_ref[32]
Definition: h264.h:554
Picture.
Definition: mpegvideo.h:97
void * hwaccel_picture_private
hardware accelerator private data
Definition: mpegvideo.h:132
SPS sps
current sps
Definition: h264.h:386
int32_t
static int vdpau_h264_end_frame(AVCodecContext *avctx)
Definition: vdpau_h264.c:189
static char buffer[20]
Definition: seek-test.c:31
static void vdpau_h264_set_reference_frames(AVCodecContext *avctx)
Definition: vdpau_h264.c:66
int init_qp
pic_init_qp_minus26 + 26
Definition: h264.h:236
int frame_num
h264 frame_num (raw frame_num from slice header)
Definition: mpegvideo.h:161
int direct_8x8_inference_flag
Definition: h264.h:183
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3027
int pic_order_present
pic_order_present_flag
Definition: h264.h:230
AVCodecContext * avctx
Definition: h264.h:287
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2513
AVHWAccel.
Definition: avcodec.h:3021
static const uint8_t start_code_prefix[3]
Definition: vdpau_h264.c:167
main external API structure.
Definition: avcodec.h:1146
Picture * short_ref[32]
Definition: h264.h:553
#define H264_RF_COUNT
int log2_max_poc_lsb
log2_max_pic_order_cnt_lsb_minus4
Definition: h264.h:172
void ff_h264_draw_horiz_band(H264Context *h, int y, int height)
Definition: h264.c:235
Public libavcodec VDPAU header.
int field_poc[2]
h264 top/bottom POC
Definition: mpegvideo.h:159
void * priv_data
Definition: avcodec.h:1182
int transform_8x8_mode
transform_8x8_mode_flag
Definition: h264.h:242
AVVDPAU_Render2 render2
Definition: vdpau.h:139
int log2_max_frame_num
log2_max_frame_num_minus4 + 4
Definition: h264.h:170
static int vdpau_h264_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: vdpau_h264.c:169
Picture * cur_pic_ptr
Definition: h264.h:299
int ff_vdpau_add_buffer(Picture *pic, const uint8_t *buf, uint32_t size)
Definition: vdpau.c:86
int pic_id
h264 pic_num (short -&gt; no wrap version of pic_num, pic_num &amp; max_pic_num; long -&gt; long_pic_num) ...
Definition: mpegvideo.h:163
struct AVFrame f
Definition: mpegvideo.h:98
HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface.
Definition: avcodec.h:4675
int nal_ref_idc
Definition: h264.h:506
VdpVideoSurface surface
Used as rendered surface, never changed.
Definition: vdpau.h:170
int short_ref_count
number of actual short term references
Definition: h264.h:569