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