]> git.sesse.net Git - ffmpeg/blob - libavcodec/vaapi.c
avcodec/jpeg2000dec: Assert that step_x/y are valid
[ffmpeg] / libavcodec / vaapi.c
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
24 #include "libavutil/log.h"
25 #include "vaapi_internal.h"
26
27 /**
28  * @addtogroup VAAPI_Decoding
29  *
30  * @{
31  */
32
33 static void destroy_buffers(VADisplay display, VABufferID *buffers, unsigned int n_buffers)
34 {
35     unsigned int i;
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;
40         }
41     }
42 }
43
44 int ff_vaapi_context_init(AVCodecContext *avctx)
45 {
46     FFVAContext * const vactx = ff_vaapi_get_context(avctx);
47     const struct vaapi_context * const user_vactx = avctx->hwaccel_context;
48
49     if (!user_vactx) {
50         av_log(avctx, AV_LOG_ERROR, "Hardware acceleration context (hwaccel_context) does not exist.\n");
51         return AVERROR(ENOSYS);
52     }
53
54     vactx->display              = user_vactx->display;
55     vactx->config_id            = user_vactx->config_id;
56     vactx->context_id           = user_vactx->context_id;
57
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;
61
62     return 0;
63 }
64
65 int ff_vaapi_context_fini(AVCodecContext *avctx)
66 {
67     return 0;
68 }
69
70 int ff_vaapi_render_picture(FFVAContext *vactx, VASurfaceID surface)
71 {
72     VABufferID va_buffers[3];
73     unsigned int n_va_buffers = 0;
74
75     if (vactx->pic_param_buf_id == VA_INVALID_ID)
76         return 0;
77
78     vaUnmapBuffer(vactx->display, vactx->pic_param_buf_id);
79     va_buffers[n_va_buffers++] = vactx->pic_param_buf_id;
80
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;
84     }
85
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;
89     }
90
91     if (vaBeginPicture(vactx->display, vactx->context_id,
92                        surface) != VA_STATUS_SUCCESS)
93         return -1;
94
95     if (vaRenderPicture(vactx->display, vactx->context_id,
96                         va_buffers, n_va_buffers) != VA_STATUS_SUCCESS)
97         return -1;
98
99     if (vaRenderPicture(vactx->display, vactx->context_id,
100                         vactx->slice_buf_ids,
101                         vactx->n_slice_buf_ids) != VA_STATUS_SUCCESS)
102         return -1;
103
104     if (vaEndPicture(vactx->display, vactx->context_id) != VA_STATUS_SUCCESS)
105         return -1;
106
107     return 0;
108 }
109
110 int ff_vaapi_commit_slices(FFVAContext *vactx)
111 {
112     VABufferID *slice_buf_ids;
113     VABufferID slice_param_buf_id, slice_data_buf_id;
114
115     if (vactx->slice_count == 0)
116         return 0;
117
118     slice_buf_ids =
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]));
122     if (!slice_buf_ids)
123         return -1;
124     vactx->slice_buf_ids = slice_buf_ids;
125
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)
132         return -1;
133     vactx->slice_count = 0;
134
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)
141         return -1;
142     vactx->slice_data = NULL;
143     vactx->slice_data_size = 0;
144
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;
147     return 0;
148 }
149
150 static void *alloc_buffer(FFVAContext *vactx, int type, unsigned int size, uint32_t *buf_id)
151 {
152     void *data = NULL;
153
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);
158
159     return data;
160 }
161
162 void *ff_vaapi_alloc_pic_param(FFVAContext *vactx, unsigned int size)
163 {
164     return alloc_buffer(vactx, VAPictureParameterBufferType, size, &vactx->pic_param_buf_id);
165 }
166
167 void *ff_vaapi_alloc_iq_matrix(FFVAContext *vactx, unsigned int size)
168 {
169     return alloc_buffer(vactx, VAIQMatrixBufferType, size, &vactx->iq_matrix_buf_id);
170 }
171
172 uint8_t *ff_vaapi_alloc_bitplane(FFVAContext *vactx, uint32_t size)
173 {
174     return alloc_buffer(vactx, VABitPlaneBufferType, size, &vactx->bitplane_buf_id);
175 }
176
177 VASliceParameterBufferBase *ff_vaapi_alloc_slice(FFVAContext *vactx, const uint8_t *buffer, uint32_t size)
178 {
179     uint8_t *slice_params;
180     VASliceParameterBufferBase *slice_param;
181
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)
186             return NULL;
187         vactx->slice_data = buffer;
188     }
189
190     slice_params =
191         av_fast_realloc(vactx->slice_params,
192                         &vactx->slice_params_alloc,
193                         (vactx->slice_count + 1) * vactx->slice_param_size);
194     if (!slice_params)
195         return NULL;
196     vactx->slice_params = slice_params;
197
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;
202
203     vactx->slice_count++;
204     vactx->slice_data_size += size;
205     return slice_param;
206 }
207
208 void ff_vaapi_common_end_frame(AVCodecContext *avctx)
209 {
210     FFVAContext * const vactx = ff_vaapi_get_context(avctx);
211
212     ff_dlog(avctx, "ff_vaapi_common_end_frame()\n");
213
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;
224 }
225
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)
230 {
231     FFVAContext * const vactx = ff_vaapi_get_context(avctx);
232     MpegEncContext *s = avctx->priv_data;
233     int ret;
234
235     ret = ff_vaapi_commit_slices(vactx);
236     if (ret < 0)
237         goto finish;
238
239     ret = ff_vaapi_render_picture(vactx,
240                                   ff_vaapi_get_surface_id(s->current_picture_ptr->f));
241     if (ret < 0)
242         goto finish;
243
244     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
245
246 finish:
247     ff_vaapi_common_end_frame(avctx);
248     return ret;
249 }
250 #endif
251
252 /* @} */