2 * Video Acceleration API (video decoding)
3 * HW decode acceleration for MPEG-2, MPEG-4, H.264 and VC-1
5 * Copyright (C) 2008-2009 Splitted-Desktop Systems
7 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/log.h"
25 #include "vaapi_internal.h"
28 * @addtogroup VAAPI_Decoding
33 static void destroy_buffers(VADisplay display, VABufferID *buffers, unsigned int n_buffers)
36 for (i = 0; i < n_buffers; i++) {
37 if (buffers[i] != VA_INVALID_ID) {
38 vaDestroyBuffer(display, buffers[i]);
39 buffers[i] = VA_INVALID_ID;
44 int ff_vaapi_context_init(AVCodecContext *avctx)
46 FFVAContext * const vactx = ff_vaapi_get_context(avctx);
47 const struct vaapi_context * const user_vactx = avctx->hwaccel_context;
50 av_log(avctx, AV_LOG_ERROR, "Hardware acceleration context (hwaccel_context) does not exist.\n");
51 return AVERROR(ENOSYS);
54 vactx->display = user_vactx->display;
55 vactx->config_id = user_vactx->config_id;
56 vactx->context_id = user_vactx->context_id;
58 vactx->pic_param_buf_id = VA_INVALID_ID;
59 vactx->iq_matrix_buf_id = VA_INVALID_ID;
60 vactx->bitplane_buf_id = VA_INVALID_ID;
65 int ff_vaapi_context_fini(AVCodecContext *avctx)
70 int ff_vaapi_render_picture(FFVAContext *vactx, VASurfaceID surface)
72 VABufferID va_buffers[3];
73 unsigned int n_va_buffers = 0;
75 if (vactx->pic_param_buf_id == VA_INVALID_ID)
78 vaUnmapBuffer(vactx->display, vactx->pic_param_buf_id);
79 va_buffers[n_va_buffers++] = vactx->pic_param_buf_id;
81 if (vactx->iq_matrix_buf_id != VA_INVALID_ID) {
82 vaUnmapBuffer(vactx->display, vactx->iq_matrix_buf_id);
83 va_buffers[n_va_buffers++] = vactx->iq_matrix_buf_id;
86 if (vactx->bitplane_buf_id != VA_INVALID_ID) {
87 vaUnmapBuffer(vactx->display, vactx->bitplane_buf_id);
88 va_buffers[n_va_buffers++] = vactx->bitplane_buf_id;
91 if (vaBeginPicture(vactx->display, vactx->context_id,
92 surface) != VA_STATUS_SUCCESS)
95 if (vaRenderPicture(vactx->display, vactx->context_id,
96 va_buffers, n_va_buffers) != VA_STATUS_SUCCESS)
99 if (vaRenderPicture(vactx->display, vactx->context_id,
100 vactx->slice_buf_ids,
101 vactx->n_slice_buf_ids) != VA_STATUS_SUCCESS)
104 if (vaEndPicture(vactx->display, vactx->context_id) != VA_STATUS_SUCCESS)
110 int ff_vaapi_commit_slices(FFVAContext *vactx)
112 VABufferID *slice_buf_ids;
113 VABufferID slice_param_buf_id, slice_data_buf_id;
115 if (vactx->slice_count == 0)
119 av_fast_realloc(vactx->slice_buf_ids,
120 &vactx->slice_buf_ids_alloc,
121 (vactx->n_slice_buf_ids + 2) * sizeof(slice_buf_ids[0]));
124 vactx->slice_buf_ids = slice_buf_ids;
126 slice_param_buf_id = VA_INVALID_ID;
127 if (vaCreateBuffer(vactx->display, vactx->context_id,
128 VASliceParameterBufferType,
129 vactx->slice_param_size,
130 vactx->slice_count, vactx->slice_params,
131 &slice_param_buf_id) != VA_STATUS_SUCCESS)
133 vactx->slice_count = 0;
135 slice_data_buf_id = VA_INVALID_ID;
136 if (vaCreateBuffer(vactx->display, vactx->context_id,
137 VASliceDataBufferType,
138 vactx->slice_data_size,
139 1, (void *)vactx->slice_data,
140 &slice_data_buf_id) != VA_STATUS_SUCCESS)
142 vactx->slice_data = NULL;
143 vactx->slice_data_size = 0;
145 slice_buf_ids[vactx->n_slice_buf_ids++] = slice_param_buf_id;
146 slice_buf_ids[vactx->n_slice_buf_ids++] = slice_data_buf_id;
150 static void *alloc_buffer(FFVAContext *vactx, int type, unsigned int size, uint32_t *buf_id)
154 *buf_id = VA_INVALID_ID;
155 if (vaCreateBuffer(vactx->display, vactx->context_id,
156 type, size, 1, NULL, buf_id) == VA_STATUS_SUCCESS)
157 vaMapBuffer(vactx->display, *buf_id, &data);
162 void *ff_vaapi_alloc_pic_param(FFVAContext *vactx, unsigned int size)
164 return alloc_buffer(vactx, VAPictureParameterBufferType, size, &vactx->pic_param_buf_id);
167 void *ff_vaapi_alloc_iq_matrix(FFVAContext *vactx, unsigned int size)
169 return alloc_buffer(vactx, VAIQMatrixBufferType, size, &vactx->iq_matrix_buf_id);
172 uint8_t *ff_vaapi_alloc_bitplane(FFVAContext *vactx, uint32_t size)
174 return alloc_buffer(vactx, VABitPlaneBufferType, size, &vactx->bitplane_buf_id);
177 VASliceParameterBufferBase *ff_vaapi_alloc_slice(FFVAContext *vactx, const uint8_t *buffer, uint32_t size)
179 uint8_t *slice_params;
180 VASliceParameterBufferBase *slice_param;
182 if (!vactx->slice_data)
183 vactx->slice_data = buffer;
184 if (vactx->slice_data + vactx->slice_data_size != buffer) {
185 if (ff_vaapi_commit_slices(vactx) < 0)
187 vactx->slice_data = buffer;
191 av_fast_realloc(vactx->slice_params,
192 &vactx->slice_params_alloc,
193 (vactx->slice_count + 1) * vactx->slice_param_size);
196 vactx->slice_params = slice_params;
198 slice_param = (VASliceParameterBufferBase *)(slice_params + vactx->slice_count * vactx->slice_param_size);
199 slice_param->slice_data_size = size;
200 slice_param->slice_data_offset = vactx->slice_data_size;
201 slice_param->slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
203 vactx->slice_count++;
204 vactx->slice_data_size += size;
208 void ff_vaapi_common_end_frame(AVCodecContext *avctx)
210 FFVAContext * const vactx = ff_vaapi_get_context(avctx);
212 ff_dlog(avctx, "ff_vaapi_common_end_frame()\n");
214 destroy_buffers(vactx->display, &vactx->pic_param_buf_id, 1);
215 destroy_buffers(vactx->display, &vactx->iq_matrix_buf_id, 1);
216 destroy_buffers(vactx->display, &vactx->bitplane_buf_id, 1);
217 destroy_buffers(vactx->display, vactx->slice_buf_ids, vactx->n_slice_buf_ids);
218 av_freep(&vactx->slice_buf_ids);
219 av_freep(&vactx->slice_params);
220 vactx->n_slice_buf_ids = 0;
221 vactx->slice_buf_ids_alloc = 0;
222 vactx->slice_count = 0;
223 vactx->slice_params_alloc = 0;
226 #if CONFIG_H263_VAAPI_HWACCEL || CONFIG_MPEG1_VAAPI_HWACCEL || \
227 CONFIG_MPEG2_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL || \
228 CONFIG_VC1_VAAPI_HWACCEL || CONFIG_WMV3_VAAPI_HWACCEL
229 int ff_vaapi_mpeg_end_frame(AVCodecContext *avctx)
231 FFVAContext * const vactx = ff_vaapi_get_context(avctx);
232 MpegEncContext *s = avctx->priv_data;
235 ret = ff_vaapi_commit_slices(vactx);
239 ret = ff_vaapi_render_picture(vactx,
240 ff_vaapi_get_surface_id(s->current_picture_ptr->f));
244 ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
247 ff_vaapi_common_end_frame(avctx);