You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

485 lines
14KB

  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. * @file
  24. * Intel QSV-accelerated H.264 decoding example.
  25. *
  26. * @example qsvdec.c
  27. * This example shows how to do QSV-accelerated H.264 decoding with output
  28. * frames in the VA-API video surfaces.
  29. */
  30. #include "config.h"
  31. #include <stdio.h>
  32. #include <mfx/mfxvideo.h>
  33. #include <va/va.h>
  34. #include <va/va_x11.h>
  35. #include <X11/Xlib.h>
  36. #include "libavformat/avformat.h"
  37. #include "libavformat/avio.h"
  38. #include "libavcodec/avcodec.h"
  39. #include "libavcodec/qsv.h"
  40. #include "libavutil/error.h"
  41. #include "libavutil/mem.h"
  42. typedef struct DecodeContext {
  43. mfxSession mfx_session;
  44. VADisplay va_dpy;
  45. VASurfaceID *surfaces;
  46. mfxMemId *surface_ids;
  47. int *surface_used;
  48. int nb_surfaces;
  49. mfxFrameInfo frame_info;
  50. } DecodeContext;
  51. static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
  52. mfxFrameAllocResponse *resp)
  53. {
  54. DecodeContext *decode = pthis;
  55. int err, i;
  56. if (decode->surfaces) {
  57. fprintf(stderr, "Multiple allocation requests.\n");
  58. return MFX_ERR_MEMORY_ALLOC;
  59. }
  60. if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)) {
  61. fprintf(stderr, "Unsupported surface type: %d\n", req->Type);
  62. return MFX_ERR_UNSUPPORTED;
  63. }
  64. if (req->Info.BitDepthLuma != 8 || req->Info.BitDepthChroma != 8 ||
  65. req->Info.Shift || req->Info.FourCC != MFX_FOURCC_NV12 ||
  66. req->Info.ChromaFormat != MFX_CHROMAFORMAT_YUV420) {
  67. fprintf(stderr, "Unsupported surface properties.\n");
  68. return MFX_ERR_UNSUPPORTED;
  69. }
  70. decode->surfaces = av_malloc_array (req->NumFrameSuggested, sizeof(*decode->surfaces));
  71. decode->surface_ids = av_malloc_array (req->NumFrameSuggested, sizeof(*decode->surface_ids));
  72. decode->surface_used = av_mallocz_array(req->NumFrameSuggested, sizeof(*decode->surface_used));
  73. if (!decode->surfaces || !decode->surface_ids || !decode->surface_used)
  74. goto fail;
  75. err = vaCreateSurfaces(decode->va_dpy, VA_RT_FORMAT_YUV420,
  76. req->Info.Width, req->Info.Height,
  77. decode->surfaces, req->NumFrameSuggested,
  78. NULL, 0);
  79. if (err != VA_STATUS_SUCCESS) {
  80. fprintf(stderr, "Error allocating VA surfaces\n");
  81. goto fail;
  82. }
  83. decode->nb_surfaces = req->NumFrameSuggested;
  84. for (i = 0; i < decode->nb_surfaces; i++)
  85. decode->surface_ids[i] = &decode->surfaces[i];
  86. resp->mids = decode->surface_ids;
  87. resp->NumFrameActual = decode->nb_surfaces;
  88. decode->frame_info = req->Info;
  89. return MFX_ERR_NONE;
  90. fail:
  91. av_freep(&decode->surfaces);
  92. av_freep(&decode->surface_ids);
  93. av_freep(&decode->surface_used);
  94. return MFX_ERR_MEMORY_ALLOC;
  95. }
  96. static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
  97. {
  98. DecodeContext *decode = pthis;
  99. if (decode->surfaces)
  100. vaDestroySurfaces(decode->va_dpy, decode->surfaces, decode->nb_surfaces);
  101. av_freep(&decode->surfaces);
  102. av_freep(&decode->surface_ids);
  103. av_freep(&decode->surface_used);
  104. decode->nb_surfaces = 0;
  105. return MFX_ERR_NONE;
  106. }
  107. static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  108. {
  109. return MFX_ERR_UNSUPPORTED;
  110. }
  111. static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
  112. {
  113. return MFX_ERR_UNSUPPORTED;
  114. }
  115. static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
  116. {
  117. *hdl = mid;
  118. return MFX_ERR_NONE;
  119. }
  120. static void free_buffer(void *opaque, uint8_t *data)
  121. {
  122. int *used = opaque;
  123. *used = 0;
  124. av_freep(&data);
  125. }
  126. static int get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  127. {
  128. DecodeContext *decode = avctx->opaque;
  129. mfxFrameSurface1 *surf;
  130. AVBufferRef *surf_buf;
  131. int idx;
  132. for (idx = 0; idx < decode->nb_surfaces; idx++) {
  133. if (!decode->surface_used[idx])
  134. break;
  135. }
  136. if (idx == decode->nb_surfaces) {
  137. fprintf(stderr, "No free surfaces\n");
  138. return AVERROR(ENOMEM);
  139. }
  140. surf = av_mallocz(sizeof(*surf));
  141. if (!surf)
  142. return AVERROR(ENOMEM);
  143. surf_buf = av_buffer_create((uint8_t*)surf, sizeof(*surf), free_buffer,
  144. &decode->surface_used[idx], AV_BUFFER_FLAG_READONLY);
  145. if (!surf_buf) {
  146. av_freep(&surf);
  147. return AVERROR(ENOMEM);
  148. }
  149. surf->Info = decode->frame_info;
  150. surf->Data.MemId = &decode->surfaces[idx];
  151. frame->buf[0] = surf_buf;
  152. frame->data[3] = (uint8_t*)surf;
  153. decode->surface_used[idx] = 1;
  154. return 0;
  155. }
  156. static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts)
  157. {
  158. while (*pix_fmts != AV_PIX_FMT_NONE) {
  159. if (*pix_fmts == AV_PIX_FMT_QSV) {
  160. if (!avctx->hwaccel_context) {
  161. DecodeContext *decode = avctx->opaque;
  162. AVQSVContext *qsv = av_qsv_alloc_context();
  163. if (!qsv)
  164. return AV_PIX_FMT_NONE;
  165. qsv->session = decode->mfx_session;
  166. qsv->iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
  167. avctx->hwaccel_context = qsv;
  168. }
  169. return AV_PIX_FMT_QSV;
  170. }
  171. pix_fmts++;
  172. }
  173. fprintf(stderr, "The QSV pixel format not offered in get_format()\n");
  174. return AV_PIX_FMT_NONE;
  175. }
  176. static int decode_packet(DecodeContext *decode, AVCodecContext *decoder_ctx,
  177. AVFrame *frame, AVPacket *pkt,
  178. AVIOContext *output_ctx)
  179. {
  180. int ret = 0;
  181. int got_frame = 1;
  182. while (pkt->size > 0 || (!pkt->data && got_frame)) {
  183. ret = avcodec_decode_video2(decoder_ctx, frame, &got_frame, pkt);
  184. if (ret < 0) {
  185. fprintf(stderr, "Error during decoding\n");
  186. return ret;
  187. }
  188. pkt->data += ret;
  189. pkt->size -= ret;
  190. /* A real program would do something useful with the decoded frame here.
  191. * We just retrieve the raw data and write it to a file, which is rather
  192. * useless but pedagogic. */
  193. if (got_frame) {
  194. mfxFrameSurface1 *surf = (mfxFrameSurface1*)frame->data[3];
  195. VASurfaceID surface = *(VASurfaceID*)surf->Data.MemId;
  196. VAImageFormat img_fmt = {
  197. .fourcc = VA_FOURCC_NV12,
  198. .byte_order = VA_LSB_FIRST,
  199. .bits_per_pixel = 8,
  200. .depth = 8,
  201. };
  202. VAImage img;
  203. VAStatus err;
  204. uint8_t *data;
  205. int i, j;
  206. img.buf = VA_INVALID_ID;
  207. img.image_id = VA_INVALID_ID;
  208. err = vaCreateImage(decode->va_dpy, &img_fmt,
  209. frame->width, frame->height, &img);
  210. if (err != VA_STATUS_SUCCESS) {
  211. fprintf(stderr, "Error creating an image: %s\n",
  212. vaErrorStr(err));
  213. ret = AVERROR_UNKNOWN;
  214. goto fail;
  215. }
  216. err = vaGetImage(decode->va_dpy, surface, 0, 0,
  217. frame->width, frame->height,
  218. img.image_id);
  219. if (err != VA_STATUS_SUCCESS) {
  220. fprintf(stderr, "Error getting an image: %s\n",
  221. vaErrorStr(err));
  222. ret = AVERROR_UNKNOWN;
  223. goto fail;
  224. }
  225. err = vaMapBuffer(decode->va_dpy, img.buf, (void**)&data);
  226. if (err != VA_STATUS_SUCCESS) {
  227. fprintf(stderr, "Error mapping the image buffer: %s\n",
  228. vaErrorStr(err));
  229. ret = AVERROR_UNKNOWN;
  230. goto fail;
  231. }
  232. for (i = 0; i < img.num_planes; i++)
  233. for (j = 0; j < (img.height >> (i > 0)); j++)
  234. avio_write(output_ctx, data + img.offsets[i] + j * img.pitches[i], img.width);
  235. fail:
  236. if (img.buf != VA_INVALID_ID)
  237. vaUnmapBuffer(decode->va_dpy, img.buf);
  238. if (img.image_id != VA_INVALID_ID)
  239. vaDestroyImage(decode->va_dpy, img.image_id);
  240. av_frame_unref(frame);
  241. if (ret < 0)
  242. return ret;
  243. }
  244. }
  245. return 0;
  246. }
  247. int main(int argc, char **argv)
  248. {
  249. AVFormatContext *input_ctx = NULL;
  250. AVStream *video_st = NULL;
  251. AVCodecContext *decoder_ctx = NULL;
  252. const AVCodec *decoder;
  253. AVPacket pkt = { 0 };
  254. AVFrame *frame = NULL;
  255. DecodeContext decode = { NULL };
  256. Display *dpy = NULL;
  257. int va_ver_major, va_ver_minor;
  258. mfxIMPL mfx_impl = MFX_IMPL_AUTO_ANY;
  259. mfxVersion mfx_ver = { { 1, 1 } };
  260. mfxFrameAllocator frame_allocator = {
  261. .pthis = &decode,
  262. .Alloc = frame_alloc,
  263. .Lock = frame_lock,
  264. .Unlock = frame_unlock,
  265. .GetHDL = frame_get_hdl,
  266. .Free = frame_free,
  267. };
  268. AVIOContext *output_ctx = NULL;
  269. int ret, i, err;
  270. av_register_all();
  271. if (argc < 3) {
  272. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  273. return 1;
  274. }
  275. /* open the input file */
  276. ret = avformat_open_input(&input_ctx, argv[1], NULL, NULL);
  277. if (ret < 0) {
  278. fprintf(stderr, "Cannot open input file '%s': ", argv[1]);
  279. goto finish;
  280. }
  281. /* find the first H.264 video stream */
  282. for (i = 0; i < input_ctx->nb_streams; i++) {
  283. AVStream *st = input_ctx->streams[i];
  284. if (st->codec->codec_id == AV_CODEC_ID_H264 && !video_st)
  285. video_st = st;
  286. else
  287. st->discard = AVDISCARD_ALL;
  288. }
  289. if (!video_st) {
  290. fprintf(stderr, "No H.264 video stream in the input file\n");
  291. goto finish;
  292. }
  293. /* initialize VA-API */
  294. dpy = XOpenDisplay(NULL);
  295. if (!dpy) {
  296. fprintf(stderr, "Cannot open the X display\n");
  297. goto finish;
  298. }
  299. decode.va_dpy = vaGetDisplay(dpy);
  300. if (!decode.va_dpy) {
  301. fprintf(stderr, "Cannot open the VA display\n");
  302. goto finish;
  303. }
  304. err = vaInitialize(decode.va_dpy, &va_ver_major, &va_ver_minor);
  305. if (err != VA_STATUS_SUCCESS) {
  306. fprintf(stderr, "Cannot initialize VA: %s\n", vaErrorStr(err));
  307. goto finish;
  308. }
  309. fprintf(stderr, "Initialized VA v%d.%d\n", va_ver_major, va_ver_minor);
  310. /* initialize an MFX session */
  311. err = MFXInit(mfx_impl, &mfx_ver, &decode.mfx_session);
  312. if (err != MFX_ERR_NONE) {
  313. fprintf(stderr, "Error initializing an MFX session\n");
  314. goto finish;
  315. }
  316. MFXVideoCORE_SetHandle(decode.mfx_session, MFX_HANDLE_VA_DISPLAY, decode.va_dpy);
  317. MFXVideoCORE_SetFrameAllocator(decode.mfx_session, &frame_allocator);
  318. /* initialize the decoder */
  319. decoder = avcodec_find_decoder_by_name("h264_qsv");
  320. if (!decoder) {
  321. fprintf(stderr, "The QSV decoder is not present in libavcodec\n");
  322. goto finish;
  323. }
  324. decoder_ctx = avcodec_alloc_context3(decoder);
  325. if (!decoder_ctx) {
  326. ret = AVERROR(ENOMEM);
  327. goto finish;
  328. }
  329. decoder_ctx->codec_id = AV_CODEC_ID_H264;
  330. if (video_st->codec->extradata_size) {
  331. decoder_ctx->extradata = av_mallocz(video_st->codec->extradata_size +
  332. AV_INPUT_BUFFER_PADDING_SIZE);
  333. if (!decoder_ctx->extradata) {
  334. ret = AVERROR(ENOMEM);
  335. goto finish;
  336. }
  337. memcpy(decoder_ctx->extradata, video_st->codec->extradata,
  338. video_st->codec->extradata_size);
  339. decoder_ctx->extradata_size = video_st->codec->extradata_size;
  340. }
  341. decoder_ctx->refcounted_frames = 1;
  342. decoder_ctx->opaque = &decode;
  343. decoder_ctx->get_buffer2 = get_buffer;
  344. decoder_ctx->get_format = get_format;
  345. ret = avcodec_open2(decoder_ctx, NULL, NULL);
  346. if (ret < 0) {
  347. fprintf(stderr, "Error opening the decoder: ");
  348. goto finish;
  349. }
  350. /* open the output stream */
  351. ret = avio_open(&output_ctx, argv[2], AVIO_FLAG_WRITE);
  352. if (ret < 0) {
  353. fprintf(stderr, "Error opening the output context: ");
  354. goto finish;
  355. }
  356. frame = av_frame_alloc();
  357. if (!frame) {
  358. ret = AVERROR(ENOMEM);
  359. goto finish;
  360. }
  361. /* actual decoding */
  362. while (ret >= 0) {
  363. ret = av_read_frame(input_ctx, &pkt);
  364. if (ret < 0)
  365. break;
  366. if (pkt.stream_index == video_st->index)
  367. ret = decode_packet(&decode, decoder_ctx, frame, &pkt, output_ctx);
  368. av_packet_unref(&pkt);
  369. }
  370. /* flush the decoder */
  371. pkt.data = NULL;
  372. pkt.size = 0;
  373. ret = decode_packet(&decode, decoder_ctx, frame, &pkt, output_ctx);
  374. finish:
  375. if (ret < 0) {
  376. char buf[1024];
  377. av_strerror(ret, buf, sizeof(buf));
  378. fprintf(stderr, "%s\n", buf);
  379. }
  380. avformat_close_input(&input_ctx);
  381. av_frame_free(&frame);
  382. if (decode.mfx_session)
  383. MFXClose(decode.mfx_session);
  384. if (decode.va_dpy)
  385. vaTerminate(decode.va_dpy);
  386. if (dpy)
  387. XCloseDisplay(dpy);
  388. if (decoder_ctx)
  389. av_freep(&decoder_ctx->hwaccel_context);
  390. avcodec_free_context(&decoder_ctx);
  391. avio_close(output_ctx);
  392. return ret;
  393. }