2023-08-14 14:14:09 -07:00
|
|
|
/*
|
2025-01-05 16:25:39 -05:00
|
|
|
Copyright (c) 2023 - 2025 Advanced Micro Devices, Inc. All rights reserved.
|
2023-08-14 14:14:09 -07:00
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2023-11-01 09:22:46 -04:00
|
|
|
#include <iostream>
|
2024-03-28 21:40:45 -04:00
|
|
|
#include <fstream>
|
2023-11-01 09:22:46 -04:00
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
2025-01-27 08:10:11 -05:00
|
|
|
#include <dirent.h>
|
|
|
|
|
#include <sys/stat.h>
|
2024-03-25 14:31:02 -04:00
|
|
|
#include <cstring>
|
2025-01-10 14:44:06 -05:00
|
|
|
#include <mutex>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <libdrm/amdgpu_drm.h>
|
|
|
|
|
#include <libdrm/amdgpu.h>
|
2023-11-01 09:22:46 -04:00
|
|
|
#include <va/va.h>
|
|
|
|
|
#include <va/va_drm.h>
|
|
|
|
|
#include <va/va_drmcommon.h>
|
|
|
|
|
#include "../../commons.h"
|
2025-03-17 11:38:01 -05:00
|
|
|
#include "../../../api/rocdecode/rocdecode.h"
|
2023-11-01 09:22:46 -04:00
|
|
|
|
2025-01-10 14:44:06 -05:00
|
|
|
#define CHECK_HIP(call) {\
|
|
|
|
|
hipError_t hip_status = call;\
|
|
|
|
|
if (hip_status != hipSuccess) {\
|
2025-10-31 20:50:33 -04:00
|
|
|
logger_.CriticalLog(MakeMsg("HIP failure: " + #call + " failed with 'status: " + STR(hipGetErrorName(hip_status)) + "' at " + __FILE__ + ":" + TOSTR(__LINE__)));\
|
2025-01-10 14:44:06 -05:00
|
|
|
return ROCDEC_RUNTIME_ERROR;\
|
|
|
|
|
}\
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-28 16:52:27 -05:00
|
|
|
#define CHECK_VAAPI(call) {\
|
|
|
|
|
VAStatus va_status = call;\
|
|
|
|
|
if (va_status != VA_STATUS_SUCCESS) {\
|
2025-10-31 20:50:33 -04:00
|
|
|
logger_.CriticalLog(MakeMsg("VAAPI failure: " + #call + " failed with 'status: " + TOSTR(va_status) + ": " + vaErrorStr(va_status) + "' at " + __FILE__ + ":" + TOSTR(__LINE__)));\
|
2023-11-28 16:52:27 -05:00
|
|
|
return ROCDEC_RUNTIME_ERROR;\
|
|
|
|
|
}\
|
2023-11-01 09:22:46 -04:00
|
|
|
}
|
|
|
|
|
|
2024-02-09 15:09:49 -05:00
|
|
|
#define INIT_SLICE_PARAM_LIST_NUM 16 // initial slice parameter buffer list size
|
|
|
|
|
|
2024-03-28 21:40:45 -04:00
|
|
|
typedef enum {
|
|
|
|
|
kSpx = 0, // Single Partition Accelerator
|
|
|
|
|
kDpx = 1, // Dual Partition Accelerator
|
|
|
|
|
kTpx = 2, // Triple Partition Accelerator
|
|
|
|
|
kQpx = 3, // Quad Partition Accelerator
|
|
|
|
|
kCpx = 4, // Core Partition Accelerator
|
|
|
|
|
} ComputePartition;
|
|
|
|
|
|
2025-01-10 14:44:06 -05:00
|
|
|
typedef struct {
|
|
|
|
|
int device_id;
|
|
|
|
|
std::string gpu_uuid;
|
|
|
|
|
int drm_fd;
|
|
|
|
|
VADisplay va_display;
|
|
|
|
|
hipDeviceProp_t hip_dev_prop;
|
|
|
|
|
uint32_t num_dec_engines;
|
|
|
|
|
int num_va_profiles;
|
|
|
|
|
std::vector<VAProfile> va_profile_list; // supported profiles by the current GPU
|
|
|
|
|
VAProfile va_profile; // current profile used
|
|
|
|
|
VAConfigID va_config_id;
|
|
|
|
|
bool config_attributes_probed;
|
|
|
|
|
uint32_t rt_format_attrib;
|
|
|
|
|
uint32_t output_format_mask;
|
|
|
|
|
uint32_t max_width;
|
|
|
|
|
uint32_t max_height;
|
|
|
|
|
uint32_t min_width;
|
|
|
|
|
uint32_t min_height;
|
|
|
|
|
} VaContextInfo;
|
|
|
|
|
|
2023-11-01 09:22:46 -04:00
|
|
|
class VaapiVideoDecoder {
|
2023-08-14 14:14:09 -07:00
|
|
|
public:
|
2023-11-01 09:22:46 -04:00
|
|
|
VaapiVideoDecoder(RocDecoderCreateInfo &decoder_create_info);
|
|
|
|
|
~VaapiVideoDecoder();
|
2025-01-10 14:44:06 -05:00
|
|
|
rocDecStatus InitializeDecoder();
|
2023-11-06 22:04:25 -05:00
|
|
|
rocDecStatus SubmitDecode(RocdecPicParams *pPicParams);
|
2023-11-09 08:47:58 -05:00
|
|
|
rocDecStatus GetDecodeStatus(int pic_idx, RocdecDecodeStatus* decode_status);
|
2023-11-10 17:02:57 -05:00
|
|
|
rocDecStatus ExportSurface(int pic_idx, VADRMPRIMESurfaceDescriptor &va_drm_prime_surface_desc);
|
2024-02-05 17:12:39 -05:00
|
|
|
rocDecStatus SyncSurface(int pic_idx);
|
2023-12-05 11:16:32 -05:00
|
|
|
rocDecStatus ReconfigureDecoder(RocdecReconfigureDecoderInfo *reconfig_params);
|
2025-01-10 14:44:06 -05:00
|
|
|
|
2023-11-01 09:22:46 -04:00
|
|
|
private:
|
|
|
|
|
RocDecoderCreateInfo decoder_create_info_;
|
|
|
|
|
VADisplay va_display_;
|
|
|
|
|
VAConfigAttrib va_config_attrib_;
|
|
|
|
|
VAConfigID va_config_id_;
|
2025-03-20 06:12:16 -07:00
|
|
|
VAProfile va_profile_;
|
2023-11-07 13:47:53 -05:00
|
|
|
VAContextID va_context_id_;
|
|
|
|
|
std::vector<VASurfaceID> va_surface_ids_;
|
2024-12-09 20:46:57 +01:00
|
|
|
bool supports_modifiers_;
|
2023-11-10 17:15:47 -05:00
|
|
|
|
|
|
|
|
VABufferID pic_params_buf_id_;
|
|
|
|
|
VABufferID iq_matrix_buf_id_;
|
2024-02-09 15:09:49 -05:00
|
|
|
std::vector<VABufferID> slice_params_buf_id_ = std::vector<VABufferID>(INIT_SLICE_PARAM_LIST_NUM, 0);
|
|
|
|
|
uint32_t num_slices_;
|
2023-11-10 17:15:47 -05:00
|
|
|
VABufferID slice_data_buf_id_;
|
2025-01-10 14:44:06 -05:00
|
|
|
|
2025-10-31 20:50:33 -04:00
|
|
|
RocDecLogger logger_;
|
|
|
|
|
|
2025-01-10 14:44:06 -05:00
|
|
|
bool IsCodecConfigSupported(int device_id, rocDecVideoCodec codec_type, rocDecVideoChromaFormat chroma_format, uint32_t bit_depth_minus8, rocDecVideoSurfaceFormat output_format);
|
|
|
|
|
rocDecStatus CreateDecoderConfig();
|
|
|
|
|
rocDecStatus CreateSurfaces();
|
|
|
|
|
rocDecStatus CreateContext();
|
|
|
|
|
rocDecStatus DestroyDataBuffers();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// The VaContext singleton class providing access to the the GPU VA services
|
|
|
|
|
class VaContext {
|
|
|
|
|
public:
|
|
|
|
|
int num_devices_;
|
|
|
|
|
std::vector<VaContextInfo> va_contexts_;
|
|
|
|
|
|
|
|
|
|
static VaContext& GetInstance() {
|
|
|
|
|
static VaContext instance;
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
rocDecStatus GetVaContext(int device_id, uint32_t *va_ctx_id);
|
|
|
|
|
rocDecStatus GetVaDisplay(uint32_t va_ctx_id, VADisplay *va_display);
|
|
|
|
|
rocDecStatus CheckDecCapForCodecType(RocdecDecodeCaps *dec_cap);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::mutex mutex;
|
2024-12-19 14:12:57 -05:00
|
|
|
/**
|
|
|
|
|
* @brief A map that associates GPU UUIDs with their corresponding render node indices.
|
|
|
|
|
*
|
|
|
|
|
* This unordered map uses GPU UUIDs as keys (std::string) and maps them to their
|
|
|
|
|
* respective render node indices (int). It provides a fast lookup mechanism to
|
|
|
|
|
* retrieve the render node index for a given GPU UUID.
|
|
|
|
|
*/
|
|
|
|
|
std::unordered_map<std::string, int> gpu_uuids_to_render_nodes_map_;
|
2025-01-18 10:12:02 -05:00
|
|
|
std::unordered_map<std::string, ComputePartition> gpu_uuids_to_compute_partition_map_;
|
2025-01-10 14:44:06 -05:00
|
|
|
VaContext();
|
|
|
|
|
VaContext(const VaContext&) = delete;
|
|
|
|
|
VaContext& operator = (const VaContext) = delete;
|
|
|
|
|
~VaContext();
|
|
|
|
|
|
2025-10-31 20:50:33 -04:00
|
|
|
RocDecLogger logger_;
|
|
|
|
|
|
2025-01-10 14:44:06 -05:00
|
|
|
rocDecStatus InitHIP(int device_id, hipDeviceProp_t& hip_dev_prop);
|
|
|
|
|
rocDecStatus InitVAAPI(int va_ctx_idx, std::string drm_node);
|
|
|
|
|
void GetVisibleDevices(std::vector<int>& visible_devices_vetor);
|
2025-01-18 10:12:02 -05:00
|
|
|
void GetDrmNodeOffset(std::string device_name, uint8_t device_id, std::vector<int>& visible_devices, ComputePartition current_compute_partition, int &offset);
|
2024-12-19 14:12:57 -05:00
|
|
|
void GetGpuUuids();
|
2023-08-14 14:14:09 -07:00
|
|
|
};
|