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.

291 lines
7.8KB

  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_internal.h"
  34. #include "qsvdec.h"
  35. int ff_qsv_map_pixfmt(enum AVPixelFormat format)
  36. {
  37. switch (format) {
  38. case AV_PIX_FMT_YUV420P:
  39. case AV_PIX_FMT_YUVJ420P:
  40. return AV_PIX_FMT_NV12;
  41. default:
  42. return AVERROR(ENOSYS);
  43. }
  44. }
  45. static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session)
  46. {
  47. if (!session) {
  48. if (!q->internal_session) {
  49. int ret = ff_qsv_init_internal_session(avctx, &q->internal_session);
  50. if (ret < 0)
  51. return ret;
  52. }
  53. q->session = q->internal_session;
  54. } else {
  55. q->session = session;
  56. }
  57. /* make sure the decoder is uninitialized */
  58. MFXVideoDECODE_Close(q->session);
  59. return 0;
  60. }
  61. int ff_qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
  62. {
  63. mfxVideoParam param = { { 0 } };
  64. int ret;
  65. ret = qsv_init_session(avctx, q, session);
  66. if (ret < 0) {
  67. av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
  68. return ret;
  69. }
  70. ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
  71. if (ret < 0)
  72. return ret;
  73. param.mfx.CodecId = ret;
  74. param.mfx.CodecProfile = avctx->profile;
  75. param.mfx.CodecLevel = avctx->level;
  76. param.mfx.FrameInfo.BitDepthLuma = 8;
  77. param.mfx.FrameInfo.BitDepthChroma = 8;
  78. param.mfx.FrameInfo.Shift = 0;
  79. param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
  80. param.mfx.FrameInfo.Width = avctx->coded_width;
  81. param.mfx.FrameInfo.Height = avctx->coded_height;
  82. param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  83. param.IOPattern = q->iopattern;
  84. param.AsyncDepth = q->async_depth;
  85. param.ExtParam = q->ext_buffers;
  86. param.NumExtParam = q->nb_ext_buffers;
  87. ret = MFXVideoDECODE_Init(q->session, &param);
  88. if (ret < 0) {
  89. av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
  90. return ff_qsv_error(ret);
  91. }
  92. return 0;
  93. }
  94. static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
  95. {
  96. int ret;
  97. ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
  98. if (ret < 0)
  99. return ret;
  100. if (frame->frame->format == AV_PIX_FMT_QSV) {
  101. frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
  102. } else {
  103. frame->surface_internal.Info.BitDepthLuma = 8;
  104. frame->surface_internal.Info.BitDepthChroma = 8;
  105. frame->surface_internal.Info.FourCC = MFX_FOURCC_NV12;
  106. frame->surface_internal.Info.Width = avctx->coded_width;
  107. frame->surface_internal.Info.Height = avctx->coded_height;
  108. frame->surface_internal.Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  109. frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
  110. frame->surface_internal.Data.Y = frame->frame->data[0];
  111. frame->surface_internal.Data.UV = frame->frame->data[1];
  112. frame->surface = &frame->surface_internal;
  113. }
  114. return 0;
  115. }
  116. static void qsv_clear_unused_frames(QSVContext *q)
  117. {
  118. QSVFrame *cur = q->work_frames;
  119. while (cur) {
  120. if (cur->surface && !cur->surface->Data.Locked) {
  121. cur->surface = NULL;
  122. av_frame_unref(cur->frame);
  123. }
  124. cur = cur->next;
  125. }
  126. }
  127. static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
  128. {
  129. QSVFrame *frame, **last;
  130. int ret;
  131. qsv_clear_unused_frames(q);
  132. frame = q->work_frames;
  133. last = &q->work_frames;
  134. while (frame) {
  135. if (!frame->surface) {
  136. ret = alloc_frame(avctx, frame);
  137. if (ret < 0)
  138. return ret;
  139. *surf = frame->surface;
  140. return 0;
  141. }
  142. last = &frame->next;
  143. frame = frame->next;
  144. }
  145. frame = av_mallocz(sizeof(*frame));
  146. if (!frame)
  147. return AVERROR(ENOMEM);
  148. frame->frame = av_frame_alloc();
  149. if (!frame->frame) {
  150. av_freep(&frame);
  151. return AVERROR(ENOMEM);
  152. }
  153. *last = frame;
  154. ret = alloc_frame(avctx, frame);
  155. if (ret < 0)
  156. return ret;
  157. *surf = frame->surface;
  158. return 0;
  159. }
  160. static AVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
  161. {
  162. QSVFrame *cur = q->work_frames;
  163. while (cur) {
  164. if (surf == cur->surface)
  165. return cur->frame;
  166. cur = cur->next;
  167. }
  168. return NULL;
  169. }
  170. int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q,
  171. AVFrame *frame, int *got_frame,
  172. AVPacket *avpkt)
  173. {
  174. mfxFrameSurface1 *insurf;
  175. mfxFrameSurface1 *outsurf;
  176. mfxSyncPoint sync;
  177. mfxBitstream bs = { { { 0 } } };
  178. int ret;
  179. if (avpkt->size) {
  180. bs.Data = avpkt->data;
  181. bs.DataLength = avpkt->size;
  182. bs.MaxLength = bs.DataLength;
  183. bs.TimeStamp = avpkt->pts;
  184. }
  185. do {
  186. ret = get_surface(avctx, q, &insurf);
  187. if (ret < 0)
  188. return ret;
  189. ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
  190. insurf, &outsurf, &sync);
  191. if (ret == MFX_WRN_DEVICE_BUSY)
  192. av_usleep(1);
  193. } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
  194. if (ret != MFX_ERR_NONE &&
  195. ret != MFX_ERR_MORE_DATA &&
  196. ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
  197. ret != MFX_ERR_MORE_SURFACE) {
  198. av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
  199. return ff_qsv_error(ret);
  200. }
  201. if (sync) {
  202. AVFrame *src_frame;
  203. MFXVideoCORE_SyncOperation(q->session, sync, 60000);
  204. src_frame = find_frame(q, outsurf);
  205. if (!src_frame) {
  206. av_log(avctx, AV_LOG_ERROR,
  207. "The returned surface does not correspond to any frame\n");
  208. return AVERROR_BUG;
  209. }
  210. ret = av_frame_ref(frame, src_frame);
  211. if (ret < 0)
  212. return ret;
  213. frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
  214. frame->repeat_pict =
  215. outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
  216. outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
  217. outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
  218. frame->top_field_first =
  219. outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
  220. frame->interlaced_frame =
  221. !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
  222. *got_frame = 1;
  223. }
  224. return bs.DataOffset;
  225. }
  226. int ff_qsv_decode_close(QSVContext *q)
  227. {
  228. QSVFrame *cur = q->work_frames;
  229. while (cur) {
  230. q->work_frames = cur->next;
  231. av_frame_free(&cur->frame);
  232. av_freep(&cur);
  233. cur = q->work_frames;
  234. }
  235. if (q->internal_session)
  236. MFXClose(q->internal_session);
  237. return 0;
  238. }