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 "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] != VA_INVALID_ID) {
39  vaDestroyBuffer(display, buffers[i]);
40  buffers[i] = VA_INVALID_ID;
41  }
42  }
43 }
44 
46 {
47  FFVAContext * const vactx = ff_vaapi_get_context(avctx);
48  const struct vaapi_context * const user_vactx = avctx->hwaccel_context;
49 
50  if (!user_vactx) {
51  av_log(avctx, AV_LOG_ERROR, "Hardware acceleration context (hwaccel_context) does not exist.\n");
52  return AVERROR(ENOSYS);
53  }
54 
55  vactx->display = user_vactx->display;
56  vactx->config_id = user_vactx->config_id;
57  vactx->context_id = user_vactx->context_id;
58 
59  vactx->pic_param_buf_id = VA_INVALID_ID;
60  vactx->iq_matrix_buf_id = VA_INVALID_ID;
61  vactx->bitplane_buf_id = VA_INVALID_ID;
62 
63  return 0;
64 }
65 
67 {
68  return 0;
69 }
70 
71 int ff_vaapi_render_picture(FFVAContext *vactx, VASurfaceID surface)
72 {
73  VABufferID va_buffers[3];
74  unsigned int n_va_buffers = 0;
75 
76  if (vactx->pic_param_buf_id == VA_INVALID_ID)
77  return 0;
78 
79  vaUnmapBuffer(vactx->display, vactx->pic_param_buf_id);
80  va_buffers[n_va_buffers++] = vactx->pic_param_buf_id;
81 
82  if (vactx->iq_matrix_buf_id != VA_INVALID_ID) {
83  vaUnmapBuffer(vactx->display, vactx->iq_matrix_buf_id);
84  va_buffers[n_va_buffers++] = vactx->iq_matrix_buf_id;
85  }
86 
87  if (vactx->bitplane_buf_id != VA_INVALID_ID) {
88  vaUnmapBuffer(vactx->display, vactx->bitplane_buf_id);
89  va_buffers[n_va_buffers++] = vactx->bitplane_buf_id;
90  }
91 
92  if (vaBeginPicture(vactx->display, vactx->context_id,
93  surface) != VA_STATUS_SUCCESS)
94  return -1;
95 
96  if (vaRenderPicture(vactx->display, vactx->context_id,
97  va_buffers, n_va_buffers) != VA_STATUS_SUCCESS)
98  return -1;
99 
100  if (vaRenderPicture(vactx->display, vactx->context_id,
101  vactx->slice_buf_ids,
102  vactx->n_slice_buf_ids) != VA_STATUS_SUCCESS)
103  return -1;
104 
105  if (vaEndPicture(vactx->display, vactx->context_id) != VA_STATUS_SUCCESS)
106  return -1;
107 
108  return 0;
109 }
110 
112 {
113  VABufferID *slice_buf_ids;
114  VABufferID slice_param_buf_id, slice_data_buf_id;
115 
116  if (vactx->slice_count == 0)
117  return 0;
118 
119  slice_buf_ids =
121  &vactx->slice_buf_ids_alloc,
122  (vactx->n_slice_buf_ids + 2) * sizeof(slice_buf_ids[0]));
123  if (!slice_buf_ids)
124  return -1;
125  vactx->slice_buf_ids = slice_buf_ids;
126 
127  slice_param_buf_id = VA_INVALID_ID;
128  if (vaCreateBuffer(vactx->display, vactx->context_id,
129  VASliceParameterBufferType,
130  vactx->slice_param_size,
131  vactx->slice_count, vactx->slice_params,
132  &slice_param_buf_id) != VA_STATUS_SUCCESS)
133  return -1;
134  vactx->slice_count = 0;
135 
136  slice_data_buf_id = VA_INVALID_ID;
137  if (vaCreateBuffer(vactx->display, vactx->context_id,
138  VASliceDataBufferType,
139  vactx->slice_data_size,
140  1, (void *)vactx->slice_data,
141  &slice_data_buf_id) != VA_STATUS_SUCCESS)
142  return -1;
143  vactx->slice_data = NULL;
144  vactx->slice_data_size = 0;
145 
146  slice_buf_ids[vactx->n_slice_buf_ids++] = slice_param_buf_id;
147  slice_buf_ids[vactx->n_slice_buf_ids++] = slice_data_buf_id;
148  return 0;
149 }
150 
151 static void *alloc_buffer(FFVAContext *vactx, int type, unsigned int size, uint32_t *buf_id)
152 {
153  void *data = NULL;
154 
155  *buf_id = VA_INVALID_ID;
156  if (vaCreateBuffer(vactx->display, vactx->context_id,
157  type, size, 1, NULL, buf_id) == VA_STATUS_SUCCESS)
158  vaMapBuffer(vactx->display, *buf_id, &data);
159 
160  return data;
161 }
162 
163 void *ff_vaapi_alloc_pic_param(FFVAContext *vactx, unsigned int size)
164 {
165  return alloc_buffer(vactx, VAPictureParameterBufferType, size, &vactx->pic_param_buf_id);
166 }
167 
168 void *ff_vaapi_alloc_iq_matrix(FFVAContext *vactx, unsigned int size)
169 {
170  return alloc_buffer(vactx, VAIQMatrixBufferType, size, &vactx->iq_matrix_buf_id);
171 }
172 
174 {
175  return alloc_buffer(vactx, VABitPlaneBufferType, size, &vactx->bitplane_buf_id);
176 }
177 
178 VASliceParameterBufferBase *ff_vaapi_alloc_slice(FFVAContext *vactx, const uint8_t *buffer, uint32_t size)
179 {
181  VASliceParameterBufferBase *slice_param;
182 
183  if (!vactx->slice_data)
184  vactx->slice_data = buffer;
185  if (vactx->slice_data + vactx->slice_data_size != buffer) {
186  if (ff_vaapi_commit_slices(vactx) < 0)
187  return NULL;
188  vactx->slice_data = buffer;
189  }
190 
191  slice_params =
193  &vactx->slice_params_alloc,
194  (vactx->slice_count + 1) * vactx->slice_param_size);
195  if (!slice_params)
196  return NULL;
197  vactx->slice_params = slice_params;
198 
199  slice_param = (VASliceParameterBufferBase *)(slice_params + vactx->slice_count * vactx->slice_param_size);
200  slice_param->slice_data_size = size;
201  slice_param->slice_data_offset = vactx->slice_data_size;
202  slice_param->slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
203 
204  vactx->slice_count++;
205  vactx->slice_data_size += size;
206  return slice_param;
207 }
208 
210 {
211  FFVAContext * const vactx = ff_vaapi_get_context(avctx);
212 
213  ff_dlog(avctx, "ff_vaapi_common_end_frame()\n");
214 
215  destroy_buffers(vactx->display, &vactx->pic_param_buf_id, 1);
216  destroy_buffers(vactx->display, &vactx->iq_matrix_buf_id, 1);
217  destroy_buffers(vactx->display, &vactx->bitplane_buf_id, 1);
218  destroy_buffers(vactx->display, vactx->slice_buf_ids, vactx->n_slice_buf_ids);
219  av_freep(&vactx->slice_buf_ids);
220  av_freep(&vactx->slice_params);
221  vactx->n_slice_buf_ids = 0;
222  vactx->slice_buf_ids_alloc = 0;
223  vactx->slice_count = 0;
224  vactx->slice_params_alloc = 0;
225 }
226 
227 #if CONFIG_H263_VAAPI_HWACCEL || CONFIG_MPEG1_VAAPI_HWACCEL || \
228  CONFIG_MPEG2_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL || \
229  CONFIG_VC1_VAAPI_HWACCEL || CONFIG_WMV3_VAAPI_HWACCEL
231 {
232  FFVAContext * const vactx = ff_vaapi_get_context(avctx);
233  MpegEncContext *s = avctx->priv_data;
234  int ret;
235 
236  ret = ff_vaapi_commit_slices(vactx);
237  if (ret < 0)
238  goto finish;
239 
240  ret = ff_vaapi_render_picture(vactx,
242  if (ret < 0)
243  goto finish;
244 
246 
247 finish:
249  return ret;
250 }
251 #endif
252 
253 /* @} */
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:178
#define NULL
Definition: coverity.c:32
int ff_vaapi_context_fini(AVCodecContext *avctx)
Common AVHWAccel.uninit() implementation.
Definition: vaapi.c:66
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:71
VABufferID pic_param_buf_id
Picture parameter buffer.
uint32_t context_id
Context ID (video decode pipeline)
Definition: vaapi.h:75
mpegvideo header.
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:2732
uint8_t
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2855
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:168
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:209
#define AVERROR(e)
Definition: error.h:43
int ff_vaapi_context_init(AVCodecContext *avctx)
Common AVHWAccel.init() implementation.
Definition: vaapi.c:45
static void destroy_buffers(VADisplay display, VABufferID *buffers, unsigned int n_buffers)
Definition: vaapi.c:34
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:181
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
static void finish(void)
Definition: movenc-test.c:299
main external API structure.
Definition: avcodec.h:1532
GLint GLenum type
Definition: opengl_enc.c:105
int ff_vaapi_commit_slices(FFVAContext *vactx)
Definition: vaapi.c:111
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:163
unsigned int slice_data_size
Current size of slice data.
MpegEncContext.
Definition: mpegvideo.h:78
attribute_deprecated void * slice_params
Pointer to VASliceParameterBuffers.
Definition: vaapi.h:139
struct AVCodecContext * avctx
Definition: mpegvideo.h:95
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:1574
#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:173
static void * alloc_buffer(FFVAContext *vactx, int type, unsigned int size, uint32_t *buf_id)
Definition: vaapi.c:151
GLuint buffer
Definition: opengl_enc.c:102
uint32_t config_id
Configuration ID.
Definition: vaapi.h:67