2 * Copyright (c) 2015 Anton Khirnov
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
25 * Intel QSV-accelerated H.264 decoding example.
28 * This example shows how to do QSV-accelerated H.264 decoding with output
29 * frames in the VA-API video surfaces.
36 #include <mfx/mfxvideo.h>
39 #include <va/va_x11.h>
42 #include "libavformat/avformat.h"
43 #include "libavformat/avio.h"
45 #include "libavcodec/avcodec.h"
46 #include "libavcodec/qsv.h"
48 #include "libavutil/error.h"
49 #include "libavutil/mem.h"
51 typedef struct DecodeContext {
52 mfxSession mfx_session;
55 VASurfaceID *surfaces;
56 mfxMemId *surface_ids;
60 mfxFrameInfo frame_info;
63 static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
64 mfxFrameAllocResponse *resp)
66 DecodeContext *decode = pthis;
69 if (decode->surfaces) {
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;
84 decode->surfaces = av_malloc_array (req->NumFrameSuggested, sizeof(*decode->surfaces));
85 decode->surface_ids = av_malloc_array (req->NumFrameSuggested, sizeof(*decode->surface_ids));
86 decode->surface_used = av_mallocz_array(req->NumFrameSuggested, sizeof(*decode->surface_used));
87 if (!decode->surfaces || !decode->surface_ids || !decode->surface_used)
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");
98 decode->nb_surfaces = req->NumFrameSuggested;
100 for (i = 0; i < decode->nb_surfaces; i++)
101 decode->surface_ids[i] = &decode->surfaces[i];
103 resp->mids = decode->surface_ids;
104 resp->NumFrameActual = decode->nb_surfaces;
106 decode->frame_info = req->Info;
110 av_freep(&decode->surfaces);
111 av_freep(&decode->surface_ids);
112 av_freep(&decode->surface_used);
114 return MFX_ERR_MEMORY_ALLOC;
117 static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
122 static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
124 return MFX_ERR_UNSUPPORTED;
127 static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
129 return MFX_ERR_UNSUPPORTED;
132 static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
138 static void free_surfaces(DecodeContext *decode)
140 if (decode->surfaces)
141 vaDestroySurfaces(decode->va_dpy, decode->surfaces, decode->nb_surfaces);
142 av_freep(&decode->surfaces);
143 av_freep(&decode->surface_ids);
144 av_freep(&decode->surface_used);
145 decode->nb_surfaces = 0;
148 static void free_buffer(void *opaque, uint8_t *data)
155 static int get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
157 DecodeContext *decode = avctx->opaque;
159 mfxFrameSurface1 *surf;
160 AVBufferRef *surf_buf;
163 for (idx = 0; idx < decode->nb_surfaces; idx++) {
164 if (!decode->surface_used[idx])
167 if (idx == decode->nb_surfaces) {
168 fprintf(stderr, "No free surfaces\n");
169 return AVERROR(ENOMEM);
172 surf = av_mallocz(sizeof(*surf));
174 return AVERROR(ENOMEM);
175 surf_buf = av_buffer_create((uint8_t*)surf, sizeof(*surf), free_buffer,
176 &decode->surface_used[idx], AV_BUFFER_FLAG_READONLY);
179 return AVERROR(ENOMEM);
182 surf->Info = decode->frame_info;
183 surf->Data.MemId = &decode->surfaces[idx];
185 frame->buf[0] = surf_buf;
186 frame->data[3] = (uint8_t*)surf;
188 decode->surface_used[idx] = 1;
193 static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts)
195 while (*pix_fmts != AV_PIX_FMT_NONE) {
196 if (*pix_fmts == AV_PIX_FMT_QSV) {
197 if (!avctx->hwaccel_context) {
198 DecodeContext *decode = avctx->opaque;
199 AVQSVContext *qsv = av_qsv_alloc_context();
201 return AV_PIX_FMT_NONE;
203 qsv->session = decode->mfx_session;
204 qsv->iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
206 avctx->hwaccel_context = qsv;
209 return AV_PIX_FMT_QSV;
215 fprintf(stderr, "The QSV pixel format not offered in get_format()\n");
217 return AV_PIX_FMT_NONE;
220 static int decode_packet(DecodeContext *decode, AVCodecContext *decoder_ctx,
221 AVFrame *frame, AVPacket *pkt,
222 AVIOContext *output_ctx)
227 while (pkt->size > 0 || (!pkt->data && got_frame)) {
228 ret = avcodec_decode_video2(decoder_ctx, frame, &got_frame, pkt);
230 fprintf(stderr, "Error during decoding\n");
237 /* A real program would do something useful with the decoded frame here.
238 * We just retrieve the raw data and write it to a file, which is rather
239 * useless but pedagogic. */
241 mfxFrameSurface1 *surf = (mfxFrameSurface1*)frame->data[3];
242 VASurfaceID surface = *(VASurfaceID*)surf->Data.MemId;
244 VAImageFormat img_fmt = {
245 .fourcc = VA_FOURCC_NV12,
246 .byte_order = VA_LSB_FIRST,
257 img.buf = VA_INVALID_ID;
258 img.image_id = VA_INVALID_ID;
260 err = vaCreateImage(decode->va_dpy, &img_fmt,
261 frame->width, frame->height, &img);
262 if (err != VA_STATUS_SUCCESS) {
263 fprintf(stderr, "Error creating an image: %s\n",
265 ret = AVERROR_UNKNOWN;
269 err = vaGetImage(decode->va_dpy, surface, 0, 0,
270 frame->width, frame->height,
272 if (err != VA_STATUS_SUCCESS) {
273 fprintf(stderr, "Error getting an image: %s\n",
275 ret = AVERROR_UNKNOWN;
279 err = vaMapBuffer(decode->va_dpy, img.buf, (void**)&data);
280 if (err != VA_STATUS_SUCCESS) {
281 fprintf(stderr, "Error mapping the image buffer: %s\n",
283 ret = AVERROR_UNKNOWN;
287 for (i = 0; i < img.num_planes; i++)
288 for (j = 0; j < (img.height >> (i > 0)); j++)
289 avio_write(output_ctx, data + img.offsets[i] + j * img.pitches[i], img.width);
292 if (img.buf != VA_INVALID_ID)
293 vaUnmapBuffer(decode->va_dpy, img.buf);
294 if (img.image_id != VA_INVALID_ID)
295 vaDestroyImage(decode->va_dpy, img.image_id);
296 av_frame_unref(frame);
306 int main(int argc, char **argv)
308 AVFormatContext *input_ctx = NULL;
309 AVStream *video_st = NULL;
310 AVCodecContext *decoder_ctx = NULL;
311 const AVCodec *decoder;
313 AVPacket pkt = { 0 };
314 AVFrame *frame = NULL;
316 DecodeContext decode = { NULL };
319 int va_ver_major, va_ver_minor;
321 mfxIMPL mfx_impl = MFX_IMPL_AUTO_ANY;
322 mfxVersion mfx_ver = { { 1, 1 } };
324 mfxFrameAllocator frame_allocator = {
326 .Alloc = frame_alloc,
328 .Unlock = frame_unlock,
329 .GetHDL = frame_get_hdl,
333 AVIOContext *output_ctx = NULL;
340 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
344 /* open the input file */
345 ret = avformat_open_input(&input_ctx, argv[1], NULL, NULL);
347 fprintf(stderr, "Cannot open input file '%s': ", argv[1]);
351 /* find the first H.264 video stream */
352 for (i = 0; i < input_ctx->nb_streams; i++) {
353 AVStream *st = input_ctx->streams[i];
355 if (st->codecpar->codec_id == AV_CODEC_ID_H264 && !video_st)
358 st->discard = AVDISCARD_ALL;
361 fprintf(stderr, "No H.264 video stream in the input file\n");
365 /* initialize VA-API */
366 dpy = XOpenDisplay(NULL);
368 fprintf(stderr, "Cannot open the X display\n");
371 decode.va_dpy = vaGetDisplay(dpy);
372 if (!decode.va_dpy) {
373 fprintf(stderr, "Cannot open the VA display\n");
377 err = vaInitialize(decode.va_dpy, &va_ver_major, &va_ver_minor);
378 if (err != VA_STATUS_SUCCESS) {
379 fprintf(stderr, "Cannot initialize VA: %s\n", vaErrorStr(err));
382 fprintf(stderr, "Initialized VA v%d.%d\n", va_ver_major, va_ver_minor);
384 /* initialize an MFX session */
385 err = MFXInit(mfx_impl, &mfx_ver, &decode.mfx_session);
386 if (err != MFX_ERR_NONE) {
387 fprintf(stderr, "Error initializing an MFX session\n");
391 MFXVideoCORE_SetHandle(decode.mfx_session, MFX_HANDLE_VA_DISPLAY, decode.va_dpy);
392 MFXVideoCORE_SetFrameAllocator(decode.mfx_session, &frame_allocator);
394 /* initialize the decoder */
395 decoder = avcodec_find_decoder_by_name("h264_qsv");
397 fprintf(stderr, "The QSV decoder is not present in libavcodec\n");
401 decoder_ctx = avcodec_alloc_context3(decoder);
403 ret = AVERROR(ENOMEM);
406 decoder_ctx->codec_id = AV_CODEC_ID_H264;
407 if (video_st->codecpar->extradata_size) {
408 decoder_ctx->extradata = av_mallocz(video_st->codecpar->extradata_size +
409 AV_INPUT_BUFFER_PADDING_SIZE);
410 if (!decoder_ctx->extradata) {
411 ret = AVERROR(ENOMEM);
414 memcpy(decoder_ctx->extradata, video_st->codecpar->extradata,
415 video_st->codecpar->extradata_size);
416 decoder_ctx->extradata_size = video_st->codecpar->extradata_size;
418 decoder_ctx->refcounted_frames = 1;
420 decoder_ctx->opaque = &decode;
421 decoder_ctx->get_buffer2 = get_buffer;
422 decoder_ctx->get_format = get_format;
424 ret = avcodec_open2(decoder_ctx, NULL, NULL);
426 fprintf(stderr, "Error opening the decoder: ");
430 /* open the output stream */
431 ret = avio_open(&output_ctx, argv[2], AVIO_FLAG_WRITE);
433 fprintf(stderr, "Error opening the output context: ");
437 frame = av_frame_alloc();
439 ret = AVERROR(ENOMEM);
443 /* actual decoding */
445 ret = av_read_frame(input_ctx, &pkt);
449 if (pkt.stream_index == video_st->index)
450 ret = decode_packet(&decode, decoder_ctx, frame, &pkt, output_ctx);
452 av_packet_unref(&pkt);
455 /* flush the decoder */
458 ret = decode_packet(&decode, decoder_ctx, frame, &pkt, output_ctx);
463 av_strerror(ret, buf, sizeof(buf));
464 fprintf(stderr, "%s\n", buf);
467 avformat_close_input(&input_ctx);
469 av_frame_free(&frame);
472 av_freep(&decoder_ctx->hwaccel_context);
473 avcodec_free_context(&decoder_ctx);
475 free_surfaces(&decode);
477 if (decode.mfx_session)
478 MFXClose(decode.mfx_session);
480 vaTerminate(decode.va_dpy);
484 avio_close(output_ctx);