| 1 | /* |
| 2 | * This file is part of FFmpeg. |
| 3 | * |
| 4 | * FFmpeg is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * FFmpeg is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with FFmpeg; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | |
| 21 | #include <vdpau/vdpau.h> |
| 22 | #include <vdpau/vdpau_x11.h> |
| 23 | |
| 24 | #include <X11/Xlib.h> |
| 25 | |
| 26 | #include "ffmpeg.h" |
| 27 | |
| 28 | #include "libavcodec/vdpau.h" |
| 29 | |
| 30 | #include "libavutil/avassert.h" |
| 31 | #include "libavutil/buffer.h" |
| 32 | #include "libavutil/frame.h" |
| 33 | #include "libavutil/pixfmt.h" |
| 34 | |
| 35 | typedef struct VDPAUContext { |
| 36 | Display *dpy; |
| 37 | |
| 38 | VdpDevice device; |
| 39 | VdpDecoder decoder; |
| 40 | VdpGetProcAddress *get_proc_address; |
| 41 | |
| 42 | VdpGetErrorString *get_error_string; |
| 43 | VdpGetInformationString *get_information_string; |
| 44 | VdpDeviceDestroy *device_destroy; |
| 45 | #if 1 // for ffmpegs older vdpau API, not the oldest though |
| 46 | VdpDecoderCreate *decoder_create; |
| 47 | VdpDecoderDestroy *decoder_destroy; |
| 48 | VdpDecoderRender *decoder_render; |
| 49 | #endif |
| 50 | VdpVideoSurfaceCreate *video_surface_create; |
| 51 | VdpVideoSurfaceDestroy *video_surface_destroy; |
| 52 | VdpVideoSurfaceGetBitsYCbCr *video_surface_get_bits; |
| 53 | VdpVideoSurfaceGetParameters *video_surface_get_parameters; |
| 54 | VdpVideoSurfaceQueryGetPutBitsYCbCrCapabilities *video_surface_query; |
| 55 | |
| 56 | AVFrame *tmp_frame; |
| 57 | |
| 58 | enum AVPixelFormat pix_fmt; |
| 59 | VdpYCbCrFormat vdpau_format; |
| 60 | } VDPAUContext; |
| 61 | |
| 62 | int vdpau_api_ver = 2; |
| 63 | |
| 64 | static void vdpau_uninit(AVCodecContext *s) |
| 65 | { |
| 66 | InputStream *ist = s->opaque; |
| 67 | VDPAUContext *ctx = ist->hwaccel_ctx; |
| 68 | |
| 69 | ist->hwaccel_uninit = NULL; |
| 70 | ist->hwaccel_get_buffer = NULL; |
| 71 | ist->hwaccel_retrieve_data = NULL; |
| 72 | |
| 73 | if (ctx->decoder_destroy) |
| 74 | ctx->decoder_destroy(ctx->decoder); |
| 75 | |
| 76 | if (ctx->device_destroy) |
| 77 | ctx->device_destroy(ctx->device); |
| 78 | |
| 79 | if (ctx->dpy) |
| 80 | XCloseDisplay(ctx->dpy); |
| 81 | |
| 82 | av_frame_free(&ctx->tmp_frame); |
| 83 | |
| 84 | av_freep(&ist->hwaccel_ctx); |
| 85 | av_freep(&s->hwaccel_context); |
| 86 | } |
| 87 | |
| 88 | static void vdpau_release_buffer(void *opaque, uint8_t *data) |
| 89 | { |
| 90 | VdpVideoSurface surface = *(VdpVideoSurface*)data; |
| 91 | VDPAUContext *ctx = opaque; |
| 92 | |
| 93 | ctx->video_surface_destroy(surface); |
| 94 | av_freep(&data); |
| 95 | } |
| 96 | |
| 97 | static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags) |
| 98 | { |
| 99 | InputStream *ist = s->opaque; |
| 100 | VDPAUContext *ctx = ist->hwaccel_ctx; |
| 101 | VdpVideoSurface *surface; |
| 102 | VdpStatus err; |
| 103 | |
| 104 | av_assert0(frame->format == AV_PIX_FMT_VDPAU); |
| 105 | |
| 106 | surface = av_malloc(sizeof(*surface)); |
| 107 | if (!surface) |
| 108 | return AVERROR(ENOMEM); |
| 109 | |
| 110 | frame->buf[0] = av_buffer_create((uint8_t*)surface, sizeof(*surface), |
| 111 | vdpau_release_buffer, ctx, |
| 112 | AV_BUFFER_FLAG_READONLY); |
| 113 | if (!frame->buf[0]) { |
| 114 | av_freep(&surface); |
| 115 | return AVERROR(ENOMEM); |
| 116 | } |
| 117 | |
| 118 | // properly we should keep a pool of surfaces instead of creating |
| 119 | // them anew for each frame, but since we don't care about speed |
| 120 | // much in this code, we don't bother |
| 121 | err = ctx->video_surface_create(ctx->device, VDP_CHROMA_TYPE_420, |
| 122 | frame->width, frame->height, surface); |
| 123 | if (err != VDP_STATUS_OK) { |
| 124 | av_log(NULL, AV_LOG_ERROR, "Error allocating a VDPAU video surface: %s\n", |
| 125 | ctx->get_error_string(err)); |
| 126 | av_buffer_unref(&frame->buf[0]); |
| 127 | return AVERROR_UNKNOWN; |
| 128 | } |
| 129 | |
| 130 | frame->data[3] = (uint8_t*)(uintptr_t)*surface; |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame) |
| 136 | { |
| 137 | VdpVideoSurface surface = (VdpVideoSurface)(uintptr_t)frame->data[3]; |
| 138 | InputStream *ist = s->opaque; |
| 139 | VDPAUContext *ctx = ist->hwaccel_ctx; |
| 140 | VdpStatus err; |
| 141 | int ret, chroma_type; |
| 142 | |
| 143 | err = ctx->video_surface_get_parameters(surface, &chroma_type, |
| 144 | &ctx->tmp_frame->width, |
| 145 | &ctx->tmp_frame->height); |
| 146 | if (err != VDP_STATUS_OK) { |
| 147 | av_log(NULL, AV_LOG_ERROR, "Error getting surface parameters: %s\n", |
| 148 | ctx->get_error_string(err)); |
| 149 | return AVERROR_UNKNOWN; |
| 150 | } |
| 151 | ctx->tmp_frame->format = ctx->pix_fmt; |
| 152 | |
| 153 | ret = av_frame_get_buffer(ctx->tmp_frame, 32); |
| 154 | if (ret < 0) |
| 155 | return ret; |
| 156 | |
| 157 | ctx->tmp_frame->width = frame->width; |
| 158 | ctx->tmp_frame->height = frame->height; |
| 159 | |
| 160 | err = ctx->video_surface_get_bits(surface, ctx->vdpau_format, |
| 161 | (void * const *)ctx->tmp_frame->data, |
| 162 | ctx->tmp_frame->linesize); |
| 163 | if (err != VDP_STATUS_OK) { |
| 164 | av_log(NULL, AV_LOG_ERROR, "Error retrieving frame data from VDPAU: %s\n", |
| 165 | ctx->get_error_string(err)); |
| 166 | ret = AVERROR_UNKNOWN; |
| 167 | goto fail; |
| 168 | } |
| 169 | |
| 170 | if (ctx->vdpau_format == VDP_YCBCR_FORMAT_YV12) |
| 171 | FFSWAP(uint8_t*, ctx->tmp_frame->data[1], ctx->tmp_frame->data[2]); |
| 172 | |
| 173 | ret = av_frame_copy_props(ctx->tmp_frame, frame); |
| 174 | if (ret < 0) |
| 175 | goto fail; |
| 176 | |
| 177 | av_frame_unref(frame); |
| 178 | av_frame_move_ref(frame, ctx->tmp_frame); |
| 179 | return 0; |
| 180 | |
| 181 | fail: |
| 182 | av_frame_unref(ctx->tmp_frame); |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | static const int vdpau_formats[][2] = { |
| 187 | { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV420P }, |
| 188 | { VDP_YCBCR_FORMAT_NV12, AV_PIX_FMT_NV12 }, |
| 189 | { VDP_YCBCR_FORMAT_YUYV, AV_PIX_FMT_YUYV422 }, |
| 190 | { VDP_YCBCR_FORMAT_UYVY, AV_PIX_FMT_UYVY422 }, |
| 191 | }; |
| 192 | |
| 193 | static int vdpau_alloc(AVCodecContext *s) |
| 194 | { |
| 195 | InputStream *ist = s->opaque; |
| 196 | int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR; |
| 197 | AVVDPAUContext *vdpau_ctx; |
| 198 | VDPAUContext *ctx; |
| 199 | const char *display, *vendor; |
| 200 | VdpStatus err; |
| 201 | int i; |
| 202 | |
| 203 | ctx = av_mallocz(sizeof(*ctx)); |
| 204 | if (!ctx) |
| 205 | return AVERROR(ENOMEM); |
| 206 | |
| 207 | ist->hwaccel_ctx = ctx; |
| 208 | ist->hwaccel_uninit = vdpau_uninit; |
| 209 | ist->hwaccel_get_buffer = vdpau_get_buffer; |
| 210 | ist->hwaccel_retrieve_data = vdpau_retrieve_data; |
| 211 | |
| 212 | ctx->tmp_frame = av_frame_alloc(); |
| 213 | if (!ctx->tmp_frame) |
| 214 | goto fail; |
| 215 | |
| 216 | ctx->dpy = XOpenDisplay(ist->hwaccel_device); |
| 217 | if (!ctx->dpy) { |
| 218 | av_log(NULL, loglevel, "Cannot open the X11 display %s.\n", |
| 219 | XDisplayName(ist->hwaccel_device)); |
| 220 | goto fail; |
| 221 | } |
| 222 | display = XDisplayString(ctx->dpy); |
| 223 | |
| 224 | err = vdp_device_create_x11(ctx->dpy, XDefaultScreen(ctx->dpy), &ctx->device, |
| 225 | &ctx->get_proc_address); |
| 226 | if (err != VDP_STATUS_OK) { |
| 227 | av_log(NULL, loglevel, "VDPAU device creation on X11 display %s failed.\n", |
| 228 | display); |
| 229 | goto fail; |
| 230 | } |
| 231 | |
| 232 | #define GET_CALLBACK(id, result) \ |
| 233 | do { \ |
| 234 | void *tmp; \ |
| 235 | err = ctx->get_proc_address(ctx->device, id, &tmp); \ |
| 236 | if (err != VDP_STATUS_OK) { \ |
| 237 | av_log(NULL, loglevel, "Error getting the " #id " callback.\n"); \ |
| 238 | goto fail; \ |
| 239 | } \ |
| 240 | ctx->result = tmp; \ |
| 241 | } while (0) |
| 242 | |
| 243 | GET_CALLBACK(VDP_FUNC_ID_GET_ERROR_STRING, get_error_string); |
| 244 | GET_CALLBACK(VDP_FUNC_ID_GET_INFORMATION_STRING, get_information_string); |
| 245 | GET_CALLBACK(VDP_FUNC_ID_DEVICE_DESTROY, device_destroy); |
| 246 | if (vdpau_api_ver == 1) { |
| 247 | GET_CALLBACK(VDP_FUNC_ID_DECODER_CREATE, decoder_create); |
| 248 | GET_CALLBACK(VDP_FUNC_ID_DECODER_DESTROY, decoder_destroy); |
| 249 | GET_CALLBACK(VDP_FUNC_ID_DECODER_RENDER, decoder_render); |
| 250 | } |
| 251 | GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_CREATE, video_surface_create); |
| 252 | GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, video_surface_destroy); |
| 253 | GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR, video_surface_get_bits); |
| 254 | GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_PARAMETERS, video_surface_get_parameters); |
| 255 | GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_QUERY_GET_PUT_BITS_Y_CB_CR_CAPABILITIES, |
| 256 | video_surface_query); |
| 257 | |
| 258 | for (i = 0; i < FF_ARRAY_ELEMS(vdpau_formats); i++) { |
| 259 | VdpBool supported; |
| 260 | err = ctx->video_surface_query(ctx->device, VDP_CHROMA_TYPE_420, |
| 261 | vdpau_formats[i][0], &supported); |
| 262 | if (err != VDP_STATUS_OK) { |
| 263 | av_log(NULL, loglevel, |
| 264 | "Error querying VDPAU surface capabilities: %s\n", |
| 265 | ctx->get_error_string(err)); |
| 266 | goto fail; |
| 267 | } |
| 268 | if (supported) |
| 269 | break; |
| 270 | } |
| 271 | if (i == FF_ARRAY_ELEMS(vdpau_formats)) { |
| 272 | av_log(NULL, loglevel, |
| 273 | "No supported VDPAU format for retrieving the data.\n"); |
| 274 | return AVERROR(EINVAL); |
| 275 | } |
| 276 | ctx->vdpau_format = vdpau_formats[i][0]; |
| 277 | ctx->pix_fmt = vdpau_formats[i][1]; |
| 278 | |
| 279 | if (vdpau_api_ver == 1) { |
| 280 | vdpau_ctx = av_vdpau_alloc_context(); |
| 281 | if (!vdpau_ctx) |
| 282 | goto fail; |
| 283 | vdpau_ctx->render = ctx->decoder_render; |
| 284 | |
| 285 | s->hwaccel_context = vdpau_ctx; |
| 286 | } else |
| 287 | if (av_vdpau_bind_context(s, ctx->device, ctx->get_proc_address, 0)) |
| 288 | goto fail; |
| 289 | |
| 290 | ctx->get_information_string(&vendor); |
| 291 | av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU -- %s -- on X11 display %s, " |
| 292 | "to decode input stream #%d:%d.\n", vendor, |
| 293 | display, ist->file_index, ist->st->index); |
| 294 | |
| 295 | return 0; |
| 296 | |
| 297 | fail: |
| 298 | av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n", |
| 299 | ist->file_index, ist->st->index); |
| 300 | vdpau_uninit(s); |
| 301 | return AVERROR(EINVAL); |
| 302 | } |
| 303 | |
| 304 | static int vdpau_old_init(AVCodecContext *s) |
| 305 | { |
| 306 | InputStream *ist = s->opaque; |
| 307 | int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR; |
| 308 | AVVDPAUContext *vdpau_ctx; |
| 309 | VDPAUContext *ctx; |
| 310 | VdpStatus err; |
| 311 | int profile, ret; |
| 312 | |
| 313 | if (!ist->hwaccel_ctx) { |
| 314 | ret = vdpau_alloc(s); |
| 315 | if (ret < 0) |
| 316 | return ret; |
| 317 | } |
| 318 | ctx = ist->hwaccel_ctx; |
| 319 | vdpau_ctx = s->hwaccel_context; |
| 320 | |
| 321 | ret = av_vdpau_get_profile(s, &profile); |
| 322 | if (ret < 0) { |
| 323 | av_log(NULL, loglevel, "No known VDPAU decoder profile for this stream.\n"); |
| 324 | return AVERROR(EINVAL); |
| 325 | } |
| 326 | |
| 327 | if (ctx->decoder) |
| 328 | ctx->decoder_destroy(ctx->decoder); |
| 329 | |
| 330 | err = ctx->decoder_create(ctx->device, profile, |
| 331 | s->coded_width, s->coded_height, |
| 332 | 16, &ctx->decoder); |
| 333 | if (err != VDP_STATUS_OK) { |
| 334 | av_log(NULL, loglevel, "Error creating the VDPAU decoder: %s\n", |
| 335 | ctx->get_error_string(err)); |
| 336 | return AVERROR_UNKNOWN; |
| 337 | } |
| 338 | |
| 339 | vdpau_ctx->decoder = ctx->decoder; |
| 340 | |
| 341 | ist->hwaccel_get_buffer = vdpau_get_buffer; |
| 342 | ist->hwaccel_retrieve_data = vdpau_retrieve_data; |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | int vdpau_init(AVCodecContext *s) |
| 348 | { |
| 349 | InputStream *ist = s->opaque; |
| 350 | |
| 351 | if (vdpau_api_ver == 1) |
| 352 | return vdpau_old_init(s); |
| 353 | |
| 354 | if (!ist->hwaccel_ctx) { |
| 355 | int ret = vdpau_alloc(s); |
| 356 | if (ret < 0) |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | ist->hwaccel_get_buffer = vdpau_get_buffer; |
| 361 | ist->hwaccel_retrieve_data = vdpau_retrieve_data; |
| 362 | |
| 363 | return 0; |
| 364 | } |