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.

220 lines
6.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 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 "h264.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]) {
  35. vaDestroyBuffer(display, buffers[i]);
  36. buffers[i] = 0;
  37. }
  38. }
  39. }
  40. int ff_vaapi_render_picture(struct vaapi_context *vactx, VASurfaceID surface)
  41. {
  42. VABufferID va_buffers[3];
  43. unsigned int n_va_buffers = 0;
  44. vaUnmapBuffer(vactx->display, vactx->pic_param_buf_id);
  45. va_buffers[n_va_buffers++] = vactx->pic_param_buf_id;
  46. if (vactx->iq_matrix_buf_id) {
  47. vaUnmapBuffer(vactx->display, vactx->iq_matrix_buf_id);
  48. va_buffers[n_va_buffers++] = vactx->iq_matrix_buf_id;
  49. }
  50. if (vactx->bitplane_buf_id) {
  51. vaUnmapBuffer(vactx->display, vactx->bitplane_buf_id);
  52. va_buffers[n_va_buffers++] = vactx->bitplane_buf_id;
  53. }
  54. if (vaBeginPicture(vactx->display, vactx->context_id,
  55. surface) != VA_STATUS_SUCCESS)
  56. return -1;
  57. if (vaRenderPicture(vactx->display, vactx->context_id,
  58. va_buffers, n_va_buffers) != VA_STATUS_SUCCESS)
  59. return -1;
  60. if (vaRenderPicture(vactx->display, vactx->context_id,
  61. vactx->slice_buf_ids,
  62. vactx->n_slice_buf_ids) != VA_STATUS_SUCCESS)
  63. return -1;
  64. if (vaEndPicture(vactx->display, vactx->context_id) != VA_STATUS_SUCCESS)
  65. return -1;
  66. return 0;
  67. }
  68. int ff_vaapi_commit_slices(struct vaapi_context *vactx)
  69. {
  70. VABufferID *slice_buf_ids;
  71. VABufferID slice_param_buf_id, slice_data_buf_id;
  72. if (vactx->slice_count == 0)
  73. return 0;
  74. slice_buf_ids =
  75. av_fast_realloc(vactx->slice_buf_ids,
  76. &vactx->slice_buf_ids_alloc,
  77. (vactx->n_slice_buf_ids + 2) * sizeof(slice_buf_ids[0]));
  78. if (!slice_buf_ids)
  79. return -1;
  80. vactx->slice_buf_ids = slice_buf_ids;
  81. slice_param_buf_id = 0;
  82. if (vaCreateBuffer(vactx->display, vactx->context_id,
  83. VASliceParameterBufferType,
  84. vactx->slice_param_size,
  85. vactx->slice_count, vactx->slice_params,
  86. &slice_param_buf_id) != VA_STATUS_SUCCESS)
  87. return -1;
  88. vactx->slice_count = 0;
  89. slice_data_buf_id = 0;
  90. if (vaCreateBuffer(vactx->display, vactx->context_id,
  91. VASliceDataBufferType,
  92. vactx->slice_data_size,
  93. 1, (void *)vactx->slice_data,
  94. &slice_data_buf_id) != VA_STATUS_SUCCESS)
  95. return -1;
  96. vactx->slice_data = NULL;
  97. vactx->slice_data_size = 0;
  98. slice_buf_ids[vactx->n_slice_buf_ids++] = slice_param_buf_id;
  99. slice_buf_ids[vactx->n_slice_buf_ids++] = slice_data_buf_id;
  100. return 0;
  101. }
  102. static void *alloc_buffer(struct vaapi_context *vactx, int type, unsigned int size, uint32_t *buf_id)
  103. {
  104. void *data = NULL;
  105. *buf_id = 0;
  106. if (vaCreateBuffer(vactx->display, vactx->context_id,
  107. type, size, 1, NULL, buf_id) == VA_STATUS_SUCCESS)
  108. vaMapBuffer(vactx->display, *buf_id, &data);
  109. return data;
  110. }
  111. void *ff_vaapi_alloc_pic_param(struct vaapi_context *vactx, unsigned int size)
  112. {
  113. return alloc_buffer(vactx, VAPictureParameterBufferType, size, &vactx->pic_param_buf_id);
  114. }
  115. void *ff_vaapi_alloc_iq_matrix(struct vaapi_context *vactx, unsigned int size)
  116. {
  117. return alloc_buffer(vactx, VAIQMatrixBufferType, size, &vactx->iq_matrix_buf_id);
  118. }
  119. uint8_t *ff_vaapi_alloc_bitplane(struct vaapi_context *vactx, uint32_t size)
  120. {
  121. return alloc_buffer(vactx, VABitPlaneBufferType, size, &vactx->bitplane_buf_id);
  122. }
  123. VASliceParameterBufferBase *ff_vaapi_alloc_slice(struct vaapi_context *vactx, const uint8_t *buffer, uint32_t size)
  124. {
  125. uint8_t *slice_params;
  126. VASliceParameterBufferBase *slice_param;
  127. if (!vactx->slice_data)
  128. vactx->slice_data = buffer;
  129. if (vactx->slice_data + vactx->slice_data_size != buffer) {
  130. if (ff_vaapi_commit_slices(vactx) < 0)
  131. return NULL;
  132. vactx->slice_data = buffer;
  133. }
  134. slice_params =
  135. av_fast_realloc(vactx->slice_params,
  136. &vactx->slice_params_alloc,
  137. (vactx->slice_count + 1) * vactx->slice_param_size);
  138. if (!slice_params)
  139. return NULL;
  140. vactx->slice_params = slice_params;
  141. slice_param = (VASliceParameterBufferBase *)(slice_params + vactx->slice_count * vactx->slice_param_size);
  142. slice_param->slice_data_size = size;
  143. slice_param->slice_data_offset = vactx->slice_data_size;
  144. slice_param->slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
  145. vactx->slice_count++;
  146. vactx->slice_data_size += size;
  147. return slice_param;
  148. }
  149. void ff_vaapi_common_end_frame(AVCodecContext *avctx)
  150. {
  151. struct vaapi_context * const vactx = avctx->hwaccel_context;
  152. av_dlog(avctx, "ff_vaapi_common_end_frame()\n");
  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. int ff_vaapi_mpeg_end_frame(AVCodecContext *avctx)
  165. {
  166. struct vaapi_context * const vactx = avctx->hwaccel_context;
  167. MpegEncContext *s = avctx->priv_data;
  168. int ret;
  169. ret = ff_vaapi_commit_slices(vactx);
  170. if (ret < 0)
  171. goto finish;
  172. ret = ff_vaapi_render_picture(vactx,
  173. ff_vaapi_get_surface_id(s->current_picture_ptr->f));
  174. if (ret < 0)
  175. goto finish;
  176. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  177. finish:
  178. ff_vaapi_common_end_frame(avctx);
  179. return ret;
  180. }
  181. /* @} */