]> git.sesse.net Git - ffmpeg/blob - libavcodec/vaapi.c
avconv_vaapi: Convert to use hw_frames_ctx only
[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 Libav.
8  *
9  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include "h264dec.h"
25 #include "mpegvideo.h"
26 #include "vaapi_internal.h"
27
28 /**
29  * @addtogroup VAAPI_Decoding
30  *
31  * @{
32  */
33
34 static void destroy_buffers(VADisplay display, VABufferID *buffers, unsigned int n_buffers)
35 {
36     unsigned int i;
37     for (i = 0; i < n_buffers; i++) {
38         if (buffers[i]) {
39             vaDestroyBuffer(display, buffers[i]);
40             buffers[i] = 0;
41         }
42     }
43 }
44
45 int ff_vaapi_render_picture(struct vaapi_context *vactx, VASurfaceID surface)
46 {
47     VABufferID va_buffers[3];
48     unsigned int n_va_buffers = 0;
49
50     vaUnmapBuffer(vactx->display, vactx->pic_param_buf_id);
51     va_buffers[n_va_buffers++] = vactx->pic_param_buf_id;
52
53     if (vactx->iq_matrix_buf_id) {
54         vaUnmapBuffer(vactx->display, vactx->iq_matrix_buf_id);
55         va_buffers[n_va_buffers++] = vactx->iq_matrix_buf_id;
56     }
57
58     if (vactx->bitplane_buf_id) {
59         vaUnmapBuffer(vactx->display, vactx->bitplane_buf_id);
60         va_buffers[n_va_buffers++] = vactx->bitplane_buf_id;
61     }
62
63     if (vaBeginPicture(vactx->display, vactx->context_id,
64                        surface) != VA_STATUS_SUCCESS)
65         return -1;
66
67     if (vaRenderPicture(vactx->display, vactx->context_id,
68                         va_buffers, n_va_buffers) != VA_STATUS_SUCCESS)
69         return -1;
70
71     if (vaRenderPicture(vactx->display, vactx->context_id,
72                         vactx->slice_buf_ids,
73                         vactx->n_slice_buf_ids) != VA_STATUS_SUCCESS)
74         return -1;
75
76     if (vaEndPicture(vactx->display, vactx->context_id) != VA_STATUS_SUCCESS)
77         return -1;
78
79     return 0;
80 }
81
82 int ff_vaapi_commit_slices(struct vaapi_context *vactx)
83 {
84     VABufferID *slice_buf_ids;
85     VABufferID slice_param_buf_id, slice_data_buf_id;
86
87     if (vactx->slice_count == 0)
88         return 0;
89
90     slice_buf_ids =
91         av_fast_realloc(vactx->slice_buf_ids,
92                         &vactx->slice_buf_ids_alloc,
93                         (vactx->n_slice_buf_ids + 2) * sizeof(slice_buf_ids[0]));
94     if (!slice_buf_ids)
95         return -1;
96     vactx->slice_buf_ids = slice_buf_ids;
97
98     slice_param_buf_id = 0;
99     if (vaCreateBuffer(vactx->display, vactx->context_id,
100                        VASliceParameterBufferType,
101                        vactx->slice_param_size,
102                        vactx->slice_count, vactx->slice_params,
103                        &slice_param_buf_id) != VA_STATUS_SUCCESS)
104         return -1;
105     vactx->slice_count = 0;
106
107     slice_data_buf_id = 0;
108     if (vaCreateBuffer(vactx->display, vactx->context_id,
109                        VASliceDataBufferType,
110                        vactx->slice_data_size,
111                        1, (void *)vactx->slice_data,
112                        &slice_data_buf_id) != VA_STATUS_SUCCESS)
113         return -1;
114     vactx->slice_data = NULL;
115     vactx->slice_data_size = 0;
116
117     slice_buf_ids[vactx->n_slice_buf_ids++] = slice_param_buf_id;
118     slice_buf_ids[vactx->n_slice_buf_ids++] = slice_data_buf_id;
119     return 0;
120 }
121
122 static void *alloc_buffer(struct vaapi_context *vactx, int type, unsigned int size, uint32_t *buf_id)
123 {
124     void *data = NULL;
125
126     *buf_id = 0;
127     if (vaCreateBuffer(vactx->display, vactx->context_id,
128                        type, size, 1, NULL, buf_id) == VA_STATUS_SUCCESS)
129         vaMapBuffer(vactx->display, *buf_id, &data);
130
131     return data;
132 }
133
134 void *ff_vaapi_alloc_pic_param(struct vaapi_context *vactx, unsigned int size)
135 {
136     return alloc_buffer(vactx, VAPictureParameterBufferType, size, &vactx->pic_param_buf_id);
137 }
138
139 void *ff_vaapi_alloc_iq_matrix(struct vaapi_context *vactx, unsigned int size)
140 {
141     return alloc_buffer(vactx, VAIQMatrixBufferType, size, &vactx->iq_matrix_buf_id);
142 }
143
144 uint8_t *ff_vaapi_alloc_bitplane(struct vaapi_context *vactx, uint32_t size)
145 {
146     return alloc_buffer(vactx, VABitPlaneBufferType, size, &vactx->bitplane_buf_id);
147 }
148
149 VASliceParameterBufferBase *ff_vaapi_alloc_slice(struct vaapi_context *vactx, const uint8_t *buffer, uint32_t size)
150 {
151     uint8_t *slice_params;
152     VASliceParameterBufferBase *slice_param;
153
154     if (!vactx->slice_data)
155         vactx->slice_data = buffer;
156     if (vactx->slice_data + vactx->slice_data_size != buffer) {
157         if (ff_vaapi_commit_slices(vactx) < 0)
158             return NULL;
159         vactx->slice_data = buffer;
160     }
161
162     slice_params =
163         av_fast_realloc(vactx->slice_params,
164                         &vactx->slice_params_alloc,
165                         (vactx->slice_count + 1) * vactx->slice_param_size);
166     if (!slice_params)
167         return NULL;
168     vactx->slice_params = slice_params;
169
170     slice_param = (VASliceParameterBufferBase *)(slice_params + vactx->slice_count * vactx->slice_param_size);
171     slice_param->slice_data_size   = size;
172     slice_param->slice_data_offset = vactx->slice_data_size;
173     slice_param->slice_data_flag   = VA_SLICE_DATA_FLAG_ALL;
174
175     vactx->slice_count++;
176     vactx->slice_data_size += size;
177     return slice_param;
178 }
179
180 void ff_vaapi_common_end_frame(AVCodecContext *avctx)
181 {
182     struct vaapi_context * const vactx = avctx->hwaccel_context;
183
184     destroy_buffers(vactx->display, &vactx->pic_param_buf_id, 1);
185     destroy_buffers(vactx->display, &vactx->iq_matrix_buf_id, 1);
186     destroy_buffers(vactx->display, &vactx->bitplane_buf_id, 1);
187     destroy_buffers(vactx->display, vactx->slice_buf_ids, vactx->n_slice_buf_ids);
188     av_freep(&vactx->slice_buf_ids);
189     av_freep(&vactx->slice_params);
190     vactx->n_slice_buf_ids     = 0;
191     vactx->slice_buf_ids_alloc = 0;
192     vactx->slice_count         = 0;
193     vactx->slice_params_alloc  = 0;
194 }
195
196 #if CONFIG_H263_VAAPI_HWACCEL  || CONFIG_MPEG1_VAAPI_HWACCEL || \
197     CONFIG_MPEG2_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL || \
198     CONFIG_VC1_VAAPI_HWACCEL   || CONFIG_WMV3_VAAPI_HWACCEL
199 int ff_vaapi_mpeg_end_frame(AVCodecContext *avctx)
200 {
201     struct vaapi_context * const vactx = avctx->hwaccel_context;
202     MpegEncContext *s = avctx->priv_data;
203     int ret;
204
205     ret = ff_vaapi_commit_slices(vactx);
206     if (ret < 0)
207         goto finish;
208
209     ret = ff_vaapi_render_picture(vactx,
210                                   ff_vaapi_get_surface_id(s->current_picture_ptr->f));
211     if (ret < 0)
212         goto finish;
213
214     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
215
216 finish:
217     ff_vaapi_common_end_frame(avctx);
218     return ret;
219 }
220 #endif
221
222 /* @} */