1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/*****************************************************************
* gmerlin-avdecoder - a general purpose multimedia decoding library
*
* Copyright (c) 2001 - 2012 Members of the Gmerlin project
* gmerlin-general@lists.sourceforge.net
* http://gmerlin.sourceforge.net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* *****************************************************************/
#include <config.h>
#include <avdec_private.h>
#include <X11/Xlib.h>
#include AVCODEC_HEADER
#include VAAPI_HEADER
#include <va/va.h>
#include <bgav_vaapi.h>
#include <gavl/hw_vaapi_x11.h>
static int get_profile(enum AVCodecID id)
{
int profile;
switch(id)
{
case AV_CODEC_ID_MPEG2VIDEO:
profile = VAProfileMPEG2Main;
break;
case AV_CODEC_ID_MPEG4:
case AV_CODEC_ID_H263:
profile = VAProfileMPEG4AdvancedSimple;
break;
case AV_CODEC_ID_H264:
profile = VAProfileH264High;
break;
case AV_CODEC_ID_WMV3:
profile = VAProfileVC1Main;
break;
case AV_CODEC_ID_VC1:
profile = VAProfileVC1Advanced;
break;
default:
profile = VAProfileNone;
break;
}
return profile;
}
static int has_profile(VADisplay dpy, VAProfile profile)
{
VAProfile * p = NULL;
int num, i;
VAStatus status;
int ret = 0;
num = vaMaxNumProfiles(dpy);
p = calloc(num, sizeof(*p));
if((status = vaQueryConfigProfiles(dpy, p, &num)) != VA_STATUS_SUCCESS)
goto fail;
for(i = 0; i < num; i++)
{
if(p[i] == profile)
{
ret = 1;
break;
}
}
fail:
if(p)
free(p);
return ret;
}
static int has_entrypoint(VADisplay dpy, VAProfile profile,
VAEntrypoint entrypoint)
{
int ret = 0;
int num;
VAStatus status;
VAEntrypoint * ep = NULL;
int i;
num = vaMaxNumEntrypoints(dpy);
ep = calloc(num, sizeof(*ep));
if((status = vaQueryConfigEntrypoints(dpy, profile, ep, &num)) != VA_STATUS_SUCCESS)
goto fail;
for(i = 0; i < num; i++)
{
if(ep[i] == entrypoint)
{
ret = 1;
break;
}
}
fail:
if(ep)
free(ep);
return ret;
}
static VAEntrypoint get_entrypoint(enum AVPixelFormat pfmt)
{
switch(pfmt)
{
case AV_PIX_FMT_VAAPI_MOCO:
return VAEntrypointMoComp;
break;
case AV_PIX_FMT_VAAPI_IDCT:
return VAEntrypointIDCT;
break;
case AV_PIX_FMT_VAAPI_VLD:
return VAEntrypointVLD;
break;
default:
break;
}
return 0;
}
/* Noop because we don't have dynamic memory in the buffer */
static void free_buffer(void * opaque, uint8_t * data)
{
}
int bgav_vaapi_init(bgav_vaapi_t * v,
AVCodecContext * avctx,
enum AVPixelFormat pfmt)
{
int i;
VAProfile profile;
VAEntrypoint ep;
VAConfigAttrib attr;
VAStatus status;
int context_flags = 0;
int surface_width, surface_height, context_width, context_height;
bgav_stream_t * s = avctx->opaque;
if((profile = get_profile(avctx->codec_id)) == VAProfileNone)
goto fail;
/* Connect to hardware */
if(!(v->hwctx = gavl_hw_ctx_create_vaapi_x11(NULL)))
goto fail;
v->vaapi_ctx.display = gavl_hw_ctx_vaapi_x11_get_va_display(v->hwctx);
/* Make IDs invalid */
v->vaapi_ctx.config_id = VA_INVALID_ID;
v->vaapi_ctx.context_id = VA_INVALID_ID;
if(!has_profile(v->vaapi_ctx.display, profile))
goto fail;
if(!(ep = get_entrypoint(pfmt)))
goto fail;
if(!has_entrypoint(v->vaapi_ctx.display, profile, ep))
goto fail;
if(!s->ci.max_ref_frames)
goto fail;
/* Get dimensions. What about padding here? */
surface_width = s->data.video.format->image_width;
surface_height = s->data.video.format->image_height;
context_width = s->data.video.format->image_width;
context_height = s->data.video.format->image_height;
/* Get surface format */
attr.type = VAConfigAttribRTFormat;
vaGetConfigAttributes(v->vaapi_ctx.display, profile, ep, &attr, 1);
if(!(attr.value & VA_RT_FORMAT_YUV420))
goto fail;
/* Create config */
if((status = vaCreateConfig(v->vaapi_ctx.display, profile, ep,
&attr, 1, &v->vaapi_ctx.config_id)) != VA_STATUS_SUCCESS)
goto fail;
/* Create surfaces */
v->num_surfaces = s->ci.max_ref_frames + 2; // Reference frames + render frame + 1 safety frame
v->surfaces = calloc(v->num_surfaces, sizeof(*v->surfaces));
/* Make invalid so we won't destroy invalid surfaces if the create call fails */
v->surfaces[0] = VA_INVALID_ID;
if((status = vaCreateSurfaces(v->vaapi_ctx.display,
VA_RT_FORMAT_YUV420,
surface_width, surface_height,
v->surfaces, v->num_surfaces, NULL, 0) != VA_STATUS_SUCCESS))
goto fail;
/* Create context */
if(s->data.video.format->interlace_mode == GAVL_INTERLACE_NONE)
context_flags |= VA_PROGRESSIVE;
if((status = vaCreateContext(v->vaapi_ctx.display,
v->vaapi_ctx.config_id,
context_width,
context_height,
context_flags,
v->surfaces,
v->num_surfaces,
&v->vaapi_ctx.context_id) != VA_STATUS_SUCCESS))
goto fail;
avctx->hwaccel_context = &v->vaapi_ctx;
/* Create frames */
v->frames = calloc(v->num_surfaces, sizeof(*v->frames));
for(i = 0; i < v->num_surfaces; i++)
{
v->frames[i].s = v->surfaces[i];
v->frames[i].f = gavl_video_frame_create(NULL);
v->frames[i].f->user_data = v->surfaces + i;
v->frames[i].f->hwctx = v->hwctx;
v->frames[i].buf = av_buffer_create((uint8_t*)(uintptr_t)v->frames[i].s, 0,
free_buffer, NULL, 0);
}
s->data.video.format->pixelformat = GAVL_YUV_420_P;
s->data.video.format->hwctx = v->hwctx;
return 1;
fail: // Cleanup
bgav_vaapi_cleanup(v);
return 0;
}
bgav_vaapi_frame_t * bgav_vaapi_get_frame_by_id(bgav_vaapi_t * v, VASurfaceID id)
{
int i;
for(i = 0; i < v->num_surfaces; i++)
{
if(v->frames[i].s == id)
return &v->frames[i];
}
return NULL;
}
bgav_vaapi_frame_t * bgav_vaapi_get_frame(bgav_vaapi_t * v)
{
int i;
for(i = 0; i < v->num_surfaces; i++)
{
if(av_buffer_get_ref_count(v->frames[i].buf) < 2)
{
// fprintf(stderr, "bgav_vaapi_get_frame %d %d\n", i + 1, v->num_surfaces);
return &v->frames[i];
}
}
// fprintf(stderr, "Out of frames\n");
return NULL;
}
void bgav_vaapi_cleanup(bgav_vaapi_t * v)
{
int i;
if(!v->hwctx)
return;
if(v->vaapi_ctx.config_id != VA_INVALID_ID)
vaDestroyConfig(v->vaapi_ctx.display, v->vaapi_ctx.config_id);
if(v->vaapi_ctx.context_id != VA_INVALID_ID)
vaDestroyContext(v->vaapi_ctx.display, v->vaapi_ctx.context_id);
if(v->surfaces)
{
if(v->surfaces[0] != VA_INVALID_ID)
vaDestroySurfaces(v->vaapi_ctx.display, v->surfaces, v->num_surfaces);
free(v->surfaces);
}
if(v->frames)
{
for(i = 0; i < v->num_surfaces; i++)
{
if(v->frames[i].f)
{
gavl_video_frame_null(v->frames[i].f);
v->frames[i].f->hwctx = NULL;
gavl_video_frame_destroy(v->frames[i].f);
}
if(v->frames[i].buf)
av_buffer_unref(&v->frames[i].buf);
}
free(v->frames);
}
gavl_hw_ctx_destroy(v->hwctx);
v->hwctx = NULL;
}
|