1 /*
2 * Copyright (c) 2015 Anton Khirnov
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 /**
24 * @file
25 * Intel QSV-accelerated H.264 decoding example.
26 *
27 * @example qsvdec.c
28 * This example shows how to do QSV-accelerated H.264 decoding with output
29 * frames in the VA-API video surfaces.
30 */
31
32 #include "config.h"
33
34 #include <stdio.h>
35
36 #include <mfx/mfxvideo.h>
37
38 #include <va/va.h>
39 #include <va/va_x11.h>
40 #include <X11/Xlib.h>
41
44
47
50
54
59
62
63 static mfxStatus
frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
64 mfxFrameAllocResponse *resp)
65 {
67 int err, i;
68
70 fprintf(stderr, "Multiple allocation requests.\n");
71 return MFX_ERR_MEMORY_ALLOC;
72 }
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;
76 }
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;
82 }
83
88 goto fail;
89
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");
96 goto fail;
97 }
99
102
105
107
108 return MFX_ERR_NONE;
109 fail:
113
114 return MFX_ERR_MEMORY_ALLOC;
115 }
116
117 static mfxStatus
frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
118 {
120
127
128 return MFX_ERR_NONE;
129 }
130
131 static mfxStatus
frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
132 {
133 return MFX_ERR_UNSUPPORTED;
134 }
135
136 static mfxStatus
frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
137 {
138 return MFX_ERR_UNSUPPORTED;
139 }
140
142 {
143 *hdl = mid;
144 return MFX_ERR_NONE;
145 }
146
148 {
149 int *used = opaque;
150 *used = 0;
152 }
153
155 {
157
158 mfxFrameSurface1 *surf;
160 int idx;
161
164 break;
165 }
167 fprintf(stderr, "No free surfaces\n");
169 }
170
172 if (!surf)
176 if (!surf_buf) {
179 }
180
182 surf->Data.MemId = &decode->
surfaces[idx];
183
184 frame->
buf[0] = surf_buf;
186
188
189 return 0;
190 }
191
193 {
199 if (!qsv)
201
203 qsv->
iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
204
206 }
207
209 }
210
211 pix_fmts++;
212 }
213
214 fprintf(stderr, "The QSV pixel format not offered in get_format()\n");
215
217 }
218
222 {
224 int got_frame = 1;
225
226 while (pkt->
size > 0 || (!pkt->
data && got_frame)) {
228 if (ret < 0) {
229 fprintf(stderr, "Error during decoding\n");
231 }
232
235
236 /* A real program would do something useful with the decoded frame here.
237 * We just retrieve the raw data and write it to a file, which is rather
238 * useless but pedagogic. */
239 if (got_frame) {
240 mfxFrameSurface1 *surf = (mfxFrameSurface1*)frame->
data[3];
241 VASurfaceID surface = *(VASurfaceID*)surf->Data.MemId;
242
243 VAImageFormat img_fmt = {
244 .fourcc = VA_FOURCC_NV12,
245 .byte_order = VA_LSB_FIRST,
246 .bits_per_pixel = 8,
247 .depth = 8,
248 };
249
251
252 VAStatus err;
254 int i, j;
255
256 img.buf = VA_INVALID_ID;
257 img.image_id = VA_INVALID_ID;
258
259 err = vaCreateImage(decode->
va_dpy, &img_fmt,
261 if (err != VA_STATUS_SUCCESS) {
262 fprintf(stderr, "Error creating an image: %s\n",
263 vaErrorStr(err));
265 goto fail;
266 }
267
268 err = vaGetImage(decode->
va_dpy, surface, 0, 0,
270 img.image_id);
271 if (err != VA_STATUS_SUCCESS) {
272 fprintf(stderr, "Error getting an image: %s\n",
273 vaErrorStr(err));
275 goto fail;
276 }
277
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",
281 vaErrorStr(err));
283 goto fail;
284 }
285
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);
289
290 fail:
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);
296
297 if (ret < 0)
299 }
300 }
301
302 return 0;
303 }
304
305 int main(
int argc,
char **argv)
306 {
311
314
316
318 int va_ver_major, va_ver_minor;
319
320 mfxIMPL mfx_impl = MFX_IMPL_AUTO_ANY;
321 mfxVersion mfx_ver = { { 1, 1 } };
322
323 mfxFrameAllocator frame_allocator = {
330 };
331
333
335
337
338 if (argc < 3) {
339 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
340 return 1;
341 }
342
343 /* open the input file */
345 if (ret < 0) {
346 fprintf(stderr, "Cannot open input file '%s': ", argv[1]);
348 }
349
350 /* find the first H.264 video stream */
353
355 video_st = st;
356 else
358 }
359 if (!video_st) {
360 fprintf(stderr, "No H.264 video stream in the input file\n");
362 }
363
364 /* initialize VA-API */
365 dpy = XOpenDisplay(
NULL);
366 if (!dpy) {
367 fprintf(stderr, "Cannot open the X display\n");
369 }
370 decode.
va_dpy = vaGetDisplay(dpy);
372 fprintf(stderr, "Cannot open the VA display\n");
374 }
375
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));
380 }
381 fprintf(stderr, "Initialized VA v%d.%d\n", va_ver_major, va_ver_minor);
382
383 /* initialize an MFX session */
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");
388 }
389
390 MFXVideoCORE_SetHandle(decode.
mfx_session, MFX_HANDLE_VA_DISPLAY, decode.
va_dpy);
391 MFXVideoCORE_SetFrameAllocator(decode.
mfx_session, &frame_allocator);
392
393 /* initialize the decoder */
395 if (!decoder) {
396 fprintf(stderr, "The QSV decoder is not present in libavcodec\n");
398 }
399
401 if (!decoder_ctx) {
404 }
412 }
416 }
418
422
424 if (ret < 0) {
425 fprintf(stderr, "Error opening the decoder: ");
427 }
428
429 /* open the output stream */
431 if (ret < 0) {
432 fprintf(stderr, "Error opening the output context: ");
434 }
435
437 if (!frame) {
440 }
441
442 /* actual decoding */
443 while (ret >= 0) {
445 if (ret < 0)
446 break;
447
449 ret =
decode_packet(&decode, decoder_ctx, frame, &pkt, output_ctx);
450
452 }
453
454 /* flush the decoder */
457 ret =
decode_packet(&decode, decoder_ctx, frame, &pkt, output_ctx);
458
460 if (ret < 0) {
463 fprintf(stderr, "%s\n", buf);
464 }
465
467
469
473 vaTerminate(decode.
va_dpy);
474 if (dpy)
475 XCloseDisplay(dpy);
476
477 if (decoder_ctx)
480
482
484 }