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.

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