FFmpeg
 All Data Structures 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 {
43  H264Context *h = s->avctx->priv_data;
44  struct vdpau_render_state *render, *render_ref;
45  VdpReferenceFrameH264 *rf, *rf2;
46  Picture *pic;
47  int i, list, pic_frame_idx;
48 
49  render = (struct vdpau_render_state *)s->current_picture_ptr->f.data[0];
50  assert(render);
51 
52  rf = &render->info.h264.referenceFrames[0];
53 #define H264_RF_COUNT FF_ARRAY_ELEMS(render->info.h264.referenceFrames)
54 
55  for (list = 0; list < 2; ++list) {
56  Picture **lp = list ? h->long_ref : h->short_ref;
57  int ls = list ? 16 : h->short_ref_count;
58 
59  for (i = 0; i < ls; ++i) {
60  pic = lp[i];
61  if (!pic || !pic->f.reference)
62  continue;
63  pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
64 
65  render_ref = (struct vdpau_render_state *)pic->f.data[0];
66  assert(render_ref);
67 
68  rf2 = &render->info.h264.referenceFrames[0];
69  while (rf2 != rf) {
70  if (
71  (rf2->surface == render_ref->surface)
72  && (rf2->is_long_term == pic->long_ref)
73  && (rf2->frame_idx == pic_frame_idx)
74  )
75  break;
76  ++rf2;
77  }
78  if (rf2 != rf) {
79  rf2->top_is_reference |= (pic->f.reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
80  rf2->bottom_is_reference |= (pic->f.reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
81  continue;
82  }
83 
84  if (rf >= &render->info.h264.referenceFrames[H264_RF_COUNT])
85  continue;
86 
87  rf->surface = render_ref->surface;
88  rf->is_long_term = pic->long_ref;
89  rf->top_is_reference = (pic->f.reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
90  rf->bottom_is_reference = (pic->f.reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
91  rf->field_order_cnt[0] = pic->field_poc[0];
92  rf->field_order_cnt[1] = pic->field_poc[1];
93  rf->frame_idx = pic_frame_idx;
94 
95  ++rf;
96  }
97  }
98 
99  for (; rf < &render->info.h264.referenceFrames[H264_RF_COUNT]; ++rf) {
100  rf->surface = VDP_INVALID_HANDLE;
101  rf->is_long_term = 0;
102  rf->top_is_reference = 0;
103  rf->bottom_is_reference = 0;
104  rf->field_order_cnt[0] = 0;
105  rf->field_order_cnt[1] = 0;
106  rf->frame_idx = 0;
107  }
108 }
109 
111  const uint8_t *buf, int buf_size)
112 {
113  struct vdpau_render_state *render;
114 
115  render = (struct vdpau_render_state *)s->current_picture_ptr->f.data[0];
116  assert(render);
117 
119  render->bitstream_buffers,
121  sizeof(*render->bitstream_buffers)*(render->bitstream_buffers_used + 1)
122  );
123 
124  render->bitstream_buffers[render->bitstream_buffers_used].struct_version = VDP_BITSTREAM_BUFFER_VERSION;
125  render->bitstream_buffers[render->bitstream_buffers_used].bitstream = buf;
126  render->bitstream_buffers[render->bitstream_buffers_used].bitstream_bytes = buf_size;
127  render->bitstream_buffers_used++;
128 }
129 
131 {
132  H264Context *h = s->avctx->priv_data;
133  struct vdpau_render_state *render;
134  int i;
135 
136  render = (struct vdpau_render_state *)s->current_picture_ptr->f.data[0];
137  assert(render);
138 
139  for (i = 0; i < 2; ++i) {
140  int foc = s->current_picture_ptr->field_poc[i];
141  if (foc == INT_MAX)
142  foc = 0;
143  render->info.h264.field_order_cnt[i] = foc;
144  }
145 
146  render->info.h264.frame_num = h->frame_num;
147 }
148 
150 {
151  H264Context *h = s->avctx->priv_data;
152  struct vdpau_render_state *render;
153 
154  render = (struct vdpau_render_state *)s->current_picture_ptr->f.data[0];
155  assert(render);
156 
157  render->info.h264.slice_count = h->slice_num;
158  if (render->info.h264.slice_count < 1)
159  return;
160 
161  render->info.h264.is_reference = (s->current_picture_ptr->f.reference & 3) ? VDP_TRUE : VDP_FALSE;
162  render->info.h264.field_pic_flag = s->picture_structure != PICT_FRAME;
163  render->info.h264.bottom_field_flag = s->picture_structure == PICT_BOTTOM_FIELD;
164  render->info.h264.num_ref_frames = h->sps.ref_frame_count;
165  render->info.h264.mb_adaptive_frame_field_flag = h->sps.mb_aff && !render->info.h264.field_pic_flag;
166  render->info.h264.constrained_intra_pred_flag = h->pps.constrained_intra_pred;
167  render->info.h264.weighted_pred_flag = h->pps.weighted_pred;
168  render->info.h264.weighted_bipred_idc = h->pps.weighted_bipred_idc;
169  render->info.h264.frame_mbs_only_flag = h->sps.frame_mbs_only_flag;
170  render->info.h264.transform_8x8_mode_flag = h->pps.transform_8x8_mode;
171  render->info.h264.chroma_qp_index_offset = h->pps.chroma_qp_index_offset[0];
172  render->info.h264.second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
173  render->info.h264.pic_init_qp_minus26 = h->pps.init_qp - 26;
174  render->info.h264.num_ref_idx_l0_active_minus1 = h->pps.ref_count[0] - 1;
175  render->info.h264.num_ref_idx_l1_active_minus1 = h->pps.ref_count[1] - 1;
176  render->info.h264.log2_max_frame_num_minus4 = h->sps.log2_max_frame_num - 4;
177  render->info.h264.pic_order_cnt_type = h->sps.poc_type;
178  render->info.h264.log2_max_pic_order_cnt_lsb_minus4 = h->sps.poc_type ? 0 : h->sps.log2_max_poc_lsb - 4;
179  render->info.h264.delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
180  render->info.h264.direct_8x8_inference_flag = h->sps.direct_8x8_inference_flag;
181  render->info.h264.entropy_coding_mode_flag = h->pps.cabac;
182  render->info.h264.pic_order_present_flag = h->pps.pic_order_present;
183  render->info.h264.deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
184  render->info.h264.redundant_pic_cnt_present_flag = h->pps.redundant_pic_cnt_present;
185  memcpy(render->info.h264.scaling_lists_4x4, h->pps.scaling_matrix4, sizeof(render->info.h264.scaling_lists_4x4));
186  memcpy(render->info.h264.scaling_lists_8x8[0], h->pps.scaling_matrix8[0], sizeof(render->info.h264.scaling_lists_8x8[0]));
187  memcpy(render->info.h264.scaling_lists_8x8[1], h->pps.scaling_matrix8[3], sizeof(render->info.h264.scaling_lists_8x8[0]));
188 
189  ff_draw_horiz_band(s, 0, s->avctx->height);
190  render->bitstream_buffers_used = 0;
191 }
192 
194  int buf_size, int slice_count)
195 {
196  struct vdpau_render_state *render, *last, *next;
197  int i;
198 
199  if (!s->current_picture_ptr) return;
200 
201  render = (struct vdpau_render_state *)s->current_picture_ptr->f.data[0];
202  assert(render);
203 
204  /* fill VdpPictureInfoMPEG1Or2 struct */
205  render->info.mpeg.picture_structure = s->picture_structure;
206  render->info.mpeg.picture_coding_type = s->pict_type;
207  render->info.mpeg.intra_dc_precision = s->intra_dc_precision;
208  render->info.mpeg.frame_pred_frame_dct = s->frame_pred_frame_dct;
209  render->info.mpeg.concealment_motion_vectors = s->concealment_motion_vectors;
210  render->info.mpeg.intra_vlc_format = s->intra_vlc_format;
211  render->info.mpeg.alternate_scan = s->alternate_scan;
212  render->info.mpeg.q_scale_type = s->q_scale_type;
213  render->info.mpeg.top_field_first = s->top_field_first;
214  render->info.mpeg.full_pel_forward_vector = s->full_pel[0]; // MPEG-1 only. Set 0 for MPEG-2
215  render->info.mpeg.full_pel_backward_vector = s->full_pel[1]; // MPEG-1 only. Set 0 for MPEG-2
216  render->info.mpeg.f_code[0][0] = s->mpeg_f_code[0][0]; // For MPEG-1 fill both horiz. & vert.
217  render->info.mpeg.f_code[0][1] = s->mpeg_f_code[0][1];
218  render->info.mpeg.f_code[1][0] = s->mpeg_f_code[1][0];
219  render->info.mpeg.f_code[1][1] = s->mpeg_f_code[1][1];
220  for (i = 0; i < 64; ++i) {
221  render->info.mpeg.intra_quantizer_matrix[i] = s->intra_matrix[i];
222  render->info.mpeg.non_intra_quantizer_matrix[i] = s->inter_matrix[i];
223  }
224 
225  render->info.mpeg.forward_reference = VDP_INVALID_HANDLE;
226  render->info.mpeg.backward_reference = VDP_INVALID_HANDLE;
227 
228  switch(s->pict_type){
229  case AV_PICTURE_TYPE_B:
230  next = (struct vdpau_render_state *)s->next_picture.f.data[0];
231  assert(next);
232  render->info.mpeg.backward_reference = next->surface;
233  // no return here, going to set forward prediction
234  case AV_PICTURE_TYPE_P:
235  last = (struct vdpau_render_state *)s->last_picture.f.data[0];
236  if (!last) // FIXME: Does this test make sense?
237  last = render; // predict second field from the first
238  render->info.mpeg.forward_reference = last->surface;
239  }
240 
241  ff_vdpau_add_data_chunk(s, buf, buf_size);
242 
243  render->info.mpeg.slice_count = slice_count;
244 
245  if (slice_count)
246  ff_draw_horiz_band(s, 0, s->avctx->height);
247  render->bitstream_buffers_used = 0;
248 }
249 
251  int buf_size)
252 {
253  VC1Context *v = s->avctx->priv_data;
254  struct vdpau_render_state *render, *last, *next;
255 
256  render = (struct vdpau_render_state *)s->current_picture.f.data[0];
257  assert(render);
258 
259  /* fill LvPictureInfoVC1 struct */
260  render->info.vc1.frame_coding_mode = v->fcm;
261  render->info.vc1.postprocflag = v->postprocflag;
262  render->info.vc1.pulldown = v->broadcast;
263  render->info.vc1.interlace = v->interlace;
264  render->info.vc1.tfcntrflag = v->tfcntrflag;
265  render->info.vc1.finterpflag = v->finterpflag;
266  render->info.vc1.psf = v->psf;
267  render->info.vc1.dquant = v->dquant;
268  render->info.vc1.panscan_flag = v->panscanflag;
269  render->info.vc1.refdist_flag = v->refdist_flag;
270  render->info.vc1.quantizer = v->quantizer_mode;
271  render->info.vc1.extended_mv = v->extended_mv;
272  render->info.vc1.extended_dmv = v->extended_dmv;
273  render->info.vc1.overlap = v->overlap;
274  render->info.vc1.vstransform = v->vstransform;
275  render->info.vc1.loopfilter = v->s.loop_filter;
276  render->info.vc1.fastuvmc = v->fastuvmc;
277  render->info.vc1.range_mapy_flag = v->range_mapy_flag;
278  render->info.vc1.range_mapy = v->range_mapy;
279  render->info.vc1.range_mapuv_flag = v->range_mapuv_flag;
280  render->info.vc1.range_mapuv = v->range_mapuv;
281  /* Specific to simple/main profile only */
282  render->info.vc1.multires = v->multires;
283  render->info.vc1.syncmarker = v->s.resync_marker;
284  render->info.vc1.rangered = v->rangered | (v->rangeredfrm << 1);
285  render->info.vc1.maxbframes = v->s.max_b_frames;
286 
287  render->info.vc1.deblockEnable = v->postprocflag & 1;
288  render->info.vc1.pquant = v->pq;
289 
290  render->info.vc1.forward_reference = VDP_INVALID_HANDLE;
291  render->info.vc1.backward_reference = VDP_INVALID_HANDLE;
292 
293  if (v->bi_type)
294  render->info.vc1.picture_type = 4;
295  else
296  render->info.vc1.picture_type = s->pict_type - 1 + s->pict_type / 3;
297 
298  switch(s->pict_type){
299  case AV_PICTURE_TYPE_B:
300  next = (struct vdpau_render_state *)s->next_picture.f.data[0];
301  assert(next);
302  render->info.vc1.backward_reference = next->surface;
303  // no break here, going to set forward prediction
304  case AV_PICTURE_TYPE_P:
305  last = (struct vdpau_render_state *)s->last_picture.f.data[0];
306  if (!last) // FIXME: Does this test make sense?
307  last = render; // predict second field from the first
308  render->info.vc1.forward_reference = last->surface;
309  }
310 
311  ff_vdpau_add_data_chunk(s, buf, buf_size);
312 
313  render->info.vc1.slice_count = 1;
314 
315  ff_draw_horiz_band(s, 0, s->avctx->height);
316  render->bitstream_buffers_used = 0;
317 }
318 
320  int buf_size)
321 {
322  struct vdpau_render_state *render, *last, *next;
323  int i;
324 
325  if (!s->current_picture_ptr) return;
326 
327  render = (struct vdpau_render_state *)s->current_picture_ptr->f.data[0];
328  assert(render);
329 
330  /* fill VdpPictureInfoMPEG4Part2 struct */
331  render->info.mpeg4.trd[0] = s->pp_time;
332  render->info.mpeg4.trb[0] = s->pb_time;
333  render->info.mpeg4.trd[1] = s->pp_field_time >> 1;
334  render->info.mpeg4.trb[1] = s->pb_field_time >> 1;
335  render->info.mpeg4.vop_time_increment_resolution = s->avctx->time_base.den;
336  render->info.mpeg4.vop_coding_type = 0;
337  render->info.mpeg4.vop_fcode_forward = s->f_code;
338  render->info.mpeg4.vop_fcode_backward = s->b_code;
339  render->info.mpeg4.resync_marker_disable = !s->resync_marker;
340  render->info.mpeg4.interlaced = !s->progressive_sequence;
341  render->info.mpeg4.quant_type = s->mpeg_quant;
342  render->info.mpeg4.quarter_sample = s->quarter_sample;
343  render->info.mpeg4.short_video_header = s->avctx->codec->id == AV_CODEC_ID_H263;
344  render->info.mpeg4.rounding_control = s->no_rounding;
345  render->info.mpeg4.alternate_vertical_scan_flag = s->alternate_scan;
346  render->info.mpeg4.top_field_first = s->top_field_first;
347  for (i = 0; i < 64; ++i) {
348  render->info.mpeg4.intra_quantizer_matrix[i] = s->intra_matrix[i];
349  render->info.mpeg4.non_intra_quantizer_matrix[i] = s->inter_matrix[i];
350  }
351  render->info.mpeg4.forward_reference = VDP_INVALID_HANDLE;
352  render->info.mpeg4.backward_reference = VDP_INVALID_HANDLE;
353 
354  switch (s->pict_type) {
355  case AV_PICTURE_TYPE_B:
356  next = (struct vdpau_render_state *)s->next_picture.f.data[0];
357  assert(next);
358  render->info.mpeg4.backward_reference = next->surface;
359  render->info.mpeg4.vop_coding_type = 2;
360  // no break here, going to set forward prediction
361  case AV_PICTURE_TYPE_P:
362  last = (struct vdpau_render_state *)s->last_picture.f.data[0];
363  assert(last);
364  render->info.mpeg4.forward_reference = last->surface;
365  }
366 
367  ff_vdpau_add_data_chunk(s, buf, buf_size);
368 
369  ff_draw_horiz_band(s, 0, s->avctx->height);
370  render->bitstream_buffers_used = 0;
371 }
372 
373 // Only dummy functions for now
374 static int vdpau_mpeg2_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
375 {
376  return 0;
377 }
378 
379 static int vdpau_mpeg2_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
380 {
381  return 0;
382 }
383 
385 {
386  return 0;
387 }
388 
390  .name = "mpeg1_vdpau",
391  .type = AVMEDIA_TYPE_VIDEO,
393  .pix_fmt = AV_PIX_FMT_VDPAU_MPEG1,
394  .start_frame = vdpau_mpeg2_start_frame,
395  .end_frame = vdpau_mpeg2_end_frame,
396  .decode_slice = vdpau_mpeg2_decode_slice,
397 };
398 
400  .name = "mpeg2_vdpau",
401  .type = AVMEDIA_TYPE_VIDEO,
403  .pix_fmt = AV_PIX_FMT_VDPAU_MPEG2,
404  .start_frame = vdpau_mpeg2_start_frame,
405  .end_frame = vdpau_mpeg2_end_frame,
406  .decode_slice = vdpau_mpeg2_decode_slice,
407 };
408 
409 /* @}*/