libxcoder  5.2.0
ni_p2p_read_test.c File Reference

Application for performing video processing using libxcoder API and P2P DMA. Its code provides examples on how to programatically use libxcoder API in conjunction with P2P DMA. More...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <time.h>
#include <sys/mman.h>
#include "ni_device_api.h"
#include "ni_util.h"
#include <sys/ioctl.h>
#include "ni_p2p_ioctl.h"

Go to the source code of this file.

Data Structures

struct  swapchain_t
 

Macros

#define _POSIX_C_SOURCE   200809L
 
#define MAX_YUV_FRAME_SIZE   (7680 * 4320 * 3 / 2)
 
#define MAX_ABGR_FRAME_SIZE   (7680 * 4320 * 4)
 
#define MAX_FRAME_SIZE   MAX_ABGR_FRAME_SIZE
 
#define FILE_NAME_LEN   256
 
#define MAX_SWAP_SIZE   3
 

Functions

void arg_error_exit (char *arg_name, char *param)
 Exit on argument error. More...
 
int read_next_chunk_from_file (int fd, uint8_t *p_dst, uint32_t to_read)
 Read the next frame. More...
 
int get_file_size (const char *filename, unsigned long *bytes_read)
 Get file size. More...
 
int recycle_frame (ni_frame_t *p2p_frame)
 Recycle hw frame back to Quadra. More...
 
int gpu_render_frame (ni_session_context_t *p_ctx, int fd, uint8_t **pp_data, ni_frame_t *p_in_frame, int input_video_width, int input_video_height, unsigned long *bytes_sent, int *input_exhausted)
 Reads video data from input file then calls a special libxcoder API function to transfer the video data into the hardware frame on the proxy GPU Quadra device. More...
 
int gpu_prepare_frame (ni_session_context_t *p_gpu_ctx, int input_video_width, int input_video_height, ni_frame_t *gpu_frame)
 Prepare frame on the proxy GPU Quadra device. More...
 
int enc_prepare_frame (ni_session_context_t *p_upl_ctx, int input_video_width, int input_video_height, ni_frame_t *p2p_frame)
 Prepare frame on the encoding Quadra device. More...
 
int encoder_encode_frame (ni_session_context_t *p_enc_ctx, ni_frame_t *p_in_frame, int input_exhausted, int *need_to_resend)
 Send the Quadra encoder a hardware frame which triggers Quadra to encode the frame. More...
 
int encoder_receive_data (ni_session_context_t *p_enc_ctx, ni_session_data_io_t *p_out_data, FILE *p_file, unsigned long long *total_bytes_received, int print_time)
 Receive output packet data from the Quadra encoder. More...
 
int encoder_open_session (ni_session_context_t *p_enc_ctx, int dst_codec_format, int iXcoderGUID, ni_xcoder_params_t *p_enc_params, int width, int height, ni_frame_t *p_frame)
 Open an encoder session to Quadra. More...
 
int uploader_open_session (ni_session_context_t *p_upl_ctx, int *iXcoderGUID, int width, int height, int poolsize, int p2p)
 Open an upload session to Quadra. More...
 
void print_usage (void)
 Print usage information. More...
 
void parse_arguments (int argc, char *argv[], char *input_filename, char *output_filename, int *iXcoderGUID, int *iGpuGUID, int *arg_width, int *arg_height, int *dst_codec_format)
 Parse user command line arguments. More...
 
int main (int argc, char *argv[])
 

Variables

int send_fin_flag = 0
 
int receive_fin_flag = 0
 
int enc_eos_sent = 0
 
uint32_t number_of_frames = 0
 
uint32_t number_of_packets = 0
 
uint64_t data_left_size = 0
 
int g_repeat = 1
 
struct timeval start_time
 
struct timeval previous_time
 
struct timeval current_time
 
time_t start_timestamp = 0
 
time_t previous_timestamp = 0
 
time_t current_timestamp = 0
 
unsigned long total_file_size = 0
 
uint8_t * g_curr_cache_pos = NULL
 
uint8_t * g_raw_frame = NULL
 
uint8_t g_rgb2yuv_csc = 0
 
int g_swapchain_size = 1
 

Detailed Description

Application for performing video processing using libxcoder API and P2P DMA. Its code provides examples on how to programatically use libxcoder API in conjunction with P2P DMA.


This test program requires TWO Quadra devices. One Quadra device acts as a proxy for the GPU card. The other Quadra device reads frames from the proxy GPU Quadra device via peer-to-peer then encodes the frame.

Definition in file ni_p2p_read_test.c.

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 40 of file ni_p2p_read_test.c.

◆ FILE_NAME_LEN

#define FILE_NAME_LEN   256

Definition at line 66 of file ni_p2p_read_test.c.

◆ MAX_ABGR_FRAME_SIZE

#define MAX_ABGR_FRAME_SIZE   (7680 * 4320 * 4)

Definition at line 58 of file ni_p2p_read_test.c.

◆ MAX_FRAME_SIZE

#define MAX_FRAME_SIZE   MAX_ABGR_FRAME_SIZE

Definition at line 61 of file ni_p2p_read_test.c.

◆ MAX_SWAP_SIZE

#define MAX_SWAP_SIZE   3

Definition at line 67 of file ni_p2p_read_test.c.

◆ MAX_YUV_FRAME_SIZE

#define MAX_YUV_FRAME_SIZE   (7680 * 4320 * 3 / 2)

Definition at line 57 of file ni_p2p_read_test.c.

Function Documentation

◆ arg_error_exit()

void arg_error_exit ( char *  arg_name,
char *  param 
)

Exit on argument error.


Parameters
[in]arg_namepointer to argument name [in] param pointer to provided parameter
Returns
None program exit

Definition at line 107 of file ni_p2p_read_test.c.

◆ enc_prepare_frame()

int enc_prepare_frame ( ni_session_context_t p_upl_ctx,
int  input_video_width,
int  input_video_height,
ni_frame_t p2p_frame 
)

Prepare frame on the encoding Quadra device.


Parameters
[in]p_upl_ctxpointer to caller allocated upload session context [in] input_video_width video width [in] input_video_height video height [out] p2p_frame p2p frame
Returns
0 on success -1 on error

Definition at line 518 of file ni_p2p_read_test.c.

◆ encoder_encode_frame()

int encoder_encode_frame ( ni_session_context_t p_enc_ctx,
ni_frame_t p_in_frame,
int  input_exhausted,
int *  need_to_resend 
)

Send the Quadra encoder a hardware frame which triggers Quadra to encode the frame.


Parameters
[in]p_enc_ctxpointer to encoder context [in] p_in_frame pointer to hw frame [in] input_exhausted flag indicating this is the last frame [in/out] need_to_resend flag indicating need to re-send
Returns
0 on success -1 on failure

Definition at line 567 of file ni_p2p_read_test.c.

◆ encoder_open_session()

int encoder_open_session ( ni_session_context_t p_enc_ctx,
int  dst_codec_format,
int  iXcoderGUID,
ni_xcoder_params_t p_enc_params,
int  width,
int  height,
ni_frame_t p_frame 
)

Open an encoder session to Quadra.


Parameters
[out]p_enc_ctxpointer to an encoder session context [in] dst_codec_format AVC or HEVC [in] iXcoderGUID id to identify the Quadra device [in] p_enc_params sets the encoder parameters [in] width width of frames to encode [in] height height of frames to encode
Returns
0 if successful, < 0 otherwise

Definition at line 809 of file ni_p2p_read_test.c.

◆ encoder_receive_data()

int encoder_receive_data ( ni_session_context_t p_enc_ctx,
ni_session_data_io_t p_out_data,
FILE *  p_file,
unsigned long long *  total_bytes_received,
int  print_time 
)

Receive output packet data from the Quadra encoder.


Parameters
[in]p_enc_ctxpointer to encoder session context [in] p_out_data pointer to output data session [in] p_file pointer to file to write the packet [out] total_bytes_received running counter of bytes read [in] print_time 1 = print the time
Returns
0 - success got packet 1 - received eos 2 - got nothing, need retry -1 - failure

Definition at line 650 of file ni_p2p_read_test.c.

◆ get_file_size()

int get_file_size ( const char *  filename,
unsigned long *  bytes_read 
)

Get file size.


Parameters
[in]filenamename of input file [out] bytes_read number of bytes in file
Returns
0 on success < 0 on error

Definition at line 171 of file ni_p2p_read_test.c.

◆ gpu_prepare_frame()

int gpu_prepare_frame ( ni_session_context_t p_gpu_ctx,
int  input_video_width,
int  input_video_height,
ni_frame_t gpu_frame 
)

Prepare frame on the proxy GPU Quadra device.


Parameters
[in]p_gpu_ctxpointer to caller allocated gpu session context [in] input_video_width video width [in] input_video_height video height [out] gpu_frame gpu frame
Returns
0 on success -1 on error

Definition at line 467 of file ni_p2p_read_test.c.

◆ gpu_render_frame()

int gpu_render_frame ( ni_session_context_t p_ctx,
int  fd,
uint8_t **  pp_data,
ni_frame_t p_in_frame,
int  input_video_width,
int  input_video_height,
unsigned long *  bytes_sent,
int *  input_exhausted 
)

Reads video data from input file then calls a special libxcoder API function to transfer the video data into the hardware frame on the proxy GPU Quadra device.


Parameters
[in]p_ctxpointer to upload session context [in] fd file descriptor of input file [in] pp_data address of pointer to frame data [in] p_in_frame pointer to hardware frame [in] input_video_width video width [in] input_video_height video height [out] bytes_sent updated byte count of total data read [out] input_exhausted set to 1 when we reach end-of-file
Returns
0 on success -1 on error

Definition at line 309 of file ni_p2p_read_test.c.

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 1124 of file ni_p2p_read_test.c.

◆ parse_arguments()

void parse_arguments ( int  argc,
char *  argv[],
char *  input_filename,
char *  output_filename,
int *  iXcoderGUID,
int *  iGpuGUID,
int *  arg_width,
int *  arg_height,
int *  dst_codec_format 
)

Parse user command line arguments.


Parameters
[in]argcargument count [in] argv argument vector [out] input_filename input filename [out] output_filename output filename [out] iXcoderGUID Quadra device [out] iGpuGUID Quadra device (GPU proxy device) [out] arg_width resolution width [out] arg_height resolution height [out] dst_codec_format codec (AVC vs HEVC)
Returns
nothing program exit on error

Definition at line 970 of file ni_p2p_read_test.c.

◆ print_usage()

void print_usage ( void  )

Print usage information.


Parameters
none
Returns
none

Definition at line 920 of file ni_p2p_read_test.c.

◆ read_next_chunk_from_file()

int read_next_chunk_from_file ( int  fd,
uint8_t *  p_dst,
uint32_t  to_read 
)

Read the next frame.


Parameters
[in]fdfile descriptor of input file
[out]p_dstpointer to place the frame
[in]to_readnumber of bytes to copy to the pointer
Returns
bytes copied

Definition at line 123 of file ni_p2p_read_test.c.

◆ recycle_frame()

int recycle_frame ( ni_frame_t p2p_frame)

Recycle hw frame back to Quadra.


Parameters
[in]p2p_frame- hw frame to recycle
Returns
Returns NI_RETCODE_SUCCESS or error

Definition at line 201 of file ni_p2p_read_test.c.

◆ uploader_open_session()

int uploader_open_session ( ni_session_context_t p_upl_ctx,
int *  iXcoderGUID,
int  width,
int  height,
int  poolsize,
int  p2p 
)

Open an upload session to Quadra.


Parameters
[out]p_upl_ctxpointer to an upload context of the open session [in] iXcoderGUID pointer to Quadra card hw id [in] width width of the frames [in] height height of the frames [in] poolsize pool size to create on session [in] p2p p2p session
Returns
0 if successful, < 0 otherwise

Definition at line 865 of file ni_p2p_read_test.c.

Variable Documentation

◆ current_time

struct timeval current_time

Definition at line 80 of file ni_p2p_read_test.c.

◆ current_timestamp

time_t current_timestamp = 0

Definition at line 84 of file ni_p2p_read_test.c.

◆ data_left_size

uint64_t data_left_size = 0

Definition at line 75 of file ni_p2p_read_test.c.

◆ enc_eos_sent

int enc_eos_sent = 0

Definition at line 71 of file ni_p2p_read_test.c.

◆ g_curr_cache_pos

uint8_t* g_curr_cache_pos = NULL

Definition at line 88 of file ni_p2p_read_test.c.

◆ g_raw_frame

uint8_t* g_raw_frame = NULL

Definition at line 89 of file ni_p2p_read_test.c.

◆ g_repeat

int g_repeat = 1

Definition at line 76 of file ni_p2p_read_test.c.

◆ g_rgb2yuv_csc

uint8_t g_rgb2yuv_csc = 0

Definition at line 91 of file ni_p2p_read_test.c.

◆ g_swapchain_size

int g_swapchain_size = 1

Definition at line 93 of file ni_p2p_read_test.c.

◆ number_of_frames

uint32_t number_of_frames = 0

Definition at line 73 of file ni_p2p_read_test.c.

◆ number_of_packets

uint32_t number_of_packets = 0

Definition at line 74 of file ni_p2p_read_test.c.

◆ previous_time

struct timeval previous_time

Definition at line 79 of file ni_p2p_read_test.c.

◆ previous_timestamp

time_t previous_timestamp = 0

Definition at line 83 of file ni_p2p_read_test.c.

◆ receive_fin_flag

int receive_fin_flag = 0

Definition at line 70 of file ni_p2p_read_test.c.

◆ send_fin_flag

int send_fin_flag = 0

Definition at line 69 of file ni_p2p_read_test.c.

◆ start_time

struct timeval start_time

Definition at line 78 of file ni_p2p_read_test.c.

◆ start_timestamp

time_t start_timestamp = 0

Definition at line 82 of file ni_p2p_read_test.c.

◆ total_file_size

unsigned long total_file_size = 0

Definition at line 86 of file ni_p2p_read_test.c.