36 #include <mfx/mfxvideo.h>
39 #include <va/va_x11.h>
63 static mfxStatus
frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
64 mfxFrameAllocResponse *resp)
70 fprintf(stderr,
"Multiple allocation requests.\n");
71 return MFX_ERR_MEMORY_ALLOC;
73 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)) {
74 fprintf(stderr,
"Unsupported surface type: %d\n", req->Type);
75 return MFX_ERR_UNSUPPORTED;
77 if (req->Info.BitDepthLuma != 8 || req->Info.BitDepthChroma != 8 ||
78 req->Info.Shift || req->Info.FourCC != MFX_FOURCC_NV12 ||
79 req->Info.ChromaFormat != MFX_CHROMAFORMAT_YUV420) {
80 fprintf(stderr,
"Unsupported surface properties.\n");
81 return MFX_ERR_UNSUPPORTED;
90 err = vaCreateSurfaces(decode->
va_dpy, VA_RT_FORMAT_YUV420,
91 req->Info.Width, req->Info.Height,
92 decode->
surfaces, req->NumFrameSuggested,
94 if (err != VA_STATUS_SUCCESS) {
95 fprintf(stderr,
"Error allocating VA surfaces\n");
114 return MFX_ERR_MEMORY_ALLOC;
117 static mfxStatus
frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
131 static mfxStatus
frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
133 return MFX_ERR_UNSUPPORTED;
136 static mfxStatus
frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
138 return MFX_ERR_UNSUPPORTED;
158 mfxFrameSurface1 *surf;
167 fprintf(stderr,
"No free surfaces\n");
182 surf->Data.MemId = &decode->
surfaces[idx];
184 frame->
buf[0] = surf_buf;
203 qsv->
iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
214 fprintf(stderr,
"The QSV pixel format not offered in get_format()\n");
226 while (pkt->
size > 0 || (!pkt->
data && got_frame)) {
229 fprintf(stderr,
"Error during decoding\n");
240 mfxFrameSurface1 *surf = (mfxFrameSurface1*)frame->
data[3];
241 VASurfaceID surface = *(VASurfaceID*)surf->Data.MemId;
243 VAImageFormat img_fmt = {
244 .fourcc = VA_FOURCC_NV12,
245 .byte_order = VA_LSB_FIRST,
256 img.buf = VA_INVALID_ID;
257 img.image_id = VA_INVALID_ID;
259 err = vaCreateImage(decode->
va_dpy, &img_fmt,
261 if (err != VA_STATUS_SUCCESS) {
262 fprintf(stderr,
"Error creating an image: %s\n",
268 err = vaGetImage(decode->
va_dpy, surface, 0, 0,
271 if (err != VA_STATUS_SUCCESS) {
272 fprintf(stderr,
"Error getting an image: %s\n",
278 err = vaMapBuffer(decode->
va_dpy, img.buf, (
void**)&data);
279 if (err != VA_STATUS_SUCCESS) {
280 fprintf(stderr,
"Error mapping the image buffer: %s\n",
286 for (i = 0; i < img.num_planes; i++)
287 for (j = 0; j < (img.height >> (i > 0)); j++)
288 avio_write(output_ctx, data + img.offsets[i] + j * img.pitches[i], img.width);
291 if (img.buf != VA_INVALID_ID)
292 vaUnmapBuffer(decode->
va_dpy, img.buf);
293 if (img.image_id != VA_INVALID_ID)
294 vaDestroyImage(decode->
va_dpy, img.image_id);
305 int main(
int argc,
char **argv)
318 int va_ver_major, va_ver_minor;
320 mfxIMPL mfx_impl = MFX_IMPL_AUTO_ANY;
321 mfxVersion mfx_ver = { { 1, 1 } };
323 mfxFrameAllocator frame_allocator = {
339 fprintf(stderr,
"Usage: %s <input file> <output file>\n", argv[0]);
346 fprintf(stderr,
"Cannot open input file '%s': ", argv[1]);
360 fprintf(stderr,
"No H.264 video stream in the input file\n");
365 dpy = XOpenDisplay(
NULL);
367 fprintf(stderr,
"Cannot open the X display\n");
370 decode.
va_dpy = vaGetDisplay(dpy);
372 fprintf(stderr,
"Cannot open the VA display\n");
376 err = vaInitialize(decode.
va_dpy, &va_ver_major, &va_ver_minor);
377 if (err != VA_STATUS_SUCCESS) {
378 fprintf(stderr,
"Cannot initialize VA: %s\n", vaErrorStr(err));
381 fprintf(stderr,
"Initialized VA v%d.%d\n", va_ver_major, va_ver_minor);
384 err = MFXInit(mfx_impl, &mfx_ver, &decode.
mfx_session);
385 if (err != MFX_ERR_NONE) {
386 fprintf(stderr,
"Error initializing an MFX session\n");
390 MFXVideoCORE_SetHandle(decode.
mfx_session, MFX_HANDLE_VA_DISPLAY, decode.
va_dpy);
391 MFXVideoCORE_SetFrameAllocator(decode.
mfx_session, &frame_allocator);
396 fprintf(stderr,
"The QSV decoder is not present in libavcodec\n");
425 fprintf(stderr,
"Error opening the decoder: ");
432 fprintf(stderr,
"Error opening the output context: ");
449 ret =
decode_packet(&decode, decoder_ctx, frame, &pkt, output_ctx);
457 ret =
decode_packet(&decode, decoder_ctx, frame, &pkt, output_ctx);
463 fprintf(stderr,
"%s\n", buf);
473 vaTerminate(decode.
va_dpy);