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.

198 lines
6.5KB

  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. if (vaCreateBuffer(vactx->display, vactx->context_id,
  44. VAPictureParameterBufferType,
  45. vactx->pic_param_size,
  46. 1, &vactx->pic_param,
  47. &vactx->pic_param_buf_id) != VA_STATUS_SUCCESS)
  48. return -1;
  49. va_buffers[n_va_buffers++] = vactx->pic_param_buf_id;
  50. if (vactx->iq_matrix_present) {
  51. if (vaCreateBuffer(vactx->display, vactx->context_id,
  52. VAIQMatrixBufferType,
  53. vactx->iq_matrix_size,
  54. 1, &vactx->iq_matrix,
  55. &vactx->iq_matrix_buf_id) != VA_STATUS_SUCCESS)
  56. return -1;
  57. va_buffers[n_va_buffers++] = vactx->iq_matrix_buf_id;
  58. }
  59. if (vactx->bitplane_buffer) {
  60. if (vaCreateBuffer(vactx->display, vactx->context_id,
  61. VABitPlaneBufferType,
  62. vactx->bitplane_buffer_size,
  63. 1, vactx->bitplane_buffer,
  64. &vactx->bitplane_buf_id) != VA_STATUS_SUCCESS)
  65. return -1;
  66. va_buffers[n_va_buffers++] = vactx->bitplane_buf_id;
  67. }
  68. if (vaBeginPicture(vactx->display, vactx->context_id,
  69. surface) != VA_STATUS_SUCCESS)
  70. return -1;
  71. if (vaRenderPicture(vactx->display, vactx->context_id,
  72. va_buffers, n_va_buffers) != VA_STATUS_SUCCESS)
  73. return -1;
  74. if (vaRenderPicture(vactx->display, vactx->context_id,
  75. vactx->slice_buf_ids,
  76. vactx->n_slice_buf_ids) != VA_STATUS_SUCCESS)
  77. return -1;
  78. if (vaEndPicture(vactx->display, vactx->context_id) != VA_STATUS_SUCCESS)
  79. return -1;
  80. return 0;
  81. }
  82. static int commit_slices(struct vaapi_context *vactx)
  83. {
  84. VABufferID *slice_buf_ids;
  85. VABufferID slice_param_buf_id, slice_data_buf_id;
  86. if (vactx->slice_count == 0)
  87. return 0;
  88. slice_buf_ids =
  89. av_fast_realloc(vactx->slice_buf_ids,
  90. &vactx->slice_buf_ids_alloc,
  91. (vactx->n_slice_buf_ids + 2) * sizeof(slice_buf_ids[0]));
  92. if (!slice_buf_ids)
  93. return -1;
  94. vactx->slice_buf_ids = slice_buf_ids;
  95. slice_param_buf_id = 0;
  96. if (vaCreateBuffer(vactx->display, vactx->context_id,
  97. VASliceParameterBufferType,
  98. vactx->slice_param_size,
  99. vactx->slice_count, vactx->slice_params,
  100. &slice_param_buf_id) != VA_STATUS_SUCCESS)
  101. return -1;
  102. vactx->slice_count = 0;
  103. slice_data_buf_id = 0;
  104. if (vaCreateBuffer(vactx->display, vactx->context_id,
  105. VASliceDataBufferType,
  106. vactx->slice_data_size,
  107. 1, (void *)vactx->slice_data,
  108. &slice_data_buf_id) != VA_STATUS_SUCCESS)
  109. return -1;
  110. vactx->slice_data = NULL;
  111. vactx->slice_data_size = 0;
  112. slice_buf_ids[vactx->n_slice_buf_ids++] = slice_param_buf_id;
  113. slice_buf_ids[vactx->n_slice_buf_ids++] = slice_data_buf_id;
  114. return 0;
  115. }
  116. VASliceParameterBufferBase *ff_vaapi_alloc_slice(struct vaapi_context *vactx, const uint8_t *buffer, uint32_t size)
  117. {
  118. uint8_t *slice_params;
  119. VASliceParameterBufferBase *slice_param;
  120. if (!vactx->slice_data)
  121. vactx->slice_data = buffer;
  122. if (vactx->slice_data + vactx->slice_data_size != buffer) {
  123. if (commit_slices(vactx) < 0)
  124. return NULL;
  125. vactx->slice_data = buffer;
  126. }
  127. slice_params =
  128. av_fast_realloc(vactx->slice_params,
  129. &vactx->slice_params_alloc,
  130. (vactx->slice_count + 1) * vactx->slice_param_size);
  131. if (!slice_params)
  132. return NULL;
  133. vactx->slice_params = slice_params;
  134. slice_param = (VASliceParameterBufferBase *)(slice_params + vactx->slice_count * vactx->slice_param_size);
  135. slice_param->slice_data_size = size;
  136. slice_param->slice_data_offset = vactx->slice_data_size;
  137. slice_param->slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
  138. vactx->slice_count++;
  139. vactx->slice_data_size += size;
  140. return slice_param;
  141. }
  142. int ff_vaapi_common_end_frame(MpegEncContext *s)
  143. {
  144. struct vaapi_context * const vactx = s->avctx->hwaccel_context;
  145. int ret = -1;
  146. dprintf(s->avctx, "ff_vaapi_common_end_frame()\n");
  147. if (commit_slices(vactx) < 0)
  148. goto done;
  149. if (vactx->n_slice_buf_ids > 0) {
  150. if (render_picture(vactx, ff_vaapi_get_surface(s->current_picture_ptr)) < 0)
  151. goto done;
  152. ff_draw_horiz_band(s, 0, s->avctx->height);
  153. }
  154. ret = 0;
  155. done:
  156. destroy_buffers(vactx->display, &vactx->pic_param_buf_id, 1);
  157. destroy_buffers(vactx->display, &vactx->iq_matrix_buf_id, 1);
  158. destroy_buffers(vactx->display, &vactx->bitplane_buf_id, 1);
  159. destroy_buffers(vactx->display, vactx->slice_buf_ids, vactx->n_slice_buf_ids);
  160. av_freep(&vactx->bitplane_buffer);
  161. av_freep(&vactx->slice_buf_ids);
  162. av_freep(&vactx->slice_params);
  163. vactx->n_slice_buf_ids = 0;
  164. vactx->slice_buf_ids_alloc = 0;
  165. vactx->slice_count = 0;
  166. vactx->slice_params_alloc = 0;
  167. return ret;
  168. }
  169. /* @} */