libxcoder  5.2.0
ni_av_codec.c File Reference

Audio/video related utility definitions. More...

#include <arpa/inet.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "ni_util.h"
#include "ni_nvme.h"
#include "ni_bitstream.h"
#include "ni_av_codec.h"
#include "ni_device_api_priv.h"

Go to the source code of this file.

Macros

#define BR_SHIFT   6
 
#define CPB_SHIFT   4
 
#define SAMPLE_SPS_MAX_SUB_LAYERS_MINUS1   0
 
#define MAX_VPS_MAX_SUB_LAYERS   16
 
#define MAX_CPB_COUNT   16
 
#define MAX_DURATION   0.5
 

Enumerations

enum  slice_type_t { SLICE_TYPE_B = 0, SLICE_TYPE_P = 1, SLICE_TYPE_I = 2, SLICE_TYPE_MP = 3 }
 
enum  gop_preset_t {
  GOP_PRESET_CUSTOM = 0, GOP_PRESET_I_1 = 1, GOP_PRESET_P_1 = 2, GOP_PRESET_B_1 = 3,
  GOP_PRESET_BP_2 = 4, GOP_PRESET_BBBP_3 = 5, GOP_PRESET_LP_4 = 6, GOP_PRESET_LD_4 = 7,
  GOP_PRESET_RA_8 = 8, GOP_PRESET_SP_1 = 9, GOP_PRESET_BSP_2 = 10, GOP_PRESET_BBBSP_3 = 11,
  GOP_PRESET_LSP_4 = 12, GOP_PRESET_BBP_3 = 13, GOP_PRESET_BBSP_3 = 14, GOP_PRESET_BBBBBBBP_8 = 15,
  GOP_PRESET_BBBBBBBSP_8 = 16, NUM_GOP_PRESET_NUM = 17
}
 

Functions

int ni_should_send_sei_with_frame (ni_session_context_t *p_enc_ctx, ni_pic_type_t pic_type, ni_xcoder_params_t *p_param)
 Whether SEI (HDR) should be sent together with this frame to encoder. More...
 
void ni_dec_retrieve_aux_data (ni_frame_t *frame)
 Retrieve auxiliary data (close caption, various SEI) associated with this frame that is returned by decoder, convert them to appropriate format and save them in the frame's auxiliary data storage for future use by encoding. Usually they would be sent together with this frame to encoder at encoding. More...
 
void ni_enc_prep_aux_data (ni_session_context_t *p_enc_ctx, ni_frame_t *p_enc_frame, ni_frame_t *p_dec_frame, ni_codec_format_t codec_format, int should_send_sei_with_frame, uint8_t *mdcv_data, uint8_t *cll_data, uint8_t *cc_data, uint8_t *udu_data, uint8_t *hdrp_data)
 Prepare auxiliary data that should be sent together with this frame to encoder based on the auxiliary data of the decoded frame. More...
 
void ni_enc_copy_aux_data (ni_session_context_t *p_enc_ctx, ni_frame_t *p_enc_frame, ni_frame_t *p_dec_frame, ni_codec_format_t codec_format, const uint8_t *mdcv_data, const uint8_t *cll_data, const uint8_t *cc_data, const uint8_t *udu_data, const uint8_t *hdrp_data, int is_hwframe, int is_semiplanar)
 Copy auxiliary data that should be sent together with this frame to encoder. More...
 
LIB_API int ni_enc_insert_timecode (ni_session_context_t *p_enc_ctx, ni_frame_t *p_enc_frame, ni_timecode_t *p_timecode)
 Insert timecode data into picture timing SEI (H264) or time code SEI (H265) More...
 
int ni_enc_write_from_yuv_buffer (ni_session_context_t *p_ctx, ni_frame_t *p_enc_frame, uint8_t *p_yuv_buffer)
 Send an input data frame to the encoder with YUV data given in the inputs. More...
 
const uint8_t * ni_find_start_code (const uint8_t *p, const uint8_t *end, uint32_t *state)
 Find the next start code. More...
 
int ni_extract_custom_sei (uint8_t *pkt_data, int pkt_size, long index, ni_packet_t *p_packet, uint8_t sei_type, int vcl_found)
 Extract custom sei payload data from pkt_data, and save it to ni_packet_t. More...
 
int ni_dec_packet_parse (ni_session_context_t *p_session_ctx, ni_xcoder_params_t *p_param, uint8_t *data, int size, ni_packet_t *p_packet, int low_delay, int codec_format, int pkt_nal_bitmap, int custom_sei_type, int *svct_skip_next_packet, int *is_lone_sei_pkt)
 Decode parse packet. More...
 
int ni_expand_frame (ni_frame_t *dst, ni_frame_t *src, int dst_stride[], int raw_width, int raw_height, int ni_fmt, int nb_planes)
 Expand frame form src frame. More...
 
int ni_reconfig_ppu_output (ni_session_context_t *p_session_ctx, ni_xcoder_params_t *p_param, ni_ppu_config_t *ppu_config)
 Reset decoder ppu resolution. More...
 

Detailed Description

Audio/video related utility definitions.


Definition in file ni_av_codec.c.

Macro Definition Documentation

◆ BR_SHIFT

#define BR_SHIFT   6

Definition at line 169 of file ni_av_codec.c.

◆ CPB_SHIFT

#define CPB_SHIFT   4

Definition at line 170 of file ni_av_codec.c.

◆ MAX_CPB_COUNT

#define MAX_CPB_COUNT   16

Definition at line 174 of file ni_av_codec.c.

◆ MAX_DURATION

#define MAX_DURATION   0.5

Definition at line 175 of file ni_av_codec.c.

◆ MAX_VPS_MAX_SUB_LAYERS

#define MAX_VPS_MAX_SUB_LAYERS   16

Definition at line 173 of file ni_av_codec.c.

◆ SAMPLE_SPS_MAX_SUB_LAYERS_MINUS1

#define SAMPLE_SPS_MAX_SUB_LAYERS_MINUS1   0

Definition at line 172 of file ni_av_codec.c.

Enumeration Type Documentation

◆ gop_preset_t

Enumerator
GOP_PRESET_CUSTOM 
GOP_PRESET_I_1 
GOP_PRESET_P_1 
GOP_PRESET_B_1 
GOP_PRESET_BP_2 
GOP_PRESET_BBBP_3 
GOP_PRESET_LP_4 
GOP_PRESET_LD_4 
GOP_PRESET_RA_8 
GOP_PRESET_SP_1 
GOP_PRESET_BSP_2 
GOP_PRESET_BBBSP_3 
GOP_PRESET_LSP_4 
GOP_PRESET_BBP_3 
GOP_PRESET_BBSP_3 
GOP_PRESET_BBBBBBBP_8 
GOP_PRESET_BBBBBBBSP_8 
NUM_GOP_PRESET_NUM 

Definition at line 51 of file ni_av_codec.c.

◆ slice_type_t

Enumerator
SLICE_TYPE_B 
SLICE_TYPE_P 
SLICE_TYPE_I 
SLICE_TYPE_MP 

Definition at line 43 of file ni_av_codec.c.

Function Documentation

◆ ni_dec_packet_parse()

int ni_dec_packet_parse ( ni_session_context_t p_session_ctx,
ni_xcoder_params_t p_param,
uint8_t *  data,
int  size,
ni_packet_t p_packet,
int  low_delay,
int  codec_format,
int  pkt_nal_bitmap,
int  custom_sei_type,
int *  svct_skip_next_packet,
int *  is_lone_sei_pkt 
)

Decode parse packet.


Parameters
[in]p_session_ctxPointer to a caller allocated ni_session_context_t struct
[in]p_paramPointer to a caller allocated ni_xcoder_params_t struct
[in]*dataFFmpeg AVPacket data
[in]sizepacket size
[in]p_packetPointer to a caller allocated ni_packet_t struct
[in]low_delayFFmpeg lowdelay
[in]codec_formatenum ni_codec_format_t
[in]pkt_nal_bitmappkt_nal_bitmap
[in]custom_sei_typecustom_sei_type
[in]<em>svct_skip_next_packetsvct_skip_next_packet int
[in]<em>is_lone_sei_pktis_lone_sei_pkt int
Returns
- 0 on success, non-0 on failure

Definition at line 3065 of file ni_av_codec.c.

◆ ni_dec_retrieve_aux_data()

void ni_dec_retrieve_aux_data ( ni_frame_t frame)

Retrieve auxiliary data (close caption, various SEI) associated with this frame that is returned by decoder, convert them to appropriate format and save them in the frame's auxiliary data storage for future use by encoding. Usually they would be sent together with this frame to encoder at encoding.


Parameters
[in/out]frame that is returned by decoder
Returns
NONE

Definition at line 237 of file ni_av_codec.c.

◆ ni_enc_copy_aux_data()

void ni_enc_copy_aux_data ( ni_session_context_t p_enc_ctx,
ni_frame_t p_enc_frame,
ni_frame_t p_dec_frame,
ni_codec_format_t  codec_format,
const uint8_t *  mdcv_data,
const uint8_t *  cll_data,
const uint8_t *  cc_data,
const uint8_t *  udu_data,
const uint8_t *  hdrp_data,
int  is_hwframe,
int  is_semiplanar 
)

Copy auxiliary data that should be sent together with this frame to encoder.


Parameters
[in]p_enc_ctxencoder session context
[out]p_enc_frameframe to be sent to encoder
[in]p_dec_frameframe returned by decoder
[in]mdcv_dataSEI for HDR mastering display color volume info
[in]cll_dataSEI for HDR content light level info
[in]cc_dataSEI for close caption
[in]udu_dataSEI for User data unregistered
[in]hdrp_dataSEI for HDR10+
[in]is_hwframe,mustbe 0 (sw frame) or 1 (hw frame)
[in]is_semiplanar,mustbe 1 (semiplanar frame) or 0 (not)
Returns
NONE

Definition at line 2267 of file ni_av_codec.c.

◆ ni_enc_insert_timecode()

LIB_API int ni_enc_insert_timecode ( ni_session_context_t p_enc_ctx,
ni_frame_t p_enc_frame,
ni_timecode_t p_timecode 
)

Insert timecode data into picture timing SEI (H264) or time code SEI (H265)


Note
This function must be callled after all other aux data has been processed by ni_enc_prep_aux_data and ni_enc_copy_aux_data. Otherwise the timecode SEI data might be overwritten
Parameters
[in]p_enc_ctxencoder session context
[out]p_enc_frameframe to be sent to encoder
[in]p_timecodethe timecode data to be written along with the frame
Returns
NI_RETCODE_SUCCESS on success, NI_RETCODE_FAILURE on failure

Definition at line 2453 of file ni_av_codec.c.

◆ ni_enc_prep_aux_data()

void ni_enc_prep_aux_data ( ni_session_context_t p_enc_ctx,
ni_frame_t p_enc_frame,
ni_frame_t p_dec_frame,
ni_codec_format_t  codec_format,
int  should_send_sei_with_frame,
uint8_t *  mdcv_data,
uint8_t *  cll_data,
uint8_t *  cc_data,
uint8_t *  udu_data,
uint8_t *  hdrp_data 
)

Prepare auxiliary data that should be sent together with this frame to encoder based on the auxiliary data of the decoded frame.


Note: Some of the SEI (e.g. HDR) will be updated and stored in encoder context whenever received through decoded frame; they will be sent out with the encoded frame to encoder only when appropriate, i.e. should_send_sei_with_frame is true. When a type of aux data is to be sent, its associated length will be set in the encoder frame.

Parameters
[in/out]p_enc_ctx encoder session contextwhose various SEI type header can be updated as the result of this function
[out]p_enc_frameframe to be sent to encoder
[in]p_dec_frameframe that is returned by decoder
[in]codec_formatH.264 or H.265
[in]should_send_sei_with_frameif need to send a certain type of SEI with this frame
[out]mdcv_dataSEI for HDR mastering display color volume info
[out]cll_dataSEI for HDR content light level info
[out]cc_dataSEI for close caption
[out]udu_dataSEI for User data unregistered
[out]hdrp_dataSEI for HDR10+
Returns
NONE

Definition at line 858 of file ni_av_codec.c.

◆ ni_enc_write_from_yuv_buffer()

int ni_enc_write_from_yuv_buffer ( ni_session_context_t p_ctx,
ni_frame_t p_enc_frame,
uint8_t *  p_yuv_buffer 
)

Send an input data frame to the encoder with YUV data given in the inputs.


For ideal performance memory should be 4k aligned. If it is not 4K aligned then a temporary 4k aligned memory will be used to copy data to and from when writing and reading. This will negatively impact performance.

Any metadata to be sent with the frame should be attached to p_enc_frame as aux data (e.g. using ni_frame_new_aux_data()).

Parameters
[in]p_ctxEncoder session context
[in]p_enc_frameStruct holding information about the frame to be sent to the encoder
[in]p_yuv_bufferCaller allocated buffer holding YUV data for the frame
Returns
On success Total number of bytes written On failure NI_RETCODE_INVALID_PARAM NI_RETCODE_ERROR_MEM_ALOC NI_RETCODE_ERROR_NVME_CMD_FAILED NI_RETCODE_ERROR_INVALID_SESSION

Definition at line 2625 of file ni_av_codec.c.

◆ ni_expand_frame()

int ni_expand_frame ( ni_frame_t dst,
ni_frame_t src,
int  dst_stride[],
int  raw_width,
int  raw_height,
int  ni_fmt,
int  nb_planes 
)

Expand frame form src frame.


Parameters
[in]dstPointer to a caller allocated ni_frame_t struct
[in]srcPointer to a caller allocated ni_frame_t struct
[in]dst_strideint dst_stride[]
[in]raw_widthframe width
[in]raw_heightframe height
[in]ni_fmtni_pix_fmt_t type for ni pix_fmt
[in]nb_planesint nb_planes
Returns
- 0 on success, NI_RETCODE_FAILURE on failure

Definition at line 3289 of file ni_av_codec.c.

◆ ni_extract_custom_sei()

int ni_extract_custom_sei ( uint8_t *  pkt_data,
int  pkt_size,
long  index,
ni_packet_t p_packet,
uint8_t  sei_type,
int  vcl_found 
)

Extract custom sei payload data from pkt_data, and save it to ni_packet_t.


Parameters
uint8_t*pkt_data - FFmpeg AVPacket data
intpkt_size - packet size
longindex - pkt data index of custom sei first byte after SEI type
ni_packet_t*p_packet - libxcoder internal packet
uint8_tsei_type - type of SEI
intvcl_found - whether got vcl in the pkt data, 1 means got
Returns
- 0 on success, non-0 on failure

extract SEI payload size. the first byte after SEI type is the SEI payload size. if the first byte is 255(0xFF), it means the SEI payload size is more than 255. in this case, to get the SEI payload size is to do a summation. the end of SEI size is the first non-0xFF value. for example, 0xFF 0xFF 0x08, the SEI payload size equals to (0xFF+0xFF+0x08).

Definition at line 2944 of file ni_av_codec.c.

◆ ni_find_start_code()

const uint8_t* ni_find_start_code ( const uint8_t *  p,
const uint8_t *  end,
uint32_t *  state 
)

Find the next start code.


Parameters
[in]ppointer to buffer start address.
[in]endpointer to buffer end address.
[state]state pointer to nalu type address
Returns
search end address

Definition at line 2897 of file ni_av_codec.c.

◆ ni_reconfig_ppu_output()

int ni_reconfig_ppu_output ( ni_session_context_t p_session_ctx,
ni_xcoder_params_t p_param,
ni_ppu_config_t ppu_config 
)

Reset decoder ppu resolution.


Parameters
[in]p_session_ctxPointer to a caller allocated ni_session_context_t struct
[in]p_paramPointer to a caller allocated ni_xcoder_params_t struct
[in]ppu_configPointer to a caller allocated ni_ppu_config_t struct
Returns
- 0 on success, NI_RETCODE_FAILURE on failure

Definition at line 3485 of file ni_av_codec.c.

◆ ni_should_send_sei_with_frame()

int ni_should_send_sei_with_frame ( ni_session_context_t p_enc_ctx,
ni_pic_type_t  pic_type,
ni_xcoder_params_t p_param 
)

Whether SEI (HDR) should be sent together with this frame to encoder.

Whether SEI should be sent together with this frame to encoder.


Parameters
[in]p_enc_ctxencoder session context
[in]pic_typeframe type
[in]p_paramencoder parameters
Returns
1 if yes, 0 otherwise

Definition at line 186 of file ni_av_codec.c.