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