Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 1 | /* |
| 2 | * Intel MediaSDK QSV codec-independent code |
| 3 | * |
| 4 | * copyright (c) 2013 Luca Barbato |
| 5 | * copyright (c) 2015 Anton Khirnov <[email protected]> |
| 6 | * |
Michael Niedermayer | 841e9f4 | 2015-02-19 19:52:29 | [diff] [blame] | 7 | * This file is part of FFmpeg. |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 8 | * |
Michael Niedermayer | 841e9f4 | 2015-02-19 19:52:29 | [diff] [blame] | 9 | * FFmpeg is free software; you can redistribute it and/or |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 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 | * |
Michael Niedermayer | 841e9f4 | 2015-02-19 19:52:29 | [diff] [blame] | 14 | * FFmpeg is distributed in the hope that it will be useful, |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 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 |
Michael Niedermayer | 841e9f4 | 2015-02-19 19:52:29 | [diff] [blame] | 20 | * License along with FFmpeg; if not, write to the Free Software |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | */ |
| 23 | |
Martin Storsjö | a78f136 | 2022-02-23 12:56:49 | [diff] [blame] | 24 | #include "config_components.h" |
| 25 | |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 26 | #include <stdint.h> |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <sys/types.h> |
| 29 | |
Haihao Xiang | 3e61b7d | 2020-09-08 03:17:27 | [diff] [blame] | 30 | #include <mfxvideo.h> |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 31 | |
| 32 | #include "libavutil/common.h" |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 33 | #include "libavutil/fifo.h" |
| 34 | #include "libavutil/frame.h" |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 35 | #include "libavutil/hwcontext.h" |
| 36 | #include "libavutil/hwcontext_qsv.h" |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 37 | #include "libavutil/mem.h" |
| 38 | #include "libavutil/log.h" |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 39 | #include "libavutil/opt.h" |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 40 | #include "libavutil/pixfmt.h" |
| 41 | #include "libavutil/time.h" |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 42 | #include "libavutil/imgutils.h" |
Haihao Xiang | 8ca06a8 | 2022-01-24 08:24:55 | [diff] [blame] | 43 | #include "libavutil/film_grain_params.h" |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 44 | |
| 45 | #include "avcodec.h" |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 46 | #include "codec_internal.h" |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 47 | #include "internal.h" |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 48 | #include "decode.h" |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 49 | #include "hwconfig.h" |
Ivan Uskov | 6e12799 | 2015-07-14 11:07:04 | [diff] [blame] | 50 | #include "qsv.h" |
Anton Khirnov | d0a63d8 | 2015-03-13 07:13:00 | [diff] [blame] | 51 | #include "qsv_internal.h" |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 52 | |
Haihao Xiang | 6900fee | 2021-01-04 02:46:14 | [diff] [blame] | 53 | #if QSV_ONEVPL |
| 54 | #include <mfxdispatcher.h> |
| 55 | #else |
| 56 | #define MFXUnload(a) do { } while(0) |
| 57 | #endif |
| 58 | |
Haihao Xiang | 971b4ac | 2021-06-11 02:19:45 | [diff] [blame] | 59 | static const AVRational mfx_tb = { 1, 90000 }; |
| 60 | |
| 61 | #define PTS_TO_MFX_PTS(pts, pts_tb) ((pts) == AV_NOPTS_VALUE ? \ |
| 62 | MFX_TIMESTAMP_UNKNOWN : pts_tb.num ? \ |
| 63 | av_rescale_q(pts, pts_tb, mfx_tb) : pts) |
| 64 | |
| 65 | #define MFX_PTS_TO_PTS(mfx_pts, pts_tb) ((mfx_pts) == MFX_TIMESTAMP_UNKNOWN ? \ |
| 66 | AV_NOPTS_VALUE : pts_tb.num ? \ |
| 67 | av_rescale_q(mfx_pts, mfx_tb, pts_tb) : mfx_pts) |
| 68 | |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 69 | typedef struct QSVAsyncFrame { |
| 70 | mfxSyncPoint *sync; |
| 71 | QSVFrame *frame; |
| 72 | } QSVAsyncFrame; |
| 73 | |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 74 | typedef struct QSVContext { |
| 75 | // the session used for decoding |
| 76 | mfxSession session; |
Haihao Xiang | 7c24a76 | 2022-01-24 08:24:54 | [diff] [blame] | 77 | mfxVersion ver; |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 78 | |
| 79 | // the session we allocated internally, in case the caller did not provide |
| 80 | // one |
| 81 | QSVSession internal_qs; |
| 82 | |
| 83 | QSVFramesContext frames_ctx; |
| 84 | |
| 85 | /** |
| 86 | * a linked list of frames currently being used by QSV |
| 87 | */ |
| 88 | QSVFrame *work_frames; |
| 89 | |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 90 | AVFifo *async_fifo; |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 91 | int zero_consume_run; |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 92 | int reinit_flag; |
| 93 | |
| 94 | enum AVPixelFormat orig_pix_fmt; |
| 95 | uint32_t fourcc; |
| 96 | mfxFrameInfo frame_info; |
| 97 | AVBufferPool *pool; |
Wenbin Chen | 0a0847d | 2022-03-18 06:25:11 | [diff] [blame] | 98 | int suggest_pool_size; |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 99 | int initialized; |
| 100 | |
| 101 | // options set by the caller |
| 102 | int async_depth; |
| 103 | int iopattern; |
| 104 | int gpu_copy; |
| 105 | |
| 106 | char *load_plugins; |
| 107 | |
| 108 | mfxExtBuffer **ext_buffers; |
| 109 | int nb_ext_buffers; |
| 110 | } QSVContext; |
| 111 | |
| 112 | static const AVCodecHWConfigInternal *const qsv_hw_configs[] = { |
Mark Thompson | 758fbc5 | 2017-10-25 23:18:40 | [diff] [blame] | 113 | &(const AVCodecHWConfigInternal) { |
| 114 | .public = { |
| 115 | .pix_fmt = AV_PIX_FMT_QSV, |
| 116 | .methods = AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX | |
Haihao Xiang | ecee3b0 | 2021-08-11 06:01:54 | [diff] [blame] | 117 | AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX, |
Mark Thompson | 758fbc5 | 2017-10-25 23:18:40 | [diff] [blame] | 118 | .device_type = AV_HWDEVICE_TYPE_QSV, |
| 119 | }, |
| 120 | .hwaccel = NULL, |
| 121 | }, |
| 122 | NULL |
| 123 | }; |
| 124 | |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 125 | static int qsv_get_continuous_buffer(AVCodecContext *avctx, AVFrame *frame, |
| 126 | AVBufferPool *pool) |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 127 | { |
| 128 | int ret = 0; |
| 129 | |
| 130 | ff_decode_frame_props(avctx, frame); |
| 131 | |
| 132 | frame->width = avctx->width; |
| 133 | frame->height = avctx->height; |
| 134 | |
| 135 | switch (avctx->pix_fmt) { |
| 136 | case AV_PIX_FMT_NV12: |
| 137 | frame->linesize[0] = FFALIGN(avctx->width, 128); |
| 138 | break; |
| 139 | case AV_PIX_FMT_P010: |
Wenbin Chen | e0ae810 | 2022-04-06 08:48:03 | [diff] [blame] | 140 | case AV_PIX_FMT_YUYV422: |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 141 | frame->linesize[0] = 2 * FFALIGN(avctx->width, 128); |
| 142 | break; |
Wenbin Chen | e0ae810 | 2022-04-06 08:48:03 | [diff] [blame] | 143 | case AV_PIX_FMT_Y210: |
Haihao Xiang | db85e01 | 2022-09-06 04:53:38 | [diff] [blame] | 144 | case AV_PIX_FMT_VUYX: |
Haihao Xiang | 1898dbd | 2022-10-06 07:35:36 | [diff] [blame^] | 145 | case AV_PIX_FMT_XV30: |
Wenbin Chen | e0ae810 | 2022-04-06 08:48:03 | [diff] [blame] | 146 | frame->linesize[0] = 4 * FFALIGN(avctx->width, 128); |
| 147 | break; |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 148 | default: |
| 149 | av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n"); |
Zhong Li | 9fff5c4 | 2019-12-28 14:22:07 | [diff] [blame] | 150 | return AVERROR(EINVAL); |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 151 | } |
| 152 | |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 153 | frame->buf[0] = av_buffer_pool_get(pool); |
| 154 | if (!frame->buf[0]) |
| 155 | return AVERROR(ENOMEM); |
| 156 | |
| 157 | frame->data[0] = frame->buf[0]->data; |
Wenbin Chen | e0ae810 | 2022-04-06 08:48:03 | [diff] [blame] | 158 | if (avctx->pix_fmt == AV_PIX_FMT_NV12 || |
| 159 | avctx->pix_fmt == AV_PIX_FMT_P010) { |
| 160 | frame->linesize[1] = frame->linesize[0]; |
| 161 | frame->data[1] = frame->data[0] + |
| 162 | frame->linesize[0] * FFALIGN(avctx->height, 64); |
| 163 | } |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 164 | |
| 165 | ret = ff_attach_decode_data(frame); |
| 166 | if (ret < 0) |
| 167 | return ret; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 172 | static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session, |
Mark Thompson | 8aa3c2d | 2017-03-04 23:57:36 | [diff] [blame] | 173 | AVBufferRef *hw_frames_ref, AVBufferRef *hw_device_ref) |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 174 | { |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 175 | int ret; |
| 176 | |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 177 | if (q->gpu_copy == MFX_GPUCOPY_ON && |
Zhong Li | 779951f | 2019-12-28 14:28:45 | [diff] [blame] | 178 | !(q->iopattern & MFX_IOPATTERN_OUT_SYSTEM_MEMORY)) { |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 179 | av_log(avctx, AV_LOG_WARNING, "GPU-accelerated memory copy " |
Zhong Li | 779951f | 2019-12-28 14:28:45 | [diff] [blame] | 180 | "only works in system memory mode.\n"); |
| 181 | q->gpu_copy = MFX_GPUCOPY_OFF; |
| 182 | } |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 183 | if (session) { |
| 184 | q->session = session; |
| 185 | } else if (hw_frames_ref) { |
Zhong Li | 74007dd | 2019-09-19 20:45:26 | [diff] [blame] | 186 | if (q->internal_qs.session) { |
| 187 | MFXClose(q->internal_qs.session); |
| 188 | q->internal_qs.session = NULL; |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 189 | } |
| 190 | av_buffer_unref(&q->frames_ctx.hw_frames_ctx); |
| 191 | |
| 192 | q->frames_ctx.hw_frames_ctx = av_buffer_ref(hw_frames_ref); |
| 193 | if (!q->frames_ctx.hw_frames_ctx) |
| 194 | return AVERROR(ENOMEM); |
| 195 | |
Zhong Li | 74007dd | 2019-09-19 20:45:26 | [diff] [blame] | 196 | ret = ff_qsv_init_session_frames(avctx, &q->internal_qs.session, |
Mark Thompson | 91c3b50 | 2017-03-04 23:57:35 | [diff] [blame] | 197 | &q->frames_ctx, q->load_plugins, |
Haihao Xiang | c77149b | 2020-08-19 01:43:12 | [diff] [blame] | 198 | #if QSV_HAVE_OPAQUE |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 199 | q->iopattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY, |
Haihao Xiang | c77149b | 2020-08-19 01:43:12 | [diff] [blame] | 200 | #else |
| 201 | 0, |
| 202 | #endif |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 203 | q->gpu_copy); |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 204 | if (ret < 0) { |
| 205 | av_buffer_unref(&q->frames_ctx.hw_frames_ctx); |
| 206 | return ret; |
| 207 | } |
| 208 | |
Zhong Li | 74007dd | 2019-09-19 20:45:26 | [diff] [blame] | 209 | q->session = q->internal_qs.session; |
Mark Thompson | 8aa3c2d | 2017-03-04 23:57:36 | [diff] [blame] | 210 | } else if (hw_device_ref) { |
Zhong Li | 74007dd | 2019-09-19 20:45:26 | [diff] [blame] | 211 | if (q->internal_qs.session) { |
| 212 | MFXClose(q->internal_qs.session); |
| 213 | q->internal_qs.session = NULL; |
Mark Thompson | 8aa3c2d | 2017-03-04 23:57:36 | [diff] [blame] | 214 | } |
| 215 | |
Zhong Li | 74007dd | 2019-09-19 20:45:26 | [diff] [blame] | 216 | ret = ff_qsv_init_session_device(avctx, &q->internal_qs.session, |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 217 | hw_device_ref, q->load_plugins, q->gpu_copy); |
Mark Thompson | 8aa3c2d | 2017-03-04 23:57:36 | [diff] [blame] | 218 | if (ret < 0) |
| 219 | return ret; |
| 220 | |
Zhong Li | 74007dd | 2019-09-19 20:45:26 | [diff] [blame] | 221 | q->session = q->internal_qs.session; |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 222 | } else { |
Zhong Li | 74007dd | 2019-09-19 20:45:26 | [diff] [blame] | 223 | if (!q->internal_qs.session) { |
| 224 | ret = ff_qsv_init_internal_session(avctx, &q->internal_qs, |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 225 | q->load_plugins, q->gpu_copy); |
Anton Khirnov | d0a63d8 | 2015-03-13 07:13:00 | [diff] [blame] | 226 | if (ret < 0) |
| 227 | return ret; |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 228 | } |
| 229 | |
Zhong Li | 74007dd | 2019-09-19 20:45:26 | [diff] [blame] | 230 | q->session = q->internal_qs.session; |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 231 | } |
| 232 | |
Haihao Xiang | 7c24a76 | 2022-01-24 08:24:54 | [diff] [blame] | 233 | if (MFXQueryVersion(q->session, &q->ver) != MFX_ERR_NONE) { |
| 234 | av_log(avctx, AV_LOG_ERROR, "Error querying the session version. \n"); |
| 235 | q->session = NULL; |
| 236 | |
| 237 | if (q->internal_qs.session) { |
| 238 | MFXClose(q->internal_qs.session); |
| 239 | q->internal_qs.session = NULL; |
| 240 | } |
| 241 | |
Haihao Xiang | 6900fee | 2021-01-04 02:46:14 | [diff] [blame] | 242 | if (q->internal_qs.loader) { |
| 243 | MFXUnload(q->internal_qs.loader); |
| 244 | q->internal_qs.loader = NULL; |
| 245 | } |
| 246 | |
Haihao Xiang | 7c24a76 | 2022-01-24 08:24:54 | [diff] [blame] | 247 | return AVERROR_EXTERNAL; |
| 248 | } |
| 249 | |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 250 | /* make sure the decoder is uninitialized */ |
| 251 | MFXVideoDECODE_Close(q->session); |
| 252 | |
| 253 | return 0; |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 254 | } |
| 255 | |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 256 | static int qsv_decode_preinit(AVCodecContext *avctx, QSVContext *q, enum AVPixelFormat pix_fmt, mfxVideoParam *param) |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 257 | { |
Anton Khirnov | 6f19bbc | 2016-05-21 16:26:40 | [diff] [blame] | 258 | mfxSession session = NULL; |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 259 | int iopattern = 0; |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 260 | int ret; |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 261 | enum AVPixelFormat pix_fmts[3] = { |
| 262 | AV_PIX_FMT_QSV, /* opaque format in case of video memory output */ |
| 263 | pix_fmt, /* system memory format obtained from bitstream parser */ |
| 264 | AV_PIX_FMT_NONE }; |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 265 | |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 266 | ret = ff_get_format(avctx, pix_fmts); |
| 267 | if (ret < 0) { |
| 268 | q->orig_pix_fmt = avctx->pix_fmt = AV_PIX_FMT_NONE; |
| 269 | return ret; |
| 270 | } |
Anton Khirnov | 92736c7 | 2016-06-22 09:53:00 | [diff] [blame] | 271 | |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 272 | if (!q->async_fifo) { |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 273 | q->async_fifo = av_fifo_alloc2(q->async_depth, sizeof(QSVAsyncFrame), 0); |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 274 | if (!q->async_fifo) |
| 275 | return AVERROR(ENOMEM); |
Hendrik Leppkes | b54d645 | 2015-10-22 15:00:49 | [diff] [blame] | 276 | } |
Hendrik Leppkes | b54d645 | 2015-10-22 15:00:49 | [diff] [blame] | 277 | |
Anton Khirnov | e328178 | 2016-07-30 14:38:51 | [diff] [blame] | 278 | if (avctx->pix_fmt == AV_PIX_FMT_QSV && avctx->hwaccel_context) { |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 279 | AVQSVContext *user_ctx = avctx->hwaccel_context; |
| 280 | session = user_ctx->session; |
| 281 | iopattern = user_ctx->iopattern; |
| 282 | q->ext_buffers = user_ctx->ext_buffers; |
| 283 | q->nb_ext_buffers = user_ctx->nb_ext_buffers; |
| 284 | } |
| 285 | |
Haihao Xiang | ecee3b0 | 2021-08-11 06:01:54 | [diff] [blame] | 286 | if (avctx->hw_device_ctx && !avctx->hw_frames_ctx && ret == AV_PIX_FMT_QSV) { |
| 287 | AVHWFramesContext *hwframes_ctx; |
| 288 | AVQSVFramesContext *frames_hwctx; |
| 289 | |
| 290 | avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx); |
| 291 | |
| 292 | if (!avctx->hw_frames_ctx) { |
| 293 | av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_alloc failed\n"); |
| 294 | return AVERROR(ENOMEM); |
| 295 | } |
| 296 | |
| 297 | hwframes_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; |
| 298 | frames_hwctx = hwframes_ctx->hwctx; |
| 299 | hwframes_ctx->width = FFALIGN(avctx->coded_width, 32); |
| 300 | hwframes_ctx->height = FFALIGN(avctx->coded_height, 32); |
| 301 | hwframes_ctx->format = AV_PIX_FMT_QSV; |
| 302 | hwframes_ctx->sw_format = avctx->sw_pix_fmt; |
Wenbin Chen | 0a0847d | 2022-03-18 06:25:11 | [diff] [blame] | 303 | hwframes_ctx->initial_pool_size = q->suggest_pool_size + 16 + avctx->extra_hw_frames; |
Haihao Xiang | ecee3b0 | 2021-08-11 06:01:54 | [diff] [blame] | 304 | frames_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET; |
| 305 | |
| 306 | ret = av_hwframe_ctx_init(avctx->hw_frames_ctx); |
| 307 | |
| 308 | if (ret < 0) { |
| 309 | av_log(NULL, AV_LOG_ERROR, "Error initializing a QSV frame pool\n"); |
| 310 | av_buffer_unref(&avctx->hw_frames_ctx); |
| 311 | return ret; |
| 312 | } |
| 313 | } |
| 314 | |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 315 | if (avctx->hw_frames_ctx) { |
| 316 | AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; |
| 317 | AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx; |
| 318 | |
| 319 | if (!iopattern) { |
Haihao Xiang | c77149b | 2020-08-19 01:43:12 | [diff] [blame] | 320 | #if QSV_HAVE_OPAQUE |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 321 | if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME) |
| 322 | iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY; |
| 323 | else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) |
| 324 | iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY; |
Haihao Xiang | c77149b | 2020-08-19 01:43:12 | [diff] [blame] | 325 | #else |
| 326 | if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) |
| 327 | iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY; |
| 328 | #endif |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | if (!iopattern) |
| 333 | iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY; |
| 334 | q->iopattern = iopattern; |
| 335 | |
Zhong Li | 525de95 | 2019-09-19 20:45:27 | [diff] [blame] | 336 | ff_qsv_print_iopattern(avctx, q->iopattern, "Decoder"); |
| 337 | |
Mark Thompson | 8aa3c2d | 2017-03-04 23:57:36 | [diff] [blame] | 338 | ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx, avctx->hw_device_ctx); |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 339 | if (ret < 0) { |
| 340 | av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n"); |
| 341 | return ret; |
Ivan Uskov | 6e12799 | 2015-07-14 11:07:04 | [diff] [blame] | 342 | } |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 343 | |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 344 | param->IOPattern = q->iopattern; |
| 345 | param->AsyncDepth = q->async_depth; |
| 346 | param->ExtParam = q->ext_buffers; |
| 347 | param->NumExtParam = q->nb_ext_buffers; |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 348 | |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 349 | return 0; |
| 350 | } |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 351 | |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 352 | static int qsv_decode_init_context(AVCodecContext *avctx, QSVContext *q, mfxVideoParam *param) |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 353 | { |
| 354 | int ret; |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 355 | |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 356 | avctx->width = param->mfx.FrameInfo.CropW; |
| 357 | avctx->height = param->mfx.FrameInfo.CropH; |
| 358 | avctx->coded_width = param->mfx.FrameInfo.Width; |
| 359 | avctx->coded_height = param->mfx.FrameInfo.Height; |
| 360 | avctx->level = param->mfx.CodecLevel; |
| 361 | avctx->profile = param->mfx.CodecProfile; |
| 362 | avctx->field_order = ff_qsv_map_picstruct(param->mfx.FrameInfo.PicStruct); |
| 363 | avctx->pix_fmt = ff_qsv_map_fourcc(param->mfx.FrameInfo.FourCC); |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 364 | |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 365 | ret = MFXVideoDECODE_Init(q->session, param); |
Anton Khirnov | 95414eb | 2016-06-25 19:38:10 | [diff] [blame] | 366 | if (ret < 0) |
| 367 | return ff_qsv_print_error(avctx, ret, |
| 368 | "Error initializing the MFX video decoder"); |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 369 | |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 370 | q->frame_info = param->mfx.FrameInfo; |
| 371 | |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 372 | if (!avctx->hw_frames_ctx) |
| 373 | q->pool = av_buffer_pool_init(av_image_get_buffer_size(avctx->pix_fmt, |
| 374 | FFALIGN(avctx->width, 128), FFALIGN(avctx->height, 64), 1), av_buffer_allocz); |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 375 | return 0; |
| 376 | } |
| 377 | |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 378 | static int qsv_decode_header(AVCodecContext *avctx, QSVContext *q, |
| 379 | const AVPacket *avpkt, enum AVPixelFormat pix_fmt, |
| 380 | mfxVideoParam *param) |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 381 | { |
| 382 | int ret; |
Haihao Xiang | c8cfe67 | 2021-07-28 08:15:46 | [diff] [blame] | 383 | mfxExtVideoSignalInfo video_signal_info = { 0 }; |
| 384 | mfxExtBuffer *header_ext_params[1] = { (mfxExtBuffer *)&video_signal_info }; |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 385 | mfxBitstream bs = { 0 }; |
| 386 | |
| 387 | if (avpkt->size) { |
| 388 | bs.Data = avpkt->data; |
| 389 | bs.DataLength = avpkt->size; |
| 390 | bs.MaxLength = bs.DataLength; |
Haihao Xiang | 971b4ac | 2021-06-11 02:19:45 | [diff] [blame] | 391 | bs.TimeStamp = PTS_TO_MFX_PTS(avpkt->pts, avctx->pkt_timebase); |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 392 | if (avctx->field_order == AV_FIELD_PROGRESSIVE) |
| 393 | bs.DataFlag |= MFX_BITSTREAM_COMPLETE_FRAME; |
| 394 | } else |
| 395 | return AVERROR_INVALIDDATA; |
| 396 | |
| 397 | |
| 398 | if(!q->session) { |
| 399 | ret = qsv_decode_preinit(avctx, q, pix_fmt, param); |
| 400 | if (ret < 0) |
| 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | ret = ff_qsv_codec_id_to_mfx(avctx->codec_id); |
| 405 | if (ret < 0) |
| 406 | return ret; |
| 407 | |
| 408 | param->mfx.CodecId = ret; |
Haihao Xiang | c8cfe67 | 2021-07-28 08:15:46 | [diff] [blame] | 409 | video_signal_info.Header.BufferId = MFX_EXTBUFF_VIDEO_SIGNAL_INFO; |
| 410 | video_signal_info.Header.BufferSz = sizeof(video_signal_info); |
| 411 | // The SDK doesn't support other ext buffers when calling MFXVideoDECODE_DecodeHeader, |
| 412 | // so do not append this buffer to the existent buffer array |
| 413 | param->ExtParam = header_ext_params; |
| 414 | param->NumExtParam = 1; |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 415 | ret = MFXVideoDECODE_DecodeHeader(q->session, &bs, param); |
| 416 | if (MFX_ERR_MORE_DATA == ret) { |
| 417 | return AVERROR(EAGAIN); |
| 418 | } |
| 419 | if (ret < 0) |
| 420 | return ff_qsv_print_error(avctx, ret, |
| 421 | "Error decoding stream header"); |
Anton Khirnov | ce320cf | 2016-06-22 09:44:09 | [diff] [blame] | 422 | |
Haihao Xiang | c8cfe67 | 2021-07-28 08:15:46 | [diff] [blame] | 423 | avctx->color_range = video_signal_info.VideoFullRange ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG; |
| 424 | |
| 425 | if (video_signal_info.ColourDescriptionPresent) { |
| 426 | avctx->color_primaries = video_signal_info.ColourPrimaries; |
| 427 | avctx->color_trc = video_signal_info.TransferCharacteristics; |
| 428 | avctx->colorspace = video_signal_info.MatrixCoefficients; |
| 429 | } |
| 430 | |
| 431 | param->ExtParam = q->ext_buffers; |
| 432 | param->NumExtParam = q->nb_ext_buffers; |
| 433 | |
Haihao Xiang | 8ca06a8 | 2022-01-24 08:24:55 | [diff] [blame] | 434 | #if QSV_VERSION_ATLEAST(1, 34) |
| 435 | if (QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 34) && avctx->codec_id == AV_CODEC_ID_AV1) |
| 436 | param->mfx.FilmGrain = (avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) ? 0 : param->mfx.FilmGrain; |
| 437 | #endif |
| 438 | |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 439 | return 0; |
| 440 | } |
| 441 | |
Anton Khirnov | ce320cf | 2016-06-22 09:44:09 | [diff] [blame] | 442 | static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame) |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 443 | { |
| 444 | int ret; |
| 445 | |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 446 | if (q->pool) |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 447 | ret = qsv_get_continuous_buffer(avctx, frame->frame, q->pool); |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 448 | else |
| 449 | ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF); |
| 450 | |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 451 | if (ret < 0) |
| 452 | return ret; |
| 453 | |
| 454 | if (frame->frame->format == AV_PIX_FMT_QSV) { |
Anton Khirnov | 404e514 | 2016-08-09 10:05:49 | [diff] [blame] | 455 | frame->surface = *(mfxFrameSurface1*)frame->frame->data[3]; |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 456 | } else { |
Wenbin Chen | e0ae810 | 2022-04-06 08:48:03 | [diff] [blame] | 457 | ret = ff_qsv_map_frame_to_surface(frame->frame, &frame->surface); |
| 458 | if (ret < 0) { |
| 459 | av_log(avctx, AV_LOG_ERROR, "map frame to surface failed.\n"); |
| 460 | return ret; |
| 461 | } |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 462 | } |
| 463 | |
Wenbin Chen | b1c26ce | 2022-02-11 02:37:36 | [diff] [blame] | 464 | frame->surface.Info = q->frame_info; |
| 465 | |
Anton Khirnov | 00aeedd | 2016-08-10 06:29:23 | [diff] [blame] | 466 | if (q->frames_ctx.mids) { |
| 467 | ret = ff_qsv_find_surface_idx(&q->frames_ctx, frame); |
| 468 | if (ret < 0) |
| 469 | return ret; |
| 470 | |
| 471 | frame->surface.Data.MemId = &q->frames_ctx.mids[ret]; |
| 472 | } |
Haihao Xiang | 8dd507b | 2022-01-24 08:24:53 | [diff] [blame] | 473 | |
| 474 | frame->surface.Data.ExtParam = frame->ext_param; |
| 475 | frame->surface.Data.NumExtParam = 0; |
| 476 | frame->num_ext_params = 0; |
Zhong Li | 52ed83f | 2018-04-04 09:51:29 | [diff] [blame] | 477 | frame->dec_info.Header.BufferId = MFX_EXTBUFF_DECODED_FRAME_INFO; |
| 478 | frame->dec_info.Header.BufferSz = sizeof(frame->dec_info); |
Haihao Xiang | 8dd507b | 2022-01-24 08:24:53 | [diff] [blame] | 479 | ff_qsv_frame_add_ext_param(avctx, frame, (mfxExtBuffer *)&frame->dec_info); |
Haihao Xiang | 8ca06a8 | 2022-01-24 08:24:55 | [diff] [blame] | 480 | #if QSV_VERSION_ATLEAST(1, 34) |
| 481 | if (QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 34) && avctx->codec_id == AV_CODEC_ID_AV1) { |
| 482 | frame->av1_film_grain_param.Header.BufferId = MFX_EXTBUFF_AV1_FILM_GRAIN_PARAM; |
| 483 | frame->av1_film_grain_param.Header.BufferSz = sizeof(frame->av1_film_grain_param); |
| 484 | frame->av1_film_grain_param.FilmGrainFlags = 0; |
| 485 | ff_qsv_frame_add_ext_param(avctx, frame, (mfxExtBuffer *)&frame->av1_film_grain_param); |
| 486 | } |
| 487 | #endif |
Anton Khirnov | 00aeedd | 2016-08-10 06:29:23 | [diff] [blame] | 488 | |
Anton Khirnov | 404e514 | 2016-08-09 10:05:49 | [diff] [blame] | 489 | frame->used = 1; |
| 490 | |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | static void qsv_clear_unused_frames(QSVContext *q) |
| 495 | { |
| 496 | QSVFrame *cur = q->work_frames; |
| 497 | while (cur) { |
Anton Khirnov | 404e514 | 2016-08-09 10:05:49 | [diff] [blame] | 498 | if (cur->used && !cur->surface.Data.Locked && !cur->queued) { |
| 499 | cur->used = 0; |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 500 | av_frame_unref(cur->frame); |
| 501 | } |
| 502 | cur = cur->next; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf) |
| 507 | { |
| 508 | QSVFrame *frame, **last; |
| 509 | int ret; |
| 510 | |
| 511 | qsv_clear_unused_frames(q); |
| 512 | |
| 513 | frame = q->work_frames; |
| 514 | last = &q->work_frames; |
| 515 | while (frame) { |
Anton Khirnov | 404e514 | 2016-08-09 10:05:49 | [diff] [blame] | 516 | if (!frame->used) { |
Anton Khirnov | ce320cf | 2016-06-22 09:44:09 | [diff] [blame] | 517 | ret = alloc_frame(avctx, q, frame); |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 518 | if (ret < 0) |
| 519 | return ret; |
Anton Khirnov | 404e514 | 2016-08-09 10:05:49 | [diff] [blame] | 520 | *surf = &frame->surface; |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | last = &frame->next; |
| 525 | frame = frame->next; |
| 526 | } |
| 527 | |
| 528 | frame = av_mallocz(sizeof(*frame)); |
| 529 | if (!frame) |
| 530 | return AVERROR(ENOMEM); |
| 531 | frame->frame = av_frame_alloc(); |
| 532 | if (!frame->frame) { |
| 533 | av_freep(&frame); |
| 534 | return AVERROR(ENOMEM); |
| 535 | } |
| 536 | *last = frame; |
| 537 | |
Anton Khirnov | ce320cf | 2016-06-22 09:44:09 | [diff] [blame] | 538 | ret = alloc_frame(avctx, q, frame); |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 539 | if (ret < 0) |
| 540 | return ret; |
| 541 | |
Anton Khirnov | 404e514 | 2016-08-09 10:05:49 | [diff] [blame] | 542 | *surf = &frame->surface; |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
Anton Khirnov | f5c4d38 | 2015-07-14 16:16:26 | [diff] [blame] | 547 | static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf) |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 548 | { |
| 549 | QSVFrame *cur = q->work_frames; |
| 550 | while (cur) { |
Anton Khirnov | 404e514 | 2016-08-09 10:05:49 | [diff] [blame] | 551 | if (surf == &cur->surface) |
Anton Khirnov | f5c4d38 | 2015-07-14 16:16:26 | [diff] [blame] | 552 | return cur; |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 553 | cur = cur->next; |
| 554 | } |
| 555 | return NULL; |
| 556 | } |
| 557 | |
Haihao Xiang | 8ca06a8 | 2022-01-24 08:24:55 | [diff] [blame] | 558 | #if QSV_VERSION_ATLEAST(1, 34) |
| 559 | static int qsv_export_film_grain(AVCodecContext *avctx, mfxExtAV1FilmGrainParam *ext_param, AVFrame *frame) |
| 560 | { |
| 561 | AVFilmGrainParams *fgp; |
| 562 | AVFilmGrainAOMParams *aom; |
| 563 | int i; |
| 564 | |
| 565 | if (!(ext_param->FilmGrainFlags & MFX_FILM_GRAIN_APPLY)) |
| 566 | return 0; |
| 567 | |
| 568 | fgp = av_film_grain_params_create_side_data(frame); |
| 569 | |
| 570 | if (!fgp) |
| 571 | return AVERROR(ENOMEM); |
| 572 | |
| 573 | fgp->type = AV_FILM_GRAIN_PARAMS_AV1; |
| 574 | fgp->seed = ext_param->GrainSeed; |
| 575 | aom = &fgp->codec.aom; |
| 576 | |
| 577 | aom->chroma_scaling_from_luma = !!(ext_param->FilmGrainFlags & MFX_FILM_GRAIN_CHROMA_SCALING_FROM_LUMA); |
| 578 | aom->scaling_shift = ext_param->GrainScalingMinus8 + 8; |
| 579 | aom->ar_coeff_lag = ext_param->ArCoeffLag; |
| 580 | aom->ar_coeff_shift = ext_param->ArCoeffShiftMinus6 + 6; |
| 581 | aom->grain_scale_shift = ext_param->GrainScaleShift; |
| 582 | aom->overlap_flag = !!(ext_param->FilmGrainFlags & MFX_FILM_GRAIN_OVERLAP); |
| 583 | aom->limit_output_range = !!(ext_param->FilmGrainFlags & MFX_FILM_GRAIN_CLIP_TO_RESTRICTED_RANGE); |
| 584 | |
| 585 | aom->num_y_points = ext_param->NumYPoints; |
| 586 | |
| 587 | for (i = 0; i < aom->num_y_points; i++) { |
| 588 | aom->y_points[i][0] = ext_param->PointY[i].Value; |
| 589 | aom->y_points[i][1] = ext_param->PointY[i].Scaling; |
| 590 | } |
| 591 | |
| 592 | aom->num_uv_points[0] = ext_param->NumCbPoints; |
| 593 | |
| 594 | for (i = 0; i < aom->num_uv_points[0]; i++) { |
| 595 | aom->uv_points[0][i][0] = ext_param->PointCb[i].Value; |
| 596 | aom->uv_points[0][i][1] = ext_param->PointCb[i].Scaling; |
| 597 | } |
| 598 | |
| 599 | aom->num_uv_points[1] = ext_param->NumCrPoints; |
| 600 | |
| 601 | for (i = 0; i < aom->num_uv_points[1]; i++) { |
| 602 | aom->uv_points[1][i][0] = ext_param->PointCr[i].Value; |
| 603 | aom->uv_points[1][i][1] = ext_param->PointCr[i].Scaling; |
| 604 | } |
| 605 | |
| 606 | for (i = 0; i < 24; i++) |
| 607 | aom->ar_coeffs_y[i] = ext_param->ArCoeffsYPlus128[i] - 128; |
| 608 | |
| 609 | for (i = 0; i < 25; i++) { |
| 610 | aom->ar_coeffs_uv[0][i] = ext_param->ArCoeffsCbPlus128[i] - 128; |
| 611 | aom->ar_coeffs_uv[1][i] = ext_param->ArCoeffsCrPlus128[i] - 128; |
| 612 | } |
| 613 | |
| 614 | aom->uv_mult[0] = ext_param->CbMult; |
| 615 | aom->uv_mult[1] = ext_param->CrMult; |
| 616 | aom->uv_mult_luma[0] = ext_param->CbLumaMult; |
| 617 | aom->uv_mult_luma[1] = ext_param->CrLumaMult; |
| 618 | aom->uv_offset[0] = ext_param->CbOffset; |
| 619 | aom->uv_offset[1] = ext_param->CrOffset; |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | #endif |
| 624 | |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 625 | static int qsv_decode(AVCodecContext *avctx, QSVContext *q, |
| 626 | AVFrame *frame, int *got_frame, |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 627 | const AVPacket *avpkt) |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 628 | { |
| 629 | mfxFrameSurface1 *insurf; |
| 630 | mfxFrameSurface1 *outsurf; |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 631 | mfxSyncPoint *sync; |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 632 | mfxBitstream bs = { { { 0 } } }; |
| 633 | int ret; |
| 634 | |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 635 | if (avpkt->size) { |
| 636 | bs.Data = avpkt->data; |
| 637 | bs.DataLength = avpkt->size; |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 638 | bs.MaxLength = bs.DataLength; |
Haihao Xiang | 971b4ac | 2021-06-11 02:19:45 | [diff] [blame] | 639 | bs.TimeStamp = PTS_TO_MFX_PTS(avpkt->pts, avctx->pkt_timebase); |
Zhong Li | 54307b3 | 2018-04-07 17:38:55 | [diff] [blame] | 640 | if (avctx->field_order == AV_FIELD_PROGRESSIVE) |
| 641 | bs.DataFlag |= MFX_BITSTREAM_COMPLETE_FRAME; |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 642 | } |
| 643 | |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 644 | sync = av_mallocz(sizeof(*sync)); |
| 645 | if (!sync) { |
| 646 | av_freep(&sync); |
| 647 | return AVERROR(ENOMEM); |
| 648 | } |
| 649 | |
| 650 | do { |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 651 | ret = get_surface(avctx, q, &insurf); |
Timothy Gu | b6f80b1 | 2016-12-05 18:20:26 | [diff] [blame] | 652 | if (ret < 0) { |
| 653 | av_freep(&sync); |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 654 | return ret; |
Timothy Gu | b6f80b1 | 2016-12-05 18:20:26 | [diff] [blame] | 655 | } |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 656 | |
| 657 | ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL, |
| 658 | insurf, &outsurf, sync); |
| 659 | if (ret == MFX_WRN_DEVICE_BUSY) |
Ivan Uskov | 9f543e0 | 2015-07-24 11:45:38 | [diff] [blame] | 660 | av_usleep(500); |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 661 | |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 662 | } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE); |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 663 | |
Wenbin Chen | 40f3865 | 2022-03-18 06:25:09 | [diff] [blame] | 664 | if (ret == MFX_ERR_INCOMPATIBLE_VIDEO_PARAM) { |
| 665 | q->reinit_flag = 1; |
| 666 | av_log(avctx, AV_LOG_DEBUG, "Video parameter change\n"); |
| 667 | av_freep(&sync); |
| 668 | return 0; |
| 669 | } |
| 670 | |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 671 | if (ret != MFX_ERR_NONE && |
| 672 | ret != MFX_ERR_MORE_DATA && |
| 673 | ret != MFX_WRN_VIDEO_PARAM_CHANGED && |
| 674 | ret != MFX_ERR_MORE_SURFACE) { |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 675 | av_freep(&sync); |
Anton Khirnov | 95414eb | 2016-06-25 19:38:10 | [diff] [blame] | 676 | return ff_qsv_print_error(avctx, ret, |
| 677 | "Error during QSV decoding."); |
Anton Khirnov | f5c4d38 | 2015-07-14 16:16:26 | [diff] [blame] | 678 | } |
| 679 | |
Anton Khirnov | aa9d15d | 2015-07-09 18:08:13 | [diff] [blame] | 680 | /* make sure we do not enter an infinite loop if the SDK |
| 681 | * did not consume any data and did not return anything */ |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 682 | if (!*sync && !bs.DataOffset) { |
Anton Khirnov | aa9d15d | 2015-07-09 18:08:13 | [diff] [blame] | 683 | bs.DataOffset = avpkt->size; |
Mark Thompson | 0940b74 | 2016-10-30 16:58:23 | [diff] [blame] | 684 | ++q->zero_consume_run; |
| 685 | if (q->zero_consume_run > 1) |
| 686 | ff_qsv_print_warning(avctx, ret, "A decode call did not consume any data"); |
| 687 | } else { |
| 688 | q->zero_consume_run = 0; |
Anton Khirnov | aa9d15d | 2015-07-09 18:08:13 | [diff] [blame] | 689 | } |
| 690 | |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 691 | if (*sync) { |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 692 | QSVAsyncFrame aframe; |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 693 | QSVFrame *out_frame = find_frame(q, outsurf); |
| 694 | |
| 695 | if (!out_frame) { |
| 696 | av_log(avctx, AV_LOG_ERROR, |
| 697 | "The returned surface does not correspond to any frame\n"); |
| 698 | av_freep(&sync); |
| 699 | return AVERROR_BUG; |
| 700 | } |
| 701 | |
Chen,Wenbin | e6b990e | 2021-03-12 02:44:06 | [diff] [blame] | 702 | out_frame->queued += 1; |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 703 | |
| 704 | aframe = (QSVAsyncFrame){ sync, out_frame }; |
| 705 | av_fifo_write(q->async_fifo, &aframe, 1); |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 706 | } else { |
| 707 | av_freep(&sync); |
Ivan Uskov | c90dbc6 | 2015-07-24 10:26:14 | [diff] [blame] | 708 | } |
| 709 | |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 710 | if ((av_fifo_can_read(q->async_fifo) >= q->async_depth) || |
| 711 | (!avpkt->size && av_fifo_can_read(q->async_fifo))) { |
| 712 | QSVAsyncFrame aframe; |
Anton Khirnov | f5c4d38 | 2015-07-14 16:16:26 | [diff] [blame] | 713 | AVFrame *src_frame; |
| 714 | |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 715 | av_fifo_read(q->async_fifo, &aframe, 1); |
| 716 | aframe.frame->queued -= 1; |
Anton Khirnov | f5c4d38 | 2015-07-14 16:16:26 | [diff] [blame] | 717 | |
Anton Khirnov | b68e353 | 2017-01-07 20:06:16 | [diff] [blame] | 718 | if (avctx->pix_fmt != AV_PIX_FMT_QSV) { |
| 719 | do { |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 720 | ret = MFXVideoCORE_SyncOperation(q->session, *aframe.sync, 1000); |
Anton Khirnov | b68e353 | 2017-01-07 20:06:16 | [diff] [blame] | 721 | } while (ret == MFX_WRN_IN_EXECUTION); |
| 722 | } |
Anton Khirnov | f5c4d38 | 2015-07-14 16:16:26 | [diff] [blame] | 723 | |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 724 | av_freep(&aframe.sync); |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 725 | |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 726 | src_frame = aframe.frame->frame; |
Anton Khirnov | f5c4d38 | 2015-07-14 16:16:26 | [diff] [blame] | 727 | |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 728 | ret = av_frame_ref(frame, src_frame); |
| 729 | if (ret < 0) |
| 730 | return ret; |
| 731 | |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 732 | outsurf = &aframe.frame->surface; |
Anton Khirnov | f5c4d38 | 2015-07-14 16:16:26 | [diff] [blame] | 733 | |
Haihao Xiang | 971b4ac | 2021-06-11 02:19:45 | [diff] [blame] | 734 | frame->pts = MFX_PTS_TO_PTS(outsurf->Data.TimeStamp, avctx->pkt_timebase); |
Haihao Xiang | 8ca06a8 | 2022-01-24 08:24:55 | [diff] [blame] | 735 | #if QSV_VERSION_ATLEAST(1, 34) |
| 736 | if ((avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) && |
| 737 | QSV_RUNTIME_VERSION_ATLEAST(q->ver, 1, 34) && |
| 738 | avctx->codec_id == AV_CODEC_ID_AV1) { |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 739 | ret = qsv_export_film_grain(avctx, &aframe.frame->av1_film_grain_param, frame); |
Haihao Xiang | 8ca06a8 | 2022-01-24 08:24:55 | [diff] [blame] | 740 | |
| 741 | if (ret < 0) |
| 742 | return ret; |
| 743 | } |
| 744 | #endif |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 745 | |
| 746 | frame->repeat_pict = |
| 747 | outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 : |
| 748 | outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 : |
| 749 | outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0; |
| 750 | frame->top_field_first = |
| 751 | outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF; |
| 752 | frame->interlaced_frame = |
| 753 | !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE); |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 754 | frame->pict_type = ff_qsv_map_pictype(aframe.frame->dec_info.FrameType); |
Zhong Li | 52ed83f | 2018-04-04 09:51:29 | [diff] [blame] | 755 | //Key frame is IDR frame is only suitable for H264. For HEVC, IRAPs are key frames. |
| 756 | if (avctx->codec_id == AV_CODEC_ID_H264) |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 757 | frame->key_frame = !!(aframe.frame->dec_info.FrameType & MFX_FRAMETYPE_IDR); |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 758 | |
Anton Khirnov | 404e514 | 2016-08-09 10:05:49 | [diff] [blame] | 759 | /* update the surface properties */ |
| 760 | if (avctx->pix_fmt == AV_PIX_FMT_QSV) |
| 761 | ((mfxFrameSurface1*)frame->data[3])->Info = outsurf->Info; |
| 762 | |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 763 | *got_frame = 1; |
| 764 | } |
| 765 | |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 766 | return bs.DataOffset; |
Ivan Uskov | 3f8e2e9 | 2015-08-06 16:10:24 | [diff] [blame] | 767 | } |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 768 | |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 769 | static void qsv_decode_close_qsvcontext(QSVContext *q) |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 770 | { |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 771 | QSVFrame *cur = q->work_frames; |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 772 | |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 773 | if (q->session) |
| 774 | MFXVideoDECODE_Close(q->session); |
Ivan Uskov | cc167f7 | 2015-08-04 10:40:06 | [diff] [blame] | 775 | |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 776 | if (q->async_fifo) { |
| 777 | QSVAsyncFrame aframe; |
| 778 | while (av_fifo_read(q->async_fifo, &aframe, 1) >= 0) |
| 779 | av_freep(&aframe.sync); |
| 780 | av_fifo_freep2(&q->async_fifo); |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | while (cur) { |
| 784 | q->work_frames = cur->next; |
| 785 | av_frame_free(&cur->frame); |
| 786 | av_freep(&cur); |
| 787 | cur = q->work_frames; |
| 788 | } |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 789 | |
Zhong Li | 74007dd | 2019-09-19 20:45:26 | [diff] [blame] | 790 | ff_qsv_close_internal_session(&q->internal_qs); |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 791 | |
| 792 | av_buffer_unref(&q->frames_ctx.hw_frames_ctx); |
Anton Khirnov | 4ab61cd | 2016-08-10 07:38:21 | [diff] [blame] | 793 | av_buffer_unref(&q->frames_ctx.mids_buf); |
Linjie Fu | 5345965 | 2019-10-08 13:41:02 | [diff] [blame] | 794 | av_buffer_pool_uninit(&q->pool); |
Anton Khirnov | 4e08c82 | 2015-02-10 09:40:59 | [diff] [blame] | 795 | } |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 796 | |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 797 | static int qsv_process_data(AVCodecContext *avctx, QSVContext *q, |
| 798 | AVFrame *frame, int *got_frame, const AVPacket *pkt) |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 799 | { |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 800 | int ret; |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 801 | mfxVideoParam param = { 0 }; |
| 802 | enum AVPixelFormat pix_fmt = AV_PIX_FMT_NV12; |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 803 | |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 804 | if (!pkt->size) |
| 805 | return qsv_decode(avctx, q, frame, got_frame, pkt); |
| 806 | |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 807 | /* TODO: flush delayed frames on reinit */ |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 808 | |
| 809 | // sw_pix_fmt, coded_width/height should be set for ff_get_format(), |
| 810 | // assume sw_pix_fmt is NV12 and coded_width/height to be 1280x720, |
| 811 | // the assumption may be not corret but will be updated after header decoded if not true. |
| 812 | if (q->orig_pix_fmt != AV_PIX_FMT_NONE) |
| 813 | pix_fmt = q->orig_pix_fmt; |
| 814 | if (!avctx->coded_width) |
| 815 | avctx->coded_width = 1280; |
| 816 | if (!avctx->coded_height) |
| 817 | avctx->coded_height = 720; |
| 818 | |
Wenbin Chen | 4a633eb | 2022-03-18 06:25:10 | [diff] [blame] | 819 | /* decode zero-size pkt to flush the buffered pkt before reinit */ |
| 820 | if (q->reinit_flag) { |
Linjie Fu | 8736888 | 2018-10-16 01:36:13 | [diff] [blame] | 821 | AVPacket zero_pkt = {0}; |
Wenbin Chen | 4a633eb | 2022-03-18 06:25:10 | [diff] [blame] | 822 | ret = qsv_decode(avctx, q, frame, got_frame, &zero_pkt); |
| 823 | if (ret < 0 || *got_frame) |
| 824 | return ret; |
| 825 | } |
Linjie Fu | 8736888 | 2018-10-16 01:36:13 | [diff] [blame] | 826 | |
Wenbin Chen | 4a633eb | 2022-03-18 06:25:10 | [diff] [blame] | 827 | if (q->reinit_flag || !q->session || !q->initialized) { |
Wenbin Chen | 0a0847d | 2022-03-18 06:25:11 | [diff] [blame] | 828 | mfxFrameAllocRequest request; |
| 829 | memset(&request, 0, sizeof(request)); |
| 830 | |
Linjie Fu | 8736888 | 2018-10-16 01:36:13 | [diff] [blame] | 831 | q->reinit_flag = 0; |
Wenbin Chen | 4a633eb | 2022-03-18 06:25:10 | [diff] [blame] | 832 | ret = qsv_decode_header(avctx, q, pkt, pix_fmt, ¶m); |
| 833 | if (ret < 0) { |
| 834 | if (ret == AVERROR(EAGAIN)) |
| 835 | av_log(avctx, AV_LOG_INFO, "More data is required to decode header\n"); |
| 836 | else |
| 837 | av_log(avctx, AV_LOG_ERROR, "Error decoding header\n"); |
| 838 | goto reinit_fail; |
| 839 | } |
Wenbin Chen | 0a0847d | 2022-03-18 06:25:11 | [diff] [blame] | 840 | param.IOPattern = q->iopattern; |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 841 | |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 842 | q->orig_pix_fmt = avctx->pix_fmt = pix_fmt = ff_qsv_map_fourcc(param.mfx.FrameInfo.FourCC); |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 843 | |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 844 | avctx->coded_width = param.mfx.FrameInfo.Width; |
| 845 | avctx->coded_height = param.mfx.FrameInfo.Height; |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 846 | |
Wenbin Chen | 0a0847d | 2022-03-18 06:25:11 | [diff] [blame] | 847 | ret = MFXVideoDECODE_QueryIOSurf(q->session, ¶m, &request); |
| 848 | if (ret < 0) |
| 849 | return ff_qsv_print_error(avctx, ret, "Error querying IO surface"); |
| 850 | |
| 851 | q->suggest_pool_size = request.NumFrameSuggested; |
| 852 | |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 853 | ret = qsv_decode_preinit(avctx, q, pix_fmt, ¶m); |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 854 | if (ret < 0) |
| 855 | goto reinit_fail; |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 856 | q->initialized = 0; |
| 857 | } |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 858 | |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 859 | if (!q->initialized) { |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 860 | ret = qsv_decode_init_context(avctx, q, ¶m); |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 861 | if (ret < 0) |
| 862 | goto reinit_fail; |
Zhong Li | 00d0a4a | 2019-08-13 06:11:09 | [diff] [blame] | 863 | q->initialized = 1; |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | return qsv_decode(avctx, q, frame, got_frame, pkt); |
| 867 | |
| 868 | reinit_fail: |
Zhong Li | 0dfcfc5 | 2019-08-13 06:11:10 | [diff] [blame] | 869 | q->orig_pix_fmt = avctx->pix_fmt = AV_PIX_FMT_NONE; |
Mark Thompson | 1f26a23 | 2016-10-21 17:57:12 | [diff] [blame] | 870 | return ret; |
| 871 | } |
| 872 | |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 873 | enum LoadPlugin { |
| 874 | LOAD_PLUGIN_NONE, |
| 875 | LOAD_PLUGIN_HEVC_SW, |
| 876 | LOAD_PLUGIN_HEVC_HW, |
| 877 | }; |
| 878 | |
| 879 | typedef struct QSVDecContext { |
| 880 | AVClass *class; |
| 881 | QSVContext qsv; |
| 882 | |
| 883 | int load_plugin; |
| 884 | |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 885 | AVFifo *packet_fifo; |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 886 | |
| 887 | AVPacket buffer_pkt; |
| 888 | } QSVDecContext; |
| 889 | |
| 890 | static void qsv_clear_buffers(QSVDecContext *s) |
| 891 | { |
| 892 | AVPacket pkt; |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 893 | while (av_fifo_read(s->packet_fifo, &pkt, 1) >= 0) |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 894 | av_packet_unref(&pkt); |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 895 | |
| 896 | av_packet_unref(&s->buffer_pkt); |
| 897 | } |
| 898 | |
| 899 | static av_cold int qsv_decode_close(AVCodecContext *avctx) |
| 900 | { |
| 901 | QSVDecContext *s = avctx->priv_data; |
| 902 | |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 903 | qsv_decode_close_qsvcontext(&s->qsv); |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 904 | |
| 905 | qsv_clear_buffers(s); |
| 906 | |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 907 | av_fifo_freep2(&s->packet_fifo); |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | static av_cold int qsv_decode_init(AVCodecContext *avctx) |
| 913 | { |
| 914 | QSVDecContext *s = avctx->priv_data; |
| 915 | int ret; |
Xu Guangxin | 7a5a5e6 | 2021-01-05 02:43:42 | [diff] [blame] | 916 | const char *uid = NULL; |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 917 | |
Xu Guangxin | d78ecf1 | 2021-01-05 02:43:41 | [diff] [blame] | 918 | if (avctx->codec_id == AV_CODEC_ID_VP8) { |
Xu Guangxin | 7a5a5e6 | 2021-01-05 02:43:42 | [diff] [blame] | 919 | uid = "f622394d8d87452f878c51f2fc9b4131"; |
Xu Guangxin | d78ecf1 | 2021-01-05 02:43:41 | [diff] [blame] | 920 | } else if (avctx->codec_id == AV_CODEC_ID_VP9) { |
Xu Guangxin | 7a5a5e6 | 2021-01-05 02:43:42 | [diff] [blame] | 921 | uid = "a922394d8d87452f878c51f2fc9b4131"; |
Xu Guangxin | d78ecf1 | 2021-01-05 02:43:41 | [diff] [blame] | 922 | } |
| 923 | else if (avctx->codec_id == AV_CODEC_ID_HEVC && s->load_plugin != LOAD_PLUGIN_NONE) { |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 924 | static const char * const uid_hevcdec_sw = "15dd936825ad475ea34e35f3f54217a6"; |
| 925 | static const char * const uid_hevcdec_hw = "33a61c0b4c27454ca8d85dde757c6f8e"; |
| 926 | |
| 927 | if (s->qsv.load_plugins[0]) { |
| 928 | av_log(avctx, AV_LOG_WARNING, |
| 929 | "load_plugins is not empty, but load_plugin is not set to 'none'." |
| 930 | "The load_plugin value will be ignored.\n"); |
| 931 | } else { |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 932 | if (s->load_plugin == LOAD_PLUGIN_HEVC_SW) |
Xu Guangxin | 7a5a5e6 | 2021-01-05 02:43:42 | [diff] [blame] | 933 | uid = uid_hevcdec_sw; |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 934 | else |
Xu Guangxin | 7a5a5e6 | 2021-01-05 02:43:42 | [diff] [blame] | 935 | uid = uid_hevcdec_hw; |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 936 | } |
| 937 | } |
Xu Guangxin | 7a5a5e6 | 2021-01-05 02:43:42 | [diff] [blame] | 938 | if (uid) { |
| 939 | av_freep(&s->qsv.load_plugins); |
| 940 | s->qsv.load_plugins = av_strdup(uid); |
| 941 | if (!s->qsv.load_plugins) |
| 942 | return AVERROR(ENOMEM); |
| 943 | } |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 944 | |
| 945 | s->qsv.orig_pix_fmt = AV_PIX_FMT_NV12; |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 946 | s->packet_fifo = av_fifo_alloc2(1, sizeof(AVPacket), |
| 947 | AV_FIFO_FLAG_AUTO_GROW); |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 948 | if (!s->packet_fifo) { |
| 949 | ret = AVERROR(ENOMEM); |
| 950 | goto fail; |
| 951 | } |
| 952 | |
Haihao Xiang | 971b4ac | 2021-06-11 02:19:45 | [diff] [blame] | 953 | if (!avctx->pkt_timebase.num) |
| 954 | av_log(avctx, AV_LOG_WARNING, "Invalid pkt_timebase, passing timestamps as-is.\n"); |
| 955 | |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 956 | return 0; |
| 957 | fail: |
| 958 | qsv_decode_close(avctx); |
| 959 | return ret; |
| 960 | } |
| 961 | |
Andreas Rheinhardt | ce7dbd0 | 2022-03-30 19:33:24 | [diff] [blame] | 962 | static int qsv_decode_frame(AVCodecContext *avctx, AVFrame *frame, |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 963 | int *got_frame, AVPacket *avpkt) |
| 964 | { |
| 965 | QSVDecContext *s = avctx->priv_data; |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 966 | int ret; |
| 967 | |
| 968 | /* buffer the input packet */ |
| 969 | if (avpkt->size) { |
| 970 | AVPacket input_ref; |
| 971 | |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 972 | ret = av_packet_ref(&input_ref, avpkt); |
| 973 | if (ret < 0) |
| 974 | return ret; |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 975 | av_fifo_write(s->packet_fifo, &input_ref, 1); |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | /* process buffered data */ |
| 979 | while (!*got_frame) { |
| 980 | /* prepare the input data */ |
| 981 | if (s->buffer_pkt.size <= 0) { |
| 982 | /* no more data */ |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 983 | if (!av_fifo_can_read(s->packet_fifo)) |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 984 | return avpkt->size ? avpkt->size : qsv_process_data(avctx, &s->qsv, frame, got_frame, avpkt); |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 985 | /* in progress of reinit, no read from fifo and keep the buffer_pkt */ |
| 986 | if (!s->qsv.reinit_flag) { |
| 987 | av_packet_unref(&s->buffer_pkt); |
Anton Khirnov | 0818162 | 2022-01-06 16:16:47 | [diff] [blame] | 988 | av_fifo_read(s->packet_fifo, &s->buffer_pkt, 1); |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 992 | ret = qsv_process_data(avctx, &s->qsv, frame, got_frame, &s->buffer_pkt); |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 993 | if (ret < 0){ |
| 994 | /* Drop buffer_pkt when failed to decode the packet. Otherwise, |
| 995 | the decoder will keep decoding the failure packet. */ |
| 996 | av_packet_unref(&s->buffer_pkt); |
| 997 | return ret; |
| 998 | } |
| 999 | if (s->qsv.reinit_flag) |
| 1000 | continue; |
| 1001 | |
| 1002 | s->buffer_pkt.size -= ret; |
| 1003 | s->buffer_pkt.data += ret; |
| 1004 | } |
| 1005 | |
| 1006 | return avpkt->size; |
| 1007 | } |
| 1008 | |
| 1009 | static void qsv_decode_flush(AVCodecContext *avctx) |
| 1010 | { |
| 1011 | QSVDecContext *s = avctx->priv_data; |
| 1012 | |
| 1013 | qsv_clear_buffers(s); |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 1014 | |
| 1015 | s->qsv.orig_pix_fmt = AV_PIX_FMT_NONE; |
| 1016 | s->qsv.initialized = 0; |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | #define OFFSET(x) offsetof(QSVDecContext, x) |
| 1020 | #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM |
| 1021 | |
| 1022 | #define DEFINE_QSV_DECODER_WITH_OPTION(x, X, bsf_name, opt) \ |
| 1023 | static const AVClass x##_qsv_class = { \ |
| 1024 | .class_name = #x "_qsv", \ |
| 1025 | .item_name = av_default_item_name, \ |
| 1026 | .option = opt, \ |
| 1027 | .version = LIBAVUTIL_VERSION_INT, \ |
| 1028 | }; \ |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1029 | const FFCodec ff_##x##_qsv_decoder = { \ |
| 1030 | .p.name = #x "_qsv", \ |
Andreas Rheinhardt | 48286d4 | 2022-08-29 11:38:02 | [diff] [blame] | 1031 | CODEC_LONG_NAME(#X " video (Intel Quick Sync Video acceleration)"), \ |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 1032 | .priv_data_size = sizeof(QSVDecContext), \ |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1033 | .p.type = AVMEDIA_TYPE_VIDEO, \ |
| 1034 | .p.id = AV_CODEC_ID_##X, \ |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 1035 | .init = qsv_decode_init, \ |
Andreas Rheinhardt | 4243da4 | 2022-03-30 21:28:24 | [diff] [blame] | 1036 | FF_CODEC_DECODE_CB(qsv_decode_frame), \ |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 1037 | .flush = qsv_decode_flush, \ |
| 1038 | .close = qsv_decode_close, \ |
| 1039 | .bsfs = bsf_name, \ |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1040 | .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HYBRID, \ |
| 1041 | .p.priv_class = &x##_qsv_class, \ |
| 1042 | .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12, \ |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 1043 | AV_PIX_FMT_P010, \ |
Wenbin Chen | e0ae810 | 2022-04-06 08:48:03 | [diff] [blame] | 1044 | AV_PIX_FMT_YUYV422, \ |
| 1045 | AV_PIX_FMT_Y210, \ |
Haihao Xiang | db85e01 | 2022-09-06 04:53:38 | [diff] [blame] | 1046 | AV_PIX_FMT_VUYX, \ |
Haihao Xiang | 1898dbd | 2022-10-06 07:35:36 | [diff] [blame^] | 1047 | AV_PIX_FMT_XV30, \ |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 1048 | AV_PIX_FMT_QSV, \ |
| 1049 | AV_PIX_FMT_NONE }, \ |
Andreas Rheinhardt | f0dc8fa | 2021-03-04 15:08:41 | [diff] [blame] | 1050 | .hw_configs = qsv_hw_configs, \ |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1051 | .p.wrapper_name = "qsv", \ |
Andreas Rheinhardt | 6aad120 | 2022-07-09 20:25:41 | [diff] [blame] | 1052 | .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE, \ |
Xu Guangxin | 399c1f9 | 2021-01-05 02:43:40 | [diff] [blame] | 1053 | }; \ |
| 1054 | |
| 1055 | #define DEFINE_QSV_DECODER(x, X, bsf_name) DEFINE_QSV_DECODER_WITH_OPTION(x, X, bsf_name, options) |
| 1056 | |
| 1057 | #if CONFIG_HEVC_QSV_DECODER |
| 1058 | static const AVOption hevc_options[] = { |
| 1059 | { "async_depth", "Internal parallelization depth, the higher the value the higher the latency.", OFFSET(qsv.async_depth), AV_OPT_TYPE_INT, { .i64 = ASYNC_DEPTH_DEFAULT }, 1, INT_MAX, VD }, |
| 1060 | |
| 1061 | { "load_plugin", "A user plugin to load in an internal session", OFFSET(load_plugin), AV_OPT_TYPE_INT, { .i64 = LOAD_PLUGIN_HEVC_HW }, LOAD_PLUGIN_NONE, LOAD_PLUGIN_HEVC_HW, VD, "load_plugin" }, |
| 1062 | { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_NONE }, 0, 0, VD, "load_plugin" }, |
| 1063 | { "hevc_sw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_SW }, 0, 0, VD, "load_plugin" }, |
| 1064 | { "hevc_hw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_HW }, 0, 0, VD, "load_plugin" }, |
| 1065 | |
| 1066 | { "load_plugins", "A :-separate list of hexadecimal plugin UIDs to load in an internal session", |
| 1067 | OFFSET(qsv.load_plugins), AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD }, |
| 1068 | |
| 1069 | { "gpu_copy", "A GPU-accelerated copy between video and system memory", OFFSET(qsv.gpu_copy), AV_OPT_TYPE_INT, { .i64 = MFX_GPUCOPY_DEFAULT }, MFX_GPUCOPY_DEFAULT, MFX_GPUCOPY_OFF, VD, "gpu_copy"}, |
| 1070 | { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_DEFAULT }, 0, 0, VD, "gpu_copy"}, |
| 1071 | { "on", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_ON }, 0, 0, VD, "gpu_copy"}, |
| 1072 | { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_OFF }, 0, 0, VD, "gpu_copy"}, |
| 1073 | { NULL }, |
| 1074 | }; |
| 1075 | DEFINE_QSV_DECODER_WITH_OPTION(hevc, HEVC, "hevc_mp4toannexb", hevc_options) |
| 1076 | #endif |
| 1077 | |
| 1078 | static const AVOption options[] = { |
| 1079 | { "async_depth", "Internal parallelization depth, the higher the value the higher the latency.", OFFSET(qsv.async_depth), AV_OPT_TYPE_INT, { .i64 = ASYNC_DEPTH_DEFAULT }, 1, INT_MAX, VD }, |
| 1080 | |
| 1081 | { "gpu_copy", "A GPU-accelerated copy between video and system memory", OFFSET(qsv.gpu_copy), AV_OPT_TYPE_INT, { .i64 = MFX_GPUCOPY_DEFAULT }, MFX_GPUCOPY_DEFAULT, MFX_GPUCOPY_OFF, VD, "gpu_copy"}, |
| 1082 | { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_DEFAULT }, 0, 0, VD, "gpu_copy"}, |
| 1083 | { "on", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_ON }, 0, 0, VD, "gpu_copy"}, |
| 1084 | { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_GPUCOPY_OFF }, 0, 0, VD, "gpu_copy"}, |
| 1085 | { NULL }, |
| 1086 | }; |
| 1087 | |
| 1088 | #if CONFIG_H264_QSV_DECODER |
| 1089 | DEFINE_QSV_DECODER(h264, H264, "h264_mp4toannexb") |
| 1090 | #endif |
Xu Guangxin | d78ecf1 | 2021-01-05 02:43:41 | [diff] [blame] | 1091 | |
| 1092 | #if CONFIG_MPEG2_QSV_DECODER |
| 1093 | DEFINE_QSV_DECODER(mpeg2, MPEG2VIDEO, NULL) |
| 1094 | #endif |
| 1095 | |
| 1096 | #if CONFIG_VC1_QSV_DECODER |
| 1097 | DEFINE_QSV_DECODER(vc1, VC1, NULL) |
| 1098 | #endif |
| 1099 | |
| 1100 | #if CONFIG_MJPEG_QSV_DECODER |
| 1101 | DEFINE_QSV_DECODER(mjpeg, MJPEG, NULL) |
| 1102 | #endif |
| 1103 | |
| 1104 | #if CONFIG_VP8_QSV_DECODER |
| 1105 | DEFINE_QSV_DECODER(vp8, VP8, NULL) |
| 1106 | #endif |
| 1107 | |
| 1108 | #if CONFIG_VP9_QSV_DECODER |
| 1109 | DEFINE_QSV_DECODER(vp9, VP9, NULL) |
| 1110 | #endif |
| 1111 | |
| 1112 | #if CONFIG_AV1_QSV_DECODER |
| 1113 | DEFINE_QSV_DECODER(av1, AV1, NULL) |
| 1114 | #endif |