| 1007 | } |
| 1008 | |
| 1009 | static int d3d12va_encode_init_rate_control(AVCodecContext *avctx) |
| 1010 | { |
| 1011 | D3D12VAEncodeContext *ctx = avctx->priv_data; |
| 1012 | int64_t rc_target_bitrate; |
| 1013 | int64_t rc_peak_bitrate; |
| 1014 | int rc_quality; |
| 1015 | int64_t hrd_buffer_size; |
| 1016 | int64_t hrd_initial_buffer_fullness; |
| 1017 | int fr_num, fr_den; |
| 1018 | const D3D12VAEncodeRCMode *rc_mode; |
| 1019 | |
| 1020 | #define SET_QP_RANGE(ctl) do { \ |
| 1021 | if (avctx->qmin > 0 || avctx->qmax > 0) { \ |
| 1022 | ctl->MinQP = avctx->qmin; \ |
| 1023 | ctl->MaxQP = avctx->qmax; \ |
| 1024 | ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QP_RANGE; \ |
| 1025 | } \ |
| 1026 | } while(0) |
| 1027 | |
| 1028 | #define SET_MAX_FRAME_SIZE(ctl) do { \ |
| 1029 | if (ctx->max_frame_size > 0) { \ |
| 1030 | ctl->MaxFrameBitSize = ctx->max_frame_size * 8; \ |
| 1031 | ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_MAX_FRAME_SIZE; \ |
| 1032 | } \ |
| 1033 | } while(0) |
| 1034 | |
| 1035 | // Rate control mode selection: |
| 1036 | // * If the user has set a mode explicitly with the rc_mode option, |
| 1037 | // use it and fail if it is not available. |
| 1038 | // * If an explicit QP option has been set, use CQP. |
| 1039 | // * If the codec is CQ-only, use CQP. |
| 1040 | // * If the QSCALE avcodec option is set, use CQP. |
| 1041 | // * If bitrate and quality are both set, try QVBR. |
| 1042 | // * If quality is set, try CQP. |
| 1043 | // * If bitrate and maxrate are set and have the same value, try CBR. |
| 1044 | // * If a bitrate is set, try VBR, then CBR. |
| 1045 | // * If no bitrate is set, try CQP. |
| 1046 | |
| 1047 | #define TRY_RC_MODE(mode, fail) do { \ |
| 1048 | rc_mode = &d3d12va_encode_rc_modes[mode]; \ |
| 1049 | if (!(rc_mode->d3d12_mode && check_rate_control_support(avctx, rc_mode))) { \ |
| 1050 | if (fail) { \ |
| 1051 | av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \ |
| 1052 | "RC mode.\n", rc_mode->name); \ |
| 1053 | return AVERROR(EINVAL); \ |
| 1054 | } \ |
| 1055 | av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \ |
| 1056 | "RC mode.\n", rc_mode->name); \ |
| 1057 | rc_mode = NULL; \ |
| 1058 | } else { \ |
| 1059 | goto rc_mode_found; \ |
| 1060 | } \ |
| 1061 | } while (0) |
| 1062 | |
| 1063 | if (ctx->explicit_rc_mode) |
| 1064 | TRY_RC_MODE(ctx->explicit_rc_mode, 1); |
| 1065 | |
| 1066 | if (ctx->explicit_qp) |
no test coverage detected