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.

223 lines
7.0KB

  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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "h264dec.h"
  24. #include "mpegvideo.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_render_picture(struct vaapi_context *vactx, VASurfaceID surface)
  42. {
  43. VABufferID va_buffers[3];
  44. unsigned int n_va_buffers = 0;
  45. vaUnmapBuffer(vactx->display, vactx->pic_param_buf_id);
  46. va_buffers[n_va_buffers++] = vactx->pic_param_buf_id;
  47. if (vactx->iq_matrix_buf_id) {
  48. vaUnmapBuffer(vactx->display, vactx->iq_matrix_buf_id);
  49. va_buffers[n_va_buffers++] = vactx->iq_matrix_buf_id;
  50. }
  51. if (vactx->bitplane_buf_id) {
  52. vaUnmapBuffer(vactx->display, vactx->bitplane_buf_id);
  53. va_buffers[n_va_buffers++] = vactx->bitplane_buf_id;
  54. }
  55. if (vaBeginPicture(vactx->display, vactx->context_id,
  56. surface) != VA_STATUS_SUCCESS)
  57. return -1;
  58. if (vaRenderPicture(vactx->display, vactx->context_id,
  59. va_buffers, n_va_buffers) != VA_STATUS_SUCCESS)
  60. return -1;
  61. if (vaRenderPicture(vactx->display, vactx->context_id,
  62. vactx->slice_buf_ids,
  63. vactx->n_slice_buf_ids) != VA_STATUS_SUCCESS)
  64. return -1;
  65. if (vaEndPicture(vactx->display, vactx->context_id) != VA_STATUS_SUCCESS)
  66. return -1;
  67. return 0;
  68. }
  69. int ff_vaapi_commit_slices(struct vaapi_context *vactx)
  70. {
  71. VABufferID *slice_buf_ids;
  72. VABufferID slice_param_buf_id, slice_data_buf_id;
  73. if (vactx->slice_count == 0)
  74. return 0;
  75. slice_buf_ids =
  76. av_fast_realloc(vactx->slice_buf_ids,
  77. &vactx->slice_buf_ids_alloc,
  78. (vactx->n_slice_buf_ids + 2) * sizeof(slice_buf_ids[0]));
  79. if (!slice_buf_ids)
  80. return -1;
  81. vactx->slice_buf_ids = slice_buf_ids;
  82. slice_param_buf_id = 0;
  83. if (vaCreateBuffer(vactx->display, vactx->context_id,
  84. VASliceParameterBufferType,
  85. vactx->slice_param_size,
  86. vactx->slice_count, vactx->slice_params,
  87. &slice_param_buf_id) != VA_STATUS_SUCCESS)
  88. return -1;
  89. vactx->slice_count = 0;
  90. slice_data_buf_id = 0;
  91. if (vaCreateBuffer(vactx->display, vactx->context_id,
  92. VASliceDataBufferType,
  93. vactx->slice_data_size,
  94. 1, (void *)vactx->slice_data,
  95. &slice_data_buf_id) != VA_STATUS_SUCCESS)
  96. return -1;
  97. vactx->slice_data = NULL;
  98. vactx->slice_data_size = 0;
  99. slice_buf_ids[vactx->n_slice_buf_ids++] = slice_param_buf_id;
  100. slice_buf_ids[vactx->n_slice_buf_ids++] = slice_data_buf_id;
  101. return 0;
  102. }
  103. static void *alloc_buffer(struct vaapi_context *vactx, int type, unsigned int size, uint32_t *buf_id)
  104. {
  105. void *data = NULL;
  106. *buf_id = 0;
  107. if (vaCreateBuffer(vactx->display, vactx->context_id,
  108. type, size, 1, NULL, buf_id) == VA_STATUS_SUCCESS)
  109. vaMapBuffer(vactx->display, *buf_id, &data);
  110. return data;
  111. }
  112. void *ff_vaapi_alloc_pic_param(struct vaapi_context *vactx, unsigned int size)
  113. {
  114. return alloc_buffer(vactx, VAPictureParameterBufferType, size, &vactx->pic_param_buf_id);
  115. }
  116. void *ff_vaapi_alloc_iq_matrix(struct vaapi_context *vactx, unsigned int size)
  117. {
  118. return alloc_buffer(vactx, VAIQMatrixBufferType, size, &vactx->iq_matrix_buf_id);
  119. }
  120. uint8_t *ff_vaapi_alloc_bitplane(struct vaapi_context *vactx, uint32_t size)
  121. {
  122. return alloc_buffer(vactx, VABitPlaneBufferType, size, &vactx->bitplane_buf_id);
  123. }
  124. VASliceParameterBufferBase *ff_vaapi_alloc_slice(struct vaapi_context *vactx, const uint8_t *buffer, uint32_t size)
  125. {
  126. uint8_t *slice_params;
  127. VASliceParameterBufferBase *slice_param;
  128. if (!vactx->slice_data)
  129. vactx->slice_data = buffer;
  130. if (vactx->slice_data + vactx->slice_data_size != buffer) {
  131. if (ff_vaapi_commit_slices(vactx) < 0)
  132. return NULL;
  133. vactx->slice_data = buffer;
  134. }
  135. slice_params =
  136. av_fast_realloc(vactx->slice_params,
  137. &vactx->slice_params_alloc,
  138. (vactx->slice_count + 1) * vactx->slice_param_size);
  139. if (!slice_params)
  140. return NULL;
  141. vactx->slice_params = slice_params;
  142. slice_param = (VASliceParameterBufferBase *)(slice_params + vactx->slice_count * vactx->slice_param_size);
  143. slice_param->slice_data_size = size;
  144. slice_param->slice_data_offset = vactx->slice_data_size;
  145. slice_param->slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
  146. vactx->slice_count++;
  147. vactx->slice_data_size += size;
  148. return slice_param;
  149. }
  150. void ff_vaapi_common_end_frame(AVCodecContext *avctx)
  151. {
  152. struct vaapi_context * const vactx = avctx->hwaccel_context;
  153. destroy_buffers(vactx->display, &vactx->pic_param_buf_id, 1);
  154. destroy_buffers(vactx->display, &vactx->iq_matrix_buf_id, 1);
  155. destroy_buffers(vactx->display, &vactx->bitplane_buf_id, 1);
  156. destroy_buffers(vactx->display, vactx->slice_buf_ids, vactx->n_slice_buf_ids);
  157. av_freep(&vactx->slice_buf_ids);
  158. av_freep(&vactx->slice_params);
  159. vactx->n_slice_buf_ids = 0;
  160. vactx->slice_buf_ids_alloc = 0;
  161. vactx->slice_count = 0;
  162. vactx->slice_params_alloc = 0;
  163. }
  164. #if CONFIG_H263_VAAPI_HWACCEL || CONFIG_MPEG1_VAAPI_HWACCEL || \
  165. CONFIG_MPEG2_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL || \
  166. CONFIG_VC1_VAAPI_HWACCEL || CONFIG_WMV3_VAAPI_HWACCEL
  167. int ff_vaapi_mpeg_end_frame(AVCodecContext *avctx)
  168. {
  169. struct vaapi_context * const vactx = avctx->hwaccel_context;
  170. MpegEncContext *s = avctx->priv_data;
  171. int ret;
  172. ret = ff_vaapi_commit_slices(vactx);
  173. if (ret < 0)
  174. goto finish;
  175. ret = ff_vaapi_render_picture(vactx,
  176. ff_vaapi_get_surface_id(s->current_picture_ptr->f));
  177. if (ret < 0)
  178. goto finish;
  179. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  180. finish:
  181. ff_vaapi_common_end_frame(avctx);
  182. return ret;
  183. }
  184. #endif
  185. /* @} */