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.

770 lines
29KB

  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 "libavutil/avassert.h"
  25. #include "avcodec.h"
  26. #include "internal.h"
  27. #include "h264.h"
  28. #include "vc1.h"
  29. #undef NDEBUG
  30. #include <assert.h>
  31. #include "vdpau.h"
  32. #include "vdpau_internal.h"
  33. /**
  34. * @addtogroup VDPAU_Decoding
  35. *
  36. * @{
  37. */
  38. static int vdpau_error(VdpStatus status)
  39. {
  40. switch (status) {
  41. case VDP_STATUS_OK:
  42. return 0;
  43. case VDP_STATUS_NO_IMPLEMENTATION:
  44. return AVERROR(ENOSYS);
  45. case VDP_STATUS_DISPLAY_PREEMPTED:
  46. return AVERROR(EIO);
  47. case VDP_STATUS_INVALID_HANDLE:
  48. return AVERROR(EBADF);
  49. case VDP_STATUS_INVALID_POINTER:
  50. return AVERROR(EFAULT);
  51. case VDP_STATUS_RESOURCES:
  52. return AVERROR(ENOBUFS);
  53. case VDP_STATUS_HANDLE_DEVICE_MISMATCH:
  54. return AVERROR(EXDEV);
  55. case VDP_STATUS_ERROR:
  56. return AVERROR(EIO);
  57. default:
  58. return AVERROR(EINVAL);
  59. }
  60. }
  61. AVVDPAUContext *av_alloc_vdpaucontext(void)
  62. {
  63. return av_vdpau_alloc_context();
  64. }
  65. MAKE_ACCESSORS(AVVDPAUContext, vdpau_hwaccel, AVVDPAU_Render2, render2)
  66. int av_vdpau_get_surface_parameters(AVCodecContext *avctx,
  67. VdpChromaType *type,
  68. uint32_t *width, uint32_t *height)
  69. {
  70. VdpChromaType t;
  71. uint32_t w = avctx->coded_width;
  72. uint32_t h = avctx->coded_height;
  73. /* See <vdpau/vdpau.h> for per-type alignment constraints. */
  74. switch (avctx->sw_pix_fmt) {
  75. case AV_PIX_FMT_YUV420P:
  76. case AV_PIX_FMT_YUVJ420P:
  77. t = VDP_CHROMA_TYPE_420;
  78. w = (w + 1) & ~1;
  79. h = (h + 3) & ~3;
  80. break;
  81. case AV_PIX_FMT_YUV422P:
  82. case AV_PIX_FMT_YUVJ422P:
  83. t = VDP_CHROMA_TYPE_422;
  84. w = (w + 1) & ~1;
  85. h = (h + 1) & ~1;
  86. break;
  87. case AV_PIX_FMT_YUV444P:
  88. case AV_PIX_FMT_YUVJ444P:
  89. t = VDP_CHROMA_TYPE_444;
  90. h = (h + 1) & ~1;
  91. break;
  92. default:
  93. return AVERROR(ENOSYS);
  94. }
  95. if (type)
  96. *type = t;
  97. if (width)
  98. *width = w;
  99. if (height)
  100. *height = h;
  101. return 0;
  102. }
  103. int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
  104. int level)
  105. {
  106. VDPAUHWContext *hwctx = avctx->hwaccel_context;
  107. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  108. VdpVideoSurfaceQueryCapabilities *surface_query_caps;
  109. VdpDecoderQueryCapabilities *decoder_query_caps;
  110. VdpDecoderCreate *create;
  111. void *func;
  112. VdpStatus status;
  113. VdpBool supported;
  114. uint32_t max_level, max_mb, max_width, max_height;
  115. VdpChromaType type;
  116. uint32_t width;
  117. uint32_t height;
  118. vdctx->width = UINT32_MAX;
  119. vdctx->height = UINT32_MAX;
  120. if (!hwctx) {
  121. vdctx->device = VDP_INVALID_HANDLE;
  122. av_log(avctx, AV_LOG_WARNING, "hwaccel_context has not been setup by the user application, cannot initialize\n");
  123. return 0;
  124. }
  125. if (hwctx->context.decoder != VDP_INVALID_HANDLE) {
  126. vdctx->decoder = hwctx->context.decoder;
  127. vdctx->render = hwctx->context.render;
  128. vdctx->device = VDP_INVALID_HANDLE;
  129. return 0; /* Decoder created by user */
  130. }
  131. hwctx->reset = 0;
  132. vdctx->device = hwctx->device;
  133. vdctx->get_proc_address = hwctx->get_proc_address;
  134. if (hwctx->flags & AV_HWACCEL_FLAG_IGNORE_LEVEL)
  135. level = 0;
  136. else if (level < 0)
  137. return AVERROR(ENOTSUP);
  138. if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
  139. return AVERROR(ENOSYS);
  140. if (!(hwctx->flags & AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH) &&
  141. type != VDP_CHROMA_TYPE_420)
  142. return AVERROR(ENOSYS);
  143. status = vdctx->get_proc_address(vdctx->device,
  144. VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
  145. &func);
  146. if (status != VDP_STATUS_OK)
  147. return vdpau_error(status);
  148. else
  149. surface_query_caps = func;
  150. status = surface_query_caps(vdctx->device, type, &supported,
  151. &max_width, &max_height);
  152. if (status != VDP_STATUS_OK)
  153. return vdpau_error(status);
  154. if (supported != VDP_TRUE ||
  155. max_width < width || max_height < height)
  156. return AVERROR(ENOTSUP);
  157. status = vdctx->get_proc_address(vdctx->device,
  158. VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
  159. &func);
  160. if (status != VDP_STATUS_OK)
  161. return vdpau_error(status);
  162. else
  163. decoder_query_caps = func;
  164. status = decoder_query_caps(vdctx->device, profile, &supported, &max_level,
  165. &max_mb, &max_width, &max_height);
  166. #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
  167. if (status != VDP_STATUS_OK && profile == VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE) {
  168. /* Run-time backward compatibility for libvdpau 0.8 and earlier */
  169. profile = VDP_DECODER_PROFILE_H264_MAIN;
  170. status = decoder_query_caps(vdctx->device, profile, &supported,
  171. &max_level, &max_mb,
  172. &max_width, &max_height);
  173. }
  174. #endif
  175. if (status != VDP_STATUS_OK)
  176. return vdpau_error(status);
  177. if (supported != VDP_TRUE || max_level < level ||
  178. max_width < width || max_height < height)
  179. return AVERROR(ENOTSUP);
  180. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_CREATE,
  181. &func);
  182. if (status != VDP_STATUS_OK)
  183. return vdpau_error(status);
  184. else
  185. create = func;
  186. status = vdctx->get_proc_address(vdctx->device, VDP_FUNC_ID_DECODER_RENDER,
  187. &func);
  188. if (status != VDP_STATUS_OK)
  189. return vdpau_error(status);
  190. else
  191. vdctx->render = func;
  192. status = create(vdctx->device, profile, width, height, avctx->refs,
  193. &vdctx->decoder);
  194. if (status == VDP_STATUS_OK) {
  195. vdctx->width = avctx->coded_width;
  196. vdctx->height = avctx->coded_height;
  197. }
  198. return vdpau_error(status);
  199. }
  200. int ff_vdpau_common_uninit(AVCodecContext *avctx)
  201. {
  202. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  203. VdpDecoderDestroy *destroy;
  204. void *func;
  205. VdpStatus status;
  206. if (vdctx->device == VDP_INVALID_HANDLE)
  207. return 0; /* Decoder created and destroyed by user */
  208. if (vdctx->width == UINT32_MAX && vdctx->height == UINT32_MAX)
  209. return 0;
  210. status = vdctx->get_proc_address(vdctx->device,
  211. VDP_FUNC_ID_DECODER_DESTROY, &func);
  212. if (status != VDP_STATUS_OK)
  213. return vdpau_error(status);
  214. else
  215. destroy = func;
  216. status = destroy(vdctx->decoder);
  217. return vdpau_error(status);
  218. }
  219. static int ff_vdpau_common_reinit(AVCodecContext *avctx)
  220. {
  221. VDPAUHWContext *hwctx = avctx->hwaccel_context;
  222. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  223. if (vdctx->device == VDP_INVALID_HANDLE)
  224. return 0; /* Decoder created by user */
  225. if (avctx->coded_width == vdctx->width &&
  226. avctx->coded_height == vdctx->height && !hwctx->reset)
  227. return 0;
  228. avctx->hwaccel->uninit(avctx);
  229. return avctx->hwaccel->init(avctx);
  230. }
  231. int ff_vdpau_common_start_frame(struct vdpau_picture_context *pic_ctx,
  232. av_unused const uint8_t *buffer,
  233. av_unused uint32_t size)
  234. {
  235. pic_ctx->bitstream_buffers_allocated = 0;
  236. pic_ctx->bitstream_buffers_used = 0;
  237. pic_ctx->bitstream_buffers = NULL;
  238. return 0;
  239. }
  240. int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
  241. struct vdpau_picture_context *pic_ctx)
  242. {
  243. VDPAUContext *vdctx = avctx->internal->hwaccel_priv_data;
  244. AVVDPAUContext *hwctx = avctx->hwaccel_context;
  245. VdpVideoSurface surf = ff_vdpau_get_surface_id(frame);
  246. VdpStatus status;
  247. int val;
  248. val = ff_vdpau_common_reinit(avctx);
  249. if (val < 0)
  250. return val;
  251. #if FF_API_BUFS_VDPAU
  252. FF_DISABLE_DEPRECATION_WARNINGS
  253. av_assert0(sizeof(hwctx->info) <= sizeof(pic_ctx->info));
  254. memcpy(&hwctx->info, &pic_ctx->info, sizeof(hwctx->info));
  255. hwctx->bitstream_buffers = pic_ctx->bitstream_buffers;
  256. hwctx->bitstream_buffers_used = pic_ctx->bitstream_buffers_used;
  257. hwctx->bitstream_buffers_allocated = pic_ctx->bitstream_buffers_allocated;
  258. FF_ENABLE_DEPRECATION_WARNINGS
  259. #endif
  260. if (!hwctx->render && hwctx->render2) {
  261. status = hwctx->render2(avctx, frame, (void *)&pic_ctx->info,
  262. pic_ctx->bitstream_buffers_used, pic_ctx->bitstream_buffers);
  263. } else
  264. status = vdctx->render(vdctx->decoder, surf, (void *)&pic_ctx->info,
  265. pic_ctx->bitstream_buffers_used,
  266. pic_ctx->bitstream_buffers);
  267. av_freep(&pic_ctx->bitstream_buffers);
  268. #if FF_API_BUFS_VDPAU
  269. FF_DISABLE_DEPRECATION_WARNINGS
  270. hwctx->bitstream_buffers = NULL;
  271. hwctx->bitstream_buffers_used = 0;
  272. hwctx->bitstream_buffers_allocated = 0;
  273. FF_ENABLE_DEPRECATION_WARNINGS
  274. #endif
  275. return vdpau_error(status);
  276. }
  277. #if CONFIG_H263_VDPAU_HWACCEL || CONFIG_MPEG1_VDPAU_HWACCEL || \
  278. CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
  279. CONFIG_VC1_VDPAU_HWACCEL || CONFIG_WMV3_VDPAU_HWACCEL
  280. int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
  281. {
  282. MpegEncContext *s = avctx->priv_data;
  283. Picture *pic = s->current_picture_ptr;
  284. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  285. int val;
  286. val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
  287. if (val < 0)
  288. return val;
  289. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  290. return 0;
  291. }
  292. #endif
  293. int ff_vdpau_add_buffer(struct vdpau_picture_context *pic_ctx,
  294. const uint8_t *buf, uint32_t size)
  295. {
  296. VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
  297. buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
  298. (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
  299. if (!buffers)
  300. return AVERROR(ENOMEM);
  301. pic_ctx->bitstream_buffers = buffers;
  302. buffers += pic_ctx->bitstream_buffers_used++;
  303. buffers->struct_version = VDP_BITSTREAM_BUFFER_VERSION;
  304. buffers->bitstream = buf;
  305. buffers->bitstream_bytes = size;
  306. return 0;
  307. }
  308. /* Obsolete non-hwaccel VDPAU support below... */
  309. void ff_vdpau_h264_set_reference_frames(H264Context *h)
  310. {
  311. struct vdpau_render_state *render, *render_ref;
  312. VdpReferenceFrameH264 *rf, *rf2;
  313. H264Picture *pic;
  314. int i, list, pic_frame_idx;
  315. render = (struct vdpau_render_state *)h->cur_pic_ptr->f.data[0];
  316. assert(render);
  317. rf = &render->info.h264.referenceFrames[0];
  318. #define H264_RF_COUNT FF_ARRAY_ELEMS(render->info.h264.referenceFrames)
  319. for (list = 0; list < 2; ++list) {
  320. H264Picture **lp = list ? h->long_ref : h->short_ref;
  321. int ls = list ? 16 : h->short_ref_count;
  322. for (i = 0; i < ls; ++i) {
  323. pic = lp[i];
  324. if (!pic || !pic->reference)
  325. continue;
  326. pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
  327. render_ref = (struct vdpau_render_state *)pic->f.data[0];
  328. assert(render_ref);
  329. rf2 = &render->info.h264.referenceFrames[0];
  330. while (rf2 != rf) {
  331. if (
  332. (rf2->surface == render_ref->surface)
  333. && (rf2->is_long_term == pic->long_ref)
  334. && (rf2->frame_idx == pic_frame_idx)
  335. )
  336. break;
  337. ++rf2;
  338. }
  339. if (rf2 != rf) {
  340. rf2->top_is_reference |= (pic->reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
  341. rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
  342. continue;
  343. }
  344. if (rf >= &render->info.h264.referenceFrames[H264_RF_COUNT])
  345. continue;
  346. rf->surface = render_ref->surface;
  347. rf->is_long_term = pic->long_ref;
  348. rf->top_is_reference = (pic->reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
  349. rf->bottom_is_reference = (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
  350. rf->field_order_cnt[0] = pic->field_poc[0];
  351. rf->field_order_cnt[1] = pic->field_poc[1];
  352. rf->frame_idx = pic_frame_idx;
  353. ++rf;
  354. }
  355. }
  356. for (; rf < &render->info.h264.referenceFrames[H264_RF_COUNT]; ++rf) {
  357. rf->surface = VDP_INVALID_HANDLE;
  358. rf->is_long_term = 0;
  359. rf->top_is_reference = 0;
  360. rf->bottom_is_reference = 0;
  361. rf->field_order_cnt[0] = 0;
  362. rf->field_order_cnt[1] = 0;
  363. rf->frame_idx = 0;
  364. }
  365. }
  366. void ff_vdpau_add_data_chunk(uint8_t *data, const uint8_t *buf, int buf_size)
  367. {
  368. struct vdpau_render_state *render = (struct vdpau_render_state*)data;
  369. assert(render);
  370. render->bitstream_buffers= av_fast_realloc(
  371. render->bitstream_buffers,
  372. &render->bitstream_buffers_allocated,
  373. sizeof(*render->bitstream_buffers)*(render->bitstream_buffers_used + 1)
  374. );
  375. render->bitstream_buffers[render->bitstream_buffers_used].struct_version = VDP_BITSTREAM_BUFFER_VERSION;
  376. render->bitstream_buffers[render->bitstream_buffers_used].bitstream = buf;
  377. render->bitstream_buffers[render->bitstream_buffers_used].bitstream_bytes = buf_size;
  378. render->bitstream_buffers_used++;
  379. }
  380. #if CONFIG_H264_VDPAU_DECODER
  381. void ff_vdpau_h264_picture_start(H264Context *h)
  382. {
  383. struct vdpau_render_state *render;
  384. int i;
  385. render = (struct vdpau_render_state *)h->cur_pic_ptr->f.data[0];
  386. assert(render);
  387. for (i = 0; i < 2; ++i) {
  388. int foc = h->cur_pic_ptr->field_poc[i];
  389. if (foc == INT_MAX)
  390. foc = 0;
  391. render->info.h264.field_order_cnt[i] = foc;
  392. }
  393. render->info.h264.frame_num = h->frame_num;
  394. }
  395. void ff_vdpau_h264_picture_complete(H264Context *h)
  396. {
  397. struct vdpau_render_state *render;
  398. render = (struct vdpau_render_state *)h->cur_pic_ptr->f.data[0];
  399. assert(render);
  400. render->info.h264.slice_count = h->current_slice;
  401. if (render->info.h264.slice_count < 1)
  402. return;
  403. render->info.h264.is_reference = (h->cur_pic_ptr->reference & 3) ? VDP_TRUE : VDP_FALSE;
  404. render->info.h264.field_pic_flag = h->picture_structure != PICT_FRAME;
  405. render->info.h264.bottom_field_flag = h->picture_structure == PICT_BOTTOM_FIELD;
  406. render->info.h264.num_ref_frames = h->sps.ref_frame_count;
  407. render->info.h264.mb_adaptive_frame_field_flag = h->sps.mb_aff && !render->info.h264.field_pic_flag;
  408. render->info.h264.constrained_intra_pred_flag = h->pps.constrained_intra_pred;
  409. render->info.h264.weighted_pred_flag = h->pps.weighted_pred;
  410. render->info.h264.weighted_bipred_idc = h->pps.weighted_bipred_idc;
  411. render->info.h264.frame_mbs_only_flag = h->sps.frame_mbs_only_flag;
  412. render->info.h264.transform_8x8_mode_flag = h->pps.transform_8x8_mode;
  413. render->info.h264.chroma_qp_index_offset = h->pps.chroma_qp_index_offset[0];
  414. render->info.h264.second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
  415. render->info.h264.pic_init_qp_minus26 = h->pps.init_qp - 26;
  416. render->info.h264.num_ref_idx_l0_active_minus1 = h->pps.ref_count[0] - 1;
  417. render->info.h264.num_ref_idx_l1_active_minus1 = h->pps.ref_count[1] - 1;
  418. render->info.h264.log2_max_frame_num_minus4 = h->sps.log2_max_frame_num - 4;
  419. render->info.h264.pic_order_cnt_type = h->sps.poc_type;
  420. render->info.h264.log2_max_pic_order_cnt_lsb_minus4 = h->sps.poc_type ? 0 : h->sps.log2_max_poc_lsb - 4;
  421. render->info.h264.delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
  422. render->info.h264.direct_8x8_inference_flag = h->sps.direct_8x8_inference_flag;
  423. render->info.h264.entropy_coding_mode_flag = h->pps.cabac;
  424. render->info.h264.pic_order_present_flag = h->pps.pic_order_present;
  425. render->info.h264.deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
  426. render->info.h264.redundant_pic_cnt_present_flag = h->pps.redundant_pic_cnt_present;
  427. memcpy(render->info.h264.scaling_lists_4x4, h->pps.scaling_matrix4, sizeof(render->info.h264.scaling_lists_4x4));
  428. memcpy(render->info.h264.scaling_lists_8x8[0], h->pps.scaling_matrix8[0], sizeof(render->info.h264.scaling_lists_8x8[0]));
  429. memcpy(render->info.h264.scaling_lists_8x8[1], h->pps.scaling_matrix8[3], sizeof(render->info.h264.scaling_lists_8x8[0]));
  430. ff_h264_draw_horiz_band(h, 0, h->avctx->height);
  431. render->bitstream_buffers_used = 0;
  432. }
  433. #endif /* CONFIG_H264_VDPAU_DECODER */
  434. #if CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER
  435. void ff_vdpau_mpeg_picture_complete(MpegEncContext *s, const uint8_t *buf,
  436. int buf_size, int slice_count)
  437. {
  438. struct vdpau_render_state *render, *last, *next;
  439. int i;
  440. if (!s->current_picture_ptr) return;
  441. render = (struct vdpau_render_state *)s->current_picture_ptr->f->data[0];
  442. assert(render);
  443. /* fill VdpPictureInfoMPEG1Or2 struct */
  444. render->info.mpeg.picture_structure = s->picture_structure;
  445. render->info.mpeg.picture_coding_type = s->pict_type;
  446. render->info.mpeg.intra_dc_precision = s->intra_dc_precision;
  447. render->info.mpeg.frame_pred_frame_dct = s->frame_pred_frame_dct;
  448. render->info.mpeg.concealment_motion_vectors = s->concealment_motion_vectors;
  449. render->info.mpeg.intra_vlc_format = s->intra_vlc_format;
  450. render->info.mpeg.alternate_scan = s->alternate_scan;
  451. render->info.mpeg.q_scale_type = s->q_scale_type;
  452. render->info.mpeg.top_field_first = s->top_field_first;
  453. render->info.mpeg.full_pel_forward_vector = s->full_pel[0]; // MPEG-1 only. Set 0 for MPEG-2
  454. render->info.mpeg.full_pel_backward_vector = s->full_pel[1]; // MPEG-1 only. Set 0 for MPEG-2
  455. render->info.mpeg.f_code[0][0] = s->mpeg_f_code[0][0]; // For MPEG-1 fill both horiz. & vert.
  456. render->info.mpeg.f_code[0][1] = s->mpeg_f_code[0][1];
  457. render->info.mpeg.f_code[1][0] = s->mpeg_f_code[1][0];
  458. render->info.mpeg.f_code[1][1] = s->mpeg_f_code[1][1];
  459. for (i = 0; i < 64; ++i) {
  460. render->info.mpeg.intra_quantizer_matrix[i] = s->intra_matrix[i];
  461. render->info.mpeg.non_intra_quantizer_matrix[i] = s->inter_matrix[i];
  462. }
  463. render->info.mpeg.forward_reference = VDP_INVALID_HANDLE;
  464. render->info.mpeg.backward_reference = VDP_INVALID_HANDLE;
  465. switch(s->pict_type){
  466. case AV_PICTURE_TYPE_B:
  467. next = (struct vdpau_render_state *)s->next_picture.f->data[0];
  468. assert(next);
  469. render->info.mpeg.backward_reference = next->surface;
  470. // no return here, going to set forward prediction
  471. case AV_PICTURE_TYPE_P:
  472. last = (struct vdpau_render_state *)s->last_picture.f->data[0];
  473. if (!last) // FIXME: Does this test make sense?
  474. last = render; // predict second field from the first
  475. render->info.mpeg.forward_reference = last->surface;
  476. }
  477. ff_vdpau_add_data_chunk(s->current_picture_ptr->f->data[0], buf, buf_size);
  478. render->info.mpeg.slice_count = slice_count;
  479. if (slice_count)
  480. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  481. render->bitstream_buffers_used = 0;
  482. }
  483. #endif /* CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER */
  484. #if CONFIG_VC1_VDPAU_DECODER
  485. void ff_vdpau_vc1_decode_picture(MpegEncContext *s, const uint8_t *buf,
  486. int buf_size)
  487. {
  488. VC1Context *v = s->avctx->priv_data;
  489. struct vdpau_render_state *render, *last, *next;
  490. render = (struct vdpau_render_state *)s->current_picture.f->data[0];
  491. assert(render);
  492. /* fill LvPictureInfoVC1 struct */
  493. render->info.vc1.frame_coding_mode = v->fcm ? v->fcm + 1 : 0;
  494. render->info.vc1.postprocflag = v->postprocflag;
  495. render->info.vc1.pulldown = v->broadcast;
  496. render->info.vc1.interlace = v->interlace;
  497. render->info.vc1.tfcntrflag = v->tfcntrflag;
  498. render->info.vc1.finterpflag = v->finterpflag;
  499. render->info.vc1.psf = v->psf;
  500. render->info.vc1.dquant = v->dquant;
  501. render->info.vc1.panscan_flag = v->panscanflag;
  502. render->info.vc1.refdist_flag = v->refdist_flag;
  503. render->info.vc1.quantizer = v->quantizer_mode;
  504. render->info.vc1.extended_mv = v->extended_mv;
  505. render->info.vc1.extended_dmv = v->extended_dmv;
  506. render->info.vc1.overlap = v->overlap;
  507. render->info.vc1.vstransform = v->vstransform;
  508. render->info.vc1.loopfilter = v->s.loop_filter;
  509. render->info.vc1.fastuvmc = v->fastuvmc;
  510. render->info.vc1.range_mapy_flag = v->range_mapy_flag;
  511. render->info.vc1.range_mapy = v->range_mapy;
  512. render->info.vc1.range_mapuv_flag = v->range_mapuv_flag;
  513. render->info.vc1.range_mapuv = v->range_mapuv;
  514. /* Specific to simple/main profile only */
  515. render->info.vc1.multires = v->multires;
  516. render->info.vc1.syncmarker = v->resync_marker;
  517. render->info.vc1.rangered = v->rangered | (v->rangeredfrm << 1);
  518. render->info.vc1.maxbframes = v->s.max_b_frames;
  519. render->info.vc1.deblockEnable = v->postprocflag & 1;
  520. render->info.vc1.pquant = v->pq;
  521. render->info.vc1.forward_reference = VDP_INVALID_HANDLE;
  522. render->info.vc1.backward_reference = VDP_INVALID_HANDLE;
  523. if (v->bi_type)
  524. render->info.vc1.picture_type = 4;
  525. else
  526. render->info.vc1.picture_type = s->pict_type - 1 + s->pict_type / 3;
  527. switch(s->pict_type){
  528. case AV_PICTURE_TYPE_B:
  529. next = (struct vdpau_render_state *)s->next_picture.f->data[0];
  530. assert(next);
  531. render->info.vc1.backward_reference = next->surface;
  532. // no break here, going to set forward prediction
  533. case AV_PICTURE_TYPE_P:
  534. last = (struct vdpau_render_state *)s->last_picture.f->data[0];
  535. if (!last) // FIXME: Does this test make sense?
  536. last = render; // predict second field from the first
  537. render->info.vc1.forward_reference = last->surface;
  538. }
  539. ff_vdpau_add_data_chunk(s->current_picture_ptr->f->data[0], buf, buf_size);
  540. render->info.vc1.slice_count = 1;
  541. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  542. render->bitstream_buffers_used = 0;
  543. }
  544. #endif /* (CONFIG_VC1_VDPAU_DECODER */
  545. #if CONFIG_MPEG4_VDPAU_DECODER
  546. void ff_vdpau_mpeg4_decode_picture(Mpeg4DecContext *ctx, const uint8_t *buf,
  547. int buf_size)
  548. {
  549. MpegEncContext *s = &ctx->m;
  550. struct vdpau_render_state *render, *last, *next;
  551. int i;
  552. if (!s->current_picture_ptr) return;
  553. render = (struct vdpau_render_state *)s->current_picture_ptr->f->data[0];
  554. assert(render);
  555. /* fill VdpPictureInfoMPEG4Part2 struct */
  556. render->info.mpeg4.trd[0] = s->pp_time;
  557. render->info.mpeg4.trb[0] = s->pb_time;
  558. render->info.mpeg4.trd[1] = s->pp_field_time >> 1;
  559. render->info.mpeg4.trb[1] = s->pb_field_time >> 1;
  560. render->info.mpeg4.vop_time_increment_resolution = s->avctx->time_base.den;
  561. render->info.mpeg4.vop_coding_type = 0;
  562. render->info.mpeg4.vop_fcode_forward = s->f_code;
  563. render->info.mpeg4.vop_fcode_backward = s->b_code;
  564. render->info.mpeg4.resync_marker_disable = !ctx->resync_marker;
  565. render->info.mpeg4.interlaced = !s->progressive_sequence;
  566. render->info.mpeg4.quant_type = s->mpeg_quant;
  567. render->info.mpeg4.quarter_sample = s->quarter_sample;
  568. render->info.mpeg4.short_video_header = s->avctx->codec->id == AV_CODEC_ID_H263;
  569. render->info.mpeg4.rounding_control = s->no_rounding;
  570. render->info.mpeg4.alternate_vertical_scan_flag = s->alternate_scan;
  571. render->info.mpeg4.top_field_first = s->top_field_first;
  572. for (i = 0; i < 64; ++i) {
  573. render->info.mpeg4.intra_quantizer_matrix[i] = s->intra_matrix[i];
  574. render->info.mpeg4.non_intra_quantizer_matrix[i] = s->inter_matrix[i];
  575. }
  576. render->info.mpeg4.forward_reference = VDP_INVALID_HANDLE;
  577. render->info.mpeg4.backward_reference = VDP_INVALID_HANDLE;
  578. switch (s->pict_type) {
  579. case AV_PICTURE_TYPE_B:
  580. next = (struct vdpau_render_state *)s->next_picture.f->data[0];
  581. assert(next);
  582. render->info.mpeg4.backward_reference = next->surface;
  583. render->info.mpeg4.vop_coding_type = 2;
  584. // no break here, going to set forward prediction
  585. case AV_PICTURE_TYPE_P:
  586. last = (struct vdpau_render_state *)s->last_picture.f->data[0];
  587. assert(last);
  588. render->info.mpeg4.forward_reference = last->surface;
  589. }
  590. ff_vdpau_add_data_chunk(s->current_picture_ptr->f->data[0], buf, buf_size);
  591. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  592. render->bitstream_buffers_used = 0;
  593. }
  594. #endif /* CONFIG_MPEG4_VDPAU_DECODER */
  595. int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
  596. {
  597. #define PROFILE(prof) \
  598. do { \
  599. *profile = VDP_DECODER_PROFILE_##prof; \
  600. return 0; \
  601. } while (0)
  602. switch (avctx->codec_id) {
  603. case AV_CODEC_ID_MPEG1VIDEO: PROFILE(MPEG1);
  604. case AV_CODEC_ID_MPEG2VIDEO:
  605. switch (avctx->profile) {
  606. case FF_PROFILE_MPEG2_MAIN: PROFILE(MPEG2_MAIN);
  607. case FF_PROFILE_MPEG2_SIMPLE: PROFILE(MPEG2_SIMPLE);
  608. default: return AVERROR(EINVAL);
  609. }
  610. case AV_CODEC_ID_H263: PROFILE(MPEG4_PART2_ASP);
  611. case AV_CODEC_ID_MPEG4:
  612. switch (avctx->profile) {
  613. case FF_PROFILE_MPEG4_SIMPLE: PROFILE(MPEG4_PART2_SP);
  614. case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: PROFILE(MPEG4_PART2_ASP);
  615. default: return AVERROR(EINVAL);
  616. }
  617. case AV_CODEC_ID_H264:
  618. switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
  619. case FF_PROFILE_H264_BASELINE: PROFILE(H264_BASELINE);
  620. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  621. case FF_PROFILE_H264_MAIN: PROFILE(H264_MAIN);
  622. case FF_PROFILE_H264_HIGH: PROFILE(H264_HIGH);
  623. #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
  624. case FF_PROFILE_H264_EXTENDED: PROFILE(H264_EXTENDED);
  625. #endif
  626. default: return AVERROR(EINVAL);
  627. }
  628. case AV_CODEC_ID_WMV3:
  629. case AV_CODEC_ID_VC1:
  630. switch (avctx->profile) {
  631. case FF_PROFILE_VC1_SIMPLE: PROFILE(VC1_SIMPLE);
  632. case FF_PROFILE_VC1_MAIN: PROFILE(VC1_MAIN);
  633. case FF_PROFILE_VC1_ADVANCED: PROFILE(VC1_ADVANCED);
  634. default: return AVERROR(EINVAL);
  635. }
  636. }
  637. return AVERROR(EINVAL);
  638. #undef PROFILE
  639. }
  640. AVVDPAUContext *av_vdpau_alloc_context(void)
  641. {
  642. return av_mallocz(sizeof(AVVDPAUContext));
  643. }
  644. int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device,
  645. VdpGetProcAddress *get_proc, unsigned flags)
  646. {
  647. VDPAUHWContext *hwctx;
  648. if (flags & ~(AV_HWACCEL_FLAG_IGNORE_LEVEL|AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH))
  649. return AVERROR(EINVAL);
  650. if (av_reallocp(&avctx->hwaccel_context, sizeof(*hwctx)))
  651. return AVERROR(ENOMEM);
  652. hwctx = avctx->hwaccel_context;
  653. memset(hwctx, 0, sizeof(*hwctx));
  654. hwctx->context.decoder = VDP_INVALID_HANDLE;
  655. hwctx->device = device;
  656. hwctx->get_proc_address = get_proc;
  657. hwctx->flags = flags;
  658. hwctx->reset = 1;
  659. return 0;
  660. }
  661. /* @}*/