FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vaapi.c
Go to the documentation of this file.
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 
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 
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 
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 =
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 
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 {
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 =
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 
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
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,
241  if (ret < 0)
242  goto finish;
243 
245 
246 finish:
248  return ret;
249 }
250 #endif
251 
252 /* @} */
VASliceParameterBufferBase * ff_vaapi_alloc_slice(FFVAContext *vactx, const uint8_t *buffer, uint32_t size)
Allocate a new slice descriptor for the input slice.
Definition: vaapi.c:177
#define NULL
Definition: coverity.c:32
int ff_vaapi_context_fini(AVCodecContext *avctx)
Common AVHWAccel.uninit() implementation.
Definition: vaapi.c:65
VAContextID context_id
Context ID (video decode pipeline)
const char * s
Definition: avisynth_c.h:631
const uint8_t * slice_data
Pointer to slice data buffer base.
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int ff_vaapi_render_picture(FFVAContext *vactx, VASurfaceID surface)
Definition: vaapi.c:70
VABufferID pic_param_buf_id
Picture parameter buffer.
uint32_t context_id
Context ID (video decode pipeline)
Definition: vaapi.h:75
This structure is used to share data between the FFmpeg library and the client video application...
Definition: vaapi.h:52
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
Definition: mpegvideo.c:2721
uint8_t
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2934
unsigned int slice_param_size
Size of a slice parameter element.
VADisplay display
Windowing system dependent handle.
#define ff_dlog(a,...)
void * ff_vaapi_alloc_iq_matrix(FFVAContext *vactx, unsigned int size)
Allocate a new IQ matrix buffer.
Definition: vaapi.c:167
static FFVAContext * ff_vaapi_get_context(AVCodecContext *avctx)
Extract vaapi_context from an AVCodecContext.
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
VABufferID iq_matrix_buf_id
Inverse quantiser matrix buffer.
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
Definition: mem.c:480
void ff_vaapi_common_end_frame(AVCodecContext *avctx)
Common AVHWAccel.end_frame() implementation.
Definition: vaapi.c:208
#define AVERROR(e)
Definition: error.h:43
int ff_vaapi_context_init(AVCodecContext *avctx)
Common AVHWAccel.init() implementation.
Definition: vaapi.c:44
static void destroy_buffers(VADisplay display, VABufferID *buffers, unsigned int n_buffers)
Definition: vaapi.c:33
unsigned int n_slice_buf_ids
Number of effective slice buffers.
unsigned int slice_params_alloc
Number of allocated slice parameters.
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:191
static VASurfaceID ff_vaapi_get_surface_id(AVFrame *pic)
Extract VASurfaceID from an AVFrame.
VABufferID * slice_buf_ids
Slice parameter/data buffers.
void * slice_params
Pointer to slice parameter buffers.
unsigned int slice_buf_ids_alloc
Number of allocated slice buffers.
void * display
Window system dependent data.
Definition: vaapi.h:59
attribute_deprecated uint32_t * slice_buf_ids
Slice parameter/data buffer IDs.
Definition: vaapi.h:112
main external API structure.
Definition: avcodec.h:1502
GLint GLenum type
Definition: opengl_enc.c:105
int ff_vaapi_commit_slices(FFVAContext *vactx)
Definition: vaapi.c:110
struct AVFrame * f
Definition: mpegpicture.h:46
void * ff_vaapi_alloc_pic_param(FFVAContext *vactx, unsigned int size)
Allocate a new picture parameter buffer.
Definition: vaapi.c:162
unsigned int slice_data_size
Current size of slice data.
MpegEncContext.
Definition: mpegvideo.h:88
attribute_deprecated void * slice_params
Pointer to VASliceParameterBuffers.
Definition: vaapi.h:139
struct AVCodecContext * avctx
Definition: mpegvideo.h:105
static void finish(void)
Definition: ffhash.c:62
GLuint * buffers
Definition: opengl_enc.c:99
unsigned int slice_count
Number of slices currently filled in.
VABufferID bitplane_buf_id
Bitplane buffer (for VC-1 decoding)
VAConfigID config_id
Configuration ID.
void * priv_data
Definition: avcodec.h:1544
#define av_freep(p)
int ff_vaapi_mpeg_end_frame(AVCodecContext *avctx)
uint8_t * ff_vaapi_alloc_bitplane(FFVAContext *vactx, uint32_t size)
Allocate a new bit-plane buffer.
Definition: vaapi.c:172
static void * alloc_buffer(FFVAContext *vactx, int type, unsigned int size, uint32_t *buf_id)
Definition: vaapi.c:150
GLuint buffer
Definition: opengl_enc.c:102
uint32_t config_id
Configuration ID.
Definition: vaapi.h:67