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