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.

209 lines
6.6KB

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