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.

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