You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

442 lines
18KB

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