/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-rocdecode/checkouts/latest/utils/rocvideodecode/roc_video_dec.h Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-rocdecode/checkouts/latest/utils/rocvideodecode/roc_video_dec.h Source File#

rocDecode: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-rocdecode/checkouts/latest/utils/rocvideodecode/roc_video_dec.h Source File
roc_video_dec.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2023 - 2024 Advanced Micro Devices, Inc. All rights reserved.
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 #pragma once
24 
25 #include <assert.h>
26 #include <stdint.h>
27 #include <mutex>
28 #include <vector>
29 #include <string>
30 #include <iostream>
31 #include <sstream>
32 #include <string.h>
33 #include <queue>
34 #include <stdexcept>
35 #include <exception>
36 #include <cstring>
37 #include <hip/hip_runtime.h>
38 extern "C" {
39 #include "libavutil/md5.h"
40 #include "libavutil/mem.h"
41 }
42 #include "rocdecode.h"
43 #include "rocparser.h"
44 
53 #define MAX_FRAME_NUM 16
54 
55 typedef int (ROCDECAPI *PFNRECONFIGUEFLUSHCALLBACK)(void *, uint32_t, void *);
56 
57 typedef enum SeiAvcHevcPayloadType_enum {
58  SEI_TYPE_TIME_CODE = 136,
59  SEI_TYPE_USER_DATA_UNREGISTERED = 5
60 } SeiAvcHevcPayloadType;
61 
67 } OutputSurfaceMemoryType;
68 
69 #define TOSTR(X) std::to_string(static_cast<int>(X))
70 #define STR(X) std::string(X)
71 
72 #if DBGINFO
73 #define INFO(X) std::clog << "[INF] " << " {" << __func__ <<"} " << " " << X << std::endl;
74 #else
75 #define INFO(X) ;
76 #endif
77 #define ERR(X) std::cerr << "[ERR] " << " {" << __func__ <<"} " << " " << X << std::endl;
78 
79 
80 class RocVideoDecodeException : public std::exception {
81 public:
82 
83  explicit RocVideoDecodeException(const std::string& message, const int err_code):_message(message), _err_code(err_code) {}
84  explicit RocVideoDecodeException(const std::string& message):_message(message), _err_code(-1) {}
85  virtual const char* what() const throw() override {
86  return _message.c_str();
87  }
88  int Geterror_code() const { return _err_code; }
89 private:
90  std::string _message;
91  int _err_code;
92 };
93 
94 #define ROCDEC_THROW(X, CODE) throw RocVideoDecodeException(" { " + std::string(__func__) + " } " + X , CODE);
95 #define THROW(X) throw RocVideoDecodeException(" { " + std::string(__func__) + " } " + X);
96 
97 #define ROCDEC_API_CALL( rocDecAPI ) \
98  do { \
99  rocDecStatus error_code = rocDecAPI; \
100  if( error_code != ROCDEC_SUCCESS) { \
101  std::ostringstream error_log; \
102  error_log << #rocDecAPI << " returned " << rocDecGetErrorName(error_code) << " at " <<__FILE__ <<":" << __LINE__;\
103  ROCDEC_THROW(error_log.str(), error_code); \
104  } \
105  } while (0)
106 
107 #define HIP_API_CALL( call ) \
108  do { \
109  hipError_t hip_status = call; \
110  if (hip_status != hipSuccess) { \
111  const char *sz_err_name = NULL; \
112  sz_err_name = hipGetErrorName(hip_status); \
113  std::ostringstream error_log; \
114  error_log << "hip API error " << sz_err_name ; \
115  ROCDEC_THROW(error_log.str(), hip_status); \
116  } \
117  } \
118  while (0)
119 
120 
121 struct Rect {
122  int left;
123  int top;
124  int right;
125  int bottom;
126 };
127 
128 struct Dim {
129  int w, h;
130 };
131 
132 static inline int align(int value, int alignment) {
133  return (value + alignment - 1) & ~(alignment - 1);
134 }
135 
136 typedef struct DecFrameBuffer_ {
137  uint8_t *frame_ptr;
138  int64_t pts;
141 
142 
143 typedef struct OutputSurfaceInfoType {
144  uint32_t output_width;
145  uint32_t output_height;
146  uint32_t output_pitch;
147  uint32_t output_vstride;
148  uint32_t bytes_per_pixel;
149  uint32_t bit_depth;
150  uint32_t num_chroma_planes;
152  rocDecVideoSurfaceFormat surface_format;
153  OutputSurfaceMemoryType mem_type;
155 
156 typedef struct ReconfigParams_t {
157  PFNRECONFIGUEFLUSHCALLBACK p_fn_reconfigure_flush;
158  void *p_reconfig_user_struct;
159  uint32_t reconfig_flush_mode;
161 
163  public:
180  RocVideoDecoder(int device_id, OutputSurfaceMemoryType out_mem_type, rocDecVideoCodec codec, bool force_zero_latency = false,
181  const Rect *p_crop_rect = nullptr, bool extract_user_SEI_Message = false, uint32_t disp_delay = 0, int max_width = 0, int max_height = 0,
182  uint32_t clk_rate = 1000);
183  ~RocVideoDecoder();
184 
185  rocDecVideoCodec GetCodecId() { return codec_id_; }
186 
187  hipStream_t GetStream() {return hip_stream_;}
188 
192  uint32_t GetWidth() { assert(disp_width_); return disp_width_;}
193 
197  int GetDecodeWidth() { assert(coded_width_); return coded_width_; }
198 
202  uint32_t GetHeight() { assert(disp_height_); return disp_height_; }
203 
207  int GetChromaHeight() { assert(chroma_height_); return chroma_height_; }
208 
212  int GetNumChromaPlanes() { assert(num_chroma_planes_); return num_chroma_planes_; }
213 
217  int GetFrameSize() { assert(disp_width_); return disp_width_ * (disp_height_ + (chroma_height_ * num_chroma_planes_)) * byte_per_pixel_; }
218 
222  int GetFrameSizePitched() { assert(surface_stride_); return surface_stride_ * (disp_height_ + (chroma_height_ * num_chroma_planes_)); }
223 
229  uint32_t GetBitDepth() { assert(bitdepth_minus_8_); return (bitdepth_minus_8_ + 8); }
230  uint32_t GetBytePerPixel() { assert(byte_per_pixel_); return byte_per_pixel_; }
234  size_t GetSurfaceSize() { assert(surface_size_); return surface_size_; }
235  uint32_t GetSurfaceStride() { assert(surface_stride_); return surface_stride_; }
236  //RocDecImageFormat GetSubsampling() { return subsampling_; }
243  const char *GetCodecFmtName(rocDecVideoCodec codec_id);
244 
251  const char *GetSurfaceFmtName(rocDecVideoSurfaceFormat surface_format_id);
252 
261 
269  bool SetReconfigParams(ReconfigParams *p_reconfig_params);
279  int DecodeFrame(const uint8_t *data, size_t size, int pkt_flags, int64_t pts = 0);
284  uint8_t* GetFrame(int64_t *pts);
285 
294  bool ReleaseFrame(int64_t pTimestamp, bool b_flushing = false);
295 
304  //void SaveImage(std::string output_file_name, void* dev_mem, OutputImageInfo* image_info, bool is_output_RGB = 0);
305 
315  void GetDeviceinfo(std::string &device_name, std::string &gcn_arch_name, int &pci_bus_id, int &pci_domain_id, int &pci_device_id);
316 
325  void SaveFrameToFile(std::string output_file_name, void *surf_mem, OutputSurfaceInfo *surf_info, size_t rgb_image_size = 0);
326 
331 
335  void InitMd5();
336 
337  void UpdateMd5ForDataBuffer(void *pDevMem, int rgb_image_size);
338 
345  void UpdateMd5ForFrame(void *surf_mem, OutputSurfaceInfo *surf_info);
346 
352  void FinalizeMd5(uint8_t **digest);
358  int32_t GetNumOfFlushedFrames() { return num_frames_flushed_during_reconfig_;}
359 
360  private:
361  int decoder_session_id_; // Decoder session identifier. Used to gather session level stats.
365  static int ROCDECAPI HandleVideoSequenceProc(void *p_user_data, RocdecVideoFormat *p_video_format) { return ((RocVideoDecoder *)p_user_data)->HandleVideoSequence(p_video_format); }
366 
370  static int ROCDECAPI HandlePictureDecodeProc(void *p_user_data, RocdecPicParams *p_pic_params) { return ((RocVideoDecoder *)p_user_data)->HandlePictureDecode(p_pic_params); }
371 
375  static int ROCDECAPI HandlePictureDisplayProc(void *p_user_data, RocdecParserDispInfo *p_disp_info) { return ((RocVideoDecoder *)p_user_data)->HandlePictureDisplay(p_disp_info); }
376 
380  static int ROCDECAPI HandleSEIMessagesProc(void *p_user_data, RocdecSeiMessageInfo *p_sei_message_info) { return ((RocVideoDecoder *)p_user_data)->GetSEIMessage(p_sei_message_info); }
381 
386  int HandleVideoSequence(RocdecVideoFormat *p_video_format);
387 
392  int HandlePictureDecode(RocdecPicParams *p_pic_params);
393 
398  int HandlePictureDisplay(RocdecParserDispInfo *p_disp_info);
402  int GetSEIMessage(RocdecSeiMessageInfo *p_sei_message_info);
403 
407  int ReconfigureDecoder(RocdecVideoFormat *p_video_format);
408 
415  bool ReleaseInternalFrames();
416 
421  bool InitHIP(int device_id);
422 
423  int num_devices_;
424  int device_id_;
425  RocdecVideoParser rocdec_parser_ = nullptr;
426  rocDecDecoderHandle roc_decoder_ = nullptr;
427  OutputSurfaceMemoryType out_mem_type_ = OUT_SURFACE_MEM_DEV_INTERNAL;
428  bool b_extract_sei_message_ = false;
429  bool b_force_zero_latency_ = false;
430  uint32_t disp_delay_;
431  ReconfigParams *p_reconfig_params_ = nullptr;
432  int32_t num_frames_flushed_during_reconfig_ = 0;
433  hipDeviceProp_t hip_dev_prop_;
434  hipStream_t hip_stream_;
435  rocDecVideoCodec codec_id_ = rocDecVideoCodec_NumCodecs;
436  rocDecVideoChromaFormat video_chroma_format_ = rocDecVideoChromaFormat_420;
437  rocDecVideoSurfaceFormat video_surface_format_ = rocDecVideoSurfaceFormat_NV12;
438  RocdecSeiMessageInfo *curr_sei_message_ptr_ = nullptr;
439  RocdecSeiMessageInfo sei_message_display_q_[MAX_FRAME_NUM];
440  int decoded_frame_cnt_ = 0, decoded_frame_cnt_ret_ = 0;
441  int decode_poc_ = 0, pic_num_in_dec_order_[MAX_FRAME_NUM];
442  int num_alloced_frames_ = 0;
443  std::ostringstream input_video_info_str_;
444  int bitdepth_minus_8_ = 0;
445  uint32_t byte_per_pixel_ = 1;
446  uint32_t coded_width_ = 0;
447  uint32_t disp_width_ = 0;
448  uint32_t coded_height_ = 0;
449  uint32_t disp_height_ = 0;
450  uint32_t target_width_ = 0;
451  uint32_t target_height_ = 0;
452  int max_width_ = 0, max_height_ = 0;
453  uint32_t chroma_height_ = 0;
454  uint32_t num_chroma_planes_ = 0;
455  uint32_t num_components_ = 0;
456  uint32_t surface_stride_ = 0;
457  uint32_t surface_vstride_ = 0, chroma_vstride_ = 0; // vertical stride between planes: used when using internal dev memory
458  size_t surface_size_ = 0;
459  OutputSurfaceInfo output_surface_info_ = {};
460  std::mutex mtx_vp_frame_;
461  std::vector<DecFrameBuffer> vp_frames_; // vector of decoded frames
462  std::queue<DecFrameBuffer> vp_frames_q_;
463  Rect disp_rect_ = {}; // displayable area specified in the bitstream
464  Rect crop_rect_ = {}; // user specified region of interest within diplayable area disp_rect_
465  FILE *fp_sei_ = NULL;
466  FILE *fp_out_ = NULL;
467  struct AVMD5 *md5_ctx_;
468  uint8_t md5_digest_[16];
469  bool is_decoder_reconfigured_ = false;
470  std::string current_output_filename = "";
471  uint32_t extra_output_file_count_ = 0;
472 };
Definition: roc_video_dec.h:80
Definition: roc_video_dec.h:162
int GetFrameSize()
This function is used to get the current frame size based on pixel format.
Definition: roc_video_dec.h:217
void SaveFrameToFile(std::string output_file_name, void *surf_mem, OutputSurfaceInfo *surf_info, size_t rgb_image_size=0)
Helper function to dump decoded output surface to file.
uint8_t * GetFrame(int64_t *pts)
This function returns a decoded frame and timestamp. This should be called in a loop fetching all the...
int32_t GetNumOfFlushedFrames()
Get the Num Of Flushed Frames from video decoder object.
Definition: roc_video_dec.h:358
const char * GetCodecFmtName(rocDecVideoCodec codec_id)
Get the name of the output format.
uint32_t GetWidth()
Get the output frame width.
Definition: roc_video_dec.h:192
int GetNumChromaPlanes()
This function is used to get the number of chroma planes.
Definition: roc_video_dec.h:212
void InitMd5()
Helper function to start MD5 calculation.
int GetChromaHeight()
This function is used to get the current chroma height.
Definition: roc_video_dec.h:207
uint32_t GetHeight()
Get the output frame height.
Definition: roc_video_dec.h:202
bool SetReconfigParams(ReconfigParams *p_reconfig_params)
Function to set the Reconfig Params object.
uint32_t GetBitDepth()
Get the Bit Depth and BytesPerPixel associated with the pixel format.
Definition: roc_video_dec.h:229
int GetFrameSizePitched()
This function is used to get the current frame size based on pitch.
Definition: roc_video_dec.h:222
bool ReleaseFrame(int64_t pTimestamp, bool b_flushing=false)
function to release frame after use by the application: Only used with "OUT_SURFACE_MEM_DEV_INTERNAL"
void GetDeviceinfo(std::string &device_name, std::string &gcn_arch_name, int &pci_bus_id, int &pci_domain_id, int &pci_device_id)
utility function to save image to a file
const char * GetSurfaceFmtName(rocDecVideoSurfaceFormat surface_format_id)
function to return the name from surface_format_id
void ResetSaveFrameToFile()
Helper funtion to close a existing file and dump to new file in case of multiple files using same dec...
bool GetOutputSurfaceInfo(OutputSurfaceInfo **surface_info)
Get the pointer to the Output Image Info.
int DecodeFrame(const uint8_t *data, size_t size, int pkt_flags, int64_t pts=0)
this function decodes a frame and returns the number of frames avalable for display
size_t GetSurfaceSize()
Functions to get the output surface attributes.
Definition: roc_video_dec.h:234
RocVideoDecoder(int device_id, OutputSurfaceMemoryType out_mem_type, rocDecVideoCodec codec, bool force_zero_latency=false, const Rect *p_crop_rect=nullptr, bool extract_user_SEI_Message=false, uint32_t disp_delay=0, int max_width=0, int max_height=0, uint32_t clk_rate=1000)
Construct a new Roc Video Decoder object.
void UpdateMd5ForFrame(void *surf_mem, OutputSurfaceInfo *surf_info)
Helper function to dump decoded output surface to file.
void FinalizeMd5(uint8_t **digest)
Helper function to complete MD5 calculation.
int GetDecodeWidth()
This function is used to get the actual decode width.
Definition: roc_video_dec.h:197
OutputSurfaceMemoryType_enum
Definition: roc_video_dec.h:62
@ OUT_SURFACE_MEM_HOST_COPIED
Definition: roc_video_dec.h:65
@ OUT_SURFACE_MEM_DEV_COPIED
Definition: roc_video_dec.h:64
@ OUT_SURFACE_MEM_DEV_INTERNAL
Definition: roc_video_dec.h:63
@ OUT_SURFACE_MEM_NOT_MAPPED
Definition: roc_video_dec.h:66
The AMD rocDecode Library.
@ rocDecVideoCodec_NumCodecs
Definition: rocdecode.h:87
@ rocDecVideoSurfaceFormat_NV12
Definition: rocdecode.h:103
void * rocDecDecoderHandle
Definition: rocdecode.h:51
@ rocDecVideoChromaFormat_420
Definition: rocdecode.h:119
The AMD rocParser Library.
void * RocdecVideoParser
Definition: rocparser.h:46
Definition: roc_video_dec.h:136
uint8_t * frame_ptr
Definition: roc_video_dec.h:137
int picture_index
Definition: roc_video_dec.h:139
int64_t pts
Definition: roc_video_dec.h:138
Definition: roc_video_dec.h:128
Definition: roc_video_dec.h:143
uint32_t output_vstride
Definition: roc_video_dec.h:147
uint32_t output_pitch
Definition: roc_video_dec.h:146
uint32_t output_height
Definition: roc_video_dec.h:145
uint64_t output_surface_size_in_bytes
Definition: roc_video_dec.h:151
uint32_t num_chroma_planes
Definition: roc_video_dec.h:150
uint32_t bit_depth
Definition: roc_video_dec.h:149
uint32_t bytes_per_pixel
Definition: roc_video_dec.h:148
rocDecVideoSurfaceFormat surface_format
Definition: roc_video_dec.h:152
uint32_t output_width
Definition: roc_video_dec.h:144
OutputSurfaceMemoryType mem_type
Definition: roc_video_dec.h:153
Definition: roc_video_dec.h:156
Definition: roc_video_dec.h:121
Timing Info struct\Used in rocdecParseVideoData API with PFNVIDDISPLAYCALLBACK pfn_display_picture.
ROCDEC_VIDEO_FORMAT structUsed in Parser callback API.
Definition: rocparser.h:54
Definition: rocdecode.h:1374