FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vdpau.c
Go to the documentation of this file.
1 /*
2  * Video Decode and Presentation API for UNIX (VDPAU) is used for
3  * HW decode acceleration for MPEG-1/2, MPEG-4 ASP, H.264 and VC-1.
4  *
5  * Copyright (c) 2008 NVIDIA
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
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include <limits.h>
25 #include "avcodec.h"
26 #include "h264.h"
27 #include "vc1.h"
28 
29 #undef NDEBUG
30 #include <assert.h>
31 
32 #include "vdpau.h"
33 #include "vdpau_internal.h"
34 
35 /**
36  * @addtogroup VDPAU_Decoding
37  *
38  * @{
39  */
40 
42  av_unused const uint8_t *buffer,
43  av_unused uint32_t size)
44 {
45  AVVDPAUContext *hwctx = avctx->hwaccel_context;
46 
47  hwctx->bitstream_buffers_used = 0;
48  return 0;
49 }
50 
51 #if CONFIG_H263_VDPAU_HWACCEL || CONFIG_MPEG1_VDPAU_HWACCEL || \
52  CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
53  CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
55 {
56  AVVDPAUContext *hwctx = avctx->hwaccel_context;
57  MpegEncContext *s = avctx->priv_data;
58  VdpVideoSurface surf = ff_vdpau_get_surface_id(s->current_picture_ptr);
59 
60  hwctx->render(hwctx->decoder, surf, (void *)&hwctx->info,
62 
64  hwctx->bitstream_buffers_used = 0;
65 
66  return 0;
67 }
68 #endif
69 
71  const uint8_t *buf, uint32_t size)
72 {
73  AVVDPAUContext *hwctx = avctx->hwaccel_context;
74  VdpBitstreamBuffer *buffers = hwctx->bitstream_buffers;
75 
76  buffers = av_fast_realloc(buffers, &hwctx->bitstream_buffers_allocated,
77  (hwctx->bitstream_buffers_used + 1) * sizeof(*buffers));
78  if (!buffers)
79  return AVERROR(ENOMEM);
80 
81  hwctx->bitstream_buffers = buffers;
82  buffers += hwctx->bitstream_buffers_used++;
83 
84  buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
85  buffers->bitstream = buf;
86  buffers->bitstream_bytes = size;
87  return 0;
88 }
89 
90 /* Obsolete non-hwaccel VDPAU support below... */
91 
93 {
94  struct vdpau_render_state *render, *render_ref;
95  VdpReferenceFrameH264 *rf, *rf2;
96  Picture *pic;
97  int i, list, pic_frame_idx;
98 
99  render = (struct vdpau_render_state *)h->cur_pic_ptr->f.data[0];
100  assert(render);
101 
102  rf = &render->info.h264.referenceFrames[0];
103 #define H264_RF_COUNT FF_ARRAY_ELEMS(render->info.h264.referenceFrames)
104 
105  for (list = 0; list < 2; ++list) {
106  Picture **lp = list ? h->long_ref : h->short_ref;
107  int ls = list ? 16 : h->short_ref_count;
108 
109  for (i = 0; i < ls; ++i) {
110  pic = lp[i];
111  if (!pic || !pic->reference)
112  continue;
113  pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
114 
115  render_ref = (struct vdpau_render_state *)pic->f.data[0];
116  assert(render_ref);
117 
118  rf2 = &render->info.h264.referenceFrames[0];
119  while (rf2 != rf) {
120  if (
121  (rf2->surface == render_ref->surface)
122  && (rf2->is_long_term == pic->long_ref)
123  && (rf2->frame_idx == pic_frame_idx)
124  )
125  break;
126  ++rf2;
127  }
128  if (rf2 != rf) {
129  rf2->top_is_reference |= (pic->reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
130  rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
131  continue;
132  }
133 
134  if (rf >= &render->info.h264.referenceFrames[H264_RF_COUNT])
135  continue;
136 
137  rf->surface = render_ref->surface;
138  rf->is_long_term = pic->long_ref;
139  rf->top_is_reference = (pic->reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
140  rf->bottom_is_reference = (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
141  rf->field_order_cnt[0] = pic->field_poc[0];
142  rf->field_order_cnt[1] = pic->field_poc[1];
143  rf->frame_idx = pic_frame_idx;
144 
145  ++rf;
146  }
147  }
148 
149  for (; rf < &render->info.h264.referenceFrames[H264_RF_COUNT]; ++rf) {
150  rf->surface = VDP_INVALID_HANDLE;
151  rf->is_long_term = 0;
152  rf->top_is_reference = 0;
153  rf->bottom_is_reference = 0;
154  rf->field_order_cnt[0] = 0;
155  rf->field_order_cnt[1] = 0;
156  rf->frame_idx = 0;
157  }
158 }
159 
160 void ff_vdpau_add_data_chunk(uint8_t *data, const uint8_t *buf, int buf_size)
161 {
162  struct vdpau_render_state *render = (struct vdpau_render_state*)data;
163  assert(render);
164 
166  render->bitstream_buffers,
168  sizeof(*render->bitstream_buffers)*(render->bitstream_buffers_used + 1)
169  );
170 
171  render->bitstream_buffers[render->bitstream_buffers_used].struct_version = VDP_BITSTREAM_BUFFER_VERSION;
172  render->bitstream_buffers[render->bitstream_buffers_used].bitstream = buf;
173  render->bitstream_buffers[render->bitstream_buffers_used].bitstream_bytes = buf_size;
174  render->bitstream_buffers_used++;
175 }
176 
177 #if CONFIG_H264_VDPAU_DECODER
179 {
180  struct vdpau_render_state *render;
181  int i;
182 
183  render = (struct vdpau_render_state *)h->cur_pic_ptr->f.data[0];
184  assert(render);
185 
186  for (i = 0; i < 2; ++i) {
187  int foc = h->cur_pic_ptr->field_poc[i];
188  if (foc == INT_MAX)
189  foc = 0;
190  render->info.h264.field_order_cnt[i] = foc;
191  }
192 
193  render->info.h264.frame_num = h->frame_num;
194 }
195 
197 {
198  struct vdpau_render_state *render;
199 
200  render = (struct vdpau_render_state *)h->cur_pic_ptr->f.data[0];
201  assert(render);
202 
203  render->info.h264.slice_count = h->slice_num;
204  if (render->info.h264.slice_count < 1)
205  return;
206 
207  render->info.h264.is_reference = (h->cur_pic_ptr->reference & 3) ? VDP_TRUE : VDP_FALSE;
208  render->info.h264.field_pic_flag = h->picture_structure != PICT_FRAME;
209  render->info.h264.bottom_field_flag = h->picture_structure == PICT_BOTTOM_FIELD;
210  render->info.h264.num_ref_frames = h->sps.ref_frame_count;
211  render->info.h264.mb_adaptive_frame_field_flag = h->sps.mb_aff && !render->info.h264.field_pic_flag;
212  render->info.h264.constrained_intra_pred_flag = h->pps.constrained_intra_pred;
213  render->info.h264.weighted_pred_flag = h->pps.weighted_pred;
214  render->info.h264.weighted_bipred_idc = h->pps.weighted_bipred_idc;
215  render->info.h264.frame_mbs_only_flag = h->sps.frame_mbs_only_flag;
216  render->info.h264.transform_8x8_mode_flag = h->pps.transform_8x8_mode;
217  render->info.h264.chroma_qp_index_offset = h->pps.chroma_qp_index_offset[0];
218  render->info.h264.second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
219  render->info.h264.pic_init_qp_minus26 = h->pps.init_qp - 26;
220  render->info.h264.num_ref_idx_l0_active_minus1 = h->pps.ref_count[0] - 1;
221  render->info.h264.num_ref_idx_l1_active_minus1 = h->pps.ref_count[1] - 1;
222  render->info.h264.log2_max_frame_num_minus4 = h->sps.log2_max_frame_num - 4;
223  render->info.h264.pic_order_cnt_type = h->sps.poc_type;
224  render->info.h264.log2_max_pic_order_cnt_lsb_minus4 = h->sps.poc_type ? 0 : h->sps.log2_max_poc_lsb - 4;
225  render->info.h264.delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
226  render->info.h264.direct_8x8_inference_flag = h->sps.direct_8x8_inference_flag;
227  render->info.h264.entropy_coding_mode_flag = h->pps.cabac;
228  render->info.h264.pic_order_present_flag = h->pps.pic_order_present;
229  render->info.h264.deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
230  render->info.h264.redundant_pic_cnt_present_flag = h->pps.redundant_pic_cnt_present;
231  memcpy(render->info.h264.scaling_lists_4x4, h->pps.scaling_matrix4, sizeof(render->info.h264.scaling_lists_4x4));
232  memcpy(render->info.h264.scaling_lists_8x8[0], h->pps.scaling_matrix8[0], sizeof(render->info.h264.scaling_lists_8x8[0]));
233  memcpy(render->info.h264.scaling_lists_8x8[1], h->pps.scaling_matrix8[3], sizeof(render->info.h264.scaling_lists_8x8[0]));
234 
236  render->bitstream_buffers_used = 0;
237 }
238 #endif /* CONFIG_H264_VDPAU_DECODER */
239 
240 #if CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER
242  int buf_size, int slice_count)
243 {
244  struct vdpau_render_state *render, *last, *next;
245  int i;
246 
247  if (!s->current_picture_ptr) return;
248 
249  render = (struct vdpau_render_state *)s->current_picture_ptr->f.data[0];
250  assert(render);
251 
252  /* fill VdpPictureInfoMPEG1Or2 struct */
253  render->info.mpeg.picture_structure = s->picture_structure;
254  render->info.mpeg.picture_coding_type = s->pict_type;
255  render->info.mpeg.intra_dc_precision = s->intra_dc_precision;
256  render->info.mpeg.frame_pred_frame_dct = s->frame_pred_frame_dct;
257  render->info.mpeg.concealment_motion_vectors = s->concealment_motion_vectors;
258  render->info.mpeg.intra_vlc_format = s->intra_vlc_format;
259  render->info.mpeg.alternate_scan = s->alternate_scan;
260  render->info.mpeg.q_scale_type = s->q_scale_type;
261  render->info.mpeg.top_field_first = s->top_field_first;
262  render->info.mpeg.full_pel_forward_vector = s->full_pel[0]; // MPEG-1 only. Set 0 for MPEG-2
263  render->info.mpeg.full_pel_backward_vector = s->full_pel[1]; // MPEG-1 only. Set 0 for MPEG-2
264  render->info.mpeg.f_code[0][0] = s->mpeg_f_code[0][0]; // For MPEG-1 fill both horiz. & vert.
265  render->info.mpeg.f_code[0][1] = s->mpeg_f_code[0][1];
266  render->info.mpeg.f_code[1][0] = s->mpeg_f_code[1][0];
267  render->info.mpeg.f_code[1][1] = s->mpeg_f_code[1][1];
268  for (i = 0; i < 64; ++i) {
269  render->info.mpeg.intra_quantizer_matrix[i] = s->intra_matrix[i];
270  render->info.mpeg.non_intra_quantizer_matrix[i] = s->inter_matrix[i];
271  }
272 
273  render->info.mpeg.forward_reference = VDP_INVALID_HANDLE;
274  render->info.mpeg.backward_reference = VDP_INVALID_HANDLE;
275 
276  switch(s->pict_type){
277  case AV_PICTURE_TYPE_B:
278  next = (struct vdpau_render_state *)s->next_picture.f.data[0];
279  assert(next);
280  render->info.mpeg.backward_reference = next->surface;
281  // no return here, going to set forward prediction
282  case AV_PICTURE_TYPE_P:
283  last = (struct vdpau_render_state *)s->last_picture.f.data[0];
284  if (!last) // FIXME: Does this test make sense?
285  last = render; // predict second field from the first
286  render->info.mpeg.forward_reference = last->surface;
287  }
288 
289  ff_vdpau_add_data_chunk(s->current_picture_ptr->f.data[0], buf, buf_size);
290 
291  render->info.mpeg.slice_count = slice_count;
292 
293  if (slice_count)
295  render->bitstream_buffers_used = 0;
296 }
297 #endif /* CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER */
298 
299 #if CONFIG_VC1_VDPAU_DECODER
301  int buf_size)
302 {
303  VC1Context *v = s->avctx->priv_data;
304  struct vdpau_render_state *render, *last, *next;
305 
306  render = (struct vdpau_render_state *)s->current_picture.f.data[0];
307  assert(render);
308 
309  /* fill LvPictureInfoVC1 struct */
310  render->info.vc1.frame_coding_mode = v->fcm ? v->fcm + 1 : 0;
311  render->info.vc1.postprocflag = v->postprocflag;
312  render->info.vc1.pulldown = v->broadcast;
313  render->info.vc1.interlace = v->interlace;
314  render->info.vc1.tfcntrflag = v->tfcntrflag;
315  render->info.vc1.finterpflag = v->finterpflag;
316  render->info.vc1.psf = v->psf;
317  render->info.vc1.dquant = v->dquant;
318  render->info.vc1.panscan_flag = v->panscanflag;
319  render->info.vc1.refdist_flag = v->refdist_flag;
320  render->info.vc1.quantizer = v->quantizer_mode;
321  render->info.vc1.extended_mv = v->extended_mv;
322  render->info.vc1.extended_dmv = v->extended_dmv;
323  render->info.vc1.overlap = v->overlap;
324  render->info.vc1.vstransform = v->vstransform;
325  render->info.vc1.loopfilter = v->s.loop_filter;
326  render->info.vc1.fastuvmc = v->fastuvmc;
327  render->info.vc1.range_mapy_flag = v->range_mapy_flag;
328  render->info.vc1.range_mapy = v->range_mapy;
329  render->info.vc1.range_mapuv_flag = v->range_mapuv_flag;
330  render->info.vc1.range_mapuv = v->range_mapuv;
331  /* Specific to simple/main profile only */
332  render->info.vc1.multires = v->multires;
333  render->info.vc1.syncmarker = v->s.resync_marker;
334  render->info.vc1.rangered = v->rangered | (v->rangeredfrm << 1);
335  render->info.vc1.maxbframes = v->s.max_b_frames;
336 
337  render->info.vc1.deblockEnable = v->postprocflag & 1;
338  render->info.vc1.pquant = v->pq;
339 
340  render->info.vc1.forward_reference = VDP_INVALID_HANDLE;
341  render->info.vc1.backward_reference = VDP_INVALID_HANDLE;
342 
343  if (v->bi_type)
344  render->info.vc1.picture_type = 4;
345  else
346  render->info.vc1.picture_type = s->pict_type - 1 + s->pict_type / 3;
347 
348  switch(s->pict_type){
349  case AV_PICTURE_TYPE_B:
350  next = (struct vdpau_render_state *)s->next_picture.f.data[0];
351  assert(next);
352  render->info.vc1.backward_reference = next->surface;
353  // no break here, going to set forward prediction
354  case AV_PICTURE_TYPE_P:
355  last = (struct vdpau_render_state *)s->last_picture.f.data[0];
356  if (!last) // FIXME: Does this test make sense?
357  last = render; // predict second field from the first
358  render->info.vc1.forward_reference = last->surface;
359  }
360 
361  ff_vdpau_add_data_chunk(s->current_picture_ptr->f.data[0], buf, buf_size);
362 
363  render->info.vc1.slice_count = 1;
364 
366  render->bitstream_buffers_used = 0;
367 }
368 #endif /* (CONFIG_VC1_VDPAU_DECODER */
369 
370 #if CONFIG_MPEG4_VDPAU_DECODER
372  int buf_size)
373 {
374  struct vdpau_render_state *render, *last, *next;
375  int i;
376 
377  if (!s->current_picture_ptr) return;
378 
379  render = (struct vdpau_render_state *)s->current_picture_ptr->f.data[0];
380  assert(render);
381 
382  /* fill VdpPictureInfoMPEG4Part2 struct */
383  render->info.mpeg4.trd[0] = s->pp_time;
384  render->info.mpeg4.trb[0] = s->pb_time;
385  render->info.mpeg4.trd[1] = s->pp_field_time >> 1;
386  render->info.mpeg4.trb[1] = s->pb_field_time >> 1;
387  render->info.mpeg4.vop_time_increment_resolution = s->avctx->time_base.den;
388  render->info.mpeg4.vop_coding_type = 0;
389  render->info.mpeg4.vop_fcode_forward = s->f_code;
390  render->info.mpeg4.vop_fcode_backward = s->b_code;
391  render->info.mpeg4.resync_marker_disable = !s->resync_marker;
392  render->info.mpeg4.interlaced = !s->progressive_sequence;
393  render->info.mpeg4.quant_type = s->mpeg_quant;
394  render->info.mpeg4.quarter_sample = s->quarter_sample;
395  render->info.mpeg4.short_video_header = s->avctx->codec->id == AV_CODEC_ID_H263;
396  render->info.mpeg4.rounding_control = s->no_rounding;
397  render->info.mpeg4.alternate_vertical_scan_flag = s->alternate_scan;
398  render->info.mpeg4.top_field_first = s->top_field_first;
399  for (i = 0; i < 64; ++i) {
400  render->info.mpeg4.intra_quantizer_matrix[i] = s->intra_matrix[i];
401  render->info.mpeg4.non_intra_quantizer_matrix[i] = s->inter_matrix[i];
402  }
403  render->info.mpeg4.forward_reference = VDP_INVALID_HANDLE;
404  render->info.mpeg4.backward_reference = VDP_INVALID_HANDLE;
405 
406  switch (s->pict_type) {
407  case AV_PICTURE_TYPE_B:
408  next = (struct vdpau_render_state *)s->next_picture.f.data[0];
409  assert(next);
410  render->info.mpeg4.backward_reference = next->surface;
411  render->info.mpeg4.vop_coding_type = 2;
412  // no break here, going to set forward prediction
413  case AV_PICTURE_TYPE_P:
414  last = (struct vdpau_render_state *)s->last_picture.f.data[0];
415  assert(last);
416  render->info.mpeg4.forward_reference = last->surface;
417  }
418 
419  ff_vdpau_add_data_chunk(s->current_picture_ptr->f.data[0], buf, buf_size);
420 
422  render->bitstream_buffers_used = 0;
423 }
424 #endif /* CONFIG_MPEG4_VDPAU_DECODER */
425 
426 /* @}*/