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.

454 lines
13KB

  1. /*
  2. * Intel MediaSDK QSV codec-independent code
  3. *
  4. * copyright (c) 2013 Luca Barbato
  5. * copyright (c) 2015 Anton Khirnov <anton@khirnov.net>
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * Libav is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <string.h>
  24. #include <sys/types.h>
  25. #include <mfx/mfxvideo.h>
  26. #include "libavutil/common.h"
  27. #include "libavutil/mem.h"
  28. #include "libavutil/log.h"
  29. #include "libavutil/pixfmt.h"
  30. #include "libavutil/time.h"
  31. #include "avcodec.h"
  32. #include "internal.h"
  33. #include "qsv.h"
  34. #include "qsv_internal.h"
  35. #include "qsvdec.h"
  36. int ff_qsv_map_pixfmt(enum AVPixelFormat format)
  37. {
  38. switch (format) {
  39. case AV_PIX_FMT_YUV420P:
  40. case AV_PIX_FMT_YUVJ420P:
  41. return AV_PIX_FMT_NV12;
  42. default:
  43. return AVERROR(ENOSYS);
  44. }
  45. }
  46. static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session)
  47. {
  48. if (!session) {
  49. if (!q->internal_session) {
  50. int ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
  51. q->load_plugins);
  52. if (ret < 0)
  53. return ret;
  54. }
  55. q->session = q->internal_session;
  56. } else {
  57. q->session = session;
  58. }
  59. /* make sure the decoder is uninitialized */
  60. MFXVideoDECODE_Close(q->session);
  61. return 0;
  62. }
  63. static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q)
  64. {
  65. mfxSession session = NULL;
  66. mfxVideoParam param = { { 0 } };
  67. int ret;
  68. if (!q->async_fifo) {
  69. q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
  70. (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));
  71. if (!q->async_fifo)
  72. return AVERROR(ENOMEM);
  73. }
  74. if (avctx->hwaccel_context) {
  75. AVQSVContext *user_ctx = avctx->hwaccel_context;
  76. session = user_ctx->session;
  77. q->iopattern = user_ctx->iopattern;
  78. q->ext_buffers = user_ctx->ext_buffers;
  79. q->nb_ext_buffers = user_ctx->nb_ext_buffers;
  80. }
  81. ret = qsv_init_session(avctx, q, session);
  82. if (ret < 0) {
  83. av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
  84. return ret;
  85. }
  86. ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
  87. if (ret < 0)
  88. return ret;
  89. param.mfx.CodecId = ret;
  90. param.mfx.CodecProfile = avctx->profile;
  91. param.mfx.CodecLevel = avctx->level;
  92. param.mfx.FrameInfo.BitDepthLuma = 8;
  93. param.mfx.FrameInfo.BitDepthChroma = 8;
  94. param.mfx.FrameInfo.Shift = 0;
  95. param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
  96. param.mfx.FrameInfo.Width = avctx->coded_width;
  97. param.mfx.FrameInfo.Height = avctx->coded_height;
  98. param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  99. param.IOPattern = q->iopattern;
  100. param.AsyncDepth = q->async_depth;
  101. param.ExtParam = q->ext_buffers;
  102. param.NumExtParam = q->nb_ext_buffers;
  103. ret = MFXVideoDECODE_Init(q->session, &param);
  104. if (ret < 0) {
  105. av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
  106. return ff_qsv_error(ret);
  107. }
  108. return 0;
  109. }
  110. static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
  111. {
  112. int ret;
  113. ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
  114. if (ret < 0)
  115. return ret;
  116. if (frame->frame->format == AV_PIX_FMT_QSV) {
  117. frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
  118. } else {
  119. frame->surface_internal.Info.BitDepthLuma = 8;
  120. frame->surface_internal.Info.BitDepthChroma = 8;
  121. frame->surface_internal.Info.FourCC = MFX_FOURCC_NV12;
  122. frame->surface_internal.Info.Width = avctx->coded_width;
  123. frame->surface_internal.Info.Height = avctx->coded_height;
  124. frame->surface_internal.Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  125. frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
  126. frame->surface_internal.Data.Y = frame->frame->data[0];
  127. frame->surface_internal.Data.UV = frame->frame->data[1];
  128. frame->surface = &frame->surface_internal;
  129. }
  130. return 0;
  131. }
  132. static void qsv_clear_unused_frames(QSVContext *q)
  133. {
  134. QSVFrame *cur = q->work_frames;
  135. while (cur) {
  136. if (cur->surface && !cur->surface->Data.Locked && !cur->queued) {
  137. cur->surface = NULL;
  138. av_frame_unref(cur->frame);
  139. }
  140. cur = cur->next;
  141. }
  142. }
  143. static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
  144. {
  145. QSVFrame *frame, **last;
  146. int ret;
  147. qsv_clear_unused_frames(q);
  148. frame = q->work_frames;
  149. last = &q->work_frames;
  150. while (frame) {
  151. if (!frame->surface) {
  152. ret = alloc_frame(avctx, frame);
  153. if (ret < 0)
  154. return ret;
  155. *surf = frame->surface;
  156. return 0;
  157. }
  158. last = &frame->next;
  159. frame = frame->next;
  160. }
  161. frame = av_mallocz(sizeof(*frame));
  162. if (!frame)
  163. return AVERROR(ENOMEM);
  164. frame->frame = av_frame_alloc();
  165. if (!frame->frame) {
  166. av_freep(&frame);
  167. return AVERROR(ENOMEM);
  168. }
  169. *last = frame;
  170. ret = alloc_frame(avctx, frame);
  171. if (ret < 0)
  172. return ret;
  173. *surf = frame->surface;
  174. return 0;
  175. }
  176. static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
  177. {
  178. QSVFrame *cur = q->work_frames;
  179. while (cur) {
  180. if (surf == cur->surface)
  181. return cur;
  182. cur = cur->next;
  183. }
  184. return NULL;
  185. }
  186. static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
  187. AVFrame *frame, int *got_frame,
  188. AVPacket *avpkt)
  189. {
  190. QSVFrame *out_frame;
  191. mfxFrameSurface1 *insurf;
  192. mfxFrameSurface1 *outsurf;
  193. mfxSyncPoint *sync;
  194. mfxBitstream bs = { { { 0 } } };
  195. int ret;
  196. if (avpkt->size) {
  197. bs.Data = avpkt->data;
  198. bs.DataLength = avpkt->size;
  199. bs.MaxLength = bs.DataLength;
  200. bs.TimeStamp = avpkt->pts;
  201. }
  202. sync = av_mallocz(sizeof(*sync));
  203. if (!sync) {
  204. av_freep(&sync);
  205. return AVERROR(ENOMEM);
  206. }
  207. do {
  208. ret = get_surface(avctx, q, &insurf);
  209. if (ret < 0)
  210. return ret;
  211. ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
  212. insurf, &outsurf, sync);
  213. if (ret == MFX_WRN_DEVICE_BUSY)
  214. av_usleep(1);
  215. } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
  216. if (ret != MFX_ERR_NONE &&
  217. ret != MFX_ERR_MORE_DATA &&
  218. ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
  219. ret != MFX_ERR_MORE_SURFACE) {
  220. av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
  221. av_freep(&sync);
  222. return ff_qsv_error(ret);
  223. }
  224. /* make sure we do not enter an infinite loop if the SDK
  225. * did not consume any data and did not return anything */
  226. if (!*sync && !bs.DataOffset) {
  227. av_log(avctx, AV_LOG_WARNING, "A decode call did not consume any data\n");
  228. bs.DataOffset = avpkt->size;
  229. }
  230. if (*sync) {
  231. QSVFrame *out_frame = find_frame(q, outsurf);
  232. if (!out_frame) {
  233. av_log(avctx, AV_LOG_ERROR,
  234. "The returned surface does not correspond to any frame\n");
  235. av_freep(&sync);
  236. return AVERROR_BUG;
  237. }
  238. out_frame->queued = 1;
  239. av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  240. av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
  241. } else {
  242. av_freep(&sync);
  243. }
  244. if (!av_fifo_space(q->async_fifo) ||
  245. (!avpkt->size && av_fifo_size(q->async_fifo))) {
  246. AVFrame *src_frame;
  247. av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  248. av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
  249. out_frame->queued = 0;
  250. do {
  251. ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
  252. } while (ret == MFX_WRN_IN_EXECUTION);
  253. av_freep(&sync);
  254. src_frame = out_frame->frame;
  255. ret = av_frame_ref(frame, src_frame);
  256. if (ret < 0)
  257. return ret;
  258. outsurf = out_frame->surface;
  259. frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
  260. frame->repeat_pict =
  261. outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
  262. outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
  263. outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
  264. frame->top_field_first =
  265. outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
  266. frame->interlaced_frame =
  267. !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
  268. *got_frame = 1;
  269. }
  270. return bs.DataOffset;
  271. }
  272. int ff_qsv_decode_close(QSVContext *q)
  273. {
  274. QSVFrame *cur = q->work_frames;
  275. if (q->session)
  276. MFXVideoDECODE_Close(q->session);
  277. while (q->async_fifo && av_fifo_size(q->async_fifo)) {
  278. QSVFrame *out_frame;
  279. mfxSyncPoint *sync;
  280. av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
  281. av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
  282. av_freep(&sync);
  283. }
  284. while (cur) {
  285. q->work_frames = cur->next;
  286. av_frame_free(&cur->frame);
  287. av_freep(&cur);
  288. cur = q->work_frames;
  289. }
  290. av_fifo_free(q->async_fifo);
  291. q->async_fifo = NULL;
  292. av_parser_close(q->parser);
  293. avcodec_free_context(&q->avctx_internal);
  294. if (q->internal_session)
  295. MFXClose(q->internal_session);
  296. return 0;
  297. }
  298. int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
  299. AVFrame *frame, int *got_frame, AVPacket *pkt)
  300. {
  301. uint8_t *dummy_data;
  302. int dummy_size;
  303. int ret;
  304. if (!q->avctx_internal) {
  305. q->avctx_internal = avcodec_alloc_context3(NULL);
  306. if (!q->avctx_internal)
  307. return AVERROR(ENOMEM);
  308. if (avctx->extradata) {
  309. q->avctx_internal->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  310. if (!q->avctx_internal->extradata)
  311. return AVERROR(ENOMEM);
  312. memcpy(q->avctx_internal->extradata, avctx->extradata,
  313. avctx->extradata_size);
  314. q->avctx_internal->extradata_size = avctx->extradata_size;
  315. }
  316. q->parser = av_parser_init(avctx->codec_id);
  317. if (!q->parser)
  318. return AVERROR(ENOMEM);
  319. q->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
  320. q->orig_pix_fmt = AV_PIX_FMT_NONE;
  321. }
  322. if (!pkt->size)
  323. return qsv_decode(avctx, q, frame, got_frame, pkt);
  324. /* we assume the packets are already split properly and want
  325. * just the codec parameters here */
  326. av_parser_parse2(q->parser, q->avctx_internal,
  327. &dummy_data, &dummy_size,
  328. pkt->data, pkt->size, pkt->pts, pkt->dts,
  329. pkt->pos);
  330. /* TODO: flush delayed frames on reinit */
  331. if (q->parser->format != q->orig_pix_fmt ||
  332. q->parser->coded_width != avctx->coded_width ||
  333. q->parser->coded_height != avctx->coded_height) {
  334. enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
  335. AV_PIX_FMT_NONE,
  336. AV_PIX_FMT_NONE };
  337. enum AVPixelFormat qsv_format;
  338. qsv_format = ff_qsv_map_pixfmt(q->parser->format);
  339. if (qsv_format < 0) {
  340. av_log(avctx, AV_LOG_ERROR,
  341. "Only 8-bit YUV420 streams are supported.\n");
  342. ret = AVERROR(ENOSYS);
  343. goto reinit_fail;
  344. }
  345. q->orig_pix_fmt = q->parser->format;
  346. avctx->pix_fmt = pix_fmts[1] = qsv_format;
  347. avctx->width = q->parser->width;
  348. avctx->height = q->parser->height;
  349. avctx->coded_width = q->parser->coded_width;
  350. avctx->coded_height = q->parser->coded_height;
  351. avctx->level = q->avctx_internal->level;
  352. avctx->profile = q->avctx_internal->profile;
  353. ret = ff_get_format(avctx, pix_fmts);
  354. if (ret < 0)
  355. goto reinit_fail;
  356. avctx->pix_fmt = ret;
  357. ret = qsv_decode_init(avctx, q);
  358. if (ret < 0)
  359. goto reinit_fail;
  360. }
  361. return qsv_decode(avctx, q, frame, got_frame, pkt);
  362. reinit_fail:
  363. q->orig_pix_fmt = q->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
  364. return ret;
  365. }
  366. void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
  367. {
  368. q->orig_pix_fmt = AV_PIX_FMT_NONE;
  369. }