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.

249 lines
7.7KB

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