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.

253 lines
7.9KB

  1. /*
  2. * Video Acceleration API (video decoding)
  3. * HW decode acceleration for MPEG-2, MPEG-4, H.264 and VC-1
  4. *
  5. * Copyright (C) 2008-2009 Splitted-Desktop Systems
  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 "libavutil/log.h"
  24. #include "vaapi_internal.h"
  25. /**
  26. * @addtogroup VAAPI_Decoding
  27. *
  28. * @{
  29. */
  30. static void destroy_buffers(VADisplay display, VABufferID *buffers, unsigned int n_buffers)
  31. {
  32. unsigned int i;
  33. for (i = 0; i < n_buffers; i++) {
  34. if (buffers[i] != VA_INVALID_ID) {
  35. vaDestroyBuffer(display, buffers[i]);
  36. buffers[i] = VA_INVALID_ID;
  37. }
  38. }
  39. }
  40. int ff_vaapi_context_init(AVCodecContext *avctx)
  41. {
  42. FFVAContext * const vactx = ff_vaapi_get_context(avctx);
  43. const struct vaapi_context * const user_vactx = avctx->hwaccel_context;
  44. if (!user_vactx) {
  45. av_log(avctx, AV_LOG_ERROR, "Hardware acceleration context (hwaccel_context) does not exist.\n");
  46. return AVERROR(ENOSYS);
  47. }
  48. vactx->display = user_vactx->display;
  49. vactx->config_id = user_vactx->config_id;
  50. vactx->context_id = user_vactx->context_id;
  51. vactx->pic_param_buf_id = VA_INVALID_ID;
  52. vactx->iq_matrix_buf_id = VA_INVALID_ID;
  53. vactx->bitplane_buf_id = VA_INVALID_ID;
  54. return 0;
  55. }
  56. int ff_vaapi_context_fini(AVCodecContext *avctx)
  57. {
  58. return 0;
  59. }
  60. int ff_vaapi_render_picture(FFVAContext *vactx, VASurfaceID surface)
  61. {
  62. VABufferID va_buffers[3];
  63. unsigned int n_va_buffers = 0;
  64. if (vactx->pic_param_buf_id == VA_INVALID_ID)
  65. return 0;
  66. vaUnmapBuffer(vactx->display, vactx->pic_param_buf_id);
  67. va_buffers[n_va_buffers++] = vactx->pic_param_buf_id;
  68. if (vactx->iq_matrix_buf_id != VA_INVALID_ID) {
  69. vaUnmapBuffer(vactx->display, vactx->iq_matrix_buf_id);
  70. va_buffers[n_va_buffers++] = vactx->iq_matrix_buf_id;
  71. }
  72. if (vactx->bitplane_buf_id != VA_INVALID_ID) {
  73. vaUnmapBuffer(vactx->display, vactx->bitplane_buf_id);
  74. va_buffers[n_va_buffers++] = vactx->bitplane_buf_id;
  75. }
  76. if (vaBeginPicture(vactx->display, vactx->context_id,
  77. surface) != VA_STATUS_SUCCESS)
  78. return -1;
  79. if (vaRenderPicture(vactx->display, vactx->context_id,
  80. va_buffers, n_va_buffers) != VA_STATUS_SUCCESS)
  81. return -1;
  82. if (vaRenderPicture(vactx->display, vactx->context_id,
  83. vactx->slice_buf_ids,
  84. vactx->n_slice_buf_ids) != VA_STATUS_SUCCESS)
  85. return -1;
  86. if (vaEndPicture(vactx->display, vactx->context_id) != VA_STATUS_SUCCESS)
  87. return -1;
  88. return 0;
  89. }
  90. int ff_vaapi_commit_slices(FFVAContext *vactx)
  91. {
  92. VABufferID *slice_buf_ids;
  93. VABufferID slice_param_buf_id, slice_data_buf_id;
  94. if (vactx->slice_count == 0)
  95. return 0;
  96. slice_buf_ids =
  97. av_fast_realloc(vactx->slice_buf_ids,
  98. &vactx->slice_buf_ids_alloc,
  99. (vactx->n_slice_buf_ids + 2) * sizeof(slice_buf_ids[0]));
  100. if (!slice_buf_ids)
  101. return -1;
  102. vactx->slice_buf_ids = slice_buf_ids;
  103. slice_param_buf_id = VA_INVALID_ID;
  104. if (vaCreateBuffer(vactx->display, vactx->context_id,
  105. VASliceParameterBufferType,
  106. vactx->slice_param_size,
  107. vactx->slice_count, vactx->slice_params,
  108. &slice_param_buf_id) != VA_STATUS_SUCCESS)
  109. return -1;
  110. vactx->slice_count = 0;
  111. slice_data_buf_id = VA_INVALID_ID;
  112. if (vaCreateBuffer(vactx->display, vactx->context_id,
  113. VASliceDataBufferType,
  114. vactx->slice_data_size,
  115. 1, (void *)vactx->slice_data,
  116. &slice_data_buf_id) != VA_STATUS_SUCCESS)
  117. return -1;
  118. vactx->slice_data = NULL;
  119. vactx->slice_data_size = 0;
  120. slice_buf_ids[vactx->n_slice_buf_ids++] = slice_param_buf_id;
  121. slice_buf_ids[vactx->n_slice_buf_ids++] = slice_data_buf_id;
  122. return 0;
  123. }
  124. static void *alloc_buffer(FFVAContext *vactx, int type, unsigned int size, uint32_t *buf_id)
  125. {
  126. void *data = NULL;
  127. *buf_id = VA_INVALID_ID;
  128. if (vaCreateBuffer(vactx->display, vactx->context_id,
  129. type, size, 1, NULL, buf_id) == VA_STATUS_SUCCESS)
  130. vaMapBuffer(vactx->display, *buf_id, &data);
  131. return data;
  132. }
  133. void *ff_vaapi_alloc_pic_param(FFVAContext *vactx, unsigned int size)
  134. {
  135. return alloc_buffer(vactx, VAPictureParameterBufferType, size, &vactx->pic_param_buf_id);
  136. }
  137. void *ff_vaapi_alloc_iq_matrix(FFVAContext *vactx, unsigned int size)
  138. {
  139. return alloc_buffer(vactx, VAIQMatrixBufferType, size, &vactx->iq_matrix_buf_id);
  140. }
  141. uint8_t *ff_vaapi_alloc_bitplane(FFVAContext *vactx, uint32_t size)
  142. {
  143. return alloc_buffer(vactx, VABitPlaneBufferType, size, &vactx->bitplane_buf_id);
  144. }
  145. VASliceParameterBufferBase *ff_vaapi_alloc_slice(FFVAContext *vactx, const uint8_t *buffer, uint32_t size)
  146. {
  147. uint8_t *slice_params;
  148. VASliceParameterBufferBase *slice_param;
  149. if (!vactx->slice_data)
  150. vactx->slice_data = buffer;
  151. if (vactx->slice_data + vactx->slice_data_size != buffer) {
  152. if (ff_vaapi_commit_slices(vactx) < 0)
  153. return NULL;
  154. vactx->slice_data = buffer;
  155. }
  156. slice_params =
  157. av_fast_realloc(vactx->slice_params,
  158. &vactx->slice_params_alloc,
  159. (vactx->slice_count + 1) * vactx->slice_param_size);
  160. if (!slice_params)
  161. return NULL;
  162. vactx->slice_params = slice_params;
  163. slice_param = (VASliceParameterBufferBase *)(slice_params + vactx->slice_count * vactx->slice_param_size);
  164. slice_param->slice_data_size = size;
  165. slice_param->slice_data_offset = vactx->slice_data_size;
  166. slice_param->slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
  167. vactx->slice_count++;
  168. vactx->slice_data_size += size;
  169. return slice_param;
  170. }
  171. void ff_vaapi_common_end_frame(AVCodecContext *avctx)
  172. {
  173. FFVAContext * const vactx = ff_vaapi_get_context(avctx);
  174. ff_dlog(avctx, "ff_vaapi_common_end_frame()\n");
  175. destroy_buffers(vactx->display, &vactx->pic_param_buf_id, 1);
  176. destroy_buffers(vactx->display, &vactx->iq_matrix_buf_id, 1);
  177. destroy_buffers(vactx->display, &vactx->bitplane_buf_id, 1);
  178. destroy_buffers(vactx->display, vactx->slice_buf_ids, vactx->n_slice_buf_ids);
  179. av_freep(&vactx->slice_buf_ids);
  180. av_freep(&vactx->slice_params);
  181. vactx->n_slice_buf_ids = 0;
  182. vactx->slice_buf_ids_alloc = 0;
  183. vactx->slice_count = 0;
  184. vactx->slice_params_alloc = 0;
  185. }
  186. #if CONFIG_H263_VAAPI_HWACCEL || CONFIG_MPEG1_VAAPI_HWACCEL || \
  187. CONFIG_MPEG2_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL || \
  188. CONFIG_VC1_VAAPI_HWACCEL || CONFIG_WMV3_VAAPI_HWACCEL
  189. int ff_vaapi_mpeg_end_frame(AVCodecContext *avctx)
  190. {
  191. FFVAContext * const vactx = ff_vaapi_get_context(avctx);
  192. MpegEncContext *s = avctx->priv_data;
  193. int ret;
  194. ret = ff_vaapi_commit_slices(vactx);
  195. if (ret < 0)
  196. goto finish;
  197. ret = ff_vaapi_render_picture(vactx,
  198. ff_vaapi_get_surface_id(s->current_picture_ptr->f));
  199. if (ret < 0)
  200. goto finish;
  201. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  202. finish:
  203. ff_vaapi_common_end_frame(avctx);
  204. return ret;
  205. }
  206. #endif
  207. /* @} */