libxcoder  5.3.1
ni_device_api_priv.c
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (C) 2022 NETINT Technologies
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18  * SOFTWARE.
19  *
20  ******************************************************************************/
21 
22 /*!*****************************************************************************
23  * \file ni_device_api_priv.c
24  *
25  * \brief Private definitions used by ni_device_api.c for video processing
26  * tasks
27  ******************************************************************************/
28 
29 #ifdef _WIN32
30 #include <windows.h>
31 #elif __linux__ || __APPLE__
32 #if __linux__
33 #include <linux/types.h>
34 #include <malloc.h>
35 #include <sys/prctl.h>
36 #endif
37 #include <sys/file.h>
38 #include <sys/ioctl.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
41 #include <sys/mman.h>
42 #include <sys/types.h>
43 #include <utime.h>
44 #include <sys/time.h>
45 #include <fcntl.h>
46 #include <errno.h>
47 #include <semaphore.h>
48 #include <limits.h>
49 #include <signal.h>
50 #include <dirent.h>
51 #include <sched.h>
52 #include <sys/syscall.h>
53 #include <sys/resource.h>
54 #if !defined(_ANDROID) && !defined(__OPENHARMONY__)
55 #include <execinfo.h>
56 #endif
57 #endif
58 
59 #include <stdint.h>
60 #include <string.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <math.h>
64 #ifdef __linux__
65 #include <stdint.h>
66 #include <poll.h>
67 #include "ni_p2p_ioctl.h"
68 #endif
69 #include "inttypes.h"
70 #include "ni_nvme.h"
71 #include "ni_device_api.h"
72 #include "ni_device_api_priv.h"
73 #include "ni_util.h"
74 #include "ni_lat_meas.h"
75 #include "ni_rsrc_priv.h"
76 
77 //Map the gopPresetIdx to gopSize,
78 //gopPresetIdx -1: Adaptive Gop, using the biggest gopsize 8
79 //gopPresetIdx 0 : Custom Gop as defined by -xcoder-gop
80 //g_map_preset_to_gopsize[lookAheadEnable][gopPresetIdx + 1];
81 //for the not support gopPresetIdx value, such as gopPresetIdx == 2, set g_map_preset_to_gopsize to -1
82 static const int g_map_preset_to_gopsize[2][18] = {{8, 0, 1, -1, 1, 2, 4, -1, 4, 8, 1, 4, -1, -1, -1, -1, 16},
83  {8, 0, -1, -1, -1, 2, 4, -1, -1, 8, 1, -1, -1, -1, -1, -1, -1}};
84 #define MAGIC_P2P_VALUE "p2p"
85 #define AI_MODEL_TYPE_HVSPLUS_FILTER 3
86 
88 {
92 
93 typedef enum
94 {
95  OPT_1 = 1, // is a combination of OPT_3 and OPT_2 in that order
96  OPT_2 = 2, // returns NI_RETCODE_FAILURE after NI_XCODER_FAILURES_MAX attempts, and LRETURNs for failures
97  OPT_3 = 3 // queries session statistics
99 
100 static uint8_t g_itu_t_t35_cc_sei_hdr_hevc[NI_CC_SEI_HDR_HEVC_LEN] = {
101  0x00, 0x00, 0x00, 0x01, // NAL start code 00 00 00 01
102  0x4e,
103  0x01, // nal_unit_header() {forbidden bit=0 nal_unit_type=39,
104  // nuh_layer_id=0 nuh_temporal_id_plus1=1)
105  0x04, // payloadType= 4 (user_data_registered_itu_t_t35)
106  0 + 11, // payLoadSize= ui16Len + 11; to be set (index 7)
107  0xb5, // itu_t_t35_country_code =181 (North America)
108  0x00,
109  0x31, // itu_t_t35_provider_code = 49
110  0x47, 0x41, 0x39,
111  0x34, // ATSC_user_identifier = "GA94"
112  0x03, // ATSC1_data_user_data_type_code=3
113  0 | 0xc0, // (ui16Len/3) | 0xc0 (to be set; index 16) (each CC character
114  //is 3 bytes)
115  0xFF // em_data = 255
116 };
117 
118 static uint8_t g_itu_t_t35_hdr10p_sei_hdr_hevc[NI_HDR10P_SEI_HDR_HEVC_LEN] = {
119  0x00, 0x00, 0x00, 0x01, // NAL start code 00 00 00 01
120  0x4e,
121  0x01, // nal_unit_header() {forbidden bit=0 nal_unit_type=39,
122  // nuh_layer_id=0 nuh_temporal_id_plus1=1)
123  0x04, // payloadType= 4 (user_data_registered_itu_t_t35)
124  0x00, // payLoadSize; to be set (index 7)
125  0xb5, // u8 itu_t_t35_country_code =181 (North America)
126  //0x00,
127  //0x3c, // u16 itu_t_t35_provider_code = 0x003c
128  //0x00,
129  //0x01, // u16 itu_t_t35_provider_oriented_code = 0x0001
130  // payLoadSize count starts from itu_t_t35_provider_code and goes until
131  // and including trailer
132 };
133 
134 static uint8_t g_itu_t_t35_cc_sei_hdr_h264[NI_CC_SEI_HDR_H264_LEN] = {
135  0x00, 0x00, 0x00, 0x01, // NAL start code 00 00 00 01
136  0x06, // nal_unit_header() {forbidden bit=0 nal_ref_idc=0, nal_unit_type=6
137  0x04, // payloadType= 4 (user_data_registered_itu_t_t35)
138  0 + 11, // payLoadSize= ui16Len + 11; to be set (index 6)
139  0xb5, // itu_t_t35_country_code =181 (North America)
140  0x00,
141  0x31, // itu_t_t35_provider_code = 49
142  0x47, 0x41, 0x39,
143  0x34, // ATSC_user_identifier = "GA94"
144  0x03, // ATSC1_data_user_data_type_code=3
145  0 | 0xc0, // (ui16Len/3) | 0xc0 (to be set; index 15) (each CC character
146  //is 3 bytes)
147  0xFF // em_data = 255
148 };
149 
150 static uint8_t g_itu_t_t35_hdr10p_sei_hdr_h264[NI_HDR10P_SEI_HDR_H264_LEN] = {
151  0x00, 0x00, 0x00, 0x01, // NAL start code 00 00 00 01
152  0x06, // nal_unit_header() {forbidden bit=0 nal_ref_idc=0, nal_unit_type=6
153  0x04, // payloadType= 4 (user_data_registered_itu_t_t35)
154  0x00, // payLoadSize; to be set (index 6)
155  0xb5, // itu_t_t35_country_code =181 (North America)
156  //0x00,
157  //0x3c, // u16 itu_t_t35_provider_code = 0x003c
158  //0x00,
159  //0x01, // u16 itu_t_t35_provider_oriented_code = 0x0001
160  // payLoadSize count starts from itu_t_t35_provider_code and goes until
161  // and including trailer
162 };
163 
164 static uint8_t g_sei_trailer[NI_CC_SEI_TRAILER_LEN] = {
165  0xFF, // marker_bits = 255
166  0x80 // RBSP trailing bits - rbsp_stop_one_bit and 7 rbsp_alignment_zero_bit
167 };
168 
169 #define NI_XCODER_FAILURES_MAX 25
170 #define DP_IPC_PASSTHRU 0xFFFFFFFF
171 
172 #ifdef _WIN32
173 #ifdef XCODER_SELF_KILL_ERR
174 #undef XCODER_SELF_KILL_ERR
175 #endif
176 #endif
177 
178 void SwapSWBytes(uint8_t *buf, uint32_t bytes)
179 {
180  uint32_t i;
181  for (i = 0; i < bytes; i += 2)
182  {
183  uint8_t val = buf[i];
184  buf[i] = buf[i + 1];
185  buf[i + 1] = val;
186  }
187 }
188 
189 void SwapSW16(uint16_t *buf, uint32_t bytes)
190 {
191  uint32_t i, words = (bytes + 1) / 2;
192 
193  for (i = 0; i < words; i += 2)
194  {
195  uint16_t val = buf[i];
196  buf[i] = buf[i + 1];
197  buf[i + 1] = val;
198  }
199 }
200 
201 void SwapSW32(uint32_t *buf, uint32_t bytes)
202 {
203  uint32_t i, words = (bytes + 3) / 4;
204  for (i = 0; i < words; i += 2)
205  {
206  uint32_t val = buf[i];
207  buf[i] = buf[i + 1];
208  buf[i + 1] = val;
209  }
210 }
211 
212 void SwapSW64(uint64_t *buf, uint64_t bytes)
213 {
214  uint64_t i, words = (bytes + 7) / 8;
215  for (i = 0; i < words; i += 2)
216  {
217  uint64_t val = buf[i];
218  buf[i] = buf[i + 1];
219  buf[i + 1] = val;
220  }
221 }
222 
223 static void memcpyToPelByBits (uint8_t *dst_u8, uint8_t *src, int width, int height, int dst_stride, int src_stride, int srcValidBitsDepth)
224 {
225  int i, j;
226  uint8_t bitsDepth = srcValidBitsDepth;
227  uint32_t mask = (1<<bitsDepth)-1;
228  uint16_t *dst = (uint16_t *)dst_u8;
229 
230  //fast for byte write
231  if (bitsDepth == 8)
232  {
233  for (j = 0; j < height; j ++)
234  {
235  for (i = 0; i< width; i ++)
236  dst_u8[i] = src[i] & 0xff;
237 
238  src += src_stride;
239  dst_u8 += dst_stride;
240  }
241  return;
242  }
243 
244  //for arbitrary bits
245  for (j = 0; j < height; j ++)
246  {
247  uint8_t *src_line = src;
248  uint32_t cache = 0, cache_bits = 0;
249  for (i = 0; i< width; i ++)
250  {
251  while(cache_bits < bitsDepth)
252  {
253  cache |= ((*src_line++) << cache_bits);
254  cache_bits += 8;
255  }
256 
257  if (cache_bits >= bitsDepth)
258  {
259  dst[i] = cache & mask;
260  cache >>= bitsDepth;
261  cache_bits -= bitsDepth;
262  }
263  }
264 
265  src += src_stride;
266  dst += dst_stride;
267  }
268 }
269 
270 /*
271 static void write_(FILE *fp, uint8_t *data, int32_t width, int32_t height, int32_t stripe)
272 {
273  int32_t i;
274 
275  for (i = 0; i < height; i++)
276  {
277  if (fwrite(data, sizeof(uint8_t ), width, fp) < (size_t)width)
278  {
279  return;
280  }
281  data += stripe;
282  }
283 }
284 */
285 
286 static void trace_recon_tile2raster_y(FILE *fRecon, uint8_t *mem, int width, int height,
287  int leftOffset, int topOffset,
288  int src_stride, int pixDepth, uint8_t *luma_mem_temp)
289 {
290  int i;
291  int write_data_len = 0;
292  int bytesPerPix = (pixDepth == 8 ? 1 : 2);
293  uint8_t *tmp_mem = (uint8_t *)malloc(bytesPerPix*src_stride);
294  int num_tiles = (leftOffset + width + 3) / 4 - leftOffset/4;
295  if(tmp_mem == NULL)
296  return;
297  mem += topOffset/4*4*src_stride + leftOffset/4*16*pixDepth/8;
298  for(i = topOffset/4*4; i < topOffset + height; i += 4) {
299  memcpyToPelByBits(tmp_mem, mem, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
300  if(i >= topOffset && i < topOffset + height){
301  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
302  memcpy(luma_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
303  write_data_len += width * bytesPerPix;
304  }
305  memcpyToPelByBits(tmp_mem, mem+4*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
306  if(i+1 >= topOffset && i+1 < topOffset + height)
307  {
308  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
309  memcpy(luma_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
310  write_data_len += width * bytesPerPix;
311  }
312  memcpyToPelByBits(tmp_mem, mem+8*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
313  if(i+2 >= topOffset && i+2 < topOffset + height)
314  {
315  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
316  memcpy(luma_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
317  write_data_len += width * bytesPerPix;
318  }
319  memcpyToPelByBits(tmp_mem, mem+12*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
320  if(i+3 >= topOffset && i+3 < topOffset + height)
321  {
322  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
323  memcpy(luma_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
324  write_data_len += width * bytesPerPix;
325  }
326  mem += 4*src_stride;
327  }
328  free(tmp_mem);
329 }
330 
331 static void trace_recon_tile2raster_uv(FILE *fRecon, uint8_t *mem_uv, int width, int height,
332  int leftOffset, int topOffset,
333  int src_stride, int pixDepth, uint8_t *ch_mem_temp)
334 {
335  int i, j, k;
336  int bytesPerPix = (pixDepth == 8 ? 1 : 2);
337  uint8_t *tmp_mem = (uint8_t *)malloc(bytesPerPix*src_stride*2);
338  int num_tiles = 2*((leftOffset + width + 3) / 4 - leftOffset/4);
339  if(tmp_mem == NULL)
340  return;
341  uint8_t *mem;
342  int uv;
343  int write_data_len = 0;
344  memset(tmp_mem, 0x00, bytesPerPix * src_stride * 2);
345  for(uv = 0; uv <= 1; uv ++) {
346  mem = mem_uv + topOffset/4*8*src_stride + leftOffset/4*32*pixDepth/8;
347  for(i = topOffset/4*4; i < topOffset + height; i += 4) {
348  if(i >= topOffset && i < topOffset + height) {
349  memcpyToPelByBits(tmp_mem, mem, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
350  for(j = uv, k = 0; j < 4 * num_tiles; j += 2, k++) {
351  if(bytesPerPix == 1)
352  tmp_mem[k] = tmp_mem[j];
353  else
354  ((uint16_t*)tmp_mem)[k] = ((uint16_t*)tmp_mem)[j];
355  }
356  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
357  memcpy(ch_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
358  write_data_len += width * bytesPerPix;
359  }
360  if(i+1 >= topOffset && i+1 < topOffset + height) {
361  memcpyToPelByBits(tmp_mem, mem+4*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
362  for(j = uv, k = 0; j < 4 * num_tiles; j += 2, k++) {
363  if(bytesPerPix == 1)
364  tmp_mem[k] = tmp_mem[j];
365  else
366  ((uint16_t*)tmp_mem)[k] = ((uint16_t*)tmp_mem)[j];
367  }
368  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
369  memcpy(ch_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
370  write_data_len += width * bytesPerPix;
371  }
372  if(i+2 >= topOffset && i+2 < topOffset + height) {
373  memcpyToPelByBits(tmp_mem, mem+8*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
374  for(j = uv, k = 0; j < 4 * num_tiles; j += 2, k++) {
375  if(bytesPerPix == 1)
376  tmp_mem[k] = tmp_mem[j];
377  else
378  ((uint16_t*)tmp_mem)[k] = ((uint16_t*)tmp_mem)[j];
379  }
380  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
381  memcpy(ch_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
382  write_data_len += width * bytesPerPix;
383  }
384  memcpyToPelByBits(tmp_mem, mem+12*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
385  if(i+3 >= topOffset && i+3 < topOffset + height) {
386  for(j = uv, k = 0; j < 4 * num_tiles; j += 2, k++) {
387  if(bytesPerPix == 1)
388  tmp_mem[k] = tmp_mem[j];
389  else
390  ((uint16_t*)tmp_mem)[k] = ((uint16_t*)tmp_mem)[j];
391  }
392  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
393  memcpy(ch_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
394  write_data_len += width * bytesPerPix;
395  }
396  mem += 8*src_stride;
397  }
398  }
399  free(tmp_mem);
400 }
401 
402 static double calc_noise(uint8_t *p1, uint8_t *p2, int size)
403 {
404  double sum;
405  double diff;
406  int i;
407 
408  sum = 0;
409  for (i = 0; i < size; i++)
410  {
411  diff = p1[i] - p2[i];
412  sum += diff * diff;
413  }
414 
415  return sum;
416 }
417 
418 /*!*****************************************************************************
419  * \brief Download hw frames by HwDesc.
420  *
421  * \param
422  *
423  * \return 0 if successful, < 0 otherwise
424  ******************************************************************************/
425 static int hwdl_frame(ni_session_context_t *p_ctx,
426  ni_session_data_io_t *p_session_data, ni_frame_t *p_src_frame,
427  int pixel_format)
428 {
429  niFrameSurface1_t *src_surf = (niFrameSurface1_t *)(p_src_frame->p_data[3]);
430  int ret = 0;
431 
432  ret = ni_frame_buffer_alloc_dl(&(p_session_data->data.frame),
433  src_surf->ui16width, src_surf->ui16height,
434  pixel_format);
435 
436  if (ret != NI_RETCODE_SUCCESS)
437  {
439  }
440 
441  p_ctx->is_auto_dl = false;
442  ret = ni_device_session_hwdl(p_ctx, p_session_data, src_surf);
443  if (ret <= 0)
444  {
445  ni_frame_buffer_free(&p_session_data->data.frame);
446  return ret;
447  }
448  return ret;
449 }
450 
452 {
453  // char dump_file_tile[256];
454  // char dump_file_raster[256];
455  //FILE *fout_tile = NULL;
456  FILE *fout_raster = NULL;
457  // snprintf(dump_file_tile, sizeof(dump_file_tile), "output-%04ld-tile.yuv", (long)p_ctx->pkt_num - 1);
458  // snprintf(dump_file_raster, sizeof(dump_file_raster), "output-%04ld-raster.yuv", (long)p_ctx->pkt_num - 1);
459  // fout_tile = fopen(dump_file_tile, "wb");
460  // fout_raster = fopen(dump_file_raster, "wb");
461  // FILE *f_out = fopen("out.yuv", "a");
462  int bit_depth = (p_ctx->pixel_format == NI_PIX_FMT_YUV420P10LE || p_ctx->pixel_format == NI_PIX_FMT_P010LE) ? 10 : 8;
463 
465 
466  // only h.265 psnr_y is supported by HW
467  p_ctx->psnr_y = p_packet->psnr_y = (float)(p_meta->ui16psnr_y / 1000.0);
468 
469  if (((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode != 3 &&
470  (p_meta->reconLumaSize != 0 || p_meta->reconChromaSize != 0))
471  {
472  uint32_t lum_sz = 0;
473  uint32_t chr_sz = 0;
474  uint8_t *luma_mem_temp = NULL;
475  uint8_t *ch_mem_temp = NULL;
476  int width = p_ctx->actual_video_width;
477  int height = p_ctx->active_video_height;//p_meta->reconLumaSize / p_meta->reconLumaWidth;
478 
479  uint8_t *luma_mem = (uint8_t *)p_packet->p_data + p_packet->data_len - p_meta->reconChromaSize - p_meta->reconLumaSize;
480  uint8_t *ch_mem = (uint8_t *)p_packet->p_data + p_packet->data_len - p_meta->reconChromaSize;
481 
482  int bit_scale = (p_ctx->pixel_format == NI_PIX_FMT_YUV420P10LE || p_ctx->pixel_format == NI_PIX_FMT_P010LE) ? 5 : 4;
483  lum_sz = (2 * p_meta->frameCropTopOffset + height + 3) / 4 * p_meta->reconLumaWidth * bit_scale;
484  chr_sz = (p_meta->frameCropTopOffset + height / 2 + 1) / 2 * p_meta->reconChromaWidth * bit_scale;
485  if (p_meta->reconLumaSize)
486  {
487  uint32_t luma_size = (bit_depth == 8) ? p_meta->reconLumaSize : p_meta->reconLumaSize * 2;
488  luma_mem_temp = (uint8_t *)malloc(luma_size);
489  memset(luma_mem_temp, 0x00, luma_size);
490  }
491  if (p_meta->reconChromaSize)
492  {
493  uint32_t ch_size = (bit_depth == 8) ? p_meta->reconChromaSize : p_meta->reconChromaSize * 2;
494  ch_mem_temp = (uint8_t *)malloc(ch_size);
495  memset(ch_mem_temp, 0x00, ch_size);
496  }
500  if (p_meta->reconLumaSize != 0)
501  {
502  SwapSWBytes(luma_mem, lum_sz);
503  SwapSW16((uint16_t *)luma_mem, lum_sz);
504  SwapSW32((uint32_t *)luma_mem, lum_sz);
505  SwapSW64((uint64_t *)luma_mem, lum_sz);
506  trace_recon_tile2raster_y (fout_raster, luma_mem, width, height,
507  2 * p_meta->frameCropLeftOffset, 2 * p_meta->frameCropTopOffset,
508  p_meta->reconLumaWidth, bit_depth, luma_mem_temp);
509  ni_log2(p_ctx, NI_LOG_DEBUG, "lum_sz %d reconLumaSize %d width %d reconLumaWidth %d\n",
510  lum_sz, p_meta->reconLumaSize, width, p_meta->reconLumaWidth);
511  // fflush(fout_raster);
512  }
513 
514  if (p_meta->reconChromaSize != 0)
515  {
516  SwapSWBytes(ch_mem, chr_sz);
517  SwapSW16((uint16_t *)ch_mem, chr_sz);
518  SwapSW32((uint32_t *)ch_mem, chr_sz);
519  SwapSW64((uint64_t *)ch_mem, chr_sz);
520  trace_recon_tile2raster_uv(fout_raster, ch_mem, width / 2, height / 2,
521  p_meta->frameCropLeftOffset, p_meta->frameCropTopOffset,
522  p_meta->reconChromaWidth, bit_depth, ch_mem_temp);
523  ni_log2(p_ctx, NI_LOG_DEBUG, "chr_sz %d reconChromaSize %d reconChromaWidth %d\n",
524  chr_sz, p_meta->reconChromaSize, p_meta->reconChromaWidth);
525  // fflush(fout_raster);
526  }
527 
528  // fwrite((uint8_t *)p_packet->p_data + sizeof(ni_metadata_enc_bstream_t) + (p_packet->data_len - p_meta->reconLumaSize - p_meta->reconChromaSize),
529  // p_meta->reconLumaSize + p_meta->reconChromaSize, 1, fout_tile);
530 
534  if (((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode < 3)
535  {
536  do
537  {
538  int max;
539  double noise_y = 0.0, noise_u = 0.0, noise_v = 0.0, noise_yuv = 0.0;
540  double power_y = 0.0, power_u = 0.0, power_v = 0.0, power_yuv = 0.0;
541  double psnr_y = 0.0, psnr_u = 0.0, psnr_v = 0.0, psnr_yuv = 0.0;
542 
544  // FILE *f_out_raster = (FILE *)fopen(dump_file_raster, "rb");
545  // fread(luma_mem_temp, width * height , 1, f_out_raster);
546  // fread(ch_mem_temp, width * height / 2, 1, f_out_raster);
547  // fflush(f_out_raster);
548  // fclose(f_out_raster);
549 
550  max = (1 << 8) - 1;
551  power_y = 1.0 * max * max * width * height;
552  power_u = power_y / 4.0;
553  power_v = power_y / 4.0;
554  power_yuv = power_y * 3.0 / 2.0;
555 
556  uint8_t *p_y_src_buf = NULL;
557  uint8_t *p_u_src_buf = NULL;
558  uint8_t *p_v_src_buf = NULL;
559  for (int i = 0; i < 120; i++)
560  {
561  if (p_ctx->input_frame_fifo[i].p_input_buffer != NULL &&
562  p_ctx->input_frame_fifo[i].pts == p_meta->frame_tstamp)
563  {
564  p_y_src_buf = p_ctx->input_frame_fifo[i].p_input_buffer;
565  p_u_src_buf = p_y_src_buf + p_ctx->input_frame_fifo[i].video_width * p_ctx->input_frame_fifo[i].video_height;
566  p_v_src_buf = p_u_src_buf + (p_ctx->input_frame_fifo[i].video_width / 2) * (p_ctx->input_frame_fifo[i].video_height / 2);
567 
568  p_ctx->input_frame_fifo[i].usable = 1;
569  ni_log2(p_ctx, NI_LOG_DEBUG, "%s %d i %d pkt_num %ld frame_num %ld frame_tstamp %lld\n",
570  __FUNCTION__, __LINE__, i, p_ctx->pkt_num, p_ctx->frame_num, p_meta->frame_tstamp);
571  break;
572  }
573  }
574  if (p_y_src_buf != NULL)
575  {
576  if (p_meta->reconLumaSize != 0)
577  {
578  noise_y = calc_noise(p_y_src_buf, luma_mem_temp, width * height);
579  }
580  else if (p_meta->ui16psnr_y != 0)
581  {
582  noise_y = power_y / pow(10, p_ctx->psnr_y / 10); // This is used for calculation noise_yuv for psnr_yuv
583  }
584 
585  if (p_meta->reconChromaSize != 0)
586  {
587  noise_u = calc_noise(p_u_src_buf, ch_mem_temp, (width / 2) * (height / 2));
588  noise_v = calc_noise(p_v_src_buf, ch_mem_temp + (width / 2) * (height / 2), (width / 2) * (height / 2));
589  }
590  noise_yuv = noise_y + noise_u + noise_v;
591 
592  //This is for the case when encoding some very simple picture
593  //this case the quality is very good. sometimes the noise_y may be zero
594  noise_y = noise_y < 1 ? 1 : noise_y;
595  noise_u = noise_u < 1 ? 1 : noise_u;
596  noise_v = noise_v < 1 ? 1 : noise_v;
597  noise_yuv = noise_yuv < 1 ? 1 : noise_yuv;
598 
599  if (p_meta->reconLumaSize != 0)
600  {
601  psnr_y = 10 * log10(power_y / noise_y);
602  }
603  else if (p_meta->ui16psnr_y != 0)
604  {
605  psnr_y = p_ctx->psnr_y;
606  }
607 
608  if (p_meta->reconChromaSize != 0)
609  {
610  psnr_u = 10 * log10(power_u / noise_u);
611  psnr_v = 10 * log10(power_v / noise_v);
612  }
613  psnr_yuv = 10 * log10(power_yuv / noise_yuv);
614  p_ctx->psnr_y = p_packet->psnr_y = psnr_y;
615  p_ctx->psnr_u = p_packet->psnr_u = psnr_u;
616  p_ctx->psnr_v = p_packet->psnr_v = psnr_v;
617  p_ctx->average_psnr = p_packet->average_psnr = psnr_yuv;
618  }
619  } while(0);
620  }
621  else
622  {
623  // get recontructed frame here
624  // int h = 0;
625  // luma
626  // for (h = 0; h < height; h++)
627  // {
628  // fwrite(luma_mem_temp + (width * h), 1, width, f_out);
629  // }
630  //chroma
631  // for (h = 0; h < height; h++)
632  // {
633  // fwrite(ch_mem_temp + ((width / 2) * h), 1, width / 2, f_out);
634  // }
635  // fclose(f_out);
636  }
637 
638  if (luma_mem_temp)
639  {
640  free(luma_mem_temp);
641  luma_mem_temp = NULL;
642  }
643  if (ch_mem_temp)
644  {
645  free(ch_mem_temp);
646  ch_mem_temp = NULL;
647  }
648  // fflush(fout_tile);
649  // fclose(fout_tile);
650  // fflush(fout_raster);
651  // fclose(fout_raster);
652  //}
653  }
654 
655  if(p_meta->reconChromaSize == 0)
656  {
657  p_ctx->psnr_u = p_ctx->psnr_v = 0;
658  p_ctx->average_psnr = p_ctx->psnr_y;
659  }
660  if (p_meta->reconLumaSize == 0 && p_meta->ui16psnr_y == 0)
661  {
662  p_ctx->psnr_y = 0;
663  p_ctx->average_psnr = (p_ctx->psnr_u + p_ctx->psnr_v) / 2;
664  }
665 
666 
667  ni_log2(p_ctx, NI_LOG_DEBUG, "pkt_num %d psnr_y %.4f psnr_u %.4f psnr_v %.4f psnr_yuv %.4f reconLumaWidth %d width %d height %d reconLumaSize 0x%x reconChromaSize 0x%x\n",
668  p_ctx->pkt_num, p_ctx->psnr_y, p_ctx->psnr_u, p_ctx->psnr_v, p_ctx->average_psnr, p_meta->reconLumaWidth,p_ctx->actual_video_width, p_ctx->active_video_height, p_meta->reconLumaSize, p_meta->reconChromaSize);
669 
670  p_packet->data_len -= p_meta->reconLumaSize + p_meta->reconChromaSize;
671 }
672 
673 // Check for critical failures.
674 // Invalid parameters or resource busy do not account for failures that cause error count to be incremented so they can be retried indefinitely
675 static ni_retcode_t check_err_rc(
676  ni_session_context_t* ctx, ni_retcode_t rc, ni_session_statistic_t *stat_full, int opcode, uint32_t type, int hw_id, uint32_t *inst_id, int opt, const char* func, int line)
677 {
678  ni_retcode_t retval = rc;
679  ni_session_stats_t stat = {0};
680  uint16_t ui16ErrorCount = 0;
681  uint32_t ui32LastErrorStatus = 0;
682 
683  if(opt == OPT_1 || opt == OPT_3)
684  {
685  retval = ni_query_session_stats(ctx, type, &stat, rc, opcode);
686  ui16ErrorCount = stat.ui16ErrorCount;
687  ui32LastErrorStatus = stat.ui32LastErrorStatus;
688  }
689 
690  if (retval != NI_RETCODE_SUCCESS)
691  {
692  ni_log2(ctx, NI_LOG_ERROR, "Query for statistics failed with %d!\n", retval);
693  return NI_RETCODE_FAILURE;
694  }
695 
696  if(opt == OPT_1)
697  {
698  retval = ni_nvme_check_error_code(stat.ui32LastTransactionCompletionStatus, opcode, type, hw_id, inst_id);
699 
700  if(!retval)
701  retval = ni_nvme_check_error_code(ui32LastErrorStatus, opcode, type, hw_id, inst_id);
702  }
703  else if(opt == OPT_2){
704  if(!stat_full){
705  ni_log2(ctx, NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n", __func__);
707  }
708  retval = ni_nvme_check_error_code(stat_full->ui32LastTransactionCompletionStatus, opcode, type, hw_id, inst_id);
709  ui16ErrorCount = stat_full->ui16ErrorCount;
710  ui32LastErrorStatus = stat_full->ui32LastErrorStatus;
711  /* re-query error status for transactionId 0xFFFF */
712  if(stat_full->ui32LastErrorTransactionId == 0xFFFF &&
713  ui16ErrorCount > 0 && !retval &&
714  type == NI_DEVICE_TYPE_DECODER)
715  {
716  retval = ni_nvme_check_error_code(ui32LastErrorStatus, opcode, type, hw_id, inst_id);
717  }
718  }
719  else if(opt == OPT_3)
720  {
721  retval = ni_nvme_check_error_code(stat.ui32LastTransactionCompletionStatus, opcode, type, hw_id, inst_id);
722  }
723 
724  if (retval)
725  {
726  ni_log2(ctx,
727  NI_LOG_ERROR,
728  "Persistent failures detected, %s() line-%d: session_no 0x%x "
729  "sess_err_count %u inst_err_no %u\n",
730  func, line, *(inst_id), ui16ErrorCount,
731  ui32LastErrorStatus);
732 #ifdef XCODER_SELF_KILL_ERR
733  if(opt != OPT_3)
734  {
735  kill(getpid(), SIGTERM);
736  }
737 #endif
738  }
739 
740  return retval;
741 }
742 
743 //Following macros will only check for critical failures.
744 //After the macro runs, rc should contain the status of the last command sent
745 //For non-critical failures, it is assumed in the code that eventually after enough retries, a command will succeed.
746 //Invalid parameters or resource busy do not account for failures that cause error count to be incremented so they can be retried indefinitely
747 #define CHECK_ERR_RC(ctx, rc, info, opcode, type, hw_id, inst_id, opt) \
748  { \
749  (rc) = check_err_rc(ctx, rc, info, opcode, type, hw_id, inst_id, opt, __func__, __LINE__); \
750  if((rc) && ((opt) != OPT_3)) LRETURN; \
751  }
752 
753 #define CHECK_VPU_RECOVERY(ret) \
754  { \
755  if (NI_RETCODE_NVME_SC_VPU_RECOVERY == (ret)) \
756  { \
757  ni_log(NI_LOG_ERROR, "Error, vpu reset.\n"); \
758  (ret) = NI_RETCODE_ERROR_VPU_RECOVERY; \
759  LRETURN; \
760  } \
761  }
762 
763 // ctx->decoder_low_delay is used as condition wait timeout for both decoder
764 // and encoder send/recv multi-thread in low delay mode.
765 static void low_delay_wait(ni_session_context_t* p_ctx)
766 {
767  const char *name = p_ctx->device_type == NI_DEVICE_TYPE_DECODER ? \
768  "decoder" : "encoder";
769  if (p_ctx->async_mode && p_ctx->decoder_low_delay > 0)
770  {
771  int ret;
772  uint64_t abs_time_ns;
773  struct timespec ts;
774 
775  ni_log2(p_ctx, NI_LOG_DEBUG, "%s waiting for %s recv thread\n", __FUNCTION__, name);
776 
777  abs_time_ns = ni_gettime_ns();
778  abs_time_ns += p_ctx->decoder_low_delay * 1000000LL;
779  ts.tv_sec = abs_time_ns / 1000000000LL;
780  ts.tv_nsec = abs_time_ns % 1000000000LL;
781 
783  while (p_ctx->low_delay_sync_flag)
784  {
785  // In case of dead lock on waiting for notification from recv thread.
788  &p_ctx->low_delay_sync_mutex, &ts);
789  ni_pthread_mutex_lock(&p_ctx->mutex);
790  if (ret == ETIMEDOUT)
791  {
792  p_ctx->low_delay_sync_flag = 0;
793  }
794  }
796  }
797 }
798 
799 static void low_delay_signal(ni_session_context_t* p_ctx)
800 {
801  const char *name = p_ctx->device_type == NI_DEVICE_TYPE_DECODER ? \
802  "decoder" : "encoder";
803  if (p_ctx->async_mode && p_ctx->decoder_low_delay > 0)
804  {
805  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: wake up %s send thread\n", __FUNCTION__, name);
807  p_ctx->low_delay_sync_flag = 0;
810  }
811 }
812 
813 static void query_sleep(ni_session_context_t* p_ctx)
814 {
815  if (p_ctx->async_mode)
816  {
817  // To avoid IO spam on NP core from queries and high volumens on latency.
820  ni_pthread_mutex_lock(&p_ctx->mutex);
821  }
822 }
823 
824 // create folder bearing the card name (nvmeX) if not existing
825 // start working inside this folder: nvmeX
826 // find the earliest saved and/or non-existing stream folder and use it as
827 // the pkt saving destination; at most 128 such folders to be checked/created;
828 // folder name is in the format of: streamY, where Y is [1, 128]
829 static void decoder_dump_dir_open(ni_session_context_t *p_ctx)
830 {
831 #ifdef _WIN32
832 #elif __linux__ || __APPLE__
833  FILE *fp;
834  char dir_name[128] = {0};
835  char file_name[512] = {0};
836  ni_device_context_t *p_device_context;
837  DIR *dir;
838  struct dirent *stream_folder;
839  int curr_stream_idx = 0;
840  int earliest_stream_idx = 0;
841  int max_exist_idx = 0;
842  time_t earliest_time = 0;
843  struct stat file_stat;
844 
845  p_device_context =
847  if (!p_device_context)
848  {
849  ni_log2(p_ctx, NI_LOG_ERROR, "Error retrieve device context for decoder guid %d\n",
850  p_ctx->hw_id);
851  return;
852  }
853 
854  flock(p_device_context->lock, LOCK_EX);
855 
856  strcpy(dir_name, &p_ctx->dev_xcoder_name[5]);
857  if (0 != access(dir_name, F_OK))
858  {
859  if (0 != mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO))
860  {
861  ni_log2(p_ctx, NI_LOG_ERROR, "Error create folder %s, errno %d\n", dir_name,
862  NI_ERRNO);
863  } else
864  {
865  ni_log2(p_ctx, NI_LOG_DEBUG, "Created pkt folder for: %s\n", dir_name);
866  }
867  }
868 
869  if (NULL == (dir = opendir(dir_name)))
870  {
871  ni_log2(p_ctx, NI_LOG_ERROR, "Error %d: failed to open directory %s\n",
872  NI_ERRNO, dir_name);
873  } else
874  {
875  // have a quick first pass of streamY folders, and if existing Y < 128,
876  // create a new folder Y+1 directly without checking existing ones'
877  // content
878  while ((stream_folder = readdir(dir)))
879  {
880  if (!strncmp(stream_folder->d_name, "stream", strlen("stream")))
881  {
882  curr_stream_idx =
883  atoi(&(stream_folder->d_name[strlen("stream")]));
884  if (curr_stream_idx > 0)
885  {
886  if (curr_stream_idx > max_exist_idx)
887  {
888  max_exist_idx = curr_stream_idx;
889  }
890  if (NI_MAX_CONTEXTS_PER_HW_INSTANCE == curr_stream_idx)
891  {
892  break;
893  }
894  }
895  }
896  }
897 
898  // if less than 128 streams created then create a new one, otherwise have
899  // to pick the stream folder that has the earliest modified file which
900  // is most likely done by finished session.
901  if (max_exist_idx < NI_MAX_CONTEXTS_PER_HW_INSTANCE)
902  {
903  curr_stream_idx = max_exist_idx + 1;
904  } else
905  {
906  rewinddir(dir);
907  while ((stream_folder = readdir(dir)))
908  {
909  // go through each of these streamY folders and get modified
910  // time of the first pkt-* file to simplify the searching
911  if (!strncmp(stream_folder->d_name, "stream", strlen("stream")))
912  {
913  snprintf(file_name, sizeof(file_name), "%s/%s/pkt-0001.bin",
914  dir_name, stream_folder->d_name);
915 
916  curr_stream_idx =
917  atoi(&(stream_folder->d_name[strlen("stream")]));
918 
919  if (curr_stream_idx > 0 && 0 == access(file_name, F_OK))
920  {
921  // just take pkt-0001 file timestamp to simplify search
922  if (stat(file_name, &file_stat))
923  {
924  ni_log2(p_ctx, NI_LOG_ERROR, "Error %d: failed to stat file %s\n",
925  NI_ERRNO,
926  file_name);
927  } else
928  {
929  if (0 == earliest_stream_idx ||
930  file_stat.st_mtime < earliest_time)
931  {
932  earliest_stream_idx = curr_stream_idx;
933  earliest_time = file_stat.st_mtime;
934  }
935  }
936  } // check first file in streamX
937  } // go through each streamX folder
938  } // read all files in nvmeY
939 
940  curr_stream_idx = earliest_stream_idx;
941 
942  // set the access/modified time of chosen pkt file to NOW so its
943  // stream folder won't be taken by other sessions.
944  snprintf(file_name, sizeof(file_name), "%s/stream%03d/pkt-0001.bin",
945  dir_name, curr_stream_idx);
946  if (utime(file_name, NULL))
947  {
948  ni_log2(p_ctx, NI_LOG_ERROR, "Error utime %s\n", file_name);
949  }
950  } // 128 streams in nvmeY already
951  closedir(dir);
952  }
953 
954  snprintf(p_ctx->stream_dir_name, sizeof(p_ctx->stream_dir_name),
955  "%s/stream%03d", dir_name, curr_stream_idx);
956 
957  if (0 != access(p_ctx->stream_dir_name, F_OK))
958  {
959  if (0 != mkdir(p_ctx->stream_dir_name, S_IRWXU | S_IRWXG | S_IRWXO))
960  {
961  ni_log2(p_ctx, NI_LOG_ERROR, "Error create stream folder %s, errno %d\n",
962  p_ctx->stream_dir_name, NI_ERRNO);
963  } else
964  {
965  ni_log2(p_ctx, NI_LOG_DEBUG, "Created stream sub folder: %s\n",
966  p_ctx->stream_dir_name);
967  }
968  } else
969  {
970  ni_log2(p_ctx, NI_LOG_DEBUG, "Reusing stream sub folder: %s\n",
971  p_ctx->stream_dir_name);
972  }
973 
974  flock(p_device_context->lock, LOCK_UN);
975  ni_rsrc_free_device_context(p_device_context);
976 
977  snprintf(file_name, sizeof(file_name), "%s/process_session_id.txt",
978  p_ctx->stream_dir_name);
979 
980  fp = fopen(file_name, "wb");
981  if (fp)
982  {
983  char number[64] = {'\0'};
984  ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder pkt dump log created: %s\n", file_name);
985  snprintf(number, sizeof(number), "proc id: %ld\nsession id: %u\n",
986  (long)getpid(), p_ctx->session_id);
987  fwrite(number, strlen(number), 1, fp);
988  fclose(fp);
989  } else
990  {
991  ni_log2(p_ctx, NI_LOG_ERROR, "Error create decoder pkt dump log: %s\n", file_name);
992  }
993 #endif
994 }
995 
996 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(__OPEN_HARMONY__) && !defined(_ANDROID)
997 #define MAX_BDF_LEN 16
998 
999 // dev_name pattern: nvmeXnY
1000 // pci_addr pattern: dddd:bb:dd.f
1001 static int find_pci_address(const char *dev_name, char *pci_addr) {
1002  char sys_block_path[256];
1003 
1004  snprintf(sys_block_path, sizeof(sys_block_path), "/sys/class/block/%s/device", dev_name);
1005 
1006  char *real_sysfs_path = realpath(sys_block_path, NULL);
1007  if (!real_sysfs_path) {
1008  perror("realpath failed");
1009  return -1;
1010  }
1011 
1012  // Walk backward in the resolved path to find a segment that matches PCI BDF format
1013  char *p = real_sysfs_path + strlen(real_sysfs_path);
1014 
1015  while (p > real_sysfs_path) {
1016  p = strrchr(real_sysfs_path, '/');
1017  if (!p) break;
1018 
1019  char segment[MAX_BDF_LEN];
1020  strncpy(segment, p + 1, MAX_BDF_LEN - 1);
1021  segment[MAX_BDF_LEN - 1] = '\0';
1022 
1023  unsigned domain, bus, dev, func;
1024  if (sscanf(segment, "%x:%x:%x.%x", &domain, &bus, &dev, &func) == 4) {
1025  strncpy(pci_addr, segment, MAX_BDF_LEN);
1026  pci_addr[MAX_BDF_LEN - 1] = '\0';
1027  free(real_sysfs_path);
1028  return 0;
1029  }
1030 
1031  *p = '\0'; // truncate path to move to the previous segment
1032  }
1033 
1034  free(real_sysfs_path);
1035  return -1;
1036 }
1037 #endif
1038 
1039 // open netint p2p driver and fill the pcie address to p_ctx
1040 static ni_retcode_t p2p_fill_pcie_address(ni_session_context_t *p_ctx)
1041 {
1042 #if defined(_WIN32) || defined(__APPLE__) || defined(__OPEN_HARMONY__)
1043  (void)p_ctx;
1044  return NI_RETCODE_FAILURE;
1045 #else
1046  int ret = 0;
1047  char pci_addr[16];
1048  struct stat bstat;
1049  char *p_dev;
1050  char *dom, *bus, *dev, *fnc;
1051 #ifdef _ANDROID
1052  char syspath[256];
1053  FILE *fp;
1054 #endif
1055 
1056  if(!p_ctx)
1057  {
1058  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() parameter is NULL\n",__func__);
1059  return NI_RETCODE_FAILURE;
1060  }
1061 
1062  p_ctx->netint_fd = open("/dev/netint", O_RDWR);
1063  if (p_ctx->netint_fd < 0)
1064  {
1065  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Can't open device /dev/netint\n");
1066  return NI_RETCODE_FAILURE;
1067  }
1068 
1069  p_dev = &p_ctx->dev_xcoder_name[0];
1070  if (stat(p_dev, &bstat) < 0)
1071  {
1072  ni_log2(p_ctx, NI_LOG_ERROR, "failed to get stat of file %s\n", p_dev);
1073  return NI_RETCODE_FAILURE;
1074  }
1075 
1076  if ((bstat.st_mode & S_IFMT) != S_IFBLK)
1077  {
1078  ni_log2(p_ctx, NI_LOG_ERROR, "%s is not a block device\n", p_dev);
1079  return NI_RETCODE_FAILURE;
1080  }
1081 
1082 #ifdef _ANDROID
1083  ret = snprintf(syspath, sizeof(syspath) - 1,
1084  "/sys/block/%s/device/address",
1085  p_dev + 5);
1086  syspath[ret] = '\0';
1087 
1088  fp = fopen(syspath, "r");
1089  if (fp == NULL)
1090  {
1091  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read address\n");
1092  return NI_RETCODE_FAILURE;
1093  }
1094 
1095  if (fgets(pci_addr, sizeof(pci_addr), fp) == NULL)
1096  {
1097  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read line from address\n");
1098  fclose(fp);
1099  return NI_RETCODE_FAILURE;
1100  }
1101 
1102  fclose(fp);
1103 #else
1104  ret = find_pci_address(p_dev + 5, pci_addr);
1105  if (ret != 0)
1106  {
1107  return NI_RETCODE_FAILURE;
1108  }
1109 #endif
1110 
1111  // pci_addr pattern: 0000:04:00.0
1112  ni_log2(p_ctx, NI_LOG_INFO, "PCI address string = %12.12s\n", pci_addr);
1113 
1114  errno = 0;
1115  p_ctx->domain = strtoul(pci_addr, &dom, 16);
1116  if (errno < 0)
1117  {
1118  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read PCI domain\n");
1119  return NI_RETCODE_FAILURE;
1120  }
1121 
1122  errno = 0;
1123  p_ctx->bus = strtoul(dom + 1, &bus, 16);
1124  if (errno < 0)
1125  {
1126  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read PCI bus\n");
1127  return NI_RETCODE_FAILURE;
1128  }
1129 
1130  errno = 0;
1131  p_ctx->dev = strtoul(bus + 1, &dev, 16);
1132 
1133  if (errno < 0)
1134  {
1135  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read PCI device\n");
1136  return NI_RETCODE_FAILURE;
1137  }
1138 
1139  errno = 0;
1140  p_ctx->fn = strtoul(dev + 1, &fnc, 16);
1141 
1142  if (errno < 0)
1143  {
1144  ni_log2(p_ctx, NI_LOG_ERROR, "Falied to read PCI function\n");
1145  return NI_RETCODE_FAILURE;
1146  }
1147 
1148  ni_log2(p_ctx, NI_LOG_DEBUG, "PCI address parsed = %04x:%02x:%02x.%x\n",
1149  p_ctx->domain, p_ctx->bus, p_ctx->dev, p_ctx->fn);
1150 
1151  return NI_RETCODE_SUCCESS;
1152 #endif
1153 }
1154 
1155 #if __linux__ || __APPLE__
1156 #if !defined(_ANDROID) && !defined(__OPENHARMONY__)
1157 #ifndef DISABLE_BACKTRACE_PRINT
1158 void ni_print_backtrace() {
1159  void* callstack[128];
1160  int frames = backtrace(callstack, 128);
1161  char** strs = backtrace_symbols(callstack, frames);
1162 
1163  ni_log(NI_LOG_ERROR, "Call stack:\n");
1164  for (int i = 0; i < frames; ++i) {
1165  ni_log(NI_LOG_ERROR, "%s\n", strs[i]);
1166  }
1167 
1168  free(strs);
1169 }
1170 #endif
1171 #endif
1172 #endif
1173 
1174 /*!******************************************************************************
1175  * \brief Open a xcoder decoder instance
1176  *
1177  * \param
1178  *
1179  * \return
1180 *******************************************************************************/
1182 {
1184  ni_xcoder_params_t *p_param = NULL;
1185  void* p_buffer = NULL;
1186  uint32_t ui32LBA = 0;
1187  char fmt_fw_api_ver1[5], fmt_fw_api_ver2[5];
1188 
1189  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1190 
1191  if (!p_ctx)
1192  {
1193  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n",
1194  __func__);
1195  retval = NI_RETCODE_INVALID_PARAM;
1196  LRETURN;
1197  }
1198 
1199  //Create the session if the create session flag is set
1200  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1201  {
1203  p_ctx->pts_table = NULL;
1204  p_ctx->dts_queue = NULL;
1205  p_ctx->p_leftover = NULL;
1206  p_ctx->buffer_pool = NULL;
1207  p_ctx->dec_fme_buf_pool = NULL;
1208  p_ctx->prev_size = 0;
1209  p_ctx->sent_size = 0;
1210  p_ctx->status = 0;
1211  p_ctx->key_frame_type = 0;
1212  p_ctx->ready_to_close = 0;
1213  p_ctx->max_retry_fail_count[0] = p_ctx->max_retry_fail_count[1] = 0;
1214  p_ctx->rc_error_count = 0;
1215  p_ctx->frame_num = 0;
1216  p_ctx->pkt_num = 0;
1217  p_ctx->pkt_index = 0;
1218  p_ctx->session_timestamp = 0;
1219  p_ctx->is_dec_pkt_512_aligned = 0;
1220  p_ctx->p_all_zero_buf = NULL;
1221  p_ctx->last_pkt_pos = 0;
1222  p_ctx->last_frame_offset = 0;
1223  memset(p_ctx->pkt_custom_sei_set, 0, NI_FIFO_SZ * sizeof(ni_custom_sei_set_t *));
1224 
1225  //malloc zero data buffer
1226  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
1228  {
1229  ni_log2(p_ctx, NI_LOG_ERROR,
1230  "ERROR %d: %s() alloc decoder all zero buffer failed\n",
1231  NI_ERRNO, __func__);
1232  retval = NI_RETCODE_ERROR_MEM_ALOC;
1233  LRETURN;
1234  }
1235  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
1236 
1237  //malloc data buffer
1238  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
1239  {
1240  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
1241  NI_ERRNO, __func__);
1243  retval = NI_RETCODE_ERROR_MEM_ALOC;
1244  LRETURN;
1245  }
1246  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1247 
1248  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
1249  //In case we can open sesison, the session id would become valid.
1250  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
1251  (uint16_t)NI_INVALID_SESSION_ID;
1252 
1253  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
1254  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_DECODER, ni_htonl(p_ctx->codec_format), p_ctx->hw_action);
1255  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1256  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1257  if (retval != NI_RETCODE_SUCCESS)
1258  {
1259  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
1260  LRETURN;
1261  }
1262  //Open will return a session status structure with a valid session id if it worked.
1263  //Otherwise the invalid session id set before the open command will stay
1264  p_ctx->session_id = ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
1265  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
1266  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
1267  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
1268  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1269  {
1270  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_ctx->device_handle=0x%" PRIx64 ", "
1271  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n", __func__,
1272  (int64_t)p_ctx->device_handle, p_ctx->hw_id, p_ctx->session_id);
1274  LRETURN;
1275  }
1276  ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder open session ID:0x%x, timestamp:%" PRIu64 "\n",
1277  p_ctx->session_id, p_ctx->session_timestamp);
1278 
1279  //Send keep alive timeout Info
1280  uint64_t keep_alive_timeout =
1281  p_ctx->keep_alive_timeout * 1000000; //send us to FW
1282  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1283  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
1284  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
1285  keep_alive_timeout);
1287  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1288  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1289  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1290  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
1291  CHECK_VPU_RECOVERY(retval);
1292 
1293  if (NI_RETCODE_SUCCESS != retval)
1294  {
1295  ni_log2(p_ctx, NI_LOG_ERROR,
1296  "ERROR %s(): nvme write keep_alive_timeout command "
1297  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
1298  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
1300  LRETURN;
1301  }
1302 
1303  // Send SW version to FW if FW API version is >= 6.2
1305  "62") >= 0)
1306  {
1307  // Send SW version to session manager
1308  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1309  memcpy(p_buffer, NI_XCODER_REVISION, sizeof(uint64_t));
1311  ni_fmt_fw_api_ver_str((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], &fmt_fw_api_ver2[0]);
1312  ni_log2(p_ctx, NI_LOG_DEBUG, "%s libxcoder FW API ver %s, FW FW API ver %s\n",
1313  __func__, fmt_fw_api_ver1, fmt_fw_api_ver2);
1314  ui32LBA = CONFIG_SESSION_SWVersion_W(p_ctx->session_id);
1315  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1316  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1317  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
1318  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
1319  CHECK_VPU_RECOVERY(retval);
1320 
1321  if (NI_RETCODE_SUCCESS != retval)
1322  {
1323  ni_log2(p_ctx, NI_LOG_ERROR,
1324  "ERROR %s(): nvme write sw_version command "
1325  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
1326  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
1328  LRETURN;
1329  }
1330  }
1331 
1332  //VP9 requires a scaler session to be opened internally and attached as
1333  //well
1334  if(p_ctx->codec_format == NI_CODEC_FORMAT_VP9)
1335  {
1336  ni_log2(p_ctx, NI_LOG_DEBUG, "Adding scaling session to Vp9 decoder\n");
1337  ui32LBA = OPEN_ADD_CODEC(NI_DEVICE_TYPE_SCALER, ni_htonl(NI_SCALER_OPCODE_SCALE), ni_htons(p_ctx->session_id));
1338  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1339  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1340 
1341  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_open,
1342  p_ctx->device_type, p_ctx->hw_id,
1343  &(p_ctx->session_id), OPT_1);
1344  if (NI_RETCODE_SUCCESS != retval)
1345  {
1346  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR couldn't add vp9 scaler to decoding session\n");
1347  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s():p_ctx->device_handle=0x%" PRIx64 ", "
1348  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n", __func__,
1349  (int64_t)p_ctx->device_handle,p_ctx->hw_id, p_ctx->session_id);
1350  ni_decoder_session_close(p_ctx, 0);
1351  LRETURN;
1352  }
1353  }
1354 
1355  ni_log2(p_ctx, NI_LOG_DEBUG,
1356  "%s(): p_ctx->device_handle=0x%" PRIx64 ", p_ctx->hw_id=%d, "
1357  "p_ctx->session_id=%d\n",
1358  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
1359  p_ctx->session_id);
1360  }
1361 
1362  //start dec config
1363  retval = ni_config_instance_set_decoder_params(p_ctx, 0);
1364  if (NI_RETCODE_SUCCESS != retval)
1365  {
1366  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: calling ni_config_instance_set_decoder_params(): p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n", (int64_t)p_ctx->device_handle, p_ctx->hw_id, p_ctx->session_id);
1367  //ni_decoder_session_close(p_ctx, 0); //close happens on above
1369  LRETURN;
1370  }
1371  //end dec config
1372 
1373  // init for frame pts calculation
1374  p_ctx->is_first_frame = 1;
1375  p_ctx->last_pts = NI_NOPTS_VALUE;
1376  p_ctx->last_dts = NI_NOPTS_VALUE;
1377  p_ctx->last_dts_interval = 0;
1378  p_ctx->last_pts_interval = 0;
1379  p_ctx->pts_correction_last_dts = INT64_MIN;
1380  p_ctx->pts_correction_last_pts = INT64_MIN;
1381 
1382  //p_ctx->p_leftover = malloc(NI_MAX_PACKET_SZ * 2);
1383  p_ctx->p_leftover = malloc(p_ctx->max_nvme_io_size * 2);
1384  if (!p_ctx->p_leftover)
1385  {
1386  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Cannot allocate leftover buffer.\n",
1387  __func__);
1388  retval = NI_RETCODE_ERROR_MEM_ALOC;
1389  //ni_decoder_session_close(p_ctx, 0);
1390  LRETURN;
1391  }
1392 
1393  ni_timestamp_init(p_ctx, &p_ctx->pts_table, "dec_pts");
1394  ni_timestamp_init(p_ctx, &p_ctx->dts_queue, "dec_dts");
1395 
1396  if (p_ctx->p_session_config)
1397  {
1398  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
1399  ni_params_print(p_param);
1400 
1401  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
1402  {
1403  retval = ni_device_set_ddr_configuration(p_ctx, p_param->ddr_priority_mode);
1404  if (NI_RETCODE_SUCCESS != retval)
1405  {
1406  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
1407  __func__, strerror(NI_ERRNO));
1408  LRETURN;
1409  }
1410  }
1411  }
1412 
1413  if (p_ctx->force_low_delay)
1414  {
1415  if (ni_cmp_fw_api_ver(
1417  "6r3") < 0)
1418  {
1419  p_ctx->force_low_delay = false; // forceLowDelay not available for fw < 6r3
1420  ni_log2(p_ctx, NI_LOG_INFO, "Warn %s(): forceLowDelay is not available for fw < 6r3\n",
1421  __func__);
1422  }
1423  }
1424 
1425  p_ctx->active_video_width = 0;
1426  p_ctx->active_video_height = 0;
1427  p_ctx->actual_video_width = 0;
1428  p_ctx->pixel_format_changed = 0;
1429 
1430  ni_log2(p_ctx, NI_LOG_DEBUG,
1431  "%s(): p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
1432  "p_ctx->session_id=%d\n",
1433  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
1434  p_ctx->session_id);
1435 
1436  if (p_param && p_param->dec_input_params.nb_save_pkt)
1437  {
1438  decoder_dump_dir_open(p_ctx);
1439  }
1440 
1441 #ifdef XCODER_DUMP_DATA
1442  char dir_name[256] = {0};
1443 
1444  snprintf(dir_name, sizeof(dir_name), "%ld-%u-dec-fme", (long)getpid(),
1445  p_ctx->session_id);
1446  DIR *dir = opendir(dir_name);
1447  if (!dir && ENOENT == NI_ERRNO)
1448  {
1449  mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO);
1450  ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder frame dump dir created: %s\n", dir_name);
1451  }
1452  if(dir){
1453  closedir(dir);
1454  }
1455 #endif
1456 
1457 END:
1458 
1459  ni_aligned_free(p_buffer);
1460  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1461 
1462  return retval;
1463 }
1464 
1465 /*!******************************************************************************
1466  * \brief send a keep alive message to firmware
1467  *
1468  * \param
1469  *
1470  * \return
1471  *******************************************************************************/
1472 ni_retcode_t ni_send_session_keep_alive(uint32_t session_id, ni_device_handle_t device_handle, ni_event_handle_t event_handle, void *p_data)
1473 {
1474  ni_retcode_t retval;
1475  uint32_t ui32LBA = 0;
1476 
1477  ni_log(NI_LOG_TRACE, "%s(): enter\n", __func__);
1478  if (NI_INVALID_SESSION_ID == session_id)
1479  {
1480  ni_log(NI_LOG_ERROR, "ERROR: %s(): Invalid session ID!, return\n",
1481  __func__);
1483  LRETURN;
1484  }
1485 
1486  if (NI_INVALID_DEVICE_HANDLE == device_handle)
1487  {
1488  ni_log(NI_LOG_ERROR, "ERROR: %s(): xcoder instance id < 0, return\n",
1489  __func__);
1490  retval = NI_RETCODE_INVALID_PARAM;
1491  LRETURN;
1492  }
1493 
1494  ui32LBA = CONFIG_SESSION_KeepAlive_W(session_id);
1495  if (ni_nvme_send_write_cmd(device_handle, event_handle, p_data,
1496  NI_DATA_BUFFER_LEN, ui32LBA) < 0)
1497  {
1498  ni_log(NI_LOG_ERROR, "ERROR: %s(): device_handle=%" PRIx64 " , "
1499  "session_id=%d\n", __func__, (int64_t)device_handle, session_id);
1500  retval = NI_RETCODE_FAILURE;
1501  }
1502  else
1503  {
1505  "SUCCESS %s(): device_handle=%" PRIx64 " , "
1506  "session_id=%d\n", __func__, (int64_t)device_handle, session_id);
1507  retval = NI_RETCODE_SUCCESS;
1508  }
1509 
1510 END:
1511 
1512  ni_log(NI_LOG_TRACE, "%s(): exit\n", __func__);
1513 
1514  return retval;
1515 }
1516 
1517 /*!******************************************************************************
1518  * \brief Send end of stream signal to the decoder
1519  *
1520  * \param
1521  *
1522  * \return
1523  *******************************************************************************/
1525 {
1526  ni_retcode_t retval;
1527  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1528  if (!p_ctx)
1529  {
1530  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
1531  __func__);
1532  retval = NI_RETCODE_INVALID_PARAM;
1533  LRETURN;
1534  }
1535 
1536  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1537  {
1538  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
1539  __func__);
1541  LRETURN;
1542  }
1543 
1545  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1546  p_ctx->device_type, p_ctx->hw_id,
1547  &(p_ctx->session_id), OPT_1);
1548  CHECK_VPU_RECOVERY(retval);
1549 
1550 END:
1551 
1552  if (NI_RETCODE_SUCCESS != retval)
1553  {
1554  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): %d, return\n", __func__, retval);
1555  }
1556 
1557  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1558 
1559  return retval;
1560 }
1561 
1562 /*!******************************************************************************
1563  * \brief Flush decoder output
1564  *
1565  * \param
1566  *
1567  * \return
1568  *******************************************************************************/
1570 {
1571  ni_retcode_t retval;
1572  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1573 
1574  if (!p_ctx)
1575  {
1576  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): passed parameters are null!, return\n",
1577  __func__);
1578  retval = NI_RETCODE_INVALID_PARAM;
1579  LRETURN;
1580  }
1581 
1582  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1583  {
1584  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): xcoder instance id < 0, return\n",
1585  __func__);
1587  LRETURN;
1588  }
1589 
1591  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1592  p_ctx->device_type, p_ctx->hw_id,
1593  &(p_ctx->session_id), OPT_1);
1594 
1595 END:
1596 
1597  if (NI_RETCODE_SUCCESS != retval)
1598  {
1599  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): %d, return\n", __func__, retval);
1600  }
1601 
1602  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1603 
1604  return retval;
1605 }
1606 
1607 /*!******************************************************************************
1608  * \brief Close a xcoder decoder instance
1609  *
1610  * \param
1611  *
1612  * \return
1613  *******************************************************************************/
1615 {
1617  void* p_buffer = NULL;
1618  uint32_t ui32LBA = 0;
1619  int i;
1620  ni_xcoder_params_t *p_param = NULL;
1621 
1622  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1623 
1624  if (!p_ctx)
1625  {
1626  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
1627  __func__);
1628  return NI_RETCODE_INVALID_PARAM;
1629  }
1630 
1631  ni_pthread_mutex_lock(&p_ctx->mutex);
1632 
1633  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1634  {
1635  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
1636  retval = NI_RETCODE_SUCCESS;
1637  LRETURN;
1638  }
1639 
1640  if (NI_CODEC_HW_ENABLE == p_ctx->hw_action)
1641  {
1642  ni_session_statistic_t sessionStatistic = {0};
1643  ni_query_session_statistic_info(p_ctx, NI_DEVICE_TYPE_DECODER, &sessionStatistic);
1644  }
1645 
1646  if (ni_cmp_fw_api_ver(
1647  (char *) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rg") >= 0)
1648  {
1649  ni_log2(p_ctx, NI_LOG_INFO,
1650  "Decoder_complete_info:session_id 0x%x, total frames input:%u "
1651  "buffered: %u completed: %u output: %u dropped: %u error: %u corrupted: %u\n",
1659  } else
1660  {
1661  ni_log2(p_ctx, NI_LOG_INFO,
1662  "Decoder_complete_info:session_id 0x%x, total frames input:%u "
1663  "buffered: %u completed: %u output: %u dropped: %u error: %u\n",
1670  }
1671 
1672  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
1673  {
1674  ni_log2(p_ctx, NI_LOG_ERROR, "%s: Invalid session ID, return.\n", __func__);
1675  retval = NI_RETCODE_SUCCESS;
1676  LRETURN;
1677  }
1678 
1679  //malloc data buffer
1680  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
1681  {
1682  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: malloc decoder close data buffer failed\n", NI_ERRNO);
1683  retval = NI_RETCODE_ERROR_MEM_ALOC;
1684  LRETURN;
1685  }
1686 
1687  if (p_ctx->p_session_config)
1688  {
1689  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
1690  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
1691  {
1693  if (NI_RETCODE_SUCCESS != retval)
1694  {
1695  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
1696  __func__, strerror(NI_ERRNO));
1697  LRETURN;
1698  }
1699  }
1700  }
1701 
1702  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1703 
1705 
1706  int retry = 0;
1707  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
1708  {
1709  ni_log2(p_ctx, NI_LOG_DEBUG,
1710  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
1711  "p_ctx->session_id=%d, close_mode=1\n",
1712  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
1713  p_ctx->session_id);
1714 
1716  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
1717  {
1718  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed!\n", __func__);
1721  break;
1722  } else
1723  {
1724  //Close should always succeed
1725  retval = NI_RETCODE_SUCCESS;
1727  break;
1728  }
1729  /*
1730  else if (*((ni_retcode_t *)p_buffer) == RETCODE_SUCCESS)
1731  {
1732  retval = NI_RETCODE_SUCCESS;
1733  p_ctx->session_id = NI_INVALID_SESSION_ID;
1734  break;
1735  }
1736  else
1737  {
1738  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): wait for close\n", __func__);
1739  ni_usleep(NI_SESSION_CLOSE_RETRY_INTERVAL_US);
1740  retval = NI_RETCODE_ERROR_NVME_CMD_FAILED;
1741  }
1742  */
1743  retry++;
1744  }
1745 
1746 END:
1747 
1748  ni_aligned_free(p_buffer);
1750  ni_memfree(p_ctx->p_leftover);
1751 
1752  if (p_ctx->pts_table)
1753  {
1754  ni_queue_free(&p_ctx->pts_table->list, p_ctx->buffer_pool);
1755  ni_memfree(p_ctx->pts_table);
1756  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
1757  }
1758 
1759  if (p_ctx->dts_queue)
1760  {
1761  ni_queue_free(&p_ctx->dts_queue->list, p_ctx->buffer_pool);
1762  ni_memfree(p_ctx->dts_queue);
1763  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
1764  }
1765 
1768  p_ctx->buffer_pool = NULL;
1769  p_ctx->dec_fme_buf_pool = NULL;
1770 
1771  for (i = 0; i < NI_FIFO_SZ; i++)
1772  {
1773  ni_memfree(p_ctx->pkt_custom_sei_set[i]);
1774  }
1775 
1776  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): CTX[Card:%" PRIx64 " / HW:%d / INST:%d]\n",
1777  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
1778  p_ctx->session_id);
1779 
1780  low_delay_signal(p_ctx);
1781  ni_pthread_mutex_unlock(&p_ctx->mutex);
1782 
1783  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1784 
1785  return retval;
1786 }
1787 
1788 /*!******************************************************************************
1789  * \brief Send a video p_packet to decoder
1790  *
1791  * \param
1792  *
1793  * \return
1794  *******************************************************************************/
1796 {
1797  uint32_t sent_size = 0;
1798  uint32_t packet_size = 0;
1799  int current_pkt_size;
1800  int retval = NI_RETCODE_SUCCESS;
1801  ni_xcoder_params_t *p_param;
1802  ni_instance_buf_info_t buf_info = { 0 };
1803  ni_session_statistic_t sessionStatistic = {0};
1804  int query_retry = 0;
1805  uint32_t ui32LBA = 0;
1806 
1807  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1808 
1809  if ((!p_ctx) || (!p_packet))
1810  {
1811  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
1812  __func__);
1813  return NI_RETCODE_INVALID_PARAM;
1814  }
1815 
1816  ni_pthread_mutex_lock(&p_ctx->mutex);
1817 
1818  if ((NI_INVALID_SESSION_ID == p_ctx->session_id))
1819  {
1820  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
1821  __func__);
1823  LRETURN;
1824  }
1825 
1826  low_delay_wait(p_ctx);
1827 
1828 #ifdef MEASURE_LATENCY
1829  if ((p_packet->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
1830  {
1831  uint64_t abs_time_ns = ni_gettime_ns();
1833  abs_time_ns, p_packet->dts);
1834  }
1835 #endif
1836 
1837  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
1838  packet_size = p_packet->data_len;
1839  current_pkt_size = packet_size;
1840 
1841 #ifdef XCODER_311
1842  int max_retry = 200; // 20ms
1843  if (p_packet->video_width * p_packet->video_height >= NI_NUM_OF_PIXELS_1440P)
1844  {
1845  max_retry = NI_MAX_TX_RETRIES; // 100ms
1846  }
1847 #endif
1848 
1849  for (;;)
1850  {
1851  query_sleep(p_ctx);
1852 
1853  query_retry++;
1854 
1855 
1857  "65") >= 0)
1858  {
1860  &sessionStatistic);
1861  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
1863  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
1864  CHECK_VPU_RECOVERY(retval);
1865 
1866  buf_info.buf_avail_size = sessionStatistic.ui32WrBufAvailSize;
1867  } else
1868  {
1870  NI_DEVICE_TYPE_DECODER, &buf_info);
1871  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
1872  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
1873  CHECK_VPU_RECOVERY(retval);
1874  }
1875 
1876  if (buf_info.buf_avail_size == DP_IPC_PASSTHRU)
1877  {
1878  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Bad available buffer size %u\n", __FUNCTION__, buf_info.buf_avail_size);
1879  retval = NI_RETCODE_FAILURE;
1880  LRETURN;
1881  }
1882 
1883  if (p_ctx->biggest_bitstream_buffer_allocated < buf_info.buf_avail_size)
1884  {
1886  }
1887  if (p_ctx->biggest_bitstream_buffer_allocated < packet_size &&
1889  {
1890  // Reallocate decoder bitstream buffers to accomodate
1892  "66") >= 0)
1893  {
1894  retval = ni_config_instance_set_write_len(p_ctx,
1896  // packet buffer aligned to NI_MAX_PACKET_SZ(128k)
1897  (packet_size / NI_MAX_PACKET_SZ + 1) * NI_MAX_PACKET_SZ);
1899  p_ctx->device_type, p_ctx->hw_id,
1900  &(p_ctx->session_id), OPT_3);
1901  CHECK_VPU_RECOVERY(retval);
1902  }
1903  else
1904  {
1905  retval = ni_config_instance_set_decoder_params(p_ctx, packet_size);
1906  }
1907  if (NI_RETCODE_SUCCESS != retval)
1908  {
1909  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): failed to reallocate bitstream\n", __FUNCTION__);
1910  LRETURN;
1911  }
1912  query_retry--;
1913  continue;
1914  }
1915 
1916  if (NI_RETCODE_SUCCESS != retval ||
1917  buf_info.buf_avail_size < packet_size)
1918  {
1919  ni_log2(p_ctx, NI_LOG_TRACE,
1920  "Warning: dec write query fail rc %d or available buf size %u < "
1921  "pkt size %u , retry: %d max_retry_fail_count %d\n",
1922  retval, buf_info.buf_avail_size, packet_size, query_retry, p_ctx->max_retry_fail_count[0]);
1923 #ifdef XCODER_311
1924  if (query_retry > max_retry ||
1925  sessionStatistic.ui32RdBufAvailSize > 0)
1926 #else
1927  if (query_retry > NI_MAX_TX_RETRIES ||
1928  sessionStatistic.ui32RdBufAvailSize > 0)
1929 #endif
1930  {
1932 #ifdef XCODER_311
1933  if (query_retry > max_retry)
1934 #else
1935  if (query_retry > NI_MAX_TX_RETRIES)
1936 #endif
1937  {
1938  p_ctx->max_retry_fail_count[0]++;
1939  }
1940  p_ctx->required_buf_size = packet_size;
1942  LRETURN;
1943  }
1944  ni_pthread_mutex_unlock(&p_ctx->mutex);
1946  ni_pthread_mutex_lock(&p_ctx->mutex);
1947  }
1948  else
1949  {
1950  p_ctx->max_retry_fail_count[0] = 0;
1951  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec write query success, available buf "
1952  "size %u >= pkt size %u !\n",
1953  buf_info.buf_avail_size, packet_size);
1954  break;
1955  }
1956  }
1957 
1958  //Configure write size for the buffer
1960  packet_size);
1962  p_ctx->device_type, p_ctx->hw_id,
1963  &(p_ctx->session_id), OPT_1);
1964  CHECK_VPU_RECOVERY(retval);
1965  if (retval < 0)
1966  {
1967  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): config pkt size command failed\n",
1968  __func__);
1970  LRETURN;
1971  }
1972 
1974 
1975  //check for start of stream flag
1976  if (p_packet->start_of_stream)
1977  {
1979  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1980  p_ctx->device_type, p_ctx->hw_id,
1981  &(p_ctx->session_id), OPT_1);
1982  CHECK_VPU_RECOVERY(retval);
1983  if (NI_RETCODE_SUCCESS != retval)
1984  {
1985  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Failed to send SOS.\n", __func__);
1986  LRETURN;
1987  }
1988 
1989  p_packet->start_of_stream = 0;
1990  }
1991 
1992  if (p_packet->p_data)
1993  {
1994  ni_log2(p_ctx, NI_LOG_DEBUG,
1995  "%s() had data to send: packet_size=%u, "
1996  "p_packet->sent_size=%d, p_packet->data_len=%u, "
1997  "p_packet->start_of_stream=%u, p_packet->end_of_stream=%u, "
1998  "p_packet->video_width=%u, p_packet->video_height=%u\n",
1999  __func__, packet_size, p_packet->sent_size, p_packet->data_len,
2000  p_packet->start_of_stream, p_packet->end_of_stream,
2001  p_packet->video_width, p_packet->video_height);
2002 
2003  uint8_t *p_data = (uint8_t *)p_packet->p_data;
2004  // Note: session status is NOT reset but tracked between send
2005  // and recv to catch and recover from a loop condition
2006  // p_ctx->status = 0;
2007 
2008  if (packet_size % NI_MEM_PAGE_ALIGNMENT) //packet size, already aligned
2009  {
2010  packet_size = ( (packet_size / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
2011  }
2012 
2013  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
2014  p_data, packet_size, ui32LBA);
2015  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write,
2016  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
2017  CHECK_VPU_RECOVERY(retval);
2018  if (retval < 0)
2019  {
2020  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
2022  LRETURN;
2023  }
2024 
2025  // reset session status after successful send
2026  p_ctx->status = 0;
2027  p_ctx->required_buf_size = 0;
2028 
2029  sent_size = p_packet->data_len;
2030  p_packet->data_len = 0;
2031 
2032  if (p_param->dec_input_params.nb_save_pkt)
2033  {
2034  char dump_file[512] = {0};
2035  long curr_pkt_num =
2036  ((long)p_ctx->pkt_num % p_param->dec_input_params.nb_save_pkt) + 1;
2037  snprintf(dump_file, sizeof(dump_file), "%s/pkt-%04ld.bin",
2038  p_ctx->stream_dir_name, curr_pkt_num);
2039  FILE *f = fopen(dump_file, "wb");
2040  if (f)
2041  {
2042  fwrite(p_packet->p_data, sent_size, 1, f);
2043  fflush(f);
2044  fclose(f);
2045  }
2046  }
2047 
2048  p_ctx->pkt_num++;
2049  p_ctx->low_delay_sync_flag = 1;
2050  }
2051 
2052  //Handle end of stream flag
2053  if (p_packet->end_of_stream)
2054  {
2056  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
2057  p_ctx->device_type, p_ctx->hw_id,
2058  &(p_ctx->session_id), OPT_1);
2059  CHECK_VPU_RECOVERY(retval);
2060 
2061  if (NI_RETCODE_SUCCESS != retval)
2062  {
2063  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Failed to send EOS.\n", __func__);
2064  LRETURN;
2065  }
2066 
2067  p_packet->end_of_stream = 0;
2068  p_ctx->ready_to_close = 1;
2069  }
2070 
2071  {
2072  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ] = p_packet->pts;
2073  p_ctx->flags_array[p_ctx->pkt_index % NI_FIFO_SZ] = p_packet->flags;
2074  p_ctx->pkt_pos[p_ctx->pkt_index % NI_FIFO_SZ] = p_packet->pkt_pos;
2075  if (p_ctx->pkt_index == 0)
2076  {
2077  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] = 0;
2078  /* minus 1 here. ffmpeg parses the msb 0 of long start code as the last packet's payload for hevc bitstream (hevc_parse).
2079  * move 1 byte forward on all the pkt_offset so that frame_offset coming from fw can fall into the correct range. */
2080  //p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ] = current_pkt_size - 1;
2081  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ] = current_pkt_size;
2082  ni_log2(p_ctx, NI_LOG_DEBUG,
2083  "%s: (first packet) pkt_index %d i %u "
2084  "pkt_offsets_index_min %" PRIu64 " pkt_offsets_index %" PRIu64
2085  " pts_offsets %" PRId64 "\n",
2086  __func__, p_ctx->pkt_index, p_ctx->pkt_index % NI_FIFO_SZ,
2087  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ],
2088  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ],
2089  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ]);
2090  }
2091  else
2092  {
2093  // cumulate sizes to correspond to FW offsets
2094  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] = p_ctx->pkt_offsets_index[(p_ctx->pkt_index - 1) % NI_FIFO_SZ];
2095  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ] = p_ctx->pkt_offsets_index[(p_ctx->pkt_index - 1) % NI_FIFO_SZ] + current_pkt_size;
2096  ni_log2(p_ctx, NI_LOG_DEBUG,
2097  "%s: pkt_index %d i %u pkt_offsets_index_min "
2098  "%" PRIu64 " pkt_offsets_index %" PRIu64 " pts_offsets %" PRId64
2099  "\n",
2100  __func__, p_ctx->pkt_index, p_ctx->pkt_index % NI_FIFO_SZ,
2101  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ],
2102  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ],
2103  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ]);
2104 
2105  //Wrapping 32 bits since FW send u32 wrapped values
2106  if (p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] > 0xFFFFFFFF)
2107  {
2108  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] = p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] - (0x100000000);
2109  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ] = p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] + current_pkt_size;
2110  ni_log2(p_ctx, NI_LOG_DEBUG,
2111  "%s: (wrap) pkt_index %d i %u "
2112  "pkt_offsets_index_min %" PRIu64 " pkt_offsets_index %" PRIu64
2113  " pts_offsets %" PRId64 "\n",
2114  __func__, p_ctx->pkt_index, p_ctx->pkt_index % NI_FIFO_SZ,
2115  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ],
2116  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ],
2117  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ]);
2118  }
2119  }
2120 
2121  /* if this wrap-around pkt_offset_index spot is about to be overwritten, free the previous one. */
2122  free(p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ]);
2123 
2124  if (p_packet->p_custom_sei_set)
2125  {
2126  p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ] = malloc(sizeof(ni_custom_sei_set_t));
2127  if (p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ])
2128  {
2129  ni_custom_sei_set_t *p_custom_sei_set = p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ];
2130  memcpy(p_custom_sei_set, p_packet->p_custom_sei_set, sizeof(ni_custom_sei_set_t));
2131  }
2132  else
2133  {
2134  /* warn and lose the sei data. */
2135  ni_log2(p_ctx, NI_LOG_ERROR,
2136  "Error %s: failed to allocate custom SEI buffer for pkt.\n",
2137  __func__);
2138  }
2139  }
2140  else
2141  {
2142  p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ] = NULL;
2143  }
2144 
2145  p_ctx->pkt_index++;
2146  }
2147 
2148  retval = ni_timestamp_register(p_ctx->buffer_pool, p_ctx->dts_queue, p_packet->dts, 0);
2149  if (NI_RETCODE_SUCCESS != retval)
2150  {
2151  ni_log2(p_ctx, NI_LOG_ERROR,
2152  "ERROR %s(): ni_timestamp_register() for dts returned %d\n",
2153  __func__, retval);
2154  }
2155 END:
2156 
2157  ni_pthread_mutex_unlock(&p_ctx->mutex);
2158 
2159  if (NI_RETCODE_SUCCESS == retval)
2160  {
2161  ni_log2(p_ctx, NI_LOG_TRACE,
2162  "%s(): exit: packets: %" PRIu64 " offset %" PRIx64 ""
2163  " sent_size = %u, status=%d\n",
2164  __func__, p_ctx->pkt_num, (uint64_t)p_packet->pos, sent_size,
2165  p_ctx->status);
2166  return sent_size;
2167  } else
2168  {
2169  ni_log2(p_ctx, NI_LOG_ERROR,
2170  "ERROR %s(): exit: returnErr: %d, p_ctx->status: %d\n", __func__,
2171  retval, p_ctx->status);
2172  return retval;
2173  }
2174 }
2175 
2176 static int64_t guess_correct_pts(ni_session_context_t* p_ctx, int64_t reordered_pts, int64_t dts)
2177 {
2178  int64_t pts = NI_NOPTS_VALUE;
2179  if (dts != NI_NOPTS_VALUE)
2180  {
2182  p_ctx->pts_correction_last_dts = dts;
2183  ni_log2(p_ctx, NI_LOG_DEBUG,
2184  "%s: pts_correction_last_dts %" PRId64 " "
2185  "pts_correction_num_faulty_dts %d\n",
2186  __func__, p_ctx->pts_correction_last_dts,
2188  }
2189  else if (reordered_pts != NI_NOPTS_VALUE)
2190  {
2191  p_ctx->pts_correction_last_dts = reordered_pts;
2192  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: pts_correction_last_dts %" PRId64 "\n", __func__,
2193  p_ctx->pts_correction_last_dts);
2194  }
2195  if (reordered_pts != NI_NOPTS_VALUE)
2196  {
2197  p_ctx->pts_correction_num_faulty_pts += reordered_pts <= p_ctx->pts_correction_last_pts;
2198  p_ctx->pts_correction_last_pts = reordered_pts;
2199  ni_log2(p_ctx, NI_LOG_DEBUG,
2200  "%s: pts_correction_last_pts %" PRId64 " "
2201  "pts_correction_num_faulty_pts %d\n",
2202  __func__, p_ctx->pts_correction_last_pts,
2204  }
2205  else if (dts != NI_NOPTS_VALUE)
2206  {
2207  p_ctx->pts_correction_last_pts = dts;
2208  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: pts_correction_last_pts %" PRId64 "\n", __func__,
2209  p_ctx->pts_correction_last_pts);
2210  }
2212  && reordered_pts != NI_NOPTS_VALUE)
2213  {
2214  pts = reordered_pts;
2215  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (reordered_pts) pts %" PRId64 "\n", __func__,
2216  pts);
2217  }
2218  else
2219  {
2220  if ((NI_NOPTS_VALUE == reordered_pts) ||
2221  (NI_NOPTS_VALUE == p_ctx->last_pts) || (dts >= p_ctx->last_pts))
2222  {
2223  pts = dts;
2224  } else
2225  {
2226  pts = reordered_pts;
2227  }
2228 
2229  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (dts) pts %" PRId64 "\n", __func__, pts);
2230  }
2231  return pts;
2232 }
2233 
2234 static int rotated_array_binary_search(uint64_t *lefts, uint64_t *rights,
2235  int32_t size, uint64_t target)
2236 {
2237  int lo = 0;
2238  int hi = size - 1;
2239  while (lo <= hi)
2240  {
2241  int mid = lo + (hi - lo) / 2;
2242  if (lefts[mid] <= target && target < rights[mid])
2243  {
2244  return mid;
2245  }
2246 
2247  if (rights[mid] == 0)
2248  {
2249  // empty in (mid, hi)
2250  hi = mid - 1;
2251  continue;
2252  }
2253 
2254  if (rights[lo] <= rights[mid])
2255  {
2256  if (lefts[lo] <= target && target < lefts[mid])
2257  {
2258  // Elements are all monotonous in (lo, mid)
2259  hi = mid - 1;
2260  } else
2261  {
2262  // Rotation in (mid, hi)
2263  lo = mid + 1;
2264  }
2265  } else
2266  {
2267  if (rights[mid] <= target && target < rights[hi])
2268  {
2269  // Elements are all monotonous in (mid, hi)
2270  lo = mid + 1;
2271  } else
2272  {
2273  // Rotation in (lo, mid)
2274  hi = mid - 1;
2275  }
2276  }
2277  }
2278 
2279  return -1;
2280 }
2281 
2282 /*!******************************************************************************
2283  * \brief Retrieve a YUV p_frame from decoder
2284  *
2285  * \param
2286  *
2287  * \return
2288  *******************************************************************************/
2290 {
2291  ni_instance_mgr_stream_info_t data = { 0 };
2292  int rx_size = 0;
2293  uint64_t frame_offset = 0;
2294  uint8_t *p_data_buffer = NULL;
2295  int i = 0;
2296  int is_planar;
2297  int retval = NI_RETCODE_SUCCESS;
2298  int metadata_hdr_size = NI_FW_META_DATA_SZ - NI_MAX_NUM_OF_DECODER_OUTPUTS * sizeof(niFrameSurface1_t);
2299  int sei_size = 0;
2300  uint32_t total_bytes_to_read = 0;
2301  uint32_t read_size_bytes = 0;
2302  ni_instance_buf_info_t buf_info = { 0 };
2303  ni_session_statistic_t sessionStatistic = {0};
2304  ni_xcoder_params_t *p_param;
2305 
2306  int query_retry = 0;
2307  uint32_t ui32LBA = 0;
2308  unsigned int bytes_read_so_far = 0;
2309  int query_type = INST_BUF_INFO_RW_READ;
2310  int low_delay_notify = 0;
2311  uint32_t frames_dropped = 0;
2312  uint8_t get_first_metadata = 0;
2313  uint8_t sequence_change = 0;
2314 
2315  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
2316 
2317  if ((!p_ctx) || (!p_frame))
2318  {
2319  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: passed parameters are null!, return\n");
2320  return NI_RETCODE_INVALID_PARAM;
2321  }
2322 
2323  ni_pthread_mutex_lock(&p_ctx->mutex);
2324 
2325 start:
2326  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
2327  {
2328  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): xcoder instance id < 0, return\n",
2329  __func__);
2331  LRETURN;
2332  }
2333 
2334  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
2335  p_data_buffer = (uint8_t *)p_frame->p_buffer;
2336 
2337  // p_frame->p_data[] can be NULL before actual resolution is returned by
2338  // decoder and buffer pool is allocated, so no checking here.
2339  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
2340  p_frame->data_len[2] + metadata_hdr_size;
2341  ni_log2(p_ctx, NI_LOG_DEBUG, "Total bytes to read %u, low_delay %u\n",
2342  total_bytes_to_read, p_ctx->decoder_low_delay);
2343 
2344  if (p_ctx->decoder_low_delay > 0 && !p_ctx->ready_to_close)
2345  {
2346  ni_log2(p_ctx, NI_LOG_DEBUG, "frame_num = %" PRIu64 ", pkt_num = %" PRIu64 "\n",
2347  p_ctx->frame_num, p_ctx->pkt_num);
2348  frames_dropped = p_ctx->session_statistic.ui32FramesDropped;
2349  if (p_ctx->force_low_delay && (p_ctx->force_low_delay_cnt < frames_dropped)) {
2350  p_ctx->force_low_delay_cnt = frames_dropped;
2351  }
2352  if (p_ctx->frame_num + p_ctx->force_low_delay_cnt >= p_ctx->pkt_num)
2353  {
2354  //nothing to query, leave
2355  retval = NI_RETCODE_SUCCESS;
2356  LRETURN;
2357  }
2358  query_type = INST_BUF_INFO_RW_READ_BUSY;
2359  }
2360  for (;;)
2361  {
2362  query_sleep(p_ctx);
2363 
2364  query_retry++;
2365 
2367  "65") >= 0)
2368  {
2370  &sessionStatistic);
2371  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
2373  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
2374  CHECK_VPU_RECOVERY(retval);
2375 
2376  buf_info.buf_avail_size = sessionStatistic.ui32RdBufAvailSize;
2377  } else
2378  {
2379  retval = ni_query_instance_buf_info(p_ctx, query_type,
2380  NI_DEVICE_TYPE_DECODER, &buf_info);
2381  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
2382  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
2383  CHECK_VPU_RECOVERY(retval);
2384  }
2385 
2386  ni_log2(p_ctx, NI_LOG_TRACE, "Info query buf_info.size = %u\n",
2387  buf_info.buf_avail_size);
2388 
2389  if (NI_RETCODE_SUCCESS != retval)
2390  {
2391  ni_log2(p_ctx, NI_LOG_TRACE, "Warning: dec read query fail rc %d retry %d max_retry_fail_count %d\n",
2392  retval, query_retry, p_ctx->max_retry_fail_count[1]);
2393 
2394  if (query_retry >= 1000)
2395  {
2396  p_ctx->max_retry_fail_count[1]++;
2397  low_delay_notify = 1;
2399  LRETURN;
2400  }
2401  ni_pthread_mutex_unlock(&p_ctx->mutex);
2403  ni_pthread_mutex_lock(&p_ctx->mutex);
2404  }
2405  else if (buf_info.buf_avail_size == DP_IPC_PASSTHRU)
2406  {
2407  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Bad available buffer size %u\n", __FUNCTION__, buf_info.buf_avail_size);
2408  retval = NI_RETCODE_FAILURE;
2409  LRETURN;
2410  }
2411  else if (buf_info.buf_avail_size == metadata_hdr_size)
2412  {
2413  ni_log2(p_ctx, NI_LOG_DEBUG, "Info only metadata hdr is available, seq change?\n");
2414  total_bytes_to_read = metadata_hdr_size;
2415  sequence_change = 1;
2416  break;
2417  }
2418  else if (0 == buf_info.buf_avail_size)
2419  {
2420  // query to see if it is eos now, if we have sent it
2421  if (p_ctx->ready_to_close)
2422  {
2423  ni_log2(p_ctx, NI_LOG_TRACE, "Info dec query, ready_to_close %u, query eos\n",
2424  p_ctx->ready_to_close);
2425  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
2426  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
2427  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
2428  CHECK_VPU_RECOVERY(retval);
2429 
2430  if (data.is_flushed ||
2431  query_retry >=
2433  {
2434  if (query_retry >=
2436  {
2437  ni_log2(p_ctx, NI_LOG_ERROR,
2438  "WARNING: Dec eos reached but also exceeded max dec read query "
2439  "retries. is_flushed=%u try=%d.\n",
2440  data.is_flushed, query_retry);
2441  } else
2442  {
2443  ni_log2(p_ctx, NI_LOG_DEBUG,
2444  "Dec eos reached. is_flushed=%u try=%d.\n",
2445  data.is_flushed, query_retry);
2446  }
2447  p_frame->end_of_stream = 1;
2448  low_delay_notify = 1;
2449  retval = NI_RETCODE_SUCCESS;
2450  LRETURN;
2451  } else
2452  {
2453  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read available buf size == 0, query try %d,"
2454  " retrying ..\n", query_retry);
2455  ni_pthread_mutex_unlock(&p_ctx->mutex);
2457  ni_pthread_mutex_lock(&p_ctx->mutex);
2458  continue;
2459  }
2460  }
2461 
2462  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read available buf size == 0. retry=%d, eos=%d"
2463  "\n", query_retry, p_frame->end_of_stream);
2465  (p_ctx->decoder_low_delay > 0 &&
2466  ((p_ctx->frame_num + p_ctx->force_low_delay_cnt)
2467  < p_ctx->pkt_num))) &&
2468  query_retry < 1000 / 2)
2469  {
2470  if (p_ctx->decoder_low_delay && p_ctx->force_low_delay) {
2471  if (p_ctx->session_statistic.ui32FramesDropped > frames_dropped) {
2472  // last pkt sent to decoder marked as dropped, no output,
2473  // so just stop query and return
2474  p_ctx->force_low_delay_cnt++;
2475  low_delay_signal(p_ctx);
2476  retval = NI_RETCODE_SUCCESS;
2477  LRETURN;
2478  }
2479  } else if (NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL == p_ctx->status &&
2480  sessionStatistic.ui32WrBufAvailSize > p_ctx->required_buf_size)
2481  {
2482  ni_log2(p_ctx, NI_LOG_TRACE, "Info dec write buffer is enough, available buf "
2483  "size %u >= required size %u !\n",
2484  sessionStatistic.ui32WrBufAvailSize, p_ctx->required_buf_size);
2485  p_ctx->status = 0;
2486  p_ctx->required_buf_size = 0;
2487  retval = NI_RETCODE_SUCCESS;
2488  LRETURN;
2489  }
2490  ni_pthread_mutex_unlock(&p_ctx->mutex);
2491  ni_usleep(25);
2492  ni_pthread_mutex_lock(&p_ctx->mutex);
2493  continue;
2494  } else
2495  {
2496  if(p_ctx->decoder_low_delay > 0)
2497  {
2498  if (p_ctx->force_low_delay) {
2499  p_ctx->force_low_delay_cnt++;
2500  low_delay_signal(p_ctx);
2501  } else {
2502  ni_log2(p_ctx, NI_LOG_ERROR,
2503  "Warning: ceased using low delay decoding mode after "
2504  "excessively long decoder read query.\n");
2505  // Here it should be the last signal to release the send thread
2506  // holding the low delay mutex.
2507  low_delay_signal(p_ctx);
2508  p_ctx->decoder_low_delay = 0;
2509  }
2510  }
2511 
2512  if ((p_param->dec_input_params.min_packets_delay && p_ctx->pkt_delay_cnt))
2513  {
2514  if(p_ctx->pkt_num >= (p_ctx->frame_num + p_ctx->pkt_delay_cnt +
2516  {
2517  if(query_retry <= 2000)
2518  {
2519  ni_pthread_mutex_unlock(&p_ctx->mutex);
2520  ni_usleep(25);
2521  ni_pthread_mutex_lock(&p_ctx->mutex);
2522  continue;
2523  } else {
2524  p_ctx->pkt_delay_cnt++;
2525  ni_log2(p_ctx, NI_LOG_ERROR,
2526  "Warning: decoder pkt_num %u frame_num %u "
2527  "timeout, increasing pkt_delay_cnt to %u\n",
2528  p_ctx->pkt_num, p_ctx->frame_num,
2529  p_ctx->pkt_delay_cnt);
2530  }
2531  }
2532  }
2533 
2534  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning: dec read failed %d retries. rc=%d; eos=%d\n",
2535  query_retry, p_ctx->status, p_frame->end_of_stream);
2536  }
2537  retval = NI_RETCODE_SUCCESS;
2538  LRETURN;
2539  }
2540  else
2541  {
2542  // We have to ensure there are adequate number of DTS for picture
2543  // reorder delay otherwise wait for more packets to be sent to decoder.
2544  ni_timestamp_table_t *p_dts_queue = p_ctx->dts_queue;
2545  if ((int)p_dts_queue->list.count < p_ctx->pic_reorder_delay + 1 &&
2546  !p_ctx->ready_to_close &&
2548  {
2549  retval = NI_RETCODE_SUCCESS;
2550  ni_log2(p_ctx, NI_LOG_DEBUG,
2551  "At least %d packets should be sent before reading the "
2552  "first frame!\n",
2553  p_ctx->pic_reorder_delay + 1);
2554  LRETURN;
2555  }
2556  p_ctx->max_retry_fail_count[1] = 0;
2557 
2558  // get actual YUV transfer size if this is the stream's very first read
2559  if (0 == p_ctx->active_video_width || 0 == p_ctx->active_video_height)
2560  {
2561  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
2562  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
2563  p_ctx->device_type, p_ctx->hw_id,
2564  &(p_ctx->session_id), OPT_1);
2565  CHECK_VPU_RECOVERY(retval);
2566 
2567  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV query, pic size %ux%u xfer frame size "
2568  "%ux%u frame-rate %u is_flushed %u\n",
2569  data.picture_width, data.picture_height,
2571  data.frame_rate, data.is_flushed);
2574  p_ctx->actual_video_width = data.picture_width;
2575  p_ctx->pixel_format = data.pix_format;
2576  is_planar = (p_ctx->pixel_format == NI_PIX_FMT_YUV420P) ||
2579  //p_ctx->bit_depth_factor = data.transfer_frame_stride / data.picture_width;
2580  p_ctx->is_first_frame = 1;
2581  p_ctx->pixel_format_changed = 0;
2582 
2583  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV, adjust frame size from %ux%u to "
2584  "%ux%u format = %d\n", p_frame->video_width, p_frame->video_height,
2585  p_ctx->active_video_width, p_ctx->active_video_height, p_ctx->pixel_format);
2586 
2588 
2589  // set up decoder YUV frame buffer pool
2592  p_ctx->active_video_height,
2594  p_ctx->bit_depth_factor))
2595  {
2596  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Cannot allocate fme buf pool.\n",
2597  __func__);
2598  retval = NI_RETCODE_ERROR_MEM_ALOC;
2599 
2600  ni_pthread_mutex_unlock(&p_ctx->mutex);
2601  ni_decoder_session_close(p_ctx, 0);
2602 #ifdef XCODER_SELF_KILL_ERR
2603  // if need to terminate at such occasion when continuing is not
2604  // possible, trigger a codec closure
2605  ni_log2(p_ctx, NI_LOG_ERROR, "Terminating due to unable to allocate fme buf "
2606  "pool.\n");
2607  kill(getpid(), SIGTERM);
2608 #endif
2609  ni_pthread_mutex_lock(&p_ctx->mutex);
2610  LRETURN;
2611  }
2612 
2614  p_ctx->dec_fme_buf_pool, p_frame, 1, // get mem buffer
2615  p_ctx->actual_video_width, p_ctx->active_video_height,
2617  p_ctx->bit_depth_factor, is_planar);
2618 
2619  if (NI_RETCODE_SUCCESS != retval)
2620  {
2621  LRETURN;
2622  }
2623  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
2624  p_frame->data_len[2] + metadata_hdr_size;
2625  p_data_buffer = (uint8_t*) p_frame->p_buffer;
2626 
2627  // make sure we don't read more than available
2628  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec buf size: %u YUV frame + meta-hdr size: %u "
2629  "available: %u\n", p_frame->buffer_size,
2630  total_bytes_to_read, buf_info.buf_avail_size);
2631  }
2632  break;
2633  }
2634  }
2635 
2636  ni_log2(p_ctx, NI_LOG_DEBUG, "total_bytes_to_read %u max_nvme_io_size %u ylen %u cr len "
2637  "%u cb len %u hdr %d\n",
2638  total_bytes_to_read, p_ctx->max_nvme_io_size,
2639  p_frame->data_len[0], p_frame->data_len[1],
2640  p_frame->data_len[2], metadata_hdr_size);
2641 
2642  if (buf_info.buf_avail_size < total_bytes_to_read)
2643  {
2644  ni_pthread_mutex_unlock(&p_ctx->mutex);
2645  ni_log2(p_ctx, NI_LOG_ERROR,
2646  "ERROR %s() avaliable size(%u)"
2647  "less than needed (%u)\n",
2648  __func__, buf_info.buf_avail_size, total_bytes_to_read);
2649  abort();
2650  }
2651 
2652  if (buf_info.buf_avail_size == metadata_hdr_size && (!p_ctx->frame_num || !p_data_buffer))
2653  {
2654  if (ni_cmp_fw_api_ver(
2656  "6rE") >= 0)
2657  {
2658  // allocate p_data_buffer to read the first metadata
2659  void *p_metadata_buffer = NULL;
2660  int buffer_size = ((metadata_hdr_size + (NI_MEM_PAGE_ALIGNMENT - 1)) /
2662  if (ni_posix_memalign(&p_metadata_buffer, sysconf(_SC_PAGESIZE), buffer_size))
2663  {
2664  ni_log2(p_ctx, NI_LOG_ERROR,
2665  "ERROR %d: %s() Cannot allocate metadata buffer.\n",
2666  NI_ERRNO, __func__);
2667  retval = NI_RETCODE_ERROR_MEM_ALOC;
2668  LRETURN;
2669  }
2670  p_data_buffer = (uint8_t *)p_metadata_buffer;
2671  get_first_metadata = 1;
2672  if (!p_ctx->frame_num)
2673  sequence_change = 0;
2674  }
2675  }
2676 
2677  if (!p_data_buffer)
2678  {
2679  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: data buffer is null!, return\n");
2680  retval = NI_RETCODE_INVALID_PARAM;
2681  LRETURN;
2682  }
2683 
2684  read_size_bytes = buf_info.buf_avail_size;
2686  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
2687  {
2688  read_size_bytes = ((read_size_bytes / NI_MEM_PAGE_ALIGNMENT) *
2691  }
2692 
2693  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
2694  p_data_buffer, read_size_bytes, ui32LBA);
2695  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
2696  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
2697  CHECK_VPU_RECOVERY(retval);
2698  if (retval < 0)
2699  {
2700  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
2702  LRETURN;
2703  } else if (get_first_metadata) {
2704  // got first metadata alone
2705  ni_metadata_dec_frame_t *p_meta =
2706  (ni_metadata_dec_frame_t *)((uint8_t *)p_data_buffer);
2707  ni_log2(p_ctx, NI_LOG_DEBUG, "Got first pkt_delay_cnt %u\n",
2708  p_meta->metadata_common.pkt_delay_cnt);
2709  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
2710  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
2711  get_first_metadata = 0;
2712  ni_aligned_free(p_data_buffer);
2713  goto start;
2714  } else {
2715  // command issued successfully, now exit
2716  ni_metadata_dec_frame_t *p_meta;
2717  p_meta =
2718  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
2719  p_frame->data_len[0] +
2720  p_frame->data_len[1] +
2721  p_frame->data_len[2]);
2722 
2723  if (buf_info.buf_avail_size != metadata_hdr_size)
2724  {
2725  low_delay_notify = 1;
2726  sei_size = p_meta->sei_size;
2727  } else if (ni_cmp_fw_api_ver(
2729  "6rE") >= 0)
2730  {
2731  p_meta =
2732  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer);
2733  ni_log2(p_ctx, NI_LOG_DEBUG, "Got pkt_delay_cnt %u\n",
2734  p_meta->metadata_common.pkt_delay_cnt);
2735  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
2736  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
2737  }
2738  total_bytes_to_read = total_bytes_to_read + sei_size;
2739  ni_log2(p_ctx, NI_LOG_DEBUG, "decoder read success, size %d total_bytes_to_read "
2740  "include sei %u sei_size %d\n",
2741  retval, total_bytes_to_read, sei_size);
2742  }
2743 
2744  bytes_read_so_far = total_bytes_to_read ;
2745  // Note: session status is NOT reset but tracked between send
2746  // and recv to catch and recover from a loop condition
2747 
2748  rx_size = ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, false);
2749  p_ctx->frame_pkt_offset = frame_offset;
2750  if (p_ctx->decoder_low_delay > 0 && buf_info.buf_avail_size == metadata_hdr_size &&
2751  p_ctx->enable_low_delay_check)
2752  {
2753  ni_log2(p_ctx, NI_LOG_TRACE, "Low delay mode amd check header if has b frame\n");
2754 
2755  ni_metadata_dec_frame_t *p_meta =
2756  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
2757  p_frame->data_len[0] +
2758  p_frame->data_len[1] +
2759  p_frame->data_len[2]);
2760  if (p_meta->metadata_common.has_b_frame == 1)
2761  {
2762  ni_log2(p_ctx, NI_LOG_ERROR,"Warning: session 0x%x decoder lowDelay mode "
2763  "is cancelled due to has_b_frames, frame_num %u\n",
2764  p_ctx->session_id, p_ctx->frame_num);
2765  p_ctx->decoder_low_delay = 0;
2766  }
2767  }
2768 
2769  if (rx_size > 0)
2770  {
2771  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): s-state %d first_frame %d\n", __func__,
2772  p_ctx->session_run_state, p_ctx->is_first_frame);
2773 
2774  int64_t tmp_dts, prev_dts = INT64_MIN, ts_diff = 0;
2775  int nb_diff = 0;
2776  // at stream start, if there are already frames dropped at decoding, drop
2777  // equal number of dts from dts queue to try to align with the first
2778  // returned frame, and calculate the average of dts difference to be used
2779  // for locating the first frame's dts
2780  if (p_ctx->is_first_frame)
2781  {
2782  ni_log2(p_ctx, NI_LOG_DEBUG,
2783  "%s(): First frame : session_id 0x%x, pic_reorder_delay: %d "
2784  "total frames input:%u buffered: %u completed: %u output: %u "
2785  "dropped: %u error: %u\n",
2786  __func__, p_ctx->session_id, p_ctx->pic_reorder_delay,
2793 
2794  if (p_ctx->session_statistic.ui32FramesDropped > 0)
2795  {
2796  for (i = 0; i < p_ctx->session_statistic.ui32FramesDropped; i++)
2797  {
2799  p_ctx->dts_queue, 0, &tmp_dts,
2801  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
2802  {
2803  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): FramesDropped pop "
2804  "decoder dts queue %d %ld failed !\n",
2805  __func__, i, tmp_dts);
2806  break;
2807  } else
2808  {
2809  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop "
2810  "decoder dts queue %d %ld success !\n",
2811  __func__, i, tmp_dts);
2812  if (prev_dts != INT64_MIN) {
2813  ts_diff += labs(tmp_dts - prev_dts);
2814  nb_diff++;
2815  }
2816  prev_dts = tmp_dts;
2817  }
2818  }
2819  if (nb_diff > 1)
2820  {
2821  ts_diff = ts_diff / nb_diff;
2822  }
2824  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop dts "
2825  "average diff: %ld\n", __func__, ts_diff);
2826  }
2827  }
2828  else
2829  {
2831  {
2832  for(i = p_ctx->decoder_last_drop_frame_num; i < p_ctx->session_statistic.ui32FramesDropped; i++)
2833  {
2835  p_ctx->dts_queue, 0, &tmp_dts,
2837  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
2838  {
2839  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): FramesDropped pop "
2840  "decoder dts queue %d %ld failed !\n",
2841  __func__, i, tmp_dts);
2842  break;
2843  }
2844  }
2846  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop dts %d\n", __func__, p_ctx->decoder_last_drop_frame_num);
2847  }
2848  }
2849 
2851  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
2853  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
2854  {
2855  if (p_ctx->last_dts != NI_NOPTS_VALUE && !p_ctx->ready_to_close)
2856  {
2857  p_ctx->pic_reorder_delay++;
2858  p_frame->dts = p_ctx->last_dts + p_ctx->last_dts_interval;
2859  ni_log2(p_ctx, NI_LOG_DEBUG, "Padding DTS: %" PRId64 "\n", p_frame->dts);
2860  } else
2861  {
2862  p_frame->dts = NI_NOPTS_VALUE;
2863  }
2864  }
2865 
2866  if (p_ctx->is_first_frame)
2867  {
2868  for (i = 0; i < p_ctx->pic_reorder_delay; i++)
2869  {
2870  if (p_ctx->last_pts == NI_NOPTS_VALUE &&
2871  p_ctx->last_dts == NI_NOPTS_VALUE)
2872  {
2873  // If the p_frame->pts is unknown in the very beginning we assume
2874  // p_frame->pts == 0 as well as DTS less than PTS by 1000 * 1/timebase
2875  if (p_frame->pts >= p_frame->dts &&
2876  p_frame->pts - p_frame->dts < 1000)
2877  {
2878  break;
2879  }
2880  }
2881 
2883  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
2885  p_ctx->frame_num % 500 == 0,
2886  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
2887  {
2888  p_frame->dts = NI_NOPTS_VALUE;
2889  } else
2890  {
2891  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): pic_reorder_delay pop "
2892  "decoder dts queue %d %ld success !\n",
2893  __func__, i, p_frame->dts);
2894  }
2895  }
2896  // Reset for DTS padding counting
2897  p_ctx->pic_reorder_delay = 0;
2898  }
2899  if (p_ctx->codec_format == NI_CODEC_FORMAT_JPEG)//fw won't save frameoffset when decoding jpeg.
2900  {
2901  if (p_ctx->is_first_frame)
2902  {
2903  p_ctx->is_first_frame = 0;
2904  }
2905  p_frame->pts = p_ctx->pts_offsets[p_ctx->frame_num % NI_FIFO_SZ];
2906  p_frame->flags = p_ctx->flags_array[p_ctx->frame_num % NI_FIFO_SZ];
2907  p_frame->pkt_pos = p_ctx->pkt_pos[p_ctx->frame_num % NI_FIFO_SZ];
2908  ni_log2(p_ctx, NI_LOG_DEBUG, "p_frame->pts = %u, frame_num = %d, p_frame->dts = %u\n",
2909  p_frame->pts, p_ctx->frame_num, p_frame->dts);
2910  }
2911  else
2912  {
2913  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: frame_offset %" PRIu64 "\n", __func__,
2914  frame_offset);
2915 
2916  // search for the pkt_offsets of received frame according to frame_offset.
2917  // here we get the index(i) which promises (p_ctx->pkt_offsets_index_min[i] <= frame_offset && p_ctx->pkt_offsets_index[i] > frame_offset)
2918  // i = -1 if not found
2919  i = rotated_array_binary_search(p_ctx->pkt_offsets_index_min,
2920  p_ctx->pkt_offsets_index, NI_FIFO_SZ,
2921  frame_offset);
2922  if (i >= 0)
2923  {
2924  p_frame->pts = p_ctx->pts_offsets[i];
2925  p_frame->flags = p_ctx->flags_array[i];
2926  p_frame->pkt_pos = p_ctx->pkt_pos[i];
2927  ni_log2(p_ctx, NI_LOG_DEBUG,
2928  "%s: (found pts) dts %" PRId64 " pts "
2929  "%" PRId64 " frame_offset %" PRIu64 " i %d "
2930  "pkt_offsets_index_min %" PRIu64 " pkt_offsets_index "
2931  "%" PRIu64 " pkt_pos %" PRIu64 " \n",
2932  __func__, p_frame->dts, p_frame->pts, frame_offset, i,
2933  p_ctx->pkt_offsets_index_min[i],
2934  p_ctx->pkt_offsets_index[i],
2935  p_ctx->pkt_pos[i]);
2936 
2937  if (p_ctx->is_first_frame)
2938  {
2939  // if the first frame is I frame and there are dropped frames,
2940  // find the dts closest to its pts using the average of dts diff
2941  if (p_frame->dts != NI_NOPTS_VALUE &&
2942  p_frame->pts != NI_NOPTS_VALUE &&
2943  PIC_TYPE_I == p_frame->ni_pict_type &&
2944  p_ctx->session_statistic.ui32FramesDropped > 0 &&
2945  ts_diff > 0)
2946  {
2947  while (p_frame->dts < p_frame->pts &&
2948  labs(p_frame->pts - p_frame->dts) > ts_diff)
2949  {
2950  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: First I frame pts %ld "
2951  "dts %ld diff. %ld > ts_diff %ld\n",
2952  __func__, p_frame->pts, p_frame->dts,
2953  labs(p_frame->pts - p_frame->dts), ts_diff);
2954 
2956  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
2958  {
2959  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): First I frame "
2960  "pop decoder dts queue error.\n", __func__);
2961  break;
2962  }
2963  }
2964  }
2965  }
2966 
2967  p_frame->p_custom_sei_set = p_ctx->pkt_custom_sei_set[i];
2968  p_ctx->pkt_custom_sei_set[i] = NULL;
2969  } else
2970  {
2971  // backup solution pts
2972  if (p_param->dec_input_params.skip_pts_guess && p_ctx->last_pts != NI_NOPTS_VALUE)
2973  {
2974  // if skip guess_correct_pts, use pts interval to get the correct pts
2975  p_frame->pts = p_ctx->last_pts + (p_ctx->last_pts_interval > 0 ? p_ctx->last_pts_interval : 1);
2976  }
2977  else
2978  {
2979  p_frame->pts = p_ctx->last_pts + (p_frame->dts - p_ctx->last_dts);
2980  }
2981  p_frame->pkt_pos = p_ctx->last_pkt_pos + (frame_offset - p_ctx->last_frame_offset);
2982  ni_log2(p_ctx, NI_LOG_ERROR,
2983  "ERROR: Frame pts %" PRId64 " not found for offset "
2984  "%" PRIu64 "\n", p_frame->pts, frame_offset);
2985  ni_log2(p_ctx, NI_LOG_DEBUG,
2986  "%s: (not found use default) dts %" PRId64 " pts %" PRId64
2987  "\n",
2988  __func__, p_frame->dts, p_frame->pts);
2989  }
2990 
2991  if (p_ctx->is_first_frame)
2992  {
2993  p_ctx->is_first_frame = 0;
2994  }
2995  }
2996  p_frame->orignal_pts = p_frame->pts;
2997  p_ctx->last_pkt_pos = p_frame->pkt_pos;
2998  p_ctx->last_frame_offset = frame_offset;
2999  if (!p_param->dec_input_params.skip_pts_guess)
3000  p_frame->pts = guess_correct_pts(p_ctx, p_frame->pts, p_frame->dts);
3001  if (p_frame->pts != NI_NOPTS_VALUE && p_ctx->last_pts != NI_NOPTS_VALUE)
3002  p_ctx->last_pts_interval = p_frame->pts - p_ctx->last_pts;
3003  p_ctx->last_pts = p_frame->pts;
3004  if (p_frame->dts != NI_NOPTS_VALUE && p_ctx->last_dts != NI_NOPTS_VALUE)
3005  p_ctx->last_dts_interval = p_frame->dts - p_ctx->last_dts;
3006  p_ctx->last_dts = p_frame->dts;
3007  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (best_effort_timestamp) pts %" PRId64 "\n",
3008  __func__, p_frame->pts);
3009  p_ctx->frame_num++;
3010 
3012  if (p_frame->error_ratio > 0)
3013  {
3014  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: frame number = %d, error_ratio = %u\n",
3015  __func__, p_ctx->frame_num, p_frame->error_ratio);
3016  }
3017 
3018 #ifdef MEASURE_LATENCY
3019 #ifndef XCODER_311
3020  ni_log2(p_ctx, NI_LOG_INFO, "DEC pkt_num %d, fme_num %d, latecy is %d\n",
3021  p_ctx->pkt_num, p_ctx->frame_num, p_ctx->pkt_num - p_ctx->frame_num);
3022 #endif
3023 #endif
3024 
3025 #ifdef XCODER_DUMP_DATA
3026  char dump_file[256];
3027  snprintf(dump_file, sizeof(dump_file), "%ld-%u-dec-fme/fme-%04ld.yuv",
3028  (long)getpid(), p_ctx->session_id, (long)p_ctx->frame_num);
3029  FILE *f = fopen(dump_file, "wb");
3030  fwrite(p_frame->p_buffer,
3031  p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2],
3032  1, f);
3033  fflush(f);
3034  fclose(f);
3035 #endif
3036  }
3037 
3038  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): received data: [0x%08x]\n", __func__, rx_size);
3039  ni_log2(p_ctx, NI_LOG_DEBUG,
3040  "%s(): p_frame->start_of_stream=%u, "
3041  "p_frame->end_of_stream=%u, p_frame->video_width=%u, "
3042  "p_frame->video_height=%u\n",
3043  __func__, p_frame->start_of_stream, p_frame->end_of_stream,
3044  p_frame->video_width, p_frame->video_height);
3045  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): p_frame->data_len[0/1/2]=%u/%u/%u\n", __func__,
3046  p_frame->data_len[0], p_frame->data_len[1], p_frame->data_len[2]);
3047 
3048  if (p_ctx->frame_num % 500 == 0)
3049  {
3050  ni_log2(p_ctx, NI_LOG_DEBUG,
3051  "Decoder pts queue size = %u dts queue size = %u\n\n",
3052  p_ctx->pts_table->list.count, p_ctx->dts_queue->list.count);
3053  // scan and clean up
3055  p_ctx->buffer_pool);
3056  }
3057 
3058 #ifdef MEASURE_LATENCY
3059  if ((p_frame->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
3060  {
3061  uint64_t abs_time_ns = ni_gettime_ns();
3063  ni_log2(p_ctx, NI_LOG_INFO, "DTS:%" PRId64 ",DELTA:%" PRId64 ",dLAT:%" PRIu64 ";\n",
3064  p_frame->dts, abs_time_ns - q->last_benchmark_time,
3065  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->dts));
3066  q->last_benchmark_time = abs_time_ns;
3067  }
3068 #endif
3069 
3070 END:
3071 
3072  ni_pthread_mutex_unlock(&p_ctx->mutex);
3073 
3074  if (get_first_metadata && p_data_buffer)
3075  ni_aligned_free(p_data_buffer);
3076  if (sequence_change && p_ctx->frame_num)
3077  {
3078  if (p_ctx->actual_video_width == p_frame->video_width &&
3079  p_ctx->active_video_height == p_frame->video_height)
3080  {
3081  p_ctx->pixel_format_changed = 1;
3082  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): format changed\n", __func__);
3083  }
3084  }
3085 
3086  if (NI_RETCODE_SUCCESS != retval)
3087  {
3088  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
3089  if (retval == NI_RETCODE_ERROR_VPU_RECOVERY)
3090  {
3091  low_delay_signal(p_ctx);
3092  }
3093  return retval;
3094  } else
3095  {
3096  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
3097  if (low_delay_notify)
3098  {
3099  low_delay_signal(p_ctx);
3100  }
3101  return rx_size;
3102  }
3103 }
3104 
3105 /*!******************************************************************************
3106  * \brief Query current xcoder status
3107  *
3108  * \param
3109  *
3110  * \return
3111  *******************************************************************************/
3113  ni_device_type_t device_type)
3114 {
3116  int retval = NI_RETCODE_SUCCESS;
3117 
3118  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): device_type %d:%s; enter\n", __func__,
3119  device_type, g_device_type_str[device_type]);
3120 
3121  if (!p_ctx)
3122  {
3123  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3124  __func__);
3125  retval = NI_RETCODE_INVALID_PARAM;
3126  LRETURN;
3127  }
3128 
3129  retval = ni_query_general_status(p_ctx, device_type, &data);
3130  if (NI_RETCODE_SUCCESS == retval)
3131  {
3135  p_ctx->overall_load_query.admin_queried = (uint32_t)data.admin_nsid;
3136  p_ctx->load_query.current_load =
3137  (uint32_t)data.process_load_percent | (uint32_t)(data.process_load_percent_upper << 8);
3138  p_ctx->load_query.fw_model_load = (uint32_t)data.fw_model_load;
3139  p_ctx->load_query.fw_load = (uint32_t)data.fw_load;
3141 
3142  switch (device_type)
3143  {
3146  case NI_DEVICE_TYPE_UPLOAD:
3147  if (p_ctx->load_query.fw_load == 1 &&
3149  {
3150  // ignore negligible non-video traffic
3151  p_ctx->load_query.fw_load -= 1;
3152  }
3153  break;
3154  default:
3155  break;
3156  }
3157  p_ctx->load_query.total_contexts = (uint32_t)data.active_sub_instances_cnt;
3158  p_ctx->load_query.fw_video_mem_usage = (uint32_t)data.fw_video_mem_usage;
3160  p_ctx->load_query.fw_share_mem_usage = (uint32_t)data.fw_share_mem_usage;
3161  p_ctx->load_query.fw_p2p_mem_usage = (uint32_t)data.fw_p2p_mem_usage;
3163  (uint32_t)data.active_hwupload_sub_inst_cnt;
3164  ni_log2(p_ctx, NI_LOG_DEBUG, "%s blk_dev %s blk_xcoder %s dev_xcoder %s current_load:%u model_load:%u fw_load:%u "
3165  "total_contexts:%u fw_video_mem_usage:%u "
3166  "fw_video_shared_mem_usage:%u fw_share_mem_usage:%u "
3167  "fw_p2p_mem_usage:%u active_hwuploaders:%u\n", __func__,
3168  p_ctx->blk_dev_name,
3169  p_ctx->blk_xcoder_name,
3170  p_ctx->dev_xcoder_name,
3171  p_ctx->load_query.current_load,
3172  p_ctx->load_query.fw_model_load,
3173  p_ctx->load_query.fw_load,
3174  p_ctx->load_query.total_contexts,
3180  if (p_ctx->overall_load_query.admin_queried)
3181  {
3182  ni_log2(p_ctx, NI_LOG_DEBUG,
3183  "Overall load %u, model load %u, instance count %u\n",
3187  }
3188  }
3189 
3190 END:
3191 
3192  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3193 
3194  return retval;
3195 }
3196 
3197 /*!******************************************************************************
3198  * \brief Query current xcoder status
3199  *
3200  * \param
3201  *
3202  * \return
3203  *******************************************************************************/
3205  ni_device_type_t device_type, void *detail_data, int ver)
3206 {
3207  int retval = NI_RETCODE_SUCCESS;
3208 
3209  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): device_type %d:%s; enter\n", __func__,
3210  device_type, g_device_type_str[device_type]);
3211 
3212  if (!p_ctx)
3213  {
3214  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3215  __func__);
3216  retval = NI_RETCODE_INVALID_PARAM;
3217  LRETURN;
3218  }
3219 
3220  retval = ni_query_detail_status(p_ctx, device_type, detail_data, ver);
3221 
3222 END:
3223 
3224  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3225 
3226  return retval;
3227 }
3228 
3229 /*!******************************************************************************
3230  * \brief Open a xcoder encoder instance
3231  *
3232  * \param
3233  *
3234  * \return
3235  *******************************************************************************/
3236 
3238 {
3240  ni_xcoder_params_t *p_param;
3241  void *p_buffer = NULL;
3242  ni_instance_buf_info_t buf_info = {0};
3243  uint32_t ui32LBA = 0;
3244  uint32_t max_cu_size;
3245  uint32_t block_size;
3246  int32_t width, height;
3247  int m_threshold = 0;
3248  char fmt_fw_api_ver1[5], fmt_fw_api_ver2[5];
3249 
3250  if (!p_ctx || !p_ctx->p_session_config)
3251  {
3252  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3253  __func__);
3254  retval = NI_RETCODE_INVALID_PARAM;
3255  LRETURN;
3256  }
3257 
3258  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
3259  bool isrgba = (p_ctx->pixel_format == NI_PIX_FMT_ABGR || p_ctx->pixel_format == NI_PIX_FMT_ARGB
3260  || p_ctx->pixel_format == NI_PIX_FMT_RGBA || p_ctx->pixel_format == NI_PIX_FMT_BGRA);
3261  if (isrgba)
3262  {
3264  || p_param->source_width < NI_MIN_WIDTH || p_param->source_height < NI_MIN_HEIGHT)
3265  {
3266  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() RGBA / BGRA / ARGB / AGBR resolution invalid, return\n",
3267  __func__);
3268  retval = NI_RETCODE_INVALID_PARAM;
3269  LRETURN;
3270  }
3271  }
3272 
3273  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter hwframes = %d\n", __func__,
3274  p_param->hwframes);
3275 
3276  // calculate encoder ROI map size: each QP info takes 8-bit, represent 8 x 8
3277  // pixel block
3278  max_cu_size = (NI_CODEC_FORMAT_H264 == p_ctx->codec_format) ? 16 : 64;
3279 
3280  width = p_param->source_width;
3281  height = p_param->source_height;
3282  // AV1 non-8x8-aligned resolution is implicitly cropped due to Quadra HW limitation
3283  if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
3284  {
3285  width = (width / 8) * 8;
3286  height = (height / 8) * 8;
3287  }
3288 
3289  block_size =
3290  ((width + max_cu_size - 1) & (~(max_cu_size - 1))) *
3291  ((height + max_cu_size - 1) & (~(max_cu_size - 1))) /
3292  (8 * 8);
3293  p_ctx->roi_len = ((block_size + 63) & (~63)); // align to 64 bytes
3294 
3295  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): finish init\n", __func__);
3296 
3297  //Check if there is an instance or we need a new one
3298  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3299  {
3301  p_ctx->pts_table = NULL;
3302  p_ctx->dts_queue = NULL;
3303  p_ctx->buffer_pool = NULL;
3304  p_ctx->status = 0;
3305  p_ctx->key_frame_type = 0;
3306  p_ctx->keyframe_factor = 1;
3307  p_ctx->frame_num = 0;
3308  p_ctx->pkt_num = 0;
3309  p_ctx->av1_pkt_num = 0;
3310  p_ctx->rc_error_count = 0;
3311  p_ctx->force_frame_type = 0;
3312  p_ctx->ready_to_close = 0;
3313  p_ctx->auto_dl_handle = 0;
3314  //Sequence change tracking related stuff
3315  p_ctx->active_video_width = 0;
3316  p_ctx->active_video_height = 0;
3317  p_ctx->p_all_zero_buf = NULL;
3318  p_ctx->actual_video_width = 0;
3319  p_ctx->enc_pts_w_idx = 0;
3320  p_ctx->enc_pts_r_idx = 0;
3321  p_ctx->session_timestamp = 0;
3322  memset(p_ctx->pkt_custom_sei_set, 0, NI_FIFO_SZ * sizeof(ni_custom_sei_set_t *));
3323  memset(&(p_ctx->param_err_msg[0]), 0, sizeof(p_ctx->param_err_msg));
3325  {
3327  }
3328 
3329  //malloc zero data buffer
3330  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
3332  {
3333  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
3334  NI_ERRNO, __func__);
3335  retval = NI_RETCODE_ERROR_MEM_ALOC;
3336  LRETURN;
3337  }
3338  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
3339 
3340  //malloc data buffer
3341  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
3342  {
3343  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
3344  NI_ERRNO, __func__);
3346  retval = NI_RETCODE_ERROR_MEM_ALOC;
3347  LRETURN;
3348  }
3349  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3350 
3351  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
3352  //In case we can open sesison, the session id would become valid.
3353  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
3354  (uint16_t)NI_INVALID_SESSION_ID;
3355 
3356  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
3358  ni_htonl(p_ctx->codec_format), 0);
3359  ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer,
3360  NI_DATA_BUFFER_LEN, ui32LBA);
3361  //Open will return a session status structure with a valid session id if it worked.
3362  //Otherwise the invalid session id set before the open command will stay
3363  p_ctx->session_id =
3364  ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
3365  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
3366  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
3367  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
3368  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3369  {
3370  ni_log2(p_ctx, NI_LOG_ERROR,
3371  "ERROR %s(): p_ctx->device_handle=%" PRIx64 ", "
3372  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
3373  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3374  p_ctx->session_id);
3375  ni_encoder_session_close(p_ctx, 0);
3377  LRETURN;
3378  }
3379  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder open session ID:0x%x timestamp:%" PRIu64 "\n",
3380  p_ctx->session_id, p_ctx->session_timestamp);
3381 
3382  //Send keep alive timeout Info
3383  uint64_t keep_alive_timeout =
3384  p_ctx->keep_alive_timeout * 1000000; //send us to FW
3385  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3386  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
3387  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
3388  keep_alive_timeout);
3390  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
3391  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
3392  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
3393  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
3394  CHECK_VPU_RECOVERY(retval);
3395 
3396  if (NI_RETCODE_SUCCESS != retval)
3397  {
3398  ni_log2(p_ctx, NI_LOG_ERROR,
3399  "ERROR %s(): nvme write keep_alive_timeout command "
3400  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
3401  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
3403  LRETURN;
3404  }
3405 
3406  // Send SW version to FW if FW API version is >= 6.2
3408  "62") >= 0)
3409  {
3410  // Send SW version to session manager
3411  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3412  memcpy(p_buffer, NI_XCODER_REVISION, sizeof(uint64_t));
3414  ni_fmt_fw_api_ver_str((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], &fmt_fw_api_ver2[0]);
3415  ni_log2(p_ctx, NI_LOG_DEBUG, "%s libxcoder FW API ver %s, FW FW API ver %s\n",
3416  __func__, fmt_fw_api_ver1, fmt_fw_api_ver2);
3417  ui32LBA = CONFIG_SESSION_SWVersion_W(p_ctx->session_id);
3418  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
3419  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
3420  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
3421  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
3422  CHECK_VPU_RECOVERY(retval);
3423 
3424  if (NI_RETCODE_SUCCESS != retval)
3425  {
3426  ni_log2(p_ctx, NI_LOG_ERROR,
3427  "ERROR %s(): nvme write sw_version command "
3428  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
3429  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
3431  LRETURN;
3432  }
3433 
3434  if (ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rc") >= 0)
3435  {
3436  // For FW API ver 6rc or newer, initialize with the most current size
3437  p_ctx->meta_size = sizeof(ni_metadata_enc_bstream_t);
3438  }
3440  "6p") >= 0)
3441  // For FW API ver 6.p or newer, initialize with the most current size
3443  else
3444  // For FW API ver 6.2 or newer, initialize with the most current size
3446  }
3447  else
3448  {
3449  // For FW API ver 6.1 or older, initialize with metadata size 32
3451  }
3452 
3453  // Open AI session for AI Enhance
3454  if(p_param->enable_ai_enhance)
3455  {
3456  // Check if the bit depth & resoultion
3457  if ((p_ctx->bit_depth_factor != 1) ||
3458  !((p_param->source_width == 1280 && p_param->source_height == 720) ||
3459  (p_param->source_width == 1920 && p_param->source_height == 1080) ||
3460  (p_param->source_width == 3840 && p_param->source_height == 2160) ||
3461  (p_param->source_width == 720 && p_param->source_height == 1280) ||
3462  (p_param->source_width == 768 && p_param->source_height == 1280) ||
3463  (p_param->source_width == 1080 && p_param->source_height == 1920) ||
3464  (p_param->source_width == 2496 && p_param->source_height == 1080)))
3465  {
3466  p_param->enable_ai_enhance = 0;
3467  ni_log2(p_ctx, NI_LOG_ERROR, "Resoultion=%dx%d,depth=%d\n",p_param->source_width,p_param->source_height,p_ctx->bit_depth_factor);
3468  ni_log2(p_ctx, NI_LOG_ERROR, "The bit depth or resoultion is not supported on ai enhance mode\n");
3469  }else{
3470  ni_log2(p_ctx, NI_LOG_DEBUG, "Adding ai enhance session to encoder\n");
3471  ui32LBA = OPEN_ADD_CODEC(NI_DEVICE_TYPE_AI, 0, ni_htons(p_ctx->session_id));
3472  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
3473  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
3474 
3475  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_open,
3476  p_ctx->device_type, p_ctx->hw_id,
3477  &(p_ctx->session_id), OPT_1);
3478  if (NI_RETCODE_SUCCESS != retval)
3479  {
3480  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR couldn't add ai to encoder session\n");
3481  ni_log2(p_ctx, NI_LOG_ERROR,
3482  "ERROR %s(): p_ctx->device_handle=%" PRIx64 ", "
3483  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
3484  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3485  p_ctx->session_id);
3486  ni_encoder_session_close(p_ctx, 0);
3487  LRETURN;
3488  }
3489  }
3490  }
3491 
3492  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
3493  {
3494  retval = ni_device_set_ddr_configuration(p_ctx, p_param->ddr_priority_mode);
3495  if (NI_RETCODE_SUCCESS != retval)
3496  {
3497  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
3498  __func__, strerror(NI_ERRNO));
3499  LRETURN;
3500  }
3501  }
3502 
3503  ni_log2(p_ctx, NI_LOG_DEBUG, "Open session completed\n");
3504  }
3505 
3506  /* Modify mmap_threshold and trim_threshold of malloc */
3507  // calculate the default threshold which equals to the resolution + padding size.
3508  // 64 is AV_INPUT_BUFFER_PADDING_SIZE
3509  m_threshold = p_param->source_width * p_param->source_height * 3 / 2 *
3510  p_ctx->bit_depth_factor + 64;
3511  m_threshold = ((m_threshold + (NI_MEM_PAGE_ALIGNMENT - 1)) /
3513 
3514  // The upper limit is DEFAULT_MMAP_THRESHOLD_MAX: 512*1024 on 32-bit systems,
3515  // or 4*1024*1024*sizeof(long) on 64-bit systems.
3516  // This macro is not defined in header file.
3517  // So, assume that is running on 64-bit systems
3518  if (m_threshold > 4 * 1024 * 1024 * sizeof(long))
3519  {
3520  ni_log2(p_ctx, NI_LOG_INFO, "Warning: m_threshold (%d) is bigger than "
3521  "DEFAULT_MMAP_THRESHOLD_MAX, use default value (%d)\n",
3522  m_threshold, 4 * 1024 * 1024 * sizeof(long));
3523  m_threshold = 4 * 1024 * 1024 * sizeof(long);
3524  }
3525 
3526  if (p_param->staticMmapThreshold) // Set Static Mmap Threshold
3527  {
3528 #if defined(__linux__) && !defined(_ANDROID) && !defined(__OPENHARMONY__)
3529  // If the malloc buffer is larger than the threshold,
3530  // glibc will use mmap to malloc the memory, which is a low speed method.
3531  // So, set the M_MMAP_THRESHOLD >= 1 yuv buffer size here.
3532  if (mallopt(M_MMAP_THRESHOLD, m_threshold) == 0)
3533  {
3534  ni_log2(p_ctx, NI_LOG_ERROR, "Error: could not set M_MMAP_THRESHOLD to %d\n",
3535  m_threshold);
3536  retval = NI_RETCODE_ERROR_MEM_ALOC;
3537  LRETURN;
3538  }
3539 
3540  // When the amount of contiguous free memory at the top of the heap
3541  // grows larger than M_TRIM_THRESHOLD,
3542  // free(3) employs sbrk(2) to release this memory back to the system.
3543  // So, set the M_TRIM_THRESHOLD to 3*m_threshold,
3544  // to avoid an inefficient case, frequently releasing and requesting physical memory.
3545  if (mallopt(M_TRIM_THRESHOLD, 3 * m_threshold) == 0)
3546  {
3547  ni_log2(p_ctx, NI_LOG_ERROR, "Error: could not set M_TRIM_THRESHOLD to %ds\n",
3548  3 * m_threshold);
3549  retval = NI_RETCODE_ERROR_MEM_ALOC;
3550  LRETURN;
3551  }
3552 #else
3553  ni_log2(p_ctx, NI_LOG_INFO,
3554  "Warning: this platform does not support staticMmapThreshold\n");
3555 #endif
3556  }
3557  else // default
3558  {
3559  // a way to dynamically trigger a pre-mature increase in mmap&trim threshold
3560  // make the mmap_threshold larger than frame size to avoid frequent page faults
3561  m_threshold = (int) (m_threshold * 1.25); // An experience value
3562  void *temp_buf = malloc((size_t) m_threshold);
3563  ni_log2(p_ctx, NI_LOG_TRACE, "trigger a pre-mature increase in mmap&trim threshold: 0x%p = malloc(%d)\n",
3564  temp_buf, m_threshold);
3565  free(temp_buf);
3566  }
3567 
3568  if (p_ctx->hw_action == NI_CODEC_HW_ENABLE)
3569  {
3570  ni_device_capability_t sender_cap, receiver_cap;
3571  bool device_in_ctxt = true;
3572  retval = ni_device_capability_query2(p_ctx->sender_handle, &sender_cap, device_in_ctxt);
3573  if (retval != NI_RETCODE_SUCCESS)
3574  {
3575  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_device_capability_query2 returned %d\n",
3576  retval);
3577  LRETURN;
3578  }
3579  retval = ni_device_capability_query2(p_ctx->blk_io_handle, &receiver_cap, device_in_ctxt);
3580  if (retval != NI_RETCODE_SUCCESS)
3581  {
3582  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_device_capability_query2 returned %d\n",
3583  retval);
3584  LRETURN;
3585  }
3586 
3587  for (uint8_t ui8Index = 0; ui8Index < 20; ui8Index++)
3588  {
3589  if (sender_cap.serial_number[ui8Index] !=
3590  receiver_cap.serial_number[ui8Index])
3591  {
3592  // QDFW-315 Autodownload
3593  p_ctx->auto_dl_handle = p_ctx->sender_handle;
3594  ni_log2(p_ctx, NI_LOG_DEBUG, "Autodownload device handle set %p!\n",
3595  p_ctx->auto_dl_handle);
3596  p_ctx->hw_action = NI_CODEC_HW_NONE;
3597  break;
3598  }
3599  else
3600  {
3602  "6m") >= 0)
3603  {
3604  ni_device_vf_ns_id_t sender_vf_ns_id = {0};
3605  ni_device_vf_ns_id_t curr_vf_ns_id = {0};
3606  retval = ni_query_vf_ns_id(p_ctx->sender_handle, &sender_vf_ns_id, p_ctx->fw_rev);
3607  if (retval != NI_RETCODE_SUCCESS)
3608  {
3609  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: calling ni_query_vf_ns_id(): "
3610  "p_ctx->sender_handle=%" PRIx64 ", p_ctx->hw_id=%d, p_ctx->session_id=%u\n",
3611  (int64_t)p_ctx->sender_handle, p_ctx->hw_id, p_ctx->session_id);
3612  ni_encoder_session_close(p_ctx, 0);
3613  LRETURN;
3614  }
3615  retval = ni_query_vf_ns_id(p_ctx->blk_io_handle, &curr_vf_ns_id, p_ctx->fw_rev);
3616  if (retval != NI_RETCODE_SUCCESS)
3617  {
3618  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: calling ni_query_vf_ns_id(): "
3619  "p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, p_ctx->session_id=%u\n",
3620  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
3621  ni_encoder_session_close(p_ctx, 0);
3622  LRETURN;
3623  }
3624 
3625  if ((sender_vf_ns_id.ns_id != curr_vf_ns_id.ns_id) ||
3626  (sender_vf_ns_id.vf_id != curr_vf_ns_id.vf_id))
3627  {
3628  p_ctx->auto_dl_handle = p_ctx->sender_handle;
3629  ni_log2(p_ctx, NI_LOG_DEBUG, "Autodownload device handle set %p!\n",
3630  p_ctx->auto_dl_handle);
3631  p_ctx->hw_action = NI_CODEC_HW_NONE;
3632  break;
3633  }
3634  }
3635  }
3636  }
3637  }
3638 
3639  retval = ni_config_instance_set_encoder_params(p_ctx);
3640  if (NI_RETCODE_SUCCESS != retval)
3641  {
3642  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: calling ni_config_instance_set_encoder_params(): "
3643  "p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
3644  (int64_t)p_ctx->device_handle, p_ctx->hw_id, p_ctx->session_id);
3645  ni_encoder_session_close(p_ctx, 0);
3646  LRETURN;
3647  }
3648  if(p_param->enable_ai_enhance)
3649  {
3650  retval = ni_ai_query_network_ready(p_ctx);
3651  if(retval != NI_RETCODE_SUCCESS)
3652  {
3653  ni_log2(p_ctx, NI_LOG_ERROR,
3654  "ERROR: ni_ai_query_network_ready returned %d\n", retval);
3655  LRETURN;
3656  }
3657  }
3658 
3659  ni_timestamp_init(p_ctx, &p_ctx->pts_table, "enc_pts");
3660  ni_timestamp_init(p_ctx, &p_ctx->dts_queue, "enc_dts");
3661 
3662  // init close caption SEI header and trailer
3663  memcpy(p_ctx->itu_t_t35_cc_sei_hdr_hevc, g_itu_t_t35_cc_sei_hdr_hevc,
3665  memcpy(p_ctx->itu_t_t35_cc_sei_hdr_h264, g_itu_t_t35_cc_sei_hdr_h264,
3667  memcpy(p_ctx->sei_trailer, g_sei_trailer, NI_CC_SEI_TRAILER_LEN);
3668  // init hdr10+ SEI header
3669  memcpy(p_ctx->itu_t_t35_hdr10p_sei_hdr_hevc, g_itu_t_t35_hdr10p_sei_hdr_hevc,
3671  memcpy(p_ctx->itu_t_t35_hdr10p_sei_hdr_h264, g_itu_t_t35_hdr10p_sei_hdr_h264,
3673 
3674  // query to check the final encoder config status
3675  for (;;)
3676  {
3678  NI_DEVICE_TYPE_ENCODER, &buf_info);
3679 
3680  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
3681  p_ctx->device_type, p_ctx->hw_id,
3682  &(p_ctx->session_id), OPT_1);
3683 
3685  || NI_RETCODE_FAILURE == retval)
3686  {
3687  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() line-%d return %d\n", __func__, __LINE__, retval);
3688  LRETURN;
3689  }
3690  else if (buf_info.buf_avail_size > 0)
3691  {
3692  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): buf_avail_size %u\n", __func__,
3693  buf_info.buf_avail_size);
3694  break;
3695  }
3696  else
3697  {
3698  ni_usleep(1000);
3699  }
3700  }
3701 
3702  ni_log2(p_ctx, NI_LOG_DEBUG,
3703  "%s(): p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
3704  "p_ctx->session_id=%d\n",
3705  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3706  p_ctx->session_id);
3707 
3708 #ifdef XCODER_DUMP_DATA
3709  char dir_name[256] = {0};
3710  snprintf(dir_name, sizeof(dir_name), "%ld-%u-enc-pkt", (long)getpid(),
3711  p_ctx->session_id);
3712  DIR *dir = opendir(dir_name);
3713  if (!dir && ENOENT == NI_ERRNO)
3714  {
3715  mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO);
3716  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder pkt dump dir created: %s\n", dir_name);
3717  }
3718 
3719  if(dir){
3720  closedir(dir);
3721  }
3722 
3723  snprintf(dir_name, sizeof(dir_name), "%ld-%u-enc-fme", (long)getpid(),
3724  p_ctx->session_id);
3725  dir = opendir(dir_name);
3726  if (!dir && ENOENT == NI_ERRNO)
3727  {
3728  mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO);
3729  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder frame dump dir created: %s\n", dir_name);
3730  }
3731  if(dir){
3732  closedir(dir);
3733  }
3734 #endif
3735 
3736 END:
3737 
3738  ni_aligned_free(p_buffer);
3739  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3740 
3741  return retval;
3742 }
3743 
3744 /*!******************************************************************************
3745  * \brief Flush encoder output
3746  *
3747  * \param
3748  *
3749  * \return
3750  *******************************************************************************/
3752 {
3753  ni_retcode_t retval;
3754  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
3755  if (!p_ctx)
3756  {
3757  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3758  __func__);
3759  retval = NI_RETCODE_INVALID_PARAM;
3760  LRETURN;
3761  }
3762 
3763  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3764  {
3765  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
3766  __func__);
3768  LRETURN;
3769  }
3770 
3772  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
3773  p_ctx->device_type, p_ctx->hw_id,
3774  &(p_ctx->session_id), OPT_1);
3775  CHECK_VPU_RECOVERY(retval);
3776 
3777 END:
3778 
3779  if (NI_RETCODE_SUCCESS != retval)
3780  {
3781  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): %d, return\n", __func__, retval);
3782  }
3783 
3784  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3785 
3786  return retval;
3787 }
3788 
3789 /*!******************************************************************************
3790  * \brief Close a xcoder encoder instance
3791  *
3792  * \param
3793  *
3794  * \return
3795  *******************************************************************************/
3797 {
3799  void* p_buffer = NULL;
3800  uint32_t ui32LBA = 0;
3801  int i = 0;
3802  ni_xcoder_params_t *p_param = NULL;
3803 
3804  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
3805 
3806  if (!p_ctx)
3807  {
3808  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3809  __func__);
3810  return NI_RETCODE_INVALID_PARAM;
3811  }
3812 
3813  ni_pthread_mutex_lock(&p_ctx->mutex);
3814 
3815  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3816  {
3817  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
3818  retval = NI_RETCODE_SUCCESS;
3819  LRETURN;
3820  }
3821 
3822  if (p_ctx->device_type == NI_DEVICE_TYPE_ENCODER)
3823  {
3824  ni_log2(p_ctx, NI_LOG_INFO,
3825  "Encoder_complete_info:session_id 0x%x, total frames input:%u "
3826  "buffered: %u completed: %u output: %u dropped: %u error: %u\n",
3833  }
3834 
3835  //malloc data buffer
3836  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
3837  {
3838  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
3839  NI_ERRNO, __func__);
3840  retval = NI_RETCODE_ERROR_MEM_ALOC;
3841  LRETURN;
3842  }
3843  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3844 
3845  if (p_ctx->p_session_config)
3846  {
3847  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
3848  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
3849  {
3851  if (NI_RETCODE_SUCCESS != retval)
3852  {
3853  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
3854  __func__, strerror(NI_ERRNO));
3855  LRETURN;
3856  }
3857  }
3858  }
3859 
3861 
3862  int retry = 0;
3863  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
3864  {
3865  ni_log2(p_ctx, NI_LOG_DEBUG,
3866  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
3867  "p_ctx->session_id=%d, close_mode=1\n",
3868  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
3869  p_ctx->session_id);
3870 
3872  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
3873  {
3874  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed\n", __func__);
3877  break;
3878  } else
3879  {
3880  //Close should always succeed
3881  retval = NI_RETCODE_SUCCESS;
3883  break;
3884  }
3885  /*
3886  else if(((ni_session_closed_status_t *)p_buffer)->session_closed)
3887  {
3888  retval = NI_RETCODE_SUCCESS;
3889  p_ctx->session_id = NI_INVALID_SESSION_ID;
3890  break;
3891  }
3892  else
3893  {
3894  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): wait for close\n");
3895  ni_usleep(NI_SESSION_CLOSE_RETRY_INTERVAL_US);
3896  retval = NI_RETCODE_ERROR_NVME_CMD_FAILED;
3897  }
3898  */
3899  retry++;
3900  }
3901 
3902 END:
3903 
3904  ni_aligned_free(p_buffer);
3906 
3907  //Sequence change related stuff cleanup here
3908  p_ctx->active_video_width = 0;
3909  p_ctx->active_video_height = 0;
3910  p_ctx->actual_video_width = 0;
3911  //End of sequence change related stuff cleanup
3912 
3913  if (p_ctx->pts_table)
3914  {
3915  ni_queue_free(&p_ctx->pts_table->list, p_ctx->buffer_pool);
3916  ni_memfree(p_ctx->pts_table);
3917  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
3918  }
3919 
3920  if (p_ctx->dts_queue)
3921  {
3922  ni_queue_free(&p_ctx->dts_queue->list, p_ctx->buffer_pool);
3923  ni_memfree(p_ctx->dts_queue);
3924  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
3925  }
3926 
3928  p_ctx->buffer_pool = NULL;
3929 
3930  for (i = 0; i < NI_FIFO_SZ; i++)
3931  {
3932  ni_memfree(p_ctx->pkt_custom_sei_set[i]);
3933  }
3934 
3935  for (i = 0; i < 120 ; i++)
3936  {
3937  if (p_ctx->input_frame_fifo[i].p_input_buffer != NULL)
3938  {
3939  free(p_ctx->input_frame_fifo[i].p_input_buffer);
3940  p_ctx->input_frame_fifo[i].p_input_buffer = NULL;
3941  }
3942  }
3943 
3944  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): CTX[Card:%" PRIx64 " / HW:%d / INST:%d]\n",
3945  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3946  p_ctx->session_id);
3947  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3948 
3949  low_delay_signal(p_ctx);
3950  ni_pthread_mutex_unlock(&p_ctx->mutex);
3951 
3952  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3953 
3954  return retval;
3955 }
3956 
3957 
3958 void enqueue_ni_frame(ni_session_context_t *p_ctx, ni_frame_t *ni_frame, int32_t source_width, int32_t source_height)
3959 {
3960  int32_t width_stride[4] = {0};
3961  int32_t height_stride[4] = {0};
3962 
3963  if (((ni_xcoder_params_t *)(p_ctx->p_session_config))->zerocopy_mode &&
3964  ((ni_xcoder_params_t *)(p_ctx->p_session_config))->luma_linesize != 0 &&
3965  ((ni_xcoder_params_t *)(p_ctx->p_session_config))->chroma_linesize != 0)
3966  {
3967  //zero copy happend
3968  width_stride[0] = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->luma_linesize;
3969  width_stride[1] = width_stride[2] = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->chroma_linesize;
3970  }
3971  else
3972  {
3973  int is_semiplanar = !ni_get_planar_from_pixfmt(p_ctx->pixel_format);
3974  ni_get_hw_yuv420p_dim(ni_frame->video_width, ni_frame->video_height,
3975  p_ctx->bit_depth_factor, is_semiplanar,
3976  width_stride, height_stride);
3977  }
3978 
3979  for (int i = 0; i < 120; i++)
3980  {
3981  // ni_log2(p_ctx, NI_LOG_ERROR, "%s %d i %d frame_num %d p_input_buffer %p data_len[0:2] %d %d %d p_data[0:2] %p %p %p video_width %d video_height %d\n",
3982  // __FUNCTION__, __LINE__, i, p_ctx->frame_num, p_ctx->input_frame_fifo[i].p_input_buffer,
3983  // ni_frame->data_len[0], ni_frame->data_len[1], ni_frame->data_len[2],
3984  // ni_frame->p_data[0], ni_frame->p_data[1], ni_frame->p_data[2],
3985  // ni_frame->video_width, ni_frame->video_height);
3986  if ((source_width != p_ctx->input_frame_fifo[i].video_width ||
3987  source_height != p_ctx->input_frame_fifo[i].video_height) &&
3988  p_ctx->input_frame_fifo[i].p_input_buffer != NULL &&
3989  p_ctx->input_frame_fifo[i].usable == 1)
3990  {
3991  free(p_ctx->input_frame_fifo[i].p_input_buffer);
3992  p_ctx->input_frame_fifo[i].p_input_buffer = NULL;
3993  p_ctx->input_frame_fifo[i].usable = -1;
3994  }
3995 
3996  if (p_ctx->input_frame_fifo[i].p_input_buffer == NULL)
3997  {
3998  p_ctx->input_frame_fifo[i].p_input_buffer = (uint8_t *)malloc(source_width * p_ctx->bit_depth_factor * source_height * 3 / 2);
3999 
4000  for (int j = 0; j < source_height; j++)
4001  {
4002  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * j,
4003  ni_frame->p_data[0] + j * width_stride[0],
4004  source_width * p_ctx->bit_depth_factor);
4005  }
4006  for (int j = 0; j < (source_height + 1) / 2; j++)
4007  {
4008  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * source_height + (source_width * p_ctx->bit_depth_factor / 2) * j,
4009  ni_frame->p_data[1] + j * width_stride[1],
4010  source_width / 2 * p_ctx->bit_depth_factor);
4011  }
4012  for (int j = 0; j < (source_height + 1) / 2; j++)
4013  {
4014  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * source_height * 5 / 4 + (source_width * p_ctx->bit_depth_factor / 2) * j,
4015  ni_frame->p_data[2] + j * width_stride[2],
4016  source_width / 2 * p_ctx->bit_depth_factor);
4017  }
4018 
4019  p_ctx->input_frame_fifo[i].video_width = source_width;
4020  p_ctx->input_frame_fifo[i].video_height = source_height;
4021 
4022  p_ctx->input_frame_fifo[i].usable = -1;
4023  p_ctx->input_frame_fifo[i].pts = ni_frame->pts;
4024  // ni_log2(p_ctx, NI_LOG_DEBUG, "%s %d i %d frame_num %d buffer_size %d video_width %d video_height %d pts %lld\n",
4025  // __FUNCTION__, __LINE__, i, p_ctx->frame_num, ni_frame->buffer_size, ni_frame->video_width, ni_frame->video_height,
4026  // p_ctx->input_frame_fifo[i].pts);
4027  break;
4028  }
4029  else if (p_ctx->input_frame_fifo[i].usable == 1)
4030  {
4031  for (int j = 0; j < source_height; j++)
4032  {
4033  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * j,
4034  ni_frame->p_data[0] + j * width_stride[0],
4035  source_width * p_ctx->bit_depth_factor);
4036  }
4037  for (int j = 0; j < (source_height + 1) / 2; j++)
4038  {
4039  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * source_height + (source_width * p_ctx->bit_depth_factor / 2) * j,
4040  ni_frame->p_data[1] + j * width_stride[1],
4041  source_width / 2 * p_ctx->bit_depth_factor);
4042  }
4043  for (int j = 0; j < (source_height + 1) / 2; j++)
4044  {
4045  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * source_height * 5 / 4 + (source_width * p_ctx->bit_depth_factor / 2) * j,
4046  ni_frame->p_data[2] + j * width_stride[2],
4047  source_width / 2 * p_ctx->bit_depth_factor);
4048  }
4049 
4050  p_ctx->input_frame_fifo[i].video_width = source_width;
4051  p_ctx->input_frame_fifo[i].video_height = source_height;
4052 
4053  p_ctx->input_frame_fifo[i].usable = -1;
4054  p_ctx->input_frame_fifo[i].pts = ni_frame->pts;
4055  break;
4056  }
4057  }
4058 }
4059 
4060 /*!******************************************************************************
4061  * \brief Send a YUV p_frame to encoder
4062  *
4063  * \param
4064  *
4065  * \return
4066  *******************************************************************************/
4068 {
4069  bool ishwframe = false;
4070  uint32_t size = 0;
4071  uint32_t send_count = 0;
4072  uint32_t i = 0;
4073  uint32_t sent_size = 0;
4074  uint32_t frame_size_bytes = 0;
4075  int retval = 0;
4076  ni_instance_buf_info_t buf_info = { 0 };
4077  ni_session_statistic_t sessionStatistic = {0};
4078 
4079  if (!p_ctx || !p_frame)
4080  {
4081  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
4082  __func__);
4083  return NI_RETCODE_INVALID_PARAM;
4084  }
4085  ishwframe = p_ctx->hw_action & NI_CODEC_HW_ENABLE;
4086  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter hw=%d\n", __func__, ishwframe);
4087 
4088  if (ishwframe && !p_frame->end_of_stream)
4089  {
4090  // check if the hw input frame is valid
4091  if (!p_frame->p_data[3])
4092  {
4093  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() hw input frame is null!, return\n",
4094  __func__);
4095  return NI_RETCODE_INVALID_PARAM;
4096  }
4097  uint16_t input_frame_idx = ((niFrameSurface1_t*)(p_frame->p_data[3]))->ui16FrameIdx;
4098  if (!((input_frame_idx > 0 &&
4099  input_frame_idx < NI_GET_MAX_HWDESC_FRAME_INDEX(p_ctx->ddr_config)) ||
4100  (input_frame_idx > NI_GET_MIN_HWDESC_P2P_BUF_ID(p_ctx->ddr_config) &&
4101  input_frame_idx <= NI_GET_MAX_HWDESC_P2P_BUF_ID(p_ctx->ddr_config))))
4102  {
4103  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): got invalid frameIdx [%u]\n",
4104  __func__, input_frame_idx);
4105  return NI_RETCODE_INVALID_PARAM;
4106  }
4107  }
4108 
4109  ni_pthread_mutex_lock(&p_ctx->mutex);
4110 
4111  uint8_t separate_metadata = p_frame->separate_metadata;
4112 
4113  uint8_t separate_start = (p_frame->separate_start && p_frame->total_start_len) ? 1 : 0;
4114 
4115  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
4116  {
4117  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
4118  __func__);
4120  LRETURN;
4121  }
4122 
4123  low_delay_wait(p_ctx);
4124 
4125 #ifdef MEASURE_LATENCY
4126  if ((p_frame->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
4127  {
4128  uint64_t abs_time_ns = ni_gettime_ns();
4129  ni_lat_meas_q_t *frame_time_q = (ni_lat_meas_q_t *)p_ctx->frame_time_q;
4130  ni_lat_meas_q_add_entry(frame_time_q, abs_time_ns, p_frame->dts);
4131  }
4132 #endif
4133 
4134  /*!********************************************************************/
4135  /*!************ Sequence Change related stuff *************************/
4136  //First check squence changed related stuff.
4137  //We need to record the current hight/width params if we didn't do it before:
4138 
4139  if( p_frame->video_height)
4140  {
4141  p_ctx->active_video_width = p_frame->data_len[0] / p_frame->video_height;
4142  p_ctx->active_video_height = p_frame->video_height;
4143  }
4144  else if (p_frame->video_width)
4145  {
4146  ni_log2(p_ctx, NI_LOG_DEBUG, "WARNING: passed video_height is not valid!, return\n");
4147  p_ctx->active_video_height = p_frame->data_len[0] / p_frame->video_width;
4148  p_ctx->active_video_width = p_frame->video_width;
4149  }
4150  else
4151  {
4152  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: passed video_height and video_width are not valid!, return\n");
4154  LRETURN;
4155  }
4156  // record actual width (in pixels / without padding) for sequnce change detection
4157  p_ctx->actual_video_width = p_frame->video_width;
4158 
4159  /*!************ Sequence Change related stuff end*************************/
4160  /*!********************************************************************/
4161 
4162  frame_size_bytes = p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2] + p_frame->data_len[3] + p_frame->extra_data_len;
4163  ni_log2(p_ctx, NI_LOG_DEBUG,
4164  "%s: data_len[0] %u data_len[1] %u "
4165  "data_len[2] %u extra_data_len %u frame_size_bytes %u\n",
4166  __func__, p_frame->data_len[0], p_frame->data_len[1],
4167  p_frame->data_len[2], p_frame->extra_data_len, frame_size_bytes);
4168 
4169  // skip query write buffer because we just send EOS
4170  if (!p_frame->end_of_stream)
4171  {
4172  for (;;)
4173  {
4174  query_sleep(p_ctx);
4175 
4177  "65") >= 0)
4178  {
4180  p_ctx, NI_DEVICE_TYPE_ENCODER, &sessionStatistic);
4181  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
4183  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
4184  CHECK_VPU_RECOVERY(retval);
4185  buf_info.buf_avail_size = sessionStatistic.ui32WrBufAvailSize;
4186  } else
4187  {
4188  retval =
4190  NI_DEVICE_TYPE_ENCODER, &buf_info);
4191  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4192  p_ctx->device_type, p_ctx->hw_id,
4193  &(p_ctx->session_id), OPT_1);
4194  CHECK_VPU_RECOVERY(retval);
4195  }
4196 
4197  if (NI_RETCODE_FAILURE == retval)
4198  {
4199  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() line-%d retrun %d\n", __func__, __LINE__, retval);
4200  LRETURN;
4201  }
4202  else if (NI_RETCODE_SUCCESS != retval ||
4203  buf_info.buf_avail_size < frame_size_bytes)
4204  {
4205  ni_log2(p_ctx, NI_LOG_TRACE,
4206  "Enc write query retry %d. rc=%d. Available buf size %u < "
4207  "frame size %u\n", retval, send_count,
4208  buf_info.buf_avail_size, frame_size_bytes);
4209  if (send_count >= NI_MAX_ENCODER_QUERY_RETRIES)
4210  {
4211  int retval_backup = retval;
4212  retval = ni_query_instance_buf_info(
4215  {
4216  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4217  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4218  CHECK_VPU_RECOVERY(retval);
4219  }
4220  else
4221  retval = NI_RETCODE_SUCCESS;
4222 
4223  ni_log2(p_ctx, NI_LOG_DEBUG,
4224  "Enc write query buf info exceeded max retries: "
4225  "%d, rc=%d. Available buf size %u < frame size %u\n",
4226  NI_MAX_ENCODER_QUERY_RETRIES, retval_backup,
4227  buf_info.buf_avail_size, frame_size_bytes);
4229 
4230  LRETURN;
4231  }
4232  send_count++;
4233  ni_pthread_mutex_unlock(&p_ctx->mutex);
4235  ni_pthread_mutex_lock(&p_ctx->mutex);
4236  } else
4237  {
4238  ni_log2(p_ctx, NI_LOG_DEBUG,
4239  "Info enc write query success, available buf "
4240  "size %u >= frame size %u !\n",
4241  buf_info.buf_avail_size, frame_size_bytes);
4242  break;
4243  }
4244  }
4245  }
4246 
4247  // fill in metadata such as timestamp
4248  ni_metadata_enc_frame_t *p_meta;
4249  if (separate_metadata)
4250  {
4251  p_meta = (ni_metadata_enc_frame_t *)p_frame->p_metadata_buffer;
4252  } else
4253  {
4254  p_meta = (ni_metadata_enc_frame_t *)((uint8_t *)
4255  p_frame->p_data[2 + ishwframe] +
4256  p_frame->data_len[2 + ishwframe]);
4257  }
4258 
4259  if (p_meta) //When hwframe xcoding reaches eos, frame looks like swframe but no allocation for p_meta
4260  {
4261  p_meta->metadata_common.ui64_data.frame_tstamp = (uint64_t)p_frame->pts;
4262 
4263  p_meta->force_headers = 0; // p_frame->force_headers not implemented/used
4266  p_meta->use_long_term_ref = p_frame->use_long_term_ref;
4267 
4268  ni_log2(p_ctx, NI_LOG_DEBUG,
4269  "%s: p_meta "
4270  "use_cur_src_as_long_term_pic %d use_long_term_ref %d\n",
4271  __func__, p_meta->use_cur_src_as_long_term_pic,
4272  p_meta->use_long_term_ref);
4273 
4274  p_meta->frame_force_type_enable = p_meta->frame_force_type = 0;
4275  // frame type to be forced to is supposed to be set correctly
4276  // in p_frame->ni_pict_type
4277  if (1 == p_ctx->force_frame_type || p_frame->force_key_frame)
4278  {
4279  if (p_frame->ni_pict_type)
4280  {
4281  p_meta->frame_force_type_enable = 1;
4282  p_meta->frame_force_type = p_frame->ni_pict_type;
4283  }
4284  ni_log2(p_ctx, NI_LOG_DEBUG,
4285  "%s(): ctx->force_frame_type"
4286  " %d frame->force_key_frame %d force frame_num %" PRIu64 ""
4287  " type to %d\n",
4288  __func__, p_ctx->force_frame_type, p_frame->force_key_frame,
4289  p_ctx->frame_num, p_frame->ni_pict_type);
4290  }
4291 
4292  // force pic qp if specified
4293  p_meta->force_pic_qp_enable = p_meta->force_pic_qp_i =
4294  p_meta->force_pic_qp_p = p_meta->force_pic_qp_b = 0;
4295  if (p_frame->force_pic_qp)
4296  {
4297  p_meta->force_pic_qp_enable = 1;
4298  p_meta->force_pic_qp_i = p_meta->force_pic_qp_p =
4299  p_meta->force_pic_qp_b = p_frame->force_pic_qp;
4300  }
4301  p_meta->frame_sei_data_size = p_frame->sei_total_len;
4302  p_meta->frame_roi_map_size = p_frame->roi_len;
4303  p_meta->frame_roi_avg_qp = p_ctx->roi_avg_qp;
4304  p_meta->enc_reconfig_data_size = p_frame->reconf_len;
4305 
4307  "6Q") >= 0)
4308  {
4309  if (separate_start)
4310  {
4311  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
4312  p_meta->start_len[i] = p_frame->start_len[i];
4313  }
4314  else
4315  {
4316  memset(p_meta->start_len, 0, sizeof(p_meta->start_len));
4317  }
4319  }
4320 
4321  if (p_ctx->frame_num % ((ni_xcoder_params_t *)(p_ctx->p_session_config))->interval_of_psnr == 0)
4322  p_meta->get_recon_frame_mode = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode;
4323  else
4324  p_meta->get_recon_frame_mode = 3;
4325 
4326  ni_log2(p_ctx,
4327  NI_LOG_DEBUG,
4328  "%s(): %d.%u p_ctx->frame_num=%" PRIu64 ", "
4329  "p_frame->start_of_stream=%u, p_frame->end_of_stream=%u, "
4330  "p_frame->video_width=%u, p_frame->video_height=%u, pts=0x%08x 0x%08x, "
4331  "dts=0x%08x 0x%08x, sei_len=%u, roi size=%u avg_qp=%u reconf_len=%u "
4332  "force_pic_qp=%u use_cur_src_as_long_term_pic %u use_long_term_ref "
4333  "%u start_len [%u,%u,%u] inconsecutive_transfer %u "
4334  "get_recon_frame_mode %u\n",
4335  __func__, p_ctx->hw_id, p_ctx->session_id, p_ctx->frame_num,
4336  p_frame->start_of_stream, p_frame->end_of_stream, p_frame->video_width,
4337  p_frame->video_height, (uint32_t)((p_frame->pts >> 32) & 0xFFFFFFFF),
4338  (uint32_t)(p_frame->pts & 0xFFFFFFFF),
4339  (uint32_t)((p_frame->dts >> 32) & 0xFFFFFFFF),
4340  (uint32_t)(p_frame->dts & 0xFFFFFFFF), p_meta->frame_sei_data_size,
4341  p_meta->frame_roi_map_size, p_meta->frame_roi_avg_qp,
4342  p_meta->enc_reconfig_data_size, p_meta->force_pic_qp_i,
4344  p_meta->start_len[0], p_meta->start_len[1], p_meta->start_len[2],
4345  p_meta->inconsecutive_transfer,
4346  p_meta->get_recon_frame_mode);
4347  }
4348  if (p_frame->start_of_stream)
4349  {
4350  //Send Start of stream p_config command here
4352  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
4353  p_ctx->device_type, p_ctx->hw_id,
4354  &(p_ctx->session_id), OPT_1);
4355  CHECK_VPU_RECOVERY(retval);
4356  if (NI_RETCODE_SUCCESS != retval)
4357  {
4358  LRETURN;
4359  }
4360 
4361  p_frame->start_of_stream = 0;
4362  }
4363 
4364  // skip direct to send eos without sending the passed in p_frame as it's been sent already
4365  if (p_frame->end_of_stream)
4366  {
4368  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
4369  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4370  CHECK_VPU_RECOVERY(retval);
4371  if (NI_RETCODE_SUCCESS != retval)
4372  {
4373  LRETURN;
4374  }
4375 
4376  p_frame->end_of_stream = 0;
4377  p_ctx->ready_to_close = 1;
4378  } else //handle regular frame sending
4379  {
4380  retval = ni_timestamp_register(p_ctx->buffer_pool, p_ctx->dts_queue,
4381  p_frame->dts, 0);
4382  if (NI_RETCODE_SUCCESS != retval)
4383  {
4384  ni_log2(p_ctx, NI_LOG_ERROR,
4385  "ERROR %s(): "
4386  "ni_timestamp_register() for dts returned: %d\n",
4387  __func__, retval);
4388  }
4389 
4390  if (separate_metadata)
4391  {
4392  uint32_t ui32LBA_metadata =
4394  ni_log2(p_ctx, NI_LOG_DEBUG,
4395  "%s: p_metadata_buffer = %p, metadata_buffer_size "
4396  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4397  __func__, p_frame->p_metadata_buffer,
4398  p_frame->metadata_buffer_size, p_ctx->frame_num,
4399  ui32LBA_metadata);
4400 
4401  sent_size =
4403 
4404  retval = ni_nvme_send_write_cmd(
4405  p_ctx->blk_io_handle, p_ctx->event_handle,
4406  p_frame->p_metadata_buffer, sent_size,
4407  ui32LBA_metadata);
4408  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4409  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4410  CHECK_VPU_RECOVERY(retval);
4411  if (retval < 0)
4412  {
4413  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n",
4414  __func__);
4416  LRETURN;
4417  }
4418  }
4419 
4420  if (separate_start)
4421  {
4422  uint32_t ui32LBA =
4424  ni_log2(p_ctx, NI_LOG_DEBUG,
4425  "%s: p_start_buffer = %p, p_frame->start_buffer_size "
4426  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4427  __func__, p_frame->p_start_buffer, p_frame->start_buffer_size, p_ctx->frame_num,
4428  ui32LBA);
4429 
4430  sent_size =
4432 
4433  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4434  p_frame->p_start_buffer, sent_size, ui32LBA);
4435  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4436  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4437  CHECK_VPU_RECOVERY(retval);
4438  if (retval < 0)
4439  {
4440  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4442  LRETURN;
4443  }
4444  }
4445 
4446  if (p_frame->inconsecutive_transfer)
4447  {
4448  uint32_t ui32LBA =
4450 
4451  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
4452  {
4453  ni_log2(p_ctx, NI_LOG_DEBUG,
4454  "%s: p_data = %p, p_frame->buffer_size "
4455  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4456  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
4457  ui32LBA);
4458 
4459  if (p_frame->data_len[i])
4460  {
4461  sent_size = p_frame->data_len[i];
4462  if (separate_start)
4463  sent_size -= p_frame->start_len[i];
4464 
4465  sent_size =
4467 
4468  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4469  p_frame->p_data[i]+p_frame->start_len[i], sent_size, ui32LBA);
4470  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4471  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4472  CHECK_VPU_RECOVERY(retval);
4473  if (retval < 0)
4474  {
4475  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4477  LRETURN;
4478  }
4479  }
4480  }
4481  }
4482  else
4483  {
4484  uint32_t ui32LBA =
4486  ni_log2(p_ctx, NI_LOG_DEBUG,
4487  "%s: p_data = %p, p_frame->buffer_size "
4488  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4489  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
4490  ui32LBA);
4491 
4492  sent_size = frame_size_bytes;
4493  if (separate_metadata)
4494  sent_size -= p_frame->extra_data_len;
4495  if (separate_start)
4496  sent_size -= p_frame->total_start_len;
4497 
4498  sent_size =
4500 
4501  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4502  p_frame->p_buffer+p_frame->total_start_len, sent_size, ui32LBA);
4503  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4504  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4505  CHECK_VPU_RECOVERY(retval);
4506  if (retval < 0)
4507  {
4508  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4510  LRETURN;
4511  }
4512  }
4513 
4514  //Save input frame data used for calculate PSNR
4515  if ((ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rc") >= 0) &&
4516  (p_ctx->frame_num == 0 || ((p_ctx->frame_num % ((ni_xcoder_params_t *)(p_ctx->p_session_config))->interval_of_psnr) == 0)) &&
4517  (((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode < 3) &&
4518  (!(((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode == 2 && p_ctx->codec_format == NI_CODEC_FORMAT_H265)))
4519  {
4520  if (ishwframe && !p_frame->end_of_stream)
4521  {
4522  // uint16_t input_frame_idx = ((niFrameSurface1_t*)(p_frame->p_data[3]))->ui16FrameIdx;
4523  // use hwdownload to download hw frame, but donot recycle hwframe buffer
4524  ni_session_data_io_t hwdl_session_data = {0};
4525  // ni_log2(p_ctx, NI_LOG_ERROR, "pixel_format %d\n", p_ctx->pixel_format);
4526  int ret = hwdl_frame(p_ctx, &hwdl_session_data, p_frame,
4527  p_frame->pixel_format);
4528  if (ret <= 0)
4529  {
4530  ni_frame_buffer_free(&(hwdl_session_data.data.frame));
4531  return ret;
4532  }
4533 
4534  hwdl_session_data.data.frame.pts = p_frame->pts;
4535  enqueue_ni_frame(p_ctx, &hwdl_session_data.data.frame, p_frame->video_width, p_frame->video_height);
4536  ni_frame_buffer_free(&hwdl_session_data.data.frame);
4537  }
4538  else
4539  {
4540  enqueue_ni_frame(p_ctx, p_frame, p_frame->video_width, p_frame->video_height);
4541  }
4542  }
4543 
4544  if (ishwframe)
4545  {
4546  ni_log2(p_ctx, NI_LOG_DEBUG,
4547  "%s(): session=0x%x ui16FrameIdx=%u\n",
4548  __func__,
4549  p_ctx->session_id,
4550  ((niFrameSurface1_t*)(p_frame->p_data[3]))->ui16FrameIdx);
4551  }
4552 
4553  p_ctx->status = 0;
4554  p_ctx->frame_num++;
4555  size = frame_size_bytes;
4556  p_ctx->low_delay_sync_flag = 1;
4557 
4558 #ifdef XCODER_DUMP_DATA
4559  char dump_file[256];
4560  snprintf(dump_file, sizeof(dump_file), "%ld-%u-enc-fme/fme-%04ld.yuv",
4561  (long)getpid(), p_ctx->session_id, (long)p_ctx->frame_num);
4562 
4563  FILE *f = fopen(dump_file, "wb");
4564  fwrite(p_frame->p_buffer,
4565  p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2],
4566  1, f);
4567  fflush(f);
4568  fclose(f);
4569 #endif
4570  }
4571 
4572  retval = size;
4573 
4574 END:
4575 
4576  ni_pthread_mutex_unlock(&p_ctx->mutex);
4577 
4578  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
4579  return retval;
4580 }
4581 
4582 
4584 {
4585  ni_instance_mgr_stream_info_t data = { 0 };
4586  uint32_t actual_read_size = 0;
4587  uint32_t to_read_size = 0;
4588  int size = 0;
4589  static long long encq_count = 0LL;
4590  int retval = NI_RETCODE_SUCCESS;
4591  int query_retry = 0;
4592  int query_type = INST_BUF_INFO_RW_READ;
4593  uint32_t ui32LBA = 0;
4594  ni_metadata_enc_bstream_t *p_meta = NULL;
4595  ni_metadata_enc_bstream_rev61_t *p_meta_rev61 = NULL;
4596  ni_instance_buf_info_t buf_info = { 0 };
4597  ni_session_statistic_t sessionStatistic = {0};
4598  int low_delay_notify = 0;
4599  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
4600 
4601  if (!p_ctx || !p_packet || !p_packet->p_data)
4602  {
4603  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
4604  __func__);
4605  retval = NI_RETCODE_INVALID_PARAM;
4606  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
4607 
4608  return retval;
4609  }
4610 
4611  ni_pthread_mutex_lock(&p_ctx->mutex);
4612 
4613  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
4614  {
4615  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
4616  __func__);
4618  LRETURN;
4619  }
4620 
4621  ni_log2(p_ctx, NI_LOG_DEBUG, "frame_num=%" PRIu64 ", pkt_num=%" PRIu64 ", av1_pkt_num=%" PRIu64 "\n",
4622  p_ctx->frame_num, p_ctx->pkt_num, p_ctx->av1_pkt_num);
4623  if (((ni_xcoder_params_t *)p_ctx->p_session_config)->low_delay_mode)
4624  {
4625  query_type = INST_BUF_INFO_RW_READ_BUSY;
4626  if (!p_packet->end_of_stream && p_ctx->frame_num < p_ctx->pkt_num)
4627  {
4628  if (p_ctx->ready_to_close)
4629  {
4630  query_type = INST_BUF_INFO_RW_READ;
4631  } else
4632  { //nothing to query, leave
4633  retval = NI_RETCODE_SUCCESS;
4634  low_delay_notify = 1;
4635  LRETURN;
4636  }
4637  }
4638  }
4639  for (;;)
4640  {
4641  query_sleep(p_ctx);
4642 
4643  query_retry++;
4644 
4646  "65") >= 0)
4647  {
4649  p_ctx, NI_DEVICE_TYPE_ENCODER, &sessionStatistic);
4650  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
4652  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
4653  CHECK_VPU_RECOVERY(retval);
4654 
4655  buf_info.buf_avail_size = sessionStatistic.ui32RdBufAvailSize;
4656 
4657  if (((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.lookAheadDepth)
4658  {
4660  "6rX") >= 0)
4661  {
4662  if (p_ctx->current_frame_delay < (int)sessionStatistic.ui8AdditionalFramesDelay + p_ctx->initial_frame_delay)
4663  {
4664  p_ctx->current_frame_delay = (int)sessionStatistic.ui8AdditionalFramesDelay + p_ctx->initial_frame_delay; // extend frames delay by FW estimated additional number of frames
4665  }
4666  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: initial_frame_delay %d max_frame_delay %d ui8AdditionalFramesDelay %u current_frame_delay %d\n",
4667  __FUNCTION__, p_ctx->initial_frame_delay, p_ctx->max_frame_delay, sessionStatistic.ui8AdditionalFramesDelay, p_ctx->current_frame_delay);
4668  }
4669  }
4670  } else
4671  {
4672  retval = ni_query_instance_buf_info(
4673  p_ctx, query_type, NI_DEVICE_TYPE_ENCODER, &buf_info);
4674  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4675  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4676  CHECK_VPU_RECOVERY(retval);
4677  }
4678 
4679  ni_log2(p_ctx, NI_LOG_TRACE,
4680  "Info enc read query rc %d, available buf size %u, "
4681  "frame_num=%" PRIu64 ", pkt_num=%" PRIu64 "\n",
4682  retval, buf_info.buf_avail_size, p_ctx->frame_num, p_ctx->pkt_num);
4683 
4684  if (NI_RETCODE_SUCCESS != retval)
4685  {
4686  ni_log2(p_ctx, NI_LOG_ERROR, "Buffer info query failed in encoder read!!!!\n");
4687  LRETURN;
4688  } else if (0 == buf_info.buf_avail_size)
4689  {
4690  // query to see if it is eos now, if we have sent it
4691  if (p_ctx->ready_to_close)
4692  {
4693  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_ENCODER, &data);
4694  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4695  p_ctx->device_type, p_ctx->hw_id,
4696  &(p_ctx->session_id), OPT_1);
4697  CHECK_VPU_RECOVERY(retval);
4698 
4699  if (NI_RETCODE_SUCCESS != retval)
4700  {
4701  ni_log2(p_ctx, NI_LOG_ERROR, "Stream info query failed in encoder read!!!!\n");
4702  LRETURN;
4703  }
4704 
4705  if (data.is_flushed)
4706  {
4707  p_packet->end_of_stream = 1;
4708  }
4709  }
4710  ni_log2(p_ctx, NI_LOG_DEBUG, "Info enc read available buf size %u, eos %u !\n",
4711  buf_info.buf_avail_size, p_packet->end_of_stream);
4712 
4713  if ((((ni_xcoder_params_t *)p_ctx->p_session_config)->low_delay_mode ||
4715  !p_packet->end_of_stream && p_ctx->frame_num >= p_ctx->pkt_num)
4716  {
4717  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder low latency mode, eos not sent, frame_num "
4718  "%" PRIu64 " >= %" PRIu64 " pkt_num, keep querying p_ctx->status %d\n",
4719  p_ctx->frame_num, p_ctx->pkt_num, p_ctx->status);
4720  ni_pthread_mutex_unlock(&p_ctx->mutex);
4722  ni_pthread_mutex_lock(&p_ctx->mutex);
4723  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES &&
4725  {
4726  low_delay_notify = 1;
4728  LRETURN;
4729  }
4730  else
4731  {
4732  continue;
4733  }
4734  }
4735  else if (((ni_xcoder_params_t *)p_ctx->p_session_config)->minFramesDelay)
4736  {
4737  if (p_ctx->pkt_num) // do not busy read until header is received
4738  {
4739  uint64_t pkt_num = (p_ctx->codec_format == NI_CODEC_FORMAT_AV1) ? p_ctx->av1_pkt_num : p_ctx->pkt_num - 1; // deduct header from pkt_num
4740  if (p_ctx->frame_num - pkt_num >= p_ctx->current_frame_delay &&
4741  !p_packet->end_of_stream)
4742  {
4743  ni_log2(p_ctx, NI_LOG_TRACE, "%s: low delay mode 2, keep reading send frame %d receive pkt %d gop %d current_frame_delay %d\n",
4744  __FUNCTION__, p_ctx->frame_num, pkt_num,
4745  p_ctx->last_gop_size, p_ctx->current_frame_delay);
4746  continue;
4747  }
4748  }
4749  }
4750  retval = NI_RETCODE_SUCCESS;
4751  LRETURN;
4752  } else
4753  {
4754  break;
4755  }
4756  }
4757  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder read buf_avail_size %u\n", buf_info.buf_avail_size);
4758 
4759  to_read_size = buf_info.buf_avail_size;
4760 
4761  p_packet->data_len = 0;
4762  p_packet->pts = NI_NOPTS_VALUE;
4763  p_packet->dts = 0;
4764 
4765  p_packet->psnr_y = 0;
4766  p_packet->psnr_u = 0;
4767  p_packet->psnr_v = 0;
4768  p_packet->average_psnr = 0;
4769  p_packet->ssim_y = 0;
4770  p_packet->ssim_u = 0;
4771  p_packet->ssim_v = 0;
4772 
4774  actual_read_size = to_read_size;
4775  if (actual_read_size % NI_MEM_PAGE_ALIGNMENT)
4776  {
4777  actual_read_size =
4778  ((actual_read_size / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) +
4780  }
4781 
4782  if (p_packet->buffer_size < actual_read_size)
4783  {
4784  if (ni_packet_buffer_alloc(p_packet, actual_read_size))
4785  {
4786  ni_log2(p_ctx, NI_LOG_ERROR,
4787  "ERROR %s(): packet buffer size %u allocation "
4788  "failed\n",
4789  __func__, actual_read_size);
4790  retval = NI_RETCODE_ERROR_MEM_ALOC;
4791  LRETURN;
4792  }
4793  }
4794 
4795  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4796  p_packet->p_data, actual_read_size, ui32LBA);
4797  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
4798  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4799  CHECK_VPU_RECOVERY(retval);
4800  if (retval < 0)
4801  {
4802  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4804  LRETURN;
4805  }
4806 
4807  // SSIM is supported if fw_rev is >= 6.2
4809  "62") >= 0)
4810  {
4811  p_meta = (ni_metadata_enc_bstream_t *)p_packet->p_data;
4812  p_packet->pts = (int64_t)(p_meta->frame_tstamp);
4813  p_packet->frame_type = p_meta->frame_type;
4814  p_packet->avg_frame_qp = p_meta->avg_frame_qp;
4815  p_packet->recycle_index = p_meta->recycle_index;
4816  p_packet->av1_show_frame = p_meta->av1_show_frame;
4817  ni_log2(p_ctx, NI_LOG_DEBUG, "%s RECYCLE INDEX = %u!!!\n", __FUNCTION__, p_meta->recycle_index);
4818  ni_log2(p_ctx, NI_LOG_DEBUG, "%s MetaDataSize %d FrameType %d AvgFrameQp %d ssim %d %d %d\n",
4819  __FUNCTION__, p_meta->metadata_size, p_meta->frame_type, p_meta->avg_frame_qp, p_meta->ssimY, p_meta->ssimU, p_meta->ssimV);
4820 
4822  "6r2") >= 0)
4823  {
4824  if (((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.lookAheadDepth)
4825  {
4827  "6rX") < 0)
4828  {
4829  if (p_meta->gop_size) // ignore frame 0 gop size 0 (other I-frame gop size 1)
4830  {
4831  if ((int)p_meta->gop_size < p_ctx->last_gop_size)
4832  {
4833  p_ctx->current_frame_delay = p_ctx->max_frame_delay; // shortening gop (including I-frame) causes lookahead queue increase, currently assume worst case frame delay
4834  }
4835  }
4836  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: current gop_size %u last_gop_size %d initial_frame_delay %d max_frame_delay %d current_frame_delay %d\n",
4837  __FUNCTION__, p_meta->gop_size, p_ctx->last_gop_size, p_ctx->initial_frame_delay, p_ctx->max_frame_delay, p_ctx->current_frame_delay);
4838  }
4839  p_ctx->last_gop_size = p_meta->gop_size;
4840  }
4841  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: current gop_size %u\n",
4842  __FUNCTION__, p_meta->gop_size);
4843  }
4844 
4846  "6p") >= 0)
4847  {
4848  if (ni_cmp_fw_api_ver((char*)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rm") >= 0)
4849  {
4850  p_packet->still_image_detected = p_meta->ui8StillImage;
4851  p_packet->scene_change_detected = p_meta->ui8SceneChange;
4852  }
4853  else
4854  {
4855  p_packet->still_image_detected = 0;
4856  p_packet->scene_change_detected = 0;
4857  }
4858 
4859  if(p_meta->frame_size > 0)
4860  ni_log2(p_ctx, NI_LOG_DEBUG, "pkt_num %d max_mv x[0] %d x[1] %d y[0] %d y[1] %d min_mv x[0] %d x[1] %d y[0] %d y[1] %d frame_size %u inter_total_count %u intra_total_count %u stillImage %d scenechange %d\n",
4861  p_ctx->pkt_num,
4862  p_meta->max_mv_x[0], p_meta->max_mv_x[1], p_meta->max_mv_y[0], p_meta->max_mv_y[1],
4863  p_meta->min_mv_x[0], p_meta->min_mv_x[1], p_meta->min_mv_y[0], p_meta->min_mv_y[1],
4864  p_meta->frame_size, p_meta->inter_total_count, p_meta->intra_total_count,
4865  p_meta->ui8StillImage, p_meta->ui8SceneChange);
4866  }
4867 
4868  p_ctx->meta_size = p_meta->metadata_size;
4869 
4870  if (p_meta->ssimY != 0)
4871  {
4872  p_packet->ssim_y = (float)p_meta->ssimY/10000;
4873  p_packet->ssim_u = (float)p_meta->ssimU/10000;
4874  p_packet->ssim_v = (float)p_meta->ssimV/10000;
4875  // The SSIM Y, U, V values returned by FW are 4 decimal places multiplied by 10000.
4876  //Divide by 10000 to get the original value.
4877  ni_log2(p_ctx,
4878 #ifdef NI_LOG_SSIM_AT_INFO
4879  NI_LOG_INFO,
4880 #else
4881  NI_LOG_DEBUG,
4882 #endif
4883  "%s: pkt #%" PRId64 " pts %" PRId64 " ssim "
4884  "Y %.4f U %.4f V %.4f\n", __FUNCTION__, p_ctx->pkt_num,
4885  p_packet->pts, (float)p_meta->ssimY/10000,
4886  (float)p_meta->ssimU/10000, (float)p_meta->ssimV/10000);
4887  }
4888  }
4889  else
4890  {
4891  // Up to fw_rev major 6 and minor 1, use the old meta data structure
4892  p_meta_rev61 = (ni_metadata_enc_bstream_rev61_t *)p_packet->p_data;
4893  p_packet->pts = (int64_t)(p_meta_rev61->frame_tstamp);
4894  p_packet->frame_type = p_meta_rev61->frame_type;
4895  p_packet->avg_frame_qp = p_meta_rev61->avg_frame_qp;
4896  p_packet->recycle_index = p_meta_rev61->recycle_index;
4897  p_packet->av1_show_frame = p_meta_rev61->av1_show_frame;
4898  ni_log2(p_ctx, NI_LOG_DEBUG, "%s RECYCLE INDEX = %u!!!\n", __FUNCTION__, p_meta_rev61->recycle_index);
4899 
4900  }
4901 
4902  p_packet->data_len = to_read_size;
4903 
4904  size = p_packet->data_len;
4905 
4906  if (size > 0)
4907  {
4908  if (p_ctx->pkt_num >= 1 &&
4910  "6rc") >= 0)
4911  {
4912  calculate_psnr(p_ctx, p_packet);
4913  }
4914 
4915  if (p_ctx->pkt_num >= 1)
4916  {
4917  if (NI_CODEC_FORMAT_AV1 != p_ctx->codec_format
4918  || p_packet->av1_show_frame)
4919  {
4920  if (ni_timestamp_get_with_threshold(p_ctx->dts_queue, 0, (int64_t*)& p_packet->dts, 0, encq_count % 500 == 0, p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
4921  {
4922  p_packet->dts = NI_NOPTS_VALUE;
4923  }
4924  }
4925 
4926  p_ctx->pkt_num++;
4927  if (p_ctx->codec_format == NI_CODEC_FORMAT_AV1 && p_packet->recycle_index != (uint32_t) NI_AV1_INVALID_BUFFER_INDEX) // av1 invalid buffer index indicates show_existing_frame header packet
4928  {
4929  p_ctx->av1_pkt_num++;
4930  }
4931  low_delay_notify = 1;
4932  }
4933 
4934  encq_count++;
4935 
4936 #ifdef MEASURE_LATENCY
4937 #ifndef XCODER_311
4938  ni_log2(p_ctx, NI_LOG_INFO, "ENC fme_num %d, pkt_num %d, latency is %d\n",
4939  p_ctx->frame_num, p_ctx->pkt_num, p_ctx->frame_num - p_ctx->pkt_num);
4940 #endif
4941 #endif
4942 
4943 #ifdef XCODER_DUMP_DATA
4944  char dump_file[256];
4945  snprintf(dump_file, sizeof(dump_file), "%ld-%u-enc-pkt/pkt-%04ld.bin",
4946  (long)getpid(), p_ctx->session_id, (long)p_ctx->pkt_num);
4947 
4948  FILE *f = fopen(dump_file, "wb");
4949  fwrite((uint8_t *)p_packet->p_data + sizeof(ni_metadata_enc_bstream_t),
4950  p_packet->data_len - sizeof(ni_metadata_enc_bstream_t), 1, f);
4951  fflush(f);
4952  fclose(f);
4953 #endif
4954  }
4955 
4956  ni_log2(p_ctx,
4957  NI_LOG_DEBUG,
4958  "%s(): %d.%u p_packet->start_of_stream=%u, "
4959  "p_packet->end_of_stream=%u, p_packet->video_width=%u, "
4960  "p_packet->video_height=%u, p_packet->dts=0x%08x 0x%08x, "
4961  "p_packet->pts=0x%08x 0x%08x, type=%u, avg_frame_qp=%u, show_frame=%d\n",
4962  __func__, p_ctx->hw_id, p_ctx->session_id, p_packet->start_of_stream,
4963  p_packet->end_of_stream, p_packet->video_width, p_packet->video_height,
4964  (uint32_t)((p_packet->dts >> 32) & 0xFFFFFFFF),
4965  (uint32_t)(p_packet->dts & 0xFFFFFFFF),
4966  (uint32_t)((p_packet->pts >> 32) & 0xFFFFFFFF),
4967  (uint32_t)(p_packet->pts & 0xFFFFFFFF), p_packet->frame_type,
4968  p_packet->avg_frame_qp, p_packet->av1_show_frame);
4969 
4970  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): p_packet->data_len=%u, size=%d\n", __func__,
4971  p_packet->data_len, size);
4972 
4973  if (encq_count % 500 == 0)
4974  {
4975  ni_log2(p_ctx, NI_LOG_DEBUG,
4976  "Encoder pts queue size = %u dts queue size = %u\n\n",
4977  p_ctx->pts_table->list.count, p_ctx->dts_queue->list.count);
4978  }
4979 
4980  retval = size;
4981 
4982 #ifdef MEASURE_LATENCY
4983  if ((p_packet->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL) && (p_ctx->pkt_num > 0))
4984  {
4985  if (NI_CODEC_FORMAT_AV1 != p_ctx->codec_format
4986  || p_packet->av1_show_frame)
4987  {
4988  uint64_t abs_time_ns = ni_gettime_ns();
4990  ni_log2(p_ctx, NI_LOG_INFO, "DTS:%" PRId64 ",DELTA:%" PRId64 ",eLAT:%" PRIu64 ";\n",
4991  p_packet->dts, abs_time_ns - q->last_benchmark_time,
4992  ni_lat_meas_q_check_latency(q, abs_time_ns, p_packet->dts));
4993  q->last_benchmark_time = abs_time_ns;
4994  }
4995  }
4996 #endif
4997 
4998 END:
4999 
5000  ni_pthread_mutex_unlock(&p_ctx->mutex);
5001 
5002  if (low_delay_notify)
5003  {
5004  low_delay_signal(p_ctx);
5005  }
5006 
5007  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
5008 
5009  return retval;
5010 }
5011 
5012 /*!******************************************************************************
5013  * \brief Send sequnce change to a xcoder encoder instance
5014  *
5015  * \param
5016  *
5017  * \return
5018  *******************************************************************************/
5020 {
5022 
5023  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
5024 
5025  // re-init last_gop_size and av1_pkt_num for minFramesDelay frame delay estimiation
5026  int lookAheadEnable = !!(((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.lookAheadDepth);
5027  int gop_preset_index = ((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.gop_preset_index;
5028  p_ctx->last_gop_size = g_map_preset_to_gopsize[lookAheadEnable][gop_preset_index + 1];
5029  p_ctx->av1_pkt_num = 0;
5030 
5031  //Configure encoder sequence change
5033  p_resolution);
5035  p_ctx->device_type, p_ctx->hw_id,
5036  &(p_ctx->session_id), OPT_1);
5037  CHECK_VPU_RECOVERY(retval);
5038  if (retval < 0)
5039  {
5040  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): config encoder sequence change command failed\n",
5041  __func__);
5043  }
5044 
5045 END:
5046 
5047  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
5048 
5049  return retval;
5050 }
5051 
5052 /*!******************************************************************************
5053  * \brief Open a xcoder scaler instance
5054  *
5055  * \param[in] p_ctx pointer to session context
5056  *
5057  * \return NI_RETCODE_INVALID_PARAM
5058  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5059  * NI_RETCODE_ERROR_INVALID_SESSION
5060  * NI_RETCODE_ERROR_MEM_ALOC
5061  *******************************************************************************/
5063 {
5065  void* p_buffer = NULL;
5066  uint32_t ui32LBA = 0;
5067  char fmt_fw_api_ver1[5], fmt_fw_api_ver2[5];
5068 
5069  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
5070 
5071  if (!p_ctx)
5072  {
5073  retval = NI_RETCODE_INVALID_PARAM;
5074  LRETURN;
5075  }
5076 
5078  {
5080  "64") < 0)
5081  {
5082  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot use stack filter on device with FW API version < 6.4\n");
5084  }
5085  }
5086 
5088  {
5090  "67") < 0)
5091  {
5092  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot use rotate filter on device with FW API version < 6.7\n");
5094  }
5095  }
5096 
5098  {
5100  "6L") < 0)
5101  {
5102  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot use in-place overlay filter on device with FW API version < 6.L\n");
5104  }
5105  }
5106 
5107  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5108  {
5110  p_ctx->pts_table = NULL;
5111  p_ctx->dts_queue = NULL;
5112  p_ctx->p_leftover = NULL;
5113  p_ctx->buffer_pool = NULL;
5114  p_ctx->dec_fme_buf_pool = NULL;
5115  p_ctx->prev_size = 0;
5116  p_ctx->sent_size = 0;
5117  p_ctx->status = 0;
5118  p_ctx->key_frame_type = 0;
5119  p_ctx->ready_to_close = 0;
5120  p_ctx->rc_error_count = 0;
5121  p_ctx->frame_num = 0;
5122  p_ctx->pkt_num = 0;
5123  p_ctx->pkt_index = 0;
5124 
5125  //malloc zero data buffer
5126  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
5128  {
5129  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
5130  NI_ERRNO, __func__);
5131  retval = NI_RETCODE_ERROR_MEM_ALOC;
5132  LRETURN;
5133  }
5134  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
5135 
5136  //malloc data buffer
5137  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
5138  {
5139  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
5140  NI_ERRNO, __func__);
5141  retval = NI_RETCODE_ERROR_MEM_ALOC;
5142  LRETURN;
5143  }
5144  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5145 
5146  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
5147  //In case we can open sesison, the session id would become valid.
5148  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
5149  (uint16_t)NI_INVALID_SESSION_ID;
5150 
5151  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
5152  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_SCALER, ni_htonl(p_ctx->scaler_operation), 0);
5153  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5154  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
5155  if (retval != NI_RETCODE_SUCCESS)
5156  {
5157  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
5158  LRETURN;
5159  }
5160  //Open will return a session status structure with a valid session id if it worked.
5161  //Otherwise the invalid session id set before the open command will stay
5162  p_ctx->session_id = ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
5163  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
5164  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
5165  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
5166  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
5167  {
5168  ni_log2(p_ctx, NI_LOG_ERROR,
5169  "ERROR %s(): p_ctx->device_handle=%" PRIx64
5170  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
5171  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
5172  p_ctx->session_id);
5174  LRETURN;
5175  }
5176  ni_log2(p_ctx, NI_LOG_DEBUG, "Scaler open session ID:0x%x\n",p_ctx->session_id);
5177  ni_log2(p_ctx, NI_LOG_DEBUG,
5178  "%s(): p_ctx->device_handle=%" PRIx64
5179  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
5180  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
5181  p_ctx->session_id);
5182 
5183  //Send keep alive timeout Info
5184  uint64_t keep_alive_timeout =
5185  p_ctx->keep_alive_timeout * 1000000; //send us to FW
5186  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5187  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
5188  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
5189  keep_alive_timeout);
5191  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5192  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
5193  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
5194  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5195  CHECK_VPU_RECOVERY(retval);
5196 
5197  if (NI_RETCODE_SUCCESS != retval)
5198  {
5199  ni_log2(p_ctx, NI_LOG_ERROR,
5200  "ERROR %s(): nvme write keep_alive_timeout command "
5201  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
5202  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
5204  LRETURN;
5205  }
5206 
5207  // Send SW version to FW if FW API version is >= 6.2
5209  "62") >= 0)
5210  {
5211  // Send SW version to session manager
5212  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5213  memcpy(p_buffer, NI_XCODER_REVISION, sizeof(uint64_t));
5215  ni_fmt_fw_api_ver_str((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], &fmt_fw_api_ver2[0]);
5216  ni_log2(p_ctx, NI_LOG_DEBUG, "%s libxcoder FW API ver %s, FW FW API ver %s\n",
5217  __func__, fmt_fw_api_ver1, fmt_fw_api_ver2);
5218  ui32LBA = CONFIG_SESSION_SWVersion_W(p_ctx->session_id);
5219  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5220  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
5221  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
5222  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5223  CHECK_VPU_RECOVERY(retval);
5224 
5225  if (NI_RETCODE_SUCCESS != retval)
5226  {
5227  ni_log2(p_ctx, NI_LOG_ERROR,
5228  "ERROR %s(): nvme write sw_version command "
5229  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
5230  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
5232  LRETURN;
5233  }
5234  }
5235  }
5236 
5237  // init for frame pts calculation
5238  p_ctx->is_first_frame = 1;
5239  p_ctx->last_pts = 0;
5240  p_ctx->last_dts = 0;
5241  p_ctx->active_video_width = 0;
5242  p_ctx->active_video_height = 0;
5243  p_ctx->actual_video_width = 0;
5244 
5245 #ifndef _WIN32
5246  if (p_ctx->isP2P)
5247  {
5248  retval = p2p_fill_pcie_address(p_ctx);
5249  if(retval != NI_RETCODE_SUCCESS)
5250  {
5251  LRETURN;
5252  }
5253  }
5254 #endif
5255 
5256 END:
5257 
5258  ni_aligned_free(p_buffer);
5259  return retval;
5260 }
5261 
5262 /*!******************************************************************************
5263  * \brief close a scaler session
5264  *
5265  * \param[in] p_ctx pointer to session context
5266  * \param[in] eos_received (not used)
5267  *
5268  * \return NI_RETCODE_INVALID_PARAM
5269  * NI_RETCODE_ERROR_INVALID_SESSION
5270  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5271  *******************************************************************************/
5273 {
5275  void * p_buffer = NULL;
5276  uint32_t ui32LBA = 0;
5277 
5278  if (!p_ctx)
5279  {
5280  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
5281  __func__);
5282  return NI_RETCODE_INVALID_PARAM;
5283  }
5284 
5285  ni_pthread_mutex_lock(&p_ctx->mutex);
5286 
5287  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5288  {
5289  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
5290  retval = NI_RETCODE_SUCCESS;
5291  LRETURN;
5292  }
5293 
5294  //malloc data buffer
5295  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
5296  {
5297  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() malloc data buffer failed\n",
5298  NI_ERRNO, __func__);
5299  retval = NI_RETCODE_ERROR_MEM_ALOC;
5300  LRETURN;
5301  }
5302  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5303 
5305 
5306  int retry = 0;
5307  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
5308  {
5309  ni_log2(p_ctx, NI_LOG_DEBUG,
5310  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
5311  "p_ctx->session_id=%d, close_mode=1\n",
5312  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5313  p_ctx->session_id);
5314 
5316  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
5317  {
5318  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed!\n", __func__);
5321  break;
5322  } else
5323  {
5324  //Close should always succeed
5325  retval = NI_RETCODE_SUCCESS;
5327  break;
5328  }
5329  retry++;
5330  }
5331 
5332 #ifndef _WIN32
5333  if (p_ctx->isP2P)
5334  {
5335  if (p_ctx->netint_fd)
5336  {
5337  close(p_ctx->netint_fd);
5338  }
5339  }
5340 #endif
5341 
5342 END:
5343 
5344  ni_aligned_free(p_buffer);
5346 
5347  ni_pthread_mutex_unlock(&p_ctx->mutex);
5348 
5349  return retval;
5350 }
5351 
5352 /*!******************************************************************************
5353  * \brief Send a p_config command to configure scaling parameters.
5354  *
5355  * \param ni_session_context_t p_ctx - xcoder Context
5356  * \param ni_scaler_params_t * params - pointer to the scaler ni_scaler_params_t struct
5357  *
5358  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
5359  *******************************************************************************/
5361  ni_scaler_params_t *p_params)
5362 {
5363  void *p_scaler_config = NULL;
5364  ni_scaler_config_t *p_cfg = NULL;
5365  uint32_t buffer_size = sizeof(ni_scaler_params_t);
5367  uint32_t ui32LBA = 0;
5368 
5369  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
5370 
5371  if (!p_ctx || !p_params)
5372  {
5373  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
5374  __func__);
5375  retval = NI_RETCODE_INVALID_PARAM;
5376  LRETURN;
5377  }
5378 
5379  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
5380  {
5381  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5382  __func__);
5384  LRETURN;
5385  }
5386 
5387  buffer_size =
5388  ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) *
5390  if (ni_posix_memalign(&p_scaler_config, sysconf(_SC_PAGESIZE), buffer_size))
5391  {
5392  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() malloc p_scaler_config buffer failed\n",
5393  NI_ERRNO, __func__);
5394  retval = NI_RETCODE_ERROR_MEM_ALOC;
5395  LRETURN;
5396  }
5397  memset(p_scaler_config, 0, buffer_size);
5398 
5399  //configure the session here
5402 
5403  //Flip the bytes!!
5404  p_cfg = (ni_scaler_config_t *)p_scaler_config;
5405  p_cfg->filterblit = p_params->filterblit;
5406  p_cfg->numInputs = p_params->nb_inputs;
5407  p_cfg->scaler_param_b = (uint16_t)(0);
5408  p_cfg->scaler_param_c = (uint16_t)(0.75 * 10000);
5409 
5410  if (p_params->enable_scaler_params)
5411  {
5412  // check fw revision
5413  if (ni_cmp_fw_api_ver(
5415  "6s2") < 0)
5416  {
5417  ni_log2(p_ctx, NI_LOG_ERROR, "%s: not supported config scaler params B and C "
5418  "on device with FW API version < 6s1\n", __func__);
5419  // Close the session since we can't configure it as per fw
5420  retval = ni_scaler_session_close(p_ctx, 0);
5421  if (NI_RETCODE_SUCCESS != retval)
5422  {
5423  ni_log2(p_ctx, NI_LOG_ERROR,
5424  "ERROR: %s failed: blk_io_handle: %" PRIx64 ","
5425  "hw_id, %d, xcoder_inst_id: %d\n",
5426  __func__,
5427  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5428  p_ctx->session_id);
5429  }
5430 
5432  LRETURN;
5433  }
5434  if (p_params->scaler_param_b < 0 || p_params->scaler_param_c < 0 ||
5435  p_params->scaler_param_b > 1 || p_params->scaler_param_c > 1)
5436  {
5437  ni_log2(p_ctx, NI_LOG_ERROR, "%s: scaler_params_b and scaler_params_c must "
5438  "be in [0 , 1]. scaler_params_b is %lf, scaler_params_c is %lf\n",
5439  __func__, p_params->scaler_param_b, p_params->scaler_param_c);
5440  // Close the session since we can't configure it as per fw
5441  retval = ni_scaler_session_close(p_ctx, 0);
5442  if (NI_RETCODE_SUCCESS != retval)
5443  {
5444  ni_log2(p_ctx, NI_LOG_ERROR,
5445  "ERROR: %s failed: blk_io_handle: %" PRIx64 ","
5446  "hw_id, %d, xcoder_inst_id: %d\n",
5447  __func__,
5448  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5449  p_ctx->session_id);
5450  }
5451 
5453  LRETURN;
5454  }
5455  p_cfg->scaler_param_b = (uint16_t)(p_params->scaler_param_b * 10000);
5456  p_cfg->scaler_param_c = (uint16_t)(p_params->scaler_param_c * 10000);
5457  }
5458 
5459  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5460  p_scaler_config, buffer_size, ui32LBA);
5461  if ((int32_t)retval < 0)
5462  {
5463  ni_log2(p_ctx, NI_LOG_ERROR,
5464  "ERROR: ni_nvme_send_write_cmd failed: blk_io_handle: %" PRIx64
5465  ", hw_id, %d, xcoder_inst_id: %d\n",
5466  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
5467  // Close the session since we can't configure it as per fw
5468  retval = ni_scaler_session_close(p_ctx, 0);
5469  if (NI_RETCODE_SUCCESS != retval)
5470  {
5471  ni_log2(p_ctx, NI_LOG_ERROR,
5472  "ERROR: %s failed: blk_io_handle: %" PRIx64 ","
5473  "hw_id, %d, xcoder_inst_id: %d\n",
5474  __func__,
5475  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5476  p_ctx->session_id);
5477  }
5478 
5480  LRETURN;
5481  }
5482  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
5483  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5484 
5485 END:
5486 
5487  ni_aligned_free(p_scaler_config);
5488  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
5489 
5490  return retval;
5491 }
5492 
5493 /*!******************************************************************************
5494  * \brief allocate a frame in the scaler
5495  *
5496  * \param[in] p_ctx pointer to session context
5497  * \param[in] width width in pixels
5498  * \param[in] height height in pixels
5499  * \param[in] format pixel format
5500  * \param[in] options option flags
5501  * \param[in] rectangle_width clipping rectangle width in pixels
5502  * \param[in] rectangle_height clipping rectangle height in pixels
5503  * \param[in] rectangle_x clipping rectangle x position
5504  * \param[in] rectangle_y clipping rectangle y position
5505  * \param[in] rgba_color background colour (only used by pad filter)
5506  * \param[in] frame_index frame index (only for hardware frames)
5507  *
5508  * \return NI_RETCODE_INVALID_PARAM
5509  * NI_RETCODE_ERROR_INVALID_SESSION
5510  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5511  * NI_RETCODE_ERROR_MEM_ALOC
5512  *******************************************************************************/
5514  int width,
5515  int height,
5516  int format,
5517  int options,
5518  int rectangle_width,
5519  int rectangle_height,
5520  int rectangle_x,
5521  int rectangle_y,
5522  int rgba_color,
5523  int frame_index)
5524 {
5527  uint32_t dataLen;
5528  uint32_t ui32LBA = 0;
5529  uint32_t query_retry = 0;
5530 
5531  /* Round up to nearest 4096 bytes */
5532  dataLen =
5534  dataLen = dataLen & 0xFFFFF000;
5535 
5536  if (!p_ctx)
5537  {
5538  return NI_RETCODE_INVALID_PARAM;
5539  }
5540 
5541  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5542  {
5543  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5544  __func__);
5546  }
5547 
5548  if (((options & NI_SCALER_FLAG_IO) && (options & NI_SCALER_FLAG_PC)))
5549  {
5550  // this operation is to free/allocate scaler frame pool
5551  if (rgba_color == 0)
5552  {
5553  if (ni_cmp_fw_api_ver(
5555  "6r3") < 0)
5556  {
5557  ni_log2(p_ctx, NI_LOG_INFO,
5558  "WARNING: Allocate framepool size 0 for session 0x%x\n", p_ctx->session_id);
5559  return NI_RETCODE_SUCCESS;
5560  }
5561  else if (p_ctx->pool_type == NI_POOL_TYPE_NONE)
5562  {
5563  ni_log2(p_ctx, NI_LOG_ERROR,
5564  "ERROR: %s() try to free session 0x%x framepool while it's not allocated\n",
5565  __func__, p_ctx->session_id);
5566  return NI_RETCODE_INVALID_PARAM;
5567  }
5568  else if ((options & NI_SCALER_FLAG_P2) == p_ctx->pool_type)
5569  {
5570  ni_log2(p_ctx, NI_LOG_INFO, "Free framepool of scaler 0x%x\n", p_ctx->session_id);
5571  }
5572  else
5573  {
5574  ni_log2(p_ctx, NI_LOG_ERROR,
5575  "ERROR: %s() try to free session 0x%x framepool of type %u while "
5576  "passing type %u\n",
5577  __func__, p_ctx->session_id, p_ctx->pool_type,
5578  (options & NI_SCALER_FLAG_P2));
5579  }
5580  }
5581  else
5582  {
5583  if (p_ctx->pool_type != NI_POOL_TYPE_NONE)
5584  {
5585  // try to expand the framepool
5586  if (ni_cmp_fw_api_ver(
5588  "6r3") < 0)
5589  {
5590  ni_log2(p_ctx, NI_LOG_ERROR,
5591  "ERROR: allocate framepool multiple times for session 0x%x "
5592  "not supported in FW API version < 6r3\n",
5593  p_ctx->session_id);
5594  return NI_RETCODE_INVALID_PARAM;
5595  }
5596  if ((options & NI_SCALER_FLAG_P2) == p_ctx->pool_type)
5597  {
5598  ni_log2(p_ctx, NI_LOG_INFO,
5599  "Expand frame pool of scaler 0x%x with %u more frames\n",
5600  p_ctx->session_id, rgba_color);
5601  }
5602  else
5603  {
5604  ni_log2(p_ctx, NI_LOG_ERROR,
5605  "ERROR: try to expand session 0x%x framepool with type %u "
5606  "while pool type is %u\n",
5607  p_ctx->session_id, options & NI_SCALER_FLAG_P2, p_ctx->pool_type);
5608  return NI_RETCODE_INVALID_PARAM;
5609  }
5610  }
5611  }
5612  }
5613 
5614  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
5615  {
5616  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5617  NI_ERRNO, __func__);
5619  }
5620 
5621  memset(p_data, 0x00, dataLen);
5622 
5623  p_data->picture_width = width;
5624  p_data->picture_height = height;
5625  p_data->picture_format = format;
5626  p_data->options = options;
5627  p_data->rectangle_width = rectangle_width;
5628  p_data->rectangle_height = rectangle_height;
5629  p_data->rectangle_x = rectangle_x;
5630  p_data->rectangle_y = rectangle_y;
5631  p_data->rgba_color = rgba_color;
5632  p_data->frame_index = frame_index;
5633 
5634  ni_log2(p_ctx, NI_LOG_DEBUG, "%s() options %d frame_index %u buffered_frame_index %d\n",
5635  __func__, options, p_data->frame_index, p_ctx->buffered_frame_index);
5636 
5637  switch (p_ctx->scaler_operation)
5638  {
5641  if ((options & NI_SCALER_FLAG_IO) && !(options & NI_SCALER_FLAG_PC))
5642  {
5643  p_data->frame_index = frame_index; // background
5644  if (p_ctx->buffered_frame_index)
5645  {
5646  p_data->rgba_color = p_ctx->buffered_frame_index; // output frame
5647  }
5648  else
5649  {
5650  p_data->rgba_color = 0; // no output frame, fw must acquire
5651  }
5652  }
5653  break;
5654 
5655  default:
5656  // Send acquired buffer index in AdminWr SET_ALLOC_FRAME command to prevent FP
5657  // ConfigOutput from acquire buffer again
5658  if ((options & NI_SCALER_FLAG_IO) && p_ctx->buffered_frame_index)
5659  {
5660  p_data->frame_index = p_ctx->buffered_frame_index;
5661  }
5662  break;
5663  }
5664 
5665  bool isrgb = ((GC620_RGBA8888 == format) || (GC620_BGRX8888 == format) ||
5666  (GC620_ARGB8888 == format) || (GC620_ABGR8888 == format));
5667  if(width > NI_MAX_RESOLUTION_WIDTH || height > NI_MAX_RESOLUTION_HEIGHT ||
5669  ((width > NI_MAX_RESOLUTION_RGBA_WIDTH || height > NI_MAX_RESOLUTION_RGBA_HEIGHT) && isrgb))
5670  {
5671  if(width < NI_MIN_RESOLUTION_WIDTH_SCALER)
5672  ni_log2(p_ctx, NI_LOG_ERROR, "Invalid Picture Width: too small for scaler\n");
5673  if(height < NI_MIN_RESOLUTION_HEIGHT_SCALER)
5674  ni_log2(p_ctx, NI_LOG_ERROR, "Invalid Picture Height: too small for scaler\n");
5675  if(width > NI_MAX_RESOLUTION_WIDTH || ((width > NI_MAX_RESOLUTION_RGBA_WIDTH) && isrgb))
5676  ni_log2(p_ctx, NI_LOG_ERROR, "Invalid Picture Width: too big for scaler\n");
5677  if(height > NI_MAX_RESOLUTION_HEIGHT || ((height > NI_MAX_RESOLUTION_RGBA_HEIGHT) && isrgb))
5678  ni_log2(p_ctx, NI_LOG_ERROR, "Invalid Picture Height: too big for scaler\n");
5679 
5680  ni_log2(p_ctx, NI_LOG_ERROR, "Resolution %d x %d not supported for %d format!\n", width, height, format);
5681  ni_aligned_free(p_data);
5683  }
5684  ni_log2(p_ctx, NI_LOG_DEBUG,
5685  "Session=0x%x: Dev alloc frame: FrameIndex=%d; W=%d; H=%d; C=%d; RW=%d; RH=%d; RX=%d; RY=%d\n",
5686  p_ctx->session_id, p_data->frame_index,
5687  p_data->picture_width, p_data->picture_height,
5688  p_data->picture_format, p_data->rectangle_width,
5689  p_data->rectangle_height, p_data->rectangle_x, p_data->rectangle_y);
5690 
5693 
5694  for (;;)
5695  {
5696  query_retry++;
5697 
5698  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5699  p_data, dataLen, ui32LBA);
5700 
5701  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
5702  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_3);
5703 
5705  {
5706  if (query_retry >= 1000 || (options & NI_SCALER_FLAG_PC))
5707  {
5708  ni_log2(p_ctx, NI_LOG_ERROR, "Error: 2D could not acquire frame\n");
5709  retval = NI_RETCODE_FAILURE;
5710  LRETURN;
5711  }
5713  continue;
5714  }
5715  else
5716  {
5717  p_ctx->pool_type = ((options & NI_SCALER_FLAG_IO) && (options & NI_SCALER_FLAG_PC)) ?
5718  (options & NI_SCALER_FLAG_P2) : (p_ctx->pool_type);
5719  break;
5720  }
5721  }
5722 
5723  if (NI_RETCODE_SUCCESS != retval)
5724  {
5725  ni_log2(p_ctx, NI_LOG_ERROR,
5726  "ERROR: ni_nvme_send_admin_cmd failed: "
5727  "blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n",
5728  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
5729  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n", __func__);
5731  }
5732 
5733 END:
5734 
5735  ni_aligned_free(p_data);
5736  return retval;
5737 }
5738 
5739 /*!******************************************************************************
5740  * \brief config a frame in the scaler
5741  *
5742  * \param[in] p_ctx pointer to session context
5743  * \param[in] p_cfg pointer to frame config
5744  *
5745  * \return NI_RETCODE_INVALID_PARAM
5746  * NI_RETCODE_ERROR_INVALID_SESSION
5747  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5748  * NI_RETCODE_ERROR_MEM_ALOC
5749  *******************************************************************************/
5751  ni_frame_config_t *p_cfg)
5752 {
5755  uint32_t dataLen;
5756  uint32_t ui32LBA = 0;
5757 
5758  /* Round up to nearest 4096 bytes */
5759  dataLen =
5761  dataLen = dataLen & 0xFFFFF000;
5762 
5763  if (!p_ctx || !p_cfg)
5764  {
5765  return NI_RETCODE_INVALID_PARAM;
5766  }
5767 
5768  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5769  {
5770  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5771  __func__);
5773  }
5774 
5775  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
5776  {
5777  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5778  NI_ERRNO, __func__);
5780  }
5781 
5782  memset(p_data, 0x00, dataLen);
5783 
5784  p_data->picture_width = p_cfg->picture_width;
5785  p_data->picture_height = p_cfg->picture_height;
5786  p_data->picture_format = p_cfg->picture_format;
5787  p_data->options = p_cfg->options;
5788 
5789  p_data->rectangle_width = p_cfg->rectangle_width;
5790  p_data->rectangle_height = p_cfg->rectangle_height;
5791  p_data->rectangle_x = p_cfg->rectangle_x;
5792  p_data->rectangle_y = p_cfg->rectangle_y;
5793  p_data->rgba_color = p_cfg->rgba_color;
5794  p_data->frame_index = p_cfg->frame_index;
5795  p_data->session_id = p_cfg->session_id;
5796  p_data->output_index = p_cfg->output_index;
5797  switch (p_cfg->orientation)
5798  {
5799  case 0:
5800  case 2:
5801  case 4:
5802  case 5:
5803  p_data->orientation = p_cfg->orientation;
5804  break;
5805  case 1:
5806  p_data->orientation = 3;
5807  break;
5808  case 3:
5809  p_data->orientation = 1;
5810  break;
5811  default:
5812  ni_log2(p_ctx, NI_LOG_ERROR, "Bad orientation: %u\n", p_cfg->orientation);
5814  }
5815  bool isrgb = ((GC620_RGBA8888 == p_data->picture_format) || (GC620_BGRX8888 == p_data->picture_format) ||
5816  (GC620_ARGB8888 == p_data->picture_format) || (GC620_ABGR8888 == p_data->picture_format));
5817  if(p_data->picture_width > NI_MAX_RESOLUTION_WIDTH || p_data->picture_height > NI_MAX_RESOLUTION_HEIGHT ||
5818  p_data->picture_width < NI_MIN_RESOLUTION_WIDTH_SCALER || p_data->picture_height < NI_MIN_RESOLUTION_HEIGHT_SCALER ||
5819  ((p_data->picture_width > NI_MAX_RESOLUTION_RGBA_WIDTH || p_data->picture_height > NI_MAX_RESOLUTION_RGBA_HEIGHT) && isrgb))
5820  {
5821  ni_log2(p_ctx, NI_LOG_ERROR, "Resolution %d x %d not supported for %d format!\n", p_data->picture_width, p_data->picture_height, p_data->picture_format);
5822  ni_aligned_free(p_data);
5824  }
5825 
5826  ni_log2(p_ctx, NI_LOG_DEBUG,
5827  "Session=0x%x: Dev config frame: FrameIndex=%u; W=%d; H=%d; C=%d; RW=%d; RH=%d; RX=%d; RY=%d; O=%d\n",
5828  p_ctx->session_id, p_cfg->frame_index,
5829  p_data->picture_width, p_data->picture_height,
5830  p_data->picture_format, p_data->rectangle_width,
5831  p_data->rectangle_height, p_data->rectangle_x,
5832  p_data->rectangle_y, p_data->orientation);
5833 
5836 
5837  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5838  p_data, dataLen, ui32LBA);
5839  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
5840  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5841  if (NI_RETCODE_SUCCESS != retval)
5842  {
5843  ni_log2(p_ctx, NI_LOG_ERROR,
5844  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
5845  ", hw_id, %u, xcoder_inst_id: %d\n",
5846  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
5847 
5848  ni_log2(p_ctx, NI_LOG_ERROR,
5849  "ERROR ni_scaler_config(): nvme command failed!\n");
5851  }
5852 
5853 END:
5854 
5855  ni_aligned_free(p_data);
5856  return retval;
5857 }
5858 
5859 /*!******************************************************************************
5860  * \brief config multiple frames in the scaler
5861  *
5862  * \param[in] p_ctx pointer to session context
5863  * \param[in] p_cfg_in pointer to input frame config array
5864  * \param[in] numInCfgs number of input frame configs in the p_cfg array
5865  * \param[in] p_cfg_out pointer to output frame config
5866  *
5867  * \return NI_RETCODE_INVALID_PARAM
5868  * NI_RETCODE_ERROR_INVALID_SESSION
5869  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5870  * NI_RETCODE_ERROR_MEM_ALOC
5871  *******************************************************************************/
5873  ni_frame_config_t p_cfg_in[],
5874  int numInCfgs,
5875  ni_frame_config_t *p_cfg_out)
5876 {
5878  ni_instance_mgr_allocation_info_t *p_data, *p_data_orig;
5879  uint32_t dataLen;
5880  uint32_t ui32LBA = 0;
5881  int i;
5882 
5883  /* Round up to nearest 4096 bytes */
5884  dataLen =
5885  sizeof(ni_instance_mgr_allocation_info_t) * (numInCfgs + 1) + NI_MEM_PAGE_ALIGNMENT - 1;
5886  dataLen = dataLen & 0xFFFFF000;
5887 
5888  if (!p_ctx || (!p_cfg_in && numInCfgs))
5889  {
5890  return NI_RETCODE_INVALID_PARAM;
5891  }
5892 
5893  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5894  {
5895  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5896  __func__);
5898  }
5899 
5900  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
5901  {
5902  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5903  NI_ERRNO, __func__);
5905  }
5906 
5907  memset(p_data, 0x00, dataLen);
5908 
5909  p_data_orig = p_data;
5910 
5911  for (i = 0; i < numInCfgs; i++)
5912  {
5913  p_data->picture_width = p_cfg_in[i].picture_width;
5914  p_data->picture_height = p_cfg_in[i].picture_height;
5915  p_data->picture_format = p_cfg_in[i].picture_format;
5916  p_data->options = p_cfg_in[i].options & ~NI_SCALER_FLAG_IO;
5917 
5918  p_data->rectangle_width = p_cfg_in[i].rectangle_width;
5919  p_data->rectangle_height = p_cfg_in[i].rectangle_height;
5920  p_data->rectangle_x = p_cfg_in[i].rectangle_x;
5921  p_data->rectangle_y = p_cfg_in[i].rectangle_y;
5922  p_data->rgba_color = p_cfg_in[i].rgba_color;
5923  p_data->frame_index = p_cfg_in[i].frame_index;
5924  p_data->session_id = p_cfg_in[i].session_id;
5925  p_data->output_index = p_cfg_in[i].output_index;
5926 
5927  ni_log2(p_ctx, NI_LOG_DEBUG,
5928  "Session=0x%x: Dev in config frame %d: FrameIndex=%u; Session=0x%x; W=%d; H=%d; C=%d; RW=%d; RH=%d; RX=%d; RY=%d\n",
5929  p_ctx->session_id, i,
5930  p_data->frame_index, p_data->session_id,
5931  p_data->picture_width, p_data->picture_height,
5932  p_data->picture_format, p_data->rectangle_width,
5933  p_data->rectangle_height, p_data->rectangle_x, p_data->rectangle_y);
5934 
5935  p_data++;
5936  }
5937 
5938  if (p_cfg_out)
5939  {
5940  p_data->picture_width = p_cfg_out->picture_width;
5941  p_data->picture_height = p_cfg_out->picture_height;
5942  p_data->picture_format = p_cfg_out->picture_format;
5943  p_data->options = p_cfg_out->options | NI_SCALER_FLAG_IO;
5944 
5945  p_data->rectangle_width = p_cfg_out->rectangle_width;
5946  p_data->rectangle_height = p_cfg_out->rectangle_height;
5947  p_data->rectangle_x = p_cfg_out->rectangle_x;
5948  p_data->rectangle_y = p_cfg_out->rectangle_y;
5949  p_data->rgba_color = p_cfg_out->rgba_color;
5950  p_data->frame_index = p_cfg_out->frame_index;
5951 
5952  ni_log2(p_ctx, NI_LOG_DEBUG,
5953  "Session=0x%x: Dev out config frame: FrameIndex=%u; W=%d; H=%d; C=%d; RW=%d; RH=%d; RX=%d; RY=%d\n",
5954  p_ctx->session_id, p_data->frame_index,
5955  p_data->picture_width, p_data->picture_height,
5956  p_data->picture_format, p_data->rectangle_width,
5957  p_data->rectangle_height, p_data->rectangle_x,
5958  p_data->rectangle_y);
5959  }
5960  if(p_data->picture_width > NI_MAX_RESOLUTION_WIDTH || p_data->picture_height > NI_MAX_RESOLUTION_HEIGHT || ((p_data->picture_width > NI_MAX_RESOLUTION_RGBA_WIDTH || p_data->picture_height> NI_MAX_RESOLUTION_RGBA_HEIGHT) && ((GC620_RGBA8888 == p_data->picture_format) || (GC620_BGRX8888 == p_data->picture_format) || (GC620_ARGB8888 == p_data->picture_format) || (GC620_ABGR8888 == p_data->picture_format))))
5961  {
5962  ni_log2(p_ctx, NI_LOG_ERROR, "Resolution %d x %d not supported for %d format!\n", p_data->picture_width, p_data->picture_height, p_data->picture_format);
5963  ni_aligned_free(p_data);
5965  }
5966 
5969 
5970  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5971  p_data_orig, dataLen, ui32LBA);
5972  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
5973  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5974  if (NI_RETCODE_SUCCESS != retval)
5975  {
5976  ni_log2(p_ctx, NI_LOG_ERROR,
5977  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
5978  ", hw_id, %u, xcoder_inst_id: %d\n",
5979  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
5980 
5981  ni_log2(p_ctx, NI_LOG_ERROR,
5982  "ERROR ni_scaler_config(): nvme command failed!\n");
5984  }
5985 
5986 END:
5987 
5988  ni_aligned_free(p_data_orig);
5989  return retval;
5990 }
5991 
5992 /*!******************************************************************************
5993  * \brief Query a particular xcoder instance to get GeneralStatus data
5994  *
5995  * \param ni_session_context_t p_ctx - xcoder Context
5996  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
5997  * \param ni_instance_mgr_general_status_t *out - Struct preallocated from the caller where the
5998  * resulting data will be placed
5999  *
6000  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6001  *******************************************************************************/
6003 {
6004  void* p_buffer = NULL;
6005  int retval = NI_RETCODE_SUCCESS;
6006  uint32_t ui32LBA = 0;
6008 
6009  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6010 
6011  if ((!p_ctx) || (!p_gen_status))
6012  {
6013  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6014  __func__);
6015  retval = NI_RETCODE_INVALID_PARAM;
6016  LRETURN;
6017  }
6018 
6019  if (!IS_XCODER_DEVICE_TYPE(device_type))
6020  {
6021  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6022  __func__, device_type);
6023  retval = NI_RETCODE_INVALID_PARAM;
6024  LRETURN;
6025  }
6026 
6027  ui32LBA = QUERY_GENERAL_GET_STATUS_R(device_type);
6028 
6029  if (ni_posix_memalign((void **)&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6030  {
6031  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6032  NI_ERRNO, __func__);
6033  retval = NI_RETCODE_ERROR_MEM_ALOC;
6034  LRETURN;
6035  }
6036 
6037  memset(p_buffer, 0, dataLen);
6038 
6039  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6040  {
6041  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6043  LRETURN;
6044  }
6045 
6046  //No need to flip the bytes since the datastruct has only uint8_t datatypes
6047  memcpy((void*)p_gen_status, p_buffer, sizeof(ni_instance_mgr_general_status_t));
6048 
6049  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): model_load:%u qc:%d percent:%d\n", __func__,
6050  p_gen_status->fw_model_load, p_gen_status->cmd_queue_count,
6051  p_gen_status->process_load_percent);
6052 END:
6053 
6054  ni_aligned_free(p_buffer);
6055  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6056 
6057  return retval;
6058 }
6059 
6060 /*!******************************************************************************
6061  * \brief Query a particular xcoder instance to get DetailStatus data
6062  *
6063  * \param ni_session_context_t p_ctx - xcoder Context
6064  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6065  * \param ni_instance_mgr_detail_status_t *out - Struct preallocated from the caller where the
6066  * resulting data will be placed
6067  *
6068  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6069  *******************************************************************************/
6070 int ni_query_detail_status(ni_session_context_t* p_ctx, ni_device_type_t device_type, void* p_detail_status, int ver)
6071 {
6072  void* p_buffer = NULL;
6073  int retval = NI_RETCODE_SUCCESS;
6074  uint32_t ui32LBA = 0;
6075  uint32_t dataLen = 0;
6076  uint32_t copyLen = 0;
6077  if(ver == 0)
6078  {
6079  ui32LBA = QUERY_DETAIL_GET_STATUS_R(device_type);
6081  dataLen = ((copyLen + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
6082  }
6083  else if(ver == 1)
6084  {
6085  ui32LBA = QUERY_DETAIL_GET_STATUS_V1_R(device_type);
6086  copyLen = sizeof(ni_instance_mgr_detail_status_v1_t);
6087  dataLen = (copyLen + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT * NI_MEM_PAGE_ALIGNMENT;
6088  }
6089  else
6090  {
6091  retval = NI_RETCODE_INVALID_PARAM;
6092  LRETURN;
6093  }
6094 
6095  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6096 
6097  if ((!p_ctx) || (!p_detail_status))
6098  {
6099  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6100  __func__);
6101  retval = NI_RETCODE_INVALID_PARAM;
6102  LRETURN;
6103  }
6104 
6105  if (!IS_XCODER_DEVICE_TYPE(device_type))
6106  {
6107  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6108  __func__, device_type);
6109  retval = NI_RETCODE_INVALID_PARAM;
6110  LRETURN;
6111  }
6112 
6113  if (ni_posix_memalign((void **)&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6114  {
6115  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6116  NI_ERRNO, __func__);
6117  retval = NI_RETCODE_ERROR_MEM_ALOC;
6118  LRETURN;
6119  }
6120 
6121  memset(p_buffer, 0, dataLen);
6122 
6123  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6124  {
6125  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6127  LRETURN;
6128  }
6129 
6130  //No need to flip the bytes since the datastruct has only uint8_t datatypes
6131  memcpy(p_detail_status, p_buffer, copyLen);
6132 
6133 END:
6134 
6135  ni_aligned_free(p_buffer);
6136  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6137 
6138  return retval;
6139 }
6140 
6141 /*!******************************************************************************
6142  * \brief Query a particular xcoder instance to get Stream Info data
6143  *
6144  * \param ni_session_context_t p_ctx - xcoder Context
6145  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6146  * \param ni_instance_mgr_stream_info_t *out - Struct preallocated from the caller where the
6147  * resulting data will be placed
6148  *
6149  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_MEM_ALOC
6150  * or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6151  *******************************************************************************/
6153 {
6154  void* p_buffer = NULL;
6156  uint32_t ui32LBA = 0;
6158 
6159  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6160 
6161  if ((!p_ctx) || (!p_stream_info))
6162  {
6163  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6164  __func__);
6165  retval = NI_RETCODE_INVALID_PARAM;
6166  LRETURN;
6167  }
6168 
6169  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
6170  NI_DEVICE_TYPE_ENCODER == device_type))
6171  {
6172  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6173  __func__, device_type);
6174  retval = NI_RETCODE_INVALID_PARAM;
6175  LRETURN;
6176  }
6177 
6178  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6179  {
6180  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6181  __func__);
6183  LRETURN;
6184  }
6185 
6186  ui32LBA = QUERY_INSTANCE_STREAM_INFO_R(p_ctx->session_id, device_type);
6187 
6188  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6189  {
6190  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6191  NI_ERRNO, __func__);
6192  retval = NI_RETCODE_ERROR_MEM_ALOC;
6193  LRETURN;
6194  }
6195  memset(p_buffer, 0, dataLen);
6196 
6197  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6198  {
6199  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6201  LRETURN;
6202  }
6203 
6204  memcpy((void*)p_stream_info, p_buffer, sizeof(ni_instance_mgr_stream_info_t));
6205 
6206  //flip the bytes to host order
6207  p_stream_info->picture_width = ni_htons(p_stream_info->picture_width);
6208  p_stream_info->picture_height = ni_htons(p_stream_info->picture_height);
6209  p_stream_info->frame_rate = ni_htons(p_stream_info->frame_rate);
6210  p_stream_info->is_flushed = ni_htons(p_stream_info->is_flushed);
6211  p_stream_info->transfer_frame_stride = ni_htons(p_stream_info->transfer_frame_stride);
6212  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): pix_format = %d\n", __func__,
6213  p_stream_info->pix_format); //temp
6214 
6215 END:
6216 
6217  ni_aligned_free(p_buffer);
6218  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6219 
6220  return retval;
6221 }
6222 
6223 /*!*****************************************************************************
6224  * \brief Query a particular session to get the stats info
6225  *
6226  * \param ni_session_context_t p_ctx - xcoder Context
6227  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6228  * \param ni_session_stats_t *out - Struct preallocated from the
6229  * caller where the resulting data will be placed
6230  *
6231  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
6232  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED
6233  * on failure
6234  ******************************************************************************/
6236  ni_device_type_t device_type,
6237  ni_session_stats_t *p_session_stats, int rc,
6238  int opcode)
6239 {
6241  void* p_buffer = NULL;
6242  uint64_t session_timestamp;
6243  uint32_t ui32LBA = 0;
6244  uint32_t dataLen = ((sizeof(ni_session_stats_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
6245 
6246  ni_device_type_t xc_device_type =
6247  (device_type != NI_DEVICE_TYPE_UPLOAD ? device_type :
6249 
6250  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6251 
6252  if ((!p_ctx) || (!p_session_stats))
6253  {
6254  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6255  __func__);
6256  retval = NI_RETCODE_INVALID_PARAM;
6257  LRETURN;
6258  }
6259 
6260  if (!IS_XCODER_DEVICE_TYPE(xc_device_type))
6261  {
6262  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6263  __func__, device_type);
6264  retval = NI_RETCODE_INVALID_PARAM;
6265  LRETURN;
6266  }
6267 
6268  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6269  {
6270  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6271  __func__);
6273  LRETURN;
6274  }
6275 
6276  ui32LBA = QUERY_SESSION_STATS_R(p_ctx->session_id, xc_device_type);
6277 
6278  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6279  {
6280  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6281  NI_ERRNO, __func__);
6282  retval = NI_RETCODE_ERROR_MEM_ALOC;
6283  LRETURN;
6284  }
6285  memset(p_buffer, 0, dataLen);
6286 
6287  // Set session ID to be invalid. In case, the last command fails because the invalid session ID was submitted
6288  // with the command, the session id would remain invalid.
6289  // If the last command is processed successfully in session manager, the session id would become valid.
6290  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
6291  (uint16_t)NI_INVALID_SESSION_ID;
6292 
6293  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA)
6294  < 0)
6295  {
6296  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): read command Failed\n", __func__);
6298  LRETURN;
6299  }
6300 
6301  memcpy((void*)p_session_stats, p_buffer, sizeof(ni_session_stats_t));
6302 
6303  // flip the bytes to host order
6304  // all query commands are guaranteed success once a session is opened
6305  p_session_stats->ui16SessionId = ni_htons(p_session_stats->ui16SessionId);
6306  p_session_stats->ui16ErrorCount = ni_htons(p_session_stats->ui16ErrorCount);
6307  p_session_stats->ui32LastTransactionId =
6308  ni_htonl(p_session_stats->ui32LastTransactionId);
6309  p_session_stats->ui32LastTransactionCompletionStatus =
6310  ni_htonl(p_session_stats->ui32LastTransactionCompletionStatus);
6311  p_session_stats->ui32LastErrorTransactionId = ni_htonl(p_session_stats->ui32LastErrorTransactionId);
6312  p_session_stats->ui32LastErrorStatus = ni_htonl(p_session_stats->ui32LastErrorStatus);
6313  p_session_stats->ui32Session_timestamp_high = ni_htonl(p_session_stats->ui32Session_timestamp_high);
6314  p_session_stats->ui32Session_timestamp_low = ni_htonl(p_session_stats->ui32Session_timestamp_low);
6315 
6316  session_timestamp = p_session_stats->ui32Session_timestamp_high;
6317  session_timestamp <<= 32;
6318  session_timestamp |= p_session_stats->ui32Session_timestamp_low;
6319 
6320  // get the session timestamp when open session
6321  // check the timestamp during transcoding
6322  if ((p_ctx->session_timestamp != session_timestamp) &&
6323  (ni_xcoder_resource_recovery != p_session_stats->ui32LastErrorStatus))
6324  // if VPU recovery, the session timestamp will be reset.
6325  {
6326  p_session_stats->ui32LastErrorStatus =
6328  ni_log2(p_ctx, NI_LOG_DEBUG, "instance id invalid:%u, timestamp:%" PRIu64 ", "
6329  "query timestamp:%" PRIu64 "\n", p_ctx->session_id,
6330  p_ctx->session_timestamp, session_timestamp);
6331  }
6332 
6333  // check rc here, if rc != NI_RETCODE_SUCCESS, it means that last read/write command failed
6334  // failures may be link layer errors, such as physical link errors or ERROR_WRITE_PROTECT in windows.
6335  if (NI_RETCODE_SUCCESS != rc)
6336  {
6337  ni_log2(p_ctx, NI_LOG_ERROR, "%s():last command Failed: rc %d\n", __func__, rc);
6338  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): session id %u ts %lu hw_id %d device_type %u codec_format %u frame_num %lu pkt_num %lu "
6339  "ready_to_close %u session_run_state %d active_video_width %u active_video_height %u\n",
6340  __func__,
6341  p_ctx->session_id,
6342  p_ctx->session_timestamp,
6343  p_ctx->hw_id,
6344  p_ctx->device_type,
6345  p_ctx->codec_format,
6346  p_ctx->frame_num,
6347  p_ctx->pkt_num,
6348  p_ctx->ready_to_close,
6349  p_ctx->session_run_state,
6350  p_ctx->active_video_width,
6351  p_ctx->active_video_height);
6352 
6353 #if __linux__ || __APPLE__
6354 #if !defined(_ANDROID) && !defined(__OPENHARMONY__)
6355 #ifndef DISABLE_BACKTRACE_PRINT
6356  ni_print_backtrace(); // log backtrace
6357 #endif
6358 #endif
6359 #endif
6360 
6361  p_session_stats->ui32LastTransactionCompletionStatus =
6363  p_session_stats->ui32LastErrorStatus = NI_RETCODE_ERROR_NVME_CMD_FAILED;
6365  } else if (p_ctx->session_id != p_session_stats->ui16SessionId)
6366  {
6367  uint64_t ct = ni_gettime_ns();
6368  uint64_t dt = ct - p_ctx->last_access_time;
6369  ni_log2(p_ctx, NI_LOG_ERROR,
6370  "%s(): device 0x%" PRIx64 " last command Failed due to wrong "
6371  "session ID. Expected 0x%x, got 0x%x keep alive last access "
6372  "time %" PRIu64 ", current %" PRIu64 "\n", __func__,
6373  (int64_t)p_ctx->blk_io_handle, p_ctx->session_id,
6374  p_session_stats->ui16SessionId, p_ctx->last_access_time, ct);
6375  if (dt > 1000000000)
6376  {
6377  ni_log2(p_ctx, NI_LOG_ERROR,
6378  "%s():long delay between last command dt = %" PRId64 " ns, "
6379  "process was possibly blocked.\n", __func__, dt);
6380  }
6381  p_session_stats->ui32LastErrorStatus = NI_RETCODE_ERROR_INVALID_SESSION;
6382 
6383  // Mark session id to INVALID so that all commands afterward are blocked
6385  }
6386 
6387  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): error count %u last rc 0x%x inst_err_no 0x%x\n",
6388  __func__, p_session_stats->ui16ErrorCount,
6389  p_session_stats->ui32LastTransactionCompletionStatus,
6390  p_session_stats->ui32LastErrorStatus);
6391 
6392 END:
6393 
6394  ni_aligned_free(p_buffer);
6395  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6396 
6397  return retval;
6398 }
6399 
6400 /*!******************************************************************************
6401  * \brief Query a particular xcoder instance to get End of Output data
6402  *
6403  * \param ni_session_context_t p_ctx - xcoder Context
6404  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6405  * \param InstMgrStreamComp *out - Struct preallocated from the caller where the
6406  * resulting data will be placed
6407  *
6408  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_MEM_ALOC
6409  * or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6410  *******************************************************************************/
6412 {
6413  void* p_buffer = NULL;
6415  uint32_t ui32LBA = 0;
6417 
6418  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6419 
6420  if (!p_ctx || !p_stream_complete)
6421  {
6422  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6423  __func__);
6424  retval = NI_RETCODE_INVALID_PARAM;
6425  LRETURN;
6426  }
6427 
6428  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
6429  NI_DEVICE_TYPE_ENCODER == device_type))
6430  {
6431  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6432  __func__, device_type);
6433  retval = NI_RETCODE_INVALID_PARAM;
6434  LRETURN;
6435  }
6436 
6437  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6438  {
6439  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6440  __func__);
6442  LRETURN;
6443  }
6444 
6445  ui32LBA = QUERY_INSTANCE_EOS_R(p_ctx->session_id, device_type);
6446 
6447  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6448  {
6449  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate buffer.\n");
6450  retval = NI_RETCODE_ERROR_MEM_ALOC;
6451  LRETURN;
6452  }
6453 
6454  memset(p_buffer, 0, dataLen);
6455 
6456  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6457  {
6458  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6460  LRETURN;
6461  }
6462 
6463  memcpy((void*)p_stream_complete, p_buffer, sizeof(ni_instance_mgr_stream_complete_t));
6464 
6465  //flip the bytes to host order
6466  p_stream_complete->is_flushed = ni_htons(p_stream_complete->is_flushed);
6467 
6468 END:
6469 
6470  ni_aligned_free(p_buffer);
6471  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6472 
6473  return retval;
6474 }
6475 
6476 static const char* ni_get_device_type_str(int type)
6477 {
6478  if (type < NI_DEVICE_TYPE_DECODER || type > NI_DEVICE_TYPE_AI)
6479  {
6480  return "Invalid device type";
6481  }
6482  return g_device_type_str[type];
6483 }
6484 
6485 static void
6486 ni_parse_session_statistic_info(ni_session_context_t *p_ctx,
6487  ni_session_statistic_t *p_session_statistic,
6488  void *p_buffer)
6489 {
6490  memcpy((void *)p_session_statistic, p_buffer,
6491  sizeof(ni_session_statistic_t));
6492 
6493  //flip the bytes to host order
6494  p_session_statistic->ui32RdBufAvailSize =
6495  ni_htonl(p_session_statistic->ui32RdBufAvailSize);
6496  p_session_statistic->ui32WrBufAvailSize =
6497  ni_htonl(p_session_statistic->ui32WrBufAvailSize);
6498 
6499  p_session_statistic->ui32FramesInput =
6500  ni_htonl(p_session_statistic->ui32FramesInput);
6501  p_session_statistic->ui32FramesBuffered =
6502  ni_htonl(p_session_statistic->ui32FramesBuffered);
6503  p_session_statistic->ui32FramesCompleted =
6504  ni_htonl(p_session_statistic->ui32FramesCompleted);
6505  p_session_statistic->ui32FramesOutput =
6506  ni_htonl(p_session_statistic->ui32FramesOutput);
6507  p_session_statistic->ui32FramesDropped =
6508  ni_htonl(p_session_statistic->ui32FramesDropped);
6509  p_session_statistic->ui32InstErrors =
6510  ni_htonl(p_session_statistic->ui32InstErrors);
6511 
6512  p_session_statistic->ui16SessionId =
6513  ni_htons(p_session_statistic->ui16SessionId);
6514  p_session_statistic->ui16ErrorCount =
6515  ni_htons(p_session_statistic->ui16ErrorCount);
6516  p_session_statistic->ui32LastTransactionId =
6517  ni_htonl(p_session_statistic->ui32LastTransactionId);
6518  p_session_statistic->ui32LastTransactionCompletionStatus =
6519  ni_htonl(p_session_statistic->ui32LastTransactionCompletionStatus);
6520  p_session_statistic->ui32LastErrorTransactionId =
6521  ni_htonl(p_session_statistic->ui32LastErrorTransactionId);
6522  p_session_statistic->ui32LastErrorStatus =
6523  ni_htonl(p_session_statistic->ui32LastErrorStatus);
6524  p_session_statistic->ui32Session_timestamp_high =
6525  ni_htonl(p_session_statistic->ui32Session_timestamp_high);
6526  p_session_statistic->ui32Session_timestamp_low =
6527  ni_htonl(p_session_statistic->ui32Session_timestamp_low);
6528 
6529  // p_session_statistic->ui8AdditionalFramesDelay does not require endian conversion
6530 
6531  if (p_ctx->session_id != p_session_statistic->ui16SessionId)
6532  {
6533  uint64_t ct = ni_gettime_ns();
6534  uint64_t dt = ct - p_ctx->last_access_time;
6535  ni_log2(p_ctx, NI_LOG_ERROR,
6536  "%s(): %s device 0x%" PRIx64 " last command Failed due to wrong "
6537  "session ID. Expected 0x%x, got 0x%x w_r <%u %u> keep alive "
6538  "last access time %" PRIu64 ", current %" PRIu64 "\n", __func__,
6539  ni_get_device_type_str(p_ctx->device_type),
6540  (int64_t)p_ctx->device_handle, p_ctx->session_id,
6541  p_session_statistic->ui16SessionId,
6542  p_session_statistic->ui32WrBufAvailSize,
6543  p_session_statistic->ui32RdBufAvailSize, p_ctx->last_access_time,
6544  ct);
6545  if (dt > 1000000000)
6546  {
6547  ni_log2(p_ctx, NI_LOG_ERROR,
6548  "%s():long delay between last command dt = %" PRId64 " ns, "
6549  "process was possibly blocked.\n", __func__, dt);
6550  }
6551  p_session_statistic->ui32LastErrorStatus =
6553  //Mark session id to INVALID so that all commands afterward are blocked
6555  } else
6556  {
6557  //Acknowledge that total error count here
6558  }
6559 
6560  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): error count %u last rc 0x%x inst_err_no 0x%x\n",
6561  __func__, p_session_statistic->ui16ErrorCount,
6562  p_session_statistic->ui32LastTransactionCompletionStatus,
6563  p_session_statistic->ui32LastErrorStatus);
6564 }
6565 
6566 /*!*****************************************************************************
6567  * \brief Query a particular xcoder session to get session statistics
6568  *
6569  * \param ni_session_context_t p_ctx - xcoder Context
6570  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6571  * \param ni_session_statistic_t*out - Struct preallocated from the caller
6572  * where the resulting data will be placed
6573  *
6574  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
6575  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
6576  * failure
6577  ******************************************************************************/
6580  ni_device_type_t device_type,
6581  ni_session_statistic_t *p_session_statistic)
6582 {
6583  void *p_buffer = NULL;
6585  uint32_t ui32LBA = 0;
6586  uint32_t dataLen =
6587  ((sizeof(ni_session_statistic_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) /
6590 
6591  if (!p_ctx || !p_session_statistic)
6592  {
6593  ni_log2(p_ctx, NI_LOG_ERROR,
6594  "ERROR: %s() passed parameters are null!, return\n", __func__);
6595  retval = NI_RETCODE_INVALID_PARAM;
6596  LRETURN;
6597  }
6598 
6600  "65") < 0)
6601  {
6602  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() not supported on device with FW api version < 6.5\n", __func__);
6604  }
6605 
6606  if (!(NI_DEVICE_TYPE_DECODER == device_type ||
6607  NI_DEVICE_TYPE_ENCODER == device_type ||
6608  NI_DEVICE_TYPE_AI == device_type))
6609  {
6610  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6611  __func__, device_type);
6612  retval = NI_RETCODE_INVALID_PARAM;
6613  LRETURN;
6614  }
6615 
6616  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6617  {
6618  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6619  __func__);
6621  LRETURN;
6622  }
6623 
6624  ui32LBA = QUERY_INSTANCE_CUR_STATUS_INFO_R(p_ctx->session_id, device_type);
6625 
6626  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6627  {
6628  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6629  NI_ERRNO, __func__);
6630  retval = NI_RETCODE_ERROR_MEM_ALOC;
6631  LRETURN;
6632  }
6633  memset(p_buffer, 0, dataLen);
6634 
6635  // Set session ID to be invalid. In case, the last command fails because the invalid session ID was submitted
6636  // with the command, the session id would remain invalid.
6637  // If the Last command is processed successfully in session manager, the session id would become valid.
6638  ((ni_session_statistic_t *)p_buffer)->ui16SessionId =
6639  (uint16_t)NI_INVALID_SESSION_ID;
6640 
6642  p_buffer, dataLen, ui32LBA) < 0)
6643  {
6644  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): NVME command Failed\n", __func__);
6645  p_session_statistic->ui32LastTransactionCompletionStatus =
6647  p_session_statistic->ui32LastErrorStatus =
6650  LRETURN;
6651  }
6652 
6653  ni_parse_session_statistic_info(p_ctx, p_session_statistic, p_buffer);
6654  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
6655  {
6657  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, %s return.\n",
6658  __func__, ni_get_device_type_str(p_ctx->device_type));
6659  LRETURN;
6660  }
6661  p_ctx->session_statistic = *p_session_statistic;
6662 
6663 END:
6664  ni_aligned_free(p_buffer);
6665  ni_log2(p_ctx, NI_LOG_TRACE, "%s():exit\n", __func__);
6666 
6667  return retval;
6668 }
6669 /*!*****************************************************************************
6670  * \brief Query a particular xcoder instance to get buffer/data Info data
6671  *
6672  * \param ni_session_context_t p_ctx - xcoder Context
6673  * \param ni_instance_buf_info_rw_type_t rw_type
6674  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6675  * \param ni_instance_buf_info_t *out - Struct preallocated from the caller
6676  * where the resulting data will be placed
6677  *
6678  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
6679  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
6680  * failure
6681  ******************************************************************************/
6684  ni_device_type_t device_type,
6685  ni_instance_buf_info_t *p_inst_buf_info)
6686 {
6687  void* p_buffer = NULL;
6689  uint32_t ui32LBA = 0;
6690  uint32_t dataLen =
6691  ((sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) /
6694 
6695  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6696 
6697  if (!p_ctx || !p_inst_buf_info)
6698  {
6699  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6700  __func__);
6701  retval = NI_RETCODE_INVALID_PARAM;
6702  LRETURN;
6703  }
6704 
6705  if (!(NI_DEVICE_TYPE_DECODER == device_type ||
6706  NI_DEVICE_TYPE_ENCODER == device_type ||
6707  NI_DEVICE_TYPE_SCALER == device_type ||
6708  NI_DEVICE_TYPE_AI == device_type))
6709  {
6710  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6711  __func__, device_type);
6712  retval = NI_RETCODE_INVALID_PARAM;
6713  LRETURN;
6714  }
6715 
6716  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6717  {
6718  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6719  __func__);
6721  LRETURN;
6722  }
6723 
6724  if (INST_BUF_INFO_RW_READ == rw_type)
6725  {
6726  ui32LBA = QUERY_INSTANCE_RBUFF_SIZE_R(p_ctx->session_id, device_type);
6727  }
6728  else if (INST_BUF_INFO_RW_WRITE == rw_type)
6729  {
6730  ui32LBA = QUERY_INSTANCE_WBUFF_SIZE_R(p_ctx->session_id, device_type);
6731  }
6732  else if(INST_BUF_INFO_RW_WRITE_BY_EP == rw_type)
6733  {
6735  "65") >= 0)
6736  {
6737  ui32LBA = QUERY_INSTANCE_WBUFF_SIZE_R_BY_EP(p_ctx->session_id, device_type);
6738  }
6739  else
6740  {
6742  LRETURN;
6743  }
6744  }
6745  else if (INST_BUF_INFO_RW_UPLOAD == rw_type)
6746  {
6747  ui32LBA = QUERY_INSTANCE_UPLOAD_ID_R(p_ctx->session_id, device_type);
6748  } else if (INST_BUF_INFO_R_ACQUIRE == rw_type)
6749  {
6750  ui32LBA = QUERY_INSTANCE_ACQUIRE_BUF(p_ctx->session_id, device_type);
6751  } else if (INST_BUF_INFO_RW_READ_BUSY == rw_type)
6752  {
6753  ui32LBA =
6754  QUERY_INSTANCE_RBUFF_SIZE_BUSY_R(p_ctx->session_id, device_type);
6755  } else if (INST_BUF_INFO_RW_WRITE_BUSY == rw_type)
6756  {
6757  ui32LBA =
6758  QUERY_INSTANCE_WBUFF_SIZE_BUSY_R(p_ctx->session_id, device_type);
6759  } else if (INST_BUF_INFO_RW_READ_BY_AI == rw_type)
6760  {
6761  ui32LBA = QUERY_INSTANCE_AI_INFO_R(p_ctx->session_id, device_type);
6762  } else
6763  {
6764  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown query type %d, return\n",
6765  __func__, rw_type);
6766  retval = NI_RETCODE_INVALID_PARAM;
6767  LRETURN;
6768  }
6769 
6770  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6771  {
6772  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6773  NI_ERRNO, __func__);
6774  retval = NI_RETCODE_ERROR_MEM_ALOC;
6775  LRETURN;
6776  }
6777 
6778  memset(p_buffer, 0, dataLen);
6779 
6780  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6781  {
6782  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6784  LRETURN;
6785  }
6786 
6787  memcpy((void*)p_inst_buf_info, p_buffer, sizeof(ni_instance_buf_info_t));
6788 
6789  p_inst_buf_info->buf_avail_size = ni_htonl(p_inst_buf_info->buf_avail_size);
6790 
6791 END:
6792 
6793  ni_aligned_free(p_buffer);
6794  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6795 
6796  return retval;
6797 }
6798 
6799 /*!*****************************************************************************
6800  * \brief Configure the read/write pipe for a session to control its behavior
6801  *
6802  * \param ni_session_context_t p_ctx - xcoder Context
6803  * \param ni_session_config_rw_t rw_type
6804  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6805  * \param uint8_t enable
6806  * \param uint8_t hw_action
6807  * \param uint16_t frame_id
6808  *
6809  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
6810  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
6811  * failure
6812  ******************************************************************************/
6815  uint8_t enable, uint8_t hw_action,
6816  uint16_t frame_id)
6817 {
6819  uint32_t ui32LBA = 0;
6820  void * p_buffer = NULL;
6821  uint32_t buffer_size = 0;
6822  ni_session_config_rw_t * rw_config = NULL;
6823 
6824  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6825 
6826  if (!p_ctx)
6827  {
6828  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6829  __func__);
6830  retval = NI_RETCODE_INVALID_PARAM;
6831  LRETURN;
6832  }
6833 
6834  if (!((SESSION_READ_CONFIG == rw_type) || (SESSION_WRITE_CONFIG == rw_type)))
6835  {
6836  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown config type %d, return\n",
6837  __func__, rw_type);
6838  retval = NI_RETCODE_INVALID_PARAM;
6839  LRETURN;
6840  }
6841 
6843  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), buffer_size))
6844  {
6845  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6846  NI_ERRNO, __func__);
6847  retval = NI_RETCODE_ERROR_MEM_ALOC;
6848  LRETURN;
6849  }
6850  memset(p_buffer, 0, buffer_size);
6851  rw_config = (ni_session_config_rw_t *)p_buffer;
6852  rw_config->ui8Enable = enable;
6853  rw_config->ui8HWAccess = hw_action;
6854  switch(rw_type)
6855  {
6856  case SESSION_READ_CONFIG:
6857  rw_config->uHWAccessField.ui16ReadFrameId = frame_id;
6858  break;
6859  case SESSION_WRITE_CONFIG:
6860  rw_config->uHWAccessField.ui16WriteFrameId = frame_id;
6861  break;
6862  default:
6863  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown config type %d, return\n",
6864  __func__, rw_type);
6865  retval = NI_RETCODE_INVALID_PARAM;
6866  LRETURN;
6867  }
6868 
6869  switch(rw_type)
6870  {
6871  case SESSION_READ_CONFIG:
6872  ui32LBA = CONFIG_SESSION_Read_W(p_ctx->session_id);
6873  break;
6874  case SESSION_WRITE_CONFIG:
6875  ui32LBA = CONFIG_SESSION_Write_W(p_ctx->session_id);
6876  break;
6877  default:
6878  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown config type %d, return\n",
6879  __func__, rw_type);
6880  retval = NI_RETCODE_INVALID_PARAM;
6881  LRETURN;
6882  }
6883  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, buffer_size, ui32LBA) < 0)
6884  {
6885  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6887  LRETURN;
6888  }
6889 
6890 END:
6891 
6892  ni_aligned_free(p_buffer);
6893  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6894 
6895  return retval;
6896 }
6897 
6898 /*!******************************************************************************
6899  * \brief Send a p_config command for Start Of Stream
6900  *
6901  * \param ni_session_context_t p_ctx - xcoder Context
6902  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6903  *
6904  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION. NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6905  *******************************************************************************/
6907 {
6909  uint32_t ui32LBA = 0;
6910 
6911  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6912 
6913  if (!p_ctx)
6914  {
6915  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6916  __func__);
6917  retval = NI_RETCODE_INVALID_PARAM;
6918  LRETURN;
6919  }
6920 
6921  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
6922  NI_DEVICE_TYPE_ENCODER == device_type))
6923  {
6924  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6925  __func__, device_type);
6926  retval = NI_RETCODE_INVALID_PARAM;
6927  LRETURN;
6928  }
6929 
6930  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6931  {
6932  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6933  __func__);
6935  LRETURN;
6936  }
6937 
6938  ui32LBA = CONFIG_INSTANCE_SetSOS_W(p_ctx->session_id, device_type);
6939 
6940  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_ctx->p_all_zero_buf, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
6941  {
6942  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6944  }
6945 
6946 END:
6947 
6948  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6949  return retval;
6950 }
6951 
6952 /*!******************************************************************************
6953  * \brief Send a p_config command for End Of Stream
6954  *
6955  * \param ni_session_context_t p_ctx - xcoder Context
6956  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6957  *
6958  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6959  *******************************************************************************/
6961 {
6963  uint32_t ui32LBA = 0;
6964 
6965  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6966 
6967  if (!p_ctx)
6968  {
6969  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6970  __func__);
6971  retval = NI_RETCODE_INVALID_PARAM;
6972  LRETURN;
6973  }
6974 
6975  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
6976  NI_DEVICE_TYPE_ENCODER == device_type))
6977  {
6978  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6979  __func__, device_type);
6980  retval = NI_RETCODE_INVALID_PARAM;
6981  LRETURN;
6982  }
6983 
6984  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6985  {
6986  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6987  __func__);
6989  LRETURN;
6990  }
6991 
6992  ui32LBA = CONFIG_INSTANCE_SetEOS_W(p_ctx->session_id, device_type);
6993 
6994  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_ctx->p_all_zero_buf, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
6995  {
6996  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6998  }
6999 
7000 END:
7001 
7002  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7003 
7004  return retval;
7005 }
7006 
7007 /*!******************************************************************************
7008  * \brief Send a p_config command to flush the stream
7009  *
7010  * \param ni_session_context_t p_ctx - xcoder Context
7011  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
7012  *
7013  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7014  *******************************************************************************/
7016 {
7018  uint32_t ui32LBA = 0;
7019 
7020  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7021 
7022  if (!p_ctx)
7023  {
7024  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7025  __func__);
7026  retval = NI_RETCODE_INVALID_PARAM;
7027  LRETURN;
7028  }
7029 
7030  if (NI_DEVICE_TYPE_DECODER != device_type)
7031  {
7032  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
7033  __func__, device_type);
7034  retval = NI_RETCODE_INVALID_PARAM;
7035  LRETURN;
7036  }
7037 
7038  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7039  {
7040  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7041  __func__);
7043  LRETURN;
7044  }
7045 
7046  ui32LBA = CONFIG_INSTANCE_Flush_W(p_ctx->session_id, device_type);
7047  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
7049  ui32LBA);
7050  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
7051  p_ctx->device_type, p_ctx->hw_id,
7052  &(p_ctx->session_id), OPT_1);
7053  CHECK_VPU_RECOVERY(retval);
7054 
7055 END:
7056 
7057  if (NI_RETCODE_SUCCESS != retval)
7058  {
7060  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed with %d\n", __func__, retval);
7061  }
7062 
7063  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7064 
7065  return retval;
7066 }
7067 
7068 /*!******************************************************************************
7069  * \brief Send a p_config command to set the length for the incoming write packet
7070  *
7071  * \param ni_session_context_t p_ctx - xcoder Context
7072  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
7073  * \param
7074  *
7075  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7076  *******************************************************************************/
7078 {
7080  uint32_t ui32LBA = 0;
7081  void * p_buffer = NULL;
7082  uint32_t buffer_size = 0;
7083 
7084  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7085 
7086  if (!p_ctx)
7087  {
7088  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7089  __func__);
7090  retval = NI_RETCODE_INVALID_PARAM;
7091  LRETURN;
7092  }
7093 
7094  if (!(NI_DEVICE_TYPE_DECODER == device_type ||
7095  NI_DEVICE_TYPE_ENCODER == device_type))
7096  {
7097  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
7098  __func__, device_type);
7099  retval = NI_RETCODE_INVALID_PARAM;
7100  LRETURN;
7101  }
7102 
7103  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7104  {
7105  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7106  __func__);
7108  LRETURN;
7109  }
7110 
7111  buffer_size = ((sizeof(len) + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
7112  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), buffer_size))
7113  {
7114  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7115  NI_ERRNO, __func__);
7116  retval = NI_RETCODE_ERROR_MEM_ALOC;
7117  LRETURN;
7118  }
7119  memset(p_buffer, 0, buffer_size);
7120  memcpy(p_buffer, &len, sizeof(len));
7121 
7122  ui32LBA = CONFIG_INSTANCE_SetPktSize_W(p_ctx->session_id, device_type);
7123 
7124  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, buffer_size, ui32LBA) < 0)
7125  {
7126  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
7128  }
7129 
7130 END:
7131 
7132  ni_aligned_free(p_buffer);
7133  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7134 
7135  return retval;
7136 }
7137 
7138 /*!******************************************************************************
7139  * \brief Send a p_config command to inform encoder sequence change
7140  *
7141  * \param ni_session_context_t p_ctx - xcoder Context
7142  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
7143  * \param ni_resolution_t p_resolution - sequence change resolution
7144  *
7145  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7146  *******************************************************************************/
7148 {
7150  uint32_t ui32LBA = 0;
7151  void * p_buffer = NULL;
7152  uint32_t buffer_size = 0;
7153 
7154  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7155 
7156  if (!p_ctx)
7157  {
7158  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7159  __func__);
7160  retval = NI_RETCODE_INVALID_PARAM;
7161  LRETURN;
7162  }
7163 
7164  if (!(NI_DEVICE_TYPE_ENCODER == device_type))
7165  {
7166  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Seq Change not supported for device type %d, return\n", device_type);
7167  retval = NI_RETCODE_INVALID_PARAM;
7168  LRETURN;
7169  }
7170 
7171  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7172  {
7173  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7174  __func__);
7176  LRETURN;
7177  }
7178 
7180  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), buffer_size))
7181  {
7182  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7183  NI_ERRNO, __func__);
7184  retval = NI_RETCODE_ERROR_MEM_ALOC;
7185  LRETURN;
7186  }
7187  memset(p_buffer, 0, buffer_size);
7188  memcpy(p_buffer, p_resolution, sizeof(ni_resolution_t));
7189 
7190  ui32LBA = CONFIG_INSTANCE_SetSeqChange_W(p_ctx->session_id, device_type);
7191 
7192  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, buffer_size, ui32LBA) < 0)
7193  {
7194  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
7196  }
7197 
7198 END:
7199 
7200  ni_aligned_free(p_buffer);
7201  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7202 
7203  return retval;
7204 }
7205 
7206 /*!******************************************************************************
7207  * \brief Send a p_config command to configure encoding parameters.
7208  *
7209  * \param ni_session_context_t p_ctx - xcoder Context
7210  *
7211  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7212  *******************************************************************************/
7214 {
7215  void* p_encoder_config = NULL;
7216  void* p_encoder_roi_qp_map = NULL;
7217  ni_encoder_config_t* p_cfg = NULL;
7218  uint32_t buffer_size = 0;
7220  uint32_t ui32LBA = 0;
7221  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7222 
7223  if (!p_ctx)
7224  {
7225  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7226  __func__);
7227  retval = NI_RETCODE_INVALID_PARAM;
7228  LRETURN;
7229  }
7230 
7231  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7232  {
7233  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7234  __func__);
7236  LRETURN;
7237  }
7238 
7239  if (ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rh") >= 0)
7240  {
7242  ni_xcoder_params_t *session_config = (ni_xcoder_params_t *)p_ctx->p_session_config;
7243  if (session_config->cfg_enc_params.customize_roi_qp_level > 64) {
7245  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
7246  if (ni_posix_memalign(&p_encoder_roi_qp_map, sysconf(_SC_PAGESIZE), buffer_size))
7247  {
7248  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7249  NI_ERRNO, __func__);
7250  retval = NI_RETCODE_ERROR_MEM_ALOC;
7251  LRETURN;
7252  }
7253  memset(p_encoder_roi_qp_map, 0, buffer_size);
7254  memcpy(p_encoder_roi_qp_map, session_config->customize_roi_qp_map,
7256  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
7257  p_encoder_roi_qp_map, buffer_size, ui32LBA);
7258  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
7259  p_ctx->device_type, p_ctx->hw_id,
7260  &(p_ctx->session_id), OPT_1);
7261  if (NI_RETCODE_SUCCESS != retval)
7262  {
7263  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
7264  //Close the session since we can't configure it
7265  retval = ni_encoder_session_close(p_ctx, 0);
7266  if (NI_RETCODE_SUCCESS != retval)
7267  {
7268  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_encoder_session_close failed: blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
7269  }
7271  LRETURN;
7272  }
7273  session_config->cfg_enc_params.customize_roi_qp_level -= 64;
7274  }
7275  }
7276 
7277  buffer_size = sizeof(ni_encoder_config_t);
7278  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
7279  if (ni_posix_memalign(&p_encoder_config, sysconf(_SC_PAGESIZE), buffer_size))
7280  {
7281  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7282  NI_ERRNO, __func__);
7283  retval = NI_RETCODE_ERROR_MEM_ALOC;
7284  LRETURN;
7285  }
7286  memset(p_encoder_config, 0, buffer_size);
7287 
7288  ni_set_custom_template(p_ctx, p_encoder_config, p_ctx->p_session_config);
7289  retval = ni_validate_custom_template(p_ctx, p_encoder_config, p_ctx->p_session_config, p_ctx->param_err_msg, sizeof(p_ctx->param_err_msg));
7290  if (NI_RETCODE_PARAM_WARN == retval)
7291  {
7292  ni_log2(p_ctx, NI_LOG_INFO, "WARNING: ni_validate_custom_template() . %s\n", p_ctx->param_err_msg);
7293  fflush(stdout);
7294  }
7295  else if (NI_RETCODE_SUCCESS != retval)
7296  {
7297  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_validate_custom_template() failed. %s\n", p_ctx->param_err_msg);
7298  fflush(stdout);
7299  retval = NI_RETCODE_INVALID_PARAM;
7300  LRETURN;
7301  }
7302  //configure the session here
7304 
7305  //Flip the bytes!! any param 16bits in size need ni_htons, 32bit need ni_htonl
7306  p_cfg = (ni_encoder_config_t*)p_encoder_config;
7307  p_cfg->i32picWidth = ni_htonl(p_cfg->i32picWidth);
7308  p_cfg->i32picHeight = ni_htonl(p_cfg->i32picHeight);
7309  p_cfg->i32meBlkMode = ni_htonl(p_cfg->i32meBlkMode);
7310  p_cfg->i32frameRateInfo = ni_htonl(p_cfg->i32frameRateInfo);
7311  p_cfg->i32vbvBufferSize = ni_htonl(p_cfg->i32vbvBufferSize);
7312  p_cfg->i32userQpMax = ni_htonl(p_cfg->i32userQpMax);
7313  p_cfg->i32maxIntraSize = ni_htonl(p_cfg->i32maxIntraSize);
7314  p_cfg->i32userMaxDeltaQp = ni_htonl(p_cfg->i32userMaxDeltaQp);
7315  p_cfg->i32userMinDeltaQp = ni_htonl(p_cfg->i32userMinDeltaQp);
7316  p_cfg->i32userQpMin = ni_htonl(p_cfg->i32userQpMin);
7317  p_cfg->i32bitRate = ni_htonl(p_cfg->i32bitRate);
7318  p_cfg->i32bitRateBL = ni_htonl(p_cfg->i32bitRateBL);
7319  p_cfg->i32srcBitDepth = ni_htonl(p_cfg->i32srcBitDepth);
7320  p_cfg->hdrEnableVUI = ni_htonl(p_cfg->hdrEnableVUI);
7321  p_cfg->ui32VuiDataSizeBits = ni_htonl(p_cfg->ui32VuiDataSizeBits);
7322  p_cfg->ui32VuiDataSizeBytes = ni_htonl(p_cfg->ui32VuiDataSizeBytes);
7323  p_cfg->i32hwframes = ni_htonl(p_cfg->i32hwframes);
7324  p_cfg->ui16HDR10MaxLight = ni_htons(p_cfg->ui16HDR10MaxLight);
7325  p_cfg->ui16HDR10AveLight = ni_htons(p_cfg->ui16HDR10AveLight);
7326  p_cfg->ui16gdrDuration = ni_htons(p_cfg->ui16gdrDuration);
7327  p_cfg->ui32ltrRefInterval = ni_htonl(p_cfg->ui32ltrRefInterval);
7328  p_cfg->i32ltrRefQpOffset = ni_htonl(p_cfg->i32ltrRefQpOffset);
7329  p_cfg->ui32ltrFirstGap = ni_htonl(p_cfg->ui32ltrFirstGap);
7330  p_cfg->i32tolCtbRcInter = ni_htonl(p_cfg->i32tolCtbRcInter);
7331  p_cfg->i32tolCtbRcIntra = ni_htonl(p_cfg->i32tolCtbRcIntra);
7332  p_cfg->i16bitrateWindow = ni_htons(p_cfg->i16bitrateWindow);
7333  p_cfg->ui16hdr10_dx0 = ni_htons(p_cfg->ui16hdr10_dx0);
7334  p_cfg->ui16hdr10_dy0 = ni_htons(p_cfg->ui16hdr10_dy0);
7335  p_cfg->ui16hdr10_dx1 = ni_htons(p_cfg->ui16hdr10_dx1);
7336  p_cfg->ui16hdr10_dy1 = ni_htons(p_cfg->ui16hdr10_dy1);
7337  p_cfg->ui16hdr10_dx2 = ni_htons(p_cfg->ui16hdr10_dx2);
7338  p_cfg->ui16hdr10_dy2 = ni_htons(p_cfg->ui16hdr10_dy2);
7339  p_cfg->ui16hdr10_wx = ni_htons(p_cfg->ui16hdr10_wx);
7340  p_cfg->ui16hdr10_wy = ni_htons(p_cfg->ui16hdr10_wy);
7341  p_cfg->ui32hdr10_maxluma = ni_htonl(p_cfg->ui32hdr10_maxluma);
7342  p_cfg->ui32hdr10_minluma = ni_htonl(p_cfg->ui32hdr10_minluma);
7343  p_cfg->ui32lumaLinesize = ni_htons(p_cfg->ui32lumaLinesize);
7344  p_cfg->ui32chromaLinesize = ni_htons(p_cfg->ui32chromaLinesize);
7345 
7346  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
7347  p_encoder_config, buffer_size, ui32LBA);
7348  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
7349  p_ctx->device_type, p_ctx->hw_id,
7350  &(p_ctx->session_id), OPT_1);
7351  if (NI_RETCODE_SUCCESS != retval)
7352  {
7353  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
7354  //Close the session since we can't configure it
7355  retval = ni_encoder_session_close(p_ctx, 0);
7356  if (NI_RETCODE_SUCCESS != retval)
7357  {
7358  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_encoder_session_close failed: blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
7359  }
7360 
7362  }
7363 
7364 END:
7365 
7366  ni_aligned_free(p_encoder_config);
7367  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7368  return retval;
7369 }
7370 
7371 /*!******************************************************************************
7372  * \brief Send a p_config command to configure encoding p_frame parameters.
7373  *
7374  * \param ni_session_context_t p_ctx - xcoder Context
7375  * \param ni_encoder_frame_params_t * params - pointer to the encoder ni_encoder_frame_params_t struct
7376  *
7377  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7378  *******************************************************************************/
7380 {
7382  uint32_t buffer_size = sizeof(ni_encoder_frame_params_t);
7384  uint32_t ui32LBA = 0;
7385 
7386  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7387 
7388  if (!p_ctx || !p_params)
7389  {
7390  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7391  __func__);
7392  return NI_RETCODE_INVALID_PARAM;
7393  }
7394 
7395  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7396  {
7397  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7398  __func__);
7400  }
7401 
7402  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
7403  if (ni_posix_memalign((void **)&p_cfg, sysconf(_SC_PAGESIZE), buffer_size))
7404  {
7405  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7406  NI_ERRNO, __func__);
7408  }
7409 
7410  //configure the session here
7412 
7413  //Flip the bytes!!
7414  p_cfg->force_picture_type = ni_htons(p_params->force_picture_type);
7415  p_cfg->data_format = ni_htons(p_params->data_format);
7416  p_cfg->picture_type = ni_htons(p_params->picture_type);
7417  p_cfg->video_width = ni_htons(p_params->video_width);
7418  p_cfg->video_height = ni_htons(p_params->video_height);
7419  p_cfg->timestamp = ni_htonl(p_params->timestamp);
7420 
7421  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_cfg,
7422  buffer_size, ui32LBA) < 0)
7423  {
7424  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64 ", hw_id, %d, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
7425  //Close the session since we can't configure it
7426  retval = ni_encoder_session_close(p_ctx, 0);
7427  if (NI_RETCODE_SUCCESS != retval)
7428  {
7429  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_encoder_session_close failed: blk_io_handle: %" PRIx64 ", hw_id, %d, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
7430  }
7431 
7433  }
7434 
7435  ni_aligned_free(p_cfg);
7436  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7437 
7438  return retval;
7439 }
7440 
7441 /*!******************************************************************************
7442  * \brief Get info from received p_frame
7443  *
7444  * \param
7445  *
7446  * \return
7447  *******************************************************************************/
7448 int ni_create_frame(ni_frame_t* p_frame, uint32_t read_length, uint64_t* p_frame_offset, bool is_hw_frame)
7449 {
7450  uint32_t rx_size =
7451  read_length; //get the length since its the only thing in DW10 now
7452 
7453  if (!p_frame || !p_frame_offset)
7454  {
7455  ni_log(NI_LOG_ERROR, "ERROR: %s(): Null pointer parameters passed\n",
7456  __func__);
7457  return NI_RETCODE_INVALID_PARAM;
7458  }
7459 
7460  *p_frame_offset = 0;
7461 
7462  unsigned int metadata_size = NI_FW_META_DATA_SZ -
7464  unsigned int video_data_size = p_frame->data_len[0] + p_frame->data_len[1] +
7465  p_frame->data_len[2] + ((is_hw_frame) ? p_frame->data_len[3] : 0);
7466  ni_log(NI_LOG_DEBUG, "rx_size = %d metadataSize = %d\n", rx_size,
7467  metadata_size);
7468 
7469  p_frame->p_custom_sei_set = NULL;
7470 
7471  if (rx_size == metadata_size)
7472  {
7473  video_data_size = 0;
7474  }
7475 
7476  if (rx_size > video_data_size)
7477  {
7478  ni_metadata_dec_frame_t *p_meta =
7479  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
7480  video_data_size);
7481 
7482  *p_frame_offset = p_meta->metadata_common.ui64_data.frame_offset;
7483  rx_size -= metadata_size;
7484  p_frame->crop_top = p_meta->metadata_common.crop_top;
7485  p_frame->crop_bottom = p_meta->metadata_common.crop_bottom;
7486  p_frame->crop_left = p_meta->metadata_common.crop_left;
7487  p_frame->crop_right = p_meta->metadata_common.crop_right;
7488  p_frame->ni_pict_type = p_meta->metadata_common.frame_type;
7489 
7490  p_frame->video_width = p_meta->metadata_common.frame_width;
7491  p_frame->video_height = p_meta->metadata_common.frame_height;
7492 
7493  ni_log(
7494  NI_LOG_DEBUG,
7495  "%s: [metadata] cropRight=%u, cropLeft=%u, "
7496  "cropBottom=%u, cropTop=%u, frame_offset=%" PRIu64 ", pic=%ux%u, "
7497  "pict_type=%d, crop=%ux%u, sei header: 0x%0x number %u size %u\n",
7498  __func__, p_frame->crop_right, p_frame->crop_left,
7499  p_frame->crop_bottom, p_frame->crop_top,
7501  p_meta->metadata_common.frame_width,
7502  p_meta->metadata_common.frame_height, p_frame->ni_pict_type,
7503  p_frame->crop_right - p_frame->crop_left,
7504  p_frame->crop_bottom - p_frame->crop_top, p_meta->sei_header,
7505  p_meta->sei_number, p_meta->sei_size);
7506 
7507  p_frame->sei_total_len = 0;
7508  p_frame->sei_cc_offset = 0;
7509  p_frame->sei_cc_len = 0;
7514  p_frame->sei_hdr_plus_offset = 0;
7515  p_frame->sei_hdr_plus_len = 0;
7516  p_frame->sei_user_data_unreg_offset = 0;
7517  p_frame->sei_user_data_unreg_len = 0;
7518 
7519  if (p_meta->sei_number)
7520  {
7521 #if 1 // QUADRA_SEI_FMT
7522  ni_log(NI_LOG_DEBUG, "ui32SeiHeader 0x%x ui16SeiNumber %d ui16SeiSize %d SEI 0x%02x%02x\n",
7523  p_meta->sei_header, p_meta->sei_number, p_meta->sei_size,
7524  *((uint8_t*)p_meta + metadata_size),
7525  *((uint8_t*)p_meta + metadata_size+1));
7526 
7527  { // retrieve sei from new format
7528  uint16_t ui16SeiProcessed = 0;
7529  ni_sei_header_t * pEntryHeader = &p_meta->first_sei_header;
7530  uint32_t ui32Offset = 0;
7531  uint32_t ui32Size;
7532 
7533  rx_size -= p_meta->sei_size;
7534 
7535  do
7536  {
7537  ui16SeiProcessed++;
7538 
7539  if (pEntryHeader->status)
7540  {
7541  ui32Size = pEntryHeader->size;
7542  }
7543  else
7544  {
7545  ui32Size = 0;
7546  }
7547 
7548  ni_log(NI_LOG_DEBUG, "SEI #%x st %d size %d ty %d sz/of %u/%u 0x%02x%02x\n",
7549  ui16SeiProcessed, pEntryHeader->status,
7550  pEntryHeader->size, pEntryHeader->type, ui32Size,
7551  ui32Offset,
7552  *((uint8_t *)p_meta + metadata_size + ui32Offset),
7553  *((uint8_t *)p_meta + metadata_size + ui32Offset + 1));
7554 
7555  // - if multiple entries with same SEI type/subtype, only the last entry is saved;
7556  // consider to use sei_offset[] array instead of explicit sei_*_offset
7557  // - user_data_unreg (UDU) and custom sei passthru via HW and SW respectively
7558  // thus custom SEI is not processed here as it is by SW.
7559 
7560  switch(pEntryHeader->type)
7561  {
7562  case 1: //SEI_TYPE_PIC_TIMING
7564  "%s: SEI message dropped (unsupported - check "
7565  "decoder SEI bitmap settings);"
7566  " type %u size %u status %u offset %u payload bytes %u\n",
7567  __func__, pEntryHeader->type, pEntryHeader->size,
7568  pEntryHeader->status, ui32Offset, ui32Size);
7569  break;
7570  case 4: //HEVC_SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35
7571  if (ui32Size)
7572  {
7573  uint8_t *ptr = (uint8_t*)p_meta + metadata_size + ui32Offset;
7574  if(ptr[0] == NI_HDR10P_SEI_BYTE0 && ptr[1] == NI_HDR10P_SEI_BYTE1 &&
7575  ptr[2] == NI_HDR10P_SEI_BYTE2 && ptr[3] == NI_HDR10P_SEI_BYTE3 &&
7576  ptr[4] == NI_HDR10P_SEI_BYTE4 && ptr[5] == NI_HDR10P_SEI_BYTE5)
7577  {
7578  p_frame->sei_hdr_plus_len = ui32Size;
7579  p_frame->sei_hdr_plus_offset =
7580  video_data_size + metadata_size + ui32Offset;
7581 
7582  p_frame->sei_total_len += ui32Size;
7583 
7584  ni_log(NI_LOG_DEBUG, "%s: hdr10+ size=%u hdr10+ offset=%u\n",
7585  __func__, p_frame->sei_hdr_plus_len,
7586  p_frame->sei_hdr_plus_offset);
7587  }
7588  else if(ptr[0] == NI_CC_SEI_BYTE0 && ptr[1] == NI_CC_SEI_BYTE1 &&
7589  ptr[2] == NI_CC_SEI_BYTE2 && ptr[3] == NI_CC_SEI_BYTE3 &&
7590  ptr[4] == NI_CC_SEI_BYTE4 && ptr[5] == NI_CC_SEI_BYTE5 &&
7591  ptr[6] == NI_CC_SEI_BYTE6 && ptr[7] == NI_CC_SEI_BYTE7)
7592  {
7593  // Found CC data
7594  // number of 3 byte close captions is bottom 5 bits of
7595  // 9th byte of T35 payload
7596  // uint32_t ui32CCSize = (ptr[8] & 0x1F) * 3; // avoid overwriting ui32CCSize
7597 
7598  // return close caption data offset and length, and
7599  // skip past 10 header bytes to close caption data
7600  p_frame->sei_cc_len = (ptr[8] & 0x1F) * 3; // ui32CCSize;
7601  p_frame->sei_cc_offset = video_data_size + metadata_size
7602  + ui32Offset + 10;
7603 
7604  p_frame->sei_total_len += p_frame->sei_cc_len;
7605 
7607  "%s: close caption size %u ,"
7608  "offset %u = video size %u meta size %d off "
7609  " %u + 10\n",
7610  __func__, p_frame->sei_cc_len, p_frame->sei_cc_offset,
7611  video_data_size, metadata_size, ui32Offset);
7612  }
7613  else
7614  {
7616  "%s: unsupported T35; type %u size %u status %u "
7617  "offset %u\n",
7618  __func__, pEntryHeader->type, pEntryHeader->size,
7619  pEntryHeader->status, ui32Offset);
7620  }
7621  }
7622  else
7623  {
7625  "Error %s: T35 SEI dropped due to %s; type %u size %u status %u offset %u\n",
7626  __func__, pEntryHeader->status ? "missing payload" : "payload size exceeded SEI buffer size",
7627  pEntryHeader->type, pEntryHeader->size, pEntryHeader->status, ui32Offset);
7628  }
7629  break;
7630 
7631  case 5: // HEVC_SEI_TYPE_USER_DATA_UNREGISTERED
7632  // set offset now so len=0 will signify an error if this SEI is dropped
7633  p_frame->sei_user_data_unreg_offset = video_data_size + metadata_size + ui32Offset;
7634  if (ui32Size)
7635  {
7636  p_frame->sei_user_data_unreg_len = ui32Size;
7637  p_frame->sei_total_len += ui32Size;
7638  ni_log(NI_LOG_DEBUG, "User Data Unreg size = %u\n", ui32Size);
7639  }
7640  else
7641  {
7642  p_frame->sei_user_data_unreg_len = 0; // in case there are multiple UDU SEI entries
7644  "Error %s: User Data Unreg dropped due to %s; type %u size %u status %u offset %u\n",
7645  __func__, pEntryHeader->status ? "missing payload" : "payload size exceeded SEI buffer size",
7646  pEntryHeader->type, pEntryHeader->size, pEntryHeader->status, ui32Offset);
7647  }
7648  break;
7649 
7650  case 137: //HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO
7651  if (ui32Size)
7652  {
7653  p_frame->sei_hdr_mastering_display_color_vol_len = ui32Size;
7655  video_data_size + metadata_size + ui32Offset;
7656 
7657  p_frame->sei_total_len += ui32Size;
7660  *)((uint8_t *)p_meta + metadata_size + ui32Offset);
7661 
7662  ni_log(NI_LOG_DEBUG, "Display Primaries x[0]=%u y[0]=%u\n",
7663  ni_ntohs(pColourVolume->display_primaries[0][0]),
7664  ni_ntohs(pColourVolume->display_primaries[0][1]));
7665  ni_log(NI_LOG_DEBUG, "Display Primaries x[1]=%u y[1]=%u\n",
7666  ni_ntohs(pColourVolume->display_primaries[1][0]),
7667  ni_ntohs(pColourVolume->display_primaries[1][1]));
7668  ni_log(NI_LOG_DEBUG, "Display Primaries x[2]=%u y[2]=%u\n",
7669  ni_ntohs(pColourVolume->display_primaries[2][0]),
7670  ni_ntohs(pColourVolume->display_primaries[2][1]));
7671 
7672  ni_log(NI_LOG_DEBUG, "White Point x=%u y=%u\n",
7673  ni_ntohs(pColourVolume->white_point_x),
7674  ni_ntohs(pColourVolume->white_point_y));
7675  ni_log(NI_LOG_DEBUG, "Display Mastering Lum, Max=%u Min=%u\n",
7676  ni_ntohl(pColourVolume->max_display_mastering_luminance),
7677  ni_ntohl(pColourVolume->min_display_mastering_luminance));
7678  }
7679  else
7680  {
7682  "Error %s: mastering display info dropped due to %s; type %u size %u status %u offset %u\n",
7683  __func__, pEntryHeader->status ? "missing payload" : "payload size exceeded SEI buffer size",
7684  pEntryHeader->type, pEntryHeader->size, pEntryHeader->status, ui32Offset);
7685  }
7686  break;
7687 
7688  case 144: //HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO
7689  if (ui32Size)
7690  {
7691  p_frame->sei_hdr_content_light_level_info_len = ui32Size;
7693  video_data_size + metadata_size + ui32Offset;
7694 
7695  p_frame->sei_total_len += ui32Offset;
7696 
7699  (uint8_t*)p_meta + metadata_size + ui32Offset);
7700 
7701  ni_log(NI_LOG_DEBUG, "Max Content Light level=%u Max Pic Avg Light Level=%u\n",
7702  ni_ntohs(pLightLevel->max_content_light_level),
7703  ni_ntohs(pLightLevel->max_pic_average_light_level));
7704  }
7705  else
7706  {
7708  "Error %s: content light level info dropped due to %s; type %u size %u status %u offset %u\n",
7709  __func__, pEntryHeader->status ? "missing payload" : "payload size exceeded SEI buffer size",
7710  pEntryHeader->type, pEntryHeader->size, pEntryHeader->status, ui32Offset);
7711  }
7712  break;
7713 
7714  case 200: // Custom SEI not included in HEVC_SEI_Type
7715  if (ui32Size)
7716  {
7717  p_frame->vui_len = ui32Size;
7718  p_frame->vui_offset =
7719  video_data_size + metadata_size + ui32Offset;
7720  p_frame->sei_total_len += ui32Size;
7721  }
7722  break;
7723  case 206: // Customer reset ppu resolution, need dropped
7724  ni_log(NI_LOG_DEBUG, "Custom SEI PPU_RECONFIG dropped", __func__);
7725  break;
7726  default:
7728  "Warning %s: SEI message dropped (unsupported - check "
7729  "decoder SEI bitmap settings);"
7730  " type %u size %u status %u offset %u payload bytes %u\n",
7731  __func__, pEntryHeader->type, pEntryHeader->size,
7732  pEntryHeader->status, ui32Offset, ui32Size);
7733  break;
7734  }
7735  ui32Offset += ui32Size;
7736  pEntryHeader = (ni_sei_header_t *)((uint8_t*)p_meta + metadata_size + ui32Offset);
7737  ui32Offset += sizeof(ni_sei_header_t);
7738  } while (ui32Offset <= p_meta->sei_size && ui16SeiProcessed < p_meta->sei_number);
7739 
7740  if (p_meta->sei_number != ui16SeiProcessed)
7741  {
7742  ni_log(
7743  NI_LOG_ERROR,
7744  "Error %s: number of SEI messages reported %u != processed %u\n",
7745  __func__, p_meta->sei_number, ui16SeiProcessed);
7746  }
7747  }
7748 
7749 #else // QUADRA_SEI_FMT
7750 
7751  // ni_assert(p_meta->sei_header);
7752  // else // backward compatibility
7753  {
7754  ni_sei_user_data_entry_t *pEntry;
7755  uint32_t ui32CCOffset = 0, ui32CCSize = 0;
7756 
7757  rx_size -= p_meta->sei_size;
7758 
7759  pEntry = (ni_sei_user_data_entry_t *)((uint8_t*)p_meta + metadata_size);
7760 
7761  if (find_t35_sei(p_meta->sei_header, NI_T35_SEI_HDR10_PLUS, pEntry,
7762  &ui32CCOffset, &ui32CCSize))
7763  {
7764  p_frame->sei_hdr_plus_len = ui32CCSize;
7765  p_frame->sei_hdr_plus_offset =
7766  video_data_size + metadata_size + ui32CCOffset;
7767 
7768  p_frame->sei_total_len += ui32CCSize;
7769 
7770  ni_log(NI_LOG_DEBUG, "%s: hdr10+ size=%u hdr10+ offset=%u\n", __func__,
7771  p_frame->sei_hdr_plus_len, p_frame->sei_hdr_plus_offset);
7772  }
7773  else
7774  {
7775  ni_log(NI_LOG_DEBUG, "%s: hdr+ NOT found in meta data!\n", __func__);
7776  }
7777 
7778  if (find_t35_sei(p_meta->sei_header, NI_T35_SEI_CLOSED_CAPTION, pEntry,
7779  &ui32CCOffset, &ui32CCSize))
7780  {
7781  uint8_t *ptr;
7782  // Found CC data at pEntry + ui32CCOffset
7783  ptr = (uint8_t*)pEntry + ui32CCOffset;
7784  // number of 3 byte close captions is bottom 5 bits of
7785  // 9th byte of T35 payload
7786  ui32CCSize = (ptr[8] & 0x1F) * 3;
7787 
7788  // return close caption data offset and length, and
7789  // skip past 10 header bytes to close caption data
7790  p_frame->sei_cc_len = ui32CCSize;
7791  p_frame->sei_cc_offset = video_data_size + metadata_size
7792  + ui32CCOffset + 10;
7793 
7794  p_frame->sei_total_len += p_frame->sei_cc_len;
7795 
7797  "%s: close caption size %u ,"
7798  "offset %u = video size %u meta size %u off "
7799  " %u + 10\n",
7800  __func__, p_frame->sei_cc_len, p_frame->sei_cc_offset,
7801  video_data_size, metadata_size, ui32CCOffset);
7802  }
7803  else
7804  {
7805  ni_log(NI_LOG_DEBUG, "%s: close caption NOT found in meta data!\n",
7806  __func__);
7807  }
7808 
7809  if (find_sei(p_meta->sei_header, pEntry,
7811  &ui32CCOffset, &ui32CCSize))
7812  {
7813  p_frame->sei_hdr_mastering_display_color_vol_len = ui32CCSize;
7815  video_data_size + metadata_size + ui32CCOffset;
7816 
7817  p_frame->sei_total_len += ui32CCSize;
7818 
7819  ni_dec_mastering_display_colour_volume_t* pColourVolume =
7820  (ni_dec_mastering_display_colour_volume_t*)((uint8_t*)pEntry + ui32CCOffset);
7821 
7822  ni_log(NI_LOG_DEBUG, "Display Primaries x[0]=%u y[0]=%u\n",
7823  pColourVolume->display_primaries_x[0],
7824  pColourVolume->display_primaries_y[0]);
7825  ni_log(NI_LOG_DEBUG, "Display Primaries x[1]=%u y[1]=%u\n",
7826  pColourVolume->display_primaries_x[1],
7827  pColourVolume->display_primaries_y[1]);
7828  ni_log(NI_LOG_DEBUG, "Display Primaries x[2]=%u y[2]=%u\n",
7829  pColourVolume->display_primaries_x[2],
7830  pColourVolume->display_primaries_y[2]);
7831 
7832  ni_log(NI_LOG_DEBUG, "White Point x=%u y=%u\n",
7833  pColourVolume->white_point_x,
7834  pColourVolume->white_point_y);
7835  ni_log(NI_LOG_DEBUG, "Display Mastering Lum, Max=%u Min=%u\n",
7836  pColourVolume->max_display_mastering_luminance, pColourVolume->min_display_mastering_luminance);
7837  }
7838  if (find_sei(p_meta->sei_header, pEntry,
7840  &ui32CCOffset, &ui32CCSize))
7841  {
7842  p_frame->sei_hdr_content_light_level_info_len = ui32CCSize;
7844  video_data_size + metadata_size + ui32CCOffset;
7845 
7846  p_frame->sei_total_len += ui32CCSize;
7847 
7848  ni_content_light_level_info_t* pLightLevel =
7849  (ni_content_light_level_info_t*)((uint8_t*)pEntry + ui32CCOffset);
7850  ni_log(NI_LOG_DEBUG, "Max Content Light level=%u Max Pic Avg Light Level=%u\n",
7851  pLightLevel->max_content_light_level, pLightLevel->max_pic_average_light_level);
7852  }
7853 
7854  if (find_sei(p_meta->sei_header, pEntry,
7856  &ui32CCOffset, &ui32CCSize) ||
7857  find_sei(p_meta->sei_header, pEntry,
7859  &ui32CCOffset, &ui32CCSize))
7860  {
7861  p_frame->sei_user_data_unreg_len = ui32CCSize;
7862  p_frame->sei_user_data_unreg_offset =
7863  video_data_size + metadata_size + ui32CCOffset;
7864 
7865  p_frame->sei_total_len += ui32CCSize;
7866 
7867  ni_log(NI_LOG_DEBUG, "User Data Unreg size = %u\n", ui32CCSize);
7868  }
7869  }
7870 #endif // QUADRA_SEI_FMT
7871  if (0 == p_frame->sei_total_len)
7872  {
7873  ni_log(NI_LOG_DEBUG, "Warning retrieved 0 supported SEI !\n");
7874  }
7875  }
7876  }
7877 
7878  p_frame->dts = NI_NOPTS_VALUE;
7879  p_frame->pts = NI_NOPTS_VALUE;
7880  //p_frame->end_of_stream = isEndOfStream;
7881  p_frame->start_of_stream = 0;
7882 
7883  if (rx_size == 0)
7884  {
7885  p_frame->data_len[0] = 0;
7886  p_frame->data_len[1] = 0;
7887  p_frame->data_len[2] = 0;
7888  p_frame->data_len[3] = 0;
7889  }
7890 
7891  ni_log(NI_LOG_DEBUG, "received [0x%08x] data size: %d, end of stream=%u\n",
7892  read_length, rx_size, p_frame->end_of_stream);
7893 
7894  return rx_size;
7895 }
7896 
7897 /*!******************************************************************************
7898  * \brief Get info from received xcoder capability
7899  *
7900  * \param
7901  *
7902  * \return
7903  *******************************************************************************/
7905  ni_device_handle_t device_handle, bool device_in_ctxt)
7906 {
7907  int i, total_types = 0, total_modules = 0;
7908  ni_nvme_identity_t *p_id_data = (ni_nvme_identity_t *)p_data;
7909 
7910  COMPILE_ASSERT(sizeof(p_cap->xcoder_cnt) <= sizeof(p_id_data->xcoder_cnt) &&
7912  sizeof(p_id_data->xcoder_cnt[0]) ==
7913  sizeof(p_cap->xcoder_cnt));
7914 
7915  if (!p_cap || !p_data)
7916  {
7917  ni_log(NI_LOG_ERROR, "ERROR: %s(): Null pointer parameters passed\n",
7918  __func__);
7919  LRETURN;
7920  }
7921 
7922  if ((p_id_data->ui16Vid != NETINT_PCI_VENDOR_ID) ||
7923  (p_id_data->ui16Ssvid != NETINT_PCI_VENDOR_ID))
7924  {
7925  if (device_in_ctxt)
7926  {
7928  "ERROR: Previously in context device got an invalid vendor ID 0x%X SSVID 0x%X. Netint "
7929  "ID 0x%X. Retrying\n",
7930  p_id_data->ui16Vid, p_id_data->ui16Ssvid, NETINT_PCI_VENDOR_ID);
7931  //print some other fields as a test to see if they are invalid too
7932  ni_log(NI_LOG_ERROR, "Model Number: %.*s\n",
7933  (int)sizeof(p_id_data->ai8Sn), p_id_data->ai8Sn);
7934  ni_log(NI_LOG_ERROR, "Serial Number: %.*s\n",
7935  (int)sizeof(p_id_data->ai8Mn), p_id_data->ai8Mn);
7936  ni_log(NI_LOG_ERROR, "Firmware Revision: %.*s\n",
7937  (int)sizeof(p_id_data->ai8Fr), p_id_data->ai8Fr);
7938  ni_log(NI_LOG_ERROR, "xcoder_num_elements: %d\n",
7939  p_id_data->xcoder_num_elements);
7940 
7941  ni_event_handle_t event_handle = NI_INVALID_EVENT_HANDLE;
7942  uint32_t ui32LBA = IDENTIFY_DEVICE_R;
7943  if (ni_nvme_send_read_cmd(device_handle, event_handle, p_data,
7944  NI_NVME_IDENTITY_CMD_DATA_SZ, ui32LBA) < 0)
7945  {
7946  LRETURN;
7947  }
7948  if ((p_id_data->ui16Vid != NETINT_PCI_VENDOR_ID) ||
7949  (p_id_data->ui16Ssvid != NETINT_PCI_VENDOR_ID))
7950  {
7952  "ERROR: %s(): Retry got an invalid vendor ID too 0x%X SSVID "
7953  "0x%X!\n",
7954  __func__, p_id_data->ui16Vid, p_id_data->ui16Ssvid);
7955  //print some other fields as a test to see if they are invalid too
7956  ni_log(NI_LOG_ERROR, "Model Number: %.*s\n",
7957  (int)sizeof(p_id_data->ai8Sn), p_id_data->ai8Sn);
7958  ni_log(NI_LOG_ERROR, "Serial Number: %.*s\n",
7959  (int)sizeof(p_id_data->ai8Mn), p_id_data->ai8Mn);
7960  ni_log(NI_LOG_ERROR, "Firmware Revision: %.*s\n",
7961  (int)sizeof(p_id_data->ai8Fr), p_id_data->ai8Fr);
7962  ni_log(NI_LOG_ERROR, "xcoder_num_elements: %d\n",
7963  p_id_data->xcoder_num_elements);
7964  LRETURN;
7965  }
7966  else
7967  {
7969  "Retry got valid a vendor ID 0x%X SSVID 0x%X. Netint ID 0x%X\n",
7970  p_id_data->ui16Vid, p_id_data->ui16Ssvid, NETINT_PCI_VENDOR_ID);
7971  }
7972  }
7973  else
7974  {
7975  LRETURN;
7976  }
7977  }
7978 
7979  memcpy(p_cap->serial_number, p_id_data->ai8Sn, sizeof(p_cap->serial_number));
7980  memcpy(p_cap->model_number, p_id_data->ai8Mn, sizeof(p_cap->model_number));
7981 
7982  memset(p_cap->fw_rev, 0, sizeof(p_cap->fw_rev));
7983  memcpy(p_cap->fw_rev, p_id_data->ai8Fr, sizeof(p_cap->fw_rev));
7984  ni_log(NI_LOG_DEBUG, "F/W rev: %.*s\n", (int)sizeof(p_cap->fw_rev),
7985  p_cap->fw_rev);
7986 
7987  if (p_id_data->xcoder_num_elements)
7988  {
7989  ni_log(NI_LOG_DEBUG, "xcoder_num_elements: %d xcoder_num_devices: %d\n",
7990  p_id_data->xcoder_num_elements,
7991  p_id_data->xcoder_num_devices);
7992 
7993  for (i = 0; i < NI_DEVICE_TYPE_XCODER_MAX; i++)
7994  {
7995  if (p_id_data->xcoder_cnt[i])
7996  {
7997  total_types++;
7998  total_modules += p_id_data->xcoder_cnt[i];
7999  ni_log(NI_LOG_DEBUG, "type #%d: xcoder_cnt[%d] = %d\n", total_types, i,
8000  p_id_data->xcoder_cnt[i]);
8001  }
8002  }
8003 
8004  if (p_id_data->xcoder_num_elements != total_types ||
8005  p_id_data->xcoder_num_devices != total_modules)
8006  {
8008  "Error: mismatch; xcoder_num_elements: %d (calculated: %d) "
8009  "xcoder_num_devices: %d (calculated: %d)\n",
8010  p_id_data->xcoder_num_elements, total_types,
8011  p_id_data->xcoder_num_devices, total_modules);
8012  LRETURN;
8013  }
8014 
8016  p_cap->hw_elements_cnt = p_id_data->xcoder_num_elements;
8017  p_cap->xcoder_devices_cnt = p_id_data->xcoder_num_devices;
8018  memcpy(p_cap->xcoder_cnt, p_id_data->xcoder_cnt,
8019  NI_DEVICE_TYPE_XCODER_MAX * sizeof(p_id_data->xcoder_cnt[0]));
8020 
8022  {
8023  if (!p_id_data->xcoder_cnt[i])
8024  continue;
8025 
8026  p_cap->xcoder_devices[i].hw_id = p_id_data->xcoder_devices[i].hw_id;
8030  p_cap->xcoder_devices[i].codec_format =
8031  p_id_data->xcoder_devices[i].hw_codec_format;
8032  p_cap->xcoder_devices[i].codec_type =
8033  p_id_data->xcoder_devices[i].hw_codec_type;
8036  if (i == NI_DEVICE_TYPE_ENCODER)
8037  {
8040  }
8041  else
8042  {
8045  }
8046 
8047  p_cap->xcoder_devices[i].video_profile =
8048  p_id_data->xcoder_devices[i].hw_video_profile;
8049  p_cap->xcoder_devices[i].video_level =
8050  p_id_data->xcoder_devices[i].hw_video_level;
8051  }
8052 
8053  goto CAP_POPULATED;
8054  }
8055 
8056  p_cap->device_is_xcoder = p_id_data->device_is_xcoder;
8057  ni_log(NI_LOG_DEBUG, "device_is_xcoder: value in id cmd: %u\n",
8058  p_cap->device_is_xcoder);
8059  if (0 == p_cap->device_is_xcoder)
8060  {
8061  ni_log(NI_LOG_ERROR, "Not an xcoder device !\n");
8062 
8063  if (device_in_ctxt)
8064  {
8066  "ERROR: Previously in context device is not a xcoder device "
8067  "now!\n");
8068  }
8069  LRETURN;
8070  }
8071 
8072  p_cap->hw_elements_cnt = p_id_data->xcoder_num_hw;
8073 
8074  total_modules = p_cap->xcoder_cnt[NI_DEVICE_TYPE_DECODER] =
8075  p_id_data->xcoder_num_h264_decoder_hw +
8076  p_id_data->xcoder_num_h265_decoder_hw;
8077 
8079  p_id_data->xcoder_num_h264_encoder_hw +
8080  p_id_data->xcoder_num_h265_encoder_hw;
8081 
8082  total_modules += p_cap->xcoder_cnt[NI_DEVICE_TYPE_ENCODER];
8083 
8084  p_cap->xcoder_devices_cnt = total_modules;
8085 
8086  if (total_modules >= 1)
8087  {
8088  p_cap->xcoder_devices[0].hw_id = p_id_data->hw0_id;
8092  p_cap->xcoder_devices[0].codec_format = p_id_data->hw0_codec_format;
8093  p_cap->xcoder_devices[0].codec_type = p_id_data->hw0_codec_type;
8098  p_cap->xcoder_devices[0].video_profile = p_id_data->hw0_video_profile;
8099  p_cap->xcoder_devices[0].video_level = p_id_data->hw0_video_level;
8100  }
8101  if (total_modules >= 2)
8102  {
8103  p_cap->xcoder_devices[1].hw_id = p_id_data->hw1_id;
8107  p_cap->xcoder_devices[1].codec_format = p_id_data->hw1_codec_format;
8108  p_cap->xcoder_devices[1].codec_type = p_id_data->hw1_codec_type;
8113  p_cap->xcoder_devices[1].video_profile = p_id_data->hw1_video_profile;
8114  p_cap->xcoder_devices[1].video_level = p_id_data->hw1_video_level;
8115  }
8116  if (total_modules >= 3)
8117  {
8118  p_cap->xcoder_devices[2].hw_id = p_id_data->hw2_id;
8122  p_cap->xcoder_devices[2].codec_format = p_id_data->hw2_codec_format;
8123  p_cap->xcoder_devices[2].codec_type = p_id_data->hw2_codec_type;
8128  p_cap->xcoder_devices[2].video_profile = p_id_data->hw2_video_profile;
8129  p_cap->xcoder_devices[2].video_level = p_id_data->hw2_video_level;
8130  }
8131  if (total_modules >= 4)
8132  {
8133  p_cap->xcoder_devices[3].hw_id = p_id_data->hw3_id;
8137  p_cap->xcoder_devices[3].codec_format = p_id_data->hw3_codec_format;
8138  p_cap->xcoder_devices[3].codec_type = p_id_data->hw3_codec_type;
8143  p_cap->xcoder_devices[3].video_profile = p_id_data->hw3_video_profile;
8144  p_cap->xcoder_devices[3].video_level = p_id_data->hw3_video_level;
8145  }
8146 
8147 CAP_POPULATED:
8148 
8149  for (i = 0; i < NI_MAX_DEVICES_PER_HW_INSTANCE; i++)
8150  {
8151  ni_log(NI_LOG_DEBUG, "HW%d hw_id: %d\n", i, p_cap->xcoder_devices[i].hw_id);
8152  ni_log(NI_LOG_DEBUG, "HW%d max_number_of_contexts: %d\n", i,
8154  ni_log(NI_LOG_DEBUG, "HW%d max_4k_fps: %d\n", i,
8155  p_cap->xcoder_devices[i].max_4k_fps);
8156  ni_log(NI_LOG_DEBUG, "HW%d codec_format: %d\n", i,
8157  p_cap->xcoder_devices[i].codec_format);
8158  ni_log(NI_LOG_DEBUG, "HW%d codec_type: %d\n", i,
8159  p_cap->xcoder_devices[i].codec_type);
8160  ni_log(NI_LOG_DEBUG, "HW%d max_video_width: %d\n", i,
8161  p_cap->xcoder_devices[i].max_video_width);
8162  ni_log(NI_LOG_DEBUG, "HW%d max_video_height: %d\n", i,
8163  p_cap->xcoder_devices[i].max_video_height);
8164  ni_log(NI_LOG_DEBUG, "HW%d min_video_width: %d\n", i,
8165  p_cap->xcoder_devices[i].min_video_width);
8166  ni_log(NI_LOG_DEBUG, "HW%d min_video_height: %d\n", i,
8167  p_cap->xcoder_devices[i].min_video_height);
8168  ni_log(NI_LOG_DEBUG, "HW%d video_profile: %d\n", i,
8169  p_cap->xcoder_devices[i].video_profile);
8170  ni_log(NI_LOG_DEBUG, "HW%d video_level: %d\n", i,
8171  p_cap->xcoder_devices[i].video_level);
8172  }
8173 
8174  memset(p_cap->fw_branch_name, 0, sizeof(p_cap->fw_branch_name));
8175  memcpy(p_cap->fw_branch_name, p_id_data->fw_branch_name,
8176  sizeof(p_cap->fw_branch_name) - 1);
8177  ni_log(NI_LOG_DEBUG, "F/W branch name: %s\n", p_cap->fw_branch_name);
8178  memset(p_cap->fw_commit_time, 0, sizeof(p_cap->fw_commit_time));
8179  memcpy(p_cap->fw_commit_time, p_id_data->fw_commit_time,
8180  sizeof(p_cap->fw_commit_time) - 1);
8181  ni_log(NI_LOG_DEBUG, "F/W commit time: %s\n", p_cap->fw_commit_time);
8182  memset(p_cap->fw_commit_hash, 0, sizeof(p_cap->fw_commit_hash));
8183  memcpy(p_cap->fw_commit_hash, p_id_data->fw_commit_hash,
8184  sizeof(p_cap->fw_commit_hash) - 1);
8185  ni_log(NI_LOG_DEBUG, "F/W commit hash: %s\n", p_cap->fw_commit_hash);
8186  memset(p_cap->fw_build_time, 0, sizeof(p_cap->fw_build_time));
8187  memcpy(p_cap->fw_build_time, p_id_data->fw_build_time,
8188  sizeof(p_cap->fw_build_time) - 1);
8189  ni_log(NI_LOG_DEBUG, "F/W build time: %s\n", p_cap->fw_build_time);
8190  memset(p_cap->fw_build_id, 0, sizeof(p_cap->fw_build_id));
8191  memcpy(p_cap->fw_build_id, p_id_data->fw_build_id,
8192  sizeof(p_cap->fw_build_id) - 1);
8193  ni_log(NI_LOG_DEBUG, "F/W build id: %s\n", p_cap->fw_build_id);
8194 
8195 END:
8196  return;
8197 }
8198 
8199 static uint32_t presetGopSize[] = {
8200  1,
8201  1,
8202  1,
8203  1,
8204  2,
8205  4,
8206  4,
8207  4,
8208  8 };
8209 
8210 /*!******************************************************************************
8211  * \brief insert the 32 bits of integer value at bit position pos
8212  *
8213  * \param int pos, int value
8214  *
8215  * \return void
8216  ******************************************************************************/
8217 void ni_fix_VUI(uint8_t *vui, int pos, int value)
8218 {
8219  int pos_byte = (pos/8);
8220  int pos_in_byte = pos%8;
8221  int remaining_bytes_in_current_byte = 8 - pos_in_byte;
8222 
8223  if (pos_in_byte == 0) // at beginning of the byte
8224  {
8225  vui[pos_byte] = (uint8_t)(value >> 24);
8226  vui[pos_byte+1] = (uint8_t)(value >> 16);
8227  vui[pos_byte+2] = (uint8_t)(value >> 8);
8228  vui[pos_byte+3] = (uint8_t)(value);
8229  }
8230  else
8231  {
8232  vui[pos_byte] = vui[pos_byte] + (uint8_t)(value >> (32-remaining_bytes_in_current_byte));
8233  vui[pos_byte+1] = (uint8_t)(value >> (32-remaining_bytes_in_current_byte-8));
8234  vui[pos_byte+2] = (uint8_t)(value >> (32-remaining_bytes_in_current_byte-16));
8235  vui[pos_byte+3] = (uint8_t)(value >> (32-remaining_bytes_in_current_byte-24));
8236  vui[pos_byte+4] = vui[pos_byte+4] + ((uint8_t)(value << remaining_bytes_in_current_byte));
8237  }
8238 
8239 }
8240 
8241 /*!******************************************************************************
8242 * \brief Setup all xcoder configurations with custom parameters (Rev. B)
8243 *
8244 * \param
8245 *
8246 * \return
8247 ******************************************************************************/
8249  ni_decoder_config_t *p_cfg,
8250  ni_xcoder_params_t *p_src,
8251  uint32_t max_pkt_size)
8252 {
8253  int i,j;
8254  ni_decoder_input_params_t* p_dec = NULL;
8255  if ((!p_ctx) || (!p_cfg) || (!p_src))
8256  {
8257  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
8258  __func__);
8259  return;
8260  }
8261  p_dec = &p_src->dec_input_params;
8262  int w = p_src->source_width;
8263  int h = p_src->source_height;
8264  bool shift_params = false;
8265 
8266  p_cfg->ui8HWFrame = p_dec->hwframes;
8267  p_cfg->ui8MCMode = p_dec->mcmode;
8270  p_cfg->ui8DisablePictureReordering = p_dec->decoder_low_delay > 0;
8271  p_ctx->force_low_delay = p_dec->force_low_delay;
8272  p_cfg->ui8EnablelowDelayCheck = p_dec->enable_low_delay_check == 1;
8273  p_cfg->ui32SourceWidth = w;
8274  p_cfg->ui32SourceHeight = h;
8275 
8276  if (max_pkt_size)
8277  {
8278  p_cfg->ui32MaxPktSize = max_pkt_size;
8279  } else {
8280  // p_cfg->ui32MaxPktSize = width x height x 3/2 x min compression ratio(QP=0);
8281  // set min compression ratio = 1/2, so MaxPktSize = w * h * 3/4
8282  p_cfg->ui32MaxPktSize = w * h * 3 / 4;
8283  }
8284  // packet buffer aligned to NI_MAX_PACKET_SZ(128k)
8285  p_cfg->ui32MaxPktSize =
8286  (((p_cfg->ui32MaxPktSize) / NI_MAX_PACKET_SZ) + 1) * NI_MAX_PACKET_SZ;
8287 
8288  p_cfg->fps_number =
8289  ((ni_xcoder_params_t *)p_ctx->p_session_config)->fps_number;
8290  p_cfg->fps_denominator =
8291  ((ni_xcoder_params_t *)p_ctx->p_session_config)->fps_denominator;
8292  ni_log2(p_ctx, NI_LOG_INFO, "%s height %d width %d fps_number %d fps_denominator %d\n",
8293  __func__, h, w, p_cfg->fps_number, p_cfg->fps_denominator);
8294 
8295  p_cfg->asOutputConfig[0].ui8Enabled = 1; // always enabled
8296  p_cfg->asOutputConfig[1].ui8Enabled = p_dec->enable_out1;
8297  p_cfg->asOutputConfig[2].ui8Enabled = p_dec->enable_out2;
8298  if (p_cfg->asOutputConfig[2].ui8Enabled && p_cfg->asOutputConfig[1].ui8Enabled == 0)
8299  {
8300  p_cfg->asOutputConfig[1].ui8Enabled = 1;
8301  p_cfg->asOutputConfig[2].ui8Enabled = 0;
8302  shift_params = true;
8303  ni_log2(p_ctx, NI_LOG_DEBUG, "Output 2 used before output 1, Shifting output2 settings to output1 and disabling output 2\n");
8304  }
8305 
8306  for (i = 0; i < NI_MAX_NUM_OF_DECODER_OUTPUTS; i++)
8307  {
8308  if (!shift_params || i == 0)
8309  {
8310  j = i;
8311  }
8312  else
8313  {
8314  j = (i == 1) ? 2 : 1; //swap settings
8315  }
8316  p_cfg->asOutputConfig[i].ui8Force8Bit = p_dec->force_8_bit[j];
8317  p_cfg->asOutputConfig[i].ui8SemiPlanarEnabled = p_dec->semi_planar[j];
8318  p_cfg->asOutputConfig[i].ui8CropMode = p_dec->crop_mode[j];
8320  (uint16_t)((p_dec->crop_whxy[j][0]) & 0xFFFE);
8322  (uint16_t)((p_dec->crop_whxy[j][1]) & 0xFFFE);
8324  (uint16_t)((p_dec->crop_whxy[j][2]) & 0xFFFE);
8326  (uint16_t)((p_dec->crop_whxy[j][3]) & 0xFFFE);
8327 
8328  //Offset resized if out of bounds
8330  {
8332  }
8334  {
8336  }
8337 
8338  if (p_dec->enable_ppu_scale_adapt)
8339  {
8341  }
8342  else if (p_dec->scale_long_short_edge[j] == 1)
8343  {
8344  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt = 3;
8345  }
8346  else if (p_dec->scale_long_short_edge[j] == 2)
8347  {
8348  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt = 4;
8349  }
8350  else
8351  {
8352  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt = 0;
8353  }
8355 
8356  if (p_dec->scale_round[j] == -1)
8357  {
8358  if (p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt == 1 ||
8359  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt == 2)
8360  {
8361  p_cfg->asOutputConfig[i].ui8ScaleResCeil = p_dec->scale_resolution_ceil[j] - 1;
8362  }
8363  else
8364  {
8366  }
8367  }
8368  else if (p_dec->scale_round[j] == 0)
8369  {
8371  }
8372  else
8373  {
8374  p_cfg->asOutputConfig[i].ui8ScaleResCeil = p_dec->scale_resolution_ceil[j] - 1;
8375  }
8376 
8378  (uint16_t)((p_dec->scale_wh[j][0]+1) & 0xFFFE);
8380  (uint16_t)((p_dec->scale_wh[j][1]+1) & 0xFFFE);
8381 
8384  {
8385  p_cfg->asOutputConfig[i].ui8ScaleEnabled = 1;
8386  }
8387  else
8388  {
8389  p_cfg->asOutputConfig[i].ui8ScaleEnabled = 0;
8390  }
8391  }
8392 
8394  if (p_cfg->ui8MaxExtraHwFrameCnt != 255 &&
8395  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rB") < 0)
8396  {
8397  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): maxExtraHwFrameCnt is not support for FW < 6rB\n", __func__);
8398  }
8399  p_cfg->ui8EcPolicy = p_dec->ec_policy;
8400  p_cfg->ui8EnableAdvancedEc = p_dec->enable_advanced_ec;
8402  if (p_cfg->ui8EnableAdvancedEc == 2 &&
8403  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rO") < 0)
8404  {
8405  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): (enableAdvancedEc == 2) is not support for FW < 6rO\n", __func__);
8406  p_cfg->ui8EnableAdvancedEc = 1;
8407  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): reset enableAdvancedEc to %d\n", __func__, p_cfg->ui8EnableAdvancedEc);
8408  }
8409  if (p_cfg->ui8EcPolicy == NI_EC_POLICY_LIMITED_ERROR &&
8410  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6ri") < 0)
8411  {
8412  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): (EcPolicy == limited_error) not supported for FW < 6ri\n", __func__);
8414  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): reset EcPolicy to %d\n", __func__, p_cfg->ui8EcPolicy);
8415  }
8417  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6ri") < 0)
8418  {
8419  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): setting ecErrThreshold not supported for FW < 6ri\n", __func__);
8421  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): reset ecErrThreshold to %d\n", __func__, NI_EC_ERR_THRESHOLD_DEFAULT);
8422  }
8424  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6s1") < 0)
8425  {
8426  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): (EcPolicy == best_effort_out_dc) not supported for FW < 6s1\n", __func__);
8428  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): reset EcPolicy to %d\n", __func__, p_cfg->ui8EcPolicy);
8429  }
8431  p_cfg->ui8SurviveStreamErr = p_dec->survive_stream_err;
8432  if (p_cfg->ui8SurviveStreamErr != 0 &&
8433  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rl") < 0) {
8434  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): surviveStreamErr is not supported for FW < 6rl\n", __func__);
8435  }
8436  if (p_dec->reduce_dpb_delay)
8437  {
8438  if (p_ctx->codec_format == NI_CODEC_FORMAT_H264 &&
8439  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rs") >= 0)
8440  p_cfg->ui8ReduceDpbDelay = p_dec->reduce_dpb_delay;
8441  else
8442  {
8443  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s():Not support to reduce dpb delay and reset.\n", __func__);
8444  p_dec->reduce_dpb_delay = 0;
8445  }
8446  }
8447 
8448  //print it all out
8449  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8HWFrame = %d\n", p_cfg->ui8HWFrame);
8450  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8MCMode = %d\n", p_cfg->ui8MCMode);
8451  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8UduSeiEnabled = %d\n", p_cfg->ui8UduSeiEnabled);
8452  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16MaxSeiDataSize = %d\n", p_cfg->ui16MaxSeiDataSize);
8453  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8DisablePictureReordering = %d\n", p_cfg->ui8DisablePictureReordering);
8454  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8Enabled0 = %d\n", p_cfg->asOutputConfig[0].ui8Enabled);
8455  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8Enabled1 = %d\n", p_cfg->asOutputConfig[1].ui8Enabled);
8456  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8Enabled2 = %d\n", p_cfg->asOutputConfig[2].ui8Enabled);
8457  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32MaxPktSize = %u\n", p_cfg->ui32MaxPktSize);
8458  for (i = 0; i < NI_MAX_NUM_OF_DECODER_OUTPUTS; i++)
8459  {
8460  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8Force8Bit %d\n", i, p_cfg->asOutputConfig[i].ui8Force8Bit);
8461  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8SemiPlanarEnabled %d\n", i, p_cfg->asOutputConfig[i].ui8SemiPlanarEnabled);
8462  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8CropMode %d\n", i, p_cfg->asOutputConfig[i].ui8CropMode);
8463  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] sCroppingRectable.ui16XYWH %d,%d - %d x %d\n", i,
8468  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] sOutputPictureSize.ui16Width x height %d x %d\n", i,
8471  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8ScaleEnabled %d\n", i, p_cfg->asOutputConfig[i].ui8ScaleEnabled);
8472  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8EnablePpuScaleAdapt %u\n", i, p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt);
8473  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8EnablePpuScaleLimit %u\n", i, p_cfg->asOutputConfig[i].ui8EnablePpuScaleLimit);
8474  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8ScaleResCeil %u\n", i, p_cfg->asOutputConfig[i].ui8ScaleResCeil);
8475  }
8476  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8MaxExtraHwFrameCnt %u\n", p_cfg->ui8MaxExtraHwFrameCnt);
8477  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EcPolicy = %u\n", p_cfg->ui8EcPolicy);
8478  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableAdvancedEc = %u\n", p_cfg->ui8EnableAdvancedEc);
8479  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnablelowDelayCheck = %u\n", p_cfg->ui8EnablelowDelayCheck);
8480  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8DisableAdaptiveBuffers = %u\n", p_cfg->ui8DisableAdaptiveBuffers);
8481  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ErrRatioThreshold = %u\n", p_cfg->ui32ErrRatioThreshold);
8482  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8ReduceDpbDelay = %u\n", p_cfg->ui8ReduceDpbDelay);
8483 
8484 }
8485 
8486 
8487 /*!******************************************************************************
8488  * \brief Setup all xcoder configurations with custom parameters (Rev. B)
8489  *
8490  * \param
8491  *
8492  * \return
8493  ******************************************************************************/
8495  ni_encoder_config_t *p_cfg,
8496  ni_xcoder_params_t *p_src)
8497 {
8498  ni_t408_config_t* p_t408 = NULL;
8499  ni_encoder_cfg_params_t *p_enc = NULL;
8500  int i = 0;
8501 
8502  if ((!p_ctx) || (!p_cfg) || (!p_src))
8503  {
8504  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
8505  __func__);
8506  return;
8507  }
8508  p_t408 = &(p_cfg->niParamT408);
8509  p_enc = &p_src->cfg_enc_params;
8510 
8511  ni_set_default_template(p_ctx, p_cfg);
8512 
8513  p_cfg->i32picWidth = p_src->source_width;
8514  p_cfg->i32picHeight = p_src->source_height;
8515  p_t408->tier = p_enc->high_tier;
8516  p_t408->gop_preset_index = p_enc->gop_preset_index;
8518  p_t408->cu_size_mode = p_enc->cu_size_mode;
8519  p_t408->max_num_merge = p_enc->max_num_merge;
8520  p_cfg->ui8AiEnhanceMode = p_src->enable_ai_enhance;
8521  p_cfg->ui8enable2PassGopPatern = p_src->enable2PassGop;
8522  // enhance_level is in range [1,3] set level when paramters is valid, otherwise make it to 1.
8523  if(p_cfg->ui8AiEnhanceMode && p_src->ai_enhance_level > 0 && p_src->ai_enhance_level < 4){
8524  p_cfg->ui8AiEnhanceLevel = p_src->ai_enhance_level;
8525  }else{
8526  p_cfg->ui8AiEnhanceLevel = 1;
8527  }
8529  p_cfg->i8skipFrameEnable = p_enc->skip_frame_enable;
8531  p_cfg->u8skipFrameInterval = p_enc->skip_frame_interval;
8532  p_cfg->ui16iFrameSizeRatio = p_enc->iframe_size_ratio;
8533  p_cfg->ui8EnableAcqLimit = p_enc->enable_acq_limit;
8536  if (p_enc->crfFloat == (float)-1.0 && p_enc->enable_smooth_crf == 1)
8537  {
8538  //smooth crf only support when setting float crf
8539  p_enc->enable_smooth_crf = 0;
8540  }
8541  p_cfg->ui8enableSmoothCrf = p_enc->enable_smooth_crf;
8543 
8544  // enable_dynamic_8x8_merge, enable_dynamic_16x16_merge, enable_dynamic_32x32_merge,
8545  // trans_rate, enable_hvs_qp_scale:
8546  // are not present in Rev B p_config
8547 
8548  p_cfg->ui8rcEnable = p_enc->rc.enable_rate_control;
8549 
8550  if(p_ctx->last_bitrate != 0)
8551  {
8552  // Slow sequence change happened. Retain the last bitrate.
8553  ni_log2(p_ctx, NI_LOG_DEBUG, "### %s: Slow sequence happened retain last_bitrate %d. assigned bitrate %d\n",
8554  __FUNCTION__, p_ctx->last_bitrate, p_src->bitrate);
8555  p_src->bitrate = p_ctx->last_bitrate;
8556  }
8557 
8558  if (p_src->bitrate != 0)
8559  {
8560  p_cfg->i32bitRate = p_src->bitrate;
8561  }
8562 
8563  // Update the bitrate to be used after Slow sequence change
8564  p_ctx->last_bitrate = p_cfg->i32bitRate;
8565 
8566 #if 0
8567  if ((p_enc->rc.enable_rate_control == 0) &&
8568  ((p_enc->rc.enable_mb_level_rc == 1) || (p_enc->rc.enable_cu_level_rate_control == 1)))
8569  {
8571  ni_log2(p_ctx, NI_LOG_DEBUG, "force enable_mb_level_rc & enable_cu_level_rate_control to 0 because rate control is disabled\n");
8572  }
8573 #endif
8574 
8576  p_t408->enable_hvs_qp = p_enc->rc.enable_hvs_qp;
8577  p_t408->hvs_qp_scale = p_enc->rc.hvs_qp_scale;
8578  p_t408->minQpI = p_enc->rc.min_qp;
8579  p_t408->minQpP = p_enc->rc.min_qp;
8580  p_t408->minQpB = p_enc->rc.min_qp;
8581  p_t408->maxQpI = p_enc->rc.max_qp;
8582  p_t408->maxQpP = p_enc->rc.max_qp;
8583  p_t408->maxQpB = p_enc->rc.max_qp;
8584 
8585  p_t408->max_delta_qp = p_enc->rc.max_delta_qp;
8586  if (p_enc->rc.vbv_buffer_size != -1)
8587  {
8588  p_cfg->i32vbvBufferSize = p_enc->rc.vbv_buffer_size;
8589  }
8590  else
8591  {
8592  if (p_enc->rc.enable_rate_control)
8593  p_cfg->i32vbvBufferSize = 3000; // enable CBR (default vbv buffer size 3000) even if user does not set vbvBufferSize
8594  else
8595  p_cfg->i32vbvBufferSize = 0; // if user sets CRF but not vbvBufferSize, do not eanble capped CRF
8596  }
8597  p_cfg->ui32vbvMaxRate = p_enc->rc.vbv_max_rate;
8598  p_cfg->i8intraQpDelta = p_enc->rc.intra_qp_delta;
8599  p_cfg->ui8fillerEnable = p_enc->rc.enable_filler;
8600  p_cfg->ui8picSkipEnable = p_enc->rc.enable_pic_skip;
8601  p_cfg->ui16maxFrameSize = p_enc->maxFrameSize / 2000;
8602  p_t408->intra_period = p_enc->intra_period;
8603  p_t408->roiEnable = p_enc->roi_enable;
8604  p_t408->useLongTerm = p_enc->long_term_ref_enable;
8605  if (QUADRA)
8606  {
8608  p_cfg->ui8setLongTermCount = p_enc->long_term_ref_count;
8609  }
8610  p_t408->conf_win_top = p_enc->conf_win_top;
8611  p_t408->conf_win_bottom = p_enc->conf_win_bottom;
8612  p_t408->conf_win_left = p_enc->conf_win_left;
8613  p_t408->conf_win_right = p_enc->conf_win_right;
8614  p_t408->avcIdrPeriod = p_enc->intra_period;
8615 
8616  if (QUADRA)
8617  {
8618  if(p_ctx->last_framerate.framerate_num != 0)
8619  {
8620  // Slow sequence change happened. Retain the last framerate.
8621  ni_log2(p_ctx, NI_LOG_DEBUG, "### %s: Slow sequence happened retain last_framerate num %d den %d. assigned num %d den %d\n",
8622  __FUNCTION__, p_ctx->last_framerate.framerate_num, p_ctx->last_framerate.framerate_denom,
8623  p_cfg->i32frameRateInfo, p_cfg->i32frameRateDenominator);
8624  p_src->fps_number = p_ctx->last_framerate.framerate_num;
8626 
8627  if (!p_src->enable_vfr) {
8628  p_enc->frame_rate = (int)(p_src->fps_number / p_src->fps_denominator);
8629  }
8630  }
8631 
8632  if (p_cfg->i32frameRateInfo != p_enc->frame_rate)
8633  {
8634  p_cfg->i32frameRateInfo = p_enc->frame_rate;
8635  p_cfg->i32frameRateDenominator = 1;
8636  if (p_src->fps_denominator != 0 &&
8637  (p_src->fps_number % p_src->fps_denominator) != 0)
8638  {
8639  uint32_t numUnitsInTick = 1000;
8640  p_cfg->i32frameRateDenominator = numUnitsInTick + 1;
8641  p_cfg->i32frameRateInfo += 1;
8642  p_cfg->i32frameRateInfo *= numUnitsInTick;
8643  }
8644  }
8645 
8646  // Update the framerate to be used after Slow sequence change
8649  }
8650  else
8651  {
8652  if (p_cfg->i32frameRateInfo != p_enc->frame_rate)
8653  {
8654  p_cfg->i32frameRateInfo = p_enc->frame_rate;
8655  p_t408->numUnitsInTick = 1000;
8656  if (p_src->fps_denominator != 0 &&
8657  (p_src->fps_number % p_src->fps_denominator) != 0)
8658  {
8659  p_t408->numUnitsInTick += 1;
8660  p_cfg->i32frameRateInfo += 1;
8661  }
8662  p_t408->timeScale = p_cfg->i32frameRateInfo * 1000;
8663  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
8664  {
8665  p_t408->timeScale *= 2;
8666  }
8667  }
8668  }
8669 
8670  p_t408->intra_qp = p_enc->rc.intra_qp;
8671 
8672  // "repeatHeaders" value 1 (all I frames) maps to forcedHeaderEnable
8673  // value 2; all other values are ignored
8674  if (p_t408->forcedHeaderEnable != p_enc->forced_header_enable &&
8676  {
8677  p_t408->forcedHeaderEnable = 2;
8678  p_cfg->ui8repeatHeaders = p_enc->forced_header_enable;
8679  }
8680 
8682 
8683  if (STD_AVC == p_cfg->ui8bitstreamFormat)
8684  {
8685  switch (p_t408->decoding_refresh_type)
8686  {
8687  case 0: // Non-IRAP I-p_frame
8688  {
8689  // intra_period set to user-configured (above), avcIdrPeriod set to 0
8690  p_t408->avcIdrPeriod = 0;
8691  break;
8692  }
8693  case 1: // CRA
8694  case 2: // IDR
8695  {
8696  // intra_period set to 0, avcIdrPeriod set to user-configured (above)
8697  p_t408->intra_period = 0;
8698  break;
8699  }
8700  default:
8701  {
8702  ni_log(
8703  NI_LOG_ERROR,
8704  "ERROR: %s() unknown value for p_t408->decoding_refresh_type: %d\n",
8705  __func__, p_t408->decoding_refresh_type);
8706  break;
8707  }
8708  }
8709  } else if (STD_HEVC == p_cfg->ui8bitstreamFormat ||
8710  STD_AV1 == p_cfg->ui8bitstreamFormat)
8711  {
8712  p_t408->avcIdrPeriod = 0;
8713  }
8714 
8715  // Rev. B: H.264 only parameters.
8716  p_t408->enable_transform_8x8 = p_enc->enable_transform_8x8;
8717  p_t408->entropy_coding_mode = p_enc->entropy_coding_mode;
8718 
8719  // Rev. B: shared between HEVC and H.264
8720  p_t408->slice_mode = p_enc->slice_mode;
8721  p_t408->slice_arg = p_enc->slice_arg;
8722  if (p_t408->intra_mb_refresh_mode != p_enc->intra_mb_refresh_mode)
8723  {
8725  if (1 != p_t408->intra_mb_refresh_mode)
8726  {
8727  p_t408->intra_mb_refresh_mode = 1;
8728  ni_log2(p_ctx, NI_LOG_DEBUG, "force intraRefreshMode to 1 because quadra only supports intra refresh by rows\n");
8729  }
8730  }
8731 
8732  if (p_t408->intra_mb_refresh_arg != p_enc->intra_mb_refresh_arg)
8733  {
8734  p_t408->intra_mb_refresh_arg = p_enc->intra_mb_refresh_arg;
8735  if (1 == p_t408->intra_mb_refresh_mode)
8736  {
8737  int mbHeight = (p_cfg->ui8bitstreamFormat == STD_AVC) ? 16 : 64;
8738  int mbRows = (p_cfg->i32picHeight + mbHeight - 1) / mbHeight;
8739  p_cfg->ui16gdrDuration = (mbRows + p_t408->intra_mb_refresh_arg - 1) / p_t408->intra_mb_refresh_arg;
8740  }
8741  }
8742 
8743  p_cfg->ui8intraResetRefresh = p_enc->intra_reset_refresh;
8744 
8745  // Rev. B: could be shared for HEVC and H.264
8746  p_t408->enable_mb_level_rc = p_enc->rc.enable_mb_level_rc;
8747 
8748  // profile setting: if user specified profile
8749  if (0 != p_enc->profile)
8750  {
8751  p_t408->profile = p_enc->profile;
8752  }
8753 
8754  p_t408->level = p_enc->level_idc;
8755 
8756  // main, extended or baseline profile of 8 bit (if input is 10 bit, Quadra auto converts to 8 bit) H.264 requires the following:
8757  // main: profile = 2 transform8x8Enable = 0
8758  // extended: profile = 3 entropyCodingMode = 0, transform8x8Enable = 0
8759  // baseline: profile = 1 entropyCodingMode = 0, transform8x8Enable = 0 and
8760  // gop with no B frames (gopPresetIdx=1, 2, 6, or 0
8761  // (custom with no B frames)
8762  if (STD_AVC == p_cfg->ui8bitstreamFormat)
8763  {
8764  if (2 == p_t408->profile)
8765  {
8766  p_t408->enable_transform_8x8 = 0;
8767  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_transform_8x8 set to 0 for profile 2 (main)\n");
8768  }
8769  else if (3 == p_t408->profile || 1 == p_t408->profile)
8770  {
8771  p_t408->entropy_coding_mode = p_t408->enable_transform_8x8 = 0;
8772  ni_log2(p_ctx, NI_LOG_DEBUG, "entropy_coding_mode and enable_transform_8x8 set to 0 "
8773  "for profile 3 (extended) or 1 (baseline)\n");
8774  }
8775  }
8776 
8777  if (QUADRA)
8778  {
8779  if (0 == p_t408->entropy_coding_mode && 1 == p_enc->EnableRdoQuant)
8780  {
8781  ni_log2(p_ctx, NI_LOG_DEBUG, "RDOQ does not support entropy_coding_mode 0 (CAVLC) "
8782  "force EnableRdoQuant 0 to accommodate HW limiation\n");
8783  p_enc->EnableRdoQuant = 0;
8784  }
8785  }
8786 
8787 #ifndef QUADRA
8788  if (!QUADRA)
8789  {
8790  if (GOP_PRESET_IDX_CUSTOM == p_t408->gop_preset_index)
8791  {
8793  for (i = 0; i < p_t408->custom_gop_params.custom_gop_size; i++)
8794  {
8797  p_t408->custom_gop_params.pic_param[i].pic_qp = p_enc->custom_gop_params.pic_param[i].pic_qp + p_t408->intra_qp;
8798  p_t408->custom_gop_params.pic_param[i].num_ref_pic_L0 = p_enc->custom_gop_params.pic_param[i].num_ref_pic_L0;
8799  p_t408->custom_gop_params.pic_param[i].ref_poc_L0 = p_enc->custom_gop_params.pic_param[i].ref_poc_L0;
8800  p_t408->custom_gop_params.pic_param[i].ref_poc_L1 = p_enc->custom_gop_params.pic_param[i].ref_poc_L1;
8802  }
8803  }
8804  }
8805  else // QUADRA
8806 #endif
8807  {
8808  if (p_enc->custom_gop_params.custom_gop_size &&
8811  {
8814  for (i = 0; i < p_t408->custom_gop_params.custom_gop_size; i++)
8815  {
8824  p_t408->custom_gop_params.pic_param[i].pic_type =
8828  for (int j = 0;
8829  j < p_enc->custom_gop_params.pic_param[i].num_ref_pics; j++)
8830  {
8831  p_t408->custom_gop_params.pic_param[i].rps[j].ref_pic =
8832  p_enc->custom_gop_params.pic_param[i].rps[j].ref_pic;
8835  }
8836  }
8837  }
8838  }
8839 
8840  p_ctx->key_frame_type = p_t408->decoding_refresh_type; //Store to use when force key p_frame
8841 
8842  // forceFrameType=1 requires intraPeriod=0 and avcIdrPeriod=0 and gopPresetIdx=8
8843  if (1 == p_src->force_frame_type)
8844  {
8845  p_t408->intra_period = 0;
8846  p_t408->avcIdrPeriod = 0;
8847  p_t408->gop_preset_index = 8;
8848  p_ctx->force_frame_type = 1;
8849  }
8850 
8851  p_cfg->hdrEnableVUI = p_src->hdrEnableVUI;
8852 
8853  if (p_cfg->i32hwframes != p_src->hwframes)
8854  {
8855  if (p_src->hwframes && p_ctx->auto_dl_handle == 0)
8856  {
8857  p_cfg->i32hwframes = p_src->hwframes;
8858  }
8859  else
8860  {
8861  p_cfg->i32hwframes = 0;
8862  }
8863  }
8864  p_cfg->ui16rootBufId = p_src->rootBufId;
8865 
8866  //set VUI info deprecated
8867  //p_cfg->ui32VuiDataSizeBits = p_src->ui32VuiDataSizeBits;
8868  //p_cfg->ui32VuiDataSizeBytes = p_src->ui32VuiDataSizeBytes;
8869  //memcpy(p_cfg->ui8VuiRbsp, p_src->ui8VuiRbsp, NI_MAX_VUI_SIZE);
8870  //if ((p_src->pos_num_units_in_tick > p_src->ui32VuiDataSizeBits) || (p_src->pos_time_scale > p_src->ui32VuiDataSizeBits))
8871  //{
8872  // ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() VUI filling error\n", __func__);
8873  // return;
8874  //}
8875  //else
8876  //{
8877  // ni_fix_VUI(p_cfg->ui8VuiRbsp, p_src->pos_num_units_in_tick, p_t408->numUnitsInTick);
8878  // ni_fix_VUI(p_cfg->ui8VuiRbsp, p_src->pos_time_scale, p_t408->timeScale);
8879  //}
8880 
8881  if (p_src->enable_vfr)
8882  {
8883  p_cfg->ui8fixedframerate = 0;
8884  } else
8885  {
8886  p_cfg->ui8fixedframerate = 1;
8887  }
8888 
8889  //new QUADRA param
8890  if (p_enc->EnableAUD != 0)
8891  {
8892  p_cfg->ui8EnableAUD = p_enc->EnableAUD;
8893  }
8894  if (p_enc->lookAheadDepth != 0)
8895  {
8896  p_cfg->ui8LookAheadDepth = p_enc->lookAheadDepth;
8897  }
8898  if (p_enc->rdoLevel != 1)
8899  {
8900  p_cfg->ui8rdoLevel = p_enc->rdoLevel;
8901  }
8902  if (p_enc->crf != -1)
8903  {
8904  p_cfg->i8crf = p_enc->crf;
8905  }
8906  if (p_enc->HDR10MaxLight != 0)
8907  {
8908  p_cfg->ui16HDR10MaxLight = p_enc->HDR10MaxLight;
8909  }
8910  if (p_enc->HDR10AveLight != 0)
8911  {
8912  p_cfg->ui16HDR10AveLight = p_enc->HDR10AveLight;
8913  }
8914  if (p_enc->HDR10CLLEnable != 0)
8915  {
8916  p_cfg->ui8HDR10CLLEnable = p_enc->HDR10CLLEnable;
8917  }
8918  if (p_enc->HDR10Enable != 0)
8919  {
8920  p_cfg->ui8hdr10_enable = p_enc->HDR10Enable;
8921  p_cfg->ui16hdr10_dx0 = p_enc->HDR10dx0;
8922  p_cfg->ui16hdr10_dy0 = p_enc->HDR10dy0;
8923  p_cfg->ui16hdr10_dx1 = p_enc->HDR10dx1;
8924  p_cfg->ui16hdr10_dy1 = p_enc->HDR10dy1;
8925  p_cfg->ui16hdr10_dx2 = p_enc->HDR10dx2;
8926  p_cfg->ui16hdr10_dy2 = p_enc->HDR10dy2;
8927  p_cfg->ui16hdr10_wx = p_enc->HDR10wx;
8928  p_cfg->ui16hdr10_wy = p_enc->HDR10wy;
8929  p_cfg->ui32hdr10_maxluma = p_enc->HDR10maxluma;
8930  p_cfg->ui32hdr10_minluma = p_enc->HDR10minluma;
8931  }
8932 
8933  if (p_enc->EnableRdoQuant != 0)
8934  {
8935  p_cfg->ui8EnableRdoQuant = p_enc->EnableRdoQuant;
8936  }
8937  if (p_enc->ctbRcMode != 0)
8938  {
8939  p_cfg->ui8ctbRcMode = p_enc->ctbRcMode;
8940  }
8941  if (p_enc->gopSize != 0)
8942  {
8943  p_cfg->ui8gopSize = p_enc->gopSize;
8944  }
8945  if (p_src->use_low_delay_poc_type != 0)
8946  {
8948  }
8949  if (p_enc->gopLowdelay != 0)
8950  {
8951  p_cfg->ui8gopLowdelay = p_enc->gopLowdelay;
8952  }
8953  if (p_enc->gdrDuration != 0)
8954  {
8955  p_cfg->ui16gdrDuration = p_enc->gdrDuration;
8956  }
8957  if (p_enc->colorDescPresent)
8958  {
8959  p_cfg->ui8colorDescPresent = 1;
8960  p_cfg->ui8colorPrimaries = p_enc->colorPrimaries;
8961  p_cfg->ui8colorTrc = p_enc->colorTrc;
8962  p_cfg->ui8colorSpace = p_enc->colorSpace;
8963  p_cfg->ui8videoFullRange = p_enc->videoFullRange;
8964  }
8965  if (p_enc->videoFullRange)
8966  {
8967  p_cfg->ui8videoFullRange = p_enc->videoFullRange;
8968  }
8969  if (p_enc->hrdEnable != 0)
8970  {
8971  p_cfg->ui8hrdEnable = p_enc->hrdEnable;
8972  }
8973 
8974  p_cfg->ui8planarFormat = p_src->cfg_enc_params.planar;
8975  p_cfg->ui16aspectRatioWidth = p_enc->aspectRatioWidth;
8976  p_cfg->ui16aspectRatioHeight = p_enc->aspectRatioHeight;
8977 
8978  if (p_enc->ltrRefInterval != 0)
8979  {
8980  p_cfg->ui32ltrRefInterval = p_enc->ltrRefInterval;
8981  p_cfg->i32ltrRefQpOffset = p_enc->ltrRefQpOffset;
8982  p_cfg->ui32ltrFirstGap = p_enc->ltrFirstGap;
8983  p_cfg->ui32ltrNextInterval = p_enc->ltrNextInterval;
8984  }
8985  if (p_enc->multicoreJointMode != 0)
8986  {
8987  p_cfg->ui8multicoreJointMode = p_enc->multicoreJointMode;
8988  }
8989  p_cfg->ui32QLevel = p_enc->qlevel;
8990 
8991  if (p_enc->chromaQpOffset != 0)
8992  {
8993  p_cfg->i8chromaQpOffset = p_enc->chromaQpOffset;
8994  }
8995 
8996  if (p_enc->tolCtbRcInter != (float)0.1)
8997  {
8998  p_cfg->i32tolCtbRcInter = (int32_t)(p_enc->tolCtbRcInter * 1000);
8999  }
9000 
9001  if (p_enc->tolCtbRcIntra != (float)0.1)
9002  {
9003  p_cfg->i32tolCtbRcIntra = (int32_t)(p_enc->tolCtbRcIntra * 1000);
9004  }
9005 
9006  if (p_enc->bitrateWindow != -255)
9007  {
9008  p_cfg->i16bitrateWindow = p_enc->bitrateWindow;
9009  }
9010 
9011  if (p_enc->inLoopDSRatio != 1)
9012  {
9013  p_cfg->ui8inLoopDSRatio = p_enc->inLoopDSRatio;
9014  }
9015 
9016  if (p_enc->blockRCSize != 0)
9017  {
9018  p_cfg->ui8blockRCSize = p_enc->blockRCSize;
9019  }
9020 
9021  if (p_enc->rcQpDeltaRange != 10)
9022  {
9023  p_cfg->ui8rcQpDeltaRange = p_enc->rcQpDeltaRange;
9024  }
9025 
9026  if (p_enc->ctbRowQpStep != 0)
9027  {
9028  p_cfg->i16ctbRowQpStep = p_enc->ctbRowQpStep;
9029  }
9030 
9031  if (p_enc->newRcEnable != -1)
9032  {
9033  p_cfg->ui8NewRCEnable = p_enc->newRcEnable;
9034  }
9035 
9036  // convert enable_mb_level_rc, enable_cu_level_rate_control, and enable_hvs_qp to ctbRcMode
9037  if (QUADRA)
9038  {
9039  // ctbRcMode has priority over enable_mb_level_rc, enable_cu_level_rate_control, and enable_hvs_qp
9040  if (!p_cfg->ui8ctbRcMode)
9041  {
9042  if (p_t408->enable_mb_level_rc || p_t408->enable_cu_level_rate_control)
9043  {
9044  if (p_t408->enable_hvs_qp)
9045  {
9046  p_cfg->ui8ctbRcMode = 3;
9047  // If hvsQP is enabled, disable strongIntraSmooth to apply one filtering at a time for better VQ.
9048  p_t408->strongIntraSmoothEnable = 0;
9050  "Turning off strongIntraSmoothing because hvsQPEnable=1 "
9051  "for better subjective VQ\n");
9052  } else
9053  {
9054  p_cfg->ui8ctbRcMode = 2;
9055  }
9056  }
9057  else if (p_t408->enable_hvs_qp)
9058  {
9059  p_cfg->ui8ctbRcMode = 1;
9060  // If hvsQP is enabled, disable strongIntraSmooth to apply one filtering at a time for better VQ.
9061  p_t408->strongIntraSmoothEnable = 0;
9063  "Turning off strongIntraSmoothing because hvsQPEnable=1 for "
9064  "better subjective VQ\n");
9065  }
9066  }
9067  }
9068 
9069  if (p_src->low_delay_mode != 0)
9070  {
9071  p_cfg->ui8LowDelay = !!(p_src->low_delay_mode);
9072  }
9073 
9074  if (p_enc->enable_ssim != 0)
9075  {
9076  p_cfg->ui8enableSSIM = p_enc->enable_ssim;
9077  }
9078 
9079  if (p_enc->avcc_hvcc != 0)
9080  {
9081  p_cfg->ui8avccHvcc = p_enc->avcc_hvcc;
9082  }
9083 
9084  if (p_enc->av1_error_resilient_mode != 0)
9085  {
9087  }
9088 
9089  if (p_enc->temporal_layers_enable != 0)
9090  {
9092  }
9093 
9094  if (p_enc->spatial_layers > 1)
9095  {
9096  p_cfg->ui8spatialLayersMinusOne = p_enc->spatial_layers - 1;
9097  if (ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rw") < 0) {
9098  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): spatialLayers is not supported for FW < 6rw\n", __func__);
9099  }
9100  }
9101 
9102  if (p_enc->spatial_layers_ref_base_layer != 0)
9103  {
9105  if (ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6s0") < 0) {
9106  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): spatialLayers is not supported for FW < 6s0\n", __func__);
9107  }
9108  }
9109 
9110  if (p_ctx->pixel_format != NI_PIX_FMT_YUV420P)
9111  {
9112  p_cfg->ui8PixelFormat = p_ctx->pixel_format;
9113  }
9114 
9115  if (p_enc->get_psnr_mode != 3)
9116  {
9117  if (p_enc->get_psnr_mode == 2 && p_ctx->codec_format == NI_CODEC_FORMAT_H265)
9118  {
9119  // h.265 psnr_y is supported by HW for 8-bit & 10-bit, no need to reject setting or disable luma RFC
9122  {
9123  p_enc->get_psnr_mode = 3;
9124  ni_log(NI_LOG_INFO, "Warning h.265 psnr_y is only supported for YUV420P, YUV420P10LE, NV12, and P010LE\n");
9125  }
9126  }
9127  else if (p_enc->get_psnr_mode == 4)
9128  {
9129  p_cfg->ui8compressor = 0;
9131  {
9132  p_enc->get_psnr_mode = 3;
9133  p_cfg->ui8compressor = 3;
9134  ni_log(NI_LOG_INFO, "Warning reconstructed frames only supported for YUV420P, NV12\n");
9135  }
9136  }
9137  else
9138  {
9139  p_cfg->ui8compressor = p_enc->get_psnr_mode;
9140  if (p_cfg->ui8PixelFormat != NI_PIX_FMT_YUV420P)
9141  {
9142  p_cfg->ui8compressor = 3;
9143  p_enc->get_psnr_mode = 3;
9144  ni_log(NI_LOG_INFO, "Warning get psnr feature only support YUV420P (except for h.265 psnr_y)\n");
9145  }
9146  if (p_enc->crop_width || p_enc->crop_height)
9147  {
9148  p_cfg->ui8compressor = 3;
9149  p_enc->get_psnr_mode = 3;
9150  ni_log(NI_LOG_INFO, "Warning get psnr feature is not supported when cropWidth x cropHeight are set\n");
9151  }
9152  }
9153  }
9154 
9155  if (p_src->zerocopy_mode == -1) // zero copy auto mode - disable zero copy for low resolution
9156  {
9157  bool is_rgba = (p_ctx->pixel_format == NI_PIX_FMT_RGBA ||
9158  p_ctx->pixel_format == NI_PIX_FMT_BGRA ||
9159  p_ctx->pixel_format == NI_PIX_FMT_ARGB ||
9160  p_ctx->pixel_format == NI_PIX_FMT_ABGR) ? true : false;
9161  if (is_rgba ||
9163  "6Q") >= 0) &&
9165  p_src->zerocopy_mode = 1;
9166  else
9167  p_src->zerocopy_mode = 0;
9168  }
9169 
9170  if (p_src->zerocopy_mode)
9171  {
9172  p_cfg->ui32lumaLinesize = p_src->luma_linesize;
9173  p_cfg->ui32chromaLinesize = p_src->chroma_linesize;
9174  }
9175  else
9176  {
9177  p_cfg->ui32lumaLinesize = p_src->luma_linesize = 0;
9178  p_cfg->ui32chromaLinesize = p_src->chroma_linesize = 0;
9179  }
9180  // for fast sequence change linesize check
9181  p_ctx->ori_luma_linesize = p_src->luma_linesize;
9182  p_ctx->ori_chroma_linesize = p_src->chroma_linesize;
9183 
9184  // calculate number for frames delay for minFramesDelay
9185  int lookAheadEnable = !!p_cfg->ui8LookAheadDepth;
9186  int gopSize = g_map_preset_to_gopsize[lookAheadEnable][p_t408->gop_preset_index + 1];
9187  int mulitcoreDelay = p_cfg->ui8multicoreJointMode ? 3 : 0;
9188 
9189  if (p_t408->gop_preset_index == 0) // Custom GOP
9190  gopSize = p_t408->custom_gop_params.custom_gop_size;
9191 
9192  if (lookAheadEnable)
9193  {
9194  int firstGopEnd = gopSize + 1 + mulitcoreDelay; // first I-frame gopSize is 1
9195  int lookaheadGopEnd = mulitcoreDelay ?
9196  p_cfg->ui8LookAheadDepth + mulitcoreDelay + (gopSize - ((p_cfg->ui8LookAheadDepth-1+mulitcoreDelay) % gopSize)) :
9197  p_cfg->ui8LookAheadDepth + (gopSize - ((p_cfg->ui8LookAheadDepth-1) % gopSize)); // lookAheadDepth-1 because lookahead queue includes first I-frame
9198  int initialDelayNum = (firstGopEnd > lookaheadGopEnd) ? firstGopEnd : lookaheadGopEnd;
9199  int maxDelayNum = p_cfg->ui8LookAheadDepth + 1 + gopSize / 2 + mulitcoreDelay;
9200  int maxLookaheadQueue = initialDelayNum + (gopSize - 1) + mulitcoreDelay; // assume worst case scenario - gop size changes from initial gop to gop size 1
9201 
9202  p_ctx->initial_frame_delay = initialDelayNum + (mulitcoreDelay ? 4 : 0); // for multicore pass-2, need to add 4 more frames before pass-2 could output frame
9203  p_ctx->max_frame_delay = ((maxDelayNum > maxLookaheadQueue) ? maxDelayNum : maxLookaheadQueue) + (mulitcoreDelay ? 4 : 0); // for multicore pass-2, need to add 4 more frames before pass-2 could output frame
9205  "6r2") >= 0)
9206  {
9207  p_ctx->last_gop_size = gopSize; // for adaptive gop, gop size change can happen in pass-1, causing the first non-IDR output to carrry gop size 4 insetad of 8 and increase lookahead queue
9209  "6rX") >= 0)
9210  {
9211  if (p_t408->gop_preset_index == GOP_PRESET_IDX_DEFAULT || mulitcoreDelay) // for adaptive gop or multicore, just set max frame delay to workaround encoding stuck
9212  p_ctx->current_frame_delay = p_ctx->max_frame_delay;
9213  else
9214  p_ctx->current_frame_delay = p_ctx->initial_frame_delay;
9215  }
9216  else
9217  {
9218  p_ctx->current_frame_delay = p_ctx->max_frame_delay;
9219  }
9220  }
9221  else
9222  {
9223  p_ctx->last_gop_size = gopSize;
9224  p_ctx->current_frame_delay = p_ctx->max_frame_delay;
9225  }
9226  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: firstGopEnd %d lookaheadGopEnd %d initialDelayNum %d maxDelayNum %d maxLookaheadQueue %d\n",
9227  __FUNCTION__, firstGopEnd, lookaheadGopEnd, initialDelayNum, maxDelayNum, maxLookaheadQueue);
9228  }
9229  else
9230  {
9231  p_ctx->last_gop_size = gopSize;
9232  p_ctx->initial_frame_delay = p_ctx->max_frame_delay = p_ctx->current_frame_delay = gopSize + mulitcoreDelay;
9233  }
9234 
9235  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: preset %d lookAheadDepth %d gopSize %d mulitcoreDelay %d "
9236  "last_gop_size %d linitial_frame_delay %d current_frame_delay %d max_frame_delay %d\n",
9237  __FUNCTION__, p_t408->gop_preset_index, p_cfg->ui8LookAheadDepth, gopSize, mulitcoreDelay,
9238  p_ctx->last_gop_size, p_ctx->initial_frame_delay, p_ctx->current_frame_delay, p_ctx->max_frame_delay);
9239 
9240  if (p_enc->crop_width != 0 && p_enc->crop_height != 0)
9241  {
9242  p_cfg->ui32cropWidth = p_enc->crop_width;
9243  p_cfg->ui32cropHeight = p_enc->crop_height;
9244  p_cfg->ui32horOffset = p_enc->hor_offset;
9245  p_cfg->ui32verOffset = p_enc->ver_offset;
9246  }
9247 
9248  if (p_enc->crfMax != -1)
9249  {
9250  p_cfg->i8crfMax = (int8_t)(p_enc->crfMax) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9251  }
9252 
9253  if (p_enc->qcomp != (float)0.6)
9254  {
9255  p_cfg->i32qcomp = (int32_t)(p_enc->qcomp * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9256  }
9257 
9258  if (p_enc->noMbtree != 0)
9259  {
9260  p_cfg->ui8noMbtree = p_enc->noMbtree;
9261  }
9262 
9263  if (p_enc->noHWMultiPassSupport != 0)
9264  {
9266  }
9267 
9268  if (p_enc->cuTreeFactor != 5)
9269  {
9270  p_cfg->i8cuTreeFactor = p_enc->cuTreeFactor;
9271  }
9272 
9273  if (p_enc->ipRatio != (float)1.4)
9274  {
9275  p_cfg->i32ipRatio = (int32_t)(p_enc->ipRatio * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9276  }
9277 
9278  if (p_enc->pbRatio != (float)1.3)
9279  {
9280  p_cfg->i32pbRatio = (int32_t)(p_enc->pbRatio * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9281  }
9282 
9283  if (p_enc->cplxDecay != (float)0.5)
9284  {
9285  p_cfg->i32cplxDecay = (int32_t)(p_enc->cplxDecay * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9286  }
9287 
9288  if (p_enc->pps_init_qp != -1)
9289  {
9290  p_cfg->i8ppsInitQp = (int8_t)(p_enc->pps_init_qp) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9291  }
9292 
9293  if (p_enc->bitrateMode != -1)
9294  {
9295  p_cfg->ui8bitrateMode = p_enc->bitrateMode;
9296  }
9297 
9298  if (p_enc->pass1_qp != -1)
9299  {
9300  p_cfg->i8pass1Qp = (int8_t)(p_enc->pass1_qp) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9301  }
9302 
9303  if (p_enc->crfFloat != (float)-1.0)
9304  {
9305  // for old libxcoder backward compatibility
9306  p_cfg->i8crf = (int8_t)(p_enc->crfFloat);
9307  p_cfg->i8crfDecimal = (int8_t)((p_enc->crfFloat - (float)p_cfg->i8crf) * 100);
9308  }
9309 
9310  if (p_enc->hvsBaseMbComplexity != 15)
9311  {
9312  p_cfg->i8hvsBaseMbComplexity = (int8_t)p_enc->hvsBaseMbComplexity - 15; // for old libxcoder backward compatibility, use -15 to represent 0
9313  }
9314 
9315  if (p_enc->enableipRatio != 0 && p_cfg->i32vbvBufferSize != 0) //vbvBufferSize !=0 for CBR not for ABR
9316  {
9317  p_cfg->i8enableipRatio = p_enc->enableipRatio;
9318  }
9319 
9320  if (p_enc->crf_max_iframe_enable != 0)
9321  {
9323  }
9324 
9325  if (p_enc->vbv_min_rate != 0)
9326  {
9327  p_cfg->ui32vbvMinRate = p_enc->vbv_min_rate;
9328  }
9329 
9330  if (p_enc->disableBframeRdoq != 0)
9331  {
9332  p_cfg->ui8disableBframeRDOQ = p_enc->disableBframeRdoq;
9333  }
9334 
9335  if (p_enc->forceBframeQpfactor != (float)-1.0)
9336  {
9337  p_cfg->i32forceBframeQpFactor = (int32_t)(p_enc->forceBframeQpfactor * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9338  }
9339 
9340  if (p_enc->tune_bframe_visual != 0)
9341  {
9342  p_cfg->ui8tuneBframeVisual = p_enc->tune_bframe_visual;
9343  }
9344 
9345  if (p_enc->customize_roi_qp_level != 0) {
9347  }
9348 
9349  if (p_enc->motionConstrainedMode != 0)
9350  {
9352  }
9353 
9354  if(p_enc->encMallocStrategy != 0)
9355  {
9356  p_cfg->ui8mallocStrategy = p_enc->encMallocStrategy;
9357  }
9358 
9359  if (p_enc->enable_timecode != 0)
9360  {
9361  p_cfg->ui8enableTimecode = p_enc->enable_timecode;
9362  }
9363 
9364  if (p_enc->vbvBufferReencode != 0)
9365  {
9366  p_cfg->ui8vbvBufferReencode = p_enc->vbvBufferReencode;
9367  }
9368 
9369  if (p_enc->totalCuTreeDepth != 0)
9370  {
9371  p_cfg->ui8totalCuTreeDepth = p_enc->totalCuTreeDepth;
9372  }
9373 
9374  if (p_enc->adaptiveCuTree != 0)
9375  {
9376  p_cfg->ui8adaptiveCuTree = p_enc->adaptiveCuTree;
9377  }
9378 
9379  if (p_enc->preIntraHandling != 1)
9380  {
9381  p_cfg->ui8preIntraHandling = (uint8_t)p_enc->preIntraHandling - 1; // for old libxcoder backward compatibility, use -1 to represent 0
9382  }
9383 
9384  if (p_enc->baseLayerOnly != 0)
9385  {
9386  p_cfg->ui8baseLayerOnly = p_enc->baseLayerOnly;
9387  }
9388 
9389  if (p_enc->pastFrameMaxIntraRatio != 20)
9390  {
9391  p_cfg->ui8pastFrameMaxIntraRatio = (uint8_t)p_enc->pastFrameMaxIntraRatio - 20; // for old libxcoder backward compatibility, use -20 to represent 0
9392  }
9393 
9394  if (p_enc->linkFrameMaxIntraRatio != 40)
9395  {
9396  p_cfg->ui8linkFrameMaxIntraRatio = (uint8_t)p_enc->linkFrameMaxIntraRatio - 40; // for old libxcoder backward compatibility, use -40 to represent 0
9397  }
9398 
9399  for (i = 0; i < NI_MAX_SPATIAL_LAYERS; i++)
9400  {
9401  if (p_enc->spatialLayerBitrate[i] != 0)
9402  {
9403  p_cfg->i32spatialLayerBitrate[i] = p_enc->spatialLayerBitrate[i];
9404  }
9405  }
9406 
9407  ni_log2(p_ctx, NI_LOG_DEBUG, "lowDelay=%d\n", p_src->low_delay_mode);
9408  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8bitstreamFormat=%d\n", p_cfg->ui8bitstreamFormat);
9409  ni_log2(p_ctx, NI_LOG_DEBUG, "i32picWidth=%d\n", p_cfg->i32picWidth);
9410  ni_log2(p_ctx, NI_LOG_DEBUG, "i32picHeight=%d\n", p_cfg->i32picHeight);
9411  ni_log2(p_ctx, NI_LOG_DEBUG, "i32meBlkMode=%d\n", p_cfg->i32meBlkMode);
9412  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8sliceMode=%d\n", p_cfg->ui8sliceMode);
9413  ni_log2(p_ctx, NI_LOG_DEBUG, "i32frameRateInfo=%d\n", p_cfg->i32frameRateInfo);
9414  ni_log2(p_ctx, NI_LOG_DEBUG, "i32vbvBufferSize=%d\n", p_cfg->i32vbvBufferSize);
9415  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userQpMax=%d\n", p_cfg->i32userQpMax);
9416  ni_log2(p_ctx, NI_LOG_DEBUG, "enableSSIM=%d\n", p_cfg->ui8enableSSIM);
9417  // AVC only
9418  ni_log2(p_ctx, NI_LOG_DEBUG, "i32maxIntraSize=%d\n", p_cfg->i32maxIntraSize);
9419  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userMaxDeltaQp=%d\n", p_cfg->i32userMaxDeltaQp);
9420  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userMinDeltaQp=%d\n", p_cfg->i32userMinDeltaQp);
9421  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userQpMin=%d\n", p_cfg->i32userQpMin);
9422  ni_log2(p_ctx, NI_LOG_DEBUG, "i32bitRate=%d\n", p_cfg->i32bitRate);
9423  ni_log2(p_ctx, NI_LOG_DEBUG, "i32bitRateBL=%d\n", p_cfg->i32bitRateBL);
9424  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8rcEnable=%d\n", p_cfg->ui8rcEnable);
9425  ni_log2(p_ctx, NI_LOG_DEBUG, "i32srcBitDepth=%d\n", p_cfg->i32srcBitDepth);
9426  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8enablePTS=%d\n", p_cfg->ui8enablePTS);
9427  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8lowLatencyMode=%d\n", p_cfg->ui8lowLatencyMode);
9428  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32sourceEndian=%u\n", p_cfg->ui32sourceEndian);
9429  ni_log2(p_ctx, NI_LOG_DEBUG, "hdrEnableVUI=%u\n", p_cfg->hdrEnableVUI);
9430  ni_log2(p_ctx, NI_LOG_DEBUG, "i32hwframes=%i\n", p_cfg->i32hwframes);
9431 
9432  ni_log2(p_ctx, NI_LOG_DEBUG, "** ni_t408_config_t: \n");
9433  ni_log2(p_ctx, NI_LOG_DEBUG, "profile=%d\n", p_t408->profile);
9434  ni_log2(p_ctx, NI_LOG_DEBUG, "level=%d\n", p_t408->level);
9435  ni_log2(p_ctx, NI_LOG_DEBUG, "tier=%d\n", p_t408->tier);
9436 
9437  ni_log2(p_ctx, NI_LOG_DEBUG, "internalBitDepth=%d\n", p_t408->internalBitDepth);
9438  ni_log2(p_ctx, NI_LOG_DEBUG, "losslessEnable=%d\n", p_t408->losslessEnable);
9439  ni_log2(p_ctx, NI_LOG_DEBUG, "constIntraPredFlag=%d\n", p_t408->constIntraPredFlag);
9440 
9441  ni_log2(p_ctx, NI_LOG_DEBUG, "decoding_refresh_type=%d\n", p_t408->decoding_refresh_type);
9442  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_qp=%d\n", p_t408->intra_qp);
9443  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_period=%d\n", p_t408->intra_period);
9444  ni_log2(p_ctx, NI_LOG_DEBUG, "roi_enable=%d\n", p_t408->roiEnable);
9445 
9446  ni_log2(p_ctx, NI_LOG_DEBUG, "useLongTerm=%u\n", p_t408->useLongTerm);
9447  ni_log2(p_ctx, NI_LOG_DEBUG, "setLongTermInterval=%u\n", p_cfg->ui32setLongTermInterval);
9448  ni_log2(p_ctx, NI_LOG_DEBUG, "setLongTermCount=%u\n", p_cfg->ui8setLongTermCount);
9449 
9450  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_top=%d\n", p_t408->conf_win_top);
9451  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_bottom=%d\n", p_t408->conf_win_bottom);
9452  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_left=%d\n", p_t408->conf_win_left);
9453  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_right=%d\n", p_t408->conf_win_right);
9454 
9455  ni_log2(p_ctx, NI_LOG_DEBUG, "independSliceMode=%d\n", p_t408->independSliceMode);
9456  ni_log2(p_ctx, NI_LOG_DEBUG, "independSliceModeArg=%d\n", p_t408->independSliceModeArg);
9457 
9458  ni_log2(p_ctx, NI_LOG_DEBUG, "dependSliceMode=%d\n", p_t408->dependSliceMode);
9459  ni_log2(p_ctx, NI_LOG_DEBUG, "dependSliceModeArg=%d\n", p_t408->dependSliceModeArg);
9460 
9461  ni_log2(p_ctx, NI_LOG_DEBUG, "intraRefreshMode=%d\n", p_t408->intraRefreshMode);
9462 
9463  ni_log2(p_ctx, NI_LOG_DEBUG, "intraRefreshArg=%d\n", p_t408->intraRefreshArg);
9464 
9465  ni_log2(p_ctx, NI_LOG_DEBUG, "use_recommend_enc_params=%d\n", p_t408->use_recommend_enc_params);
9466  ni_log2(p_ctx, NI_LOG_DEBUG, "scalingListEnable=%d\n", p_t408->scalingListEnable);
9467 
9468  ni_log2(p_ctx, NI_LOG_DEBUG, "cu_size_mode=%d\n", p_t408->cu_size_mode);
9469  ni_log2(p_ctx, NI_LOG_DEBUG, "tmvpEnable=%d\n", p_t408->tmvpEnable);
9470  ni_log2(p_ctx, NI_LOG_DEBUG, "wppEnable=%d\n", p_t408->wppEnable);
9471  ni_log2(p_ctx, NI_LOG_DEBUG, "max_num_merge=%d\n", p_t408->max_num_merge);
9472  ni_log2(p_ctx, NI_LOG_DEBUG, "disableDeblk=%d\n", p_t408->disableDeblk);
9473  ni_log2(p_ctx, NI_LOG_DEBUG, "lfCrossSliceBoundaryEnable=%d\n", p_t408->lfCrossSliceBoundaryEnable);
9474  ni_log2(p_ctx, NI_LOG_DEBUG, "betaOffsetDiv2=%d\n", p_t408->betaOffsetDiv2);
9475  ni_log2(p_ctx, NI_LOG_DEBUG, "tcOffsetDiv2=%d\n", p_t408->tcOffsetDiv2);
9476  ni_log2(p_ctx, NI_LOG_DEBUG, "skipIntraTrans=%d\n", p_t408->skipIntraTrans);
9477  ni_log2(p_ctx, NI_LOG_DEBUG, "saoEnable=%d\n", p_t408->saoEnable);
9478  ni_log2(p_ctx, NI_LOG_DEBUG, "intraNxNEnable=%d\n", p_t408->intraNxNEnable);
9479  ni_log2(p_ctx, NI_LOG_DEBUG, "bitAllocMode=%d\n", p_t408->bitAllocMode);
9480 
9481  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_cu_level_rate_control=%d\n", p_t408->enable_cu_level_rate_control);
9482 
9483  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_hvs_qp=%d\n", p_t408->enable_hvs_qp);
9484 
9485  ni_log2(p_ctx, NI_LOG_DEBUG, "hvs_qp_scale=%d\n", p_t408->hvs_qp_scale);
9486 
9487  ni_log2(p_ctx, NI_LOG_DEBUG, "max_delta_qp=%d\n", p_t408->max_delta_qp);
9488 
9489  // CUSTOM_GOP
9490  ni_log2(p_ctx, NI_LOG_DEBUG, "gop_preset_index=%d\n", p_t408->gop_preset_index);
9491 #ifndef QUADRA
9492  if (!QUADRA)
9493  {
9494  if (p_t408->gop_preset_index == GOP_PRESET_IDX_CUSTOM)
9495  {
9496  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_t408->custom_gop_params.custom_gop_size);
9497  for (i = 0; i < 8; i++)
9498  //for (i = 0; i < p_t408->custom_gop_params.custom_gop_size; i++)
9499  {
9500  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].pic_type=%d\n", i, p_t408->custom_gop_params.pic_param[i].pic_type);
9501  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].poc_offset=%d\n", i, p_t408->custom_gop_params.pic_param[i].poc_offset);
9502  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].pic_qp=%d\n", i, p_t408->custom_gop_params.pic_param[i].pic_qp);
9503  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].num_ref_pic_L0=%d\n", i, p_t408->custom_gop_params.pic_param[i].num_ref_pic_L0);
9504  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].ref_poc_L0=%d\n", i, p_t408->custom_gop_params.pic_param[i].ref_poc_L0);
9505  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].ref_poc_L1=%d\n", i, p_t408->custom_gop_params.pic_param[i].ref_poc_L1);
9506  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].temporal_id=%d\n", i, p_t408->custom_gop_params.pic_param[i].temporal_id);
9507  }
9508  }
9509  }
9510  else // QUADRA
9511 #endif
9512  {
9513  if (p_t408->custom_gop_params.custom_gop_size)
9514  {
9515  int j;
9516  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_t408->custom_gop_params.custom_gop_size);
9517  for (i = 0; i < NI_MAX_GOP_NUM; i++)
9518  {
9519  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].poc_offset=%d\n", i, p_t408->custom_gop_params.pic_param[i].poc_offset);
9520  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].qp_offset=%d\n", i, p_t408->custom_gop_params.pic_param[i].qp_offset);
9521  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].qp_factor=%f\n", i, p_t408->custom_gop_params.pic_param[i].qp_factor);
9522  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].temporal_id=%d\n", i, p_t408->custom_gop_params.pic_param[i].temporal_id);
9523  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].pic_type=%d\n", i, p_t408->custom_gop_params.pic_param[i].pic_type);
9524  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].num_ref_pics=%d\n", i, p_t408->custom_gop_params.pic_param[i].num_ref_pics);
9525  for (j = 0; j < NI_MAX_REF_PIC; j++)
9526  {
9527  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].rps[%d].ref_pic=%d\n", i, j, p_t408->custom_gop_params.pic_param[i].rps[j].ref_pic);
9528  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].rps[%d].ref_pic_used=%d\n", i, j, p_t408->custom_gop_params.pic_param[i].rps[j].ref_pic_used);
9529  }
9530  }
9531  }
9532  }
9533 
9534  ni_log2(p_ctx, NI_LOG_DEBUG, "roiEnable=%d\n", p_t408->roiEnable);
9535 
9536  ni_log2(p_ctx, NI_LOG_DEBUG, "numUnitsInTick=%u\n", p_t408->numUnitsInTick);
9537  ni_log2(p_ctx, NI_LOG_DEBUG, "timeScale=%u\n", p_t408->timeScale);
9538  ni_log2(p_ctx, NI_LOG_DEBUG, "numTicksPocDiffOne=%u\n", p_t408->numTicksPocDiffOne);
9539 
9540  ni_log2(p_ctx, NI_LOG_DEBUG, "chromaCbQpOffset=%d\n", p_t408->chromaCbQpOffset);
9541  ni_log2(p_ctx, NI_LOG_DEBUG, "chromaCrQpOffset=%d\n", p_t408->chromaCrQpOffset);
9542 
9543  ni_log2(p_ctx, NI_LOG_DEBUG, "initialRcQp=%d\n", p_t408->initialRcQp);
9544 
9545  ni_log2(p_ctx, NI_LOG_DEBUG, "nrYEnable=%u\n", p_t408->nrYEnable);
9546  ni_log2(p_ctx, NI_LOG_DEBUG, "nrCbEnable=%u\n", p_t408->nrCbEnable);
9547  ni_log2(p_ctx, NI_LOG_DEBUG, "nrCrEnable=%u\n", p_t408->nrCrEnable);
9548 
9549  // ENC_NR_WEIGHT
9550  ni_log2(p_ctx, NI_LOG_DEBUG, "nrIntraWeightY=%u\n", p_t408->nrIntraWeightY);
9551  ni_log2(p_ctx, NI_LOG_DEBUG, "nrIntraWeightCb=%u\n", p_t408->nrIntraWeightCb);
9552  ni_log2(p_ctx, NI_LOG_DEBUG, "nrIntraWeightCr=%u\n", p_t408->nrIntraWeightCr);
9553  ni_log2(p_ctx, NI_LOG_DEBUG, "nrInterWeightY=%u\n", p_t408->nrInterWeightY);
9554  ni_log2(p_ctx, NI_LOG_DEBUG, "nrInterWeightCb=%u\n", p_t408->nrInterWeightCb);
9555  ni_log2(p_ctx, NI_LOG_DEBUG, "nrInterWeightCr=%u\n", p_t408->nrInterWeightCr);
9556 
9557  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseEstEnable=%u\n", p_t408->nrNoiseEstEnable);
9558  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseSigmaY=%u\n", p_t408->nrNoiseSigmaY);
9559  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseSigmaCb=%u\n", p_t408->nrNoiseSigmaCb);
9560  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseSigmaCr=%u\n", p_t408->nrNoiseSigmaCr);
9561 
9562  // newly added for T408
9563  ni_log2(p_ctx, NI_LOG_DEBUG, "monochromeEnable=%u\n", p_t408->monochromeEnable);
9564  ni_log2(p_ctx, NI_LOG_DEBUG, "strongIntraSmoothEnable=%u\n",
9565  p_t408->strongIntraSmoothEnable);
9566 
9567  ni_log2(p_ctx, NI_LOG_DEBUG, "weightPredEnable=%u\n", p_t408->weightPredEnable);
9568  ni_log2(p_ctx, NI_LOG_DEBUG, "bgDetectEnable=%u\n", p_t408->bgDetectEnable);
9569  ni_log2(p_ctx, NI_LOG_DEBUG, "bgThrDiff=%u\n", p_t408->bgThrDiff);
9570  ni_log2(p_ctx, NI_LOG_DEBUG, "bgThrMeanDiff=%u\n", p_t408->bgThrMeanDiff);
9571  ni_log2(p_ctx, NI_LOG_DEBUG, "bgLambdaQp=%u\n", p_t408->bgLambdaQp);
9572  ni_log2(p_ctx, NI_LOG_DEBUG, "bgDeltaQp=%d\n", p_t408->bgDeltaQp);
9573 
9574  ni_log2(p_ctx, NI_LOG_DEBUG, "customLambdaEnable=%u\n", p_t408->customLambdaEnable);
9575  ni_log2(p_ctx, NI_LOG_DEBUG, "customMDEnable=%u\n", p_t408->customMDEnable);
9576  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04DeltaRate=%d\n", p_t408->pu04DeltaRate);
9577  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08DeltaRate=%d\n", p_t408->pu08DeltaRate);
9578  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16DeltaRate=%d\n", p_t408->pu16DeltaRate);
9579  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32DeltaRate=%d\n", p_t408->pu32DeltaRate);
9580  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04IntraPlanarDeltaRate=%d\n", p_t408->pu04IntraPlanarDeltaRate);
9581  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04IntraDcDeltaRate=%d\n", p_t408->pu04IntraDcDeltaRate);
9582  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04IntraAngleDeltaRate=%d\n", p_t408->pu04IntraAngleDeltaRate);
9583  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08IntraPlanarDeltaRate=%d\n", p_t408->pu08IntraPlanarDeltaRate);
9584  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08IntraDcDeltaRate=%d\n", p_t408->pu08IntraDcDeltaRate);
9585  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08IntraAngleDeltaRate=%d\n", p_t408->pu08IntraAngleDeltaRate);
9586  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16IntraPlanarDeltaRate=%d\n", p_t408->pu16IntraPlanarDeltaRate);
9587  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16IntraDcDeltaRate=%d\n", p_t408->pu16IntraDcDeltaRate);
9588  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16IntraAngleDeltaRate=%d\n", p_t408->pu16IntraAngleDeltaRate);
9589  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32IntraPlanarDeltaRate=%d\n", p_t408->pu32IntraPlanarDeltaRate);
9590  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32IntraDcDeltaRate=%d\n", p_t408->pu32IntraDcDeltaRate);
9591  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32IntraAngleDeltaRate=%d\n", p_t408->pu32IntraAngleDeltaRate);
9592  ni_log2(p_ctx, NI_LOG_DEBUG, "cu08IntraDeltaRate=%d\n", p_t408->cu08IntraDeltaRate);
9593  ni_log2(p_ctx, NI_LOG_DEBUG, "cu08InterDeltaRate=%d\n", p_t408->cu08InterDeltaRate);
9594  ni_log2(p_ctx, NI_LOG_DEBUG, "cu08MergeDeltaRate=%d\n", p_t408->cu08MergeDeltaRate);
9595  ni_log2(p_ctx, NI_LOG_DEBUG, "cu16IntraDeltaRate=%d\n", p_t408->cu16IntraDeltaRate);
9596  ni_log2(p_ctx, NI_LOG_DEBUG, "cu16InterDeltaRate=%d\n", p_t408->cu16InterDeltaRate);
9597  ni_log2(p_ctx, NI_LOG_DEBUG, "cu16MergeDeltaRate=%d\n", p_t408->cu16MergeDeltaRate);
9598  ni_log2(p_ctx, NI_LOG_DEBUG, "cu32IntraDeltaRate=%d\n", p_t408->cu32IntraDeltaRate);
9599  ni_log2(p_ctx, NI_LOG_DEBUG, "cu32InterDeltaRate=%d\n", p_t408->cu32InterDeltaRate);
9600  ni_log2(p_ctx, NI_LOG_DEBUG, "cu32MergeDeltaRate=%d\n", p_t408->cu32MergeDeltaRate);
9601  ni_log2(p_ctx, NI_LOG_DEBUG, "coefClearDisable=%d\n", p_t408->coefClearDisable);
9602  ni_log2(p_ctx, NI_LOG_DEBUG, "minQpI=%d\n", p_t408->minQpI);
9603  ni_log2(p_ctx, NI_LOG_DEBUG, "maxQpI=%d\n", p_t408->maxQpI);
9604  ni_log2(p_ctx, NI_LOG_DEBUG, "minQpP=%d\n", p_t408->minQpP);
9605  ni_log2(p_ctx, NI_LOG_DEBUG, "maxQpP=%d\n", p_t408->maxQpP);
9606  ni_log2(p_ctx, NI_LOG_DEBUG, "minQpB=%d\n", p_t408->minQpB);
9607  ni_log2(p_ctx, NI_LOG_DEBUG, "maxQpB=%d\n", p_t408->maxQpB);
9608 
9609  // for H.264 on T408
9610  ni_log2(p_ctx, NI_LOG_DEBUG, "avcIdrPeriod=%d\n", p_t408->avcIdrPeriod);
9611  ni_log2(p_ctx, NI_LOG_DEBUG, "rdoSkip=%d\n", p_t408->rdoSkip);
9612  ni_log2(p_ctx, NI_LOG_DEBUG, "lambdaScalingEnable=%d\n", p_t408->lambdaScalingEnable);
9613  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_transform_8x8=%d\n", p_t408->enable_transform_8x8);
9614  ni_log2(p_ctx, NI_LOG_DEBUG, "slice_mode=%d\n", p_t408->slice_mode);
9615  ni_log2(p_ctx, NI_LOG_DEBUG, "slice_arg=%d\n", p_t408->slice_arg);
9616  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_mb_refresh_mode=%d\n", p_t408->intra_mb_refresh_mode);
9617  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_mb_refresh_arg=%d\n", p_t408->intra_mb_refresh_arg);
9618  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_mb_level_rc=%d\n", p_t408->enable_mb_level_rc);
9619  ni_log2(p_ctx, NI_LOG_DEBUG, "entropy_coding_mode=%d\n", p_t408->entropy_coding_mode);
9620  ni_log2(p_ctx, NI_LOG_DEBUG, "forcedHeaderEnable=%u\n", p_t408->forcedHeaderEnable);
9621 
9622  //QUADRA
9623  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableAUD=%d\n", p_cfg->ui8EnableAUD);
9624  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8LookAheadDepth=%d\n", p_cfg->ui8LookAheadDepth);
9625  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8rdoLevel=%d\n", p_cfg->ui8rdoLevel);
9626  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crf=%d\n", p_cfg->i8crf);
9627  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crfDecimal=%d\n", p_cfg->i8crfDecimal);
9628  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16HDR10MaxLight=%d\n", p_cfg->ui16HDR10MaxLight);
9629  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16HDR10AveLight=%d\n", p_cfg->ui16HDR10AveLight);
9630  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8HDR10CLLEnable=%d\n", p_cfg->ui8HDR10CLLEnable);
9631  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableRdoQuant=%d\n", p_cfg->ui8EnableRdoQuant);
9632  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8ctbRcMode=%d\n", p_cfg->ui8ctbRcMode);
9633  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8gopSize=%d\n", p_cfg->ui8gopSize);
9634  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8useLowDelayPocType=%d\n", p_cfg->ui8useLowDelayPocType);
9635  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8gopLowdelay=%d\n", p_cfg->ui8gopLowdelay);
9636  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16gdrDuration=%d\n", p_cfg->ui16gdrDuration);
9637  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8hrdEnable=%d\n", p_cfg->ui8hrdEnable);
9638  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorDescPresent=%d\n", p_cfg->ui8colorDescPresent);
9639  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorPrimaries=%d\n", p_cfg->ui8colorPrimaries);
9640  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorTrc=%d\n", p_cfg->ui8colorTrc);
9641  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorSpace=%d\n", p_cfg->ui8colorSpace);
9642  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16aspectRatioWidth=%d\n", p_cfg->ui16aspectRatioWidth);
9643  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16aspectRatioHeight=%d\n", p_cfg->ui16aspectRatioHeight);
9644  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16rootBufId=%d\n", p_cfg->ui16rootBufId);
9645  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8planarFormat=%d\n", p_cfg->ui8planarFormat);
9646  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8PixelFormat=%d\n", p_cfg->ui8PixelFormat);
9647  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ltrRefInterval=%u\n", p_cfg->ui32ltrRefInterval);
9648  ni_log2(p_ctx, NI_LOG_DEBUG, "i32ltrRefQpOffset=%d\n", p_cfg->i32ltrRefQpOffset);
9649  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ltrFirstGap=%u\n", p_cfg->ui32ltrFirstGap);
9650  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ltrNextInterval=%u\n", p_cfg->ui32ltrNextInterval);
9651  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8multicoreJointMode=%d\n", p_cfg->ui8multicoreJointMode);
9652  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8videoFullRange=%u\n", p_cfg->ui8videoFullRange);
9653  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32QLevel=%u\n", p_cfg->ui32QLevel);
9654  ni_log2(p_ctx, NI_LOG_DEBUG, "i8chromaQpOffset=%d\n", p_cfg->i8chromaQpOffset);
9655  ni_log2(p_ctx, NI_LOG_DEBUG, "i32tolCtbRcInter=0x%x\n", p_cfg->i32tolCtbRcInter);
9656  ni_log2(p_ctx, NI_LOG_DEBUG, "i32tolCtbRcIntra=0x%x\n", p_cfg->i32tolCtbRcIntra);
9657  ni_log2(p_ctx, NI_LOG_DEBUG, "i16bitrateWindow=%d\n", p_cfg->i16bitrateWindow);
9658  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8inLoopDSRatio=%u\n", p_cfg->ui8inLoopDSRatio);
9659  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8blockRCSize=%u\n", p_cfg->ui8blockRCSize);
9660  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8rcQpDeltaRange=%u\n", p_cfg->ui8rcQpDeltaRange);
9661  ni_log2(p_ctx, NI_LOG_DEBUG, "i16ctbRowQpStep=%u\n", p_cfg->i16ctbRowQpStep);
9662  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8NewRCEnable=%u\n", p_cfg->ui8NewRCEnable);
9663  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8LowDelay=%d\n", p_cfg->ui8LowDelay);
9664  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8hdr10_enable=%u\n", p_cfg->ui8hdr10_enable);
9665  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dx0=%u\n", p_cfg->ui16hdr10_dx0);
9666  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dy0=%u\n", p_cfg->ui16hdr10_dy0);
9667  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dx1=%u\n", p_cfg->ui16hdr10_dx1);
9668  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dy1=%u\n", p_cfg->ui16hdr10_dy1);
9669  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dx2=%u\n", p_cfg->ui16hdr10_dx2);
9670  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dy2=%u\n", p_cfg->ui16hdr10_dy2);
9671  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_wx=%u\n", p_cfg->ui16hdr10_wx);
9672  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_wy=%u\n", p_cfg->ui16hdr10_wy);
9673  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32hdr10_maxluma=%u\n", p_cfg->ui32hdr10_maxluma);
9674  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32hdr10_minluma=%u\n", p_cfg->ui32hdr10_minluma);
9675  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8avccHvcc=%u\n", p_cfg->ui8avccHvcc);
9676  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8av1ErrResilientMode=%u\n", p_cfg->ui8av1ErrResilientMode);
9677  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8intraResetRefresh=%d\n", p_cfg->ui8intraResetRefresh);
9678  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8temporalLayersEnable=%u\n", p_cfg->ui8temporalLayersEnable);
9679  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8enable2PassGopPattern=%u\n", p_cfg->ui8enable2PassGopPatern);
9680  ni_log2(p_ctx, NI_LOG_DEBUG, "zerocopy_mode=%d\n", p_src->zerocopy_mode);
9681  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32lumaLinesize=%u\n", p_cfg->ui32lumaLinesize);
9682  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32chromaLinesize=%u\n", p_cfg->ui32chromaLinesize);
9683  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32cropWidth=%u\n", p_cfg->ui32cropWidth);
9684  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32cropHeight=%u\n", p_cfg->ui32cropHeight);
9685  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32horOffset=%u\n", p_cfg->ui32horOffset);
9686  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32verOffset=%u\n", p_cfg->ui32verOffset);
9687  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crfMax=%d\n", p_cfg->i8crfMax);
9688  ni_log2(p_ctx, NI_LOG_DEBUG, "i32qcomp=%d\n", p_cfg->i32qcomp);
9689  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8noMbtree=%u\n", p_cfg->ui8noMbtree);
9690  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8noHWMultiPassSupport=%u\n", p_cfg->ui8noHWMultiPassSupport);
9691  ni_log2(p_ctx, NI_LOG_DEBUG, "i8cuTreeFactor=%d\n", p_cfg->i8cuTreeFactor);
9692  ni_log2(p_ctx, NI_LOG_DEBUG, "i32ipRatio=%d\n", p_cfg->i32ipRatio);
9693  ni_log2(p_ctx, NI_LOG_DEBUG, "i32pbRatio=%d\n", p_cfg->i32pbRatio);
9694  ni_log2(p_ctx, NI_LOG_DEBUG, "i32cplxDecay=%d\n", p_cfg->i32cplxDecay);
9695  ni_log2(p_ctx, NI_LOG_DEBUG, "i8ppsInitQp=%d\n", p_cfg->i8ppsInitQp);
9696  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8bitrateMode=%u\n", p_cfg->ui8bitrateMode);
9697  ni_log2(p_ctx, NI_LOG_DEBUG, "i8pass1Qp=%d\n", p_cfg->i8pass1Qp);
9698  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crfDecimal=%d\n", p_cfg->i8crfDecimal);
9699  ni_log2(p_ctx, NI_LOG_DEBUG, "i8hvsBaseMbComplexity=%u\n", p_cfg->i8hvsBaseMbComplexity);
9700  ni_log2(p_ctx, NI_LOG_DEBUG, "i8enableipRatio=%d\n",p_cfg->i8enableipRatio);
9701  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16iFrameSizeRatio=%u\n",p_cfg->ui16iFrameSizeRatio);
9702  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8crfMaxIframeEnable=%u\n", p_cfg->ui8crfMaxIframeEnable);
9703  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32vbvMinRate=%u\n", p_cfg->ui32vbvMinRate);
9704  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8disableBframeRDOQ=%u\n", p_cfg->ui8disableBframeRDOQ);
9705  ni_log2(p_ctx, NI_LOG_DEBUG, "i32forceBframeQpFactor=%d\n", p_cfg->i32forceBframeQpFactor);
9706  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8tuneBframeVisual=%u\n", p_cfg->ui8tuneBframeVisual);
9707  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableAcqLimit=%u\n", p_cfg->ui8EnableAcqLimit);
9708  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8compressor=%u\n", p_cfg->ui8compressor);
9709  ni_log2(p_ctx, NI_LOG_DEBUG, "u8customizeRoiQpLevel=%u\n", p_cfg->u8customizeRoiQpLevel);
9710  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8motionConstrainedMode=%d\n", p_cfg->ui8motionConstrainedMode);
9711  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8mallocStrategy=%d\n", p_cfg->ui8mallocStrategy);
9712  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8spatialLayersMinusOne=%d\n", p_cfg->ui8spatialLayersMinusOne);
9713  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8enableTimecode=%d\n", p_cfg->ui8enableTimecode);
9714  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8spatialLayersRefBaseLayer=%d\n", p_cfg->ui8spatialLayersRefBaseLayer);
9715  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8vbvBufferReencode=%d\n", p_cfg->ui8vbvBufferReencode);
9716  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8totalCuTreeDepth=%u\n", p_cfg->ui8totalCuTreeDepth);
9717  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8adaptiveCuTree=%u\n", p_cfg->ui8adaptiveCuTree);
9718  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8preIntraHandling=%u\n", p_cfg->ui8preIntraHandling);
9719  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8baseLayerOnly=%u\n", p_cfg->ui8baseLayerOnly);
9720  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8pastFrameMaxIntraRatio=%u\n", p_cfg->ui8pastFrameMaxIntraRatio);
9721  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8linkFrameMaxIntraRatio=%u\n", p_cfg->ui8linkFrameMaxIntraRatio);
9722  ni_log2(p_ctx, NI_LOG_DEBUG, "i32spatialLayerBitrate=%d,%d,%d,%d\n", p_cfg->i32spatialLayerBitrate[0],
9723  p_cfg->i32spatialLayerBitrate[1], p_cfg->i32spatialLayerBitrate[2], p_cfg->i32spatialLayerBitrate[3]);
9724 }
9725 
9726 /*!******************************************************************************
9727  * \brief Setup and initialize all xcoder configuration to default (Rev. B)
9728  *
9729  * \param
9730  *
9731  * \return
9732  ******************************************************************************/
9734 {
9735  int i = 0;
9736 
9737  if( (!p_ctx) || (!p_config) )
9738  {
9739  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
9740  __func__);
9741  return;
9742  }
9743 
9744  memset(p_config, 0, sizeof(ni_encoder_config_t));
9745 
9746  // fill in common attributes values
9747  p_config->i32picWidth = 720;
9748  p_config->i32picHeight = 480;
9749  p_config->i32meBlkMode = 0; // (AVC ONLY) 0 means use all possible block partitions
9750  p_config->ui8sliceMode = 0; // 0 means 1 slice per picture
9751  p_config->i32frameRateInfo = 30;
9752  p_config->i32frameRateDenominator = 1;
9753  p_config->i32vbvBufferSize = 0; //0; parameter is ignored if rate control is off, if rate control is on, 0 means do not check vbv constraints
9754  p_config->i32userQpMax = 51; // this should also be h264-only parameter
9755 
9756  // AVC only
9757  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
9758  {
9759  p_config->i32maxIntraSize = 8000000; // how big an intra p_frame can get?
9760  p_config->i32userMaxDeltaQp = 51;
9761  p_config->i32userMinDeltaQp = 51;
9762  p_config->i32userQpMin = 8;
9763  }
9764 
9765  p_config->i32bitRate = 0;
9766  p_config->i32bitRateBL = 0;
9767  p_config->ui8rcEnable = 0;
9768  p_config->i32srcBitDepth = p_ctx->src_bit_depth;
9769  p_config->ui8enablePTS = 0;
9770  p_config->ui8lowLatencyMode = 0;
9771 
9772  // profiles for H.264: 1 = baseline, 2 = main, 3 = extended, 4 = high
9773  // 5 = high10 (default 8 bit: 4, 10 bit: 5)
9774  // profiles for HEVC: 1 = main, 2 = main10 (default 8 bit: 1, 10 bit: 2)
9775 
9776  // bitstream type: H.264 or HEVC
9777  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
9778  {
9779  p_config->ui8bitstreamFormat = STD_AVC;
9780 
9781  p_config->niParamT408.profile = 4;
9782  if (10 == p_ctx->src_bit_depth)
9783  {
9784  p_config->niParamT408.profile = 5;
9785  }
9786  } else if (NI_CODEC_FORMAT_JPEG == p_ctx->codec_format)
9787  {
9788  p_config->ui8bitstreamFormat = STD_JPEG;
9789  } else if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
9790  {
9791  p_config->ui8bitstreamFormat = STD_AV1;
9792 
9793  p_config->niParamT408.profile = 1;
9794  } else
9795  {
9797 
9798  p_config->ui8bitstreamFormat = STD_HEVC;
9799 
9800  p_config->niParamT408.profile = 1;
9801  if (10 == p_ctx->src_bit_depth)
9802  {
9803  p_config->niParamT408.profile = 2;
9804  }
9805  }
9806 
9807  p_config->hdrEnableVUI = 0;
9808  p_config->ui32sourceEndian = p_ctx->src_endian;
9809 
9810  p_config->niParamT408.level = 0;
9811  p_config->niParamT408.tier = 0; // 0 means main tier
9812 
9813  p_config->niParamT408.internalBitDepth = p_ctx->src_bit_depth;
9814  p_config->niParamT408.losslessEnable = 0;
9815  p_config->niParamT408.constIntraPredFlag = 0;
9816 
9817  if (QUADRA)
9819  else
9821 
9822  p_config->niParamT408.decoding_refresh_type = 1;
9823  p_config->niParamT408.intra_qp = 22;
9824  // avcIdrPeriod (H.264 on T408), NOT shared with intra_period
9825  p_config->niParamT408.intra_period = 120;
9826  p_config->niParamT408.avcIdrPeriod = 120;
9827 
9828  p_config->niParamT408.conf_win_top = 0;
9829  p_config->niParamT408.conf_win_bottom = 0;
9830  p_config->niParamT408.conf_win_left = 0;
9831  p_config->niParamT408.conf_win_right = 0;
9832 
9833  p_config->niParamT408.independSliceMode = 0;
9834  p_config->niParamT408.independSliceModeArg = 0;
9835  p_config->niParamT408.dependSliceMode = 0;
9836  p_config->niParamT408.dependSliceModeArg = 0;
9837  p_config->niParamT408.intraRefreshMode = 0;
9838  p_config->niParamT408.intraRefreshArg = 0;
9839 
9840  p_config->niParamT408.use_recommend_enc_params = 0; //1;
9841  p_config->niParamT408.scalingListEnable = 0;
9842 
9843  p_config->niParamT408.cu_size_mode = 7;
9844  p_config->niParamT408.tmvpEnable = 1;
9845  p_config->niParamT408.wppEnable = 0;
9846  p_config->niParamT408.max_num_merge = 2;
9847  p_config->niParamT408.disableDeblk = 0;
9849  p_config->niParamT408.betaOffsetDiv2 = 0;
9850  p_config->niParamT408.tcOffsetDiv2 = 0;
9851  p_config->niParamT408.skipIntraTrans = 1;
9852  p_config->niParamT408.saoEnable = 1;
9853  p_config->niParamT408.intraNxNEnable = 1;
9854 
9855  p_config->niParamT408.bitAllocMode = 0;
9856 
9857  for (i = 0; i < NI_MAX_GOP_NUM; i++)
9858  {
9859  p_config->niParamT408.fixedBitRatio[i] = 1;
9860  }
9861 
9862  if (QUADRA)
9864  else
9866 
9867  p_config->niParamT408.enable_hvs_qp = 0;
9868  p_config->niParamT408.hvs_qp_scale = 2;
9869 
9870  p_config->niParamT408.max_delta_qp = 10;
9871 
9872  // CUSTOM_GOP
9874 #ifndef QUADRA
9875  if (!QUADRA)
9876  {
9877  for (i = 0; i < p_config->niParamT408.custom_gop_params.custom_gop_size; i++)
9878  {
9881  p_config->niParamT408.custom_gop_params.pic_param[i].pic_qp = 0;
9882  p_config->niParamT408.custom_gop_params.pic_param[i].num_ref_pic_L0 = 0;
9883  p_config->niParamT408.custom_gop_params.pic_param[i].ref_poc_L0 = 0;
9884  p_config->niParamT408.custom_gop_params.pic_param[i].ref_poc_L1 = 0;
9886  }
9887  }
9888  else // QUADRA
9889 #endif
9890  {
9891  int j;
9892  for (i = 0; i < NI_MAX_GOP_NUM; i++)
9893  {
9897  (float)0.3; // QP Factor range is between 0.3 and 1, higher values mean lower quality and less bits
9901  for (j = 0; j < NI_MAX_REF_PIC; j++)
9902  {
9903  p_config->niParamT408.custom_gop_params.pic_param[i].rps[j].ref_pic = 0;
9905  }
9906  }
9907  }
9908 
9909  p_config->niParamT408.roiEnable = 0;
9910 
9911  p_config->niParamT408.numUnitsInTick = 1000;
9912  p_config->niParamT408.timeScale = p_config->i32frameRateInfo * 1000;
9913  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
9914  {
9915  p_config->niParamT408.timeScale *= 2;
9916  }
9917 
9918  p_config->niParamT408.numTicksPocDiffOne = 0;
9919 
9920  p_config->niParamT408.chromaCbQpOffset = 0;
9921  p_config->niParamT408.chromaCrQpOffset = 0;
9922 
9923  p_config->niParamT408.initialRcQp = 63; //-1;
9924 
9925  p_config->niParamT408.nrYEnable = 0;
9926  p_config->niParamT408.nrCbEnable = 0;
9927  p_config->niParamT408.nrCrEnable = 0;
9928 
9929  // ENC_NR_WEIGHT
9930  p_config->niParamT408.nrIntraWeightY = 7;
9931  p_config->niParamT408.nrIntraWeightCb = 7;
9932  p_config->niParamT408.nrIntraWeightCr = 7;
9933  p_config->niParamT408.nrInterWeightY = 4;
9934  p_config->niParamT408.nrInterWeightCb = 4;
9935  p_config->niParamT408.nrInterWeightCr = 4;
9936 
9937  p_config->niParamT408.nrNoiseEstEnable = 0;
9938  p_config->niParamT408.nrNoiseSigmaY = 0;
9939  p_config->niParamT408.nrNoiseSigmaCb = 0;
9940  p_config->niParamT408.nrNoiseSigmaCr = 0;
9941 
9942  p_config->niParamT408.useLongTerm = 0;
9943 
9944  // newly added for T408
9945  p_config->niParamT408.monochromeEnable = 0;
9946  p_config->niParamT408.strongIntraSmoothEnable = 1;
9947 
9948  p_config->niParamT408.weightPredEnable = 0;
9949  p_config->niParamT408.bgDetectEnable = 0;
9950  p_config->niParamT408.bgThrDiff = 8; // matching the C-model
9951  p_config->niParamT408.bgThrMeanDiff = 1; // matching the C-model
9952  p_config->niParamT408.bgLambdaQp = 32; // matching the C-model
9953  p_config->niParamT408.bgDeltaQp = 3; // matching the C-model
9954 
9955  p_config->niParamT408.customLambdaEnable = 0;
9956  p_config->niParamT408.customMDEnable = 0;
9957  p_config->niParamT408.pu04DeltaRate = 0;
9958  p_config->niParamT408.pu08DeltaRate = 0;
9959  p_config->niParamT408.pu16DeltaRate = 0;
9960  p_config->niParamT408.pu32DeltaRate = 0;
9961  p_config->niParamT408.pu04IntraPlanarDeltaRate = 0;
9962  p_config->niParamT408.pu04IntraDcDeltaRate = 0;
9963  p_config->niParamT408.pu04IntraAngleDeltaRate = 0;
9964  p_config->niParamT408.pu08IntraPlanarDeltaRate = 0;
9965  p_config->niParamT408.pu08IntraDcDeltaRate = 0;
9966  p_config->niParamT408.pu08IntraAngleDeltaRate = 0;
9967  p_config->niParamT408.pu16IntraPlanarDeltaRate = 0;
9968  p_config->niParamT408.pu16IntraDcDeltaRate = 0;
9969  p_config->niParamT408.pu16IntraAngleDeltaRate = 0;
9970  p_config->niParamT408.pu32IntraPlanarDeltaRate = 0;
9971  p_config->niParamT408.pu32IntraDcDeltaRate = 0;
9972  p_config->niParamT408.pu32IntraAngleDeltaRate = 0;
9973  p_config->niParamT408.cu08IntraDeltaRate = 0;
9974  p_config->niParamT408.cu08InterDeltaRate = 0;
9975  p_config->niParamT408.cu08MergeDeltaRate = 0;
9976  p_config->niParamT408.cu16IntraDeltaRate = 0;
9977  p_config->niParamT408.cu16InterDeltaRate = 0;
9978  p_config->niParamT408.cu16MergeDeltaRate = 0;
9979  p_config->niParamT408.cu32IntraDeltaRate = 0;
9980  p_config->niParamT408.cu32InterDeltaRate = 0;
9981  p_config->niParamT408.cu32MergeDeltaRate = 0;
9982  p_config->niParamT408.coefClearDisable = 0;
9983  p_config->niParamT408.minQpI = 8;
9984  p_config->niParamT408.maxQpI = 51;
9985  p_config->niParamT408.minQpP = 8;
9986  p_config->niParamT408.maxQpP = 51;
9987  p_config->niParamT408.minQpB = 8;
9988  p_config->niParamT408.maxQpB = 51;
9989 
9990  // for H.264 on T408
9991  p_config->niParamT408.rdoSkip = 0;
9992  p_config->niParamT408.lambdaScalingEnable = 0;
9993  p_config->niParamT408.enable_transform_8x8 = 1;
9994  p_config->niParamT408.slice_mode = 0;
9995  p_config->niParamT408.slice_arg = 0;
9996  p_config->niParamT408.intra_mb_refresh_mode = 0;
9997  p_config->niParamT408.intra_mb_refresh_arg = 0;
9998  if (QUADRA)
9999  p_config->niParamT408.enable_mb_level_rc = 0;
10000  else
10001  p_config->niParamT408.enable_mb_level_rc = 1;
10002  p_config->niParamT408.entropy_coding_mode = 1; // 1 means CABAC, make sure profile is main or above, can't have CABAC in baseline
10003  p_config->niParamT408.forcedHeaderEnable = 0; // first IDR frame
10004 
10005  //QUADRA
10006  p_config->ui8EnableAUD = 0;
10007  p_config->ui8LookAheadDepth = 0;
10008  p_config->ui8rdoLevel = (NI_CODEC_FORMAT_JPEG == p_ctx->codec_format) ? 0 : 1;
10009  p_config->i8crf = -1;
10010  p_config->ui16HDR10MaxLight = 0;
10011  p_config->ui16HDR10AveLight = 0;
10012  p_config->ui8HDR10CLLEnable = 0;
10013  p_config->ui8EnableRdoQuant = 0;
10014  p_config->ui8ctbRcMode = 0;
10015  p_config->ui8gopSize = 0;
10016  p_config->ui8useLowDelayPocType = 0;
10017  p_config->ui8gopLowdelay = 0;
10018  p_config->ui16gdrDuration = 0;
10019  p_config->ui8hrdEnable = 0;
10020  p_config->ui8colorDescPresent = 0;
10022  p_config->ui8colorPrimaries = 2;
10023  //AVCOL_TRC_UNSPECIFIED
10024  p_config->ui8colorTrc = 2;
10025  //AVCOL_SPC_UNSPECIFIED
10026  p_config->ui8colorSpace = 2;
10027  p_config->ui16aspectRatioWidth = 0;
10028  p_config->ui16aspectRatioHeight = 0;
10030  p_config->ui8PixelFormat = NI_PIX_FMT_YUV420P;
10031  p_config->ui32ltrRefInterval = 0;
10032  p_config->i32ltrRefQpOffset = 0;
10033  p_config->ui32ltrFirstGap= 0;
10034  p_config->ui32ltrNextInterval = 1;
10035  p_config->ui8multicoreJointMode = 0;
10036  p_config->ui8videoFullRange = 0;
10037  p_config->ui32setLongTermInterval = 0;
10038  p_config->ui8setLongTermCount = 2;
10039  p_config->ui32QLevel = -1;
10040  p_config->i8chromaQpOffset = 0;
10041  p_config->i32tolCtbRcInter = (int32_t)(0.1 * 1000);
10042  p_config->i32tolCtbRcIntra = (int32_t)(0.1 * 1000);
10043  p_config->i16bitrateWindow = -255;
10044  p_config->ui8inLoopDSRatio = 1;
10045  p_config->ui8blockRCSize = 0;
10046  p_config->ui8rcQpDeltaRange = 10;
10047  p_config->i16ctbRowQpStep = 0;
10048  p_config->ui8NewRCEnable = 255;
10049  p_config->ui8LowDelay = 0;
10050  p_config->ui8fixedframerate = 1;
10051  p_config->ui8enableSSIM = 0;
10052  p_config->ui8hdr10_enable = 0;
10053  p_config->ui16hdr10_dx0 = 0;
10054  p_config->ui16hdr10_dy0 = 0;
10055  p_config->ui16hdr10_dx1 = 0;
10056  p_config->ui16hdr10_dy1 = 0;
10057  p_config->ui16hdr10_dx2 = 0;
10058  p_config->ui16hdr10_dy2 = 0;
10059  p_config->ui16hdr10_wx = 0;
10060  p_config->ui16hdr10_wy = 0;
10061  p_config->ui32hdr10_maxluma = 0;
10062  p_config->ui32hdr10_minluma = 0;
10063  p_config->ui8avccHvcc = 0;
10064  p_config->ui8av1ErrResilientMode = 0;
10065  p_config->ui8intraResetRefresh = 0;
10066  p_config->ui8temporalLayersEnable = 0;
10067  p_config->ui8enable2PassGopPatern = 0;
10068  p_config->i8crfMax = (int8_t)(-1) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
10069  p_config->i32qcomp = (int32_t)(0.6 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
10070  p_config->ui8noMbtree = 0;
10071  p_config->ui8noHWMultiPassSupport = 0;
10072  p_config->i8cuTreeFactor = 5;
10073  p_config->i32ipRatio = (int32_t)(1.4 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
10074  p_config->i32pbRatio = (int32_t)(1.3 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
10075  p_config->i32cplxDecay = (int32_t)(0.5 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
10076  p_config->ui32vbvMaxRate = 0;
10077  p_config->i8ppsInitQp = (int8_t)(-1) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
10078  p_config->ui8bitrateMode = 0; // bitrateMode 0 = max bitrate (actual bitrate can be lower than bitrate target), bitrateMode 1 = average bitrate (actual bitrate approximately equal to bitrate target)
10079  p_config->i8pass1Qp = (int8_t)(-1) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
10080  p_config->i8crfDecimal = 0;
10081  p_config->i8hvsBaseMbComplexity = (int8_t)(15)-15; // for old libxcoder backward compatibility, use -15 to represent 0
10082  p_config->i8statisticOutputLevel = 0;
10083  p_config->i8enableipRatio = 0;
10084  p_config->ui16iFrameSizeRatio = 100;
10085  p_config->ui8compressor = 3;
10086  p_config->u8skipFrameInterval = 0;
10087  p_config->ui8crfMaxIframeEnable = 0;
10088  p_config->ui32vbvMinRate = 0;
10089  p_config->ui8disableBframeRDOQ = 0;
10090  p_config->i32forceBframeQpFactor = (int32_t)(-1.0 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
10091  p_config->ui8tuneBframeVisual = 0;
10092  p_config->ui8EnableAcqLimit = 0;
10093  p_config->u8customizeRoiQpLevel = 0;
10094  p_config->ui32cropWidth = 0;
10095  p_config->ui32cropHeight = 0;
10096  p_config->ui32horOffset = 0;
10097  p_config->ui32verOffset = 0;
10098  p_config->ui8motionConstrainedMode = 0;
10099  p_config->ui8stillImageDetectLevel = 0;
10100  p_config->ui8sceneChangeDetectLevel = 0;
10101  p_config->ui8mallocStrategy = 0;
10102  p_config->ui8spatialLayersMinusOne = 0;
10103  p_config->ui8enableTimecode = 0;
10104  p_config->ui8spatialLayersRefBaseLayer = 0;
10105  p_config->ui8vbvBufferReencode = 0;
10106  p_config->ui8totalCuTreeDepth = 0;
10107  p_config->ui8adaptiveCuTree = 0;
10108  p_config->ui8preIntraHandling = (uint8_t)(1)-1; // for old libxcoder backward compatibility, use -1 to represent 0
10109  p_config->ui8baseLayerOnly = 0;
10110  p_config->ui8pastFrameMaxIntraRatio = (uint8_t)(20)-20; // for old libxcoder backward compatibility, use -20 to represent 0
10111  p_config->ui8linkFrameMaxIntraRatio = (uint8_t)(40)-40; // for old libxcoder backward compatibility, use -40 to represent 0
10112  for (i = 0; i < NI_MAX_SPATIAL_LAYERS; i++)
10113  {
10114  p_config->i32spatialLayerBitrate[i] = 0;
10115  }
10116 }
10117 
10118 /*!******************************************************************************
10119 * \brief Perform validation on custom dec parameters (Rev. B)
10120 *
10121 * \param
10122 *
10123 * \return
10124 ******************************************************************************/
10126  ni_session_context_t *p_ctx,
10127  ni_decoder_config_t *p_cfg,
10128  char *p_param_err,
10129  uint32_t max_err_len)
10130 {
10131  ni_retcode_t param_ret = NI_RETCODE_SUCCESS;
10132  ni_retcode_t warning = NI_RETCODE_SUCCESS;
10133  int w = p_src->source_width;
10134  int h = p_src->source_height;
10135  char *p_param_warn = NULL;
10136  int i;
10137 
10138  if (!p_ctx || !p_cfg || !p_param_err)
10139  {
10140  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
10141  __func__);
10142  param_ret = NI_RETCODE_INVALID_PARAM;
10143  LRETURN;
10144  }
10145 
10146  // Zero out the error buffer
10147  p_param_warn = malloc(sizeof(p_ctx->param_err_msg));
10148  if (!p_param_warn)
10149  {
10150  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() p_param_warn malloc failure\n",
10151  NI_ERRNO, __func__);
10152  param_ret = NI_RETCODE_ERROR_MEM_ALOC;
10153  LRETURN;
10154  }
10155 
10156  memset(p_param_err, 0, max_err_len);
10157  memset(p_param_warn, 0, max_err_len);
10158 
10159 
10160  //if (p_cfg->i32bitRate > NI_MAX_BITRATE)
10161  //{
10162  // strncpy(p_param_err, "Invalid i32bitRate: too big", max_err_len);
10163  // param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
10164  // LRETURN;
10165  //}
10166 
10167  if (p_cfg->ui8HWFrame == 0 &&
10168  (p_cfg->asOutputConfig[1].ui8Enabled || p_cfg->asOutputConfig[2].ui8Enabled))
10169  {
10170  strncpy(p_param_err, "Incompatible output format: hw frame must be used if out1 or out2 used", max_err_len);
10171  param_ret = NI_RETCODE_INVALID_PARAM;
10172  LRETURN;
10173  }
10174 
10175  if (p_src->ddr_priority_mode >= 0)
10176  {
10178  "6e") < 0)
10179  {
10180  strncpy(p_param_err, "ddr_priority_mode not supported on device with FW api version < 6.e",
10181  max_err_len);
10183  LRETURN;
10184  }
10185  }
10186 
10188  {
10189  strncpy(p_param_err, "LowDelay is not supported on jpeg/vp9 decoder",
10190  max_err_len);
10192  LRETURN;
10193  }
10194 
10195  for (i = 0; i < NI_MAX_NUM_OF_DECODER_OUTPUTS; i++)
10196  {
10197  //checking cropping param
10199  {
10202  {
10203  strncpy(p_param_err, "Invalid crop offset: extends past 48x48 minimum window", max_err_len);
10204  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10205  LRETURN;
10206  }
10209  {
10210  strncpy(p_param_err, "Invalid crop w or h: must be at least 48x48 minimum window", max_err_len);
10212  LRETURN;
10213  }
10214  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16W > w ||
10215  p_cfg->asOutputConfig[i].sCroppingRectable.ui16H > h)
10216  {
10217  strncpy(p_param_err, "Invalid crop w or h: must be smaller than input", max_err_len);
10218  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10219  LRETURN;
10220  }
10223  {
10224  strncpy(p_param_err, "Invalid crop rect: must fit in input", max_err_len);
10225  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10226  LRETURN;
10227  }
10228  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16X & 1 ||
10229  p_cfg->asOutputConfig[i].sCroppingRectable.ui16Y & 1 ||
10230  p_cfg->asOutputConfig[i].sCroppingRectable.ui16W & 1 ||
10231  p_cfg->asOutputConfig[i].sCroppingRectable.ui16H & 1)
10232  {
10233  strncpy(p_param_err, "Invalid crop value: even values only", max_err_len);
10234  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10235  LRETURN;
10236  }
10237  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
10238  {
10239  strncpy(p_param_warn, "crop param used but output not enabled!", max_err_len);
10240  warning = NI_RETCODE_PARAM_WARN;
10241  }
10242 
10243  }
10244 
10245  //checking scaling param
10246  if (p_cfg->asOutputConfig[i].ui8ScaleEnabled)
10247  {
10248  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Height == 0 ||
10250  {
10251  if (p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt)
10252  {
10253  //may need to revisit if based on cropped input or base input for w/h limit
10254  strncpy(p_param_err, "Invalid scale dimensions: zero", max_err_len);
10256  LRETURN;
10257  }
10258  }
10259  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Height & 1 ||
10261  {
10262  strncpy(p_param_err, "Invalid scale value: even values only", max_err_len);
10263  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10264  LRETURN;
10265  }
10266 
10267  if (p_cfg->asOutputConfig[i].ui8EnablePpuScaleLimit == 1)
10268  {
10269  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Width > w ||
10271  {
10272  strncpy(p_param_err, "Invalid scale value: downscale only", max_err_len);
10273  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10274  LRETURN;
10275  }
10276  }
10277 
10279  {
10280  //reject if scale dimensions exceed crop dimensions
10283  {
10284  strncpy(p_param_err, "Invalid scale dimensions: downscale only after cropping", max_err_len);
10285  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10286  LRETURN;
10287  }
10288  }
10289  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
10290  {
10291  strncpy(p_param_warn, "scale param used but output not enabled!", max_err_len);
10292  warning = NI_RETCODE_PARAM_WARN;
10293  }
10294  }
10295  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
10296  {
10298  {
10299  strncpy(p_param_warn, "force8bit or semiPlanar used but output not enabled!", max_err_len);
10300  warning = NI_RETCODE_PARAM_WARN;
10301  }
10302  }
10303 
10304  //check tiled format compatibility
10306  {
10307  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
10308  {
10309  continue;
10310  }
10311  if (p_cfg->ui8HWFrame == 0)
10312  {
10313  strncpy(p_param_err,
10314  "Invalid pairing: out=HW must be set with tiled format",
10315  max_err_len);
10316  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10317  LRETURN;
10318  }
10319  if (p_ctx->codec_format == NI_CODEC_FORMAT_VP9)
10320  {
10321  strncpy(p_param_err, "Invalid pairing: VP9 not compatible with tiled format",
10322  max_err_len);
10323  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10324  LRETURN;
10325  }
10326  if (p_cfg->asOutputConfig[i].ui8ScaleEnabled)
10327  {
10328  //only no need to check crop compat if scale is set
10329  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Height % 4 ||
10331  {
10332  strncpy(p_param_err,
10333  "Invalid scale height: mult of 4 only, >= 128",
10334  max_err_len);
10335  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10336  LRETURN;
10337  }
10338  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Width % 4 ||
10340  {
10341  //minimum supported dec is 128 but min enc is 144 so round up
10342  strncpy(p_param_err,
10343  "Invalid scale width: mult of 128 only, >= 144",
10344  max_err_len);
10345  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10346  LRETURN;
10347  }
10348  }
10349  else if (p_cfg->asOutputConfig[i].ui8CropMode == NI_DEC_CROP_MODE_MANUAL)
10350  {
10351  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16H % 4 ||
10352  p_cfg->asOutputConfig[i].sCroppingRectable.ui16H < 128)
10353  {
10354  strncpy(p_param_err,
10355  "Invalid crop height: mult of 4 only, >= 128",
10356  max_err_len);
10357  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10358  LRETURN;
10359  }
10360  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16W % 4 ||
10362  {
10363  //minimum supported dec is 128 but min enc is 144 so round up
10364  strncpy(p_param_err,
10365  "Invalid crop width: mult of 128 only, >= 144",
10366  max_err_len);
10367  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10368  LRETURN;
10369  }
10370  }
10371  if (p_cfg->asOutputConfig[i].ui8Force8Bit)
10372  {
10373  strncpy(p_param_err, "Force 8 bit: not supported with tiled format\n",
10374  max_err_len);
10375  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10376  LRETURN;
10377  }
10378  }
10379 
10380  }//end forloop
10381  if (warning == NI_RETCODE_PARAM_WARN && param_ret == NI_RETCODE_SUCCESS)
10382  {
10383  param_ret = NI_RETCODE_PARAM_WARN;
10384  strncpy(p_param_err, p_param_warn, max_err_len);
10385  }
10386 
10387 END:
10388 
10389  free(p_param_warn);
10390  return param_ret;
10391 }
10392 
10393 // Check encoder level parameters
10394 static ni_retcode_t ni_check_level(int level, int codec_id)
10395 {
10396  const int l_levels_264[] = {10, 11, 12, 13, 20, 21, 22, 30, 31, 32,
10397  40, 41, 42, 50, 51, 52, 60, 61, 62, 0};
10398  const int l_levels_265[] = {10, 20, 21, 30, 31, 40, 41,
10399  50, 51, 52, 60, 61, 62, 0};
10400  const int l_levels_av1[] = {20, 21, 30, 31, 40, 41, 50, 51, 0};
10401  const int *l_levels = l_levels_264;
10402 
10403  if (level == 0)
10404  {
10405  return NI_RETCODE_SUCCESS;
10406  }
10407 
10408  if (codec_id == NI_CODEC_FORMAT_H265)
10409  {
10410  l_levels = l_levels_265;
10411  } else if (codec_id == NI_CODEC_FORMAT_AV1)
10412  {
10413  l_levels = l_levels_av1;
10414  }
10415 
10416  while (*l_levels != 0)
10417  {
10418  if (*l_levels == level)
10419  {
10420  return NI_RETCODE_SUCCESS;
10421  }
10422  l_levels++;
10423  }
10424 
10425  return NI_RETCODE_FAILURE;
10426 }
10427 
10428 /*!******************************************************************************
10429  * \brief Perform validation on custom parameters (Rev. B)
10430  *
10431  * \param
10432  *
10433  * \return
10434  ******************************************************************************/
10436  ni_encoder_config_t *p_cfg,
10437  ni_xcoder_params_t *p_src,
10438  char *p_param_err,
10439  uint32_t max_err_len)
10440 {
10441  ni_retcode_t param_ret = NI_RETCODE_SUCCESS;
10442  ni_retcode_t warning = NI_RETCODE_SUCCESS;
10443  char* p_param_warn = malloc(sizeof(p_ctx->param_err_msg));
10444  ni_encoder_cfg_params_t *p_enc;
10445  int i;
10446 
10447  if( (!p_ctx) || (!p_cfg) || (!p_src) || (!p_param_err) )
10448  {
10449  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
10450  __func__);
10451  param_ret = NI_RETCODE_INVALID_PARAM;
10452  LRETURN;
10453  }
10454 
10455  //Zero out the error buffer
10456  p_enc = &p_src->cfg_enc_params;
10457  memset(p_param_err, 0, max_err_len);
10458  memset(p_param_warn, 0, max_err_len);
10459 
10460  if (0 == p_cfg->i32frameRateInfo)
10461  {
10462  strncpy(p_param_err, "Invalid frame_rate of 0 value", max_err_len);
10463  param_ret = NI_RETCODE_PARAM_ERROR_FRATE;
10464  LRETURN;
10465  }
10466 
10467  if (((p_cfg->i32frameRateInfo + p_cfg->i32frameRateDenominator - 1) /
10469  {
10470  strncpy(p_param_err, "Invalid i32frameRateInfo: too big", max_err_len);
10471  param_ret = NI_RETCODE_PARAM_ERROR_FRATE;
10472  LRETURN;
10473  }
10474 
10475  if (p_cfg->i32bitRate <= p_cfg->i32frameRateInfo)
10476  {
10477  strncpy(p_param_err, "Invalid i32bitRate: smaller than or equal to frame rate", max_err_len);
10478  param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
10479  LRETURN;
10480  }
10481 
10482  if (p_cfg->i32bitRate > NI_MAX_BITRATE)
10483  {
10484  strncpy(p_param_err, "Invalid i32bitRate: too big", max_err_len);
10485  param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
10486  LRETURN;
10487  }
10488 
10489  if (p_cfg->i32bitRate < NI_MIN_BITRATE )
10490  {
10491  strncpy(p_param_err, "Invalid i32bitRate: too low", max_err_len);
10492  param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
10493  LRETURN;
10494  }
10495 
10497  {
10498  strncpy(p_param_err, "Invalid Picture Width: too small", max_err_len);
10500  LRETURN;
10501  }
10502 
10504  {
10505  strncpy(p_param_err, "Invalid Picture Width: too big", max_err_len);
10507  LRETURN;
10508  }
10509 
10511  {
10512  strncpy(p_param_err, "Invalid Picture Height: too small", max_err_len);
10514  LRETURN;
10515  }
10516 
10518  {
10519  strncpy(p_param_err, "Invalid Picture Height: too big", max_err_len);
10521  LRETURN;
10522  }
10523 
10524  if (p_cfg->ui32cropWidth || p_cfg->ui32cropHeight)
10525  {
10526  // check cropping width & height are both non-zero
10527  if (!p_cfg->ui32cropWidth || !p_cfg->ui32cropHeight)
10528  {
10529  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height (%u x %u): both need to be specified",
10530  p_cfg->ui32cropWidth, p_cfg->ui32cropHeight);
10531  param_ret = NI_RETCODE_INVALID_PARAM;
10532  LRETURN;
10533  }
10534  // check cropping width & height are even
10535  if ((p_cfg->ui32cropWidth % 2) || (p_cfg->ui32cropHeight % 2))
10536  {
10537  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height (%u x %uu): must be even",
10538  p_cfg->ui32cropWidth, p_cfg->ui32cropHeight);
10539  param_ret = NI_RETCODE_INVALID_PARAM;
10540  LRETURN;
10541  }
10542  // check cropping width & height meet resoultion constraint (including AV1 max resoultion)
10543  if (p_cfg->ui32cropWidth < NI_MIN_WIDTH)
10544  {
10545  snprintf(p_param_err, max_err_len, "Invalid Crop Width: less than %d",
10546  NI_MIN_WIDTH);
10548  LRETURN;
10549  }
10550  if (p_cfg->ui32cropHeight < NI_MIN_HEIGHT)
10551  {
10552  snprintf(p_param_err, max_err_len, "Invalid Crop Height: less than %d",
10553  NI_MIN_HEIGHT);
10555  LRETURN;
10556  }
10557  if (p_cfg->ui32cropWidth > NI_PARAM_MAX_WIDTH)
10558  {
10559  snprintf(p_param_err, max_err_len, "Invalid Crop Width: exceeds %d",
10562  LRETURN;
10563  }
10564  if (p_cfg->ui32cropHeight > NI_PARAM_MAX_HEIGHT)
10565  {
10566  snprintf(p_param_err, max_err_len, "Invalid Crop Height: exceeds %d",
10569  LRETURN;
10570  }
10571  if (p_cfg->ui32cropWidth * p_cfg->ui32cropHeight > NI_MAX_RESOLUTION_AREA)
10572  {
10573  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height: exceeds %d",
10576  LRETURN;
10577  }
10578 
10579  if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
10580  {
10582  {
10583  snprintf(p_param_err, max_err_len, "Invalid Crop Width: exceeds %d",
10586  LRETURN;
10587  }
10589  {
10590  snprintf(p_param_err, max_err_len, "Invalid Crop Height: exceeds %d",
10593  LRETURN;
10594  }
10595  if (p_cfg->ui32cropWidth * p_cfg->ui32cropHeight > NI_PARAM_AV1_MAX_AREA)
10596  {
10597  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height: exceeds %d",
10600  LRETURN;
10601  }
10602  }
10603 
10604  // check cropping horOffset & verOffset are even
10605  if ((p_cfg->ui32horOffset % 2) || (p_cfg->ui32verOffset % 2))
10606  {
10607  snprintf(p_param_err, max_err_len, "Invalid Crop horOffset %u and/or verOffset %u: must be even",
10608  p_cfg->ui32horOffset, p_cfg->ui32verOffset);
10609  param_ret = NI_RETCODE_INVALID_PARAM;
10610  LRETURN;
10611  }
10612 
10613  if (p_src->source_width < (p_cfg->ui32horOffset + p_cfg->ui32cropWidth))
10614  {
10615  snprintf(p_param_err, max_err_len, "Invalid Crop Width: offset %u + crop width %u too big > %d",
10616  p_cfg->ui32horOffset, p_cfg->ui32cropWidth, p_src->source_width);
10618  LRETURN;
10619  }
10620  if (p_src->source_height < (p_cfg->ui32verOffset + p_cfg->ui32cropHeight))
10621  {
10622  snprintf(p_param_err, max_err_len, "Invalid Crop Height: offset %u + crop height %u too big > %d",
10623  p_cfg->ui32verOffset, p_cfg->ui32cropHeight, p_src->source_height);
10625  LRETURN;
10626  }
10627  }
10628  else // check source width / height meet AV1 max resoultion constraint if cropping not specified
10629  {
10630  if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
10631  {
10632  if (p_src->source_width > NI_PARAM_AV1_MAX_WIDTH)
10633  {
10634  snprintf(p_param_err, max_err_len, "Invalid Picture Width: exceeds %d",
10637  LRETURN;
10638  }
10640  {
10641  snprintf(p_param_err, max_err_len, "Invalid Picture Height: exceeds %d",
10644  LRETURN;
10645  }
10646  if (p_src->source_width * p_src->source_height > NI_PARAM_AV1_MAX_AREA)
10647  {
10648  snprintf(p_param_err, max_err_len, "Invalid Picture Width x Height: exceeds %d",
10651  LRETURN;
10652  }
10653  }
10654  }
10655 
10656  if (p_cfg->ui8planarFormat >= NI_PIXEL_PLANAR_MAX)
10657  {
10658  strncpy(p_param_err, "Invalid input planar format: out of range", max_err_len);
10659  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10660  LRETURN;
10661  }
10663  {
10665  "68") < 0)
10666  {
10667  strncpy(p_param_err, "Invalid input planar format for device with FW api version < 6.8",
10668  max_err_len);
10670  LRETURN;
10671  }
10672  if (p_ctx->auto_dl_handle)
10673  {
10674  strncpy(p_param_err, "Invalid Encoder Selected: Tiled format must be on same device",
10675  max_err_len);
10676  param_ret = NI_RETCODE_ERROR_INVALID_HANDLE;
10677  LRETURN;
10678  }
10679  if (p_src->source_height % 4 != 0)
10680  {
10681  strncpy(p_param_err,
10682  "Invalid Picture Height: tiled format only supports "
10683  "multiples of 4",
10684  max_err_len);
10686  LRETURN;
10687  }
10688  if (p_src->source_width % 4 != 0)
10689  {
10690  strncpy(p_param_err,
10691  "Invalid Picture Width: tiled format only supports "
10692  "multiples of 4",
10693  max_err_len);
10695  LRETURN;
10696  }
10697  }
10698 
10699  if (p_cfg->ui8PixelFormat == NI_PIX_FMT_RGBA ||
10700  p_cfg->ui8PixelFormat == NI_PIX_FMT_BGRA ||
10701  p_cfg->ui8PixelFormat == NI_PIX_FMT_ABGR ||
10702  p_cfg->ui8PixelFormat == NI_PIX_FMT_ARGB)
10703  {
10704  if (p_src->zerocopy_mode == 0)
10705  {
10706  strncpy(p_param_err, "zeroCopyMode must not be disabled for RGBA / BGRA / ABGR / ARGB pixel formats", max_err_len);
10707  param_ret = NI_RETCODE_INVALID_PARAM;
10708  LRETURN;
10709  }
10710  if (p_cfg->ui8PixelFormat == NI_PIX_FMT_RGBA ||
10711  p_cfg->ui8PixelFormat == NI_PIX_FMT_BGRA)
10712  {
10714  "6Y") < 0)
10715  {
10716  strncpy(p_param_err, "RGBA / BGRA pixel formats not supported on device with FW api version < 6.Y",
10717  max_err_len);
10719  LRETURN;
10720  }
10721  }
10722  }
10723  else if (p_cfg->ui8PixelFormat != NI_PIX_FMT_YUV420P &&
10725  p_cfg->ui8PixelFormat != NI_PIX_FMT_NV12 &&
10726  p_cfg->ui8PixelFormat != NI_PIX_FMT_P010LE &&
10729  {
10730  snprintf(p_param_err, max_err_len, "Unsupported pixel format %d in encoder", p_cfg->ui8PixelFormat);
10731  param_ret = NI_RETCODE_INVALID_PARAM;
10732  LRETURN;
10733  }
10734 
10735  if (p_src->ddr_priority_mode >= 0)
10736  {
10738  "6e") < 0)
10739  {
10740  strncpy(p_param_err, "ddr_priority_mode not supported on device with FW api version < 6.e",
10741  max_err_len);
10743  LRETURN;
10744  }
10745  }
10746 
10747  if (NI_RETCODE_SUCCESS !=
10748  ni_check_level(p_src->cfg_enc_params.level_idc, p_ctx->codec_format))
10749  {
10750  strncpy(p_param_err, "Invalid Encoder Level: out of range", max_err_len);
10751  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10752  LRETURN;
10753  }
10754 
10755  if ((p_src->cfg_enc_params.intra_mb_refresh_mode < 0 ||
10756  p_src->cfg_enc_params.intra_mb_refresh_mode > 4) ||
10757  (NI_CODEC_FORMAT_H264 == p_ctx->codec_format &&
10759  {
10760  strncpy(p_param_err, "Invalid intra_mb_refresh_mode: out of range",
10761  max_err_len);
10762  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10763  LRETURN;
10764  }
10765 
10766  if (!QUADRA)
10767  {
10769  {
10771  {
10772  strncpy(p_param_err, "Invalid custom GOP paramaters: custom_gop_size too small", max_err_len);
10774  LRETURN;
10775  }
10778  {
10779  strncpy(p_param_err, "Invalid custom GOP paramaters: custom_gop_size too big", max_err_len);
10781  LRETURN;
10782  }
10783  }
10784  }
10785  else // QUADRA
10786  {
10788  {
10790  {
10791  strncpy(p_param_err,
10792  "Invalid custom GOP paramaters: custom gop size must > 0",
10793  max_err_len);
10795  LRETURN;
10796  }
10797  }
10799  {
10802  {
10803  strncpy(p_param_err,
10804  "Invalid custom GOP paramaters: selected gopPresetIdx is "
10805  "not compatible with custom gop",
10806  max_err_len);
10808  LRETURN;
10809  }
10811  {
10812  strncpy(p_param_err, "Invalid custom GOP paramaters: custom_gop_size too small", max_err_len);
10814  LRETURN;
10815  }
10818  {
10819  strncpy(p_param_err, "Invalid custom GOP paramaters: custom_gop_size too big", max_err_len);
10821  LRETURN;
10822  }
10823 
10824  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
10825  {
10828  {
10829  strncpy(p_param_err, "Invalid custom gop parameters: poc_offset larger"
10830  " than GOP size", max_err_len);
10832  LRETURN;
10833  }
10834  }
10835  }
10836 
10837  if(p_cfg->ui8useLowDelayPocType != 0)
10838  {
10839  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
10840  {
10841  p_cfg->ui8useLowDelayPocType = 0;
10842  strncpy(p_param_warn, "useLowDelayPocType is only supported for H.264. Change useLowDelayPocType to 0", max_err_len);
10843  warning = NI_RETCODE_PARAM_WARN;
10844  }
10845  }
10846  if(p_cfg->niParamT408.entropy_coding_mode != 1)
10847  {
10848  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
10849  {
10850  strncpy(p_param_err,
10851  "entropyCodingMode is only supported for H.264.",
10852  max_err_len);
10854  LRETURN;
10855  }
10856  }
10857  if (p_cfg->ui8av1ErrResilientMode)
10858  {
10859  if (NI_CODEC_FORMAT_AV1 != p_ctx->codec_format)
10860  {
10861  strncpy(p_param_err,
10862  "av1ErrorResilientMode is only supported for AV1.",
10863  max_err_len);
10865  LRETURN;
10866  }
10867  }
10868  if (0 != p_src->enable_ai_enhance)
10869  {
10870  if (NI_CODEC_FORMAT_JPEG == p_ctx->codec_format)
10871  {
10872  snprintf(p_param_err, max_err_len, "%s is not supported for JPEG.",
10873  ((p_src->enable_ai_enhance == 2) ? "enableHVSPlus" : "enableAIEnhance"));
10875  LRETURN;
10876  }
10877  }
10878  if (-1 != p_cfg->ui32QLevel)
10879  {
10880  if (NI_CODEC_FORMAT_JPEG != p_ctx->codec_format)
10881  {
10882  strncpy(p_param_err, "qLevel is only supported for JPEG.",
10883  max_err_len);
10885  LRETURN;
10886  }
10887  }
10888 
10889  if(p_cfg->ui8enableSSIM != 0)
10890  {
10892  "62") < 0 || NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
10893  {
10894  p_cfg->ui8enableSSIM = 0;
10895  strncpy(p_param_warn, "enableSSIM only supported on device with FW api version < 6.2 "
10896  "and the encoder is not av1_ni_quadra_enc. Reported ssim will be 0.", max_err_len);
10897  warning = NI_RETCODE_PARAM_WARN;
10898  }
10899  }
10900 
10901  if (p_cfg->niParamT408.slice_mode || p_cfg->niParamT408.slice_arg)
10902  {
10904  {
10905  strncpy(p_param_err, "sliceMode/sliceArg is only supported for H.264 or H.265.",
10906  max_err_len);
10908  LRETURN;
10909  }
10910 
10911  if (p_cfg->niParamT408.slice_mode)
10912  {
10913  int ctu_mb_size = (NI_CODEC_FORMAT_H264 == p_ctx->codec_format) ? 16 : 64;
10914  int max_num_ctu_mb_row = (p_src->source_height + ctu_mb_size - 1) / ctu_mb_size;
10915  if (p_cfg->niParamT408.slice_arg < 1 || p_cfg->niParamT408.slice_arg > max_num_ctu_mb_row ||
10917  {
10918  snprintf(p_param_err, max_err_len, "Invalid number of rows per slice: should be between 1 and %d",
10919  max_num_ctu_mb_row < NI_MAX_SLICE_SIZE ? max_num_ctu_mb_row : NI_MAX_SLICE_SIZE);
10920  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10921  LRETURN;
10922  }
10923  }
10924  }
10925  if (p_src->use_low_delay_poc_type)
10926  {
10927  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
10928  {
10929  strncpy(p_param_err, "useLowDelayPocType is only supported for H.264.",
10930  max_err_len);
10932  LRETURN;
10933  }
10934  }
10935 
10936  if(p_cfg->ui8enableCompensateQp != 0)
10937  {
10938  if (ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6ru") < 0 ||
10939  NI_CODEC_FORMAT_AV1 == p_ctx->codec_format || p_cfg->ui16maxFrameSize == 0)
10940  {
10941  p_cfg->ui8enableCompensateQp = 0;
10942  strncpy(p_param_warn, "ui8enableCompensateQp only supported when device with FW api version < 6rt "
10943  "and the encoder is not av1_ni_quadra_enc and ui16maxFrameSize > 0. Reported enableCompensateQp will be 0.", max_err_len);
10944  warning = NI_RETCODE_PARAM_WARN;
10945  }
10946  }
10947 
10948  if (p_cfg->ui8enableTimecode != 0)
10949  {
10950  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
10951  {
10952  p_cfg->ui8enableTimecode = 0;
10953  if (NI_CODEC_FORMAT_H265 == p_ctx->codec_format)
10954  {
10955  strncpy(p_param_warn, "enableTimecode does not need to be set for H.265. Use ni_enc_insert_timecode API"
10956  "from libxcoder directly to insert time code SEI", max_err_len);
10957  }
10958  else
10959  {
10960  strncpy(p_param_warn, "enableTimecode not supported for the codec used. Forcing value to 0.", max_err_len);
10961  }
10962  warning = NI_RETCODE_PARAM_WARN;
10963  }
10964  }
10965  }
10966 
10967  if (QUADRA)
10968  {
10969  if (p_cfg->ui8LookAheadDepth != 0 || p_cfg->i8crf >= 0)
10970  {
10971  if ((1 == p_cfg->niParamT408.gop_preset_index && p_cfg->ui8LookAheadDepth != 0) ||
10972  3 == p_cfg->niParamT408.gop_preset_index ||
10973  7 == p_cfg->niParamT408.gop_preset_index ||
10974  10 == p_cfg->niParamT408.gop_preset_index ||
10975  15 == p_cfg->niParamT408.gop_preset_index)
10976  {
10977  strncpy(p_param_err,
10978  "this gopPreset is not supported for lookahead and/or CRF",
10979  max_err_len);
10981  LRETURN;
10982  }
10983 
10985  {
10986  bool bIsgopLowdelay = true;
10987  // lookahead does not support gopLowDelay (all B-frames, encode in order) gop patterns
10988  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size;
10989  i++)
10990  {
10991  // check if all frames are B-frames
10992  if (2 != p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
10993  {
10994  bIsgopLowdelay = false;
10995  break;
10996  }
10997  // check if all frames are encoded in display order
10998  if (p_cfg->niParamT408.custom_gop_params.pic_param[i].poc_offset != (i+1))
10999  {
11000  bIsgopLowdelay = false;
11001  break;
11002  }
11003  }
11004  if (bIsgopLowdelay)
11005  {
11006  strncpy(p_param_err, "B-frames low delay custom gop is not supported for "
11007  "lookahead and/or CRF", max_err_len);
11008  param_ret = NI_RETCODE_INVALID_PARAM;
11009  LRETURN;
11010  }
11011  }
11012 
11013  if (p_enc->bitrateMode != -1)
11014  {
11015  strncpy(p_param_err,
11016  "bitrateMode is invalid when lookahead is enabled (or in CRF mode)",
11017  max_err_len);
11019  LRETURN;
11020  }
11021  }
11022 
11023  if (p_cfg->ui8totalCuTreeDepth != 0)
11024  {
11025  if (p_cfg->ui8LookAheadDepth == 0 || p_cfg->ui8LookAheadDepth == 1)
11026  {
11027  strncpy(p_param_err,
11028  "totalCuTreeDepth is invalid without lookahead",
11029  max_err_len);
11031  LRETURN;
11032  }
11033 
11034  if (p_cfg->ui8totalCuTreeDepth <= p_cfg->ui8LookAheadDepth)
11035  {
11036  strncpy(p_param_warn, "totalCuTreeDepth does not take effects when its value <= lookahead depth", max_err_len);
11037  warning = NI_RETCODE_PARAM_WARN;
11038  p_cfg->ui8totalCuTreeDepth = 0;
11039  }
11040  else
11041  {
11042  if (p_cfg->ui8multicoreJointMode != 0)
11043  {
11044  strncpy(p_param_err,
11045  "totalCuTreeDepth is not supported in multicoreJointMode",
11046  max_err_len);
11048  LRETURN;
11049  }
11050  }
11051  }
11052 
11054  {
11055  // when gop_preset_index = -1 (default), gop pattern is decided by gopSize and gopLowdelay
11056  //p_cfg->ui8gopSize = 0;
11057  //p_cfg->ui8gopLowdelay = 0;
11058  }
11059  if (1 == p_cfg->niParamT408.gop_preset_index)
11060  {
11061  p_cfg->niParamT408.intra_period = 1;
11062  p_cfg->niParamT408.avcIdrPeriod = 1;
11063  p_cfg->ui8gopSize = 1;
11064  p_cfg->ui16gdrDuration = 0;
11065  }
11066  if (2 == p_cfg->niParamT408.gop_preset_index)
11067  {
11068  strncpy(p_param_err,
11069  "gopPresetIdx 2 is obsolete, suggest to use gopPresetIdx 9 "
11070  "instead",
11071  max_err_len);
11073  LRETURN;
11074  }
11075  if (3 == p_cfg->niParamT408.gop_preset_index)
11076  {
11077  p_cfg->ui8gopSize = 1;
11078  p_cfg->ui8gopLowdelay = 1;
11079  }
11080  if (4 == p_cfg->niParamT408.gop_preset_index)
11081  {
11082  p_cfg->ui8gopSize = 2;
11083  p_cfg->ui8gopLowdelay = 0;
11084  }
11085  if (5 == p_cfg->niParamT408.gop_preset_index)
11086  {
11087  p_cfg->ui8gopSize = 4;
11088  p_cfg->ui8gopLowdelay = 0;
11089  }
11090  if (6 == p_cfg->niParamT408.gop_preset_index)
11091  {
11092  strncpy(p_param_err,
11093  "gopPresetIdx 6 is obsolete, suggest to use gopPresetIdx 7 "
11094  "instead",
11095  max_err_len);
11097  LRETURN;
11098  }
11099  if (7 == p_cfg->niParamT408.gop_preset_index)
11100  {
11101  p_cfg->ui8gopSize = 4;
11102  p_cfg->ui8gopLowdelay = 1;
11103  }
11104  if (8 == p_cfg->niParamT408.gop_preset_index)
11105  {
11106  p_cfg->ui8gopSize = 8;
11107  p_cfg->ui8gopLowdelay = 0;
11108  }
11109  if (9 == p_cfg->niParamT408.gop_preset_index)
11110  {
11111  p_cfg->ui8gopSize = 1;
11112  p_cfg->ui8gopLowdelay = 0;
11113  }
11114  if (10 == p_cfg->niParamT408.gop_preset_index)
11115  {
11116  p_cfg->ui8gopSize = 4;
11117  p_cfg->ui8gopLowdelay = 0;
11118  }
11119  if (15 == p_cfg->niParamT408.gop_preset_index)
11120  {
11121  p_cfg->ui8gopSize = 16;
11122  p_cfg->ui8gopLowdelay = 0;
11123  }
11124 
11125  if (p_cfg->ui8LookAheadDepth != 0
11126  || p_cfg->ui8bitrateMode == 1
11127  || p_cfg->ui32vbvMaxRate
11128  || p_cfg->ui32vbvMinRate
11129  || p_cfg->i8enableipRatio)
11130  {
11131  ni_log2(p_ctx, NI_LOG_DEBUG, "Force newRcEnable to 0 in 2-pass encode or in 1-pass average bitrate mode, with ipRatio, VbvMaxRate, or VbvMinRate\n");
11132  p_cfg->ui8NewRCEnable = 0;
11133  }
11134  else if (p_cfg->ui8NewRCEnable == 255)
11135  {
11136  if ((p_cfg->ui8gopSize > 1 || p_cfg->ui8gopSize == 0) && p_cfg->ui8gopLowdelay == 0)
11137  {
11138  ni_log2(p_ctx, NI_LOG_DEBUG, "Set newRcEnable to 0 in non low delay gop preset\n");
11139  p_cfg->ui8NewRCEnable = 0;
11140  }
11141  else if (p_cfg->niParamT408.intra_period == 1 || p_cfg->niParamT408.avcIdrPeriod == 1)
11142  {
11143  ni_log2(p_ctx, NI_LOG_DEBUG, "Set newRcEnable to 0 in intra only mode\n");
11144  p_cfg->ui8NewRCEnable = 0;
11145  }
11146  }
11147 
11148  if ((p_cfg->ui8stillImageDetectLevel != 0 || p_cfg->ui8sceneChangeDetectLevel != 0) &&
11149  (p_cfg->niParamT408.gop_preset_index != 9 || ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rm") < 0))
11150  {
11151  strncpy(p_param_warn, "StillImageDetect or SceneChangeDetect only support gopPresetIdx=9 with FW > 6rm\n", max_err_len);
11152  warning = NI_RETCODE_PARAM_WARN;
11153  p_cfg->ui8stillImageDetectLevel = 0;
11154  p_cfg->ui8sceneChangeDetectLevel = 0;
11155  }
11156 
11157  if (p_cfg->ui8spatialLayersMinusOne > 0)
11158  {
11160  {
11161  strncpy(p_param_err,
11162  "currently do not support gop preset in multi spatial layers encode",
11163  max_err_len);
11165  LRETURN;
11166  }
11167  p_cfg->ui8gopSize = p_cfg->ui8spatialLayersMinusOne + 1;
11168  p_cfg->ui8gopLowdelay = 0;
11169  }
11170 
11171  if (p_cfg->ui8spatialLayersRefBaseLayer != 0)
11172  {
11173  if (p_cfg->ui8spatialLayersMinusOne == 0)
11174  {
11175  strncpy(p_param_err,
11176  "higher spatial layers referencing base layer is only supported in multi spatial layers encode",
11177  max_err_len);
11179  LRETURN;
11180  }
11181  }
11182 
11183  for (i = 0; i < NI_MAX_SPATIAL_LAYERS; i++)
11184  {
11185  if (p_cfg->i32spatialLayerBitrate[i] == 0)
11186  {
11187  break;
11188  }
11189  }
11190  if (i > 0)
11191  {
11192  if (p_cfg->ui8spatialLayersMinusOne == 0)
11193  {
11194  strncpy(p_param_err, "spatialLayerBitrate should only be used in multi spatial layers encode", max_err_len);
11195  param_ret = NI_RETCODE_INVALID_PARAM;
11196  LRETURN;
11197  }
11198  else if (p_cfg->ui8spatialLayersMinusOne + 1 != i)
11199  {
11200  strncpy(p_param_err, "number of values specified in spatialLayerBitrate must match the total number of spatial layers", max_err_len);
11201  param_ret = NI_RETCODE_INVALID_PARAM;
11202  LRETURN;
11203  }
11204  if (p_cfg->ui8multicoreJointMode != 0)
11205  {
11206  strncpy(p_param_err, "spatialLayerBitrate is not supported in multicoreJointMode", max_err_len);
11208  LRETURN;
11209  }
11210  }
11211 
11212  if (p_cfg->ui8crfMaxIframeEnable > 0)
11213  {
11214  if (p_cfg->i8crf < 0 || p_cfg->i32vbvBufferSize == 0 || p_cfg->ui8LookAheadDepth == 0)
11215  {
11216  strncpy(p_param_err,
11217  "crfMaxIframeEnable is only supported in Capped CRF mode with lookahead enabled",
11218  max_err_len);
11219  param_ret = NI_RETCODE_INVALID_PARAM;
11220  LRETURN;
11221  }
11222  }
11223  }
11224 
11225  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
11226  {
11227  if (!QUADRA)
11228  {
11229  if (10 == p_ctx->src_bit_depth)
11230  {
11231  if (p_cfg->niParamT408.profile != 5)
11232  {
11233  strncpy(p_param_err, "Invalid profile: must be 5 (high10)",
11234  max_err_len);
11235  param_ret = NI_RETCODE_INVALID_PARAM;
11236  LRETURN;
11237  }
11238  }
11239  else
11240  {
11241  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 5)
11242  {
11243  strncpy(p_param_err, "Invalid profile: must be 1 (baseline), 2 (main),"
11244  " 3 (extended), 4 (high), or 5 (high10)", max_err_len);
11245  param_ret = NI_RETCODE_INVALID_PARAM;
11246  LRETURN;
11247  }
11248  }
11249  if (1 == p_cfg->niParamT408.profile &&
11250  ! (0 == p_cfg->niParamT408.gop_preset_index ||
11251  1 == p_cfg->niParamT408.gop_preset_index ||
11252  2 == p_cfg->niParamT408.gop_preset_index ||
11253  6 == p_cfg->niParamT408.gop_preset_index))
11254  {
11255  strncpy(p_param_err, "Invalid gopPresetIdx for H.264 baseline profile:"
11256  " must be 1, 2, 6 or 0 (custom with no B frames)", max_err_len);
11257  param_ret = NI_RETCODE_INVALID_PARAM;
11258  LRETURN;
11259  }
11260  if (1 == p_cfg->niParamT408.profile &&
11262  {
11263  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size;
11264  i++)
11265  {
11266  if (2 == p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
11267  {
11268  strncpy(p_param_err, "H.264 baseline profile: custom GOP can not "
11269  "have B frames", max_err_len);
11270  param_ret = NI_RETCODE_INVALID_PARAM;
11271  LRETURN;
11272  }
11273  }
11274  }
11275  }
11276  else // QUADRA
11277  {
11278  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 5 || p_cfg->niParamT408.profile == 3)
11279  {
11280  strncpy(p_param_err, "Invalid profile: must be 1 (baseline), 2 (main),"
11281  " 4 (high), or 5 (high10)", max_err_len);
11282  param_ret = NI_RETCODE_INVALID_PARAM;
11283  LRETURN;
11284  }
11285  if (10 == p_ctx->src_bit_depth && p_cfg->niParamT408.profile != 5)
11286  {
11287  strncpy(p_param_warn, "AVC Baseline/Main/High Profile do not support 10-bit, auto convert to 8-bit", max_err_len);
11288  warning = NI_RETCODE_PARAM_WARN;
11289  }
11290  if (1 == p_cfg->niParamT408.profile)
11291  {
11293  {
11294  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
11295  {
11296  if (2 == p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
11297  {
11298  strncpy(p_param_err, "H.264 baseline profile: custom GOP can not "
11299  "have B frames", max_err_len);
11300  param_ret = NI_RETCODE_INVALID_PARAM;
11301  LRETURN;
11302  }
11303  }
11304  } else if (((p_cfg->ui8gopSize != 1 || p_cfg->ui8gopLowdelay)) &&
11305  (p_cfg->niParamT408.intra_period != 1) &&
11307  {
11309  p_cfg->ui8gopSize != 0 ||
11310  p_cfg
11311  ->ui8gopLowdelay) // if gopSize is 0 (default / adapative gop), autoset to 1
11312  {
11313  strncpy(p_param_err,
11314  "Must use gopPresetIdx 1,9,10 (no "
11315  "B frames) for profile 1",
11316  max_err_len);
11317  param_ret = NI_RETCODE_INVALID_PARAM;
11318  LRETURN;
11319  }
11320  p_cfg->ui8gopSize = 1; //autoset to 1
11321  }
11322  }
11323  if (p_cfg->niParamT408.tier)
11324  {
11325  strncpy(p_param_err, "Tier is not supported for H.264", max_err_len);
11326  param_ret = NI_RETCODE_INVALID_PARAM;
11327  LRETURN;
11328  }
11329  if (p_cfg->ui8spatialLayersMinusOne > 0)
11330  {
11331  strncpy(p_param_err,
11332  "spatialLayers is not supported for h.264 encode",
11333  max_err_len);
11335  LRETURN;
11336  }
11337  }
11338  }
11339  else if (NI_CODEC_FORMAT_H265 == p_ctx->codec_format)
11340  {
11341  if (!QUADRA)
11342  {
11343  if (10 == p_ctx->src_bit_depth)
11344  {
11345  if (p_cfg->niParamT408.profile != 2)
11346  {
11347  strncpy(p_param_err, "Invalid profile: must be 2 (main10)",
11348  max_err_len);
11349  param_ret = NI_RETCODE_INVALID_PARAM;
11350  LRETURN;
11351  }
11352  }
11353  else
11354  {
11355  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 2)
11356  {
11357  strncpy(p_param_err, "Invalid profile: must be 1 (main) or 2 (main10)",
11358  max_err_len);
11359  param_ret = NI_RETCODE_INVALID_PARAM;
11360  LRETURN;
11361  }
11362  }
11363  }
11364  else // QUADRA
11365  {
11366  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 2)
11367  {
11368  strncpy(p_param_err, "Invalid profile: must be 1 (main) or 2 (main10)",
11369  max_err_len);
11370  param_ret = NI_RETCODE_INVALID_PARAM;
11371  LRETURN;
11372  }
11373  if (10 == p_ctx->src_bit_depth && p_cfg->niParamT408.profile != 2)
11374  {
11375  strncpy(p_param_warn, "HEVC Main Profile does not support 10-bit, auto convert to 8-bit", max_err_len);
11376  warning = NI_RETCODE_PARAM_WARN;
11377  }
11378  if (p_cfg->ui8spatialLayersMinusOne > 0)
11379  {
11380  strncpy(p_param_err,
11381  "spatialLayers is not supported for h.265 encode",
11382  max_err_len);
11384  LRETURN;
11385  }
11386  }
11387  } else if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
11388  {
11390  {
11391  strncpy(p_param_err, "TuneBframeVisual MEDIUM is not supported for AV1", max_err_len);
11392  param_ret = NI_RETCODE_INVALID_PARAM;
11393  LRETURN;
11394  }
11395  if (p_cfg->niParamT408.profile != 1)
11396  {
11397  strncpy(p_param_err, "Invalid profile: must be 1 (main)",
11398  max_err_len);
11399  param_ret = NI_RETCODE_INVALID_PARAM;
11400  LRETURN;
11401  }
11402  if (p_cfg->niParamT408.level) // 0 means auto level
11403  {
11404  if (p_cfg->niParamT408.level < 20)
11405  {
11406  p_cfg->niParamT408.level = 20;
11407  strncpy(p_param_warn,
11408  "AV1 level < 2.0 is not supported, change to level 2.0",
11409  max_err_len);
11410  warning = NI_RETCODE_PARAM_WARN;
11411  } else if (p_cfg->niParamT408.level > 51)
11412  {
11413  p_cfg->niParamT408.level = 51;
11414  strncpy(p_param_warn,
11415  "AV1 level > 5.1 is not supported, change to level 5.1",
11416  max_err_len);
11417  warning = NI_RETCODE_PARAM_WARN;
11418  }
11419  }
11420 
11421  if (p_cfg->niParamT408.conf_win_top != 0)
11422  {
11424  0;
11425  strncpy(p_param_warn, "confWinTop is not supported in AV1",
11426  max_err_len);
11427  warning = NI_RETCODE_PARAM_WARN;
11428  }
11429  if (p_cfg->niParamT408.conf_win_bottom != 0)
11430  {
11431  p_cfg->niParamT408.conf_win_bottom =
11432  p_src->cfg_enc_params.conf_win_bottom = 0;
11433  strncpy(p_param_warn, "confWinBottom is not supported in AV1",
11434  max_err_len);
11435  warning = NI_RETCODE_PARAM_WARN;
11436  }
11437  if (p_cfg->niParamT408.conf_win_left != 0)
11438  {
11439  p_cfg->niParamT408.conf_win_left =
11440  p_src->cfg_enc_params.conf_win_left = 0;
11441  strncpy(p_param_warn, "confWinLeft is not supported in AV1",
11442  max_err_len);
11443  warning = NI_RETCODE_PARAM_WARN;
11444  }
11445  if (p_cfg->niParamT408.conf_win_right != 0)
11446  {
11447  p_cfg->niParamT408.conf_win_right =
11448  p_src->cfg_enc_params.conf_win_right = 0;
11449  strncpy(p_param_warn, "confWinRight is not supported in AV1",
11450  max_err_len);
11451  warning = NI_RETCODE_PARAM_WARN;
11452  }
11453  if (p_cfg->ui8hdr10_enable)
11454  {
11455  strncpy(p_param_err,
11456  "masterDisplay not supported for AV1",
11457  max_err_len);
11458  warning = NI_RETCODE_PARAM_WARN;
11459  }
11460  if (p_cfg->ui8hrdEnable)
11461  {
11462  strncpy(p_param_err, "hrdEnable is not supported on av1 encoder",
11463  max_err_len);
11465  LRETURN;
11466  }
11467  if (p_cfg->ui8EnableAUD)
11468  {
11469  strncpy(p_param_err, "enableAUD is not supported on av1 encoder",
11470  max_err_len);
11472  LRETURN;
11473  }
11474  if (p_cfg->ui8HDR10CLLEnable)
11475  {
11476  strncpy(p_param_err, "maxCLL is not supported on av1 encoder",
11477  max_err_len);
11479  LRETURN;
11480  }
11481  if (p_cfg->ui8repeatHeaders)
11482  {
11483  strncpy(p_param_err, "repeatHeaders is not supported on av1 encoder",
11484  max_err_len);
11485  warning = NI_RETCODE_PARAM_WARN;
11486  }
11487  if (p_cfg->ui8enableSSIM)
11488  {
11489  strncpy(p_param_err, "enableSSIM is not supported on av1 encoder",
11490  max_err_len);
11492  LRETURN;
11493  }
11494  if (p_cfg->ui8EnableRdoQuant)
11495  {
11496  strncpy(p_param_err, "EnableRdoQuant is not supported on av1 encoder",
11497  max_err_len);
11499  LRETURN;
11500  }
11501  if (p_cfg->ui8fillerEnable)
11502  {
11503  strncpy(p_param_err, "fillerEnable is not supported on av1 encoder",
11504  max_err_len);
11506  LRETURN;
11507  }
11508  if (p_cfg->i8ppsInitQp)
11509  {
11510  strncpy(p_param_err, "ppsInitQp is not supported for av1 encoder",
11511  max_err_len);
11513  LRETURN;
11514  }
11515  if (p_cfg->ui8vbvBufferReencode && p_cfg->ui8multicoreJointMode)
11516  {
11517  strncpy(p_param_err, "vbvBufferReencode is not supported for av1 multicoreJointMode",
11518  max_err_len);
11520  LRETURN;
11521  }
11522  } else if (NI_CODEC_FORMAT_JPEG == p_ctx->codec_format)
11523  {
11524  if (p_cfg->ui32cropWidth || p_cfg->ui32cropHeight || p_cfg->ui32horOffset || p_cfg->ui32verOffset)
11525  {
11526  strncpy(p_param_err, "crop Parameters not supported for JPEG", max_err_len);
11528  LRETURN;
11529  }
11530  if (p_cfg->niParamT408.tier)
11531  {
11532  strncpy(p_param_err, "Tier is not supported for JPEG", max_err_len);
11534  LRETURN;
11535  }
11536  if (p_cfg->ui8tuneBframeVisual)
11537  {
11538  strncpy(p_param_err, "TuneBframeVisual is not supported for JPEG", max_err_len);
11540  LRETURN;
11541  }
11542  if (p_cfg->ui8hdr10_enable)
11543  {
11544  strncpy(p_param_err,
11545  "masterDisplay not supported for jpeg",
11546  max_err_len);
11547  warning = NI_RETCODE_PARAM_WARN;
11548  }
11549  if (p_cfg->niParamT408.conf_win_top != 0)
11550  {
11551  strncpy(p_param_err, "confWinTop is not supported in jpeg",
11552  max_err_len);
11554  LRETURN;
11555  }
11556  if (p_cfg->niParamT408.conf_win_bottom != 0)
11557  {
11558  strncpy(p_param_err, "confWinBottom is not supported in jpeg",
11559  max_err_len);
11561  LRETURN;
11562  }
11563  if (p_cfg->niParamT408.conf_win_left != 0)
11564  {
11565  strncpy(p_param_err, "confWinLeft is not supported in jpeg",
11566  max_err_len);
11568  LRETURN;
11569  }
11570  if (p_cfg->niParamT408.conf_win_right != 0)
11571  {
11572  strncpy(p_param_err, "confWinRight is not supported in jpeg",
11573  max_err_len);
11575  LRETURN;
11576  }
11577  if (p_cfg->ui8hrdEnable)
11578  {
11579  strncpy(p_param_err, "hrdEnable is not supported on jpeg encoder",
11580  max_err_len);
11582  LRETURN;
11583  }
11584  if (p_cfg->ui8EnableAUD)
11585  {
11586  strncpy(p_param_err, "enableAUD is not supported on jpeg encoder",
11587  max_err_len);
11589  LRETURN;
11590  }
11591  if (p_cfg->ui8repeatHeaders)
11592  {
11593  strncpy(p_param_err, "repeatHeaders is not supported on jpeg encoder",
11594  max_err_len);
11596  LRETURN;
11597  }
11599  {
11600  strncpy(p_param_err, "prefTRC is not supported on jpeg encoder",
11601  max_err_len);
11603  LRETURN;
11604  }
11605  if (p_cfg->ui8HDR10CLLEnable)
11606  {
11607  strncpy(p_param_err, "maxCLL is not supported on jpeg encoder",
11608  max_err_len);
11610  LRETURN;
11611  }
11612  if (p_cfg->ui8colorPrimaries != 2)
11613  {
11614  strncpy(p_param_err, "colorPri is not supported on jpeg encoder",
11615  max_err_len);
11617  LRETURN;
11618  }
11619  if (p_cfg->ui8colorTrc != 2)
11620  {
11621  strncpy(p_param_err, "colorTrc is not supported on jpeg encoder",
11622  max_err_len);
11624  LRETURN;
11625  }
11626  if (p_cfg->ui8colorSpace != 2)
11627  {
11628  strncpy(p_param_err, "colorSpc is not supported on jpeg encoder",
11629  max_err_len);
11631  LRETURN;
11632  }
11633  if (p_src->sar_num)
11634  {
11635  strncpy(p_param_err, "sarNum is not supported on jpeg encoder",
11636  max_err_len);
11638  LRETURN;
11639  }
11640  if (p_src->sar_denom != 1)
11641  {
11642  strncpy(p_param_err, "sarDenom is not supported on jpeg encoder",
11643  max_err_len);
11645  LRETURN;
11646  }
11647  if (p_src->video_full_range_flag != -1)
11648  {
11649  strncpy(p_param_err,
11650  "videoFullRangeFlag is not supported on jpeg encoder",
11651  max_err_len);
11653  LRETURN;
11654  }
11655  if (p_cfg->ui8temporalLayersEnable)
11656  {
11657  strncpy(p_param_err,
11658  "temporalLayersEnable is not supported on jpeg encoder",
11659  max_err_len);
11661  LRETURN;
11662  }
11663  if (p_cfg->ui8spatialLayersMinusOne > 0)
11664  {
11665  strncpy(p_param_err,
11666  "spatialLayers is not supported for jpeg encode",
11667  max_err_len);
11669  LRETURN;
11670  }
11671  if (p_cfg->ui8LowDelay)
11672  {
11673  strncpy(p_param_err, "LowDelay is not supported on jpeg encoder",
11674  max_err_len);
11676  LRETURN;
11677  }
11678  if (p_cfg->ui8rdoLevel)
11679  {
11680  strncpy(p_param_err, "rdoLevel is not supported on jpeg encoder",
11681  max_err_len);
11683  LRETURN;
11684  }
11685  if (p_cfg->ui8EnableRdoQuant)
11686  {
11687  strncpy(p_param_err, "EnableRdoQuant is not supported on jpeg encoder",
11688  max_err_len);
11690  LRETURN;
11691  }
11692  if (p_cfg->ui8enable2PassGopPatern)
11693  {
11694  strncpy(p_param_err,
11695  "enable2PassGop is not supported on jpeg encoder",
11696  max_err_len);
11698  LRETURN;
11699  }
11700  if (p_cfg->ui8LookAheadDepth)
11701  {
11702  strncpy(p_param_err,
11703  "lookAheadDepth is not supported on jpeg encoder",
11704  max_err_len);
11706  LRETURN;
11707  }
11709  {
11710  strncpy(p_param_err,
11711  "gopPresetIdx is not supported on jpeg encoder",
11712  max_err_len);
11714  LRETURN;
11715  }
11716  if (p_cfg->niParamT408.roiEnable)
11717  {
11718  strncpy(p_param_err, "roiEnable is not supported on jpeg encoder",
11719  max_err_len);
11721  LRETURN;
11722  }
11723  if(p_src->roi_demo_mode)
11724  {
11725  strncpy(p_param_err,
11726  "RoiDemoMode is not supported on jpeg encoder",
11727  max_err_len);
11729  LRETURN;
11730  }
11731  if (p_src->cacheRoi)
11732  {
11733  strncpy(p_param_err, "cacheRoi is not supported on jpeg encoder",
11734  max_err_len);
11736  LRETURN;
11737  }
11739  {
11740  strncpy(p_param_err,
11741  "ReconfDemoMode is not supported on jpeg encoder",
11742  max_err_len);
11744  LRETURN;
11745  }
11746  if (p_cfg->niParamT408.intraRefreshMode)
11747  {
11748  strncpy(p_param_err,
11749  "intraRefreshMode is not supported on jpeg encoder",
11750  max_err_len);
11752  LRETURN;
11753  }
11754  if (p_cfg->niParamT408.intraRefreshArg)
11755  {
11756  strncpy(p_param_err,
11757  "intraRefreshArg is not supported on jpeg encoder",
11758  max_err_len);
11760  LRETURN;
11761  }
11762  if (p_cfg->niParamT408.intra_period != 120)
11763  {
11764  strncpy(p_param_err,
11765  "intraPeriod is not supported on jpeg encoder",
11766  max_err_len);
11768  LRETURN;
11769  }
11770  if (p_cfg->ui8intraResetRefresh)
11771  {
11772  strncpy(
11773  p_param_err,
11774  "IntraRefreshResetOnForceIDR is not supported on jpeg encoder",
11775  max_err_len);
11777  LRETURN;
11778  }
11779  if (p_cfg->niParamT408.useLongTerm)
11780  {
11781  strncpy(p_param_err,
11782  "longTermReferenceEnable is not supported on jpeg encoder",
11783  max_err_len);
11785  LRETURN;
11786  }
11787  if (p_cfg->ui32setLongTermInterval)
11788  {
11789  strncpy(p_param_err,
11790  "longTermReferenceInterval is not supported on jpeg encoder",
11791  max_err_len);
11793  LRETURN;
11794  }
11795  if (p_cfg->ui8setLongTermCount != 2)
11796  {
11797  strncpy(p_param_err,
11798  "longTermReferenceCount is not supported on jpeg encoder",
11799  max_err_len);
11801  LRETURN;
11802  }
11803  if (p_cfg->ui8multicoreJointMode)
11804  {
11805  strncpy(p_param_err,
11806  "multicoreJointMode is not supported on jpeg encoder",
11807  max_err_len);
11809  LRETURN;
11810  }
11811  if (p_cfg->ui8enableSSIM)
11812  {
11813  strncpy(p_param_err, "enableSSIM is not supported on jpeg encoder",
11814  max_err_len);
11816  LRETURN;
11817  }
11818  if (p_enc->rc.vbv_buffer_size != -1)
11819  {
11820  strncpy(p_param_err,
11821  "vbvBufferSize is not supported on jpeg encoder",
11822  max_err_len);
11824  LRETURN;
11825  }
11826  if (p_cfg->ui8fillerEnable)
11827  {
11828  strncpy(p_param_err,
11829  "fillerEnable is not supported on jpeg encoder",
11830  max_err_len);
11832  LRETURN;
11833  }
11834  if (p_cfg->ui8picSkipEnable)
11835  {
11836  strncpy(p_param_err, "picSkip is not supported on jpeg encoder",
11837  max_err_len);
11839  LRETURN;
11840  }
11841  if (p_cfg->ui16maxFrameSize)
11842  {
11843  strncpy(p_param_err, "maxFrameSize is not supported on jpeg encoder",
11844  max_err_len);
11846  LRETURN;
11847  }
11849  {
11850  strncpy(p_param_err,
11851  "cuLevelRCEnable is not supported on jpeg encoder",
11852  max_err_len);
11854  LRETURN;
11855  }
11856  if (p_cfg->niParamT408.enable_hvs_qp)
11857  {
11858  strncpy(p_param_err, "hvsQPEnable is not supported on jpeg encoder",
11859  max_err_len);
11861  LRETURN;
11862  }
11863  if (p_cfg->niParamT408.profile)
11864  {
11865  strncpy(p_param_err, "profile is not supported on jpeg encoder",
11866  max_err_len);
11868  LRETURN;
11869  }
11871  {
11872  strncpy(p_param_err, "intraRefreshMode or intraRefreshArg is not supported on jpeg encoder",
11873  max_err_len);
11875  LRETURN;
11876  }
11877  if (p_cfg->i8crf != -1)
11878  {
11879  strncpy(p_param_err, "crf is not supported on jpeg encoder",
11880  max_err_len);
11882  LRETURN;
11883  }
11884  if (p_cfg->i32tolCtbRcInter != (int32_t)(0.1 * 1000))
11885  {
11886  strncpy(p_param_err, "tolCtbRcInter is not supported on jpeg encoder",
11887  max_err_len);
11889  LRETURN;
11890  }
11891  if (p_cfg->i32tolCtbRcIntra != (int32_t)(0.1 * 1000))
11892  {
11893  strncpy(p_param_err, "tolCtbRcIntra is not supported on jpeg encoder",
11894  max_err_len);
11896  LRETURN;
11897  }
11898  if (p_cfg->ui8rcQpDeltaRange != 10)
11899  {
11900  strncpy(p_param_err,
11901  "rcQpDeltaRange is not supported on jpeg encoder",
11902  max_err_len);
11904  LRETURN;
11905  }
11906  if (p_cfg->i16bitrateWindow != -255)
11907  {
11908  strncpy(p_param_err, "bitrateWindow is not supported on jpeg encoder",
11909  max_err_len);
11911  LRETURN;
11912  }
11913  if (p_cfg->i16ctbRowQpStep)
11914  {
11915  strncpy(p_param_err, "ctbRowQpStep is not supported on jpeg encoder",
11916  max_err_len);
11918  LRETURN;
11919  }
11920  if (p_cfg->ui8AiEnhanceMode)
11921  {
11922  strncpy(p_param_err,
11923  "enableAIEnhance is not supported on jpeg encoder",
11924  max_err_len);
11926  LRETURN;
11927  }
11928  if (p_cfg->i8ppsInitQp)
11929  {
11930  strncpy(p_param_err, "ppsInitQp is not supported for jpeg encoder",
11931  max_err_len);
11933  LRETURN;
11934  }
11935  if (p_cfg->i8pass1Qp)
11936  {
11937  strncpy(p_param_err, "pass1Qp is not supported for jpeg encoder",
11938  max_err_len);
11940  LRETURN;
11941  }
11942  if (p_enc->bitrateMode != -1)
11943  {
11944  strncpy(p_param_err, "bitrateMode is not supported for jpeg encoder",
11945  max_err_len);
11947  LRETURN;
11948  }
11949  if (p_cfg->ui8vbvBufferReencode)
11950  {
11951  strncpy(p_param_err,
11952  "vbvBufferReencode is not supported on jpeg encoder",
11953  max_err_len);
11955  LRETURN;
11956  }
11957  }
11958 
11959  if (p_src->force_frame_type != 0 && p_src->force_frame_type != 1)
11960  {
11961  strncpy(p_param_err, "Invalid forceFrameType: out of range",
11962  max_err_len);
11963  param_ret = NI_RETCODE_INVALID_PARAM;
11964  LRETURN;
11965  }
11966 
11967  if (p_cfg->niParamT408.forcedHeaderEnable > 2)
11968  {
11969  strncpy(p_param_err, "Invalid forcedHeaderEnable: out of range",
11970  max_err_len);
11971  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
11972  LRETURN;
11973  }
11974 
11975  if (p_cfg->niParamT408.decoding_refresh_type < 0 ||
11977  {
11978  strncpy(p_param_err, "Invalid decoding_refresh_type: out of range", max_err_len);
11980  LRETURN;
11981  }
11982 
11983  if (!QUADRA)
11984  {
11985  if (p_cfg->niParamT408.gop_preset_index < 0 ||
11986  p_cfg->niParamT408.gop_preset_index > 8)
11987  {
11988  strcpy(p_param_err, "Invalid gop_preset_index: out of range");
11990  LRETURN;
11991  }
11992 
11993  if (p_src->low_delay_mode && 1 != p_cfg->niParamT408.gop_preset_index &&
11994  2 != p_cfg->niParamT408.gop_preset_index &&
11995  3 != p_cfg->niParamT408.gop_preset_index &&
11996  6 != p_cfg->niParamT408.gop_preset_index &&
11997  7 != p_cfg->niParamT408.gop_preset_index &&
11998  !(0 == p_cfg->niParamT408.gop_preset_index &&
12000  {
12001  strcpy(p_param_err, "GOP size must be 1 when lowDelay is enabled");
12003  LRETURN;
12004  }
12005  }
12006  else // QUADRA
12007  {
12008  if (p_cfg->ui8gopSize > 16)
12009  {
12010  strncpy(p_param_err, "Invalid gopSize out of range", max_err_len);
12012  LRETURN;
12013  }
12014 
12015  if (p_cfg->ui8gopLowdelay &&
12016  p_cfg->ui8gopSize > 4)
12017  {
12018  strncpy(p_param_err, "GOP size must be <= 4 for low delay GOP", max_err_len);
12020  LRETURN;
12021  }
12022 
12023  if (p_cfg->ui8LookAheadDepth)
12024  {
12025  if (p_cfg->ui8LookAheadDepth < 4 || p_cfg->ui8LookAheadDepth > 40)
12026  {
12027  strncpy(p_param_err, "Invalid LookAheadDepth: out of range. <[4-40]>", max_err_len);
12029  LRETURN;
12030  }
12031  if (p_cfg->ui8gopLowdelay)
12032  {
12033  strncpy(p_param_err, "2-pass encode does not support low delay GOP", max_err_len);
12035  LRETURN;
12036  }
12038  {
12039  strncpy(p_param_err, "2-pass encode does not support tile4x4 format",
12040  max_err_len);
12042  LRETURN;
12043  }
12044  if (p_cfg->ui8spatialLayersMinusOne > 0)
12045  {
12046  strncpy(p_param_err, "currently do not support lookahead encode with multi spatial layers", max_err_len);
12048  LRETURN;
12049  }
12050  }
12051 
12052  if (p_src->low_delay_mode || p_cfg->ui8picSkipEnable)
12053  {
12055  {
12056  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
12057  {
12058  if (p_cfg->niParamT408.custom_gop_params.pic_param[i].poc_offset != (i+1))
12059  {
12060  if (p_src->low_delay_mode)
12061  strncpy(p_param_err, "Custom GOP must not include backward prediction when lowDelay is enabled", max_err_len);
12062  else
12063  strncpy(p_param_err, "Custom GOP must not include backward prediction when picSkip is enabled", max_err_len);
12064  param_ret = NI_RETCODE_INVALID_PARAM;
12065  LRETURN;
12066  }
12067  }
12068  } else if (1 != p_cfg->ui8gopSize && !p_cfg->ui8gopLowdelay &&
12069  p_cfg->niParamT408.intra_period != 1 &&
12071  {
12072  if (p_src->low_delay_mode)
12073  {
12074  if (p_cfg->ui8spatialLayersMinusOne == 0)
12075  {
12076  strncpy(p_param_err, "Must use low delay GOP (gopPresetIdx 1,3,7,9,10) when lowDelay is enabled", max_err_len);
12078  LRETURN;
12079  }
12080  }
12081  else
12082  {
12083  strncpy(p_param_err, "Must use low delay GOP (gopPresetIdx 1,3,7,9,10) when picSkip is enabled", max_err_len);
12085  LRETURN;
12086  }
12087  } else if ((p_cfg->ui8LookAheadDepth != 0) && (!p_cfg->ui8useLowDelayPocType))
12088  {
12089  if (p_src->low_delay_mode)
12090  strncpy(p_param_err, "lookAheadDepth must be 0 when lowDelay is enabled", max_err_len);
12091  else
12092  strncpy(p_param_err, "lookAheadDepth must be 0 when picSkip is enabled", max_err_len);
12093  param_ret = NI_RETCODE_INVALID_PARAM;
12094  LRETURN;
12095  }
12096 
12097  if (p_cfg->ui8multicoreJointMode)
12098  {
12099  if (p_src->low_delay_mode)
12100  strncpy(p_param_err,
12101  "Cannot use multicoreJointMode when lowDelay is enabled",
12102  max_err_len);
12103  else
12104  strncpy(p_param_err,
12105  "Cannot use multicoreJointMode when picSkip is enabled",
12106  max_err_len);
12107  param_ret = NI_RETCODE_INVALID_PARAM;
12108  LRETURN;
12109  }
12110 
12111  if (p_src->minFramesDelay)
12112  {
12113  if (p_src->low_delay_mode)
12114  strncpy(p_param_err,
12115  "Cannot enable minFramesDelay when lowDelay is enabled",
12116  max_err_len);
12117  else
12118  strncpy(p_param_err,
12119  "Cannot enable minFramesDelay when picSkip is enabled",
12120  max_err_len);
12121  param_ret = NI_RETCODE_INVALID_PARAM;
12122  LRETURN;
12123  }
12124  }
12125 
12126  if (p_cfg->ui8spatialLayersMinusOne > 0)
12127  {
12128  int intra_period_in_frames = p_cfg->niParamT408.intra_period * (p_cfg->ui8spatialLayersMinusOne+1);
12129  p_cfg->niParamT408.intra_period = p_cfg->niParamT408.avcIdrPeriod = intra_period_in_frames;
12130  }
12131 
12132  if (p_cfg->ui8useLowDelayPocType)
12133  {
12135  {
12136  strncpy(p_param_err, "Custom GOP size must be 1 when useLowDelayPocType is enabled", max_err_len);
12137  param_ret = NI_RETCODE_INVALID_PARAM;
12138  LRETURN;
12139  } else if (1 != p_cfg->ui8gopSize && !p_cfg->ui8gopLowdelay &&
12140  p_cfg->niParamT408.intra_period != 1)
12141  {
12142  strncpy(p_param_err, "Must use GOP with all frames as reference frames (gopPresetIdx 1,3,7,9) when useLowDelayPocType is enabled", max_err_len);
12144  LRETURN;
12145  }
12146  }
12147 
12148  if (p_src->low_delay_mode)
12149  {
12150  // minimum acceptable value of maxFrameSize is bitrate / framerate in bytes
12151  uint32_t min_maxFrameSize = p_cfg->i32bitRate / p_cfg->i32frameRateInfo * p_cfg->i32frameRateDenominator / 8;
12152 
12153  if (p_cfg->ui16maxFrameSize == 0)
12154  {
12155  if (p_enc->maxFrameSizeRatio > 0)
12156  {
12157  if (min_maxFrameSize * p_enc->maxFrameSizeRatio > NI_MAX_FRAME_SIZE)
12158  {
12159  p_cfg->ui16maxFrameSize = NI_MAX_FRAME_SIZE / 2000;
12160  } else
12161  {
12162  p_cfg->ui16maxFrameSize = min_maxFrameSize * p_enc->maxFrameSizeRatio / 2000;
12163  }
12164  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: Set maxFrameSize to %d times the "
12165  "minimum frame size %d bytes in low delay mode\n", __func__,
12166  p_enc->maxFrameSizeRatio, p_cfg->ui16maxFrameSize * 2000);
12167  } else
12168  {
12169  p_cfg->ui16maxFrameSize = ((p_src->source_width * p_src->source_height * 3 / 4) * p_ctx->bit_depth_factor) / 2000;
12170  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: maxFrameSize is not set in low delay "
12171  "mode. Set it to half of the maximum frame size %d bytes\n",
12172  __func__, p_cfg->ui16maxFrameSize*2000);
12173  }
12174  }
12175 
12176  if (p_cfg->ui16maxFrameSize < min_maxFrameSize / 2000)
12177  {
12178  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: maxFrameSize %u is too small. Changed to minimum value (bitrate/framerate in byte): %u\n",
12179  __func__, p_cfg->ui16maxFrameSize*2000, min_maxFrameSize);
12180  p_cfg->ui16maxFrameSize = min_maxFrameSize / 2000;
12181  }
12182  }
12183  else
12184  {
12185  if (p_cfg->ui16maxFrameSize != 0 || p_enc->maxFrameSizeRatio > 0)
12186  {
12187  strncpy(p_param_err, "maxFrameSize can only be used when lowDelay is enabled", max_err_len);
12188  param_ret = NI_RETCODE_INVALID_PARAM;
12189  LRETURN;
12190  }
12191  }
12192  }
12193 
12194  if (QUADRA)
12195  {
12196  if (p_cfg->ui16gdrDuration)
12197  {
12199  {
12200  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
12201  {
12202  if (2 == p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
12203  {
12204  strncpy(p_param_err, "Custom GOP can not have B frames for intra refresh", max_err_len);
12205  param_ret = NI_RETCODE_INVALID_PARAM;
12206  LRETURN;
12207  }
12208  }
12209  }
12210  else if (p_cfg->ui8gopSize != 1 || p_cfg->ui8gopLowdelay)
12211  {
12213  {
12214  strncpy(p_param_err,
12215  "Must use gopPresetIdx 9 (consecutive P frame) for intra refresh",
12216  max_err_len);
12217  param_ret = NI_RETCODE_INVALID_PARAM;
12218  LRETURN;
12219  }
12220  strncpy(p_param_warn, "GOP size forced to 1 and low delay GOP force disabled (no B frames) for intra refresh", max_err_len);
12221  warning = NI_RETCODE_PARAM_WARN;
12222  p_cfg->ui8gopSize = 1;
12223  p_cfg->ui8gopLowdelay = 0;
12224  }
12225  if (p_cfg->ui16gdrDuration == 1)
12226  {
12227  strncpy(p_param_err,
12228  "intra refresh cycle (height / intraRefreshArg MB or CTU) must > 1",
12229  max_err_len);
12230  param_ret = NI_RETCODE_INVALID_PARAM;
12231  LRETURN;
12232  }
12233  if (p_cfg->ui8LookAheadDepth != 0)
12234  {
12235  strncpy(p_param_err, "lookaheadDepth must be 0 for intra refresh", max_err_len);
12236  param_ret = NI_RETCODE_INVALID_PARAM;
12237  LRETURN;
12238  }
12239  if (STD_HEVC == p_cfg->ui8bitstreamFormat ||
12240  STD_AV1 == p_cfg->ui8bitstreamFormat)
12241  {
12242  if (p_cfg->niParamT408.intra_period < p_cfg->ui16gdrDuration)
12243  {
12244  strncpy(p_param_warn, "intraPeriod forced to match intra refersh cycle (intraPeriod must >= intra refersh cycle)", max_err_len);
12245  warning = NI_RETCODE_PARAM_WARN;
12246  p_cfg->niParamT408.intra_period = p_cfg->ui16gdrDuration;
12247  }
12248  }
12249  else if (STD_AVC == p_cfg->ui8bitstreamFormat)
12250  {
12251  if (p_cfg->niParamT408.avcIdrPeriod < p_cfg->ui16gdrDuration)
12252  {
12253  strncpy(p_param_warn, "intraPeriod forced to match intra refersh cycle (intraPeriod must >= intra refersh cycle)", max_err_len);
12254  warning = NI_RETCODE_PARAM_WARN;
12255  p_cfg->niParamT408.avcIdrPeriod = p_cfg->ui16gdrDuration;
12256  }
12257  }
12258  }
12259 
12260  if (p_cfg->ui8multicoreJointMode)
12261  {
12262  if (p_cfg->ui8hrdEnable)
12263  {
12264  strncpy(p_param_warn, "HRD conformance is not guaranteed in multicoreJointMode", max_err_len);
12265  warning = NI_RETCODE_PARAM_WARN;
12266  }
12267  }
12268 
12269  if (p_cfg->ui8temporalLayersEnable)
12270  {
12271  if (p_cfg->ui8hrdEnable)
12272  {
12273  snprintf(p_param_err, max_err_len, "HRD conformance with multiple temporal layers is currently not supported");
12274  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
12275  LRETURN;
12276  }
12277  }
12278 
12279  if ((p_cfg->ui8hrdEnable) || (p_cfg->ui8fillerEnable))
12280  {
12281  // enable rate control
12282  if (p_cfg->ui8rcEnable == 0)
12283  {
12284  p_cfg->ui8rcEnable = p_src->cfg_enc_params.rc.enable_rate_control = 1;
12285  }
12286 
12287  // enable hrd if it is off
12288  if (p_cfg->i32vbvBufferSize == 0)
12289  {
12290  p_cfg->i32vbvBufferSize = 3000;
12291  }
12292  }
12293 
12294  // maxrate must >= bitrate
12295  if (p_cfg->ui32vbvMaxRate != 0)
12296  {
12297  if (p_cfg->ui32vbvMaxRate < p_cfg->i32bitRate)
12298  {
12299  snprintf(p_param_err, max_err_len, "vbvMaxRate %u cannot be smaller than bitrate %d",
12300  p_cfg->ui32vbvMaxRate, p_cfg->i32bitRate);
12301  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
12302  LRETURN;
12303  }
12304  }
12305 
12306  // minrate must <= bitrate
12307  if (p_cfg->ui32vbvMinRate != 0)
12308  {
12309  if (p_cfg->ui32vbvMinRate > p_cfg->i32bitRate)
12310  {
12311  snprintf(p_param_err, max_err_len, "vbvMinRate %u cannot be larger than bitrate %d",
12312  p_cfg->ui32vbvMinRate, p_cfg->i32bitRate);
12313  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
12314  LRETURN;
12315  }
12316  }
12317 
12318  // The supported range of vbvBufferSize are 0, or 1/framerate to 3000. If the vbvBufferSize is not 0.
12319  // The minimum vbvBufferSize in msec is 1/framerate. The actual minimum in bits is bitrate/framerate.
12320  if (p_cfg->i32vbvBufferSize != 0)
12321  {
12322  // check buffer size compatible to bitrate
12323  uint32_t minVbvBufferSize = p_cfg->i32frameRateDenominator * 1000 / p_cfg->i32frameRateInfo;
12324  if (p_cfg->i32vbvBufferSize < minVbvBufferSize)
12325  {
12326  snprintf(p_param_err, max_err_len, "vbvBufferSize must be greater than the average frame size. Minimum is %u msec for framerate %d fps",
12327  minVbvBufferSize, (p_cfg->i32frameRateInfo / p_cfg->i32frameRateDenominator));
12328  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
12329  LRETURN;
12330  }
12331 
12332  // check buffer size compatible to maxrate
12333  if (p_cfg->ui32vbvMaxRate != 0)
12334  {
12335  uint32_t maxRateMinVbvBufferSize = ((int64_t)p_cfg->i32frameRateDenominator * 1000 / p_cfg->i32frameRateInfo) * p_cfg->ui32vbvMaxRate / p_cfg->i32bitRate;
12336  if (p_cfg->i32vbvBufferSize < maxRateMinVbvBufferSize)
12337  {
12338  snprintf(p_param_warn, max_err_len, "vbvBufferSize cannot be smaller than one frame size based on vbvMaxRate, force vbvBufferSize to %u msec for bitrate %d vbvMaxRate %u and framerate %d fps",
12339  maxRateMinVbvBufferSize, p_cfg->i32bitRate, p_cfg->ui32vbvMaxRate, (p_cfg->i32frameRateInfo / p_cfg->i32frameRateDenominator));
12340  warning = NI_RETCODE_PARAM_WARN;
12341  p_cfg->i32vbvBufferSize = maxRateMinVbvBufferSize;
12342  }
12343  }
12344  // check buffer size compatible to minrate (capped CRF may set minrate without maxrate)
12345  else if (p_cfg->ui32vbvMinRate != 0)
12346  {
12347  uint32_t minRateMinVbvBufferSize = ((int64_t)p_cfg->i32frameRateDenominator * 1000 / p_cfg->i32frameRateInfo) * p_cfg->ui32vbvMinRate / p_cfg->i32bitRate;
12348  if (p_cfg->i32vbvBufferSize < minRateMinVbvBufferSize)
12349  {
12350  snprintf(p_param_warn, max_err_len, "vbvBufferSize cannot be smaller than one frame size based on vbvMinRate, force vbvBufferSize to %u msec for bitrate %d vbvMinRate %u and framerate %d fps",
12351  minRateMinVbvBufferSize, p_cfg->i32bitRate, p_cfg->ui32vbvMinRate, (p_cfg->i32frameRateInfo / p_cfg->i32frameRateDenominator));
12352  warning = NI_RETCODE_PARAM_WARN;
12353  p_cfg->i32vbvBufferSize = minRateMinVbvBufferSize;
12354  }
12355  }
12356  }
12357  else
12358  {
12359  // check buffer size compatible to maxrate and/or minrate
12360  if ( p_cfg->ui32vbvMaxRate != 0 || p_cfg->ui32vbvMinRate != 0 )
12361  {
12362  snprintf(p_param_warn, max_err_len, "vbvMaxRate %u vbvMinRate %u does not take effect when vbvBufferSize is 0, force vbvMaxRate vbvMinRate to 0",
12363  p_cfg->ui32vbvMaxRate, p_cfg->ui32vbvMinRate);
12364  warning = NI_RETCODE_PARAM_WARN;
12365  p_cfg->ui32vbvMaxRate = 0;
12366  p_cfg->ui32vbvMinRate = 0;
12367  }
12368  }
12369 
12370  if (p_cfg->ui32ltrRefInterval || p_cfg->niParamT408.useLongTerm)
12371  {
12372  if (p_cfg->ui32ltrRefInterval && p_cfg->niParamT408.useLongTerm)
12373  {
12374  strncpy(p_param_err,
12375  "Can't enable ltrRefInterval and longTermReferenceEnable "
12376  "at same time",
12377  max_err_len);
12378  param_ret = NI_RETCODE_INVALID_PARAM;
12379  LRETURN;
12380  }
12381 
12383  {
12385  {
12386  strncpy(p_param_err, "Custom GOP size can not be > 1 for long term reference", max_err_len);
12387  param_ret = NI_RETCODE_INVALID_PARAM;
12388  LRETURN;
12389  }
12390  } else if ((p_cfg->ui8gopSize != 1) && (p_cfg->ui8gopLowdelay == 0) &&
12391  (p_cfg->niParamT408.intra_period != 1))
12392  {
12394  {
12395  strncpy(p_param_err,
12396  "Must use low delay GOP (gopPresetIdx 1,3,7,9) for long term reference",
12397  max_err_len);
12398  param_ret = NI_RETCODE_INVALID_PARAM;
12399  LRETURN;
12400  }
12401  strncpy(p_param_warn, "GOP size forced to 1 for long term reference", max_err_len);
12402  warning = NI_RETCODE_PARAM_WARN;
12403  p_cfg->ui8gopSize = 1;
12404  }
12405 
12406  if (p_cfg->ui8LookAheadDepth != 0)
12407  {
12408  strncpy(p_param_err, "lookaheadDepth must be 0 for long term reference", max_err_len);
12409  param_ret = NI_RETCODE_INVALID_PARAM;
12410  LRETURN;
12411  }
12412  if (p_cfg->i8crf >= 0)
12413  {
12414  strncpy(p_param_err, "crf must < 0 for long term reference", max_err_len);
12415  param_ret = NI_RETCODE_INVALID_PARAM;
12416  LRETURN;
12417  }
12418  }
12419 
12420  if (p_cfg->ui32setLongTermInterval && (p_cfg->niParamT408.useLongTerm == 0))
12421  {
12422  strncpy(
12423  p_param_err,
12424  "Must set longTermReferenceEnable for longTermReferenceInterval",
12425  max_err_len);
12426  param_ret = NI_RETCODE_INVALID_PARAM;
12427  LRETURN;
12428  }
12429 
12430  if (p_cfg->ui8av1ErrResilientMode)
12431  {
12432  if (STD_AV1 != p_cfg->ui8bitstreamFormat)
12433  {
12434  strncpy(p_param_warn, "AV1 err resilient mode forced to 0 when using other codecs", max_err_len);
12435  warning = NI_RETCODE_PARAM_WARN;
12436  p_cfg->ui8av1ErrResilientMode = 0;
12437  }
12438  }
12439 
12440  if (p_cfg->ui8motionConstrainedMode)
12441  {
12442  if (STD_HEVC != p_cfg->ui8bitstreamFormat)
12443  {
12444  strncpy(p_param_warn, "Motion Constrained mode force disabled for codecs other than HEVC", max_err_len);
12445  warning = NI_RETCODE_PARAM_WARN;
12446  p_cfg->ui8motionConstrainedMode = 0;
12447  }
12448  else
12449  {
12450  if (p_cfg->ui8rdoLevel != 1)
12451  {
12452  strncpy(
12453  p_param_err,
12454  "rdoLevel must be 1 for Motion Constrained mode 1 or 2",
12455  max_err_len);
12456  param_ret = NI_RETCODE_INVALID_PARAM;
12457  LRETURN;
12458  }
12459 
12461  {
12462  if (p_cfg->ui8multicoreJointMode)
12463  {
12464  strncpy(
12465  p_param_err,
12466  "multicoreJointMode must be 0 for Motion Constrained mode 2",
12467  max_err_len);
12468  param_ret = NI_RETCODE_INVALID_PARAM;
12469  LRETURN;
12470  }
12471  if (p_src->source_width % 64 || p_src->source_height % 64)
12472  {
12473  if (!p_cfg->ui32cropWidth || !p_cfg->ui32cropHeight ||
12474  p_cfg->ui32cropWidth % 64 || p_cfg->ui32cropHeight % 64)
12475  {
12476  strncpy(
12477  p_param_err,
12478  "input resolution (or cropping window) must be 64x64 aligned for Motion Constrained mode 2",
12479  max_err_len);
12480  param_ret = NI_RETCODE_INVALID_PARAM;
12481  LRETURN;
12482  }
12483  }
12484  }
12485  }
12486  }
12487 
12488  if (p_cfg->ui8avccHvcc)
12489  {
12490  if ((STD_AVC != p_cfg->ui8bitstreamFormat) && (STD_HEVC != p_cfg->ui8bitstreamFormat))
12491  {
12492  strncpy(p_param_warn, "AVCC HVCC forced to 0 for codecs other than AVC HEVC", max_err_len);
12493  warning = NI_RETCODE_PARAM_WARN;
12494  p_cfg->ui8avccHvcc = 0;
12495  }
12496  }
12497  }
12498 
12499  if (p_cfg->niParamT408.cu_size_mode < 0 ||
12500  p_cfg->niParamT408.cu_size_mode > 7)
12501  {
12502  strncpy(p_param_err, "Invalid cu_size_mode: out of range", max_err_len);
12504  LRETURN;
12505  }
12506 
12507 
12508 
12509  if (p_cfg->niParamT408.use_recommend_enc_params < 0 ||
12511  {
12512  strncpy(p_param_err, "Invalid use_recommend_enc_params: out of range", max_err_len);
12514  LRETURN;
12515  }
12516 
12517  switch (p_cfg->niParamT408.use_recommend_enc_params)
12518  {
12519  case 0:
12520  case 2:
12521  case 3:
12522  {
12523  if (p_cfg->niParamT408.use_recommend_enc_params != 3)
12524  {
12525  // in FAST mode (recommendEncParam==3), max_num_merge value will be
12526  // decided in FW
12527  if (p_cfg->niParamT408.max_num_merge < 0 ||
12528  p_cfg->niParamT408.max_num_merge > 3)
12529  {
12530  strncpy(p_param_err, "Invalid max_num_merge: out of range", max_err_len);
12532  LRETURN;
12533  }
12534  }
12535  break;
12536  }
12537 
12538  default: break;
12539  }
12540 
12541  if ( p_cfg->niParamT408.intra_qp < -1 ||
12542  p_cfg->niParamT408.intra_qp > 51 )
12543  {
12544  strncpy(p_param_err, "Invalid intra_qp: out of range", max_err_len);
12545  param_ret = NI_RETCODE_PARAM_ERROR_INTRA_QP;
12546  LRETURN;
12547  }
12548 
12549  if (QUADRA)
12550  {
12551  if (p_cfg->i8crf >= 0 && p_cfg->i8crf <= 51)
12552  {
12553  if (p_cfg->ui8LookAheadDepth < 4 || p_cfg->ui8LookAheadDepth > 40)
12554  {
12556  "6X") < 0)
12557  {
12558  strncpy(p_param_err, "CRF requres LookAheadDepth <[4-40]>", max_err_len);
12560  LRETURN;
12561  }
12562  else
12563  {
12564  p_cfg->ui8LookAheadDepth = 1;
12565  p_cfg->ui8noMbtree = 1;
12566  strncpy(p_param_warn, "enable lookahead of current frame", max_err_len);
12567  warning = NI_RETCODE_PARAM_WARN;
12568  }
12569  }
12570 
12571  if (p_cfg->ui8rcEnable == 1)
12572  {
12573  strncpy(p_param_err, "CRF requires RcEnable 0", max_err_len);
12574  param_ret = NI_RETCODE_PARAM_ERROR_RCENABLE;
12575  LRETURN;
12576  }
12577  #if 0
12578  if (p_cfg->ui8ctbRcMode > 0)
12579  {
12580  strncpy(p_param_warn, "Lookahead with cuLevelRCEnable or hvsQPEnable may degrade quality", max_err_len);
12581  warning = NI_RETCODE_PARAM_WARN;
12582  //LRETURN;
12583  }
12584  #endif
12585  }
12586 
12588  {
12589  if (p_cfg->ui8LookAheadDepth == 0)
12590  {
12591  strncpy(p_param_err, "tuneBframeVisual level 1 (medium) requires lookahead or crf encode", max_err_len);
12592  param_ret = NI_RETCODE_PARAM_ERROR_RCENABLE;
12593  LRETURN;
12594  }
12595  }
12596  }
12597 
12598  if ( p_cfg->niParamT408.enable_mb_level_rc != 1 &&
12599  p_cfg->niParamT408.enable_mb_level_rc != 0 )
12600  {
12601  strncpy(p_param_err, "Invalid enable_mb_level_rc: out of range", max_err_len);
12602  param_ret = NI_RETCODE_PARAM_ERROR_RCENABLE;
12603  LRETURN;
12604  }
12605 
12606  {
12607  if ( p_cfg->niParamT408.minQpI < 0 ||
12608  p_cfg->niParamT408.minQpI > 51 )
12609  {
12610  strncpy(p_param_err, "Invalid min_qp: out of range", max_err_len);
12611  param_ret = NI_RETCODE_PARAM_ERROR_MN_QP;
12612  LRETURN;
12613  }
12614 
12615  if ( p_cfg->niParamT408.maxQpI < 0 ||
12616  p_cfg->niParamT408.maxQpI > 51 )
12617  {
12618  strncpy(p_param_err, "Invalid max_qp: out of range", max_err_len);
12619  param_ret = NI_RETCODE_PARAM_ERROR_MX_QP;
12620  LRETURN;
12621  }
12622 
12623  if ( p_cfg->niParamT408.enable_cu_level_rate_control != 1 &&
12625  {
12626  strncpy(p_param_err, "Invalid enable_cu_level_rate_control: out of range", max_err_len);
12628  LRETURN;
12629  }
12630 
12631  //if (p_cfg->niParamT408.enable_cu_level_rate_control == 1)
12632  {
12633  if ( p_cfg->niParamT408.enable_hvs_qp != 1 &&
12634  p_cfg->niParamT408.enable_hvs_qp != 0 )
12635  {
12636  strncpy(p_param_err, "Invalid enable_hvs_qp: out of range", max_err_len);
12638  LRETURN;
12639  }
12640 
12641  if (p_cfg->niParamT408.enable_hvs_qp)
12642  {
12643  if ( p_cfg->niParamT408.max_delta_qp < 0 ||
12644  p_cfg->niParamT408.max_delta_qp > 51 )
12645  {
12646  strncpy(p_param_err, "Invalid max_delta_qp: out of range", max_err_len);
12648  LRETURN;
12649  }
12650  }
12651  }
12652  // hrd is off when i32vbvBufferSize is 0
12653  if ((p_cfg->i32vbvBufferSize < 10 && p_cfg->i32vbvBufferSize != 0) || p_cfg->i32vbvBufferSize > 3000)
12654  {
12655  strncpy(p_param_err, "Invalid i32vbvBufferSize: out of range", max_err_len);
12657  LRETURN;
12658  }
12659  }
12660 
12661  // check valid for common param
12662  param_ret = ni_check_common_params(&p_cfg->niParamT408, p_src, p_param_err, max_err_len);
12663  if (param_ret != NI_RETCODE_SUCCESS)
12664  {
12665  LRETURN;
12666  }
12667 
12668  // check valid for RC param
12669  param_ret = ni_check_ratecontrol_params(p_cfg, p_param_err, max_err_len);
12670  if (param_ret != NI_RETCODE_SUCCESS)
12671  {
12672  LRETURN;
12673  }
12674 
12675  if (warning == NI_RETCODE_PARAM_WARN && param_ret == NI_RETCODE_SUCCESS)
12676  {
12677  param_ret = NI_RETCODE_PARAM_WARN;
12678  strncpy(p_param_err, p_param_warn, max_err_len);
12679  }
12680 
12681 END:
12682  free(p_param_warn);
12683  return param_ret;
12684 }
12685 
12687  ni_xcoder_params_t *p_src,
12688  char *p_param_err, uint32_t max_err_len)
12689 {
12691  int32_t low_delay = 0;
12692  int32_t intra_period_gop_step_size;
12693  int32_t i, j;
12694 
12695  if (!p_param || !p_src || !p_param_err)
12696  {
12697  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
12698  __func__);
12700  LRETURN;
12701  }
12702 
12703  //Zero out the error buffer
12704  memset(p_param_err, 0, max_err_len);
12705 
12706  // check low-delay gop structure
12707  if (!QUADRA)
12708  {
12709  if (0 == p_param->gop_preset_index) // common gop
12710  {
12711  if (p_param->custom_gop_params.custom_gop_size > 1)
12712  {
12713  int minVal = p_param->custom_gop_params.pic_param[0].poc_offset;
12714  low_delay = 1;
12715  for (i = 1; i < p_param->custom_gop_params.custom_gop_size; i++)
12716  {
12717  if (minVal > p_param->custom_gop_params.pic_param[i].poc_offset)
12718  {
12719  low_delay = 0;
12720  break;
12721  } else
12722  {
12723  minVal = p_param->custom_gop_params.pic_param[i].poc_offset;
12724  }
12725  }
12726  }
12727  }
12728  else if (p_param->gop_preset_index == 2 ||
12729  p_param->gop_preset_index == 3 ||
12730  p_param->gop_preset_index == 6 ||
12731  p_param->gop_preset_index == 7) // low-delay case (IPPP, IBBB)
12732  {
12733  low_delay = 1;
12734  }
12735 
12736  if (low_delay)
12737  {
12738  intra_period_gop_step_size = 1;
12739  }
12740  else
12741  {
12742  if (p_param->gop_preset_index == GOP_PRESET_IDX_CUSTOM)
12743  {
12744  intra_period_gop_step_size = p_param->custom_gop_params.custom_gop_size;
12745  }
12746  else
12747  {
12748  intra_period_gop_step_size = presetGopSize[p_param->gop_preset_index];
12749  }
12750  }
12751 
12752  if (((p_param->intra_period != 0) && ((p_param->intra_period < intra_period_gop_step_size+1) == 1)) ||
12753  ((p_param->avcIdrPeriod != 0) && ((p_param->avcIdrPeriod < intra_period_gop_step_size+1) == 1)))
12754  {
12755  strncpy(p_param_err, "Invalid intra_period and gop_preset_index: gop structure is larger than intra period", max_err_len);
12757  LRETURN;
12758  }
12759 
12760  if (((!low_delay) && (p_param->intra_period != 0) && ((p_param->intra_period % intra_period_gop_step_size) != 0)) ||
12761  ((!low_delay) && (p_param->avcIdrPeriod != 0) && ((p_param->avcIdrPeriod % intra_period_gop_step_size) != 0)))
12762  {
12763  strncpy(p_param_err, "Invalid intra_period and gop_preset_index: intra period is not a multiple of gop structure size", max_err_len);
12765  LRETURN;
12766  }
12767 
12768  if (p_param->gop_preset_index == GOP_PRESET_IDX_CUSTOM)
12769  {
12770  int temp_poc[NI_MAX_GOP_NUM];
12771  int min_poc = p_param->custom_gop_params.pic_param[0].poc_offset;
12772  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
12773  {
12775  {
12776  strncpy(p_param_err, "Invalid custom gop parameters: temporal_id larger than 7", max_err_len);
12778  LRETURN;
12779  }
12780 
12781  if (p_param->custom_gop_params.pic_param[i].temporal_id < 0)
12782  {
12783  strncpy(p_param_err, "Invalid custom gop parameters: temporal_id is zero or negative", max_err_len);
12785  LRETURN;
12786  }
12787  temp_poc[i] = p_param->custom_gop_params.pic_param[i].poc_offset;
12788  if (min_poc > temp_poc[i])
12789  {
12790  min_poc = temp_poc[i];
12791  }
12792  }
12793  int count_pos = 0;
12794  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
12795  {
12796  for (j = 0; j < p_param->custom_gop_params.custom_gop_size; j++)
12797  {
12798  if (temp_poc[j] == min_poc)
12799  {
12800  count_pos++;
12801  min_poc++;
12802  }
12803  }
12804  }
12805  if (count_pos != p_param->custom_gop_params.custom_gop_size)
12806  {
12807  strncpy(p_param_err, "Invalid custom gop parameters: poc_offset is invalid", max_err_len);
12809  LRETURN;
12810  }
12811  }
12812  }
12813  else // QUADRA
12814  {
12815  if (p_param->custom_gop_params.custom_gop_size)
12816  {
12817  int temp_poc[NI_MAX_GOP_NUM];
12818  int min_poc = p_param->custom_gop_params.pic_param[0].poc_offset;
12819  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
12820  {
12822  {
12823  strncpy(p_param_err, "Invalid custom gop parameters: temporal_id larger than 7", max_err_len);
12825  LRETURN;
12826  }
12827 
12828  if (p_param->custom_gop_params.pic_param[i].temporal_id < 0)
12829  {
12830  strncpy(p_param_err, "Invalid custom gop parameters: temporal_id is negative", max_err_len);
12832  LRETURN;
12833  }
12834 
12835  for (j = 0; j < p_param->custom_gop_params.pic_param[i].num_ref_pics; j++)
12836  {
12837  if (p_param->custom_gop_params.pic_param[i].rps[j].ref_pic == 0)
12838  {
12839  strncpy(p_param_err, "Invalid custom gop parameters: ref pic delta cannot be 0", max_err_len);
12841  LRETURN;
12842  }
12843  }
12844 
12845  for (j = 0; j < NI_MAX_REF_PIC; j++)
12846  {
12847  if (p_param->custom_gop_params.pic_param[i].rps[j].ref_pic != 0 &&
12848  p_param->custom_gop_params.pic_param[i].rps[j].ref_pic_used == -1)
12849  {
12850  ni_log(NI_LOG_ERROR,"g%drefPic%d specified without g%drefPic%dUsed specified!\n", i, j, i, j);
12852  LRETURN;
12853  }
12854  }
12855 
12856  temp_poc[i] = p_param->custom_gop_params.pic_param[i].poc_offset;
12857  if (min_poc > temp_poc[i])
12858  {
12859  min_poc = temp_poc[i];
12860  }
12861  }
12862  int count_pos = 0;
12863  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
12864  {
12865  for (j = 0; j < p_param->custom_gop_params.custom_gop_size; j++)
12866  {
12867  if (temp_poc[j] == min_poc)
12868  {
12869  count_pos++;
12870  min_poc++;
12871  }
12872  }
12873  }
12874  if (count_pos != p_param->custom_gop_params.custom_gop_size)
12875  {
12876  strncpy(p_param_err, "Invalid custom gop parameters: poc_offset is invalid", max_err_len);
12878  LRETURN;
12879  }
12880  }
12881  }
12882 
12883  if (0 == p_param->use_recommend_enc_params)
12884  {
12885  // RDO
12886  {
12887  int align_32_width_flag = p_src->source_width % 32;
12888  int align_16_width_flag = p_src->source_width % 16;
12889  int align_8_width_flag = p_src->source_width % 8;
12890  int align_32_height_flag = p_src->source_height % 32;
12891  int align_16_height_flag = p_src->source_height % 16;
12892  int align_8_height_flag = p_src->source_height % 8;
12893 
12894  if (((p_param->cu_size_mode & 0x1) == 0) && ((align_8_width_flag != 0) || (align_8_height_flag != 0)))
12895  {
12896  strncpy(p_param_err, "Invalid use_recommend_enc_params and cu_size_mode: picture width and height must be aligned with 8 pixels when enable CU8x8 of cu_size_mode. Recommend to set cu_size_mode |= 0x1 (CU8x8)", max_err_len);
12898  LRETURN;
12899  }
12900  else if (((p_param->cu_size_mode & 0x1) == 0) && ((p_param->cu_size_mode & 0x2) == 0) && ((align_16_width_flag != 0) || (align_16_height_flag != 0)))
12901  {
12902  strncpy(p_param_err, "Invalid use_recommend_enc_params and cu_size_mode: picture width and height must be aligned with 16 pixels when enable CU16x16 of cu_size_mode. Recommend to set cu_size_mode |= 0x2 (CU16x16)", max_err_len);
12904  LRETURN;
12905  }
12906  else if (((p_param->cu_size_mode & 0x1) == 0) && ((p_param->cu_size_mode & 0x2) == 0) && ((p_param->cu_size_mode & 0x4) == 0) && ((align_32_width_flag != 0) || (align_32_height_flag != 0)))
12907  {
12908  strncpy(p_param_err, "Invalid use_recommend_enc_params and cu_size_mode: picture width and height must be aligned with 32 pixels when enable CU32x32 of cu_size_mode. Recommend to set cu_size_mode |= 0x4 (CU32x32)", max_err_len);
12910  LRETURN;
12911  }
12912  }
12913  }
12914 
12915  if ((p_param->conf_win_top < 0) || (p_param->conf_win_top > 8192))
12916  {
12917  strncpy(p_param_err, "Invalid conf_win_top: out of range", max_err_len);
12919  LRETURN;
12920  }
12921  if (p_param->conf_win_top % 2)
12922  {
12923  strncpy(p_param_err, "Invalid conf_win_top: not multiple of 2", max_err_len);
12925  LRETURN;
12926  }
12927 
12928  if ((p_param->conf_win_bottom < 0) || (p_param->conf_win_bottom > 8192))
12929  {
12930  strncpy(p_param_err, "Invalid conf_win_bottom: out of range", max_err_len);
12932  LRETURN;
12933  }
12934  if (p_param->conf_win_bottom % 2)
12935  {
12936  strncpy(p_param_err, "Invalid conf_win_bottom: not multiple of 2", max_err_len);
12938  LRETURN;
12939  }
12940 
12941  if ((p_param->conf_win_left < 0) || (p_param->conf_win_left > 8192))
12942  {
12943  strncpy(p_param_err, "Invalid conf_win_left: out of range", max_err_len);
12945  LRETURN;
12946  }
12947  if (p_param->conf_win_left % 2)
12948  {
12949  strncpy(p_param_err, "Invalid conf_win_left: not multiple of 2", max_err_len);
12951  LRETURN;
12952  }
12953 
12954  if (p_param->conf_win_right < 0 || p_param->conf_win_right > 8192)
12955  {
12956  strncpy(p_param_err, "Invalid conf_win_right: out of range", max_err_len);
12958  LRETURN;
12959  }
12960  if (p_param->conf_win_right % 2)
12961  {
12962  strncpy(p_param_err, "Invalid conf_win_right: not multiple of 2", max_err_len);
12964  }
12965 
12966 END:
12967 
12968  return ret;
12969 }
12970 
12971 ni_retcode_t ni_check_ratecontrol_params(ni_encoder_config_t* p_cfg, char* p_param_err, uint32_t max_err_len)
12972 {
12974  ni_t408_config_t* p_param = NULL;
12975 
12976  if( (!p_cfg) || (!p_param_err) )
12977  {
12978  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
12979  __func__);
12981  LRETURN;
12982  }
12983  p_param = &p_cfg->niParamT408;
12984 
12985  //Zero out the error buffer
12986  memset(p_param_err, 0, max_err_len);
12987 
12988  if (p_param->roiEnable != 0 && p_param->roiEnable != 1)
12989  {
12990  strncpy(p_param_err, "Invalid roiEnable: out of range", max_err_len);
12992  LRETURN;
12993  }
12994 
12995  if (p_param->roiEnable && p_param->enable_hvs_qp)
12996  {
12997  strncpy(p_param_err, "hvsQPEnable and roiEnable: not mutually exclusive", max_err_len);
12999  LRETURN;
13000  }
13001 
13002  if (p_cfg->ui8rcEnable == 1)
13003  {
13004  if (p_param->minQpP > p_param->maxQpP || p_param->minQpB > p_param->maxQpB)
13005  {
13006  strncpy(p_param_err, "Invalid min_qp(P/B) and max_qp(P/B): min_qp cannot be larger than max_qp", max_err_len);
13008  LRETURN;
13009  }
13010  }
13011 
13012 END:
13013 
13014  return ret;
13015 }
13016 
13017 
13018 /*!******************************************************************************
13019  * \brief Print xcoder user configurations
13020  *
13021  * \param
13022  *
13023  * \return
13024  *******************************************************************************/
13025 void ni_params_print(ni_xcoder_params_t *const p_encoder_params)
13026 {
13027  if (!p_encoder_params)
13028  {
13029  return;
13030  }
13031 
13032  ni_encoder_cfg_params_t *p_enc = &p_encoder_params->cfg_enc_params;
13033 
13034  ni_log(NI_LOG_DEBUG, "XCoder Params:\n");
13035 
13036  ni_log(NI_LOG_DEBUG, "preset=%d\n", p_encoder_params->preset);
13037  ni_log(NI_LOG_DEBUG, "fps_number / fps_denominator=%u / %u\n",
13038  p_encoder_params->fps_number,
13039  p_encoder_params->fps_denominator);
13040 
13041  ni_log(NI_LOG_DEBUG, "source_width x source_height=%dx%d\n", p_encoder_params->source_width, p_encoder_params->source_height);
13042  ni_log(NI_LOG_DEBUG, "bitrate=%d\n", p_encoder_params->bitrate);
13043 
13044  ni_log(NI_LOG_DEBUG, "profile=%d\n", p_enc->profile);
13045  ni_log(NI_LOG_DEBUG, "level_idc=%d\n", p_enc->level_idc);
13046  ni_log(NI_LOG_DEBUG, "high_tier=%d\n", p_enc->high_tier);
13047 
13048  ni_log(NI_LOG_DEBUG, "frame_rate=%d\n", p_enc->frame_rate);
13049 
13050  ni_log(NI_LOG_DEBUG, "use_recommend_enc_params=%d\n", p_enc->use_recommend_enc_params);
13051  ni_log(NI_LOG_DEBUG, "cu_size_mode=%d\n", p_enc->cu_size_mode);
13052  ni_log(NI_LOG_DEBUG, "max_num_merge=%d\n", p_enc->max_num_merge);
13053  ni_log(NI_LOG_DEBUG, "enable_dynamic_8x8_merge=%d\n", p_enc->enable_dynamic_8x8_merge);
13054  ni_log(NI_LOG_DEBUG, "enable_dynamic_16x16_merge=%d\n", p_enc->enable_dynamic_16x16_merge);
13055  ni_log(NI_LOG_DEBUG, "enable_dynamic_32x32_merge=%d\n", p_enc->enable_dynamic_32x32_merge);
13056  // trans_rate not available in Rev B
13057  ni_log(NI_LOG_DEBUG, "enable_rate_control=%d\n", p_enc->rc.enable_rate_control);
13058  ni_log(NI_LOG_DEBUG, "enable_cu_level_rate_control=%d\n", p_enc->rc.enable_cu_level_rate_control);
13059  ni_log(NI_LOG_DEBUG, "enable_hvs_qp=%d\n", p_enc->rc.enable_hvs_qp);
13060  ni_log(NI_LOG_DEBUG, "enable_hvs_qp_scale=%d\n", p_enc->rc.enable_hvs_qp_scale);
13061  ni_log(NI_LOG_DEBUG, "hvs_qp_scale=%d\n", p_enc->rc.hvs_qp_scale);
13062  ni_log(NI_LOG_DEBUG, "min_qp=%d\n", p_enc->rc.min_qp);
13063  ni_log(NI_LOG_DEBUG, "max_qp=%d\n", p_enc->rc.max_qp);
13064  ni_log(NI_LOG_DEBUG, "max_delta_qp=%d\n", p_enc->rc.max_delta_qp);
13065  ni_log(NI_LOG_DEBUG, "vbv_buffer_size=%d\n", p_enc->rc.vbv_buffer_size);
13066  ni_log(NI_LOG_DEBUG, "enable_filler=%d\n", p_enc->rc.enable_filler);
13067  ni_log(NI_LOG_DEBUG, "enable_pic_skip=%d\n", p_enc->rc.enable_pic_skip);
13068 
13069  ni_log(NI_LOG_DEBUG, "forcedHeaderEnable=%d\n", p_enc->forced_header_enable);
13070  ni_log(NI_LOG_DEBUG, "roi_enable=%d\n", p_enc->roi_enable);
13071  ni_log(NI_LOG_DEBUG, "long_term_ref_enable=%d\n", p_enc->long_term_ref_enable);
13072  ni_log(NI_LOG_DEBUG, "long_term_ref_interval=%d\n", p_enc->long_term_ref_interval);
13073  ni_log(NI_LOG_DEBUG, "long_term_ref_count=%d\n", p_enc->long_term_ref_count);
13074  ni_log(NI_LOG_DEBUG, "conf_win_top=%d\n", p_enc->conf_win_top);
13075  ni_log(NI_LOG_DEBUG, "conf_win_bottom=%d\n", p_enc->conf_win_bottom);
13076  ni_log(NI_LOG_DEBUG, "conf_win_left=%d\n", p_enc->conf_win_left);
13077  ni_log(NI_LOG_DEBUG, "conf_win_right=%d\n", p_enc->conf_win_right);
13078 
13079  ni_log(NI_LOG_DEBUG, "intra_qp=%d\n", p_enc->rc.intra_qp);
13080  ni_log(NI_LOG_DEBUG, "enable_mb_level_rc=%d\n", p_enc->rc.enable_mb_level_rc);
13081 
13082  ni_log(NI_LOG_DEBUG, "intra_period=%d\n", p_enc->intra_period);
13083  ni_log(NI_LOG_DEBUG, "decoding_refresh_type=%d\n", p_enc->decoding_refresh_type);
13084 
13085  // Rev. B: H.264 only or HEVC-shared parameters, in ni_t408_config_t
13086  ni_log(NI_LOG_DEBUG, "enable_transform_8x8=%d\n", p_enc->enable_transform_8x8);
13087  ni_log(NI_LOG_DEBUG, "slice_mode=%d\n", p_enc->slice_mode);
13088  ni_log(NI_LOG_DEBUG, "slice_arg=%d\n", p_enc->slice_arg);
13089  ni_log(NI_LOG_DEBUG, "entropy_coding_mode=%d\n", p_enc->entropy_coding_mode);
13090  ni_log(NI_LOG_DEBUG, "intra_mb_refresh_mode=%d\n", p_enc->intra_mb_refresh_mode);
13091  ni_log(NI_LOG_DEBUG, "intra_mb_refresh_arg=%d\n", p_enc->intra_mb_refresh_arg);
13092  ni_log(NI_LOG_DEBUG, "intra_reset_refresh=%d\n", p_enc->intra_reset_refresh);
13093 
13094  ni_log(NI_LOG_DEBUG, "gop_preset_index=%d\n", p_enc->gop_preset_index);
13095 #ifndef QUADRA
13096  if (!QUADRA)
13097  {
13099  {
13100  int i;
13101  ni_log(NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_enc->custom_gop_params.custom_gop_size);
13102  for (i = 0; i < p_enc->custom_gop_params.custom_gop_size; i++)
13103  {
13104  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].pic_type=%d\n", i, p_enc->custom_gop_params.pic_param[i].pic_type);
13105  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].poc_offset=%d\n", i, p_enc->custom_gop_params.pic_param[i].poc_offset);
13106  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].pic_qp=%d\n", i, p_enc->custom_gop_params.pic_param[i].pic_qp);
13107  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].num_ref_pic_L0=%d\n", i, p_enc->custom_gop_params.pic_param[i].num_ref_pic_L0);
13108  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].ref_poc_L0=%d\n", i, p_enc->custom_gop_params.pic_param[i].ref_poc_L0);
13109  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].ref_poc_L1=%d\n", i, p_enc->custom_gop_params.pic_param[i].ref_poc_L1);
13110  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].temporal_id=%d\n", i, p_enc->custom_gop_params.pic_param[i].temporal_id);
13111  }
13112  }
13113  }
13114  else // QUADRA
13115 #endif
13116  {
13118  {
13119  int i, j;
13120  ni_log(NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_enc->custom_gop_params.custom_gop_size);
13121  for (i = 0; i < NI_MAX_GOP_NUM; i++)
13122  {
13123  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].poc_offset=%d\n", i, p_enc->custom_gop_params.pic_param[i].poc_offset);
13124  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].qp_offset=%d\n", i, p_enc->custom_gop_params.pic_param[i].qp_offset);
13125  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].qp_factor=%lf\n", i, p_enc->custom_gop_params.pic_param[i].qp_factor);
13126  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].temporal_id=%d\n", i, p_enc->custom_gop_params.pic_param[i].temporal_id);
13127  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].pic_type=%d\n", i, p_enc->custom_gop_params.pic_param[i].pic_type);
13128  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].num_ref_pics=%d\n", i, p_enc->custom_gop_params.pic_param[i].num_ref_pics);
13129  for (j = 0; j < NI_MAX_REF_PIC; j++)
13130  {
13131  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].rps[%d].ref_pic=%d\n", i, j, p_enc->custom_gop_params.pic_param[i].rps[j].ref_pic);
13132  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].rps[%d].ref_pic_used=%d\n", i, j, p_enc->custom_gop_params.pic_param[i].rps[j].ref_pic_used);
13133  }
13134  }
13135  }
13136  }
13137 
13138  return;
13139 }
13140 
13141 /*!******************************************************************************
13142  * \brief decoder keep alive thread function triggers every 1 second
13143  *
13144  * \param void thread args
13145  *
13146  * \return void
13147  *******************************************************************************/
13148 void *ni_session_keep_alive_thread(void *arguments)
13149 {
13151  ni_thread_arg_struct_t *args = (ni_thread_arg_struct_t *)arguments;
13152  ni_session_stats_t inst_info = {0};
13153  ni_session_context_t ctx = {0};
13154  uint64_t endtime = ni_gettime_ns();
13155  uint64_t current_time;
13156  ni_pthread_mutex_t *p_mutex;
13157  //interval(nanoseconds) is equals to ctx.keep_alive_timeout/3(330,000,000ns approximately equal to 1/3 second).
13158  uint64_t interval = args->keep_alive_timeout * 330000000LL;
13159 #ifndef _ANDROID
13160 #ifdef __linux__
13161  struct sched_param sched_param;
13162 
13163  // Linux has a wide variety of signals, Windows has a few.
13164  // A large number of signals will interrupt the thread, which will cause heartbeat command interval more than 1 second.
13165  // So just mask the unuseful signals in Linux
13166  sigset_t signal;
13167  sigfillset(&signal);
13168  ni_pthread_sigmask(SIG_BLOCK, &signal, NULL);
13169 
13170  /* set up schedule priority
13171  * first try to run with RR mode.
13172  * if fails, try to set nice value.
13173  * if fails either, ignore it and run with default priority.
13174  * Note: Scheduling requires root permission. App is probably exectued
13175  * without root so the priority for this thread might just end up
13176  * being default.
13177  */
13178  if (((sched_param.sched_priority = sched_get_priority_max(SCHED_RR)) ==
13179  -1) ||
13180  sched_setscheduler(syscall(SYS_gettid), SCHED_RR, &sched_param) < 0)
13181  {
13182  ni_log(NI_LOG_DEBUG, "%s cannot set scheduler: %s\n", __func__,
13183  strerror(NI_ERRNO));
13184  if (setpriority(PRIO_PROCESS, 0, -20) != 0)
13185  {
13186  ni_log(NI_LOG_DEBUG, "%s cannot set nice value: %s\n", __func__,
13187  strerror(NI_ERRNO));
13188  }
13189  }
13190 
13191 #elif defined(_WIN32)
13192  /* set up schedule priority.
13193  * try to set the current thread to time critical level which is the highest prioriy
13194  * level.
13195  */
13196  if (SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL) ==
13197  0)
13198  {
13199  ni_log(NI_LOG_DEBUG, "%s cannot set priority: %d.\n", __func__,
13200  GetLastError());
13201  }
13202 #endif
13203 #endif
13204 #ifndef _WIN32
13205  // Set thread name, name is KAT + hw_id + session_id
13206  // hw_id need at most 2 bytes, session_id at most 4 bytes.
13207  char name[16] = {0};
13208  snprintf(name, sizeof(name), "%s%.2x%.4x", "KAT", args->hw_id, args->session_id);
13209 #if __linux__
13210  prctl(PR_SET_NAME, name);
13211 #elif __APPLE__
13212  pthread_setname_np(name);
13213 #endif
13214 #endif
13215  // Initializes the session context variables that keep alive command and query status command need.
13217  ctx.last_access_time = endtime;
13218  ctx.hw_id = args->hw_id;
13219  ctx.session_id = args->session_id;
13221  ctx.device_type = args->device_type;
13222  ctx.blk_io_handle = args->device_handle;
13223  ctx.event_handle = args->thread_event_handle;
13224  ctx.p_all_zero_buf = args->p_buffer;
13226  volatile uint64_t * plast_access_time = args->plast_access_time;
13227  if((ctx.last_access_time - *plast_access_time) >=
13228  ctx.keep_alive_timeout * 1000000000LL)
13229  {
13231  "%s creation timeout. session_id=0x%X requested timeout: %" PRIu64
13232  "ns, ping time delta: %" PRIu64 "ns\n ",
13233  __func__, ctx.session_id,
13234  (uint64_t)ctx.keep_alive_timeout * 1000000000LL,
13235  ctx.last_access_time - *plast_access_time);
13236  }
13237  ni_log(NI_LOG_DEBUG, "%s ctx.keep_alive_timeout: %us.\n", __func__,
13238  ctx.keep_alive_timeout);
13239  p_mutex = args->p_mutex;
13240 
13241  for (;;)
13242  {
13243  ni_pthread_mutex_lock(p_mutex);
13244 
13245  retval =
13247  ctx.event_handle, ctx.p_all_zero_buf);
13248 
13249  retval = ni_query_session_stats(&ctx,
13250  ctx.device_type,
13251  &inst_info,
13252  retval,
13254 
13255  if (NI_RETCODE_SUCCESS == retval)
13256  {
13259  ctx.device_type,
13260  ctx.hw_id,
13261  &(ctx.session_id));
13262  }
13263 
13264  ni_pthread_mutex_unlock(p_mutex);
13265 
13266  if(retval)
13267  {
13268  uint32_t error_status = inst_info.ui32LastTransactionCompletionStatus;
13269  if(error_status == NI_RETCODE_SUCCESS)
13270  {
13271  /* QDFWSH-971: Error is sometimes captured by keep_alive_thread
13272  but LastTransactionCompletionStatus may be overwrited and cause
13273  incorrect log. In this case, check LastErrorStatus.*/
13274  ni_log(NI_LOG_ERROR, "session_no 0x%x inst_err_no may be overwrited!\n",
13275  ctx.session_id);
13278  ctx.device_type,
13279  ctx.hw_id,
13280  &(ctx.session_id));
13281  error_status = inst_info.ui32LastErrorStatus;
13282  }
13284  "Persistent failures detected, %s() line-%d: session_no 0x%x sess_err_no %u "
13285  "inst_err_no %u\n",
13286  __func__, __LINE__, ctx.session_id, inst_info.ui16ErrorCount, error_status);
13287  LRETURN;
13288  }
13290  /*If the interval between two heartbeats is greater then expected(interval) or
13291  acceptable(timeout) then the thread might have been blocked.*/
13292  if ((current_time - ctx.last_access_time) >= (2 * interval) || //*2 is for safety
13293  (current_time - ctx.last_access_time) >=
13294  args->keep_alive_timeout * 1000000000LL)
13295  {
13296  ni_log(
13297  NI_LOG_INFO,
13298  "%s was possibly blocked. session_id=0x%X requested timeout: %" PRIu64
13299  "ns, ping time delta: %" PRIu64 "ns\n ",
13300  __func__, ctx.session_id,
13301  (uint64_t)ctx.keep_alive_timeout * 1000000000LL,
13303  }
13304  *plast_access_time = ctx.last_access_time = current_time;
13305  if (ctx.session_id == NI_INVALID_SESSION_ID)
13306  {
13308  }
13309 
13310  // 1. If received failure, set the close_thread flag to TRUE, and exit,
13311  // then main thread will check this flag and return failure directly;
13312  // 2. skip checking VPU recovery.
13313  // If keep_alive thread detect the VPU RECOVERY before main thread,
13314  // the close_thread flag may damage the vpu recovery handling process.
13315  if ((NI_RETCODE_SUCCESS != retval) &&
13316  (NI_RETCODE_NVME_SC_VPU_RECOVERY != retval))
13317  {
13318  LRETURN;
13319  }
13320  endtime += interval;
13321  while (ni_gettime_ns() < endtime)
13322  {
13323  if (args->close_thread)
13324  {
13325  LRETURN;
13326  }
13327  ni_usleep(10000); // 10ms per loop
13328  }
13329  }
13330 
13331 END:
13332 
13333  if (NI_RETCODE_SUCCESS != retval)
13334  {
13335  ni_log(NI_LOG_ERROR, "%s abnormal closed:%d\n", __func__, retval);
13336  // changing the value to be True here means the thread has been closed.
13337  args->close_thread = true;
13338  }
13339 
13341 
13342  ni_log(NI_LOG_DEBUG, "%s(): exit\n", __func__);
13343 
13344  return NULL;
13345 }
13346 
13347 /*!******************************************************************************
13348 * \brief Open a xcoder upload instance
13349 *
13350 * \param p_ctx - pointer to caller allocated uploader session context
13351 *
13352 * \return
13353 * On success
13354 * NI_RETCODE_SUCCESS
13355 *
13356 * On failure
13357 * NI_RETCODE_INVALID_PARAM
13358 * NI_RETCODE_ERROR_MEM_ALOC
13359 * NI_RETCODE_ERROR_INVALID_SESSION
13360 * NI_RETCODE_FAILURE
13361 *******************************************************************************/
13363 {
13365  void * p_buffer = NULL;
13366  uint32_t ui32LBA = 0;
13367  uint32_t modelled_load;
13368 
13369  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
13370 
13371  if (!p_ctx)
13372  {
13373  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n",
13374  __func__);
13375  retval = NI_RETCODE_INVALID_PARAM;
13376  LRETURN;
13377  }
13378 
13379  //Create the session if the create session flag is set
13380  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
13381  {
13383  p_ctx->pts_table = NULL;
13384  p_ctx->dts_queue = NULL;
13385  p_ctx->p_leftover = NULL;
13386  p_ctx->buffer_pool = NULL;
13387  p_ctx->prev_size = 0;
13388  p_ctx->sent_size = 0;
13389  p_ctx->status = 0;
13390  p_ctx->key_frame_type = 0;
13391  p_ctx->ready_to_close = 0;
13392  p_ctx->rc_error_count = 0;
13393  p_ctx->frame_num = 0;
13394  p_ctx->pkt_num = 0;
13395  p_ctx->pkt_index = 0;
13396 
13397  //malloc zero data buffer
13398  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
13400  {
13401  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
13402  NI_ERRNO, __func__);
13403  retval = NI_RETCODE_ERROR_MEM_ALOC;
13404  LRETURN;
13405  }
13406  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
13407 
13408  //malloc data buffer
13409  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
13410  {
13411  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
13412  NI_ERRNO, __func__);
13413  retval = NI_RETCODE_ERROR_MEM_ALOC;
13414  LRETURN;
13415  }
13416  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
13417 
13418  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
13419  //In case we can open sesison, the session id would become valid.
13420  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
13421  (uint16_t)NI_INVALID_SESSION_ID;
13422 
13423  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
13424  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_ENCODER, ni_htonl(p_ctx->codec_format), 1/*1 for uploadMode*/);
13425  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13426  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
13427  if (retval != NI_RETCODE_SUCCESS)
13428  {
13429  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
13430  LRETURN;
13431  }
13432  //Open will return a session status structure with a valid session id if it worked.
13433  //Otherwise the invalid session id set before the open command will stay
13434  p_ctx->session_id = ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
13435  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
13436  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
13437  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
13438  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
13439  {
13440  ni_log2(p_ctx, NI_LOG_ERROR,
13441  "ERROR %s(): p_ctx->device_handle=%" PRIx64
13442  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
13443  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
13444  p_ctx->session_id);
13445  ni_encoder_session_close(p_ctx, 0);
13447  LRETURN;
13448  }
13449  ni_log2(p_ctx, NI_LOG_DEBUG,
13450  "Uploader open session ID:0x%x,timestamp:%" PRIu64 "\n",
13451  p_ctx->session_id, p_ctx->session_timestamp);
13452 
13453  //Send keep alive timeout Info
13454  uint64_t keep_alive_timeout =
13455  p_ctx->keep_alive_timeout * 1000000; //send us to FW
13456  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
13457  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
13458  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
13459  keep_alive_timeout);
13461  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13462  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
13463  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
13464  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13465  CHECK_VPU_RECOVERY(retval);
13466 
13467  if (NI_RETCODE_SUCCESS != retval)
13468  {
13469  ni_log2(p_ctx, NI_LOG_ERROR,
13470  "ERROR %s(): nvme write keep_alive_timeout command "
13471  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
13472  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
13474  LRETURN;
13475  }
13476 
13477  // only update firmware with pixel rate if firmware >= 6rf
13478  if (ni_cmp_fw_api_ver(
13479  (char *) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rf") >= 0)
13480  {
13481  /* Add the modelled load of the hwuploader */
13482  if (p_ctx->framerate.framerate_denom == 0)
13483  {
13484  // unknown or variable frame rate. Assume 25 fps
13485  modelled_load = (p_ctx->active_video_width * p_ctx->active_video_height) * 25;
13486  }
13487  else
13488  {
13489  modelled_load = (p_ctx->active_video_width * p_ctx->active_video_height *
13491  }
13492 
13493  // modelled_load can overflow a 4-byte value so we will downscale it to
13494  // kilopixels per sec by shifting the modelled load down by 10 bits.
13495  modelled_load >>= 10;
13496 
13498 
13499  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
13500  memcpy(p_buffer, &modelled_load, 4);
13501 
13502  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13503  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
13504 
13505  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
13506  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13507  CHECK_VPU_RECOVERY(retval);
13508 
13509  if (NI_RETCODE_SUCCESS != retval)
13510  {
13512  LRETURN;
13513  }
13514  }
13515 
13516  ni_log2(p_ctx, NI_LOG_DEBUG, "Open session completed\n");
13517  }
13518 
13519  // init for frame pts calculation
13520  p_ctx->is_first_frame = 1;
13521  p_ctx->last_pts = 0;
13522  p_ctx->last_dts = 0;
13523 
13524  ni_timestamp_init(p_ctx, &p_ctx->pts_table, "dec_pts");
13525  ni_timestamp_init(p_ctx, &p_ctx->dts_queue, "dec_dts");
13526 
13527  //p_ctx->active_video_width = 0;
13528  //p_ctx->active_video_height = 0;
13529 
13530  ni_log2(p_ctx, NI_LOG_DEBUG,
13531  "%s(): p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
13532  "p_ctx->session_id=%d\n",
13533  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
13534  p_ctx->session_id);
13535 
13536  p_ctx->hw_action = NI_CODEC_HW_NONE;
13537 #ifndef _WIN32
13538  // If this is a P2P upload session, open the Netint kernel driver
13539  if (p_ctx->isP2P)
13540  {
13541  retval = p2p_fill_pcie_address(p_ctx);
13542  if(retval != NI_RETCODE_SUCCESS)
13543  {
13544  LRETURN;
13545  }
13546  }
13547 #endif
13548 
13549 END:
13550 
13551  ni_aligned_free(p_buffer);
13552  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
13553  return retval;
13554 }
13555 
13556 /*!******************************************************************************
13557 * \brief Copy a xcoder decoder worker thread info
13558 *
13559 * \param
13560 *
13561 * \return
13562 *******************************************************************************/
13564 {
13565  if (!src_p_ctx || !dst_p_ctx)
13566  {
13567  ni_log(NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n", __func__);
13568  return NI_RETCODE_INVALID_PARAM;
13569  }
13570 
13571  dst_p_ctx->pext_mutex = src_p_ctx->pext_mutex; //for hwdl
13572  dst_p_ctx->max_nvme_io_size = src_p_ctx->max_nvme_io_size;
13573  dst_p_ctx->device_handle = src_p_ctx->device_handle;
13574  dst_p_ctx->blk_io_handle = src_p_ctx->blk_io_handle;
13575  dst_p_ctx->hw_id = src_p_ctx->hw_id;
13576  dst_p_ctx->session_timestamp = src_p_ctx->session_timestamp;
13577  memcpy(dst_p_ctx->fw_rev, src_p_ctx->fw_rev, sizeof(src_p_ctx->fw_rev));
13578  if (src_p_ctx->isP2P)
13579  {
13580  dst_p_ctx->isP2P = src_p_ctx->isP2P;
13581  dst_p_ctx->ddr_config = src_p_ctx->ddr_config;
13582  dst_p_ctx->domain = src_p_ctx->domain;
13583  dst_p_ctx->bus = src_p_ctx->bus;
13584  dst_p_ctx->dev = src_p_ctx->dev;
13585  dst_p_ctx->fn = src_p_ctx->fn;
13586  dst_p_ctx->netint_fd = src_p_ctx->netint_fd;
13587  }
13588 
13589  return NI_RETCODE_SUCCESS;
13590 }
13591 
13592 
13593 /*!******************************************************************************
13594  * \brief Query and acquire buffer from xcoder scaler instance
13595  *
13596  * \param[in] p_ctx pointer to scaler session context
13597  *
13598  * \return NI_RETCODE_INVALID_PARAM
13599  * NI_RETCODE_ERROR_INVALID_SESSION
13600  * NI_RETCODE_ERROR_MEM_ALOC
13601  * NI_RETCODE_ERROR_NVME_CMD_FAILED
13602  * NI_RETCODE_FAILURE
13603  *******************************************************************************/
13605 {
13607  ni_instance_buf_info_t sInstanceBuf = {0};
13608  int query_retry = 0;
13609 
13610  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
13611  {
13612  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
13613  __func__);
13615  }
13616 
13617  if (p_ctx->buffered_frame_index > 0)
13618  {
13619  ni_log2(p_ctx, NI_LOG_ERROR,
13620  "Session=0x%x: %s buffered_frame_index=%d return %d\n",
13621  p_ctx->session_id,
13622  __func__,
13623  p_ctx->buffered_frame_index,
13624  retval);
13625  return retval;
13626  }
13627 
13628  for (;;)
13629  {
13630  query_sleep(p_ctx);
13631  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: query by ni_query_instance_buf_info INST_BUF_INFO_RW_UPLOAD\n", __func__);
13633  NI_DEVICE_TYPE_SCALER, &sInstanceBuf);
13634 
13635  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
13636  p_ctx->device_type, p_ctx->hw_id,
13637  &(p_ctx->session_id), OPT_3);
13638 
13639  /* A frame index of zero is invalid, the memory acquisition failed */
13640  if (NI_RETCODE_SUCCESS != retval || (sInstanceBuf.hw_inst_ind.frame_index == 0 && query_retry >=500))
13641  {
13642  if (query_retry >=500)
13643  {
13644  ni_log2(p_ctx, NI_LOG_DEBUG,
13645  "scaler write exceeded max query retries. rc=%d try=%d\n",
13646  retval, query_retry);
13647  }
13648  retval = NI_RETCODE_ERROR_MEM_ALOC;
13649  LRETURN;
13650  }
13651  if (sInstanceBuf.hw_inst_ind.frame_index == 0 && query_retry < 500)
13652  {
13653  query_retry++;
13654  ni_pthread_mutex_unlock(&p_ctx->mutex);
13655  ni_usleep(100);
13656  ni_pthread_mutex_lock(&p_ctx->mutex);
13657  }
13658  else
13659  {
13660  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: FID %d\n", __func__,
13661  sInstanceBuf.hw_inst_ind.frame_index);
13662  p_ctx->buffered_frame_index = sInstanceBuf.hw_inst_ind.frame_index;
13663  break;
13664  }
13665  }
13666 
13667  ni_log2(p_ctx, NI_LOG_DEBUG,
13668  "Session=0x%x: %s got FrameIndex=%u buffered_frame_index=%d\n",
13669  p_ctx->session_id,
13670  __func__,
13671  sInstanceBuf.hw_inst_ind.frame_index,p_ctx->buffered_frame_index);
13672 
13673 END:
13674 
13675  if (NI_RETCODE_SUCCESS != retval)
13676  {
13677  ni_log2(p_ctx, NI_LOG_DEBUG,
13678  "%s Warning scalar read hwdesc fail rc %d or ind !\n",
13679  __func__, retval);
13680  }
13681  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
13682 
13683  return retval;
13684 }
13685 
13686 
13687 /*!******************************************************************************
13688 * \brief Query and acquire buffer from xcoder upload instance
13689 *
13690 * \param p_ctx pointer to uploader session context
13691 *
13692 * \return NI_RETCODE_SUCCESS
13693 * NI_RETCODE_ERROR_INVALID_SESSION
13694 * NI_RETCODE_ERROR_MEM_ALOC
13695 *******************************************************************************/
13697 {
13699  //uint32_t metadata_size = NI_APP_ENC_FRAME_META_DATA_SIZE;
13700  uint32_t frame_size_bytes = 0;
13701  uint32_t retry_count = 0;
13702  ni_instance_buf_info_t buf_info = { 0 };
13703 
13704  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
13705 
13706  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
13707  {
13708  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
13709  __func__);
13711  LRETURN;
13712  }
13713 
13714 //#ifdef MEASURE_LATENCY
13715 #if 0
13716  if ((p_frame->pts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
13717  {
13718  uint64_t abs_time_ns = ni_gettime_ns();
13720  abs_time_ns, p_frame->pts);
13721  }
13722 #endif
13723 
13724  //p_ctx->status = 0;
13725 
13726  if (p_ctx->buffered_frame_index > 0)
13727  return retval;
13728 
13729  for (;;)
13730  {
13731  query_sleep(p_ctx);
13732 
13734  NI_DEVICE_TYPE_ENCODER, &buf_info);
13735  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
13736  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13737  if (NI_RETCODE_SUCCESS != retval ||
13738  (buf_info.hw_inst_ind.buffer_avail == 0 && retry_count >= 500))
13739  {
13740  if (retry_count >= 500)
13741  {
13742  ni_log2(p_ctx, NI_LOG_DEBUG,
13743  "hwupload write exceeded max query retries. rc=%d try=%d"
13744  "\n",
13745  retval, retry_count);
13746  }
13747  retval = NI_RETCODE_ERROR_MEM_ALOC;
13748  //p_ctx->status = NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL;
13749  LRETURN;
13750  }
13751  if (buf_info.hw_inst_ind.buffer_avail == 0 && retry_count < 500)
13752  {
13753  retry_count++;
13754  ni_pthread_mutex_unlock(&p_ctx->mutex);
13755  ni_usleep(100);
13756  ni_pthread_mutex_lock(&p_ctx->mutex);
13757  }
13758  else
13759  {
13760  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: avail %d, FID %d\n", __func__,
13761  buf_info.hw_inst_ind.buffer_avail,
13762  buf_info.hw_inst_ind.frame_index);
13763 
13764  p_ctx->buffered_frame_index = buf_info.hw_inst_ind.frame_index;
13765  break;
13766  }
13767  }
13768  ni_log2(p_ctx, NI_LOG_DEBUG, "Info hwupload write query success, available buf "
13769  "size %u >= frame size %u , retry %u\n",
13770  buf_info.buf_avail_size, frame_size_bytes, retry_count);
13771 
13772 //#ifdef MEASURE_LATENCY
13773 #if 0
13774  if ((p_frame->pts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
13775  {
13776  uint64_t abs_time_ns = ni_gettime_ns();
13778  ni_log(NI_LOG_INFO, "PTS:%" PRId64 ",DELTA:%" PRId64 ",uLAT:%" PRIu64 ";\n",
13779  p_frame->pts, abs_time_ns - q->last_benchmark_time,
13780  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->pts));
13781  q->last_benchmark_time = abs_time_ns;
13782  }
13783 #endif
13784 
13785  //retval = size;
13786 
13787 END:
13788 
13789  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
13790  return retval;
13791 }
13792 
13793 
13794 /*!******************************************************************************
13795 * \brief Send a YUV p_frame to upload session
13796 *
13797 * \param
13798 *
13799 * \return
13800 *******************************************************************************/
13802  niFrameSurface1_t *hwdesc)
13803 {
13804  int retval = 0;
13805  uint32_t size = 0;
13806  //uint32_t metadata_size = NI_APP_ENC_FRAME_META_DATA_SIZE;
13807  uint32_t i = 0;
13808  uint32_t sent_size = 0;
13809  uint32_t frame_size_bytes = 0;
13810  uint32_t retry_count = 0;
13811  ni_instance_buf_info_t buf_info = { 0 };
13812 
13813  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
13814  //niFrameSurface1_t* p_data3 = (niFrameSurface1_t*)((uint8_t*)p_frame->p_data[3]);
13816  //ni_log2(p_ctx, NI_LOG_DEBUG, "%s:mar16 HW=%d ui16FrameIdx=%d i8InstID=%d device_handle=%d\n",
13817  // ishwframe, p_data3->ui16FrameIdx, p_data3->i8InstID, p_data3->device_handle);
13818 
13819  if (!p_ctx || !p_frame)
13820  {
13821  ni_log(NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
13822  __func__);
13823  retval = NI_RETCODE_INVALID_PARAM;
13824  LRETURN;
13825  }
13826 
13827  uint8_t separate_metadata = p_frame->separate_metadata;
13828  uint8_t separate_start = (p_frame->separate_start && p_frame->total_start_len) ? 1 : 0;
13829 
13830  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
13831  {
13832  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
13833  __func__);
13835  LRETURN;
13836  }
13837 
13838 #ifdef MEASURE_LATENCY
13839  if ((p_frame->pts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
13840  {
13841  uint64_t abs_time_ns = ni_gettime_ns();
13843  abs_time_ns, p_frame->pts);
13844  }
13845 #endif
13846 
13847  frame_size_bytes = p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2];// +p_frame->data_len[3] + p_frame->extra_data_len;
13848  ni_log2(p_ctx, NI_LOG_DEBUG, "frame size bytes =%u %d is metadata!\n", frame_size_bytes,
13849  0);
13850  p_ctx->status = 0;
13851 
13852  if (p_frame->end_of_stream)
13853  {
13854  retval = NI_RETCODE_SUCCESS;
13855  LRETURN;
13856  }
13857 
13858  if (p_ctx->buffered_frame_index > 0)
13859  {
13860  buf_info.hw_inst_ind.frame_index = p_ctx->buffered_frame_index;
13861  p_ctx->buffered_frame_index = 0;
13862  }
13863  else
13864  {
13865  for (;;)
13866  {
13867  query_sleep(p_ctx);
13868 
13870  NI_DEVICE_TYPE_ENCODER, &buf_info);
13871  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
13872  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13873  if (NI_RETCODE_SUCCESS != retval ||
13874  (buf_info.hw_inst_ind.buffer_avail == 0 && retry_count >= 500))
13875  {
13876  if (retry_count >= 500)
13877  {
13878  ni_log2(p_ctx, NI_LOG_DEBUG,
13879  "hwupload write exceeded max query retries. rc=%d try=%d"
13880  "\n",
13881  retval, retry_count);
13882  }
13883  retval = NI_RETCODE_ERROR_MEM_ALOC;
13885  LRETURN;
13886  }
13887  if (buf_info.hw_inst_ind.buffer_avail == 0 && retry_count < 500)
13888  {
13889  retry_count++;
13890  ni_pthread_mutex_unlock(&p_ctx->mutex);
13891  ni_usleep(100);
13892  ni_pthread_mutex_lock(&p_ctx->mutex);
13893  } else //available
13894  {
13895  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: avail %d, FID %d\n", __func__,
13896  buf_info.hw_inst_ind.buffer_avail,
13897  buf_info.hw_inst_ind.frame_index);
13898  break;
13899  }
13900  }
13901  ni_log2(p_ctx, NI_LOG_DEBUG, "Info hwupload write query success, available buf "
13902  "size %u >= frame size %u , retry %u\n",
13903  buf_info.buf_avail_size, frame_size_bytes, retry_count);
13904  }
13905 
13906  {
13907 #ifdef XCODER_TIMESTAMP_DTS_ENABLED
13908  retval = ni_timestamp_register(p_ctx->buffer_pool, p_ctx->dts_queue,
13909  p_frame->dts, 0);
13910  if (NI_RETCODE_SUCCESS != retval)
13911  {
13912  ni_log2(p_ctx, NI_LOG_ERROR,
13913  "ERROR %s(): ni_timestamp_register() for dts "
13914  "returned: %d\n",
13915  __func__, retval);
13916  }
13917 #endif
13918 
13919  //Apply write configuration here
13920  retval = ni_config_session_rw(p_ctx, SESSION_WRITE_CONFIG, 1,
13921  NI_CODEC_HW_UPLOAD, 0);
13922  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
13923  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13924  CHECK_VPU_RECOVERY(retval);
13925 
13926  if (separate_metadata)
13927  {
13929  "6S") < 0)
13930  {
13931  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): uploader separated metadata not supported on device with FW api version < 6.S\n",
13932  __func__);
13934  LRETURN;
13935  }
13936 
13937  if (!p_frame->p_metadata_buffer)
13938  {
13939  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_metadata_buffer is NULL, allocation failed?\n",
13940  __func__);
13941  retval = NI_RETCODE_ERROR_MEM_ALOC;
13942  LRETURN;
13943  }
13944 
13945  // fill in metadata
13946  ni_metadata_enc_frame_t *p_meta;
13947  p_meta = (ni_metadata_enc_frame_t *)p_frame->p_metadata_buffer;
13948 
13949  if (separate_start)
13950  {
13951  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
13952  p_meta->start_len[i] = p_frame->start_len[i];
13953  }
13954  else
13955  {
13956  memset(p_meta->start_len, 0, sizeof(p_meta->start_len));
13957  }
13959 
13960  ni_log(
13961  NI_LOG_DEBUG,
13962  "%s(): %d.%u p_ctx->frame_num=%" PRIu64 ", "
13963  "p_frame->video_width=%u, p_frame->video_height=%u, "
13964  "start_len [%u,%u,%u] inconsecutive_transfer %u\n",
13965  __func__, p_ctx->hw_id, p_ctx->session_id, p_ctx->frame_num,
13966  p_frame->video_width, p_frame->video_height,
13967  p_meta->start_len[0], p_meta->start_len[1], p_meta->start_len[2],
13968  p_meta->inconsecutive_transfer);
13969 
13970  uint32_t ui32LBA_metadata =
13972  ni_log2(p_ctx, NI_LOG_DEBUG,
13973  "%s: p_metadata_buffer = %p, metadata_buffer_size "
13974  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
13975  __func__, p_frame->p_metadata_buffer,
13976  p_frame->metadata_buffer_size, p_ctx->frame_num,
13977  ui32LBA_metadata);
13978 
13979  sent_size =
13981 
13982  retval = ni_nvme_send_write_cmd(
13983  p_ctx->blk_io_handle, p_ctx->event_handle,
13984  p_frame->p_metadata_buffer, sent_size,
13985  ui32LBA_metadata);
13986  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
13987  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13988  CHECK_VPU_RECOVERY(retval);
13989  if (retval < 0)
13990  {
13991  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n",
13992  __func__);
13994  LRETURN;
13995  }
13996  }
13997 
13998  if (separate_start)
13999  {
14000  if (!p_frame->p_start_buffer)
14001  {
14002  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_start_buffer is NULL, allocation failed?\n",
14003  __func__);
14004  retval = NI_RETCODE_ERROR_MEM_ALOC;
14005  LRETURN;
14006  }
14007 
14008  uint32_t ui32LBA =
14010  ni_log2(p_ctx, NI_LOG_DEBUG,
14011  "%s: p_start_buffer = %p, p_frame->start_buffer_size "
14012  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
14013  __func__, p_frame->p_start_buffer, p_frame->start_buffer_size, p_ctx->frame_num,
14014  ui32LBA);
14015 
14016  sent_size =
14018 
14019  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
14020  p_frame->p_start_buffer, sent_size, ui32LBA);
14021  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
14022  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14023  CHECK_VPU_RECOVERY(retval);
14024  if (retval < 0)
14025  {
14026  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
14028  LRETURN;
14029  }
14030  }
14031 
14032  if (p_frame->inconsecutive_transfer)
14033  {
14034  uint32_t ui32LBA =
14036 
14037  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
14038  {
14039  ni_log2(p_ctx, NI_LOG_DEBUG,
14040  "%s: p_data = %p, p_frame->buffer_size "
14041  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
14042  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
14043  ui32LBA);
14044 
14045  if (p_frame->data_len[i])
14046  {
14047  sent_size = p_frame->data_len[i];
14048  if (separate_start)
14049  sent_size -= p_frame->start_len[i];
14050 
14051  sent_size =
14053 
14054  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
14055  p_frame->p_data[i]+p_frame->start_len[i], sent_size, ui32LBA);
14056  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
14057  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14058  CHECK_VPU_RECOVERY(retval);
14059  if (retval < 0)
14060  {
14061  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
14063  LRETURN;
14064  }
14065  }
14066  }
14067  }
14068  else
14069  {
14070  uint32_t ui32LBA =
14072  ni_log2(p_ctx, NI_LOG_DEBUG,
14073  "%s: p_data = %p, p_frame->buffer_size = %u, "
14074  "p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
14075  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
14076  ui32LBA);
14077 
14078  sent_size = frame_size_bytes;
14079  if (separate_start)
14080  sent_size -= p_frame->total_start_len;
14081 
14082  sent_size =
14084 
14085  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
14086  p_frame->p_buffer+p_frame->total_start_len, sent_size, ui32LBA);
14087  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
14088  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14089  CHECK_VPU_RECOVERY(retval);
14090  if (retval < 0)
14091  {
14092  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
14094  LRETURN;
14095  }
14096  }
14097 
14098  hwdesc->ui16FrameIdx = buf_info.hw_inst_ind.frame_index;
14099  hwdesc->ui16session_ID = p_ctx->session_id;
14100  hwdesc->device_handle =
14101  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
14102  hwdesc->bit_depth = p_ctx->bit_depth_factor;
14103  hwdesc->src_cpu = (uint8_t)NI_DEVICE_TYPE_ENCODER;
14104  hwdesc->output_idx = hwdesc->ui32nodeAddress = 0;
14105 
14106  p_ctx->frame_num++;
14107  size = frame_size_bytes;
14108 
14109 #ifdef XCODER_DUMP_DATA
14110  char dump_file[256];
14111  snprintf(dump_file, sizeof(dump_file), "%ld-%u-hwup-fme/fme-%04ld.yuv",
14112  (long)getpid(), p_ctx->session_id, (long)p_ctx->frame_num);
14113 
14114  FILE *f = fopen(dump_file, "wb");
14115  fwrite(p_frame->p_buffer,
14116  p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2],
14117  1, f);
14118  fflush(f);
14119  fclose(f);
14120 #endif
14121  }
14122 
14123 #ifdef MEASURE_LATENCY
14124  if ((p_frame->pts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
14125  {
14126  uint64_t abs_time_ns = ni_gettime_ns();
14128  ni_log(NI_LOG_INFO, "PTS:%" PRId64 ",DELTA:%" PRId64 ",uLAT:%" PRIu64 ";\n",
14129  p_frame->pts, abs_time_ns - q->last_benchmark_time,
14130  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->pts));
14131  q->last_benchmark_time = abs_time_ns;
14132  }
14133 #endif
14134 
14135  retval = size;
14136 
14137 END:
14138 
14139  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
14140  return retval;
14141 }
14142 
14143 /*!******************************************************************************
14144 * \brief Retrieve a HW descriptor of uploaded frame
14145 *
14146 * \param p_ctx pointer to uploader session context
14147 * hwdesc pointer to hw descriptor
14148 *
14149 * \return
14150 * On success
14151 * NI_RETCODE_SUCCESS
14152 * On failure
14153 * NI_RETCODE_INVALID_PARAM
14154 * NI_RETCODE_ERROR_INVALID_SESSION
14155 * NI_RETCODE_FAILURE
14156 *******************************************************************************/
14158  niFrameSurface1_t *hwdesc)
14159 {
14160  int retval = 0;
14161  ni_instance_buf_info_t hwdesc_info = { 0 };
14162  int query_retry = 0;
14163 
14164  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
14165 
14166  if (!p_ctx || !hwdesc)
14167  {
14168  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
14169  __func__);
14170  retval = NI_RETCODE_INVALID_PARAM;
14171  LRETURN;
14172  }
14173 
14174  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
14175  {
14176  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14177  __func__);
14179  LRETURN;
14180  }
14181 
14182  for (;;)
14183  {
14184  query_sleep(p_ctx);
14185 
14186  query_retry++;
14187 #ifndef _WIN32
14189  NI_DEVICE_TYPE_ENCODER, &hwdesc_info);
14190 #else
14192  NI_DEVICE_TYPE_ENCODER, &hwdesc_info);
14193 #endif
14194  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14195  p_ctx->device_type, p_ctx->hw_id,
14196  &(p_ctx->session_id), OPT_1);
14197 
14198  if (NI_RETCODE_SUCCESS != retval)
14199  {
14200  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning upload read hwdesc fail rc %d or ind "
14201  "!\n", retval);
14202 
14203  if (query_retry >= 1000)
14204  {
14205  retval = NI_RETCODE_FAILURE;
14206  LRETURN;
14207  }
14208  ni_usleep(100);
14209  }
14210  else
14211  {
14212  ni_log2(p_ctx, NI_LOG_DEBUG, "Info hwupload read hwdesc success, "
14213  "frame_ind=%d !\n", hwdesc_info.hw_inst_ind.frame_index);
14214 
14215  hwdesc->ui16FrameIdx = hwdesc_info.hw_inst_ind.frame_index;
14216  hwdesc->ui16session_ID = p_ctx->session_id;
14217  hwdesc->device_handle =
14218  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
14219  hwdesc->bit_depth = p_ctx->bit_depth_factor;
14220  hwdesc->src_cpu = (uint8_t)NI_DEVICE_TYPE_ENCODER;
14221  hwdesc->output_idx = 0;
14222  LRETURN;
14223  }
14224  }
14225 
14226 END:
14227  return retval;
14228 }
14229 
14230 /*!*****************************************************************************
14231 * \brief clear a particular xcoder instance buffer/data
14232 *
14233 * \param ni_session_context_t p_ctx - xcoder Context
14234 * \param ni_instance_buf_info_rw_type_t rw_type
14235 * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
14236 * \param ni_instance_buf_info_t *out - Struct preallocated from the caller
14237 * where the resulting data will be placed
14238 *
14239 * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
14240 * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
14241 * failure
14242 ******************************************************************************/
14244 {
14245  void* p_buffer = NULL;
14247  uint32_t ui32LBA = 0;
14248 
14249  ni_log(NI_LOG_TRACE, "%s(): enter - device_handle %d\n", __func__,
14250  surface->device_handle);
14251 
14252  if ((uint16_t)NI_INVALID_SESSION_ID == surface->ui16session_ID)
14253  {
14254  ni_log(NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14255  __func__);
14257  LRETURN;
14258  }
14259 
14260  //malloc data buffer
14261  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
14262  {
14263  ni_log(NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
14264  NI_ERRNO, __func__);
14265  retval = NI_RETCODE_ERROR_MEM_ALOC;
14266  LRETURN;
14267  }
14268  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
14269  ni_log(NI_LOG_DEBUG, "%s(): FID = %d\n", __func__, surface->ui16FrameIdx);
14270  ui32LBA = CLEAR_INSTANCE_BUF_W(((uint16_t)surface->ui16FrameIdx));
14271  retval = ni_nvme_send_write_cmd((ni_device_handle_t)(int64_t)surface->device_handle,
14272  NI_INVALID_DEVICE_HANDLE, p_buffer,
14273  NI_DATA_BUFFER_LEN, ui32LBA);
14274  //Cannot check sessio stats here since this isn't a session command.
14275  if (retval < 0)
14276  {
14277  ni_log(NI_LOG_ERROR, "[session_id=0x%x,time_stamp=%" PRIu64 "] " "%s(): NVME command Failed\n", surface->ui16session_ID, ni_log_get_utime(), __func__);
14279  LRETURN;
14280  }
14281 
14282 END:
14283 
14284  ni_aligned_free(p_buffer);
14285  ni_log(NI_LOG_TRACE, "%s(): exit\n", __func__);
14286  return retval;
14287 }
14288 
14289 /*!******************************************************************************
14290 * \brief Retrieve a hw desc p_frame from decoder
14291 * \param
14292 *
14293 * \return
14294 *******************************************************************************/
14296 {
14297  //Needs serious editing to support hwdesc read again, this is currently vanilla read
14298  //queue_info decoder_read_workerqueue;
14299  ni_instance_mgr_stream_info_t data = {0};
14300  int rx_size = 0;
14301  uint64_t frame_offset = 0;
14302  uint8_t *p_data_buffer = NULL;
14303  int i = 0;
14304  int retval = NI_RETCODE_SUCCESS;
14305  int metadata_hdr_size = NI_FW_META_DATA_SZ -
14307  int sei_size = 0;
14308  uint32_t total_bytes_to_read = 0;
14309  uint32_t total_yuv_met_size = 0;
14310  uint32_t read_size_bytes = 0;
14311  ni_instance_buf_info_t buf_info = {0};
14312  int query_retry = 0;
14313  uint32_t ui32LBA = 0;
14314  unsigned int bytes_read_so_far = 0;
14315  int query_type = INST_BUF_INFO_RW_READ;
14316  int low_delay_notify = 0;
14317  ni_session_statistic_t sessionStatistic = {0};
14318  uint32_t frames_dropped = 0;
14319  ni_xcoder_params_t *p_param;
14320  uint8_t get_first_metadata = 0;
14321  uint8_t sequence_change = 0;
14322 
14323  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
14324 
14325  if (!p_ctx || !p_frame)
14326  {
14327  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
14328  __func__);
14329  return NI_RETCODE_INVALID_PARAM;
14330  }
14331 
14332  ni_pthread_mutex_lock(&p_ctx->mutex);
14333 
14334 start:
14335  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
14336  {
14337  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14338  __func__);
14340  LRETURN;
14341  }
14342 
14343  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
14344  p_data_buffer = (uint8_t *)p_frame->p_buffer;
14345 
14346  // p_frame->p_data[] can be NULL before actual resolution is returned by
14347  // decoder and buffer pool is allocated, so no checking here.
14348 
14349  total_bytes_to_read = p_frame->data_len[3] + metadata_hdr_size;
14350  total_yuv_met_size = p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2] + metadata_hdr_size;
14351  ni_log2(p_ctx, NI_LOG_DEBUG,
14352  "Total bytes to read %u total_yuv_met_size %u, low_delay %u\n",
14353  total_bytes_to_read, total_yuv_met_size, p_ctx->decoder_low_delay);
14354  if (p_ctx->decoder_low_delay > 0 && !p_ctx->ready_to_close)
14355  {
14356  ni_log2(p_ctx, NI_LOG_DEBUG, "frame_num = %" PRIu64 ", pkt_num = %" PRIu64 "\n",
14357  p_ctx->frame_num, p_ctx->pkt_num);
14358  frames_dropped = p_ctx->session_statistic.ui32FramesDropped;
14359  if (p_ctx->force_low_delay && (p_ctx->force_low_delay_cnt < frames_dropped)) {
14360  p_ctx->force_low_delay_cnt = frames_dropped;
14361  }
14362  if (p_ctx->frame_num + p_ctx->force_low_delay_cnt
14363  >= p_ctx->pkt_num)
14364  {
14365  //nothing to query, leave
14366  retval = NI_RETCODE_SUCCESS;
14367  LRETURN;
14368  }
14369  query_type = INST_BUF_INFO_RW_READ_BUSY;
14370  }
14371  for (;;)
14372  {
14373  query_sleep(p_ctx);
14374 
14375  query_retry++;
14376 
14377  if (ni_cmp_fw_api_ver(
14379  "6r3") >= 0)
14380  {
14382  &sessionStatistic);
14383  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
14385  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
14386  CHECK_VPU_RECOVERY(retval);
14387 
14388  buf_info.buf_avail_size = sessionStatistic.ui32RdBufAvailSize;
14389  } else
14390  {
14391  retval = ni_query_instance_buf_info(p_ctx, query_type,
14392  NI_DEVICE_TYPE_DECODER, &buf_info);
14393  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14394  p_ctx->device_type, p_ctx->hw_id,
14395  &(p_ctx->session_id), OPT_1);
14396  CHECK_VPU_RECOVERY(retval);
14397  }
14398 
14399  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read desc query buf_info.size = %u\n",
14400  buf_info.buf_avail_size);
14401 
14402  if (NI_RETCODE_SUCCESS != retval)
14403  {
14404  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read desc failed. Retry %d\n", query_retry);
14405 
14406  if (query_retry >= 1000)
14407  {
14408  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning: dec read desc failed %d retries. rc=%d"
14409  "\n", query_retry, retval);
14410  p_ctx->max_retry_fail_count[1]++;
14411  low_delay_notify = 1;
14413  LRETURN;
14414  }
14415  ni_pthread_mutex_unlock(&p_ctx->mutex);
14417  ni_pthread_mutex_lock(&p_ctx->mutex);
14418  } else if (buf_info.buf_avail_size == DP_IPC_PASSTHRU)
14419  {
14420  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Bad available buffer size %u\n", __FUNCTION__, buf_info.buf_avail_size);
14421  retval = NI_RETCODE_FAILURE;
14422  LRETURN;
14423  } else if (buf_info.buf_avail_size == metadata_hdr_size)
14424  {
14425  ni_log2(p_ctx, NI_LOG_DEBUG,
14426  "Dec read desc only hdr metadata is available. Seq change may "
14427  "have occured.\n");
14428  total_bytes_to_read = metadata_hdr_size;
14429  sequence_change = 1;
14430  break;
14431  } else if (buf_info.buf_avail_size < total_yuv_met_size)
14432  {
14433  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read desc buf_size < frame_size. Retry %d\n", query_retry);
14434 
14435  // query to see if it is eos now, if we have sent it
14436  if (p_ctx->ready_to_close)
14437  {
14438  ni_log2(p_ctx, NI_LOG_TRACE,
14439  "Dec read desc query, ready_to_close %u, query eos\n",
14440  p_ctx->ready_to_close);
14441  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
14442  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14443  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14444  CHECK_VPU_RECOVERY(retval);
14445 
14446  if (data.is_flushed ||
14447  query_retry >=
14449  {
14450  if (query_retry >=
14452  {
14453  ni_log2(p_ctx, NI_LOG_ERROR,
14454  "WARNING: Dec read desc query eos reached but exceeded max "
14455  "retries. is_flushed=%u try=%d.\n",
14456  data.is_flushed, query_retry);
14457  } else
14458  {
14459  ni_log2(p_ctx, NI_LOG_DEBUG,
14460  "Dec read desc query eos reached. is_flushed=%u try=%d"
14461  "\n",
14462  data.is_flushed, query_retry);
14463  }
14464  p_frame->end_of_stream = 1;
14465  low_delay_notify = 1;
14466  retval = NI_RETCODE_SUCCESS;
14467  LRETURN;
14468  }
14469  else
14470  {
14471  ni_log2(p_ctx, NI_LOG_TRACE,
14472  "Dec read desc available buf size == %d, query try %d, "
14473  "retrying...\n",
14474  buf_info.buf_avail_size, query_retry);
14475  ni_pthread_mutex_unlock(&p_ctx->mutex);
14477  ni_pthread_mutex_lock(&p_ctx->mutex);
14478  continue;
14479  }
14480  }
14481 
14483  (p_ctx->decoder_low_delay > 0 &&
14484  ((p_ctx->frame_num + p_ctx->force_low_delay_cnt)
14485  < p_ctx->pkt_num))) &&
14486  query_retry < 1000 / 2)
14487  {
14488  if (p_ctx->decoder_low_delay && p_ctx->force_low_delay) {
14489  if (p_ctx->session_statistic.ui32FramesDropped > frames_dropped) {
14490  // last pkt sent to decoder marked as dropped, no output,
14491  // so just stop query and return
14492  p_ctx->force_low_delay_cnt++;
14493  low_delay_signal(p_ctx);
14494  retval = NI_RETCODE_SUCCESS;
14495  LRETURN;
14496  }
14497  } else if (NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL == p_ctx->status &&
14498  sessionStatistic.ui32WrBufAvailSize > p_ctx->required_buf_size)
14499  {
14500  ni_log2(p_ctx, NI_LOG_TRACE, "Info dec write buffer is enough, available buf "
14501  "size %u >= required size %u !\n",
14502  sessionStatistic.ui32WrBufAvailSize, p_ctx->required_buf_size);
14503  p_ctx->status = 0;
14504  p_ctx->required_buf_size = 0;
14505  retval = NI_RETCODE_SUCCESS;
14506  LRETURN;
14507  }
14508  ni_pthread_mutex_unlock(&p_ctx->mutex);
14510  ni_pthread_mutex_lock(&p_ctx->mutex);
14511 
14512  continue;
14513  } else
14514  {
14515  if (p_ctx->decoder_low_delay > 0)
14516  {
14517  if (p_ctx->force_low_delay) {
14518  p_ctx->force_low_delay_cnt++;
14519  low_delay_signal(p_ctx);
14520  } else {
14521  ni_log2(p_ctx, NI_LOG_ERROR,"Warning: low delay mode with non sequential "
14522  "input (B frames)? Just cancel the low delay mode then\n");
14523  // Here it should be the last signal to release the send thread
14524  // holding the low delay mutex.
14525  low_delay_signal(p_ctx);
14526  p_ctx->decoder_low_delay = 0;
14527  }
14528  }
14529 
14530  if ((p_param->dec_input_params.min_packets_delay && p_ctx->pkt_delay_cnt))
14531  {
14532  if(p_ctx->pkt_num >= (p_ctx->frame_num + p_ctx->pkt_delay_cnt +
14534  {
14535  if(query_retry <= 2000)
14536  {
14537  ni_pthread_mutex_unlock(&p_ctx->mutex);
14538  ni_usleep(25);
14539  ni_pthread_mutex_lock(&p_ctx->mutex);
14540  continue;
14541  } else {
14542  p_ctx->pkt_delay_cnt++;
14543  ni_log2(p_ctx, NI_LOG_ERROR,
14544  "Warning: decoder pkt_num %u frame_num %u "
14545  "timeout, increasing pkt_delay_cnt to %u\n",
14546  p_ctx->pkt_num, p_ctx->frame_num,
14547  p_ctx->pkt_delay_cnt);
14548  }
14549  }
14550  }
14551  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning: dec read desc failed %d retries. rc=%d"
14552  "\n", query_retry, retval);
14553  }
14554  retval = NI_RETCODE_SUCCESS;
14555  LRETURN;
14556  }
14557  else
14558  {
14559  // We have to ensure there are adequate number of DTS for picture
14560  // reorder delay otherwise wait for more packets to be sent to decoder.
14561  ni_timestamp_table_t *p_dts_queue = p_ctx->dts_queue;
14562  if ((int)p_dts_queue->list.count < p_ctx->pic_reorder_delay + 1 &&
14563  !p_ctx->ready_to_close &&
14565  {
14566  retval = NI_RETCODE_SUCCESS;
14567  ni_log2(p_ctx, NI_LOG_DEBUG,
14568  "At least %d packets should be sent before reading the "
14569  "first frame!\n",
14570  p_ctx->pic_reorder_delay + 1);
14571  LRETURN;
14572  }
14573  p_ctx->max_retry_fail_count[1] = 0;
14574 
14575  // get actual YUV transfer size if this is the stream's very first read
14576  if (0 == p_ctx->active_video_width || 0 == p_ctx->active_video_height)
14577  {
14578  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
14579  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14580  p_ctx->device_type, p_ctx->hw_id,
14581  &(p_ctx->session_id), OPT_1);
14582  CHECK_VPU_RECOVERY(retval);
14583 
14584  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV query, pic size %ux%u xfer frame size "
14585  "%ux%u frame-rate %u is_flushed %u\n",
14586  data.picture_width, data.picture_height,
14588  data.frame_rate, data.is_flushed);
14591  p_ctx->actual_video_width = data.picture_width;
14592  p_ctx->pixel_format = data.pix_format;
14594  //p_ctx->bit_depth_factor = data.transfer_frame_stride / data.picture_width;
14595  p_ctx->is_first_frame = 1;
14596  p_ctx->pixel_format_changed = 0;
14597 
14598  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV, adjust frame size from %ux%u to "
14599  "%ux%u\n", p_frame->video_width, p_frame->video_height,
14600  p_ctx->active_video_width, p_ctx->active_video_height);
14601 
14602  retval = ni_frame_buffer_alloc(
14603  p_frame, p_ctx->actual_video_width, p_ctx->active_video_height,
14604  p_ctx->codec_format == NI_CODEC_FORMAT_H264, 1,
14605  p_ctx->bit_depth_factor,
14606  3, // Alloc space for write to data[3] and metadata
14607  1);
14608 
14609  if (NI_RETCODE_SUCCESS != retval)
14610  {
14611  LRETURN;
14612  }
14613  total_bytes_to_read = p_frame->data_len[3] + metadata_hdr_size;
14614  p_data_buffer = (uint8_t*)p_frame->p_buffer;
14615  // make sure we don't read more than available
14616  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec buf size: %u YUV frame + meta-hdr size: %u "
14617  "available: %u\n", p_frame->buffer_size,
14618  total_bytes_to_read, buf_info.buf_avail_size);
14619  }
14620  break;
14621  }
14622  }// end while1 query retry
14623 
14624  ni_log2(p_ctx, NI_LOG_DEBUG, "total_bytes_to_read %u max_nvme_io_size %u ylen %u cr len "
14625  "%u cb len %u hdr %d\n",
14626  total_bytes_to_read, p_ctx->max_nvme_io_size,
14627  p_frame->data_len[0], p_frame->data_len[1],
14628  p_frame->data_len[2], metadata_hdr_size);
14629 
14630  ni_log2(p_ctx, NI_LOG_DEBUG, "p_frame->data_len[3] = %u\n", p_frame->data_len[3]);
14631  if (buf_info.buf_avail_size < total_bytes_to_read)
14632  {
14633  ni_pthread_mutex_unlock(&p_ctx->mutex);
14634 
14635  ni_log2(p_ctx, NI_LOG_ERROR,
14636  "ERROR %s() avaliable size(%u) less than "
14637  "needed (%u)\n",
14638  __func__, buf_info.buf_avail_size, total_bytes_to_read);
14639  abort();
14640  } else if (total_bytes_to_read == metadata_hdr_size && !p_ctx->frame_num)
14641  {
14642  if (ni_cmp_fw_api_ver(
14644  "6rE") >= 0)
14645  {
14646  // allocate p_data_buffer to read the first metadata
14647  void *p_metadata_buffer = NULL;
14648  int buffer_size = ((metadata_hdr_size + (NI_MEM_PAGE_ALIGNMENT - 1)) /
14650  if (ni_posix_memalign(&p_metadata_buffer, sysconf(_SC_PAGESIZE), buffer_size))
14651  {
14652  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate metadata buffer.\n",
14653  NI_ERRNO, __func__);
14654  retval = NI_RETCODE_ERROR_MEM_ALOC;
14655  LRETURN;
14656  }
14657  p_data_buffer = (uint8_t *)p_metadata_buffer;
14658  get_first_metadata = 1;
14659  sequence_change = 0;
14660  }
14661  }
14662 
14663  //Apply read configuration here
14664  retval = ni_config_session_rw(p_ctx, SESSION_READ_CONFIG, 1,
14665  NI_CODEC_HW_ENABLE, 0);
14666  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
14667  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14668  CHECK_VPU_RECOVERY(retval);
14669 
14670  read_size_bytes = total_bytes_to_read;
14672  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
14673  {
14674  read_size_bytes = ( (read_size_bytes / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
14675  }
14676 
14677  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
14678  p_data_buffer, read_size_bytes, ui32LBA);
14679  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
14680  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14681  CHECK_VPU_RECOVERY(retval);
14682  if (retval < 0)
14683  {
14684  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
14686  LRETURN;
14687  } else if (get_first_metadata) {
14688  // got first metadata alone
14689  ni_metadata_dec_frame_t *p_meta =
14690  (ni_metadata_dec_frame_t *)((uint8_t *)p_data_buffer);
14691  ni_log2(p_ctx, NI_LOG_DEBUG, "Got first pkt_delay_cnt %u\n",
14692  p_meta->metadata_common.pkt_delay_cnt);
14693  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
14694  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
14695  get_first_metadata = 0;
14696  ni_aligned_free(p_data_buffer);
14697  goto start;
14698  } else
14699  {
14700  // command issued successfully, now exit
14701  ni_metadata_dec_frame_t *p_meta;
14702  p_meta =
14703  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
14704  p_frame->data_len[0] +
14705  p_frame->data_len[1] +
14706  p_frame->data_len[2] +
14707  p_frame->data_len[3]);
14708 
14709  if (buf_info.buf_avail_size != metadata_hdr_size)
14710  {
14711  low_delay_notify = 1;
14712  // shift metadata to end of triple output
14713 #ifdef _WIN32
14714  p_data_buffer = (uint8_t *)p_frame->p_buffer +
14716  memcpy(p_meta, p_data_buffer, metadata_hdr_size);
14717 #else
14718  memcpy(p_meta,
14719  p_frame->p_buffer +
14721  metadata_hdr_size);
14722 #endif
14723  sei_size = p_meta->sei_size;
14724  niFrameSurface1_t *p_data3 =
14725  (niFrameSurface1_t *)((uint8_t *)p_frame->p_buffer +
14726  p_frame->data_len[0] +
14727  p_frame->data_len[1] +
14728  p_frame->data_len[2]);
14729 
14730  niFrameSurface1_t *p_data3_1 =
14731  (niFrameSurface1_t *)((uint8_t *)p_frame->p_buffer +
14732  sizeof(niFrameSurface1_t));
14733  niFrameSurface1_t *p_data3_2 =
14734  (niFrameSurface1_t *)((uint8_t *)p_frame->p_buffer +
14735  2 * sizeof(niFrameSurface1_t));
14736  // Libxcoder knows the handle so overwrite here
14737  p_data3->device_handle =
14738  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
14739  p_data3_1->device_handle =
14740  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
14741  p_data3_2->device_handle =
14742  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
14743  p_data3->ui16session_ID = p_data3_1->ui16session_ID =
14744  p_data3_2->ui16session_ID = (uint16_t)p_ctx->session_id;
14745  p_data3->src_cpu = p_data3_1->src_cpu = p_data3_2->src_cpu =
14746  (uint8_t)NI_DEVICE_TYPE_DECODER;
14747 
14748  p_data3->output_idx = 0;
14749  p_data3_1->output_idx = 1;
14750  p_data3_2->output_idx = 2;
14751 
14752  ni_log2(p_ctx, NI_LOG_DEBUG,
14753  "p_data3_1:sei_size=%d device_handle=%d == hw_id=%d ses_id=%d\n",
14754  sei_size, p_data3_1->device_handle, p_ctx->hw_id,
14755  p_data3_1->ui16session_ID);
14756  ni_log2(p_ctx, NI_LOG_DEBUG,
14757  "p_data3_1: ui16FrameIdx=%d NodeAddre=0x%x planar=%d bd=%d\n",
14758  p_data3_1->ui16FrameIdx, p_data3_1->ui32nodeAddress,
14759  p_data3_1->encoding_type, p_data3_1->bit_depth);
14760  ni_log2(p_ctx,
14761  NI_LOG_DEBUG,
14762  "p_data3_2:sei_size=%d device_handle=%d == hw_id=%d ses_id=%d\n",
14763  sei_size, p_data3_2->device_handle, p_ctx->hw_id,
14764  p_data3_2->ui16session_ID);
14765  ni_log2(p_ctx, NI_LOG_DEBUG,
14766  "p_data3_2: ui16FrameIdx=%d NodeAddre=0x%x planar=%d bd=%d\n",
14767  p_data3_2->ui16FrameIdx, p_data3_2->ui32nodeAddress,
14768  p_data3_2->encoding_type, p_data3_2->bit_depth);
14769 
14770  ni_log2(p_ctx, NI_LOG_DEBUG,
14771  "%s:sei_size=%d device_handle=%d == hw_id=%d "
14772  "ses_id=%d\n",
14773  __func__, sei_size, p_data3->device_handle, p_ctx->hw_id,
14774  p_data3->ui16session_ID);
14775  ni_log2(p_ctx, NI_LOG_DEBUG,
14776  "%s: session=0x%x ui16FrameIdx=%u NodeAddress=0x%x, "
14777  "planar=%d bd=%d\n",
14778  __func__, p_ctx->session_id, p_data3->ui16FrameIdx, p_data3->ui32nodeAddress,
14779  p_data3->encoding_type, p_data3->bit_depth);
14780  } else if (ni_cmp_fw_api_ver(
14782  "6rE") >= 0)
14783  {
14784  p_meta =
14785  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer);
14786  ni_log2(p_ctx, NI_LOG_DEBUG, "Got pkt_delay_cnt %u\n",
14787  p_meta->metadata_common.pkt_delay_cnt);
14788  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
14789  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
14790  }
14791 
14792  total_bytes_to_read = total_bytes_to_read + sei_size;
14793  ni_log2(p_ctx, NI_LOG_DEBUG,
14794  "%s decoder read desc success, retval %d "
14795  "total_bytes_to_read include sei %u sei_size %d\n",
14796  __func__, retval, total_bytes_to_read, sei_size);
14797 
14798  if (total_bytes_to_read > NI_MEM_PAGE_ALIGNMENT)
14799  {
14800  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Oversized metadata!\n", __func__);
14801  retval = NI_RETCODE_ERROR_MEM_ALOC;
14802  LRETURN;
14803  }
14804  }
14805 
14806  //bytes_read_so_far = total_bytes_to_read;
14807  // Note: session status is NOT reset but tracked between send
14808  // and recv to catch and recover from a loop condition
14809  //total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2] + p_frame->data_len[3] + metadata_hdr_size + sei_size; //since only HW desc
14810  bytes_read_so_far = total_bytes_to_read;
14811  //bytes_read_so_far = p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2] + p_frame->data_len[3] + metadata_hdr_size + sei_size; //since only HW desc
14812  rx_size = ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, true);
14813  p_ctx->frame_pkt_offset = frame_offset;
14814  if (p_ctx->decoder_low_delay > 0 && buf_info.buf_avail_size == metadata_hdr_size &&
14815  p_ctx->enable_low_delay_check)
14816  {
14817  ni_log2(p_ctx, NI_LOG_TRACE, "Low delay mode amd check header if has b frame\n");
14818 
14819  ni_metadata_dec_frame_t *p_meta =
14820  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
14821  p_frame->data_len[0] +
14822  p_frame->data_len[1] +
14823  p_frame->data_len[2]);
14824  if (p_meta->metadata_common.has_b_frame == 1)
14825  {
14826  ni_log2(p_ctx, NI_LOG_ERROR,"Warning: session 0x%x decoder lowDelay mode "
14827  "is cancelled due to has_b_frames, frame_num %u\n",
14828  p_ctx->session_id, p_ctx->frame_num);
14829  p_ctx->decoder_low_delay = 0;
14830  }
14831  }
14832 
14833  if (rx_size > 0)
14834  {
14835  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): s-state %d first_frame %d\n", __func__,
14836  p_ctx->session_run_state, p_ctx->is_first_frame);
14837 
14838  int64_t tmp_dts, prev_dts = INT64_MIN, ts_diff = 0;
14839  int nb_diff = 0;
14840  if (p_ctx->is_first_frame)
14841  {
14842  ni_log2(p_ctx, NI_LOG_DEBUG,
14843  "%s(): First frame : session_id 0x%x, pic_reorder_delay: %d "
14844  "total frames input:%u buffered: %u completed: %u output: %u "
14845  "dropped: %u error: %u\n",
14846  __func__, p_ctx->session_id, p_ctx->pic_reorder_delay,
14853 
14854  if (p_ctx->session_statistic.ui32FramesDropped > 0)
14855  {
14856  for (i = 0; i < p_ctx->session_statistic.ui32FramesDropped; i++)
14857  {
14859  p_ctx->dts_queue, 0, &tmp_dts,
14861  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
14862  {
14863  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): FramesDropped pop "
14864  "decoder dts queue %d %ld failed !\n",
14865  __func__, i, tmp_dts);
14866  break;
14867  } else
14868  {
14869  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop "
14870  "decoder dts queue %d %ld success !\n",
14871  __func__, i, tmp_dts);
14872  if (prev_dts != INT64_MIN) {
14873  ts_diff += labs(tmp_dts - prev_dts);
14874  nb_diff++;
14875  }
14876  prev_dts = tmp_dts;
14877  }
14878  }
14879  if (nb_diff > 1)
14880  {
14881  ts_diff = ts_diff / nb_diff;
14882  }
14884  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop dts "
14885  "average diff: %ld\n", __func__, ts_diff);
14886  }
14887  }
14888  else
14889  {
14891  {
14892  for(i = p_ctx->decoder_last_drop_frame_num; i < p_ctx->session_statistic.ui32FramesDropped; i++)
14893  {
14895  p_ctx->dts_queue, 0, &tmp_dts,
14897  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
14898  {
14899  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): FramesDropped pop "
14900  "decoder dts queue %d %ld failed !\n",
14901  __func__, i, tmp_dts);
14902  break;
14903  }
14904  }
14906  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop dts %d\n", __func__, p_ctx->decoder_last_drop_frame_num);
14907  }
14908  }
14909 
14911  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
14913  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
14914  {
14915  if (p_ctx->last_dts != NI_NOPTS_VALUE && !p_ctx->ready_to_close)
14916  {
14917  p_ctx->pic_reorder_delay++;
14918  p_frame->dts = p_ctx->last_dts + p_ctx->last_dts_interval;
14919  ni_log2(p_ctx, NI_LOG_DEBUG, "Padding DTS: %" PRId64 "\n", p_frame->dts);
14920  } else
14921  {
14922  p_frame->dts = NI_NOPTS_VALUE;
14923  }
14924  }
14925 
14926  if (p_ctx->is_first_frame)
14927  {
14928  for (i = 0; i < p_ctx->pic_reorder_delay; i++)
14929  {
14930  if (p_ctx->last_pts == NI_NOPTS_VALUE &&
14931  p_ctx->last_dts == NI_NOPTS_VALUE)
14932  {
14933  // If the p_frame->pts is unknown in the very beginning we assume
14934  // p_frame->pts == 0 as well as DTS less than PTS by 1000 * 1/timebase
14935  if (p_frame->pts >= p_frame->dts &&
14936  p_frame->pts - p_frame->dts < 1000)
14937  {
14938  break;
14939  }
14940  }
14941 
14943  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
14945  p_ctx->frame_num % 500 == 0,
14946  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
14947  {
14948  p_frame->dts = NI_NOPTS_VALUE;
14949  } else
14950  {
14951  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): pic_reorder_delay pop "
14952  "decoder dts queue %d %ld success !\n",
14953  __func__, i, p_frame->dts);
14954  }
14955  }
14956  // Reset for DTS padding counting
14957  p_ctx->pic_reorder_delay = 0;
14958  }
14959  if (p_ctx->codec_format == NI_CODEC_FORMAT_JPEG)//fw won't save frameoffset when decoding jpeg.
14960  {
14961  if (p_ctx->is_first_frame)
14962  {
14963  p_ctx->is_first_frame = 0;
14964  }
14965  p_frame->pts = p_ctx->pts_offsets[p_ctx->frame_num % NI_FIFO_SZ];
14966  p_frame->flags = p_ctx->flags_array[p_ctx->frame_num % NI_FIFO_SZ];
14967  p_frame->pkt_pos = p_ctx->pkt_pos[p_ctx->frame_num % NI_FIFO_SZ];
14968  ni_log2(p_ctx, NI_LOG_DEBUG, "p_frame->pts = %u, frame_num = %d, p_frame->dts = %u\n",
14969  p_frame->pts, p_ctx->frame_num, p_frame->dts);
14970  }
14971  else
14972  {
14973  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: frame_offset %" PRIu64 "\n", __func__,
14974  frame_offset);
14975 
14976  // search for the pkt_offsets of received frame according to frame_offset.
14977  // here we get the index(i) which promises (p_ctx->pkt_offsets_index_min[i] <= frame_offset && p_ctx->pkt_offsets_index[i] > frame_offset)
14978  // i = -1 if not found
14979  i = rotated_array_binary_search(p_ctx->pkt_offsets_index_min,
14980  p_ctx->pkt_offsets_index, NI_FIFO_SZ,
14981  frame_offset);
14982  if (i >= 0)
14983  {
14984  p_frame->pts = p_ctx->pts_offsets[i];
14985  p_frame->flags = p_ctx->flags_array[i];
14986  p_frame->pkt_pos = p_ctx->pkt_pos[i];
14987  ni_log2(p_ctx, NI_LOG_DEBUG,
14988  "%s: (found pts) dts %" PRId64 " pts "
14989  "%" PRId64 " frame_offset %" PRIu64 " i %d "
14990  "pkt_offsets_index_min %" PRIu64 " "
14991  "pkt_offsets_index %" PRIu64 " pkt_pos %" PRIu64 "\n",
14992  __func__, p_frame->dts, p_frame->pts, frame_offset, i,
14993  p_ctx->pkt_offsets_index_min[i],
14994  p_ctx->pkt_offsets_index[i],
14995  p_ctx->pkt_pos[i]);
14996 
14997  if (p_ctx->is_first_frame)
14998  {
14999  // if the first frame is I frame and there are dropped frames,
15000  // find the dts closest to its pts using the average of dts diff
15001  if (p_frame->dts != NI_NOPTS_VALUE &&
15002  p_frame->pts != NI_NOPTS_VALUE &&
15003  PIC_TYPE_I == p_frame->ni_pict_type &&
15004  p_ctx->session_statistic.ui32FramesDropped > 0 &&
15005  ts_diff > 0)
15006  {
15007  while (p_frame->dts < p_frame->pts &&
15008  labs(p_frame->pts - p_frame->dts) > ts_diff)
15009  {
15010  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: First I frame pts %ld "
15011  "dts %ld diff. %ld > ts_diff %ld\n",
15012  __func__, p_frame->pts, p_frame->dts,
15013  labs(p_frame->pts - p_frame->dts), ts_diff);
15014 
15016  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
15018  {
15019  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): First I frame "
15020  "pop decoder dts queue error.\n", __func__);
15021  break;
15022  }
15023  }
15024  }
15025  }
15026 
15027  p_frame->p_custom_sei_set = p_ctx->pkt_custom_sei_set[i];
15028  p_ctx->pkt_custom_sei_set[i] = NULL;
15029  } else
15030  {
15031  //backup solution pts
15032  if (p_param->dec_input_params.skip_pts_guess && p_ctx->last_pts != NI_NOPTS_VALUE)
15033  {
15034  // if skip guess_correct_pts, use pts interval to get the correct pts
15035  p_frame->pts = p_ctx->last_pts + (p_ctx->last_pts_interval > 0 ? p_ctx->last_pts_interval : 1);
15036  }
15037  else
15038  {
15039  p_frame->pts = p_ctx->last_pts + (p_frame->dts - p_ctx->last_dts);
15040  }
15041  p_frame->pkt_pos = p_ctx->last_pkt_pos + (frame_offset - p_ctx->last_frame_offset);
15042  ni_log2(p_ctx, NI_LOG_ERROR,
15043  "ERROR: Frame pts %" PRId64 " not found for offset "
15044  "%" PRIu64 "\n", p_frame->pts, frame_offset);
15045  ni_log2(p_ctx, NI_LOG_DEBUG,
15046  "%s: (not found use default) dts %" PRId64 " pts %" PRId64
15047  "\n",
15048  __func__, p_frame->dts, p_frame->pts);
15049  }
15050 
15051  if (p_ctx->is_first_frame)
15052  {
15053  p_ctx->is_first_frame = 0;
15054  }
15055  }
15056 
15057  p_frame->orignal_pts = p_frame->pts;
15058  p_ctx->last_pkt_pos = p_frame->pkt_pos;
15059  p_ctx->last_frame_offset = frame_offset;
15060  if (!p_param->dec_input_params.skip_pts_guess)
15061  p_frame->pts = guess_correct_pts(p_ctx, p_frame->pts, p_frame->dts);
15062  if (p_frame->pts != NI_NOPTS_VALUE && p_ctx->last_pts != NI_NOPTS_VALUE)
15063  p_ctx->last_pts_interval = p_frame->pts - p_ctx->last_pts;
15064  p_ctx->last_pts = p_frame->pts;
15065  if (p_frame->dts != NI_NOPTS_VALUE && p_ctx->last_dts != NI_NOPTS_VALUE)
15066  p_ctx->last_dts_interval = p_frame->dts - p_ctx->last_dts;
15067  p_ctx->last_dts = p_frame->dts;
15068  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (best_effort_timestamp) pts %" PRId64 "\n",
15069  __func__, p_frame->pts);
15070  p_ctx->frame_num++;
15071 
15073  if (p_frame->error_ratio > 0)
15074  {
15075  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: frame number = %d, error_ratio = %u\n",
15076  __func__, p_ctx->frame_num, p_frame->error_ratio);
15077  }
15078 
15079 #ifdef MEASURE_LATENCY
15080 #ifndef XCODER_311
15081  ni_log2(p_ctx, NI_LOG_INFO, "DEC pkt_num %d, fme_num %d, latecy is %d\n",
15082  p_ctx->pkt_num, p_ctx->frame_num, p_ctx->pkt_num - p_ctx->frame_num);
15083 #endif
15084 #endif
15085 
15086  }
15087 
15088  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): received data: [0x%08x]\n", __func__, rx_size);
15089  ni_log2(p_ctx, NI_LOG_DEBUG,
15090  "%s(): p_frame->start_of_stream=%u, "
15091  "p_frame->end_of_stream=%u, p_frame->video_width=%u, "
15092  "p_frame->video_height=%u\n",
15093  __func__, p_frame->start_of_stream, p_frame->end_of_stream,
15094  p_frame->video_width, p_frame->video_height);
15095  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): p_frame->data_len[0/1/2]=%u/%u/%u\n", __func__,
15096  p_frame->data_len[0], p_frame->data_len[1], p_frame->data_len[2]);
15097 
15098  if (p_ctx->frame_num % 500 == 0)
15099  {
15100  ni_log2(p_ctx, NI_LOG_DEBUG,
15101  "Decoder pts queue size = %d dts queue size = %d\n\n",
15102  p_ctx->pts_table->list.count, p_ctx->dts_queue->list.count);
15103  // scan and clean up
15105  p_ctx->buffer_pool);
15106  }
15107 
15108 #ifdef MEASURE_LATENCY
15109  if ((p_frame->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
15110  {
15111  uint64_t abs_time_ns = ni_gettime_ns();
15113  ni_log2(p_ctx, NI_LOG_INFO, "DTS:%" PRId64 ",DELTA:%" PRId64 ",dLAT:%" PRIu64 ";\n",
15114  p_frame->dts, abs_time_ns - q->last_benchmark_time,
15115  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->dts));
15116  q->last_benchmark_time = abs_time_ns;
15117  }
15118 #endif
15119 
15120 END:
15121 
15122  ni_pthread_mutex_unlock(&p_ctx->mutex);
15123 
15124  if (get_first_metadata && p_data_buffer)
15125  ni_aligned_free(p_data_buffer);
15126  if (sequence_change && p_ctx->frame_num)
15127  {
15128  if (p_ctx->actual_video_width == p_frame->video_width &&
15129  p_ctx->active_video_height == p_frame->video_height)
15130  {
15131  p_ctx->pixel_format_changed = 1;
15132  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): format changed\n", __func__);
15133  }
15134  }
15135  if (NI_RETCODE_SUCCESS != retval)
15136  {
15137  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): bad exit, retval = %d\n", __func__, retval);
15138  if (retval == NI_RETCODE_ERROR_VPU_RECOVERY)
15139  {
15140  low_delay_signal(p_ctx);
15141  }
15142 
15143  return retval;
15144  } else
15145  {
15146  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
15147  if (low_delay_notify)
15148  {
15149  low_delay_signal(p_ctx);
15150  }
15151 
15152  return rx_size;
15153  }
15154 }
15155 
15156 /*!******************************************************************************
15157 * \brief Retrieve a YUV p_frame from decoder
15158 *
15159 * \param
15160 *
15161 * \return
15162 *******************************************************************************/
15164 {
15165  int retval = NI_RETCODE_SUCCESS;
15166  int rx_size = 0;
15167  uint64_t frame_offset = 0;
15168  uint8_t *p_data_buffer;
15169  int metadata_hdr_size = NI_FW_META_DATA_SZ -
15171  uint32_t total_bytes_to_read = 0;
15172  uint32_t read_size_bytes = 0;
15173  uint32_t ui32LBA = 0;
15174 
15175  //ni_log2(p_ctx, NI_LOG_DEBUG, "hwcontext.c:ni_hwdl_frame() hwdesc %d %d %d\n",
15176  // hwdesc->ui16FrameIdx,
15177  // hwdesc->i8InstID,
15178  // hwdesc->ui16session_ID);
15179 
15180  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15181 
15182  if ((!p_ctx) || (!p_frame))
15183  {
15184  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
15185  __func__);
15186  return NI_RETCODE_INVALID_PARAM;
15187  }
15188 
15189  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15190  {
15191  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15192  __func__);
15194  LRETURN;
15195  }
15196 
15197  p_data_buffer = (uint8_t *)p_frame->p_buffer;
15198 
15199  if (!p_frame->p_data[0] || !p_data_buffer)
15200  {
15201  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): No receive buffer allocated.\n",
15202  __func__);
15203  retval = NI_RETCODE_INVALID_PARAM;
15204  LRETURN;
15205  }
15206 
15207  if (0 == p_frame->data_len[0])
15208  {
15209  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_frame->data_len[0] = 0!.\n",
15210  __func__);
15211  retval = NI_RETCODE_INVALID_PARAM;
15212  LRETURN;
15213  }
15214 
15216  {
15217  p_frame->data_len[2] = 0;
15218  } else if (hwdesc->encoding_type == NI_PIXEL_PLANAR_FORMAT_TILED4X4)
15219  {
15220  ni_log2(p_ctx, NI_LOG_ERROR,
15221  "ERROR %s(): NI_PIXEL_PLANAR_FORMAT_TILED4X4 not supported in download.\n",
15222  __func__);
15223  retval = NI_RETCODE_INVALID_PARAM;
15224  LRETURN;
15225  }
15226  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
15227  p_frame->data_len[2];// +metadata_hdr_size;
15228  unsigned int bytes_read_so_far = 0;
15229  uint32_t output_chunk_offset = hwdesc->ui32nodeAddress / FRAME_CHUNK_INDEX_SIZE; //for reading output1 or output2
15230  uint32_t output_minor_offset = hwdesc->ui32nodeAddress - output_chunk_offset * FRAME_CHUNK_INDEX_SIZE;
15231  ni_log2(p_ctx, NI_LOG_DEBUG, "Total bytes to download %u, start offset = %u, chunkOffset "
15232  "%u, minorOffset %u\n",
15233  total_bytes_to_read, hwdesc->ui32nodeAddress,
15234  output_chunk_offset, output_minor_offset);
15235 
15236  ni_log2(p_ctx, NI_LOG_DEBUG, "total_bytes_to_read %u max_nvme_io_size %u ylen %u cr len "
15237  "%u cb len %u hdr %d\n",
15238  total_bytes_to_read, p_ctx->max_nvme_io_size,
15239  p_frame->data_len[0], p_frame->data_len[1],
15240  p_frame->data_len[2], metadata_hdr_size);
15241 
15242  //Apply read configuration here
15243  retval =
15245  (output_minor_offset << NI_CODEC_HW_PAYLOAD_OFFSET) |
15247  hwdesc->ui16FrameIdx);
15248  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, hwdesc->src_cpu,
15249  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15250  CHECK_VPU_RECOVERY(retval);
15251  if (retval < 0)
15252  {
15253  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15255  LRETURN;
15256  } else
15257  {
15258  ni_log2(p_ctx, NI_LOG_DEBUG,
15259  "Config HW download read desc success, retval %d total_bytes_to_read %u\n",
15260  retval, total_bytes_to_read);
15261  }
15262 
15263  read_size_bytes = total_bytes_to_read;
15264  ui32LBA = READ_INSTANCE_R(p_ctx->session_id, hwdesc->src_cpu);
15265  ui32LBA += output_chunk_offset;
15266  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
15267  {
15268  read_size_bytes = ( (read_size_bytes / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
15269  }
15270 
15271  retval = ni_nvme_send_read_cmd(
15272  (ni_device_handle_t)(int64_t)hwdesc->device_handle,
15273  NI_INVALID_DEVICE_HANDLE, p_data_buffer, read_size_bytes, ui32LBA);
15274  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, hwdesc->src_cpu,
15275  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15276  CHECK_VPU_RECOVERY(retval);
15277  if (retval < 0)
15278  {
15279  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15281  LRETURN;
15282  } else
15283  {
15284  ni_log2(p_ctx, NI_LOG_DEBUG,
15285  "HW download read desc success, retval %d total_bytes_to_read %u\n",
15286  retval, total_bytes_to_read);
15287  }
15288 
15289  //Unset applied read configuration here
15290  retval = ni_config_session_rw(p_ctx, SESSION_READ_CONFIG, 0, 0, 0);
15291  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, hwdesc->src_cpu,
15292  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15293  CHECK_VPU_RECOVERY(retval);
15294 
15295  if (retval < 0)
15296  {
15297  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15299  LRETURN;
15300  } else
15301  {
15302  ni_log2(p_ctx, NI_LOG_DEBUG,
15303  "Unconfig HW download read desc success, retval %d total_bytes_to_read %u\n",
15304  retval, total_bytes_to_read);
15305  }
15306 
15307  bytes_read_so_far = total_bytes_to_read;
15308  // Note: session status is NOT reset but tracked between send
15309  // and recv to catch and recover from a loop condition
15310 
15311  if (p_ctx->is_auto_dl)
15312  rx_size = (int)bytes_read_so_far;
15313  else
15314  {
15315  rx_size =
15316  ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, false);
15317  p_ctx->frame_pkt_offset = frame_offset;
15318  }
15319 
15320  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): received data: [0x%08x]\n",
15321  __func__, rx_size);
15322  ni_log2(p_ctx, NI_LOG_DEBUG,
15323  "%s(): p_frame->start_of_stream=%u, "
15324  "p_frame->end_of_stream=%u, p_frame->video_width=%u, "
15325  "p_frame->video_height=%u\n",
15326  __func__, p_frame->start_of_stream, p_frame->end_of_stream,
15327  p_frame->video_width, p_frame->video_height);
15328  ni_log2(p_ctx, NI_LOG_DEBUG,
15329  "%s(): p_ctx->frame_num %" PRIu64 ", "
15330  "p_frame->data_len[0/1/2]=%u/%u/%u\n",
15331  __func__, p_ctx->frame_num, p_frame->data_len[0], p_frame->data_len[1],
15332  p_frame->data_len[2]);
15333 
15334  //if (decq_count % 500 == 0)
15335  //{
15336  // ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder pts queue size = %d dts queue size = %d\n\n",
15337  // p_ctx->pts_table->list.count, p_ctx->dts_queue)->list.count);
15338  // // scan and clean up
15339  // ni_timestamp_scan_cleanup(p_ctx->pts_table, p_ctx->dts_queue, p_ctx->buffer_pool);
15340  //}
15341 
15342 END:
15343 
15344  if (NI_RETCODE_SUCCESS != retval)
15345  {
15346  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
15347  return retval;
15348  } else
15349  {
15350  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
15351  return rx_size;
15352  }
15353 }
15354 
15355 int ni_hwdownload_by_frame_idx(niFrameSurface1_t* hwdesc, ni_frame_t* p_frame, int is_auto_dl)
15356 {
15357  int retval = NI_RETCODE_SUCCESS;
15358  int rx_size = 0;
15359  uint64_t frame_offset = 0;
15360  uint8_t *p_data_buffer;
15361  int metadata_hdr_size = NI_FW_META_DATA_SZ -
15363  uint32_t total_bytes_to_read = 0;
15364  uint32_t read_size_bytes = 0;
15365  uint32_t ui32LBA = 0;
15366  ni_global_session_stats_t *global_state;
15367  const char *error_flag = "NetintQuadraErr";
15368 
15369  ni_log(NI_LOG_TRACE, "%s(): enter\n", __func__);
15370 
15371  p_data_buffer = (uint8_t *)p_frame->p_buffer;
15372 
15373  if (!p_frame->p_data[0] || !p_data_buffer || 0 == p_frame->data_len[0])
15374  {
15375  ni_log(NI_LOG_ERROR, "ERROR %s(): passed parameters are null or p_frame->data_len is 0.\n",
15376  __func__);
15377  retval = NI_RETCODE_INVALID_PARAM;
15378  LRETURN;
15379  }
15380 
15382  {
15383  p_frame->data_len[2] = 0;
15384  } else if (hwdesc->encoding_type >= NI_PIXEL_PLANAR_FORMAT_TILED4X4)
15385  {
15387  "ERROR %s(): Unsupported format %d for hw download.\n",
15388  __func__, hwdesc->encoding_type);
15389  retval = NI_RETCODE_INVALID_PARAM;
15390  LRETURN;
15391  }
15392  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
15393  p_frame->data_len[2];// +metadata_hdr_size;
15394  unsigned int bytes_read_so_far = 0;
15395  uint32_t output_chunk_offset = hwdesc->ui32nodeAddress / FRAME_CHUNK_INDEX_SIZE; //for reading output1 or output2
15396  uint32_t output_minor_offset = hwdesc->ui32nodeAddress - output_chunk_offset * FRAME_CHUNK_INDEX_SIZE;
15397 
15398  ni_log(NI_LOG_DEBUG, "total_bytes_to_read %u ylen %u cr len "
15399  "%u cb len %u hdr %d\n",
15400  total_bytes_to_read,
15401  p_frame->data_len[0], p_frame->data_len[1],
15402  p_frame->data_len[2], metadata_hdr_size);
15403 
15404  // preset content of buffer, if not refresh means download fail
15405  memset(p_data_buffer, 0, NI_DATA_BUFFER_LEN);
15406  global_state = (ni_global_session_stats_t *)p_data_buffer;
15407  memcpy(global_state->error_flag, error_flag, strlen(error_flag)+1);
15408  global_state->check_flag[0] = global_state->check_flag[2] = 0x0;
15409  global_state->check_flag[1] = global_state->check_flag[3] = 0xFF;
15410 
15411  read_size_bytes = total_bytes_to_read;
15412  ui32LBA = DOWNLOAD_FRAMEIDX_R(hwdesc->ui16FrameIdx);
15413  ui32LBA += output_chunk_offset;
15414  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
15415  {
15416  read_size_bytes = ( (read_size_bytes / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
15417  }
15418  ni_log(NI_LOG_DEBUG, "Total bytes to download %u, start offset = %u, chunkOffset "
15419  "%u, minorOffset %u hwdesc->ui16FrameIdx %d ui32LBA 0x%x\n",
15420  total_bytes_to_read, hwdesc->ui32nodeAddress,
15421  output_chunk_offset, output_minor_offset, hwdesc->ui16FrameIdx, ui32LBA);
15422 
15423  retval = ni_nvme_send_read_cmd(
15424  (ni_device_handle_t)(int64_t)hwdesc->device_handle,
15425  NI_INVALID_DEVICE_HANDLE, p_data_buffer, read_size_bytes, ui32LBA);
15426 
15427  if (retval < 0)
15428  {
15429  ni_log(NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15431  LRETURN;
15432  }
15433  global_state = (ni_global_session_stats_t *)p_data_buffer;
15434  if (!strcmp(global_state->error_flag, error_flag) &&
15435  global_state->check_flag[0] == 0x0 && global_state->check_flag[1] == 0xFF &&
15436  global_state->check_flag[2] == 0x0 && global_state->check_flag[3] == 0xFF) {
15437  ni_log(NI_LOG_ERROR, "ERROR %s(): nvme download failed, invalid frameidx %u or "
15438  "size %u + offset %u out of range\n",
15439  __func__, hwdesc->ui16FrameIdx, read_size_bytes, output_chunk_offset);
15440  retval = NI_RETCODE_INVALID_PARAM;
15441  LRETURN;
15442  }
15443 
15444  bytes_read_so_far = total_bytes_to_read;
15445  // Note: session status is NOT reset but tracked between send
15446  // and recv to catch and recover from a loop condition
15447 
15448  if (is_auto_dl)
15449  rx_size = (int)bytes_read_so_far;
15450  else
15451  {
15452  rx_size =
15453  ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, false);
15454  }
15455 
15457  "HW download read desc success, retval %d total_bytes_to_read %u\n",
15458  retval, total_bytes_to_read);
15459 
15460 END:
15461 
15462  if (NI_RETCODE_SUCCESS != retval)
15463  {
15464  ni_log(NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
15465  return retval;
15466  } else
15467  {
15468  ni_log(NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
15469  return rx_size;
15470  }
15471 }
15472 
15474  ni_frameclone_desc_t *p_frameclone_desc)
15475 {
15476  int retval = NI_RETCODE_SUCCESS;
15477  uint8_t *p_data = NULL;
15478  uint32_t dataLen;
15479  uint32_t ui32LBA = 0;
15480 
15481  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15482 
15483  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15484  {
15485  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15486  __func__);
15488  LRETURN;
15489  }
15490 
15491  dataLen = (sizeof(ni_frameclone_desc_t) + NI_MEM_PAGE_ALIGNMENT - 1) &
15492  ~(NI_MEM_PAGE_ALIGNMENT - 1);
15493  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
15494  {
15495  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
15496  NI_ERRNO, __func__);
15497  retval = NI_RETCODE_ERROR_MEM_ALOC;
15498  LRETURN;
15499  }
15500  memset(p_data, 0, dataLen);
15501  memcpy(p_data, p_frameclone_desc, sizeof(ni_frameclone_desc_t));
15502  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): ui16DstIdx %u, ui16SrcIdx %u, size %u, offset %u\n", __func__,
15503  p_frameclone_desc->ui16DstIdx, p_frameclone_desc->ui16SrcIdx,
15504  p_frameclone_desc->ui32Size, p_frameclone_desc->ui32Offset);
15505 
15506  ui32LBA = CONFIG_SESSION_FRAME_COPY_W(p_ctx->session_id);
15507  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, NI_INVALID_EVENT_HANDLE,
15508  p_data, dataLen, ui32LBA);
15509  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
15510  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
15511  OPT_1);
15512  CHECK_VPU_RECOVERY(retval);
15513  if (NI_RETCODE_SUCCESS != retval)
15514  {
15515  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n",
15516  __func__);
15518  LRETURN;
15519  }
15520 
15521 END:
15522  ni_aligned_free(p_data);
15523  if (NI_RETCODE_SUCCESS != retval)
15524  {
15525  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
15526  }
15527  return retval;
15528 }
15529 
15530 /*!******************************************************************************
15531 * \brief Close an xcoder upload instance
15532 *
15533 * \param p_ctx pointer to uploader session context
15534 *
15535 * \return NI_RETCODE_SUCCESS
15536 *******************************************************************************/
15538 {
15539 #ifndef _WIN32
15540  if (p_ctx->isP2P)
15541  {
15542  if (p_ctx->netint_fd)
15543  {
15544  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: close driver fd %d\n", __func__,
15545  p_ctx->netint_fd);
15546  close(p_ctx->netint_fd);
15547  }
15548  }
15549 #endif
15550  return NI_RETCODE_SUCCESS;
15551 }
15552 
15553 /*!******************************************************************************
15554 * \brief Send a p_config command to configure uploading parameters.
15555 *
15556 * \param ni_session_context_t p_ctx - xcoder Context
15557 * \param[in] pool_size pool size to create
15558 * \param[in] pool 0 = normal pool, 1 = P2P pool
15559 *
15560 * \return - NI_RETCODE_SUCCESS on success,
15561 * NI_RETCODE_ERROR_INVALID_SESSION
15562 * NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
15563 *******************************************************************************/
15565  uint32_t pool_size,
15566  uint32_t pool)
15567 {
15568  void* p_uploader_config = NULL;
15569  ni_uploader_config_t* p_cfg = NULL;
15570  uint32_t buffer_size = sizeof(ni_encoder_config_t);
15572  uint32_t ui32LBA = 0;
15573  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15574 
15575  if (!p_ctx)
15576  {
15577  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
15578  __func__);
15579  retval = NI_RETCODE_INVALID_PARAM;
15580  LRETURN;
15581  }
15582 
15583  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15584  {
15585  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15586  __func__);
15588  LRETURN;
15589  }
15590 
15591  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
15592  if (ni_posix_memalign(&p_uploader_config, sysconf(_SC_PAGESIZE),
15593  buffer_size))
15594  {
15595  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
15596  NI_ERRNO, __func__);
15597  retval = NI_RETCODE_ERROR_MEM_ALOC;
15598  LRETURN;
15599  }
15600  memset(p_uploader_config, 0, buffer_size);//
15601 
15602  p_cfg = (ni_uploader_config_t*)p_uploader_config;
15603  p_cfg->ui16picWidth = p_ctx->active_video_width;
15604  p_cfg->ui16picHeight = p_ctx->active_video_height;
15605  p_cfg->ui8poolSize = pool_size;
15606  p_cfg->ui8PixelFormat = p_ctx->pixel_format;
15607  p_cfg->ui8Pool = pool;
15608  p_cfg->ui32lumaLinesize = 0; // cannot support config linesize yet because ni_frames_init is called when filter graph is created (no frame linesize info)
15609  p_cfg->ui32chromaLinesize = 0;
15610  bool isrgb = ((NI_PIX_FMT_RGBA == p_cfg->ui8PixelFormat) || (NI_PIX_FMT_BGRA == p_cfg->ui8PixelFormat) ||
15611  (NI_PIX_FMT_ARGB == p_cfg->ui8PixelFormat) || (NI_PIX_FMT_ABGR == p_cfg->ui8PixelFormat) ||
15612  (NI_PIX_FMT_BGR0 == p_cfg->ui8PixelFormat));
15616  {
15617  ni_log2(p_ctx, NI_LOG_ERROR, "Resolution %d x %d not supported for %d format!\n", p_cfg->ui16picWidth, p_cfg->ui16picHeight, p_cfg->ui8PixelFormat);
15618  retval = NI_RETCODE_INVALID_PARAM;
15619  LRETURN;
15620  }
15621 
15622  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_config_instance_set_uploader_params():%d x %d x Format %d with %d framepool\n",
15623  p_cfg->ui16picWidth, p_cfg->ui16picHeight, p_cfg->ui8PixelFormat, p_cfg->ui8poolSize);
15624 
15625  //configure the session here
15626  ui32LBA =
15628 
15629  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
15630  p_uploader_config, buffer_size, ui32LBA);
15631  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
15632  p_ctx->device_type, p_ctx->hw_id,
15633  &(p_ctx->session_id), OPT_1);
15634  if (NI_RETCODE_SUCCESS != retval)
15635  {
15636  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
15637  //Close the session since we can't configure it
15638  retval = ni_encoder_session_close(p_ctx, 0);
15639  if (NI_RETCODE_SUCCESS != retval)
15640  {
15641  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_uploader_session_close failed: blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
15642  }
15643 
15645  }
15646  else
15647  {
15649  }
15650 
15651 END:
15652 
15653  ni_aligned_free(p_uploader_config);
15654  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
15655 
15656  return retval;
15657 }
15658 
15659 /*!******************************************************************************
15660 * \brief Send a p_config command to configure decoding parameters.
15661 *
15662 * \param ni_session_context_t p_ctx - xcoder Context
15663 * \param uint32_t max_pkt_size - overwrite maximum packet size if nonzero
15664 *
15665 * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
15666 *******************************************************************************/
15668 {
15669  void* p_decoder_config = NULL;
15670  uint32_t buffer_size = sizeof(ni_decoder_config_t);
15672  uint32_t ui32LBA = 0;
15673  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15674 
15675  if (!p_ctx)
15676  {
15677  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
15678  __func__);
15679  retval = NI_RETCODE_INVALID_PARAM;
15680  LRETURN;
15681  }
15682 
15683  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15684  {
15685  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15686  __func__);
15688  LRETURN;
15689  }
15690 
15691  if (ni_cmp_fw_api_ver((char*)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rT") < 0)
15692  {
15693  ni_log2(p_ctx, NI_LOG_INFO, "%s() FW rev %s < 6rT-- load balancing might be affected\n", __func__,
15695  }
15696 
15697  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
15698  if (ni_posix_memalign(&p_decoder_config, sysconf(_SC_PAGESIZE), buffer_size))
15699  {
15700  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate p_decoder_config buffer\n",
15701  NI_ERRNO, __func__);
15702  retval = NI_RETCODE_ERROR_MEM_ALOC;
15703  LRETURN;
15704  }
15705  memset(p_decoder_config, 0, buffer_size);
15706 
15707  ni_set_custom_dec_template(p_ctx, p_decoder_config, p_ctx->p_session_config, max_pkt_size);
15708  retval = ni_validate_custom_dec_template(p_ctx->p_session_config, p_ctx, p_decoder_config, p_ctx->param_err_msg, sizeof(p_ctx->param_err_msg));
15709  if (NI_RETCODE_PARAM_WARN == retval)
15710  {
15711  ni_log2(p_ctx, NI_LOG_INFO, "WARNING: %s . %s\n", __func__, p_ctx->param_err_msg);
15712  fflush(stdout);
15713  }
15714  else if (NI_RETCODE_SUCCESS != retval)
15715  {
15716  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s failed. %s\n", __func__,
15717  p_ctx->param_err_msg);
15718  fflush(stdout);
15719  retval = NI_RETCODE_INVALID_PARAM;
15720  LRETURN;
15721  }
15722 
15723  // configure the session here
15725 
15726  //Flip the bytes!!
15727  //p_cfg = (ni_decoder_config_t*)p_decoder_config;
15728  //p_cfg->i32picWidth = ni_htonl(p_cfg->i32picWidth);
15729  //p_cfg->i32picHeight = ni_htonl(p_cfg->i32picHeight);
15730  //p_cfg->i32meBlkMode = ni_htonl(p_cfg->i32meBlkMode);
15731  //p_cfg->i32frameRateInfo = ni_htonl(p_cfg->i32frameRateInfo);
15732  //p_cfg->i32vbvBufferSize = ni_htonl(p_cfg->i32vbvBufferSize);
15733  //p_cfg->i32userQpMax = ni_htonl(p_cfg->i32userQpMax);
15734  //p_cfg->i32maxIntraSize = ni_htonl(p_cfg->i32maxIntraSize);
15735  //p_cfg->i32userMaxDeltaQp = ni_htonl(p_cfg->i32userMaxDeltaQp);
15736  //p_cfg->i32userMinDeltaQp = ni_htonl(p_cfg->i32userMinDeltaQp);
15737  //p_cfg->i32userQpMin = ni_htonl(p_cfg->i32userQpMin);
15738  //p_cfg->i32bitRate = ni_htonl(p_cfg->i32bitRate);
15739  //p_cfg->i32bitRateBL = ni_htonl(p_cfg->i32bitRateBL);
15740  //p_cfg->i32srcBitDepth = ni_htonl(p_cfg->i32srcBitDepth);
15741  //p_cfg->hdrEnableVUI = ni_htonl(p_cfg->hdrEnableVUI);
15742  //p_cfg->ui32VuiDataSizeBits = ni_htonl(p_cfg->ui32VuiDataSizeBits);
15743  //p_cfg->ui32VuiDataSizeBytes = ni_htonl(p_cfg->ui32VuiDataSizeBytes);
15744  //p_cfg->i32hwframes = ni_htonl(p_cfg->i32hwframes);
15745  // flip the 16 bytes of the reserved field using 32 bits pointers
15746  //for (i = 0; i < (16 >> 2); i++)
15747  //{
15748  // ((uint32_t*)p_cfg->ui8Reserved)[i] = ni_htonl(((uint32_t*)p_cfg->ui8Reserved)[i]);
15749  //}
15751  //for (i = 0; i < (NI_MAX_VUI_SIZE >> 2); i++) // apply on 32 bits
15752  //{
15753  // ((uint32_t*)p_cfg->ui8VuiRbsp)[i] = ni_htonl(((uint32_t*)p_cfg->ui8VuiRbsp)[i]);
15754  //}
15755 
15756  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: ui32LBA = 0x%x\n", __func__, ui32LBA);
15757  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
15758  p_decoder_config, buffer_size, ui32LBA);
15759  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
15760  p_ctx->device_type, p_ctx->hw_id,
15761  &(p_ctx->session_id), OPT_1);
15762  if (NI_RETCODE_SUCCESS != retval)
15763  {
15764  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
15765  //Close the session since we can't configure it
15766  retval = ni_decoder_session_close(p_ctx, 0);
15767  if (NI_RETCODE_SUCCESS != retval)
15768  {
15769  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_encoder_session_close failed: blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
15770  }
15771 
15773  }
15774 
15775 END:
15776 
15777  ni_aligned_free(p_decoder_config);
15778  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
15779  return retval;
15780 }
15781 
15782 /*!******************************************************************************
15783  * \brief read a hardware descriptor from a scaler session
15784  *
15785  * \param[in] p_ctx pointer to session context
15786  * \param[out] p_frame pointer to frame to write hw descriptor
15787  *
15788  * \return NI_RETCODE_INVALID_PARAM
15789  * NI_RETCODE_ERROR_INVALID_SESSION
15790  * NI_RETCODE_ERROR_MEM_ALOC
15791  * NI_RETCODE_ERROR_NVME_CMD_FAILED
15792  * NI_RETCODE_FAILURE
15793  *******************************************************************************/
15795  ni_session_context_t *p_ctx,
15796  ni_frame_t *p_frame)
15797 {
15798  ni_retcode_t retval;
15799  ni_instance_buf_info_t sInstanceBuf = {0};
15800  niFrameSurface1_t *pFrameSurface;
15801  int query_retry = 0;
15802 
15803  if (!p_ctx || !p_frame || !p_frame->p_data[3])
15804  {
15805  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
15806  __func__);
15807  return NI_RETCODE_INVALID_PARAM;
15808  }
15809 
15810  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
15811  {
15812  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15813  __func__);
15815  }
15816 
15817  if (p_ctx->buffered_frame_index)
15818  {
15819  pFrameSurface = (niFrameSurface1_t *) p_frame->p_data[3];
15820  pFrameSurface->ui16FrameIdx = p_ctx->buffered_frame_index;
15821  pFrameSurface->ui16session_ID = p_ctx->session_id;
15822  pFrameSurface->device_handle =
15823  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
15824  pFrameSurface->src_cpu = (uint8_t) NI_DEVICE_TYPE_SCALER;
15825  pFrameSurface->output_idx = 0;
15826 
15827  p_ctx->buffered_frame_index = 0;
15828  return NI_RETCODE_SUCCESS;
15829  }
15830 
15831  ni_pthread_mutex_lock(&p_ctx->mutex);
15832 
15833  for (;;)
15834  {
15835  query_retry++;
15836 
15837  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: query by ni_query_instance_buf_info INST_BUF_INFO_RW_UPLOAD\n",
15838  __func__);
15840  NI_DEVICE_TYPE_SCALER, &sInstanceBuf);
15841 
15842  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
15843  p_ctx->device_type, p_ctx->hw_id,
15844  &(p_ctx->session_id), OPT_3);
15845 
15847  {
15848  if (query_retry >= 1000)
15849  {
15850  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning hwdesc read fail rc %d\n", retval);
15851  LRETURN;
15852  }
15853  }
15854  else if (retval != NI_RETCODE_SUCCESS)
15855  {
15856  LRETURN;
15857  }
15858  else
15859  {
15860  pFrameSurface = (niFrameSurface1_t *) p_frame->p_data[3];
15861  pFrameSurface->ui16FrameIdx = sInstanceBuf.hw_inst_ind.frame_index;
15862  pFrameSurface->ui16session_ID = p_ctx->session_id;
15863  pFrameSurface->device_handle =
15864  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
15865  pFrameSurface->src_cpu = (uint8_t) NI_DEVICE_TYPE_SCALER;
15866  pFrameSurface->output_idx = 0;
15867 
15868  /* A frame index of zero is invalid, the memory acquisition failed */
15869  if (pFrameSurface->ui16FrameIdx == 0)
15870  {
15871  if (query_retry >= 1000)
15872  {
15873  ni_log2(p_ctx, NI_LOG_ERROR, "Error: 2D could not acquire frame\n");
15874  retval = NI_RETCODE_FAILURE;
15875  LRETURN;
15876  }
15877  ni_usleep(100);
15878  continue;
15879  }
15880 
15881  ni_log2(p_ctx, NI_LOG_DEBUG,
15882  "Session=0x%x: %s got FrameIndex=%u\n",
15883  p_ctx->session_id,
15884  __func__,
15885  pFrameSurface->ui16FrameIdx);
15886 
15887  LRETURN;
15888  }
15889  }
15890 
15891 END:
15892 
15893  ni_pthread_mutex_unlock(&p_ctx->mutex);
15894 
15895  if (NI_RETCODE_SUCCESS != retval)
15896  {
15897  ni_log2(p_ctx, NI_LOG_DEBUG,
15898  "%s Warning scalar read hwdesc fail rc %d or ind !\n",
15899  __func__, retval);
15900  retval = NI_RETCODE_FAILURE;
15901  }
15902 
15903  return retval;
15904 }
15905 
15906 /*!******************************************************************************
15907 * \brief Grab bitdepth factor from NI_PIX_FMT
15908 *
15909 * \param[in] pix_fmt ni_pix_fmt_t
15910 *
15911 * \return 1 or 2 for success, -1 for error
15912 *******************************************************************************/
15914 {
15915  switch (pix_fmt)
15916  {
15917  case NI_PIX_FMT_YUV420P:
15918  case NI_PIX_FMT_NV12:
15919  case NI_PIX_FMT_8_TILED4X4:
15920  return 1;
15922  case NI_PIX_FMT_P010LE:
15924  return 2;
15925  default:
15926  {
15927  ni_log(NI_LOG_ERROR, "ERROR: %s() non applicable format %d\n", __func__,
15928  pix_fmt);
15929  break;
15930  }
15931  }
15932  return -1;
15933 }
15934 
15935 /*!******************************************************************************
15936 * \brief Grab planar info from NI_PIX_FMT
15937 *
15938 * \param[in] pix_fmt ni_pix_fmt_t
15939 *
15940 * \return 0 or 1 for success, -1 for error
15941 *******************************************************************************/
15943 {
15944  switch (pix_fmt)
15945  {
15946  case NI_PIX_FMT_YUV420P:
15948  case NI_PIX_FMT_ABGR:
15949  case NI_PIX_FMT_ARGB:
15950  case NI_PIX_FMT_RGBA:
15951  case NI_PIX_FMT_BGRA:
15952  return 1;
15953  break;
15954  case NI_PIX_FMT_NV12:
15955  case NI_PIX_FMT_P010LE:
15956  return 0;
15957  break;
15958  default:
15959  {
15960  ni_log(NI_LOG_ERROR, "ERROR: %s() non applicable format %d\n", __func__,
15961  pix_fmt);
15962  break;
15963  }
15964  }
15965  return -1;
15966 }
15967 
15968 #ifndef _WIN32
15969 /*!*****************************************************************************
15970  * \brief Get an address offset from a hw descriptor
15971  *
15972  * \param[in] p_ctx ni_session_context_t to be referenced
15973  * \param[in] hwdesc Pointer to caller allocated niFrameSurface1_t
15974  * \param[out] p_offset Value of offset
15975  *
15976  * \return On success NI_RETCODE_SUCCESS
15977  * On failure NI_RETCODE_INVALID_PARAM
15978  ******************************************************************************/
15980  uint32_t *p_offset)
15981 {
15982  if (!hwdesc)
15983  {
15984  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed null parameter\n", __func__);
15985  return NI_RETCODE_INVALID_PARAM;
15986  }
15987 
15988  if (hwdesc->ui16FrameIdx <= NI_GET_MIN_HWDESC_P2P_BUF_ID(p_ctx->ddr_config) ||
15990  {
15991  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() pass invalid data. "
15992  "FrameIdx %d OOR (%d,%d]. DDR config %d \n", __func__, hwdesc->ui16FrameIdx,
15994  p_ctx->ddr_config);
15995  return NI_RETCODE_INVALID_PARAM;
15996  }
15997 
15998  *p_offset = (hwdesc->ui16FrameIdx - NI_GET_MIN_HWDESC_P2P_BUF_ID(p_ctx->ddr_config)) *
16000 
16001  return NI_RETCODE_SUCCESS;
16002 }
16003 #endif
16004 
16005 /* AI functions */
16007  void *nb_data, uint32_t nb_size)
16008 {
16009  void *p_ai_config = NULL;
16010  void *p_nb_data = NULL;
16011  uint32_t buffer_size;
16012  // uint8_t *p_data;
16013  // uint32_t transferred, this_size;
16015  uint32_t ui32LBA = 0;
16016  uint32_t config_size;
16017  void *p_buffer = NULL;
16018  uint32_t dataLen;
16019  ni_instance_buf_info_t buf_info = {0};
16020  int retry_count = 0;
16021 
16022  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16023 
16024  if (!p_ctx)
16025  {
16026  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16027  __func__);
16028  return NI_RETCODE_INVALID_PARAM;
16029  }
16030 
16031  if (!nb_data || nb_size == 0) {
16032  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() invalid nb_data %p nb_size %u\n",
16033  __func__, nb_data, nb_size);
16034  return NI_RETCODE_INVALID_PARAM;
16035  }
16036 
16037  ni_pthread_mutex_lock(&p_ctx->mutex);
16038 
16039  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16040  {
16041  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16042  __func__);
16044  LRETURN;
16045  }
16046 
16047  config_size = (sizeof(ni_ai_config_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16048  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16049  if (ni_posix_memalign(&p_ai_config, sysconf(_SC_PAGESIZE), config_size))
16050  {
16051  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate ai config buffer.\n");
16052  retval = NI_RETCODE_ERROR_MEM_ALOC;
16053  LRETURN;
16054  }
16055 
16056  ((ni_ai_config_t *)p_ai_config)->ui32NetworkBinarySize = nb_size;
16057  ni_calculate_sha256(nb_data, nb_size,
16058  ((ni_ai_config_t *)p_ai_config)->ui8Sha256);
16059 
16060  buffer_size =
16061  (nb_size + (NI_MEM_PAGE_ALIGNMENT - 1)) & ~(NI_MEM_PAGE_ALIGNMENT - 1);
16062  if (ni_posix_memalign(&p_nb_data, sysconf(_SC_PAGESIZE), buffer_size))
16063  {
16064  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate encConf buffer.\n");
16065  retval = NI_RETCODE_ERROR_MEM_ALOC;
16066  LRETURN;
16067  }
16068 
16069  memcpy(p_nb_data, nb_data, nb_size);
16070 
16071  /* configure network binary size to be written */
16073  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): LBA 0x%x, nb_size %u\n", __func__, ui32LBA,
16074  ((ni_ai_config_t *)p_ai_config)->ui32NetworkBinarySize);
16075 
16076  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16077  p_ai_config, config_size, ui32LBA);
16078  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
16079  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16080  if (NI_RETCODE_SUCCESS != retval)
16081  {
16082  ni_log2(p_ctx, NI_LOG_ERROR,
16083  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
16084  ", hw_id, %u, xcoder_inst_id: %d\n",
16085  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
16086  //Close the session since we can't configure it
16087  retval = ni_ai_session_close(p_ctx, 0);
16088  if (NI_RETCODE_SUCCESS != retval)
16089  {
16090  ni_log2(p_ctx, NI_LOG_ERROR,
16091  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
16092  ", hw_id, %u, xcoder_inst_id: %d\n",
16093  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
16094  p_ctx->session_id);
16095  }
16096 
16098  LRETURN;
16099  }
16100 
16101  dataLen = (sizeof(ni_network_layer_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16102  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16103  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
16104  {
16105  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate buffer.\n");
16106  retval = NI_RETCODE_ERROR_MEM_ALOC;
16107  LRETURN;
16108  }
16109 
16110  for (;;)
16111  {
16112  /* test if the model is already exist. if not, then continue to write binary data */
16113  memset(p_buffer, 0, dataLen);
16115  "6J") >= 0)
16116  {
16117  ui32LBA = QUERY_INSTANCE_NL_SIZE_V2_R(p_ctx->session_id,
16119  } else
16120  {
16121  ui32LBA =
16123  }
16124  retval =
16126  p_buffer, dataLen, ui32LBA);
16127  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
16128  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16129  if (retval != NI_RETCODE_SUCCESS)
16130  {
16131  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
16133  LRETURN;
16134  }
16135  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size > 0)
16136  {
16137  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): network binary registered\n", __func__);
16138  LRETURN;
16139  }
16140 
16141  /* if this model is not ready, try to require nbuffer to write */
16143  NI_DEVICE_TYPE_AI, &buf_info);
16144  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
16145  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16146  if (retval != NI_RETCODE_SUCCESS)
16147  {
16148  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Query Write buffer Failed\n", __func__);
16149  LRETURN;
16150  }
16151  if (buf_info.buf_avail_size < nb_size)
16152  {
16153  /*
16154  * Cannot aquire the write access to nbuffer because someone else on the go,
16155  * Retry to see if model is ready.
16156  */
16157  ni_log2(p_ctx, NI_LOG_TRACE,
16158  "AI write query failed or buf_size %u < "
16159  "frame_size %u.\n", buf_info.buf_avail_size, nb_size);
16160  } else
16161  {
16162  /* Grant write access to nbuffer */
16163  ni_log2(p_ctx, NI_LOG_DEBUG,
16164  "Info ai write query success, available buf "
16165  "size %u >= frame size %u !\n",
16166  buf_info.buf_avail_size, nb_size);
16167  break;
16168  }
16169  retry_count++;
16170  /* Wait for at least 30s */
16172  {
16173  ni_pthread_mutex_unlock(&p_ctx->mutex);
16174  ni_usleep(100);
16175  ni_pthread_mutex_lock(&p_ctx->mutex);
16176  } else
16177  {
16178  ni_log2(p_ctx, NI_LOG_DEBUG, "AI network binary configuration polling timeout\n");
16180  LRETURN;
16181  }
16182  }
16183 
16184 #if 1
16185  /* write network binary data */
16186  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI);
16187  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): write nb LBA 0x%x\n", __func__, ui32LBA);
16188  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16189  p_nb_data, buffer_size, ui32LBA);
16190  ni_log2(p_ctx, NI_LOG_DEBUG, "write complete retval %d\n", retval);
16191  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
16192  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16193  if (NI_RETCODE_SUCCESS != retval)
16194  {
16195  ni_log2(p_ctx, NI_LOG_ERROR,
16196  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
16197  ", hw_id, %u, xcoder_inst_id: %d\n",
16198  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
16199  //Close the session since we can't configure it
16200  retval = ni_ai_session_close(p_ctx, 0);
16201  if (NI_RETCODE_SUCCESS != retval)
16202  {
16203  ni_log2(p_ctx, NI_LOG_ERROR,
16204  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
16205  ", hw_id, %u, xcoder_inst_id: %d\n",
16206  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
16207  p_ctx->session_id);
16208  }
16209 
16211  LRETURN;
16212  }
16213 
16214 #else
16215  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): write nb buffer_size %u\n", __func__,
16216  buffer_size);
16217  for (transferred = 0; transferred < buffer_size; transferred += this_size)
16218  {
16219  this_size = p_ctx->max_nvme_io_size < (buffer_size - transferred) ?
16220  p_ctx->max_nvme_io_size :
16221  (buffer_size - transferred);
16222 
16223  if (this_size & (4096 - 1))
16224  {
16225  this_size = (this_size + (4096 - 1)) & ~(4096 - 1);
16226  }
16227  /* write network binary data */
16228  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI) +
16229  (transferred >> 12);
16230  ni_log2(p_ctx, NI_LOG_DEBUG,
16231  "%s(): write nb LBA 0x%x, this_size %u, page_offset %u\n",
16232  __func__, ui32LBA, this_size, (transferred >> 12));
16233  p_data = (uint8_t *)p_nb_data + transferred;
16234  retval = ni_nvme_send_write_cmd(
16235  p_ctx->blk_io_handle, p_ctx->event_handle,
16236  (uint8_t *)p_nb_data + transferred, this_size, ui32LBA);
16237  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): write retval %d\n", __func__, retval);
16238  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
16239  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16240  if (NI_RETCODE_SUCCESS != retval)
16241  {
16242  ni_log(
16243  NI_LOG_ERROR,
16244  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
16245  ", hw_id, %u, xcoder_inst_id: %d\n",
16246  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
16247  //Close the session since we can't configure it
16248  retval = ni_ai_session_close(p_ctx, 0);
16249  if (NI_RETCODE_SUCCESS != retval)
16250  {
16251  ni_log(
16252  NI_LOG_ERROR,
16253  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
16254  ", hw_id, %u, xcoder_inst_id: %d\n",
16255  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
16256  p_ctx->session_id);
16257  }
16258 
16260  LRETURN;
16261  }
16262  }
16263 
16264 #endif
16265 
16266 END:
16267  ni_pthread_mutex_unlock(&p_ctx->mutex);
16268 
16269  ni_aligned_free(p_ai_config);
16270  ni_aligned_free(p_nb_data);
16271  ni_aligned_free(p_buffer);
16272 
16273  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16274 
16275  return retval;
16276 }
16277 
16279 {
16280  void *p_stream_info = NULL;
16281  void *p_ai_config = NULL;
16282  void *p_nb_data = NULL;
16284  uint32_t ui32LBA = 0;
16285  uint32_t config_size;
16286 
16287  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16288 
16289  if (!p_ctx)
16290  {
16291  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16292  __func__);
16293  retval = NI_RETCODE_INVALID_PARAM;
16294  LRETURN;
16295  }
16296 
16297  ni_pthread_mutex_lock(&p_ctx->mutex);
16298 
16299  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16300  {
16301  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16302  __func__);
16304  LRETURN;
16305  }
16306 
16307  config_size = (sizeof(ni_instance_mgr_stream_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16308  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16309 
16310  if (ni_posix_memalign(&p_stream_info, sysconf(_SC_PAGESIZE), config_size))
16311  {
16312  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate stream info buffer.\n");
16313  retval = NI_RETCODE_ERROR_MEM_ALOC;
16314  LRETURN;
16315  }
16316 
16317  ((ni_instance_mgr_stream_info_t *)p_stream_info)->picture_width = p_ctx->active_video_width;
16318  ((ni_instance_mgr_stream_info_t *)p_stream_info)->picture_height = p_ctx->active_video_height;
16319  ((ni_instance_mgr_stream_info_t *)p_stream_info)->ai_model_type = AI_MODEL_TYPE_HVSPLUS_FILTER;
16320  ((ni_instance_mgr_stream_info_t *)p_stream_info)->ai_enhance_level = p_ctx->hvsplus_level;
16321  ((ni_instance_mgr_stream_info_t *)p_stream_info)->pix_format = p_ctx->pixel_format;
16322 
16323  ni_log2(p_ctx, NI_LOG_DEBUG,
16324  "### %s: width %d %d height %d model type %d level %d pix_format %d\n", __func__,
16325  p_ctx->active_video_width, p_ctx->ori_width, p_ctx->active_video_height,
16326  ((ni_instance_mgr_stream_info_t *)p_stream_info)->ai_model_type,
16327  ((ni_instance_mgr_stream_info_t *)p_stream_info)->ai_enhance_level,
16328  ((ni_instance_mgr_stream_info_t *)p_stream_info)->pix_format);
16329 
16330  /* configure AI manager to load the suitable network binary */
16332  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): LBA 0x%x config_size %d %d\n", __func__, ui32LBA, config_size, sizeof(ni_instance_mgr_stream_info_t));
16333 
16334  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16335  p_stream_info, config_size, ui32LBA);
16336  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
16337  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16338  if (NI_RETCODE_SUCCESS != retval)
16339  {
16340  ni_log2(p_ctx, NI_LOG_ERROR,
16341  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
16342  ", hw_id, %u, xcoder_inst_id: %d\n",
16343  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
16344  //Close the session since we can't configure it
16345  retval = ni_ai_session_close(p_ctx, 0);
16346  if (NI_RETCODE_SUCCESS != retval)
16347  {
16348  ni_log2(p_ctx, NI_LOG_ERROR,
16349  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
16350  ", hw_id, %u, xcoder_inst_id: %d\n",
16351  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
16352  p_ctx->session_id);
16353  }
16354 
16356  LRETURN;
16357  }
16358 
16359 END:
16360  ni_pthread_mutex_unlock(&p_ctx->mutex);
16361 
16362  ni_aligned_free(p_ai_config);
16363  ni_aligned_free(p_nb_data);
16364  ni_aligned_free(p_stream_info);
16365 
16366  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16367 
16368  return retval;
16369 }
16370 
16372 {
16374  void *p_buffer = NULL;
16375  uint32_t ui32LBA = 0;
16376  int retry_count = 0;
16377  uint32_t dataLen = (sizeof(ni_network_layer_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16378  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16379  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
16380  {
16381  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate buffer.\n");
16382  retval = NI_RETCODE_ERROR_MEM_ALOC;
16383  LRETURN;
16384  }
16385  for(;;)
16386  {
16387  memset(p_buffer, 0, dataLen);
16388  ui32LBA =
16390  retval =
16392  p_buffer, dataLen, ui32LBA);
16393  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
16394  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16395  if (retval != NI_RETCODE_SUCCESS)
16396  {
16397  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
16398  }
16399  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size > 0)
16400  {
16401  ni_log2(p_ctx, NI_LOG_INFO, "%s(): network binary registered\n", __func__);
16402  break;
16403  }
16404 
16405  retry_count++;
16406  /* Wait for at least 30s */
16408  {
16409  ni_usleep(100);
16410  } else
16411  {
16412  ni_log2(p_ctx, NI_LOG_DEBUG, "AI network binary configuration polling timeout\n");
16414  LRETURN;
16415  }
16416  }
16417 END:
16418 
16419  ni_aligned_free(p_buffer);
16420  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16421 
16422  return retval;
16423 
16424 }
16425 
16427  ni_frame_t *p_frame)
16428 {
16430  uint32_t ui32LBA = 0;
16431  ni_instance_buf_info_t buf_info = {0};
16432  uint32_t frame_size_bytes;
16433  uint32_t sent_size = 0;
16434  int32_t query_retry = 0;
16435  uint32_t dataLen;
16436  ni_network_buffer_info_t *buffer_info = NULL;
16437 
16438  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16439 
16440  if (!p_ctx || !p_frame)
16441  {
16442  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): passed parameters is null\n",
16443  __func__);
16444  retval = NI_RETCODE_INVALID_PARAM;
16445  return retval;
16446  }
16447 
16448  if (p_frame->data_len[0] == 0)
16449  {
16450  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() invalid data length\n",
16451  __func__);
16452  retval = NI_RETCODE_INVALID_PARAM;
16453  return retval;
16454  }
16455 
16456  ni_pthread_mutex_lock(&p_ctx->mutex);
16457 
16458 
16459  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16460  {
16461  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16462  __func__);
16464  LRETURN;
16465  }
16466 
16467  frame_size_bytes = p_frame->data_len[0];
16468  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): frame_size_bytes %u\n", __func__,
16469  frame_size_bytes);
16470 
16471  for (;;)
16472  {
16473  if (p_ctx->session_statistic.ui32WrBufAvailSize >= frame_size_bytes)
16474  {
16475  buf_info.buf_avail_size =
16477  ni_log2(p_ctx, NI_LOG_DEBUG,
16478  "Info ai write query success, available buf "
16479  "size %u >= frame size %u !\n",
16480  buf_info.buf_avail_size, frame_size_bytes);
16481  break;
16482  }
16483 
16485  "6K") >= 0)
16486  {
16488  &p_ctx->session_statistic);
16489  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
16491  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
16492  buf_info.buf_avail_size =
16494  } else
16495  {
16497  NI_DEVICE_TYPE_AI, &buf_info);
16498  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
16499  p_ctx->device_type, p_ctx->hw_id,
16500  &(p_ctx->session_id), OPT_1);
16501  }
16502  if (NI_RETCODE_SUCCESS != retval ||
16503  buf_info.buf_avail_size < frame_size_bytes)
16504  {
16505  ni_log2(p_ctx, NI_LOG_TRACE,
16506  "AI write query failed or buf_size < frame_size. Retry %d\n",
16507  query_retry);
16508  // extend to 5000 retries for 8K encode on FPGA
16509  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES)
16510  {
16511  ni_log2(p_ctx, NI_LOG_DEBUG,
16512  "AI write query exceeded max retries: %d\n",
16515  retval = NI_RETCODE_SUCCESS;
16516  LRETURN;
16517  }
16518  ni_pthread_mutex_unlock(&p_ctx->mutex);
16520  ni_pthread_mutex_lock(&p_ctx->mutex);
16521  query_retry++;
16522  }
16523  }
16524 
16526  < 0 || p_frame->iovec_num <= 1) {
16527  void *p_data;
16528  if (p_frame->iovec_num == 1) {
16529  if (!p_frame->iovec) {
16530  ni_log2(p_ctx, NI_LOG_ERROR, "Ai session write: invaid iovec\n");
16531  retval = NI_RETCODE_INVALID_PARAM;
16532  LRETURN;
16533  }
16534  if (!p_frame->iovec[0].ptr || p_frame->iovec[0].size == 0 ||
16535  ((unsigned long)p_frame->iovec[0].ptr & (NI_MEM_PAGE_ALIGNMENT - 1))) {
16536  ni_log2(p_ctx, NI_LOG_ERROR, "Ai session write: invalid iovec ptr 0x%lx, size %u\n",
16537  (unsigned long)p_frame->iovec[0].ptr, p_frame->iovec[0].size);
16538  retval = NI_RETCODE_INVALID_PARAM;
16539  LRETURN;
16540  }
16541  p_data = p_frame->iovec[0].ptr;
16542  } else {
16543  p_data = p_frame->p_buffer; //equals to p_frame->p_data[0]
16544  }
16545  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI);
16546  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai session write: p_data = %p, p_frame->buffer_size = %u, "
16547  "p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
16548  p_data, p_frame->buffer_size, p_ctx->frame_num,
16549  ui32LBA);
16550 
16551  sent_size = frame_size_bytes;
16552  if (sent_size & (NI_MEM_PAGE_ALIGNMENT - 1))
16553  {
16554  sent_size = (sent_size + NI_MEM_PAGE_ALIGNMENT - 1) &
16555  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16556  }
16557 
16558  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16559  p_data, sent_size, ui32LBA);
16561  "6K") >= 0)
16562  {
16563  if (retval != NI_RETCODE_SUCCESS)
16564  {
16565  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
16567  LRETURN;
16568  }
16570  &p_ctx->session_statistic);
16571  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
16573  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
16574  } else
16575  {
16576  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
16577  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16578  if (retval < 0)
16579  {
16580  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
16582  LRETURN;
16583  }
16584  }
16585  } else { //for iovec
16586  //validate iovec
16587  int i;
16588  uint32_t rel_offset = 0;
16589  int32_t iovec_index = 0;
16590  int32_t iovec_left;
16591 
16592  if (!p_frame->iovec) {
16593  ni_log2(p_ctx, NI_LOG_ERROR, "Ai session write: invaid iovec\n");
16594  retval = NI_RETCODE_INVALID_PARAM;
16595  LRETURN;
16596  }
16597 
16598  for (i = 0; i < p_frame->iovec_num; i++) {
16599  ni_iovec_t *this_iovec = &p_frame->iovec[i];
16600 
16601  ni_log2(p_ctx, NI_LOG_DEBUG, "iovec %d, ptr %p, size %u\n", i,
16602  this_iovec->ptr, this_iovec->size);
16603  if ((this_iovec->ptr == NULL) || (this_iovec->size == 0) ||
16604  ((unsigned long)this_iovec->ptr & (NI_MEM_PAGE_ALIGNMENT - 1))) {
16605  ni_log2(p_ctx, NI_LOG_ERROR, "Ai session write: invalid iovec ptr 0x%lx, size %u\n",
16606  (unsigned long)this_iovec->ptr, this_iovec->size);
16607  retval = NI_RETCODE_INVALID_PARAM;
16608  LRETURN;
16609  }
16610  }
16611 
16612  //1, write iovec details
16613  dataLen = (sizeof(ni_network_buffer_info_t) + NI_MEM_PAGE_ALIGNMENT - 1) &
16614  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16615  if (ni_posix_memalign((void **)&buffer_info, sysconf(_SC_PAGESIZE), dataLen))
16616  {
16617  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
16618  NI_ERRNO, __func__);
16620  }
16621  memset(buffer_info, 0x00, dataLen);
16622 
16623  //2, write iovec
16624  iovec_left = p_frame->iovec_num;
16625  do {
16626  int iovec_batch = iovec_left < NI_MAX_SEGMENT_NUM ? iovec_left : NI_MAX_SEGMENT_NUM;
16627  uint32_t rel_lba = 0;
16628 
16629  buffer_info->ui16Option = NI_AI_FLAG_IOVEC;
16630  buffer_info->ui32IovecNum = iovec_batch;
16631  buffer_info->ui32RelOffset = rel_offset;
16632  for (i = 0; i < iovec_batch; i++) {
16633  ni_iovec_t *iovec = &p_frame->iovec[iovec_index + i];
16634  buffer_info->segment[i].ui32RelLba = rel_lba;
16635  buffer_info->segment[i].ui32Size = iovec->size;
16636  rel_lba += (((iovec->size + NI_MEM_PAGE_ALIGNMENT - 1) &
16637  ~(NI_MEM_PAGE_ALIGNMENT - 1)) >> LBA_BIT_OFFSET) + 1;
16638  rel_offset += iovec->size;
16639  }
16640  ni_log2(p_ctx, NI_LOG_DEBUG, "Dev alloc frame: frame_index %u\n",
16641  buffer_info->ui16FrameIdx[0]);
16642 
16643  ui32LBA =
16645 
16646  retval =
16648  buffer_info, dataLen, ui32LBA);
16649  //CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
16650  // p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
16651  // OPT_1);
16652  if (NI_RETCODE_SUCCESS != retval)
16653  {
16654  ni_log2(p_ctx,
16655  NI_LOG_ERROR,
16656  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
16657  ", hw_id, %u, xcoder_inst_id: %d\n",
16658  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
16659  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n",
16660  __func__);
16662  LRETURN;
16663  }
16664 
16665 #ifdef __linux__
16666  for (i = 0; i < iovec_batch; i++) {
16667  ni_iovec_t *iovec = &p_frame->iovec[iovec_index + i];
16668  ni_segment_t *segment = &buffer_info->segment[i];
16669  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI) + segment->ui32RelLba;
16670  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai session write: data = %p, size = %u, "
16671  "p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
16672  iovec->ptr, iovec->size, p_ctx->frame_num, ui32LBA);
16673  ni_nvme_setup_aio_iocb(p_ctx->blk_io_handle, p_ctx->iocbs[i], iovec->ptr,
16674  (iovec->size + NI_MEM_PAGE_ALIGNMENT - 1) & ~(NI_MEM_PAGE_ALIGNMENT - 1),
16675  ui32LBA, 1);
16676  }
16677 
16678  retval = ni_nvme_batch_cmd_aio(p_ctx->aio_context, p_ctx->iocbs, p_ctx->io_event, iovec_batch);
16679  if (retval != NI_RETCODE_SUCCESS) {
16680  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): failed to do batch io\n", __func__);
16681  LRETURN;
16682  }
16683 #else
16684  for (i = 0; i < iovec_batch; i++) {
16685  ni_iovec_t *iovec = &p_frame->iovec[iovec_index + i];
16686  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI) + rel_lba;
16687  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai session write: data = %p, size = %u, "
16688  "p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
16689  iovec->ptr, iovec->size, p_ctx->frame_num,
16690  ui32LBA);
16691 
16692  sent_size = (iovec->size + NI_MEM_PAGE_ALIGNMENT - 1) &
16693  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16694  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16695  iovec->ptr, sent_size, ui32LBA);
16696  if (retval != NI_RETCODE_SUCCESS) {
16697  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
16698  LRETURN;
16699  }
16700  rel_lba = (((iovec->size + NI_MEM_PAGE_ALIGNMENT - 1) &
16701  ~(NI_MEM_PAGE_ALIGNMENT - 1)) >> LBA_BIT_OFFSET) + 1;
16702  }
16703 #endif
16704  iovec_index += iovec_batch;
16705  iovec_left -= iovec_batch;
16706  } while (iovec_left);
16707 
16709  "6K") >= 0)
16710  {
16711  if (retval != NI_RETCODE_SUCCESS)
16712  {
16713  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
16715  LRETURN;
16716  }
16718  &p_ctx->session_statistic);
16719  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
16721  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
16722  } else
16723  {
16724  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
16725  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16726  if (retval < 0)
16727  {
16728  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
16730  LRETURN;
16731  }
16732  }
16733  }
16734 
16735  p_ctx->frame_num++;
16736 
16737  retval = frame_size_bytes;
16738 
16739 END:
16740 
16741  ni_pthread_mutex_unlock(&p_ctx->mutex);
16742 
16743  free(buffer_info);
16744 
16745  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16746 
16747  return retval;
16748 }
16749 
16751  ni_packet_t *p_packet)
16752 {
16753  uint32_t actual_read_size = 0;
16754  int retval = NI_RETCODE_SUCCESS;
16755  uint32_t ui32LBA = 0;
16756  ni_instance_buf_info_t buf_info = {0};
16757 
16758  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16759 
16760  if (!p_ctx || !p_packet || !p_packet->p_data)
16761  {
16762  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16763  __func__);
16764  retval = NI_RETCODE_INVALID_PARAM;
16765  return retval;
16766  }
16767 
16768  ni_pthread_mutex_lock(&p_ctx->mutex);
16769 
16770  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16771  {
16772  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16773  __func__);
16775  LRETURN;
16776  }
16777 
16778  for (;;)
16779  {
16780  if (p_ctx->session_statistic.ui32RdBufAvailSize >= p_packet->data_len)
16781  {
16782  buf_info.buf_avail_size =
16784  ni_log2(p_ctx, NI_LOG_DEBUG,
16785  "Info ai read query success, available buf "
16786  "size %u >= frame size %u !\n",
16787  buf_info.buf_avail_size, p_packet->data_len);
16788  break;
16789  }
16791  "6K") >= 0)
16792  {
16794  &p_ctx->session_statistic);
16795  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
16797  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
16798  buf_info.buf_avail_size =
16800  } else
16801  {
16803  NI_DEVICE_TYPE_AI, &buf_info);
16804  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
16805  p_ctx->device_type, p_ctx->hw_id,
16806  &(p_ctx->session_id), OPT_1);
16807  }
16808  ni_log2(p_ctx, NI_LOG_TRACE,
16809  "Info ai read query rc %d, available buf size %u, "
16810  "frame_num=%" PRIu64 ", pkt_num=%" PRIu64 "\n",
16811  retval, buf_info.buf_avail_size, p_ctx->frame_num,
16812  p_ctx->pkt_num);
16813 
16814  if (NI_RETCODE_SUCCESS != retval)
16815  {
16816  ni_log2(p_ctx, NI_LOG_DEBUG, "Buffer info query failed in ai read!!!!\n");
16817  LRETURN;
16818  } else if (0 == buf_info.buf_avail_size)
16819  {
16820  ni_log2(p_ctx, NI_LOG_DEBUG, "Info ai read available buf size %u, eos %u !\n",
16821  buf_info.buf_avail_size, p_packet->end_of_stream);
16822  retval = NI_RETCODE_SUCCESS;
16823  LRETURN;
16824  }
16825  }
16826  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai read buf_avail_size %u\n", buf_info.buf_avail_size);
16827 
16828  assert(buf_info.buf_avail_size >= p_packet->data_len);
16829 
16830  ui32LBA = READ_INSTANCE_R(p_ctx->session_id, NI_DEVICE_TYPE_AI);
16831  actual_read_size = p_packet->data_len;
16832  if (actual_read_size & (NI_MEM_PAGE_ALIGNMENT - 1))
16833  {
16834  actual_read_size = (actual_read_size + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16835  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16836  }
16837 
16838  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16839  p_packet->p_data, actual_read_size, ui32LBA);
16841  "6K") >= 0)
16842  {
16843  if (retval != NI_RETCODE_SUCCESS)
16844  {
16845  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
16847  LRETURN;
16848  }
16850  &p_ctx->session_statistic);
16851  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
16853  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
16854  } else
16855  {
16856  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
16857  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16858  if (retval < 0)
16859  {
16860  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
16862  LRETURN;
16863  }
16864  }
16865 
16866  retval = p_packet->data_len;
16867 
16868 END:
16869 
16870  ni_pthread_mutex_unlock(&p_ctx->mutex);
16871 
16872  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16873 
16874  return retval;
16875 }
16876 
16877 static void ni_unreference_network_data(ni_network_data_t *network_data)
16878 {
16879  if (network_data)
16880  {
16881  ni_memfree(network_data->inset);
16882  ni_memfree(network_data->linfo.in_param);
16883  }
16884 }
16885 
16887  ni_network_data_t *p_network)
16888 {
16889  void *p_buffer = NULL;
16890  void *p_info = NULL;
16892  uint32_t ui32LBA = 0;
16893  uint32_t dataLen;
16894  int32_t query_retry = 0;
16895  int l;
16896  ni_network_layer_params_t *layer_param;
16897  uint32_t buffer_size;
16898  uint32_t this_size;
16899  ni_network_data_t *network_data = NULL;
16900  uint32_t total_io_num;
16901 
16902  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16903 
16904  if (!p_ctx || !p_network)
16905  {
16906  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16907  __func__);
16908  retval = NI_RETCODE_INVALID_PARAM;
16909  LRETURN;
16910  }
16911 
16912  network_data = p_ctx->network_data;
16913  if (network_data->linfo.in_param != NULL)
16914  {
16915  ni_log2(p_ctx, NI_LOG_ERROR,
16916  "ERROR: %s(): network parameters data is already initialized\n",
16917  __func__);
16918  retval = NI_RETCODE_SUCCESS;
16919  LRETURN;
16920  }
16921 
16922  ni_pthread_mutex_lock(&p_ctx->mutex);
16923 
16924  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16925  {
16926  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16927  __func__);
16929  LRETURN;
16930  }
16931 
16932  /* query available size can be read. */
16933  dataLen = (sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16934  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16935  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
16936  {
16937  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
16938  NI_ERRNO, __func__);
16939  retval = NI_RETCODE_ERROR_MEM_ALOC;
16940  LRETURN;
16941  }
16942  memset(p_buffer, 0, dataLen);
16943 
16944  for (;;)
16945  {
16947  "6J") >= 0)
16948  {
16949  ui32LBA = QUERY_INSTANCE_NL_SIZE_V2_R(p_ctx->session_id,
16951  } else
16952  {
16953  ui32LBA =
16955  }
16956  retval =
16958  p_buffer, dataLen, ui32LBA);
16959  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
16960  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16961  if (retval != NI_RETCODE_SUCCESS)
16962  {
16963  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
16965  LRETURN;
16966  }
16967 
16968  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size > 0)
16969  {
16970  break;
16971  }
16972 
16973  query_retry++;
16974  if (query_retry > 50000)
16975  {
16976  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): exceeded max query retries: %d\n",
16977  __func__, query_retry - 1);
16979  LRETURN;
16980  }
16981  ni_pthread_mutex_unlock(&p_ctx->mutex);
16982  ni_usleep(200);
16983  ni_pthread_mutex_lock(&p_ctx->mutex);
16984  continue;
16985  }
16986 
16987  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size == 0)
16988  {
16990  LRETURN;
16991  }
16992 
16994  "6J") >= 0)
16995  {
16996  network_data->input_num =
16997  ((ni_instance_buf_info_t *)p_buffer)->buf_avail_size >> 16;
16998  network_data->output_num =
16999  ((ni_instance_buf_info_t *)p_buffer)->buf_avail_size & 0xFFFF;
17000  } else
17001  {
17002  network_data->input_num = 4;
17003  network_data->output_num = 4;
17004  }
17005 
17006  total_io_num = network_data->input_num + network_data->output_num;
17007 
17008  network_data->linfo.in_param = (ni_network_layer_params_t *)calloc(
17009  total_io_num, sizeof(ni_network_layer_params_t));
17010  if (!network_data->linfo.in_param)
17011  {
17012  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Unable to allocate network layer params\n",
17013  __func__);
17014  retval = NI_RETCODE_ERROR_MEM_ALOC;
17015  LRETURN;
17016  }
17017  network_data->linfo.out_param =
17018  network_data->linfo.in_param + network_data->input_num;
17019 
17020  network_data->inset = (ni_network_layer_offset_t *)calloc(
17021  total_io_num, sizeof(ni_network_layer_offset_t));
17022  if (!network_data->inset)
17023  {
17024  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Unable to allocate network offset\n",
17025  __func__);
17026  retval = NI_RETCODE_ERROR_MEM_ALOC;
17027  LRETURN;
17028  }
17029  network_data->outset = network_data->inset + network_data->input_num;
17030 
17032  "6J") >= 0)
17033  {
17034  /* query the real network layer data */
17035  this_size = sizeof(ni_network_layer_params_t) * total_io_num;
17036  dataLen = (this_size + (NI_MEM_PAGE_ALIGNMENT - 1)) &
17037  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17038  if (ni_posix_memalign(&p_info, sysconf(_SC_PAGESIZE), dataLen))
17039  {
17040  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate info buffer\n",
17041  NI_ERRNO, __func__);
17042  retval = NI_RETCODE_ERROR_MEM_ALOC;
17043  LRETURN;
17044  }
17045  memset(p_info, 0, dataLen);
17046 
17048  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
17049  p_info, dataLen, ui32LBA);
17050  if ((int32_t)retval < 0)
17051  {
17052  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
17054  LRETURN;
17055  }
17056  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
17057  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
17058  memcpy(network_data->linfo.in_param, p_info, this_size);
17059  } else
17060  {
17061  /* query the real network layer data */
17062  this_size = sizeof(ni_network_layer_params_t) * total_io_num;
17063  dataLen = (this_size + (NI_MEM_PAGE_ALIGNMENT - 1)) &
17064  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17065  if (ni_posix_memalign(&p_info, sysconf(_SC_PAGESIZE), dataLen))
17066  {
17067  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate info buffer\n",
17068  NI_ERRNO, __func__);
17069  retval = NI_RETCODE_ERROR_MEM_ALOC;
17070  LRETURN;
17071  }
17072  memset(p_info, 0, dataLen);
17073 
17075  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
17076  p_info, dataLen, ui32LBA);
17077  if ((int32_t)retval < 0)
17078  {
17079  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
17081  LRETURN;
17082  }
17083  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
17084  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
17085  memcpy(network_data->linfo.in_param, p_info, this_size);
17086 
17087  for (l = 0, network_data->input_num = 0; l < 4; l++)
17088  {
17089  layer_param = &network_data->linfo.in_param[l];
17090  if (layer_param->num_of_dims == 0)
17091  {
17092  break;
17093  }
17094  network_data->input_num++;
17095  }
17096 
17097  for (l = 0, network_data->output_num = 0; l < 4; l++)
17098  {
17099  layer_param = &network_data->linfo.out_param[l];
17100  if (layer_param->num_of_dims == 0)
17101  {
17102  break;
17103  }
17104  network_data->output_num++;
17105  }
17106  }
17107 
17108  for (l = 0, buffer_size = 0; l < network_data->input_num; l++)
17109  {
17110  layer_param = &network_data->linfo.in_param[l];
17111  this_size = ni_ai_network_layer_size(layer_param);
17112  this_size =
17113  (this_size + NI_AI_HW_ALIGN_SIZE - 1) & ~(NI_AI_HW_ALIGN_SIZE - 1);
17114  network_data->inset[l].offset = buffer_size;
17115  buffer_size += this_size;
17116 
17117  ni_log2(p_ctx,
17118  NI_LOG_DEBUG,
17119  "%s(): network input layer %d: dims %u, %u/%u/%u/%u, f %d, q %d\n",
17120  __func__, l, layer_param->num_of_dims, layer_param->sizes[0],
17121  layer_param->sizes[1], layer_param->sizes[2], layer_param->sizes[3],
17122  layer_param->data_format, layer_param->quant_format);
17123  }
17124 
17125  for (l = 0, buffer_size = 0; l < network_data->output_num; l++)
17126  {
17127  layer_param = &network_data->linfo.out_param[l];
17128  this_size = ni_ai_network_layer_size(layer_param);
17129  this_size =
17130  (this_size + NI_AI_HW_ALIGN_SIZE - 1) & ~(NI_AI_HW_ALIGN_SIZE - 1);
17131  network_data->outset[l].offset = buffer_size;
17132  buffer_size += this_size;
17133 
17134  ni_log2(p_ctx,
17135  NI_LOG_DEBUG,
17136  "%s(): network output layer %d: dims %u, %u/%u/%u/%u, f %d, q %d\n",
17137  __func__, l, layer_param->num_of_dims, layer_param->sizes[0],
17138  layer_param->sizes[1], layer_param->sizes[2], layer_param->sizes[3],
17139  layer_param->data_format, layer_param->quant_format);
17140  }
17141 
17142  memcpy(p_network, network_data, sizeof(ni_network_data_t));
17143 
17144  retval = NI_RETCODE_SUCCESS;
17145 
17146 END:
17147  ni_pthread_mutex_unlock(&p_ctx->mutex);
17148 
17149  if (retval != NI_RETCODE_SUCCESS)
17150  {
17151  ni_unreference_network_data(network_data);
17152  }
17153  ni_aligned_free(p_buffer);
17154  ni_aligned_free(p_info);
17155  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
17156  return retval;
17157 }
17158 
17160 {
17162  void *p_buffer = NULL;
17163  uint32_t ui32LBA = 0;
17164 
17165  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
17166 
17167  if (!p_ctx)
17168  {
17169  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
17170  __func__);
17171  retval = NI_RETCODE_INVALID_PARAM;
17172  LRETURN;
17173  }
17174 
17175  //Check if there is an instance or we need a new one
17176  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
17177  {
17178  p_ctx->device_type = NI_DEVICE_TYPE_AI;
17179  p_ctx->pts_table = NULL;
17180  p_ctx->dts_queue = NULL;
17181  p_ctx->buffer_pool = NULL;
17182  p_ctx->status = 0;
17183  p_ctx->key_frame_type = 0;
17184  p_ctx->keyframe_factor = 1;
17185  p_ctx->frame_num = 0;
17186  p_ctx->pkt_num = 0;
17187  p_ctx->rc_error_count = 0;
17188  p_ctx->force_frame_type = 0;
17189  p_ctx->ready_to_close = 0;
17190  //Sequence change tracking reated stuff
17191  p_ctx->active_video_width = 0;
17192  p_ctx->active_video_height = 0;
17193  p_ctx->p_all_zero_buf = NULL;
17194  p_ctx->actual_video_width = 0;
17196  memset(&(p_ctx->param_err_msg[0]), 0, sizeof(p_ctx->param_err_msg));
17197 
17198  //malloc zero data buffer
17199  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
17201  {
17202  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
17203  NI_ERRNO, __func__);
17204  retval = NI_RETCODE_ERROR_MEM_ALOC;
17205  LRETURN;
17206  }
17207  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
17208 
17209  //malloc data buffer
17210  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE),
17212  {
17213  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
17214  NI_ERRNO, __func__);
17215  retval = NI_RETCODE_ERROR_MEM_ALOC;
17216  LRETURN;
17217  }
17218  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
17219 
17220  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
17221  //In case we can open sesison, the session id would become valid.
17222  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
17223  (uint16_t)NI_INVALID_SESSION_ID;
17224 
17225  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
17226  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_AI, 0, p_ctx->hw_action);
17227  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): LBA 0x%x, hw_action %d\n", __func__,
17228  ui32LBA, p_ctx->hw_action);
17229  retval =
17231  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
17232  if (retval != NI_RETCODE_SUCCESS)
17233  {
17234  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
17235  LRETURN;
17236  }
17237  //Open will return a session status structure with a valid session id if it worked.
17238  //Otherwise the invalid session id set before the open command will stay
17239  if ((uint16_t)NI_INVALID_SESSION_ID ==
17240  ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId))
17241  {
17242  ni_log2(p_ctx, NI_LOG_ERROR,
17243  "ERROR %s(): p_ctx->device_handle=%" PRIx64
17244  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
17245  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
17246  p_ctx->session_id);
17247  ni_ai_session_close(p_ctx, 0);
17249  LRETURN;
17250  }
17251  p_ctx->session_id =
17252  ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
17253  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
17254  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
17255  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
17256  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai open session ID:0x%x,timestamp:%" PRIu64 "\n",
17257  p_ctx->session_id, p_ctx->session_timestamp);
17258 
17259  ni_log2(p_ctx, NI_LOG_DEBUG, "Open session completed\n");
17260  ni_log2(p_ctx, NI_LOG_DEBUG,
17261  "%s(): p_ctx->device_handle=%" PRIx64
17262  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
17263  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
17264  p_ctx->session_id);
17265 
17266  //Send keep alive timeout Info
17267  uint64_t keep_alive_timeout =
17268  p_ctx->keep_alive_timeout * 1000000; //send us to FW
17269  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
17270  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
17271  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
17272  keep_alive_timeout);
17274  retval =
17276  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
17277  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
17278  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
17279  CHECK_VPU_RECOVERY(retval);
17280 
17281  if (NI_RETCODE_SUCCESS != retval)
17282  {
17283  ni_log2(p_ctx, NI_LOG_ERROR,
17284  "ERROR %s(): nvme write keep_alive_timeout command "
17285  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
17286  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
17288  LRETURN;
17289  }
17290 
17291  p_ctx->network_data =
17292  (ni_network_data_t *)calloc(1, sizeof(ni_network_data_t));
17293  if (!p_ctx->network_data)
17294  {
17295  ni_log2(p_ctx, NI_LOG_ERROR,
17296  "ERROR %s(): Unable to allocate network_data memory\n");
17297  retval = NI_RETCODE_ERROR_MEM_ALOC;
17298  LRETURN;
17299  }
17300  }
17301 
17302  //aio
17303 #ifdef __linux__
17304  if (ni_aio_setup(NI_MAX_SEGMENT_NUM, &p_ctx->aio_context) != 0) {
17305  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): failed to setup aio context\n", __func__);
17306  retval = NI_RETCODE_ERROR_MEM_ALOC;
17307  LRETURN;
17308  }
17309 
17310  p_ctx->iocbs = (ni_iocb_t **)malloc(sizeof(ni_iocb_t *) * NI_MAX_SEGMENT_NUM);
17311  if (!p_ctx->iocbs) {
17312  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): failed to allocate iocbs\n", __func__);
17313  retval = NI_RETCODE_ERROR_MEM_ALOC;
17314  LRETURN;
17315  } else {
17316  int i;
17317  for (i = 0; i < NI_MAX_SEGMENT_NUM; i++) {
17318  p_ctx->iocbs[i] = (ni_iocb_t *)malloc(sizeof(ni_iocb_t));
17319  if (!p_ctx->iocbs[i]) {
17320  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): failed to allocate iocb %d\n", __func__, i);
17321  retval = NI_RETCODE_ERROR_MEM_ALOC;
17322  LRETURN;
17323  }
17324  }
17325  }
17326 
17327  p_ctx->io_event = (ni_io_event_t *)malloc(sizeof(ni_io_event_t) * NI_MAX_SEGMENT_NUM);
17328  if (!p_ctx->io_event) {
17329  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): failed to allocate ioevent %d\n", __func__);
17330  retval = NI_RETCODE_ERROR_MEM_ALOC;
17331  LRETURN;
17332  }
17333 #endif
17334 
17335  // init for frame pts calculation
17336  p_ctx->is_first_frame = 1;
17337  p_ctx->last_pts = 0;
17338  p_ctx->last_dts = 0;
17339  p_ctx->active_video_width = 0;
17340  p_ctx->active_video_height = 0;
17341  p_ctx->actual_video_width = 0;
17342 
17343 END:
17344 
17345  ni_aligned_free(p_buffer);
17346  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
17347  return retval;
17348 }
17349 
17351 {
17353  void *p_buffer = NULL;
17354  uint32_t ui32LBA = 0;
17355 
17356  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
17357 
17358  if (!p_ctx)
17359  {
17360  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
17361  __func__);
17362  return NI_RETCODE_INVALID_PARAM;
17363  }
17364 
17365  ni_pthread_mutex_lock(&p_ctx->mutex);
17366 
17367  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
17368  {
17369  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
17370  retval = NI_RETCODE_SUCCESS;
17371  LRETURN;
17372  }
17373 
17374  //malloc data buffer
17375  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
17376  {
17377  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
17378  NI_ERRNO, __func__);
17379  retval = NI_RETCODE_ERROR_MEM_ALOC;
17380  LRETURN;
17381  }
17382  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
17383 
17384  ui32LBA = CLOSE_SESSION_R(p_ctx->session_id, NI_DEVICE_TYPE_AI);
17385 
17386  int retry = 0;
17387  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
17388  {
17389  ni_log2(p_ctx, NI_LOG_DEBUG,
17390  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
17391  "p_ctx->session_id=%d, close_mode=1\n",
17392  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
17393  p_ctx->session_id);
17394 
17396  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
17397  {
17398  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed\n", __func__);
17401  break;
17402  } else
17403  {
17404  //Close should always succeed
17405  retval = NI_RETCODE_SUCCESS;
17407  break;
17408  }
17409  /*
17410  else if(((ni_session_closed_status_t *)p_buffer)->session_closed)
17411  {
17412  retval = NI_RETCODE_SUCCESS;
17413  p_ctx->session_id = NI_INVALID_SESSION_ID;
17414  break;
17415  }
17416  else
17417  {
17418  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): wait for close\n", __func__);
17419  ni_usleep(NI_SESSION_CLOSE_RETRY_INTERVAL_US);
17420  retval = NI_RETCODE_ERROR_NVME_CMD_FAILED;
17421  }
17422  */
17423  retry++;
17424  }
17425 
17426 END:
17427 
17428 #ifdef __linux__
17429  if (p_ctx->aio_context != 0) {
17430  if (ni_aio_destroy(p_ctx->aio_context) != 0) {
17431  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): failed to destroy aio context\n", __func__);
17432  } else {
17433  p_ctx->aio_context = 0;
17434  }
17435  }
17436  if (p_ctx->iocbs) {
17437  for (int i = 0; i < NI_MAX_SEGMENT_NUM; i++) {
17438  ni_memfree(p_ctx->iocbs[i]);
17439  }
17440  ni_memfree(p_ctx->iocbs);
17441  }
17442  ni_memfree(p_ctx->io_event);
17443 #endif
17444 
17445  ni_unreference_network_data(p_ctx->network_data);
17446  ni_memfree(p_ctx->network_data);
17447 
17448  ni_aligned_free(p_buffer);
17450 
17451  //Sequence change related stuff cleanup here
17452  p_ctx->active_video_width = 0;
17453  p_ctx->active_video_height = 0;
17454  p_ctx->actual_video_width = 0;
17455 
17456  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): CTX[Card:%" PRIx64 " / HW:%d / INST:%d]\n",
17457  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
17458  p_ctx->session_id);
17459  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
17460 
17461  ni_pthread_mutex_unlock(&p_ctx->mutex);
17462 
17463  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
17464 
17465  return retval;
17466 }
17467 
17469  ni_frame_config_t p_cfg_in[],
17470  int numInCfgs,
17471  ni_frame_config_t *p_cfg_out)
17472 {
17474  ni_network_buffer_info_t *p_data = NULL;
17475  void *p_read_data = NULL;
17476  uint32_t dataLen;
17477  uint32_t ui32LBA = 0;
17478 
17479  if (!p_ctx)
17480  {
17481  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
17482  __func__);
17483  return NI_RETCODE_INVALID_PARAM;
17484  }
17485 
17486  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
17487  {
17488  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
17489  __func__);
17491  }
17492  ni_pthread_mutex_lock(&p_ctx->mutex);
17493  ni_instance_buf_info_t buf_info = {0};
17494  int32_t query_retry = 0;
17495  for (;;)
17496  {
17497  if (p_ctx->session_statistic.ui32WrBufAvailSize > 0)
17498  {
17499  buf_info.buf_avail_size =
17501  ni_log2(p_ctx, NI_LOG_DEBUG,
17502  "Info ai write query success, available buf "
17503  "size %u !\n",
17504  buf_info.buf_avail_size);
17505  break;
17506  }
17507 
17509  &p_ctx->session_statistic);
17510  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
17512  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
17513  buf_info.buf_avail_size =
17515  if (NI_RETCODE_SUCCESS != retval ||
17516  buf_info.buf_avail_size == 0)
17517  {
17518  ni_log2(p_ctx, NI_LOG_TRACE,
17519  "AI write query failed or buf_size < frame_size. Retry %d\n",
17520  query_retry);
17521  // extend to 5000 retries for 8K encode on FPGA
17522  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES)
17523  {
17524  ni_log2(p_ctx, NI_LOG_TRACE,
17525  "AI write query exceeded max retries: %d\n",
17528  retval = NI_RETCODE_SUCCESS;
17529  LRETURN;
17530  }
17531  ni_pthread_mutex_unlock(&p_ctx->mutex);
17533  ni_pthread_mutex_lock(&p_ctx->mutex);
17534  query_retry++;
17535  }
17536  }
17537 
17538  dataLen = (sizeof(ni_network_buffer_info_t) + NI_MEM_PAGE_ALIGNMENT - 1) &
17539  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17540 
17541  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
17542  {
17543  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
17544  NI_ERRNO, __func__);
17546  }
17547 
17548  memset(p_data, 0x00, dataLen);
17549  p_data->ui16Width = p_cfg_in[0].picture_width;
17550  p_data->ui16Height = p_cfg_in[0].picture_height;
17551  p_data->ui16Option = p_cfg_in[0].options;
17552  p_data->ui8PoolSize = p_cfg_in[0].rgba_color;
17553  p_data->ui8MultiIn = 1;
17554  for(int i = 0; i < numInCfgs; i++){
17555  p_data->ui16FrameIdx[i] = p_cfg_in[i].frame_index;
17556  ni_log2(p_ctx, NI_LOG_DEBUG, "Dev alloc frame[%d]: frame_index %u, hw=%d\n",
17557  i,p_data->ui16FrameIdx[i], p_data->ui8MultiIn);
17558  }
17559 
17560 
17561  ui32LBA =
17563 
17564  retval =
17566  p_data, dataLen, ui32LBA);
17567  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
17568  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
17569  OPT_1);
17570  if (NI_RETCODE_SUCCESS != retval)
17571  {
17572  ni_log2(p_ctx,
17573  NI_LOG_ERROR,
17574  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
17575  ", hw_id, %u, xcoder_inst_id: %d\n",
17576  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
17577  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n",
17578  __func__);
17580  LRETURN;
17581  }
17582 
17583  END:
17584  ni_pthread_mutex_unlock(&p_ctx->mutex);
17585  ni_aligned_free(p_data);
17586  if (p_read_data != NULL)
17587  {
17588  ni_aligned_free(p_read_data);
17589  }
17590  return retval;
17591 }
17592 
17594  niFrameSurface1_t *p_out_surface)
17595 {
17597  ni_network_buffer_info_t *p_data = NULL;
17598  void *p_read_data = NULL;
17599  uint32_t dataLen;
17600  uint32_t ui32LBA = 0;
17601 
17602  if (!p_ctx)
17603  {
17604  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
17605  __func__);
17606  return NI_RETCODE_INVALID_PARAM;
17607  }
17608 
17609  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
17610  {
17611  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
17612  __func__);
17614  }
17615 
17616  if (ni_cmp_fw_api_ver(
17618  "6rL") < 0)
17619  {
17620  ni_log2(p_ctx, NI_LOG_ERROR,
17621  "Error: %s function not supported on device with FW API version < 6rL\n",
17622  __func__);
17624  }
17625 
17626  ni_pthread_mutex_lock(&p_ctx->mutex);
17627 
17628  int query_retry = 0;
17629  dataLen =
17630  (sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
17631  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17632  if (ni_posix_memalign(&p_read_data, sysconf(_SC_PAGESIZE), dataLen))
17633  {
17634  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
17635  NI_ERRNO, __func__);
17636  retval = NI_RETCODE_ERROR_MEM_ALOC;
17637  LRETURN;
17638  }
17639  memset(p_read_data, 0, dataLen);
17640 
17641  for (;;)
17642  {
17643  ui32LBA = QUERY_INSTANCE_HW_OUT_SIZE_R(p_ctx->session_id,
17645  retval =
17647  p_read_data, dataLen, ui32LBA);
17648  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read,
17649  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
17650  OPT_1);
17651  if (retval != NI_RETCODE_SUCCESS)
17652  {
17653  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
17655  LRETURN;
17656  }
17657 
17658  if (((ni_instance_buf_info_t *)p_read_data)->hw_inst_ind.buffer_avail > 0)
17659  {
17660  p_out_surface->ui16FrameIdx = ((ni_instance_buf_info_t *)p_read_data)->hw_inst_ind.frame_index;
17661  p_out_surface->ui16session_ID = p_ctx->session_id;
17662  p_out_surface->device_handle =
17663  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
17664  p_out_surface->bit_depth = p_ctx->bit_depth_factor;
17665  p_out_surface->src_cpu = (uint8_t)NI_DEVICE_TYPE_AI;
17666  p_out_surface->output_idx = 0;
17667  break;
17668  }
17669 
17670  query_retry++;
17671  if (query_retry > 2000)
17672  {
17673  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): exceeded max query retries: %d\n",
17674  __func__, query_retry - 1);
17675  retval = NI_RETCODE_EAGAIN;
17676  LRETURN;
17677  }
17678  ni_pthread_mutex_unlock(&p_ctx->mutex);
17679  ni_usleep(200);
17680  ni_pthread_mutex_lock(&p_ctx->mutex);
17681  continue;
17682  }
17683 
17684  if (((ni_instance_buf_info_t *)p_read_data)->hw_inst_ind.buffer_avail == 0)
17685  {
17687  LRETURN;
17688  }
17689 END:
17690  ni_pthread_mutex_unlock(&p_ctx->mutex);
17691  ni_aligned_free(p_data);
17692  if (p_read_data != NULL)
17693  {
17694  ni_aligned_free(p_read_data);
17695  }
17696  return retval;
17697 }
17698 
17700  int height, int options, int pool_size,
17701  int frame_index)
17702 {
17704  ni_network_buffer_info_t *p_data = NULL;
17705  void *p_read_data = NULL;
17706  uint32_t dataLen;
17707  uint32_t ui32LBA = 0;
17708 
17709  if (!p_ctx)
17710  {
17711  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
17712  __func__);
17713  return NI_RETCODE_INVALID_PARAM;
17714  }
17715 
17716  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
17717  {
17718  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
17719  __func__);
17721  }
17722 
17723  ni_pthread_mutex_lock(&p_ctx->mutex);
17724 
17725  if (options == NI_AI_FLAG_IO)
17726  {
17727  int query_retry = 0;
17728  dataLen =
17729  (sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
17730  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17731  if (ni_posix_memalign(&p_read_data, sysconf(_SC_PAGESIZE), dataLen))
17732  {
17733  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
17734  NI_ERRNO, __func__);
17735  retval = NI_RETCODE_ERROR_MEM_ALOC;
17736  LRETURN;
17737  }
17738  memset(p_read_data, 0, dataLen);
17739 
17740  for (;;)
17741  {
17742  ui32LBA = QUERY_INSTANCE_HW_OUT_SIZE_R(p_ctx->session_id,
17744  retval =
17746  p_read_data, dataLen, ui32LBA);
17747  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read,
17748  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
17749  OPT_1);
17750  if (retval != NI_RETCODE_SUCCESS)
17751  {
17752  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
17754  LRETURN;
17755  }
17756 
17757  if (((ni_instance_buf_info_t *)p_read_data)->buf_avail_size > 0)
17758  {
17759  break;
17760  }
17761 
17762  query_retry++;
17763  if (query_retry > 2000)
17764  {
17765  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): exceeded max query retries: %d\n",
17766  __func__, query_retry - 1);
17767  retval = NI_RETCODE_EAGAIN;
17768  LRETURN;
17769  }
17770  ni_pthread_mutex_unlock(&p_ctx->mutex);
17771  ni_usleep(200);
17772  ni_pthread_mutex_lock(&p_ctx->mutex);
17773  continue;
17774  }
17775 
17776  if (((ni_instance_buf_info_t *)p_read_data)->buf_avail_size == 0)
17777  {
17779  LRETURN;
17780  }
17781  } else
17782  {
17783  ni_instance_buf_info_t buf_info = {0};
17784  int32_t query_retry = 0;
17785  for (;;)
17786  {
17787  if (p_ctx->session_statistic.ui32WrBufAvailSize > 0)
17788  {
17789  buf_info.buf_avail_size =
17791  ni_log2(p_ctx, NI_LOG_DEBUG,
17792  "Info ai write query success, available buf "
17793  "size %u !\n",
17794  buf_info.buf_avail_size);
17795  break;
17796  }
17797 
17799  &p_ctx->session_statistic);
17800  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
17802  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
17803  buf_info.buf_avail_size =
17805  if (NI_RETCODE_SUCCESS != retval ||
17806  buf_info.buf_avail_size == 0)
17807  {
17808  ni_log2(p_ctx, NI_LOG_TRACE,
17809  "AI write query failed or buf_size < frame_size. Retry %d\n",
17810  query_retry);
17811  // extend to 5000 retries for 8K encode on FPGA
17812  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES)
17813  {
17814  ni_log2(p_ctx, NI_LOG_TRACE,
17815  "AI write query exceeded max retries: %d\n",
17818  retval = NI_RETCODE_SUCCESS;
17819  LRETURN;
17820  }
17821  ni_pthread_mutex_unlock(&p_ctx->mutex);
17823  ni_pthread_mutex_lock(&p_ctx->mutex);
17824  query_retry++;
17825  }
17826  }
17827 
17828  dataLen = (sizeof(ni_network_buffer_info_t) + NI_MEM_PAGE_ALIGNMENT - 1) &
17829  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17830 
17831  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
17832  {
17833  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
17834  NI_ERRNO, __func__);
17836  }
17837 
17838  memset(p_data, 0x00, dataLen);
17839  p_data->ui16FrameIdx[0] = frame_index;
17840  p_data->ui16Width = width;
17841  p_data->ui16Height = height;
17842  p_data->ui16Option = options;
17843  p_data->ui8PoolSize = pool_size;
17844  p_data->ui8MultiIn = 0;
17845  ni_log2(p_ctx, NI_LOG_DEBUG, "Dev alloc frame: frame_index %u\n",
17846  p_data->ui16FrameIdx[0]);
17847 
17848  ui32LBA =
17850 
17851  retval =
17853  p_data, dataLen, ui32LBA);
17854  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
17855  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
17856  OPT_1);
17857  if (NI_RETCODE_SUCCESS != retval)
17858  {
17859  ni_log2(p_ctx,
17860  NI_LOG_ERROR,
17861  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
17862  ", hw_id, %u, xcoder_inst_id: %d\n",
17863  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
17864  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n",
17865  __func__);
17867  LRETURN;
17868  }
17869  }
17870 
17871 END:
17872  ni_pthread_mutex_unlock(&p_ctx->mutex);
17873  ni_aligned_free(p_data);
17874  if (p_read_data != NULL)
17875  {
17876  ni_aligned_free(p_read_data);
17877  }
17878  return retval;
17879 }
17880 
17881 /*!******************************************************************************
17882  * \brief read a hardware descriptor from a scaler session
17883  *
17884  * \param[in] p_ctx pointer to session context
17885  * \param[out] p_frame pointer to frame to write hw descriptor
17886  *
17887  * \return NI_RETCODE_INVALID_PARAM
17888  * NI_RETCODE_ERROR_INVALID_SESSION
17889  * NI_RETCODE_ERROR_MEM_ALOC
17890  * NI_RETCODE_ERROR_NVME_CMD_FAILED
17891  * NI_RETCODE_FAILURE
17892  *******************************************************************************/
17894  ni_frame_t *p_frame)
17895 {
17896  int retval = NI_RETCODE_SUCCESS;
17897  int retry_count = 0;
17898 
17899  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
17900 
17901  ni_pthread_mutex_lock(&p_ctx->mutex);
17902 
17903  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
17904  {
17905  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
17906  __func__);
17908  LRETURN;
17909  }
17910 
17911  for (;;)
17912  {
17913  retry_count++;
17914  ni_instance_buf_info_t sInstanceBuf = {0};
17915  niFrameSurface1_t *pFrameSurface;
17917  NI_DEVICE_TYPE_AI, &sInstanceBuf);
17918  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
17919  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
17920  OPT_3);
17921 
17923  {
17924  if (retry_count >= 500)
17925  {
17926  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning hwdesc read fail rc %d\n",
17927  retval);
17928  LRETURN;
17929  }
17930  ni_pthread_mutex_unlock(&p_ctx->mutex);
17931  ni_usleep(100);
17932  ni_pthread_mutex_lock(&p_ctx->mutex);
17933  }
17934  else if (retval != NI_RETCODE_SUCCESS)
17935  {
17936  LRETURN;
17937  } else
17938  {
17939  pFrameSurface = (niFrameSurface1_t *)p_frame->p_data[3];
17940  pFrameSurface->ui16FrameIdx = sInstanceBuf.hw_inst_ind.frame_index;
17941  pFrameSurface->ui16session_ID = p_ctx->session_id;
17942  pFrameSurface->device_handle =
17943  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
17944  pFrameSurface->src_cpu = (uint8_t)NI_DEVICE_TYPE_AI;
17945  pFrameSurface->output_idx = 0;
17946 
17947  /* A frame index of zero is invalid, the memory acquisition failed */
17948  if (pFrameSurface->ui16FrameIdx == 0)
17949  {
17950  if (retry_count >= 500)
17951  {
17952  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning hwdesc read fail rc %d\n",
17953  retval);
17954  retval = NI_RETCODE_EAGAIN;
17955  LRETURN;
17956  }
17957  ni_pthread_mutex_unlock(&p_ctx->mutex);
17958  ni_usleep(100);
17959  ni_pthread_mutex_lock(&p_ctx->mutex);
17960  continue;
17961  }
17962  LRETURN;
17963  }
17964  }
17965 
17966 END:
17967 
17968  ni_pthread_mutex_unlock(&p_ctx->mutex);
17969 
17970  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
17971 
17972  return retval;
17973 }
17974 
17975 /*!*****************************************************************************
17976  * \brief Get DDR configuration of Quadra device
17977  *
17978  * \param[in/out] p_ctx pointer to a session context with valid file handle
17979  *
17980  * \return On success NI_RETCODE_SUCCESS
17981  * On failure NI_RETCODE_INVALID_PARAM
17982  * NI_RETCODE_ERROR_MEM_ALOC
17983  * NI_RETCODE_ERROR_NVME_CMD_FAILED
17984  ******************************************************************************/
17986 {
17987  void *p_buffer = NULL;
17988  ni_nvme_identity_t *p_id_data;
17990  ni_event_handle_t event_handle = NI_INVALID_EVENT_HANDLE;
17991  uint32_t ui32LBA = IDENTIFY_DEVICE_R;
17992  ni_device_handle_t device_handle = p_ctx->blk_io_handle;
17993 
17994  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
17995 
17996  if (NI_INVALID_DEVICE_HANDLE == device_handle)
17997  {
17998  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): invalid passed parameters\n",
17999  __func__);
18000  retval = NI_RETCODE_INVALID_PARAM;
18001  LRETURN;
18002  }
18003 
18004  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE),
18006  {
18007  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer.\n",
18008  NI_ERRNO, __func__);
18009  retval = NI_RETCODE_ERROR_MEM_ALOC;
18010  LRETURN;
18011  }
18012 
18013  memset(p_buffer, 0, NI_NVME_IDENTITY_CMD_DATA_SZ);
18014 
18015  if (ni_nvme_send_read_cmd(device_handle, event_handle, p_buffer,
18016  NI_NVME_IDENTITY_CMD_DATA_SZ, ui32LBA) < 0)
18017  {
18019  LRETURN;
18020  }
18021 
18022  p_id_data = (ni_nvme_identity_t *) p_buffer;
18023 
18024  if (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR_4G)
18025  {
18026  p_ctx->ddr_config = 6;
18027  }
18028  else
18029  {
18030  if (ni_cmp_fw_api_ver(
18032  "6rJ") >= 0)
18033  {
18034  p_ctx->ddr_config = (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR)
18035  ? 3 : ((p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_DR)? 4 : 5);
18036  }
18037  else if (ni_cmp_fw_api_ver(
18039  "6rD") >= 0)
18040  {
18041  p_ctx->ddr_config = (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR)
18042  ? 3 : 4;
18043  } else
18044  {
18045  p_ctx->ddr_config = (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR)
18046  ? 1 : 2;
18047  }
18048  }
18049 
18050  ni_log2(p_ctx, NI_LOG_DEBUG, "Memory configuration %d\n",p_ctx->ddr_config);
18051 END:
18052 
18053  ni_aligned_free(p_buffer);
18054  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): retval: %d\n", __func__, retval);
18055 
18056  return retval;
18057 }
18058 
18059 /*!*****************************************************************************
18060  * \brief Set DDR configuration of Quadra device
18061  *
18062  * \param[in] p_ctx pointer to a session context with valid file handle
18063  * \param[in] ddr_priority_mode ddr priority mode
18064  *
18065  * \return On success NI_RETCODE_SUCCESS
18066  * On failure NI_RETCODE_INVALID_PARAM
18067  * NI_RETCODE_ERROR_MEM_ALOC
18068  * NI_RETCODE_ERROR_NVME_CMD_FAILED
18069  ******************************************************************************/
18071  uint8_t ddr_priority_mode)
18072 {
18073  void *p_buffer = NULL;
18075  ni_ddr_priority_config_t *p_cfg = NULL;
18076  uint32_t ui32LBA = CONFIG_SESSION_DDR_PRIORITY_W(p_ctx->session_id);
18077  ni_device_handle_t device_handle = p_ctx->blk_io_handle;
18078 
18079  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
18080 
18081  ni_log2(p_ctx, NI_LOG_INFO, "set ddr priority %d\n",
18082  ddr_priority_mode);
18083 
18084  if (NI_INVALID_DEVICE_HANDLE == device_handle)
18085  {
18086  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): invalid passed parameters\n",
18087  __func__);
18088  retval = NI_RETCODE_INVALID_PARAM;
18089  LRETURN;
18090  }
18091 
18092  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE),
18094  {
18095  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer.\n",
18096  NI_ERRNO, __func__);
18097  retval = NI_RETCODE_ERROR_MEM_ALOC;
18098  LRETURN;
18099  }
18100 
18101  memset(p_buffer, 0, NI_NVME_IDENTITY_CMD_DATA_SZ);
18102  p_cfg = (ni_ddr_priority_config_t *)p_buffer;
18103  p_cfg->ddr_mode = ddr_priority_mode;
18104  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, NI_INVALID_EVENT_HANDLE,
18105  p_buffer,NI_DATA_BUFFER_LEN, ui32LBA) < 0)
18106  {
18107  ni_log2(p_ctx, NI_LOG_ERROR, "DDR priority setting failed with mode %d\n",
18108  ddr_priority_mode);
18110  LRETURN;
18111  }
18112 END:
18113  ni_aligned_free(p_buffer);
18114  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): retval: %d\n", __func__, retval);
18115 
18116  return retval;
18117 }
18118 
18119 /*!*****************************************************************************
18120  * \brief Allocate memory for the metadata header and auxillary data for
18121  * encoder input data.
18122  *
18123  * \param[in] p_frame Pointer to a caller allocated ni_frame_t struct
18124  *
18125  * \param[in] extra_len Length header and auxillary data
18126  *
18127  * \return On success
18128  * NI_RETCODE_SUCCESS
18129  * On failure
18130  * NI_RETCODE_INVALID_PARAM
18131  * NI_RETCODE_ERROR_MEM_ALOC
18132  *****************************************************************************/
18134  int extra_len)
18135 {
18136  void *metadata_buffer = NULL;
18137  int retval = NI_RETCODE_SUCCESS;
18138 
18139  if ((!p_frame) || (extra_len <= 0))
18140  {
18142  "ERROR: %s passed parameters are null or not supported, "
18143  "p_frame %p, extra_len %d",
18144  __func__, p_frame, extra_len);
18145  return NI_RETCODE_INVALID_PARAM;
18146  }
18147 
18148  int buffer_size = extra_len;
18149  if (buffer_size % NI_MEM_PAGE_ALIGNMENT)
18150  {
18151  buffer_size = ((buffer_size / NI_MEM_PAGE_ALIGNMENT) + 1) *
18153  }
18154 
18155  // Check if previous metadata buffer needs to be freed
18156  if ((p_frame->metadata_buffer_size != buffer_size) &&
18157  (p_frame->metadata_buffer_size > 0))
18158  {
18160  "%s: free current p_frame metadata buffer, "
18161  "p_frame->buffer_size=%u\n",
18162  __func__, p_frame->metadata_buffer_size);
18163  p_frame->metadata_buffer_size = 0;
18165  }
18166 
18167  // Check if new metadata buffer needs to be allocated
18168  if (p_frame->metadata_buffer_size != buffer_size)
18169  {
18170  if (ni_posix_memalign(&metadata_buffer, sysconf(_SC_PAGESIZE),
18171  buffer_size))
18172  {
18174  "ERROR %d: %s() Cannot allocate metadata buffer.\n",
18175  NI_ERRNO, __func__);
18176  retval = NI_RETCODE_ERROR_MEM_ALOC;
18177  LRETURN;
18178  }
18179 
18180  // init once after allocation
18181  memset(metadata_buffer, 0, buffer_size);
18182  p_frame->metadata_buffer_size = buffer_size;
18183  p_frame->p_metadata_buffer = metadata_buffer;
18184 
18185  ni_log(NI_LOG_DEBUG, "%s: allocated new metadata buffer\n", __func__);
18186  } else
18187  {
18188  ni_log(NI_LOG_DEBUG, "%s: reuse metadata buffer\n", __func__);
18189  }
18190 
18192  "%s: success: p_frame->p_metadata_buffer %p "
18193  "p_frame->metadata_buffer_size=%u\n",
18194  __func__, p_frame->p_metadata_buffer, p_frame->metadata_buffer_size);
18195 
18196 END:
18197 
18198  if (NI_RETCODE_SUCCESS != retval)
18199  {
18200  ni_aligned_free(metadata_buffer);
18201  }
18202 
18203  return retval;
18204 }
18205 
18206 /*!*****************************************************************************
18207  * \brief Allocate memory for the non-4k-aligned part at the start of YUV data for
18208  * encoder input data.
18209  *
18210  * \param[in] p_frame Pointer to a caller allocated ni_frame_t struct
18211  *
18212  * \param[in] start_len Length of non-4k-aligned part at the start of YUV data
18213  *
18214  * \return On success
18215  * NI_RETCODE_SUCCESS
18216  * On failure
18217  * NI_RETCODE_INVALID_PARAM
18218  * NI_RETCODE_ERROR_MEM_ALOC
18219  *****************************************************************************/
18221 {
18222  void *start_buffer = NULL;
18223  int retval = NI_RETCODE_SUCCESS;
18224 
18225  if (!p_frame)
18226  {
18228  "ERROR: %s passed parameters are null or not supported, "
18229  "p_frame %p\n",
18230  __func__, p_frame);
18231  return NI_RETCODE_INVALID_PARAM;
18232  }
18233 
18234  // Check if new start buffer needs to be allocated
18235  if (!p_frame->start_buffer_size)
18236  {
18237  if (ni_posix_memalign(&start_buffer, sysconf(_SC_PAGESIZE),
18239  {
18241  "ERROR %d: %s() Cannot allocate start buffer.\n",
18242  NI_ERRNO, __func__);
18243  retval = NI_RETCODE_ERROR_MEM_ALOC;
18244  LRETURN;
18245  }
18246 
18247  // init once after allocation
18250  p_frame->p_start_buffer = start_buffer;
18251 
18252  ni_log(NI_LOG_DEBUG, "%s: allocated new start buffer\n", __func__);
18253  } else
18254  {
18255  ni_log(NI_LOG_DEBUG, "%s: reuse start buffer\n", __func__);
18256  }
18257 
18259  "%s: success: p_frame->p_start_buffer %p "
18260  "p_frame->start_buffer_size=%u\n",
18261  __func__, p_frame->p_start_buffer, p_frame->start_buffer_size);
18262 
18263 END:
18264 
18265  if (NI_RETCODE_SUCCESS != retval)
18266  {
18267  ni_aligned_free(start_buffer);
18268  }
18269 
18270  return retval;
18271 }
18272 
18274  ni_network_perf_metrics_t *p_metrics)
18275 {
18277  void *p_buffer = NULL;
18278  uint32_t dataLen;
18279  uint32_t ui32LBA = 0;
18280 
18281  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
18282  if (!p_ctx || !p_metrics)
18283  {
18284  ni_log2(p_ctx, NI_LOG_ERROR,
18285  "ERROR: %s() passed parameters are null!, return\n", __func__);
18286  retval = NI_RETCODE_INVALID_PARAM;
18287  LRETURN;
18288  }
18289 
18291  "6N") >= 0)
18292  {
18293  dataLen =
18295  ~(NI_MEM_PAGE_ALIGNMENT - 1);
18296  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
18297  {
18298  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
18299  NI_ERRNO, __func__);
18300  retval = NI_RETCODE_ERROR_MEM_ALOC;
18301  LRETURN;
18302  }
18303  memset(p_buffer, 0, sizeof(ni_network_perf_metrics_t));
18304 
18305  ui32LBA =
18307  retval =
18309  p_buffer, dataLen, ui32LBA);
18310  if ((int32_t)retval != NI_RETCODE_SUCCESS)
18311  {
18312  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
18314  LRETURN;
18315  }
18316  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
18317  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
18318 
18319  *p_metrics = *((ni_network_perf_metrics_t *)p_buffer);
18320  } else
18321  {
18322  p_metrics->total_cycles = (uint32_t)(-1);
18323  p_metrics->total_idle_cycles = (uint32_t)(-1);
18324  }
18325 END:
18326 
18327  ni_aligned_free(p_buffer);
18328  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
18329 
18330  return retval;
18331 }
18332 
18333 /*!*****************************************************************************
18334  * \brief Send namespace num / Opmode and SRIOv index/value to the device with
18335  * specified logic block address.
18336  *
18337  * \param[in] device_handle Device handle obtained by calling ni_device_open
18338  * \param[in] Key Represents either namespace num or opmode
18339  * \param[in] Value Represents either SRIOv index or opmode value
18340  *
18341  * \return On success
18342  * NI_RETCODE_SUCCESS
18343  * On failure
18344  * NI_RETCODE_ERROR_MEM_ALOC
18345  * NI_RETCODE_ERROR_NVME_CMD_FAILED
18346  ******************************************************************************/
18347 ni_retcode_t ni_device_config_ns_qos(ni_device_handle_t device_handle,
18348  uint32_t key,
18349  uint32_t value)
18350 {
18351  char buf[NI_DATA_BUFFER_LEN] = {'\0'};
18352  uint32_t *u32_buf = (uint32_t *)buf;
18353  u32_buf[0] = key;
18354  u32_buf[1] = value;
18355  // event handle could be ignored
18356  return ni_nvme_send_write_cmd(device_handle, NI_INVALID_EVENT_HANDLE,
18357  (void *)buf, NI_DATA_BUFFER_LEN,
18359 }
18360 
18362 {
18363  char *CoreName;
18364  switch (eCoreType)
18365  {
18366  case ALL_CORE:
18367  CoreName = (char *)"all";
18368  break;
18369  case NVME_CORE:
18370  CoreName = (char *)"np";
18371  break;
18372  case EP_CORE:
18373  CoreName = (char *)"ep";
18374  break;
18375  case DP_CORE:
18376  CoreName = (char *)"dp";
18377  break;
18378  case TP_CORE:
18379  CoreName = (char *)"tp";
18380  break;
18381  case FP_CORE:
18382  CoreName = (char *)"fp";
18383  break;
18384  default:
18385  CoreName = (char *)"Not Found";
18386  break;
18387  }
18388  return CoreName;
18389 }
18390 
18391 uint32_t ni_get_log_lba(ni_core_type_t eCoreType)
18392 {
18393  uint32_t lba;
18394  switch (eCoreType)
18395  {
18396  case NVME_CORE:
18397  lba = NVME_LOG_OFFSET_IN_4K;
18398  break;
18399  case EP_CORE:
18400  lba = EP_LOG_OFFSET_IN_4K;
18401  break;
18402  case DP_CORE:
18403  lba = DP_LOG_OFFSET_IN_4K;
18404  break;
18405  case TP_CORE:
18406  lba = TP_LOG_OFFSET_IN_4K;
18407  break;
18408  case FP_CORE:
18409  lba = FP_LOG_OFFSET_IN_4K;
18410  break;
18411  default:
18412  ni_log(NI_LOG_ERROR, "%s:() Invalid core ID:%u\n", __func__, eCoreType);
18413  lba = 0;
18414  break;
18415  }
18416 
18417  return lba;
18418 }
18419 
18420 ni_retcode_t ni_dump_log_single_core(ni_session_context_t *p_ctx, void* p_data, uint32_t core_id, bool gen_log_file)
18421 {
18422  int32_t rc;
18423  uint32_t lba = 0;
18424  uint32_t data_len = CPU_LOG_BUFFER_SIZE;
18425  char *core_name = NULL;
18426  FILE *p_file = NULL;
18428 
18429  if (!p_ctx || !p_data)
18430  {
18431  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR invalid pointer p_ctx %p p_data %p\n",
18432  __func__, __LINE__, p_ctx, p_data);
18433  return NI_RETCODE_INVALID_PARAM;
18434  }
18435 
18436  memset(p_data, 0, CPU_LOG_BUFFER_SIZE);
18437  *(uint8_t *)p_data = 0x55;
18438 
18439  lba = ni_get_log_lba((ni_core_type_t)core_id);
18440  if (lba == 0)
18441  {
18442  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR core_id %u\n",
18443  __func__, __LINE__, core_id);
18444  return NI_RETCODE_INVALID_PARAM;
18445  }
18446 
18447  core_name = ni_get_core_name((ni_core_type_t)core_id);
18448 
18449  rc = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_data, data_len, lba);
18450 
18451  if (rc != NI_RETCODE_SUCCESS)
18452  {
18453  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR %d: nvme read %s core failed\n",
18454  __func__, __LINE__, rc, core_name);
18455  }
18456  else if (gen_log_file)
18457  {
18458  //generate log file e.g. raw_dp_slot_0_0000.bin
18459  char filename[32] = "raw_";
18460  strcat(filename, core_name);
18461  strcat(filename, "_slot_");
18462 #ifdef __linux__
18463  bool pcie_id_name = false;
18464  char devFilePath[1024] = {0};
18465  char devFDPath[1024] = {0};
18466  char pcie[64] = {0};
18467  char domain[5] = {0}, slot[3] = {0}, dev[3] = {0}, func[2] = {0};
18468  //p_ctx->blk_dev_name might be empty so look up the file name
18469  snprintf(devFDPath, sizeof(devFDPath), "/proc/self/fd/%d", p_ctx->blk_io_handle);
18470  ssize_t len = readlink(devFDPath, devFilePath, sizeof(devFilePath)-1);
18471  if (len != -1) {
18472  devFilePath[len] = '\0';
18473  }
18474  if (strstr(devFilePath, "/dev/nvme") != NULL)
18475  {
18476  get_dev_pcie_addr(devFilePath, pcie, domain, slot, dev, func);
18477  if (strlen(pcie) > 0 && strlen(slot) > 0 && strlen(domain) > 0)
18478  {
18479  strcat(filename, slot);
18480  strcat(filename, "_");
18481  strcat(filename, domain);
18482  pcie_id_name = true;
18483  }
18484  }
18485  if (!pcie_id_name)
18486  {
18487  ni_log2(p_ctx, NI_LOG_INFO, "%s:():%d: For dev %d can't look up PCI domain and slot info. Defaulting to slot=hw_id and domain=0000\n",
18488  __func__, __LINE__, p_ctx->blk_io_handle);
18489  char num[4] = {0};
18490  snprintf(num, 4, "%d", p_ctx->hw_id);
18491  strcat(filename, num);
18492  strcat(filename, "_0000");
18493  }
18494  strcat(filename, ".bin");
18495  ni_log2(p_ctx, NI_LOG_INFO, "For dev %d %s core %s creating file %s\n",
18496  p_ctx->blk_io_handle, devFilePath, core_name, filename);
18497 #else
18498  char num[4] = {0};
18499  snprintf(num, 4, "%02x", p_ctx->hw_id);
18500  strcat(filename, num);
18501  strcat(filename, "_0000");
18502  strcat(filename, ".bin");
18503  ni_log2(p_ctx, NI_LOG_INFO, "For dev %d core %s creating file %s\n",
18504  p_ctx->blk_io_handle, core_name, filename);
18505 #endif
18506  p_file = fopen(filename, "wb");
18507  if (p_file)
18508  {
18509  /* Write out the stream header */
18510  if (fwrite((uint8_t *)p_data ,
18511  data_len, 1, p_file) != 1)
18512  {
18513  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: Error: writing data %u bytes error!\n",
18514  __func__, __LINE__, data_len);
18515  ni_log2(p_ctx, NI_LOG_ERROR, "Error: ferror rc = %d\n", ferror(p_file));
18516  retval = NI_RETCODE_FAILURE;
18517  }
18518  if (fflush(p_file))
18519  {
18520  ni_log2(p_ctx, NI_LOG_ERROR, "Error: writing data frame flush failed! errno %d\n",
18521  ferror(p_file));
18522  retval = NI_RETCODE_FAILURE;
18523  }
18524  fclose(p_file);
18525  }
18526  else
18527  {
18528  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: Error: cannot open %s\n",
18529  __func__, __LINE__, filename);
18530  retval = NI_RETCODE_FAILURE;
18531  }
18532  }
18533 
18534  return retval;
18535 }
18536 
18537 ni_retcode_t ni_dump_log_all_cores(ni_session_context_t *p_ctx, void* p_data, bool gen_log_file)
18538 {
18539  int i = 1;
18540 
18541  if (!p_ctx || !p_data)
18542  {
18543  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR invalid pointer p_ctx %p p_data %p\n",
18544  __func__, __LINE__, p_ctx, p_data);
18545  return NI_RETCODE_INVALID_PARAM;
18546  }
18547 
18548  for (i = NVME_CORE; i< NUM_OF_CORES; i++)
18549  {
18550  ni_dump_log_single_core(p_ctx, p_data, i, gen_log_file);
18551  p_data = (void*)((uint8_t *)p_data + CPU_LOG_BUFFER_SIZE);
18552  }
18553 
18554  return NI_RETCODE_SUCCESS;
18555 }
18556 
18558  niFrameSurface1_t *source,
18559  uint64_t ui64DestAddr,
18560  uint32_t ui32FrameSize)
18561 {
18562  void *p_buffer = NULL;
18563  ni_retcode_t retval;
18564  uint32_t ui32LBA;
18565  uint8_t *p_data;
18566  uint16_t ui16Direction = NI_P2P_SEND;
18567 
18568  /* allocate memory aligned buffer */
18569  retval = ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN);
18570  if (retval != 0)
18571  {
18572  retval = NI_RETCODE_ERROR_MEM_ALOC;
18573  LRETURN;
18574  }
18575 
18576  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
18577 
18578  p_data = (uint8_t *) p_buffer;
18579 
18580  /* Add payload */
18581  memcpy(p_data, &ui64DestAddr, sizeof(uint64_t));
18582  memcpy(p_data + 8, &ui32FrameSize, sizeof(uint32_t));
18583  memcpy(p_data + 12, &source->ui16FrameIdx, sizeof(uint16_t));
18584  memcpy(p_data + 14, MAGIC_P2P_VALUE, 4);
18585  memcpy(p_data + 18, &ui16Direction, sizeof(uint16_t));
18586 
18588 
18589  retval = ni_nvme_send_write_cmd((ni_device_handle_t)(int64_t)source->device_handle,
18590  NI_INVALID_DEVICE_HANDLE, p_buffer,
18591  NI_DATA_BUFFER_LEN, ui32LBA);
18592 
18593  if (retval < 0)
18594  {
18595  ni_log2(p_ctx, NI_LOG_ERROR, "%s: NVME command failed %d\n", __func__, retval);
18596  ni_aligned_free(p_buffer);
18598  LRETURN;
18599  }
18600 
18601 END:
18602  if (p_buffer != NULL)
18603  ni_aligned_free(p_buffer);
18604  return retval;
18605 }
18606 
18608  ni_session_context_t *p_ctx,
18609  const ni_p2p_sgl_t *dmaAddrs,
18610  ni_frame_t *pDstFrame)
18611 {
18612  void *p_buffer = NULL;
18613  ni_retcode_t retval;
18614  uint32_t ui32LBA = 0;
18615  niFrameSurface1_t *pSurface;
18616  uint8_t *p_data;
18617  uint16_t ui16Direction = NI_P2P_RECV;
18618  uint32_t ui32Dummy = 0;
18619  int i;
18620 
18621  if (dmaAddrs->ui32NumEntries > NI_MAX_P2P_SGL_ENTRIES)
18622  {
18623  ni_log2(p_ctx, NI_LOG_ERROR, "Too many SGL entries\n");
18624  retval = NI_RETCODE_INVALID_PARAM;
18625  LRETURN;
18626  }
18627 
18628  /* allocate memory aligned buffer */
18629  retval = ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN);
18630  if (retval != 0)
18631  {
18632  retval = NI_RETCODE_ERROR_MEM_ALOC;
18633  LRETURN;
18634  }
18635 
18636  pSurface = (niFrameSurface1_t *) pDstFrame->p_data[3];
18637 
18638  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
18639 
18640  p_data = (uint8_t *) p_buffer;
18641 
18642  /* Add payload */
18643  memcpy(p_data, &dmaAddrs->ui64DMAAddr[0], 8); // dummy data, not used for p2p read
18644  memcpy(p_data + 8, &ui32Dummy, 4); // dummy data, not used for p2p read
18645  memcpy(p_data + 12, &pSurface->ui16FrameIdx, 2); // dummy data, not used for p2p read
18646  memcpy(p_data + 14, MAGIC_P2P_VALUE, 4);
18647  memcpy(p_data + 18, &ui16Direction, 2);
18648  memcpy(p_data + 20, &dmaAddrs->ui32NumEntries, 4);
18649 
18650  for (i = 0; i < dmaAddrs->ui32NumEntries; i++)
18651  {
18652  memcpy(&p_data[24] + (i*8), &dmaAddrs->ui64DMAAddr[i], 8);
18653  }
18654 
18655  for (i = 0; i < dmaAddrs->ui32NumEntries; i++)
18656  {
18657  memcpy(&p_data[24] + (dmaAddrs->ui32NumEntries * 8) + (i * 4), &dmaAddrs->ui32DMALen[i], 4);
18658  }
18659 
18661 
18662  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
18663  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
18664 
18665  if (retval < 0)
18666  {
18667  ni_log2(p_ctx, NI_LOG_ERROR, "%s: NVME command failed\n", __func__);
18668  ni_aligned_free(p_buffer);
18670  LRETURN;
18671  }
18672 
18673 END:
18674  if (p_buffer != NULL)
18675  ni_aligned_free(p_buffer);
18676  return retval;
18677 }
18678 
18679 int lower_pixel_rate(const ni_load_query_t *pQuery, uint32_t ui32CurrentLowest)
18680 {
18681  return (pQuery->total_pixel_load < ui32CurrentLowest) ? 1 : 0;
18682 }
_ni_t408_config_t::maxQpB
int32_t maxQpB
Definition: ni_device_api_priv.h:486
_ni_metadata_enc_bstream::ui16psnr_y
uint16_t ui16psnr_y
Definition: ni_device_api_priv.h:297
_ni_metadata_enc_bstream::metadata_size
uint32_t metadata_size
Definition: ni_device_api_priv.h:276
TUNE_BFRAME_VISUAL_MEDIUM
#define TUNE_BFRAME_VISUAL_MEDIUM
Definition: ni_device_api_priv.h:313
ni_decoder_output_config_t::ui8CropMode
uint8_t ui8CropMode
Definition: ni_device_api_priv.h:714
_ni_encoder_config_t::ui16rootBufId
uint16_t ui16rootBufId
Definition: ni_device_api_priv.h:560
_ni_xcoder_params::reconf_demo_mode
int reconf_demo_mode
Definition: ni_device_api.h:2761
_ni_encoder_config_t::ui16hdr10_dx2
uint16_t ui16hdr10_dx2
Definition: ni_device_api_priv.h:593
NI_RETCODE_PARAM_ERROR_CONF_WIN_BOT
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_BOT
Definition: ni_defs.h:479
NI_MAX_FRAMERATE
#define NI_MAX_FRAMERATE
Definition: ni_device_api.h:116
_ni_encoder_cfg_params::enable_mb_level_rc
int enable_mb_level_rc
Definition: ni_device_api.h:2398
_ni_encoder_cfg_params::frame_rate
int frame_rate
Definition: ni_device_api.h:2253
ni_decoder_output_picture_size::ui16Width
uint16_t ui16Width
Definition: ni_device_api_priv.h:708
_ni_encoder_config_t::ui8pastFrameMaxIntraRatio
uint8_t ui8pastFrameMaxIntraRatio
Definition: ni_device_api_priv.h:659
_ni_frame_config::picture_width
uint16_t picture_width
Definition: ni_device_api.h:2840
_ni_encoder_config_t::ui32hdr10_minluma
uint32_t ui32hdr10_minluma
Definition: ni_device_api_priv.h:598
NI_RETCODE_ERROR_VPU_RECOVERY
@ NI_RETCODE_ERROR_VPU_RECOVERY
Definition: ni_defs.h:527
_ni_xcoder_params::fps_denominator
uint32_t fps_denominator
Definition: ni_device_api.h:2749
_ni_session_config_rw::uHWAccessField
union _ni_session_config_rw::@20 uHWAccessField
_ni_metadata_enc_frame::force_pic_qp_b
uint16_t force_pic_qp_b
Definition: ni_device_api_priv.h:252
ni_decoder_output_config_t::ui8Force8Bit
uint8_t ui8Force8Bit
Definition: ni_device_api_priv.h:713
_ni_nvme_identity::fw_commit_hash
uint8_t fw_commit_hash[41]
Definition: ni_nvme.h:220
ni_lat_meas.h
Utility definitions for measuring frame/packet processing time in NETINT video processing devices.
_ni_encoder_config_t::u8skipFrameInterval
uint8_t u8skipFrameInterval
Definition: ni_device_api_priv.h:602
ni_send_to_target
ni_retcode_t ni_send_to_target(ni_session_context_t *p_ctx, niFrameSurface1_t *source, uint64_t ui64DestAddr, uint32_t ui32FrameSize)
Definition: ni_device_api_priv.c:18557
_ni_t408_config_t::saoEnable
int32_t saoEnable
Definition: ni_device_api_priv.h:401
_ni_xcoder_params::luma_linesize
int luma_linesize
Definition: ni_device_api.h:2814
ni_pthread_mutex_unlock
int ni_pthread_mutex_unlock(ni_pthread_mutex_t *mutex)
thread mutex unlock
Definition: ni_util.c:4083
NI_PIX_FMT_BGRA
@ NI_PIX_FMT_BGRA
Definition: ni_device_api.h:268
CLEAR_INSTANCE_BUF_W
#define CLEAR_INSTANCE_BUF_W(frame_id)
Definition: ni_nvme.h:876
_ni_t35_sei_mesg_type
_ni_t35_sei_mesg_type
Definition: ni_device_api_priv.c:87
_ni_encoder_config_t::u8customizeRoiQpLevel
uint8_t u8customizeRoiQpLevel
Definition: ni_device_api_priv.h:643
_ni_packet::psnr_v
double psnr_v
Definition: ni_device_api.h:2887
_ni_load_query::fw_p2p_mem_usage
uint32_t fw_p2p_mem_usage
Definition: ni_device_api.h:1213
_ni_sei_header::size
uint16_t size
Definition: ni_device_api.h:347
CONFIG_SESSION_KeepAliveTimeout_W
#define CONFIG_SESSION_KeepAliveTimeout_W(sid)
Definition: ni_nvme.h:880
ni_config_instance_flush
ni_retcode_t ni_config_instance_flush(ni_session_context_t *p_ctx, ni_device_type_t device_type)
Send a p_config command to flush the stream.
Definition: ni_device_api_priv.c:7015
NI_CODEC_FORMAT_JPEG
@ NI_CODEC_FORMAT_JPEG
Definition: ni_device_api.h:916
_ni_encoder_cfg_params::ver_offset
int ver_offset
Definition: ni_device_api.h:2412
ni_decoder_output_picture_size::ui16Height
uint16_t ui16Height
Definition: ni_device_api_priv.h:707
NI_QUADRA_MEMORY_CONFIG_DR
#define NI_QUADRA_MEMORY_CONFIG_DR
Definition: ni_device_api_priv.h:823
_ni_network_buffer_info::segment
ni_segment_t segment[NI_MAX_SEGMENT_NUM]
Definition: ni_device_api_priv.h:778
_ni_dec_mastering_display_colour_volume_bytes::display_primaries
uint16_t display_primaries[3][2]
Definition: ni_device_api.h:1065
_ni_nvme_identity::memory_cfg
uint8_t memory_cfg
Definition: ni_nvme.h:225
NI_P2P_SEND
#define NI_P2P_SEND
Definition: ni_device_api_priv.h:829
ni_device_get_ddr_configuration
ni_retcode_t ni_device_get_ddr_configuration(ni_session_context_t *p_ctx)
Get DDR configuration of Quadra device.
Definition: ni_device_api_priv.c:17985
ni_encoder_session_write
int ni_encoder_session_write(ni_session_context_t *p_ctx, ni_frame_t *p_frame)
Send a YUV p_frame to encoder.
Definition: ni_device_api_priv.c:4067
_ni_device_capability::fw_commit_time
uint8_t fw_commit_time[26]
Definition: ni_device_api.h:1168
_ni_t408_config_t::pu16IntraDcDeltaRate
int32_t pu16IntraDcDeltaRate
Definition: ni_device_api_priv.h:466
_ni_session_statistic_t::ui32InstErrors
uint32_t ui32InstErrors
Definition: ni_defs.h:396
NI_MAX_RESOLUTION_HEIGHT
#define NI_MAX_RESOLUTION_HEIGHT
Definition: ni_device_api.h:101
_ni_decoder_input_params_t::mcmode
int mcmode
Definition: ni_device_api.h:2523
_ni_t408_config_t::losslessEnable
int32_t losslessEnable
Definition: ni_device_api_priv.h:374
AI_MODEL_TYPE_HVSPLUS_FILTER
#define AI_MODEL_TYPE_HVSPLUS_FILTER
Definition: ni_device_api_priv.c:85
_ni_encoder_config_t::ui32ltrFirstGap
uint32_t ui32ltrFirstGap
Definition: ni_device_api_priv.h:569
NI_DEVICE_TYPE_ENCODER
@ NI_DEVICE_TYPE_ENCODER
Definition: ni_defs.h:359
_ni_t408_config_t::tier
int32_t tier
Definition: ni_device_api_priv.h:372
_ni_frame::separate_start
uint8_t separate_start
Definition: ni_device_api.h:2733
_ni_nvme_identity::xcoder_cnt
uint8_t xcoder_cnt[14]
Definition: ni_nvme.h:231
_ni_packet::p_custom_sei_set
ni_custom_sei_set_t * p_custom_sei_set
Definition: ni_device_api.h:2883
_ni_encoder_config_t::ui8enable2PassGopPatern
uint8_t ui8enable2PassGopPatern
Definition: ni_device_api_priv.h:612
INST_BUF_INFO_R_ACQUIRE
@ INST_BUF_INFO_R_ACQUIRE
Definition: ni_device_api_priv.h:53
_ni_session_statistic_t::ui32FramesErrorRatio
uint32_t ui32FramesErrorRatio
Definition: ni_defs.h:411
_ni_p2p_sgl_t
Definition: ni_defs.h:415
_ni_t408_config_t::strongIntraSmoothEnable
uint32_t strongIntraSmoothEnable
Definition: ni_device_api_priv.h:444
_ni_instance_mgr_general_status::fw_model_load
uint32_t fw_model_load
Definition: ni_device_api_priv.h:91
_ni_frame::buffer_size
uint32_t buffer_size
Definition: ni_device_api.h:2693
_ni_encoder_config_t::i32spatialLayerBitrate
int32_t i32spatialLayerBitrate[NI_MAX_SPATIAL_LAYERS]
Definition: ni_device_api_priv.h:661
_ni_network_data
Definition: ni_device_api.h:1365
NI_CC_SEI_BYTE0
#define NI_CC_SEI_BYTE0
Definition: ni_device_api.h:445
_ni_session_context::decoder_low_delay
int decoder_low_delay
Definition: ni_device_api.h:1596
_ni_decoder_input_params_t::error_ratio_threshold
int error_ratio_threshold
Definition: ni_device_api.h:2559
_ni_session_context::p_all_zero_buf
void * p_all_zero_buf
Definition: ni_device_api.h:1573
NI_HDR10P_SEI_BYTE1
#define NI_HDR10P_SEI_BYTE1
Definition: ni_device_api.h:455
_ni_session_context::required_buf_size
uint32_t required_buf_size
Definition: ni_device_api.h:1722
ni_config_instance_set_encoder_params
ni_retcode_t ni_config_instance_set_encoder_params(ni_session_context_t *p_ctx)
Send a p_config command to configure encoding parameters.
Definition: ni_device_api_priv.c:7213
_ni_session_context::auto_dl_handle
ni_device_handle_t auto_dl_handle
Definition: ni_device_api.h:1471
_ni_thread_arg_struct_t::p_mutex
ni_pthread_mutex_t * p_mutex
Definition: ni_device_api.h:1269
_ni_metadata_enc_bstream::gop_size
uint8_t gop_size
Definition: ni_device_api_priv.h:295
_ni_encoder_cfg_params::skip_frame_interval
int skip_frame_interval
Definition: ni_device_api.h:2433
NI_MIN_RESOLUTION_WIDTH
#define NI_MIN_RESOLUTION_WIDTH
Definition: ni_device_api.h:86
_ni_decoder_config_t::ui8MaxExtraHwFrameCnt
uint8_t ui8MaxExtraHwFrameCnt
Definition: ni_device_api_priv.h:738
NI_POOL_TYPE_P2P
@ NI_POOL_TYPE_P2P
Definition: ni_device_api.h:495
NI_H265_USERDATA_FLAG_MASTERING_COLOR_VOL
@ NI_H265_USERDATA_FLAG_MASTERING_COLOR_VOL
Definition: ni_device_api.h:371
NI_FW_ENC_BITSTREAM_META_DATA_SIZE
#define NI_FW_ENC_BITSTREAM_META_DATA_SIZE
Definition: ni_device_api_priv.h:838
XCODER_FRAME_OFFSET_DIFF_THRES
#define XCODER_FRAME_OFFSET_DIFF_THRES
Definition: ni_util.h:105
_ni_metadata_enc_bstream
Definition: ni_device_api_priv.h:274
_ni_session_config_rw::ui8Enable
uint8_t ui8Enable
Definition: ni_device_api_priv.h:70
_ni_hw_capability::codec_type
uint8_t codec_type
Definition: ni_device_api.h:1142
_ni_encoder_cfg_params::enable_dynamic_32x32_merge
int enable_dynamic_32x32_merge
Definition: ni_device_api.h:2366
_ni_encoder_cfg_params::hor_offset
int hor_offset
Definition: ni_device_api.h:2411
_ni_encoder_cfg_params::inLoopDSRatio
int inLoopDSRatio
Definition: ni_device_api.h:2349
_ni_session_context::max_retry_fail_count
int max_retry_fail_count[2]
Definition: ni_device_api.h:1703
_ni_metadata_enc_bstream_rev61::recycle_index
uint32_t recycle_index
Definition: ni_device_api_priv.h:270
_ni_network_buffer_info::ui16Option
uint16_t ui16Option
Definition: ni_device_api_priv.h:767
ni_config_session_rw
ni_retcode_t ni_config_session_rw(ni_session_context_t *p_ctx, ni_session_config_rw_type_t rw_type, uint8_t enable, uint8_t hw_action, uint16_t frame_id)
Configure the read/write pipe for a session to control its behavior.
Definition: ni_device_api_priv.c:6813
_ni_content_light_level_info_bytes
payload format of HDR SEI content light level info
Definition: ni_device_api.h:1076
ni_decoder_session_read_desc
ni_retcode_t ni_decoder_session_read_desc(ni_session_context_t *p_ctx, ni_frame_t *p_frame)
Retrieve a hw desc p_frame from decoder.
Definition: ni_device_api_priv.c:14295
_ni_encoder_config_t::ui8tuneBframeVisual
uint8_t ui8tuneBframeVisual
Definition: ni_device_api_priv.h:641
_ni_scaler_config
Definition: ni_device_api_priv.h:783
ni_frame_buffer_free
ni_retcode_t ni_frame_buffer_free(ni_frame_t *p_frame)
Free frame buffer that was previously allocated with either ni_frame_buffer_alloc or ni_encoder_frame...
Definition: ni_device_api.c:3562
QUERY_INSTANCE_UPLOAD_ID_R
#define QUERY_INSTANCE_UPLOAD_ID_R(sid, instance)
Definition: ni_nvme.h:738
_ni_nvme_identity::hw3_codec_format
uint8_t hw3_codec_format
Definition: ni_nvme.h:208
_ni_encoder_config_t::ui16HDR10AveLight
uint16_t ui16HDR10AveLight
Definition: ni_device_api_priv.h:544
NI_MAX_4K_FPS_QUADRA
#define NI_MAX_4K_FPS_QUADRA
Definition: ni_defs.h:249
_ni_t408_config_t::minQpP
int32_t minQpP
Definition: ni_device_api_priv.h:483
_ni_frame::vui_len
unsigned int vui_len
Definition: ni_device_api.h:2676
_ni_frame::sei_hdr_content_light_level_info_offset
unsigned int sei_hdr_content_light_level_info_offset
Definition: ni_device_api.h:2663
QUERY_SESSION_STATS_R
#define QUERY_SESSION_STATS_R(sid, instance)
Definition: ni_nvme.h:706
GC620_ABGR8888
#define GC620_ABGR8888
Definition: ni_device_api.h:203
_ni_encoder_frame_params::picture_type
uint16_t picture_type
Definition: ni_device_api_priv.h:201
_ni_decoder_config_t
Definition: ni_device_api_priv.h:728
_ni_instance_buf_info::hw_inst_ind
ni_instance_upload_ret_hwdesc_t hw_inst_ind
Definition: ni_device_api_priv.h:193
_ni_decoder_input_params_t::enable_ppu_scale_adapt
int enable_ppu_scale_adapt
Definition: ni_device_api.h:2546
_ni_encoder_cfg_params::pbRatio
float pbRatio
Definition: ni_device_api.h:2419
_ni_session_statistic_t::ui16ErrorCount
uint16_t ui16ErrorCount
Definition: ni_defs.h:399
_ni_encoder_config_t::i32tolCtbRcInter
int32_t i32tolCtbRcInter
Definition: ni_device_api_priv.h:576
ni_scaler_config_frame
ni_retcode_t ni_scaler_config_frame(ni_session_context_t *p_ctx, ni_frame_config_t *p_cfg)
config a frame in the scaler
Definition: ni_device_api_priv.c:5750
_ni_decoder_input_params_t::enable_out1
int enable_out1
Definition: ni_device_api.h:2521
ni_config_instance_sos
ni_retcode_t ni_config_instance_sos(ni_session_context_t *p_ctx, ni_device_type_t device_type)
Send a p_config command for Start Of Stream.
Definition: ni_device_api_priv.c:6906
_ni_network_layer_info::in_param
ni_network_layer_params_t * in_param
Definition: ni_device_api.h:1356
_ni_t408_config_t::disableDeblk
int32_t disableDeblk
Definition: ni_device_api_priv.h:396
_ni_packet::pts
long long pts
Definition: ni_device_api.h:2858
_ni_instance_mgr_stream_complete
Definition: ni_device_api_priv.h:176
_ni_encoder_cfg_params::vbvBufferReencode
int vbvBufferReencode
Definition: ni_device_api.h:2456
_ni_encoder_config_t::i32vbvBufferSize
int32_t i32vbvBufferSize
Definition: ni_device_api_priv.h:515
_ni_session_config_rw::ui16ReadFrameId
uint16_t ui16ReadFrameId
Definition: ni_device_api_priv.h:74
ni_instance_mgr_detail_status_v1_t
struct _ni_instance_mgr_detail_status_v1 ni_instance_mgr_detail_status_v1_t
_ni_t408_config_t::nrInterWeightY
uint32_t nrInterWeightY
Definition: ni_device_api_priv.h:431
QUERY_INSTANCE_WBUFF_SIZE_R_BY_EP
#define QUERY_INSTANCE_WBUFF_SIZE_R_BY_EP(sid, instance)
Definition: ni_nvme.h:726
_ni_encoder_cfg_params::enable_filler
int enable_filler
Definition: ni_device_api.h:2390
NI_RETCODE_PARAM_ERROR_RCENABLE
@ NI_RETCODE_PARAM_ERROR_RCENABLE
Definition: ni_defs.h:488
_ni_session_context::itu_t_t35_cc_sei_hdr_h264
uint8_t itu_t_t35_cc_sei_hdr_h264[NI_CC_SEI_HDR_H264_LEN]
Definition: ni_device_api.h:1420
_ni_t408_config_t::cu32MergeDeltaRate
int32_t cu32MergeDeltaRate
Definition: ni_device_api_priv.h:479
ni_rsrc_get_device_context
LIB_API ni_device_context_t * ni_rsrc_get_device_context(ni_device_type_t type, int guid)
Allocates and returns a pointer to ni_device_context_t struct based on provided device_type and guid....
NI_PARAM_AV1_MAX_AREA
#define NI_PARAM_AV1_MAX_AREA
Definition: ni_device_api.h:147
_ni_session_context::force_low_delay
bool force_low_delay
Definition: ni_device_api.h:1701
QUERY_INSTANCE_HW_OUT_SIZE_R
#define QUERY_INSTANCE_HW_OUT_SIZE_R(sid, instance)
Definition: ni_nvme.h:746
NI_CC_SEI_BYTE7
#define NI_CC_SEI_BYTE7
Definition: ni_device_api.h:452
_ni_instance_upload_ret_hwdesc::buffer_avail
int16_t buffer_avail
Definition: ni_device_api_priv.h:184
ni_config_instance_hvsplus
ni_retcode_t ni_config_instance_hvsplus(ni_session_context_t *p_ctx)
Definition: ni_device_api_priv.c:16278
ni_ai_alloc_dst_frame
ni_retcode_t ni_ai_alloc_dst_frame(ni_session_context_t *p_ctx, niFrameSurface1_t *p_out_surface)
Definition: ni_device_api_priv.c:17593
_ni_instance_mgr_general_status::fw_share_mem_usage
uint8_t fw_share_mem_usage
Definition: ni_device_api_priv.h:94
XCODER_MIN_ENC_PIC_WIDTH
#define XCODER_MIN_ENC_PIC_WIDTH
Definition: ni_util.h:112
NI_CC_SEI_BYTE3
#define NI_CC_SEI_BYTE3
Definition: ni_device_api.h:448
NI_RETCODE_NVME_SC_RESOURCE_UNAVAILABLE
@ NI_RETCODE_NVME_SC_RESOURCE_UNAVAILABLE
Definition: ni_defs.h:548
_ni_encoder_cfg_params::gopLowdelay
int gopLowdelay
Definition: ni_device_api.h:2338
CONFIG_SESSION_DDR_PRIORITY_W
#define CONFIG_SESSION_DDR_PRIORITY_W(sid)
Definition: ni_nvme.h:897
ni_timestamp_register
ni_retcode_t ni_timestamp_register(ni_queue_buffer_pool_t *p_buffer_pool, ni_timestamp_table_t *p_table, int64_t timestamp, uint64_t data_info)
Register timestamp in timestamp/frameoffset table.
Definition: ni_util.c:1373
_ni_nvme_identity::hw3_video_level
uint8_t hw3_video_level
Definition: ni_nvme.h:215
CLOSE_SESSION_R
#define CLOSE_SESSION_R(sid, instance)
Definition: ni_nvme.h:700
_ni_network_buffer_info::ui32IovecNum
uint32_t ui32IovecNum
Definition: ni_device_api_priv.h:776
ni_decode_cropping_rectangle::ui16W
uint16_t ui16W
Definition: ni_device_api_priv.h:702
_ni_frame_config::rectangle_x
int16_t rectangle_x
Definition: ni_device_api.h:2846
LBA_BIT_OFFSET
#define LBA_BIT_OFFSET
Definition: ni_nvme.h:624
_ni_encoder_config_t::ui32hdr10_maxluma
uint32_t ui32hdr10_maxluma
Definition: ni_device_api_priv.h:597
_ni_gop_rps::ref_pic_used
int ref_pic_used
Definition: ni_device_api.h:1963
_ni_load_query::current_load
uint32_t current_load
Definition: ni_device_api.h:1200
_ni_packet::ssim_u
double ssim_u
Definition: ni_device_api.h:2890
_ni_session_context::dev_xcoder_name
char dev_xcoder_name[MAX_CHAR_IN_DEVICE_NAME]
Definition: ni_device_api.h:1490
_ni_encoder_config_t::ui8sceneChangeDetectLevel
uint8_t ui8sceneChangeDetectLevel
Definition: ni_device_api_priv.h:646
_ni_packet::recycle_index
int recycle_index
Definition: ni_device_api.h:2866
_ni_load_query::total_contexts
uint32_t total_contexts
Definition: ni_device_api.h:1203
_ni_t408_config_t::cu08IntraDeltaRate
int32_t cu08IntraDeltaRate
Definition: ni_device_api_priv.h:471
_ni_session_context::frame_time_q
void * frame_time_q
Definition: ni_device_api.h:1415
_ni_network_buffer_info::ui16FrameIdx
uint16_t ui16FrameIdx[4]
Definition: ni_device_api_priv.h:772
NI_CC_SEI_BYTE2
#define NI_CC_SEI_BYTE2
Definition: ni_device_api.h:447
_ni_encoder_cfg_params::statistic_output_level
int statistic_output_level
Definition: ni_device_api.h:2430
_ni_dec_mastering_display_colour_volume_bytes::white_point_y
uint16_t white_point_y
Definition: ni_device_api.h:1067
_ni_session_statistic_t
Definition: ni_defs.h:386
_ni_metadata_enc_bstream::ssimY
uint32_t ssimY
Definition: ni_device_api_priv.h:284
ni_device_config_ns_qos
ni_retcode_t ni_device_config_ns_qos(ni_device_handle_t device_handle, uint32_t key, uint32_t value)
Send namespace num / Opmode and SRIOv index/value to the device with specified logic block address.
Definition: ni_device_api_priv.c:18347
_ni_encoder_config_t::ui16gdrDuration
uint16_t ui16gdrDuration
Definition: ni_device_api_priv.h:552
_ni_session_context::itu_t_t35_hdr10p_sei_hdr_h264
uint8_t itu_t_t35_hdr10p_sei_hdr_h264[NI_HDR10P_SEI_HDR_H264_LEN]
Definition: ni_device_api.h:1422
_ni_t408_config_t::numTicksPocDiffOne
uint32_t numTicksPocDiffOne
Definition: ni_device_api_priv.h:416
_ni_t408_config_t::enable_cu_level_rate_control
int32_t enable_cu_level_rate_control
Definition: ni_device_api_priv.h:405
_ni_dec_mastering_display_colour_volume_bytes
decoded payload format of HDR SEI mastering display colour volume
Definition: ni_device_api.h:1063
_ni_lat_meas_q_t
Definition: ni_lat_meas.h:39
_ni_decoder_input_params_t::ec_policy
int ec_policy
Definition: ni_device_api.h:2544
_ni_t408_config_t::customMDEnable
uint32_t customMDEnable
Definition: ni_device_api_priv.h:454
ni_device_api_priv.h
Private definitions used by ni_device_api.c for video processing tasks.
_ni_frame::metadata_buffer_size
uint32_t metadata_buffer_size
Definition: ni_device_api.h:2722
_ni_encoder_config_t::i8cuTreeFactor
int8_t i8cuTreeFactor
Definition: ni_device_api_priv.h:625
_ni_nvme_identity::xcoder_num_h265_encoder_hw
uint8_t xcoder_num_h265_encoder_hw
Definition: ni_nvme.h:163
_ni_decoder_input_params_t::skip_pts_guess
int skip_pts_guess
Definition: ni_device_api.h:2549
_ni_t408_config_t::dependSliceModeArg
int32_t dependSliceModeArg
Definition: ni_device_api_priv.h:387
_ni_t408_config_t::pu32IntraPlanarDeltaRate
int32_t pu32IntraPlanarDeltaRate
Definition: ni_device_api_priv.h:468
_ni_packet::psnr_u
double psnr_u
Definition: ni_device_api.h:2886
_ni_encoder_cfg_params::temporal_layers_enable
int temporal_layers_enable
Definition: ni_device_api.h:2406
ni_clear_instance_buf
ni_retcode_t ni_clear_instance_buf(niFrameSurface1_t *surface)
clear a particular xcoder instance buffer/data
Definition: ni_device_api_priv.c:14243
NI_NUM_OF_PIXELS_1080P
#define NI_NUM_OF_PIXELS_1080P
Definition: ni_device_api.h:77
_ni_session_context::fn
unsigned short fn
Definition: ni_device_api.h:1624
_ni_packet::flags
int flags
Definition: ni_device_api.h:2881
NI_DEVICE_TYPE_DECODER
@ NI_DEVICE_TYPE_DECODER
Definition: ni_defs.h:358
_niFrameSurface1::ui16height
uint16_t ui16height
Definition: ni_device_api.h:2828
NI_GET_MAX_HWDESC_P2P_BUF_ID
#define NI_GET_MAX_HWDESC_P2P_BUF_ID(x)
Definition: ni_defs.h:283
_ni_session_context::enc_pts_w_idx
int64_t enc_pts_w_idx
Definition: ni_device_api.h:1443
_ni_session_context::key_frame_type
int key_frame_type
Definition: ni_device_api.h:1527
_ni_encoder_config_t::ui32ltrNextInterval
uint32_t ui32ltrNextInterval
Definition: ni_device_api_priv.h:570
READ_INSTANCE_R
#define READ_INSTANCE_R(sid, instance)
Definition: ni_nvme.h:679
_ni_session_context::input_frame_fifo
ni_input_frame input_frame_fifo[120]
encoder:calculate PSNR start
Definition: ni_device_api.h:1711
_ni_encoder_config_t::ui8hrdEnable
uint8_t ui8hrdEnable
Definition: ni_device_api_priv.h:553
_ni_session_context::current_frame_delay
int current_frame_delay
Definition: ni_device_api.h:1695
_ni_encoder_config_t::i8crfMax
int8_t i8crfMax
Definition: ni_device_api_priv.h:620
ni_instance_mgr_general_status_t
struct _ni_instance_mgr_general_status ni_instance_mgr_general_status_t
_ni_session_context::session_id
uint32_t session_id
Definition: ni_device_api.h:1482
_ni_nvme_identity::hw0_codec_format
uint8_t hw0_codec_format
Definition: ni_nvme.h:169
ni_scaler_session_read_hwdesc
ni_retcode_t ni_scaler_session_read_hwdesc(ni_session_context_t *p_ctx, ni_frame_t *p_frame)
read a hardware descriptor from a scaler session
Definition: ni_device_api_priv.c:15794
_ni_t408_config_t::enable_hvs_qp
int32_t enable_hvs_qp
Definition: ni_device_api_priv.h:406
_ni_t408_config_t::avcIdrPeriod
int32_t avcIdrPeriod
Definition: ni_device_api_priv.h:489
_ni_xcoder_params::source_width
int source_width
Definition: ni_device_api.h:2753
_ni_packet::start_of_stream
uint32_t start_of_stream
Definition: ni_device_api.h:2862
NI_PARAM_AV1_MAX_WIDTH
#define NI_PARAM_AV1_MAX_WIDTH
Definition: ni_device_api.h:145
_ni_nvme_identity::hw0_video_profile
uint8_t hw0_video_profile
Definition: ni_nvme.h:175
_ni_frame::reconf_len
unsigned int reconf_len
Definition: ni_device_api.h:2681
NI_MAX_PACKET_SZ
#define NI_MAX_PACKET_SZ
Definition: ni_defs.h:239
_ni_t408_config_t
Definition: ni_device_api_priv.h:368
_ni_input_frame::usable
int8_t usable
Definition: ni_device_api.h:1407
_ni_decoder_input_params_t::enable_ppu_scale_limit
int enable_ppu_scale_limit
Definition: ni_device_api.h:2547
_ni_session_config_rw::ui16WriteFrameId
uint16_t ui16WriteFrameId
Definition: ni_device_api_priv.h:75
NI_SCALER_OPCODE_WATERMARK
@ NI_SCALER_OPCODE_WATERMARK
Definition: ni_defs.h:594
_ni_encoder_cfg_params::ltrRefInterval
int ltrRefInterval
Definition: ni_device_api.h:2299
_ni_encoder_config_t::i8enableipRatio
int8_t i8enableipRatio
Definition: ni_device_api_priv.h:601
ni_query_vf_ns_id
ni_retcode_t ni_query_vf_ns_id(ni_device_handle_t device_handle, ni_device_vf_ns_id_t *p_dev_ns_vf, uint8_t fw_rev[])
Query VF and NS id from device.
Definition: ni_device_api.c:11611
_ni_nvme_identity::hw0_id
uint8_t hw0_id
Definition: ni_nvme.h:166
_ni_device_capability::xcoder_devices
ni_hw_capability_t xcoder_devices[NI_MAX_DEVICES_PER_HW_INSTANCE]
Definition: ni_device_api.h:1161
_ni_nvme_identity::hw1_video_profile
uint8_t hw1_video_profile
Definition: ni_nvme.h:188
ni_config_instance_network_binary
ni_retcode_t ni_config_instance_network_binary(ni_session_context_t *p_ctx, void *nb_data, uint32_t nb_size)
Definition: ni_device_api_priv.c:16006
_ni_device_capability::fw_branch_name
uint8_t fw_branch_name[256]
Definition: ni_device_api.h:1167
NI_HDR10P_SEI_HDR_H264_LEN
#define NI_HDR10P_SEI_HDR_H264_LEN
Definition: ni_device_api.h:465
_ni_session_context::netint_fd
int netint_fd
Definition: ni_device_api.h:1620
_ni_hw_capability::hw_id
uint8_t hw_id
Definition: ni_device_api.h:1138
_ni_frame_config::frame_index
uint16_t frame_index
Definition: ni_device_api.h:2849
_ni_session_context::keyframe_factor
int keyframe_factor
Definition: ni_device_api.h:1532
_ni_scaler_config::scaler_param_c
uint16_t scaler_param_c
Definition: ni_device_api_priv.h:788
_ni_encoder_config_t::ui8crfMaxIframeEnable
uint8_t ui8crfMaxIframeEnable
Definition: ni_device_api_priv.h:637
OPT_2
@ OPT_2
Definition: ni_device_api_priv.c:96
NI_CODEC_HW_NONE
@ NI_CODEC_HW_NONE
Definition: ni_device_api.h:941
NI_PIX_FMT_YUV420P
@ NI_PIX_FMT_YUV420P
Definition: ni_device_api.h:263
_ni_uploader_config_t
Definition: ni_device_api_priv.h:664
_ni_encoder_config_t::ui16aspectRatioWidth
uint16_t ui16aspectRatioWidth
Definition: ni_device_api_priv.h:558
_ni_resolution
Definition: ni_device_api_priv.h:677
_ni_encoder_config_t::i32maxIntraSize
int32_t i32maxIntraSize
Definition: ni_device_api_priv.h:518
ni_device_type_t
ni_device_type_t
Definition: ni_defs.h:353
_niFrameSurface1::ui32nodeAddress
uint32_t ui32nodeAddress
Definition: ni_device_api.h:2829
_ni_session_context::isP2P
int32_t isP2P
Definition: ni_device_api.h:1619
_ni_packet::end_of_stream
uint32_t end_of_stream
Definition: ni_device_api.h:2861
_ni_encoder_cfg_params::enable_hvs_qp_scale
int enable_hvs_qp_scale
Definition: ni_device_api.h:2395
ni_frame_buffer_alloc
ni_retcode_t ni_frame_buffer_alloc(ni_frame_t *p_frame, int video_width, int video_height, int alignment, int metadata_flag, int factor, int hw_frame_count, int is_planar)
Allocate preliminary memory for the frame buffer based on provided parameters. Applicable to YUV420 P...
Definition: ni_device_api.c:2123
_ni_thread_arg_struct_t::close_thread
bool close_thread
Definition: ni_device_api.h:1264
_ni_session_context::p_leftover
uint8_t * p_leftover
Definition: ni_device_api.h:1508
_ni_metadata_enc_bstream_rev61::frame_tstamp
uint64_t frame_tstamp
Definition: ni_device_api_priv.h:267
_ni_t408_config_t::useLongTerm
uint32_t useLongTerm
Definition: ni_device_api_priv.h:440
NI_MAX_RESOLUTION_RGBA_WIDTH
#define NI_MAX_RESOLUTION_RGBA_WIDTH
Definition: ni_device_api.h:103
ni_decoder_output_config_t::sOutputPictureSize
ni_decoder_output_picture_size sOutputPictureSize
Definition: ni_device_api_priv.h:725
_ni_xcoder_params::enable2PassGop
int enable2PassGop
Definition: ni_device_api.h:2812
_ni_t408_config_t::roiEnable
int32_t roiEnable
Definition: ni_device_api_priv.h:412
_ni_device_capability::model_number
uint8_t model_number[40]
Definition: ni_device_api.h:1164
_ni_t408_config_t::cu16InterDeltaRate
int32_t cu16InterDeltaRate
Definition: ni_device_api_priv.h:475
_ni_packet::dts
long long dts
Definition: ni_device_api.h:2857
_ni_encoder_cfg_params::crf_max_iframe_enable
int crf_max_iframe_enable
Definition: ni_device_api.h:2437
WRITE_INSTANCE_W
#define WRITE_INSTANCE_W(sid, instance)
Definition: ni_nvme.h:673
ni_set_custom_template
void ni_set_custom_template(ni_session_context_t *p_ctx, ni_encoder_config_t *p_cfg, ni_xcoder_params_t *p_src)
Setup all xcoder configurations with custom parameters (Rev. B)
Definition: ni_device_api_priv.c:8494
NI_RETCODE_SUCCESS
@ NI_RETCODE_SUCCESS
Definition: ni_defs.h:439
_ni_session_context::active_video_width
uint32_t active_video_width
Definition: ni_device_api.h:1551
_ni_t408_config_t::gop_preset_index
int32_t gop_preset_index
Definition: ni_device_api_priv.h:376
_ni_t408_config_t::intraRefreshMode
int32_t intraRefreshMode
Definition: ni_device_api_priv.h:388
_ni_nvme_identity::ai8Mn
uint8_t ai8Mn[40]
Definition: ni_nvme.h:96
_ni_network_layer_params_t::num_of_dims
uint32_t num_of_dims
Definition: ni_device_api.h:1330
_ni_nvme_identity_xcoder_hw::hw_max_number_of_contexts
uint8_t hw_max_number_of_contexts
Definition: ni_nvme.h:75
_ni_network_layer_params_t::data_format
int32_t data_format
Definition: ni_device_api.h:1333
_ni_nvme_identity::hw3_codec_type
uint8_t hw3_codec_type
Definition: ni_nvme.h:209
_ni_metadata_common::crop_left
uint16_t crop_left
Definition: ni_device_api_priv.h:210
_ni_uploader_config_t::ui8Pool
uint8_t ui8Pool
Definition: ni_device_api_priv.h:670
_ni_gop_params::poc_offset
int poc_offset
Definition: ni_device_api.h:1968
_ni_gop_params::qp_offset
int qp_offset
Definition: ni_device_api.h:1969
_ni_framerate::framerate_denom
int32_t framerate_denom
Definition: ni_device_api.h:703
ni_resolution_t
struct _ni_resolution ni_resolution_t
NI_RETCODE_PARAM_ERROR_PIC_WIDTH
@ NI_RETCODE_PARAM_ERROR_PIC_WIDTH
Definition: ni_defs.h:492
NI_DEVICE_TYPE_UPLOAD
@ NI_DEVICE_TYPE_UPLOAD
Definition: ni_defs.h:365
_ni_scaler_config::numInputs
uint8_t numInputs
Definition: ni_device_api_priv.h:786
QUERY_INSTANCE_AI_INFO_R
#define QUERY_INSTANCE_AI_INFO_R(sid, instance)
Definition: ni_nvme.h:713
_ni_decoder_input_params_t::enable_user_data_sei_passthru
int enable_user_data_sei_passthru
Definition: ni_device_api.h:2541
NI_CC_SEI_BYTE1
#define NI_CC_SEI_BYTE1
Definition: ni_device_api.h:446
_ni_t408_config_t::hvs_qp_scale
int32_t hvs_qp_scale
Definition: ni_device_api_priv.h:407
_ni_session_context::pts_correction_last_dts
int64_t pts_correction_last_dts
Definition: ni_device_api.h:1445
ni_ai_session_open
ni_retcode_t ni_ai_session_open(ni_session_context_t *p_ctx)
Definition: ni_device_api_priv.c:17159
_ni_session_context::status
int status
Definition: ni_device_api.h:1526
_ni_decoder_config_t::ui8EcPolicy
uint8_t ui8EcPolicy
Definition: ni_device_api_priv.h:741
_ni_session_context::domain
unsigned short domain
Definition: ni_device_api.h:1621
_ni_session_context::is_dec_pkt_512_aligned
int is_dec_pkt_512_aligned
Definition: ni_device_api.h:1461
_ni_session_context::pool_type
ni_frame_pool_type_t pool_type
Definition: ni_device_api.h:1699
_ni_t408_config_t::nrNoiseSigmaY
uint32_t nrNoiseSigmaY
Definition: ni_device_api_priv.h:436
NI_INVALID_SESSION_ID
#define NI_INVALID_SESSION_ID
Definition: ni_device_api.h:111
_ni_t408_config_t::maxQpI
int32_t maxQpI
Definition: ni_device_api_priv.h:482
_ni_t408_config_t::level
int32_t level
Definition: ni_device_api_priv.h:371
_ni_encoder_cfg_params::enable_ssim
int enable_ssim
Definition: ni_device_api.h:2401
ni_network_layer_params_t
struct _ni_network_layer_params_t ni_network_layer_params_t
_ni_session_context::param_err_msg
char param_err_msg[512]
Definition: ni_device_api.h:1530
_ni_device_capability::xcoder_cnt
uint8_t xcoder_cnt[NI_DEVICE_TYPE_XCODER_MAX]
Definition: ni_device_api.h:1160
_ni_session_context::ori_chroma_linesize
int ori_chroma_linesize
Definition: ni_device_api.h:1683
_ni_encoder_cfg_params::enable_compensate_qp
int enable_compensate_qp
Definition: ni_device_api.h:2451
_ni_instance_mgr_general_status::active_sub_instances_cnt_overall
uint8_t active_sub_instances_cnt_overall
Definition: ni_device_api_priv.h:101
_ni_network_layer_offset
Definition: ni_device_api.h:1360
_ni_frame::total_start_len
uint32_t total_start_len
Definition: ni_device_api.h:2731
_ni_session_context::last_bitrate
int32_t last_bitrate
Definition: ni_device_api.h:1676
current_time
struct timeval current_time
Definition: ni_p2p_read_test.c:80
_ni_network_buffer_info::ui16Height
uint16_t ui16Height
Definition: ni_device_api_priv.h:766
_ni_input_frame::video_width
uint32_t video_width
Definition: ni_device_api.h:1402
_ni_session_context::pts_correction_num_faulty_dts
int pts_correction_num_faulty_dts
Definition: ni_device_api.h:1444
ni_xcoder_session_query_detail
int ni_xcoder_session_query_detail(ni_session_context_t *p_ctx, ni_device_type_t device_type, void *detail_data, int ver)
Query current xcoder status.
Definition: ni_device_api_priv.c:3204
GC620_ARGB8888
#define GC620_ARGB8888
Definition: ni_device_api.h:204
_ni_session_context::actual_video_width
uint32_t actual_video_width
Definition: ni_device_api.h:1555
ni_config_instance_set_sequence_change
ni_retcode_t ni_config_instance_set_sequence_change(ni_session_context_t *p_ctx, ni_device_type_t device_type, ni_resolution_t *p_resolution)
Send a p_config command to inform encoder sequence change.
Definition: ni_device_api_priv.c:7147
ni_gettime_ns
uint64_t ni_gettime_ns(void)
Definition: ni_util.c:1998
_ni_encoder_config_t::ui8compressor
uint8_t ui8compressor
Definition: ni_device_api_priv.h:604
_ni_encoder_cfg_params::chromaQpOffset
int chromaQpOffset
Definition: ni_device_api.h:2342
ni_send_session_keep_alive
ni_retcode_t ni_send_session_keep_alive(uint32_t session_id, ni_device_handle_t device_handle, ni_event_handle_t event_handle, void *p_data)
send a keep alive message to firmware
Definition: ni_device_api_priv.c:1472
_ni_frame::iovec
ni_iovec_t * iovec
Definition: ni_device_api.h:2740
_ni_encoder_config_t::ui8colorPrimaries
uint8_t ui8colorPrimaries
Definition: ni_device_api_priv.h:555
NI_PIX_FMT_BGR0
@ NI_PIX_FMT_BGR0
Definition: ni_device_api.h:271
_ni_t408_config_t::fixedBitRatio
int32_t fixedBitRatio[NI_MAX_GOP_NUM]
Definition: ni_device_api_priv.h:404
_ni_session_config_rw
Definition: ni_device_api_priv.h:68
_ni_encoder_config_t::i32qcomp
int32_t i32qcomp
Definition: ni_device_api_priv.h:621
_ni_instance_mgr_general_status::process_load_percent
uint8_t process_load_percent
Definition: ni_device_api_priv.h:82
_ni_frameclone_desc
Definition: ni_device_api.h:1377
NI_MIN_RESOLUTION_HEIGHT_SCALER
#define NI_MIN_RESOLUTION_HEIGHT_SCALER
Definition: ni_device_api.h:95
ni_query_eos
int ni_query_eos(ni_session_context_t *p_ctx, ni_device_type_t device_type, ni_instance_mgr_stream_complete_t *p_stream_complete)
Query a particular xcoder instance to get End of Output data.
Definition: ni_device_api_priv.c:6411
NI_EC_POLICY_LIMITED_ERROR
#define NI_EC_POLICY_LIMITED_ERROR
Definition: ni_device_api.h:324
_ni_xcoder_params::rootBufId
int rootBufId
Definition: ni_device_api.h:2805
_ni_encoder_config_t::ui32ltrRefInterval
uint32_t ui32ltrRefInterval
Definition: ni_device_api_priv.h:567
OPT_3
@ OPT_3
Definition: ni_device_api_priv.c:97
_ni_t408_config_t::pu04DeltaRate
int32_t pu04DeltaRate
Definition: ni_device_api_priv.h:455
_ni_ddr_priority_config::ddr_mode
uint8_t ddr_mode
Definition: ni_device_api_priv.h:795
_ni_frame::p_start_buffer
uint8_t * p_start_buffer
Definition: ni_device_api.h:2728
_ni_session_context::event_handle
ni_event_handle_t event_handle
Definition: ni_device_api.h:1576
_ni_encoder_config_t::i16bitrateWindow
int16_t i16bitrateWindow
Definition: ni_device_api_priv.h:578
QUERY_INSTANCE_WBUFF_SIZE_BUSY_R
#define QUERY_INSTANCE_WBUFF_SIZE_BUSY_R(sid, instance)
Definition: ni_nvme.h:733
QUERY_INSTANCE_ACQUIRE_BUF
#define QUERY_INSTANCE_ACQUIRE_BUF(sid, instance)
Definition: ni_nvme.h:740
NI_XCODER_FAILURES_MAX
#define NI_XCODER_FAILURES_MAX
Definition: ni_device_api_priv.c:169
ni_set_custom_dec_template
void ni_set_custom_dec_template(ni_session_context_t *p_ctx, ni_decoder_config_t *p_cfg, ni_xcoder_params_t *p_src, uint32_t max_pkt_size)
Setup all xcoder configurations with custom parameters (Rev. B)
Definition: ni_device_api_priv.c:8248
NI_XCODER_REVISION
#define NI_XCODER_REVISION
Definition: ni_defs.h:98
QUERY_DETAIL_GET_STATUS_R
#define QUERY_DETAIL_GET_STATUS_R(instance)
Definition: ni_nvme.h:784
_ni_t408_config_t::maxQpP
int32_t maxQpP
Definition: ni_device_api_priv.h:484
_ni_device_capability::hw_elements_cnt
uint8_t hw_elements_cnt
Definition: ni_device_api.h:1158
_ni_nvme_identity::xcoder_num_h265_decoder_hw
uint8_t xcoder_num_h265_decoder_hw
Definition: ni_nvme.h:162
ni_config_instance_set_scaler_params
ni_retcode_t ni_config_instance_set_scaler_params(ni_session_context_t *p_ctx, ni_scaler_params_t *p_params)
Send a p_config command to configure scaling parameters.
Definition: ni_device_api_priv.c:5360
ni_instance_mgr_detail_status_t
struct _ni_instance_mgr_detail_status ni_instance_mgr_detail_status_t
_ni_frame::crop_top
uint32_t crop_top
Definition: ni_device_api.h:2639
ni_check_common_params
ni_retcode_t ni_check_common_params(ni_t408_config_t *p_param, ni_xcoder_params_t *p_src, char *p_param_err, uint32_t max_err_len)
Definition: ni_device_api_priv.c:12686
_ni_encoder_cfg_params::enable_acq_limit
int enable_acq_limit
Definition: ni_device_api.h:2443
_ni_encoder_cfg_params::noMbtree
int noMbtree
Definition: ni_device_api.h:2415
_ni_t408_config_t::nrNoiseSigmaCb
uint32_t nrNoiseSigmaCb
Definition: ni_device_api_priv.h:437
_ni_scaler_config::scaler_param_b
uint16_t scaler_param_b
Definition: ni_device_api_priv.h:787
ni_scaler_multi_config_frame
ni_retcode_t ni_scaler_multi_config_frame(ni_session_context_t *p_ctx, ni_frame_config_t p_cfg_in[], int numInCfgs, ni_frame_config_t *p_cfg_out)
config multiple frames in the scaler
Definition: ni_device_api_priv.c:5872
_ni_session_context::last_dts
int64_t last_dts
Definition: ni_device_api.h:1440
_ni_session_context::blk_io_handle
ni_device_handle_t blk_io_handle
Definition: ni_device_api.h:1467
ni_config_read_inout_layers
ni_retcode_t ni_config_read_inout_layers(ni_session_context_t *p_ctx, ni_network_data_t *p_network)
Definition: ni_device_api_priv.c:16886
ni_timestamp_get_with_threshold
ni_retcode_t ni_timestamp_get_with_threshold(ni_timestamp_table_t *p_table, uint64_t frame_info, int64_t *p_timestamp, int32_t threshold, int32_t print, ni_queue_buffer_pool_t *p_buffer_pool)
Definition: ni_util.c:1423
_ni_instance_mgr_general_status::process_load_percent_upper
uint8_t process_load_percent_upper
Definition: ni_device_api_priv.h:99
NI_XCODER_QUADRA
@ NI_XCODER_QUADRA
Definition: ni_defs.h:345
ni_lat_meas_q_add_entry
void * ni_lat_meas_q_add_entry(ni_lat_meas_q_t *frame_time_q, uint64_t abs_time, int64_t ts_time)
Add a new entry to latency queue.
Definition: ni_lat_meas.c:181
_ni_frame::start_len
uint32_t start_len[NI_MAX_NUM_DATA_POINTERS]
Definition: ni_device_api.h:2730
OPEN_SESSION_CODEC
#define OPEN_SESSION_CODEC(instance, codec, param)
Definition: ni_nvme.h:693
NI_HDR10P_SEI_HDR_HEVC_LEN
#define NI_HDR10P_SEI_HDR_HEVC_LEN
Definition: ni_device_api.h:464
_ni_segment::ui32RelLba
uint32_t ui32RelLba
Definition: ni_device_api_priv.h:758
_ni_network_buffer_info::ui8PoolSize
uint8_t ui8PoolSize
Definition: ni_device_api_priv.h:770
NI_RETCODE_PARAM_ERROR_AREA_TOO_BIG
@ NI_RETCODE_PARAM_ERROR_AREA_TOO_BIG
Definition: ni_defs.h:514
NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_32X32_EN
@ NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_32X32_EN
Definition: ni_defs.h:500
_ni_encoder_cfg_params::roi_enable
int roi_enable
Definition: ni_device_api.h:2271
_ni_session_context::frame_pkt_offset
uint64_t frame_pkt_offset
Definition: ni_device_api.h:1649
_ni_encoder_cfg_params::pastFrameMaxIntraRatio
int pastFrameMaxIntraRatio
Definition: ni_device_api.h:2461
NI_MAX_DEVICES_PER_HW_INSTANCE
#define NI_MAX_DEVICES_PER_HW_INSTANCE
Definition: ni_defs.h:251
_ni_encoder_cfg_params::intra_period
int intra_period
Definition: ni_device_api.h:2274
_ni_session_context::pts_correction_last_pts
int64_t pts_correction_last_pts
Definition: ni_device_api.h:1447
_ni_decoder_config_t::ui32ErrRatioThreshold
uint32_t ui32ErrRatioThreshold
Definition: ni_device_api_priv.h:747
_ni_encoder_cfg_params::EnableRdoQuant
int EnableRdoQuant
Definition: ni_device_api.h:2316
_ni_encoder_cfg_params::spatialLayerBitrate
int spatialLayerBitrate[NI_MAX_SPATIAL_LAYERS]
Definition: ni_device_api.h:2463
_ni_frame_config::rectangle_height
uint16_t rectangle_height
Definition: ni_device_api.h:2845
_ni_decoder_input_params_t::min_packets_delay
bool min_packets_delay
Definition: ni_device_api.h:2552
_ni_nvme_identity::ui16Vid
uint16_t ui16Vid
Definition: ni_nvme.h:93
_ni_frame::separate_metadata
uint8_t separate_metadata
Definition: ni_device_api.h:2724
_ni_nvme_identity::hw3_video_profile
uint8_t hw3_video_profile
Definition: ni_nvme.h:214
_ni_encoder_cfg_params::enable_pic_skip
int enable_pic_skip
Definition: ni_device_api.h:2384
_ni_network_data::outset
ni_network_layer_offset_t * outset
Definition: ni_device_api.h:1373
_ni_network_perf_metrics
Definition: ni_device_api.h:1386
_ni_hw_capability::max_4k_fps
uint8_t max_4k_fps
Definition: ni_device_api.h:1140
_ni_t408_config_t::betaOffsetDiv2
int32_t betaOffsetDiv2
Definition: ni_device_api_priv.h:398
_ni_encoder_cfg_params::use_recommend_enc_params
int use_recommend_enc_params
Definition: ni_device_api.h:2360
_ni_thread_arg_struct_t::p_buffer
void * p_buffer
Definition: ni_device_api.h:1268
_ni_packet::frame_type
uint32_t frame_type
Definition: ni_device_api.h:2865
_ni_encoder_config_t::ui8disableBframeRDOQ
uint8_t ui8disableBframeRDOQ
Definition: ni_device_api_priv.h:639
_ni_encoder_config_t::ui8planarFormat
uint8_t ui8planarFormat
Definition: ni_device_api_priv.h:561
CONFIG_INSTANCE_SetEncRoiQpMap_W
#define CONFIG_INSTANCE_SetEncRoiQpMap_W(sid, instance)
Definition: ni_nvme.h:844
_ni_hw_capability::min_video_height
uint16_t min_video_height
Definition: ni_device_api.h:1146
_ni_load_query::fw_share_mem_usage
uint32_t fw_share_mem_usage
Definition: ni_device_api.h:1212
ni_query_session_statistic_info
ni_retcode_t ni_query_session_statistic_info(ni_session_context_t *p_ctx, ni_device_type_t device_type, ni_session_statistic_t *p_session_statistic)
Query a particular xcoder session to get session statistics.
Definition: ni_device_api_priv.c:6579
_ni_gop_params::pic_type
int pic_type
Definition: ni_device_api.h:1972
ni_metadata_enc_bstream_t
struct _ni_metadata_enc_bstream ni_metadata_enc_bstream_t
QUERY_INSTANCE_WBUFF_SIZE_R
#define QUERY_INSTANCE_WBUFF_SIZE_R(sid, instance)
Definition: ni_nvme.h:724
_ni_encoder_config_t::ui8LookAheadDepth
uint8_t ui8LookAheadDepth
Definition: ni_device_api_priv.h:540
_ni_encoder_cfg_params::min_qp
int min_qp
Definition: ni_device_api.h:2380
_ni_encoder_config_t::i32userMinDeltaQp
int32_t i32userMinDeltaQp
Definition: ni_device_api_priv.h:520
_ni_frame::error_ratio
uint32_t error_ratio
Definition: ni_device_api.h:2736
_ni_encoder_cfg_params::gop_preset_index
int gop_preset_index
Definition: ni_device_api.h:2265
_ni_instance_mgr_stream_info::pix_format
uint8_t pix_format
Definition: ni_device_api_priv.h:130
_ni_encoder_config_t::ui16hdr10_dy2
uint16_t ui16hdr10_dy2
Definition: ni_device_api_priv.h:594
_ni_frame::sei_user_data_unreg_len
unsigned int sei_user_data_unreg_len
Definition: ni_device_api.h:2670
_ni_encoder_cfg_params::max_qp
int max_qp
Definition: ni_device_api.h:2381
ni_scaler_alloc_frame
ni_retcode_t ni_scaler_alloc_frame(ni_session_context_t *p_ctx, int width, int height, int format, int options, int rectangle_width, int rectangle_height, int rectangle_x, int rectangle_y, int rgba_color, int frame_index)
allocate a frame in the scaler
Definition: ni_device_api_priv.c:5513
_ni_frame_config
Definition: ni_device_api.h:2838
_ni_encoder_cfg_params::ipRatio
float ipRatio
Definition: ni_device_api.h:2418
_ni_session_statistic_t::ui32FramesCorrupted
uint32_t ui32FramesCorrupted
Definition: ni_defs.h:410
NI_SCALER_FLAG_IO
#define NI_SCALER_FLAG_IO
Definition: ni_device_api.h:292
ni_decoder_output_config_t::ui8Enabled
uint8_t ui8Enabled
Definition: ni_device_api_priv.h:712
_ni_encoder_config_t::ui8av1ErrResilientMode
uint8_t ui8av1ErrResilientMode
Definition: ni_device_api_priv.h:606
_ni_session_context::force_frame_type
int force_frame_type
Definition: ni_device_api.h:1544
_ni_encoder_cfg_params::blockRCSize
int blockRCSize
Definition: ni_device_api.h:2350
NI_SCALER_OPCODE_IPOVLY
@ NI_SCALER_OPCODE_IPOVLY
Definition: ni_defs.h:593
ni_pthread_cond_signal
int ni_pthread_cond_signal(ni_pthread_cond_t *cond)
signal a condition
Definition: ni_util.c:4251
_ni_encoder_config_t::i32meBlkMode
int32_t i32meBlkMode
Definition: ni_device_api_priv.h:512
_ni_instance_buf_info
Definition: ni_device_api_priv.h:188
_ni_t408_config_t::nrNoiseEstEnable
uint32_t nrNoiseEstEnable
Definition: ni_device_api_priv.h:435
_ni_lat_meas_q_t::last_benchmark_time
uint64_t last_benchmark_time
Definition: ni_lat_meas.h:42
_ni_session_statistic_t::ui32WrBufAvailSize
uint32_t ui32WrBufAvailSize
Definition: ni_defs.h:388
_ni_xcoder_params::roi_demo_mode
int roi_demo_mode
Definition: ni_device_api.h:2760
_ni_sei_header::status
uint8_t status
Definition: ni_device_api.h:346
_niFrameSurface1::src_cpu
int8_t src_cpu
Definition: ni_device_api.h:2834
_ni_network_perf_metrics::total_idle_cycles
uint32_t total_idle_cycles
Definition: ni_device_api.h:1389
_ni_metadata_enc_bstream::frameCropTopOffset
uint16_t frameCropTopOffset
Definition: ni_device_api_priv.h:302
_ni_session_context::src_endian
int src_endian
Definition: ni_device_api.h:1495
_ni_session_context::low_delay_sync_mutex
ni_pthread_mutex_t low_delay_sync_mutex
Definition: ni_device_api.h:1667
_ni_encoder_config_t::i32userQpMin
int32_t i32userQpMin
Definition: ni_device_api_priv.h:521
_ni_encoder_cfg_params::intra_mb_refresh_mode
int intra_mb_refresh_mode
Definition: ni_device_api.h:2275
nvme_config_xcoder_config_set_sequence_change
@ nvme_config_xcoder_config_set_sequence_change
Definition: ni_nvme.h:436
_ni_encoder_config_t::ui8lowLatencyMode
uint8_t ui8lowLatencyMode
Definition: ni_device_api_priv.h:527
_ni_encoder_config_t::ui8spatialLayersRefBaseLayer
uint8_t ui8spatialLayersRefBaseLayer
Definition: ni_device_api_priv.h:653
NI_MIN_RESOLUTION_WIDTH_SCALER
#define NI_MIN_RESOLUTION_WIDTH_SCALER
Definition: ni_device_api.h:94
DOWNLOAD_FRAMEIDX_R
#define DOWNLOAD_FRAMEIDX_R(frame_id)
Definition: ni_nvme.h:682
_ni_frame::orignal_pts
long long orignal_pts
Definition: ni_device_api.h:2735
NI_RETCODE_PARAM_ERROR_CONF_WIN_TOP
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_TOP
Definition: ni_defs.h:477
_ni_encoder_config_t::ui8totalCuTreeDepth
uint8_t ui8totalCuTreeDepth
Definition: ni_device_api_priv.h:655
_ni_encoder_cfg_params::vbv_buffer_size
int vbv_buffer_size
Definition: ni_device_api.h:2391
_ni_t408_config_t::nrIntraWeightCb
uint32_t nrIntraWeightCb
Definition: ni_device_api_priv.h:429
_ni_nvme_identity::device_is_xcoder
uint8_t device_is_xcoder
Definition: ni_nvme.h:155
ni_decoder_output_config_t::ui8ScaleResCeil
uint8_t ui8ScaleResCeil
Definition: ni_device_api_priv.h:723
_ni_frame::sei_total_len
unsigned int sei_total_len
Definition: ni_device_api.h:2655
_ni_t408_config_t::conf_win_right
int32_t conf_win_right
Definition: ni_device_api_priv.h:383
INST_BUF_INFO_RW_WRITE_BUSY
@ INST_BUF_INFO_RW_WRITE_BUSY
Definition: ni_device_api_priv.h:52
_ni_session_context::pts_offsets
int64_t pts_offsets[NI_FIFO_SZ]
Definition: ni_device_api.h:1451
_ni_frame::p_buffer
uint8_t * p_buffer
Definition: ni_device_api.h:2692
NUM_OF_CORES
@ NUM_OF_CORES
Definition: ni_defs.h:383
_ni_metadata_enc_bstream::max_mv_x
int16_t max_mv_x[2]
Definition: ni_device_api_priv.h:288
_ni_encoder_config_t::i32ipRatio
int32_t i32ipRatio
Definition: ni_device_api_priv.h:627
_ni_encoder_cfg_params::noHWMultiPassSupport
int noHWMultiPassSupport
Definition: ni_device_api.h:2416
_ni_session_statistic_t::ui32FramesCompleted
uint32_t ui32FramesCompleted
Definition: ni_defs.h:393
_ni_encoder_cfg_params::gdrDuration
int gdrDuration
Definition: ni_device_api.h:2298
_ni_packet::psnr_y
double psnr_y
Definition: ni_device_api.h:2885
NI_RETCODE_PARAM_ERROR_CONF_WIN_L
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_L
Definition: ni_defs.h:481
_ni_xcoder_params::video_full_range_flag
int video_full_range_flag
Definition: ni_device_api.h:2791
NI_FIFO_SZ
#define NI_FIFO_SZ
Definition: ni_defs.h:303
_ni_frame::dts
long long dts
Definition: ni_device_api.h:2632
_ni_decoder_input_params_t::force_low_delay
bool force_low_delay
Definition: ni_device_api.h:2539
_ni_metadata_common::crop_right
uint16_t crop_right
Definition: ni_device_api_priv.h:211
NI_EC_POLICY_BEST_EFFORT_OUT_DC
#define NI_EC_POLICY_BEST_EFFORT_OUT_DC
Definition: ni_device_api.h:325
_ni_encoder_cfg_params::aspectRatioWidth
int aspectRatioWidth
Definition: ni_device_api.h:2254
NI_RETCODE_INVALID_PARAM
@ NI_RETCODE_INVALID_PARAM
Definition: ni_defs.h:441
_ni_metadata_enc_bstream::reconLumaWidth
uint16_t reconLumaWidth
Definition: ni_device_api_priv.h:300
ni_decoder_session_write
int ni_decoder_session_write(ni_session_context_t *p_ctx, ni_packet_t *p_packet)
Send a video p_packet to decoder.
Definition: ni_device_api_priv.c:1795
ni_ai_session_write
ni_retcode_t ni_ai_session_write(ni_session_context_t *p_ctx, ni_frame_t *p_frame)
Definition: ni_device_api_priv.c:16426
_ni_encoder_config_t::ui8avccHvcc
uint8_t ui8avccHvcc
Definition: ni_device_api_priv.h:652
_ni_frame::pkt_pos
uint64_t pkt_pos
Definition: ni_device_api.h:2725
ni_buffer_pool_free
void ni_buffer_pool_free(ni_queue_buffer_pool_t *p_buffer_pool)
Definition: ni_util.c:789
NI_AV1_INVALID_BUFFER_INDEX
#define NI_AV1_INVALID_BUFFER_INDEX
Definition: ni_device_api.h:179
_ni_session_context::buffered_frame_index
int16_t buffered_frame_index
Definition: ni_device_api.h:1719
_ni_encoder_config_t::i32picHeight
int32_t i32picHeight
Definition: ni_device_api_priv.h:511
_ni_device_capability::fw_commit_hash
uint8_t fw_commit_hash[41]
Definition: ni_device_api.h:1169
_ni_frame::crop_left
uint32_t crop_left
Definition: ni_device_api.h:2641
_ni_metadata_enc_frame::inconsecutive_transfer
uint8_t inconsecutive_transfer
Definition: ni_device_api_priv.h:258
_ni_nvme_identity::xcoder_num_devices
uint8_t xcoder_num_devices
Definition: ni_nvme.h:230
_ni_metadata_enc_bstream::ssimV
uint32_t ssimV
Definition: ni_device_api_priv.h:286
_ni_instance_mgr_stream_info
Definition: ni_device_api_priv.h:122
_ni_packet::ssim_y
double ssim_y
Definition: ni_device_api.h:2889
_ni_encoder_cfg_params::maxFrameSizeRatio
int maxFrameSizeRatio
Definition: ni_device_api.h:2258
_ni_decoder_input_params_t::enable_advanced_ec
int enable_advanced_ec
Definition: ni_device_api.h:2545
_ni_t408_config_t::bgThrMeanDiff
uint32_t bgThrMeanDiff
Definition: ni_device_api_priv.h:449
_ni_encoder_config_t::ui8adaptiveCuTree
uint8_t ui8adaptiveCuTree
Definition: ni_device_api_priv.h:656
_ni_encoder_cfg_params::HDR10AveLight
int HDR10AveLight
Definition: ni_device_api.h:2285
_ni_encoder_config_t::ui32verOffset
uint32_t ui32verOffset
Definition: ni_device_api_priv.h:618
_ni_encoder_cfg_params::rc
struct _ni_encoder_cfg_params::@16 rc
ni_decoder_session_read
int ni_decoder_session_read(ni_session_context_t *p_ctx, ni_frame_t *p_frame)
Retrieve a YUV p_frame from decoder.
Definition: ni_device_api_priv.c:2289
_ni_decoder_config_t::ui8HWFrame
uint8_t ui8HWFrame
Definition: ni_device_api_priv.h:730
_ni_encoder_cfg_params::cu_size_mode
int cu_size_mode
Definition: ni_device_api.h:2362
ni_config_instance_set_write_len
ni_retcode_t ni_config_instance_set_write_len(ni_session_context_t *p_ctx, ni_device_type_t device_type, uint32_t len)
Send a p_config command to set the length for the incoming write packet.
Definition: ni_device_api_priv.c:7077
_ni_decoder_config_t::ui16MaxSeiDataSize
uint16_t ui16MaxSeiDataSize
Definition: ni_device_api_priv.h:732
ni_query_session_stats
ni_retcode_t ni_query_session_stats(ni_session_context_t *p_ctx, ni_device_type_t device_type, ni_session_stats_t *p_session_stats, int rc, int opcode)
Query a particular session to get the stats info.
Definition: ni_device_api_priv.c:6235
_ni_encoder_cfg_params::hrdEnable
int hrdEnable
Definition: ni_device_api.h:2317
_ni_encoder_config_t::i32bitRate
int32_t i32bitRate
Definition: ni_device_api_priv.h:522
_ni_t408_config_t::pu04IntraPlanarDeltaRate
int32_t pu04IntraPlanarDeltaRate
Definition: ni_device_api_priv.h:459
SwapSWBytes
void SwapSWBytes(uint8_t *buf, uint32_t bytes)
Definition: ni_device_api_priv.c:178
_ni_frame_config::session_id
uint16_t session_id
Definition: ni_device_api.h:2850
_ni_encoder_config_t::ui8AiEnhanceLevel
uint8_t ui8AiEnhanceLevel
Definition: ni_device_api_priv.h:619
_ni_t408_config_t::pu32IntraAngleDeltaRate
int32_t pu32IntraAngleDeltaRate
Definition: ni_device_api_priv.h:470
NI_PIX_FMT_8_TILED4X4
@ NI_PIX_FMT_8_TILED4X4
Definition: ni_device_api.h:276
_ni_session_context::bit_depth_factor
int bit_depth_factor
Definition: ni_device_api.h:1496
_ni_session_statistic_t::ui32FramesBuffered
uint32_t ui32FramesBuffered
Definition: ni_defs.h:392
_ni_uploader_config_t::ui32chromaLinesize
uint32_t ui32chromaLinesize
Definition: ni_device_api_priv.h:673
_ni_encoder_config_t
Definition: ni_device_api_priv.h:507
ni_lat_meas_q_check_latency
uint64_t ni_lat_meas_q_check_latency(ni_lat_meas_q_t *frame_time_q, uint64_t abs_time, int64_t ts_time)
Check latency of a frame referenced by its timestamp.
Definition: ni_lat_meas.c:201
_ni_metadata_common::has_b_frame
uint8_t has_b_frame
Definition: ni_device_api_priv.h:222
_ni_encoder_cfg_params::encMallocStrategy
int encMallocStrategy
Definition: ni_device_api.h:2449
_ni_encoder_config_t::ui32setLongTermInterval
uint32_t ui32setLongTermInterval
Definition: ni_device_api_priv.h:573
ni_network_layer_info_t
struct _ni_network_layer_info ni_network_layer_info_t
CONFIG_INSTANCE_SetAiFrm_W
#define CONFIG_INSTANCE_SetAiFrm_W(sid, instance)
Definition: ni_nvme.h:856
ni_decoder_output_config_t::ui8ScaleEnabled
uint8_t ui8ScaleEnabled
Definition: ni_device_api_priv.h:715
_ni_frame::force_pic_qp
uint16_t force_pic_qp
Definition: ni_device_api.h:2685
_ni_encoder_cfg_params::high_tier
int high_tier
Definition: ni_device_api.h:2358
_ni_session_context::mutex
ni_pthread_mutex_t mutex
Definition: ni_device_api.h:1567
_ni_encoder_cfg_params::max_num_merge
int max_num_merge
Definition: ni_device_api.h:2363
_ni_gop_params::num_ref_pics
int num_ref_pics
Definition: ni_device_api.h:1973
_ni_decoder_input_params_t::crop_mode
int crop_mode[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2527
ni_config_instance_set_uploader_params
ni_retcode_t ni_config_instance_set_uploader_params(ni_session_context_t *p_ctx, uint32_t pool_size, uint32_t pool)
Send a p_config command to configure uploading parameters.
Definition: ni_device_api_priv.c:15564
_ni_encoder_cfg_params::crf
int crf
Definition: ni_device_api.h:2283
_ni_session_context::hw_id
int hw_id
Definition: ni_device_api.h:1480
ni_dec_fme_buffer_pool_free
void ni_dec_fme_buffer_pool_free(ni_buf_pool_t *p_buffer_pool)
Definition: ni_util.c:735
_ni_encoder_config_t::i8pass1Qp
int8_t i8pass1Qp
Definition: ni_device_api_priv.h:633
_ni_hw_capability::video_level
uint8_t video_level
Definition: ni_device_api.h:1148
NI_MAX_DEC_SESSION_READ_QUERY_EOS_RETRIES
#define NI_MAX_DEC_SESSION_READ_QUERY_EOS_RETRIES
Definition: ni_device_api_priv.h:833
_ni_network_layer_params_t
Definition: ni_device_api.h:1328
_ni_encoder_config_t::ui8fixedframerate
uint8_t ui8fixedframerate
Definition: ni_device_api_priv.h:605
NVME_CORE
@ NVME_CORE
Definition: ni_defs.h:378
_ni_encoder_config_t::ui8mallocStrategy
uint8_t ui8mallocStrategy
Definition: ni_device_api_priv.h:647
_ni_encoder_config_t::ui8colorTrc
uint8_t ui8colorTrc
Definition: ni_device_api_priv.h:556
_ni_session_context::ori_width
int ori_width
Definition: ni_device_api.h:1564
_ni_xcoder_params::fps_number
uint32_t fps_number
Definition: ni_device_api.h:2748
QUERY_INSTANCE_RBUFF_SIZE_R
#define QUERY_INSTANCE_RBUFF_SIZE_R(sid, instance)
Definition: ni_nvme.h:722
MOTION_CONSTRAINED_QUALITY_MODE
#define MOTION_CONSTRAINED_QUALITY_MODE
Definition: ni_device_api.h:486
_ni_session_context::ddr_config
uint8_t ddr_config
Definition: ni_device_api.h:1628
_ni_t408_config_t::bgLambdaQp
uint32_t bgLambdaQp
Definition: ni_device_api_priv.h:450
_ni_nvme_identity::hw3_id
uint8_t hw3_id
Definition: ni_nvme.h:205
ni_cmp_fw_api_ver
int ni_cmp_fw_api_ver(const char ver1[], const char ver2[])
Compare two 3 character strings containing a FW API version. Handle comparision when FW API version f...
Definition: ni_util.c:3674
_ni_xcoder_params::hdrEnableVUI
int hdrEnableVUI
Definition: ni_device_api.h:2775
_ni_network_buffer_info
Definition: ni_device_api_priv.h:762
NI_HDR10P_SEI_BYTE5
#define NI_HDR10P_SEI_BYTE5
Definition: ni_device_api.h:459
_ni_load_query::fw_video_shared_mem_usage
uint32_t fw_video_shared_mem_usage
Definition: ni_device_api.h:1209
_ni_scaler_params_t
Definition: ni_device_api.h:2587
_ni_t408_config_t::conf_win_bottom
int32_t conf_win_bottom
Definition: ni_device_api_priv.h:381
_ni_nvme_identity::hw0_video_level
uint8_t hw0_video_level
Definition: ni_nvme.h:176
nvme_config_xcoder_config_set_write_legth
@ nvme_config_xcoder_config_set_write_legth
Definition: ni_nvme.h:434
NI_RETCODE_PARAM_ERROR_CU_LVL_RC_EN
@ NI_RETCODE_PARAM_ERROR_CU_LVL_RC_EN
Definition: ni_defs.h:467
_ni_t408_config_t::use_recommend_enc_params
int32_t use_recommend_enc_params
Definition: ni_device_api_priv.h:390
_ni_device_capability::fw_build_id
uint8_t fw_build_id[256]
Definition: ni_device_api.h:1171
CONFIG_GLOBAL_NAMESPACE_NUM
#define CONFIG_GLOBAL_NAMESPACE_NUM
Definition: ni_nvme.h:892
_ni_p2p_sgl_t::ui64DMAAddr
uint64_t ui64DMAAddr[NI_MAX_P2P_SGL_ENTRY]
Definition: ni_defs.h:419
NI_RETCODE_PARAM_ERROR_HEIGHT_TOO_BIG
@ NI_RETCODE_PARAM_ERROR_HEIGHT_TOO_BIG
Definition: ni_defs.h:510
_ni_timestamp_table_t::list
ni_queue_t list
Definition: ni_device_api.h:1325
_ni_metadata_enc_frame::start_len
uint16_t start_len[3]
Definition: ni_device_api_priv.h:257
NI_RETCODE_PARAM_ERROR_MX_QP
@ NI_RETCODE_PARAM_ERROR_MX_QP
Definition: ni_defs.h:473
_ni_encoder_cfg_params::enable_timecode
int enable_timecode
Definition: ni_device_api.h:2453
STD_AV1
@ STD_AV1
Definition: ni_device_api_priv.h:363
NI_RETCODE_ERROR_MEM_ALOC
@ NI_RETCODE_ERROR_MEM_ALOC
Definition: ni_defs.h:443
ni_session_config_rw_type_t
ni_session_config_rw_type_t
Definition: ni_device_api_priv.h:40
ni_retcode_t
ni_retcode_t
Definition: ni_defs.h:437
_ni_metadata_enc_frame::use_cur_src_as_long_term_pic
uint8_t use_cur_src_as_long_term_pic
Definition: ni_device_api_priv.h:254
ni_populate_device_capability_struct
void ni_populate_device_capability_struct(ni_device_capability_t *p_cap, void *p_data, ni_device_handle_t device_handle, bool device_in_ctxt)
Get info from received xcoder capability.
Definition: ni_device_api_priv.c:7904
_ni_load_query::fw_model_load
uint32_t fw_model_load
Definition: ni_device_api.h:1201
_ni_metadata_enc_bstream::inter_total_count
uint16_t inter_total_count
Definition: ni_device_api_priv.h:293
_ni_t408_config_t::forcedHeaderEnable
uint32_t forcedHeaderEnable
Definition: ni_device_api_priv.h:504
_ni_instance_mgr_general_status::ui32UploadRate
uint32_t ui32UploadRate
Definition: ni_device_api_priv.h:109
_ni_nvme_identity::xcoder_num_h264_encoder_hw
uint8_t xcoder_num_h264_encoder_hw
Definition: ni_nvme.h:161
CONFIG_INSTANCE_Flush_W
#define CONFIG_INSTANCE_Flush_W(sid, instance)
Definition: ni_nvme.h:806
SESSION_RUN_STATE_SEQ_CHANGE_DRAINING
@ SESSION_RUN_STATE_SEQ_CHANGE_DRAINING
Definition: ni_device_api.h:1180
NI_FW_META_DATA_SZ
#define NI_FW_META_DATA_SZ
Definition: ni_defs.h:318
NI_CC_SEI_HDR_H264_LEN
#define NI_CC_SEI_HDR_H264_LEN
Definition: ni_device_api.h:466
_ni_decoder_config_t::ui32SourceHeight
uint32_t ui32SourceHeight
Definition: ni_device_api_priv.h:746
ni_nvme_send_write_cmd
int32_t ni_nvme_send_write_cmd(ni_device_handle_t handle, ni_event_handle_t event_handle, void *p_data, uint32_t data_len, uint32_t lba)
Compose a io write command.
Definition: ni_nvme.c:660
_ni_session_context::fw_rev
uint8_t fw_rev[8]
Definition: ni_device_api.h:1627
nvme_cmd_xcoder_read
@ nvme_cmd_xcoder_read
Definition: ni_defs.h:580
_ni_network_data::output_num
uint32_t output_num
Definition: ni_device_api.h:1368
_ni_encoder_cfg_params::intra_mb_refresh_arg
int intra_mb_refresh_arg
Definition: ni_device_api.h:2276
XCODER_MAX_NUM_TEMPORAL_LAYER
#define XCODER_MAX_NUM_TEMPORAL_LAYER
Definition: ni_util.h:108
SESSION_RUN_STATE_NORMAL
@ SESSION_RUN_STATE_NORMAL
Definition: ni_device_api.h:1179
_ni_metadata_enc_frame::force_pic_qp_i
uint16_t force_pic_qp_i
Definition: ni_device_api_priv.h:250
nvme_cmd_xcoder_write
@ nvme_cmd_xcoder_write
Definition: ni_defs.h:579
_ni_session_context::decoder_last_drop_frame_num
uint32_t decoder_last_drop_frame_num
Definition: ni_device_api.h:1725
ni_packet_buffer_alloc
ni_retcode_t ni_packet_buffer_alloc(ni_packet_t *p_packet, int packet_size)
Allocate memory for the packet buffer based on provided packet size.
Definition: ni_device_api.c:3714
NI_MAX_REF_PIC
#define NI_MAX_REF_PIC
Definition: ni_device_api.h:59
_ni_session_context::pts_correction_num_faulty_pts
int pts_correction_num_faulty_pts
Definition: ni_device_api.h:1446
_ni_encoder_cfg_params::pps_init_qp
int pps_init_qp
Definition: ni_device_api.h:2421
_ni_session_context::pkt_pos
uint64_t pkt_pos[NI_FIFO_SZ]
Definition: ni_device_api.h:1455
ni_ai_session_query_metrics
ni_retcode_t ni_ai_session_query_metrics(ni_session_context_t *p_ctx, ni_network_perf_metrics_t *p_metrics)
Definition: ni_device_api_priv.c:18273
NI_RETCODE_PARAM_INVALID_VALUE
@ NI_RETCODE_PARAM_INVALID_VALUE
Definition: ni_defs.h:449
MAGIC_P2P_VALUE
#define MAGIC_P2P_VALUE
Definition: ni_device_api_priv.c:84
ni_instance_buf_info_t
struct _ni_instance_buf_info ni_instance_buf_info_t
_ni_frame::sei_hdr_mastering_display_color_vol_offset
unsigned int sei_hdr_mastering_display_color_vol_offset
Definition: ni_device_api.h:2661
_ni_frame::sei_cc_offset
unsigned int sei_cc_offset
Definition: ni_device_api.h:2658
_ni_packet::pkt_pos
uint64_t pkt_pos
Definition: ni_device_api.h:2860
_ni_encoder_cfg_params::gopSize
int gopSize
Definition: ni_device_api.h:2337
ni_session_stats_t
struct _ni_session_stats ni_session_stats_t
_ni_session_context::average_psnr
double average_psnr
Definition: ni_device_api.h:1715
ni_decode_cropping_rectangle::ui16X
uint16_t ui16X
Definition: ni_device_api_priv.h:700
_ni_frame_config::options
uint16_t options
Definition: ni_device_api.h:2843
_ni_session_context::framerate
ni_framerate_t framerate
Definition: ni_device_api.h:1606
_ni_nvme_identity::fw_build_time
uint8_t fw_build_time[26]
Definition: ni_nvme.h:221
_ni_encoder_config_t::ui8preIntraHandling
uint8_t ui8preIntraHandling
Definition: ni_device_api_priv.h:657
ni_decoder_session_flush
ni_retcode_t ni_decoder_session_flush(ni_session_context_t *p_ctx)
Flush decoder output.
Definition: ni_device_api_priv.c:1569
_ni_session_context::pext_mutex
ni_pthread_mutex_t * pext_mutex
Definition: ni_device_api.h:1672
_ni_frame::iovec_num
uint32_t iovec_num
Definition: ni_device_api.h:2739
_ni_frame_config::picture_format
uint16_t picture_format
Definition: ni_device_api.h:2842
_ni_p2p_sgl_t::ui32NumEntries
uint32_t ui32NumEntries
Definition: ni_defs.h:417
_ni_thread_arg_struct_t::device_handle
ni_device_handle_t device_handle
Definition: ni_device_api.h:1266
_ni_decoder_input_params_t::crop_whxy
int crop_whxy[NI_MAX_NUM_OF_DECODER_OUTPUTS][4]
Definition: ni_device_api.h:2528
NI_RETCODE_PARAM_ERROR_VBV_BUFFER_SIZE
@ NI_RETCODE_PARAM_ERROR_VBV_BUFFER_SIZE
Definition: ni_defs.h:456
_ni_device_vf_ns_id::ns_id
uint16_t ns_id
Definition: ni_rsrc_api.h:232
_ni_input_frame::pts
int64_t pts
Definition: ni_device_api.h:1405
SwapSW16
void SwapSW16(uint16_t *buf, uint32_t bytes)
Definition: ni_device_api_priv.c:189
_ni_encoder_cfg_params::qlevel
int qlevel
Definition: ni_device_api.h:2332
NI_MAX_SPATIAL_LAYERS
#define NI_MAX_SPATIAL_LAYERS
Definition: ni_device_api.h:180
_ni_encoder_config_t::ui8rcEnable
uint8_t ui8rcEnable
Definition: ni_device_api_priv.h:524
NI_MAX_CONTEXTS_PER_HW_INSTANCE
#define NI_MAX_CONTEXTS_PER_HW_INSTANCE
Definition: ni_defs.h:248
_ni_session_statistic_t::ui32FramesInput
uint32_t ui32FramesInput
Definition: ni_defs.h:391
_ni_custom_gop_params::custom_gop_size
int custom_gop_size
Definition: ni_device_api.h:2059
XCODER_TEST_RECONF_OFF
@ XCODER_TEST_RECONF_OFF
Definition: ni_device_api.h:1748
_ni_session_statistic_t::ui32LastTransactionCompletionStatus
uint32_t ui32LastTransactionCompletionStatus
Definition: ni_defs.h:401
_ni_t408_config_t::internalBitDepth
int32_t internalBitDepth
Definition: ni_device_api_priv.h:373
_ni_t408_config_t::conf_win_top
int32_t conf_win_top
Definition: ni_device_api_priv.h:380
NI_PARAM_MAX_HEIGHT
#define NI_PARAM_MAX_HEIGHT
Definition: ni_device_api.h:137
_niFrameSurface1::ui16FrameIdx
uint16_t ui16FrameIdx
Definition: ni_device_api.h:2825
_ni_encoder_config_t::i8chromaQpOffset
int8_t i8chromaQpOffset
Definition: ni_device_api_priv.h:575
ni_encoder_session_open
ni_retcode_t ni_encoder_session_open(ni_session_context_t *p_ctx)
Open a xcoder encoder instance.
Definition: ni_device_api_priv.c:3237
_ni_encoder_cfg_params::HDR10dx2
int HDR10dx2
Definition: ni_device_api.h:2292
_ni_session_statistic_t::ui32LastErrorStatus
uint32_t ui32LastErrorStatus
Definition: ni_defs.h:403
_ni_encoder_config_t::i32cplxDecay
int32_t i32cplxDecay
Definition: ni_device_api_priv.h:629
_ni_metadata_enc_frame
Definition: ni_device_api_priv.h:240
_ni_encoder_config_t::ui8enableCompensateQp
uint8_t ui8enableCompensateQp
Definition: ni_device_api_priv.h:649
ni_decoder_output_config_t::ui8EnablePpuScaleLimit
uint8_t ui8EnablePpuScaleLimit
Definition: ni_device_api_priv.h:722
_ni_metadata_enc_frame::force_pic_qp_p
uint16_t force_pic_qp_p
Definition: ni_device_api_priv.h:251
_ni_instance_mgr_stream_info::transfer_frame_height
uint16_t transfer_frame_height
Definition: ni_device_api_priv.h:127
_ni_session_context::psnr_v
double psnr_v
Definition: ni_device_api.h:1714
NI_LOG_INFO
@ NI_LOG_INFO
Definition: ni_log.h:61
_ni_encoder_config_t::i8intraQpDelta
int8_t i8intraQpDelta
Definition: ni_device_api_priv.h:564
_ni_frameclone_desc::ui16DstIdx
uint16_t ui16DstIdx
Definition: ni_device_api.h:1380
_ni_uploader_config_t::ui8PixelFormat
uint8_t ui8PixelFormat
Definition: ni_device_api_priv.h:669
ni_hwframe_clone
ni_retcode_t ni_hwframe_clone(ni_session_context_t *p_ctx, ni_frameclone_desc_t *p_frameclone_desc)
Copy a src hw frame to a dst hw frame.
Definition: ni_device_api_priv.c:15473
NI_AI_FLAG_IO
#define NI_AI_FLAG_IO
Definition: ni_device_api.h:301
ni_timestamp_scan_cleanup
void ni_timestamp_scan_cleanup(ni_timestamp_table_t *pts_list, ni_timestamp_table_t *dts_list, ni_queue_buffer_pool_t *p_buffer_pool)
Definition: ni_util.c:1431
CONFIG_SESSION_Read_W
#define CONFIG_SESSION_Read_W(sid)
Definition: ni_nvme.h:871
DP_LOG_OFFSET_IN_4K
#define DP_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:662
_ni_metadata_dec_frame
Definition: ni_device_api_priv.h:227
_ni_metadata_enc_frame::use_long_term_ref
uint8_t use_long_term_ref
Definition: ni_device_api_priv.h:255
_ni_nvme_identity
Definition: ni_nvme.h:89
CONFIG_INSTANCE_SetPktSize_W
#define CONFIG_INSTANCE_SetPktSize_W(sid, instance)
Definition: ni_nvme.h:840
_ni_encoder_cfg_params::HDR10wx
int HDR10wx
Definition: ni_device_api.h:2294
_ni_session_context::io_event
ni_io_event_t * io_event
Definition: ni_device_api.h:1730
_ni_xcoder_params::source_height
int source_height
Definition: ni_device_api.h:2758
_ni_metadata_common::frame_tstamp
uint64_t frame_tstamp
Definition: ni_device_api_priv.h:217
CONFIG_INSTANCE_SetSeqChange_W
#define CONFIG_INSTANCE_SetSeqChange_W(sid, instance)
Definition: ni_nvme.h:842
_ni_encoder_config_t::i32srcBitDepth
int32_t i32srcBitDepth
Definition: ni_device_api_priv.h:525
_ni_encoder_cfg_params::rcQpDeltaRange
int rcQpDeltaRange
Definition: ni_device_api.h:2354
_ni_nvme_identity::hw1_codec_format
uint8_t hw1_codec_format
Definition: ni_nvme.h:182
ni_decoder_frame_buffer_alloc
ni_retcode_t ni_decoder_frame_buffer_alloc(ni_buf_pool_t *p_pool, ni_frame_t *p_frame, int alloc_mem, int video_width, int video_height, int alignment, int factor, int is_planar)
Allocate memory for decoder frame buffer based on provided parameters; the memory is retrieved from a...
Definition: ni_device_api.c:2586
_ni_metadata_enc_bstream::avg_frame_qp
uint32_t avg_frame_qp
Definition: ni_device_api_priv.h:280
_ni_encoder_cfg_params::hvsBaseMbComplexity
int hvsBaseMbComplexity
Definition: ni_device_api.h:2425
_ni_metadata_enc_bstream::ssimU
uint32_t ssimU
Definition: ni_device_api_priv.h:285
NI_LOG_ERROR
@ NI_LOG_ERROR
Definition: ni_log.h:60
_ni_encoder_config_t::i8statisticOutputLevel
int8_t i8statisticOutputLevel
Definition: ni_device_api_priv.h:636
_ni_metadata_enc_frame::enc_reconfig_data_size
uint32_t enc_reconfig_data_size
Definition: ni_device_api_priv.h:246
IS_XCODER_DEVICE_TYPE
#define IS_XCODER_DEVICE_TYPE(t)
Definition: ni_defs.h:422
_ni_encoder_config_t::ui8setLongTermCount
uint8_t ui8setLongTermCount
Definition: ni_device_api_priv.h:585
_ni_encoder_config_t::i32tolCtbRcIntra
int32_t i32tolCtbRcIntra
Definition: ni_device_api_priv.h:577
_ni_session_context::src_bit_depth
int src_bit_depth
Definition: ni_device_api.h:1494
_ni_frame::ni_pict_type
ni_pic_type_t ni_pict_type
Definition: ni_device_api.h:2653
_ni_encoder_config_t::i32bitRateBL
int32_t i32bitRateBL
Definition: ni_device_api_priv.h:523
_ni_nvme_identity::hw2_id
uint8_t hw2_id
Definition: ni_nvme.h:192
_ni_session_stats
Definition: ni_device_api_priv.h:136
NI_MAX_RESOLUTION_WIDTH
#define NI_MAX_RESOLUTION_WIDTH
Definition: ni_device_api.h:100
_ni_framerate::framerate_num
int32_t framerate_num
Definition: ni_device_api.h:700
DP_CORE
@ DP_CORE
Definition: ni_defs.h:380
_ni_packet::ssim_v
double ssim_v
Definition: ni_device_api.h:2891
ni_scaler_session_open
int ni_scaler_session_open(ni_session_context_t *p_ctx)
Open a xcoder scaler instance.
Definition: ni_device_api_priv.c:5062
_ni_encoder_config_t::ui8stillImageDetectLevel
uint8_t ui8stillImageDetectLevel
Definition: ni_device_api_priv.h:645
_ni_instance_mgr_general_status::process_load_percent_overall
uint8_t process_load_percent_overall
Definition: ni_device_api_priv.h:100
_ni_encoder_config_t::i8skipFrameEnable
int8_t i8skipFrameEnable
Definition: ni_device_api_priv.h:599
GOP_PRESET_IDX_DEFAULT
@ GOP_PRESET_IDX_DEFAULT
Definition: ni_device_api_priv.h:318
nvme_admin_cmd_xcoder_open
@ nvme_admin_cmd_xcoder_open
Definition: ni_nvme.h:318
NI_QUADRA_MEMORY_CONFIG_SR_4G
#define NI_QUADRA_MEMORY_CONFIG_SR_4G
Definition: ni_device_api_priv.h:826
_ni_encoder_cfg_params::HDR10maxluma
int HDR10maxluma
Definition: ni_device_api.h:2296
_ni_decoder_config_t::ui8ReduceDpbDelay
uint8_t ui8ReduceDpbDelay
Definition: ni_device_api_priv.h:744
_ni_metadata_enc_bstream::ui8StillImage
uint8_t ui8StillImage
Definition: ni_device_api_priv.h:306
_ni_network_data::linfo
ni_network_layer_info_t linfo
Definition: ni_device_api.h:1369
_niFrameSurface1::ui16session_ID
uint16_t ui16session_ID
Definition: ni_device_api.h:2826
NI_SCALER_OPCODE_OVERLAY
@ NI_SCALER_OPCODE_OVERLAY
Definition: ni_defs.h:589
NI_RETCODE_ERROR_UNSUPPORTED_FW_VERSION
@ NI_RETCODE_ERROR_UNSUPPORTED_FW_VERSION
Definition: ni_defs.h:533
_ni_instance_buf_info::buf_avail_size
uint32_t buf_avail_size
Definition: ni_device_api_priv.h:192
_ni_xcoder_params::cfg_enc_params
ni_encoder_cfg_params_t cfg_enc_params
Definition: ni_device_api.h:2795
_ni_session_context::last_pkt_pos
uint64_t last_pkt_pos
Definition: ni_device_api.h:1456
_ni_t408_config_t::skipIntraTrans
int32_t skipIntraTrans
Definition: ni_device_api_priv.h:400
_ni_encoder_cfg_params::vbv_max_rate
int vbv_max_rate
Definition: ni_device_api.h:2392
_ni_gop_params::qp_factor
float qp_factor
Definition: ni_device_api.h:1970
_ni_encoder_config_t::ui16aspectRatioHeight
uint16_t ui16aspectRatioHeight
Definition: ni_device_api_priv.h:559
ni_instance_buf_info_rw_type_t
ni_instance_buf_info_rw_type_t
Definition: ni_device_api_priv.h:46
check_err_rc_option_t
check_err_rc_option_t
Definition: ni_device_api_priv.c:93
_ni_xcoder_params::force_frame_type
int force_frame_type
Definition: ni_device_api.h:2772
_ni_gop_rps::ref_pic
int ref_pic
Definition: ni_device_api.h:1962
NI_PIX_FMT_NV12
@ NI_PIX_FMT_NV12
Definition: ni_device_api.h:265
_ni_encoder_config_t::ui8fillerEnable
uint8_t ui8fillerEnable
Definition: ni_device_api_priv.h:565
NI_P2P_RECV
#define NI_P2P_RECV
Definition: ni_device_api_priv.h:828
ni_query_general_status
int ni_query_general_status(ni_session_context_t *p_ctx, ni_device_type_t device_type, ni_instance_mgr_general_status_t *p_gen_status)
Query a particular xcoder instance to get GeneralStatus data.
Definition: ni_device_api_priv.c:6002
_ni_nvme_identity_xcoder_hw::hw_codec_type
uint8_t hw_codec_type
Definition: ni_nvme.h:78
_ni_session_stats::ui32LastTransactionCompletionStatus
uint32_t ui32LastTransactionCompletionStatus
Definition: ni_device_api_priv.h:141
NI_H265_USERDATA_FLAG_UNREGISTERED_PRE
@ NI_H265_USERDATA_FLAG_UNREGISTERED_PRE
Definition: ni_device_api.h:367
_ni_frame::data_len
uint32_t data_len[NI_MAX_NUM_DATA_POINTERS]
Definition: ni_device_api.h:2690
ni_decoder_session_copy_internal
ni_retcode_t ni_decoder_session_copy_internal(ni_session_context_t *src_p_ctx, ni_session_context_t *dst_p_ctx)
Copy a xcoder decoder worker thread info.
Definition: ni_device_api_priv.c:13563
NI_MAX_SLICE_SIZE
#define NI_MAX_SLICE_SIZE
Definition: ni_device_api.h:220
_ni_session_context::pixel_format_changed
int pixel_format_changed
Definition: ni_device_api.h:1707
_ni_frame::extra_data_len
unsigned int extra_data_len
Definition: ni_device_api.h:2683
DP_IPC_PASSTHRU
#define DP_IPC_PASSTHRU
Definition: ni_device_api_priv.c:170
_ni_encoder_cfg_params::intra_qp
int intra_qp
Definition: ni_device_api.h:2382
_ni_global_session_stats::error_flag
char error_flag[16]
Definition: ni_device_api_priv.h:151
PIC_TYPE_I
@ PIC_TYPE_I
Definition: ni_device_api.h:386
_ni_encoder_cfg_params::enable_rate_control
int enable_rate_control
Definition: ni_device_api.h:2379
_ni_instance_mgr_general_status::fw_video_mem_usage
uint8_t fw_video_mem_usage
Definition: ni_device_api_priv.h:93
ni_p2p_ioctl.h
Definitions related to NETINT P2P kernel driver interface.
_ni_encoder_config_t::ui8multicoreJointMode
uint8_t ui8multicoreJointMode
Definition: ni_device_api_priv.h:571
CONFIG_SESSION_Write_W
#define CONFIG_SESSION_Write_W(sid)
Definition: ni_nvme.h:873
XCODER_MAX_ENC_PIC_WIDTH
#define XCODER_MAX_ENC_PIC_WIDTH
Definition: ni_util.h:114
_ni_encoder_cfg_params::max_consecutive_skip_num
int max_consecutive_skip_num
Definition: ni_device_api.h:2432
NI_PIXEL_PLANAR_MAX
@ NI_PIXEL_PLANAR_MAX
Definition: ni_device_api.h:925
_ni_decoder_input_params_t::semi_planar
int semi_planar[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2526
_ni_metadata_common::ui64_data
union _ni_metadata_common::@27 ui64_data
_ni_decoder_input_params_t::scale_long_short_edge
int scale_long_short_edge[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2554
_ni_encoder_config_t::ui16hdr10_wx
uint16_t ui16hdr10_wx
Definition: ni_device_api_priv.h:595
_ni_nvme_identity::hw1_id
uint8_t hw1_id
Definition: ni_nvme.h:179
_ni_t408_config_t::max_delta_qp
int32_t max_delta_qp
Definition: ni_device_api_priv.h:408
_ni_encoder_config_t::ui16hdr10_dx1
uint16_t ui16hdr10_dx1
Definition: ni_device_api_priv.h:591
_ni_decoder_input_params_t::hwframes
int hwframes
Definition: ni_device_api.h:2520
NI_POOL_TYPE_NORMAL
@ NI_POOL_TYPE_NORMAL
Definition: ni_device_api.h:494
NI_DEVICE_TYPE_AI
@ NI_DEVICE_TYPE_AI
Definition: ni_defs.h:361
ni_hwupload_session_read_hwdesc
int ni_hwupload_session_read_hwdesc(ni_session_context_t *p_ctx, niFrameSurface1_t *hwdesc)
Retrieve a HW descriptor of uploaded frame.
Definition: ni_device_api_priv.c:14157
_ni_ai_config_t
Definition: ni_device_api_priv.h:751
NI_RETCODE_PARAM_ERROR_DECODING_REFRESH_TYPE
@ NI_RETCODE_PARAM_ERROR_DECODING_REFRESH_TYPE
Definition: ni_defs.h:496
_ni_encoder_config_t::ui8PixelFormat
uint8_t ui8PixelFormat
Definition: ni_device_api_priv.h:562
_ni_xcoder_params::customize_roi_qp_map
int8_t customize_roi_qp_map[NI_CUSTOMIZE_ROI_QPOFFSET_LEVEL][NI_CUSTOMIZE_ROI_QP_NUM]
Definition: ni_device_api.h:2820
ni_scaler_session_close
ni_retcode_t ni_scaler_session_close(ni_session_context_t *p_ctx, int eos_received)
close a scaler session
Definition: ni_device_api_priv.c:5272
ni_network_buffer_info_t
struct _ni_network_buffer_info ni_network_buffer_info_t
_ni_uploader_config_t::ui32lumaLinesize
uint32_t ui32lumaLinesize
Definition: ni_device_api_priv.h:672
NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_16X16_EN
@ NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_16X16_EN
Definition: ni_defs.h:499
_ni_encoder_cfg_params::multicoreJointMode
int multicoreJointMode
Definition: ni_device_api.h:2303
CHECK_ERR_RC
#define CHECK_ERR_RC(ctx, rc, info, opcode, type, hw_id, inst_id, opt)
Definition: ni_device_api_priv.c:747
ni_timestamp_init
ni_retcode_t ni_timestamp_init(ni_session_context_t *p_ctx, ni_timestamp_table_t **pp_table, const char *name)
Initialize timestamp handling.
Definition: ni_util.c:1308
FP_CORE
@ FP_CORE
Definition: ni_defs.h:382
_ni_hw_capability::max_video_height
uint16_t max_video_height
Definition: ni_device_api.h:1144
_ni_xcoder_params::enable_ai_enhance
int enable_ai_enhance
Definition: ni_device_api.h:2811
NETINT_PCI_VENDOR_ID
#define NETINT_PCI_VENDOR_ID
Definition: ni_defs.h:129
ni_calculate_sha256
void ni_calculate_sha256(const uint8_t aui8Data[], size_t ui32DataLength, uint8_t aui8Hash[])
Definition: ni_util.c:3550
NI_RETCODE_ERROR_NVME_CMD_FAILED
@ NI_RETCODE_ERROR_NVME_CMD_FAILED
Definition: ni_defs.h:444
_ni_t408_config_t::cu16MergeDeltaRate
int32_t cu16MergeDeltaRate
Definition: ni_device_api_priv.h:476
_ni_thread_arg_struct_t::device_type
uint32_t device_type
Definition: ni_device_api.h:1265
NI_HDR10P_SEI_BYTE3
#define NI_HDR10P_SEI_BYTE3
Definition: ni_device_api.h:457
_ni_decoder_config_t::ui8MCMode
uint8_t ui8MCMode
Definition: ni_device_api_priv.h:735
_ni_decoder_input_params_t
Definition: ni_device_api.h:2466
_ni_t408_config_t::nrCbEnable
uint32_t nrCbEnable
Definition: ni_device_api_priv.h:424
_ni_encoder_config_t::ui32vbvMinRate
uint32_t ui32vbvMinRate
Definition: ni_device_api_priv.h:638
_ni_encoder_config_t::i32frameRateInfo
int32_t i32frameRateInfo
Definition: ni_device_api_priv.h:514
_ni_t408_config_t::bgThrDiff
uint32_t bgThrDiff
Definition: ni_device_api_priv.h:448
GOP_PRESET_IDX_CUSTOM
@ GOP_PRESET_IDX_CUSTOM
Definition: ni_device_api_priv.h:320
_ni_encoder_config_t::ui8AiEnhanceMode
uint8_t ui8AiEnhanceMode
Definition: ni_device_api_priv.h:611
QUERY_INSTANCE_EOS_R
#define QUERY_INSTANCE_EOS_R(sid, instance)
Definition: ni_nvme.h:720
_ni_t408_config_t::dependSliceMode
int32_t dependSliceMode
Definition: ni_device_api_priv.h:386
_ni_encoder_cfg_params::HDR10dy0
int HDR10dy0
Definition: ni_device_api.h:2289
_ni_load_query::fw_video_mem_usage
uint32_t fw_video_mem_usage
Definition: ni_device_api.h:1205
ni_pthread_cond_timedwait
int ni_pthread_cond_timedwait(ni_pthread_cond_t *cond, ni_pthread_mutex_t *mutex, const struct timespec *abstime)
wait on a condition
Definition: ni_util.c:4271
_ni_encoder_cfg_params::enable_dynamic_8x8_merge
int enable_dynamic_8x8_merge
Definition: ni_device_api.h:2364
_ni_t408_config_t::profile
int32_t profile
Definition: ni_device_api_priv.h:370
_ni_scaler_params_t::nb_inputs
int nb_inputs
Definition: ni_device_api.h:2590
_ni_encoder_cfg_params::ctbRcMode
int ctbRcMode
Definition: ni_device_api.h:2336
ni_encoder_frame_params_t
struct _ni_encoder_frame_params ni_encoder_frame_params_t
_ni_t408_config_t::entropy_coding_mode
int32_t entropy_coding_mode
Definition: ni_device_api_priv.h:498
_ni_thread_arg_struct_t::keep_alive_timeout
uint32_t keep_alive_timeout
Definition: ni_device_api.h:1270
NI_MEM_PAGE_ALIGNMENT
#define NI_MEM_PAGE_ALIGNMENT
Definition: ni_defs.h:263
NI_LOG_TRACE
@ NI_LOG_TRACE
Definition: ni_log.h:63
_ni_encoder_cfg_params::long_term_ref_interval
int long_term_ref_interval
Definition: ni_device_api.h:2305
ni_decoder_session_open
ni_retcode_t ni_decoder_session_open(ni_session_context_t *p_ctx)
Open a xcoder decoder instance.
Definition: ni_device_api_priv.c:1181
_ni_frame::end_of_stream
uint32_t end_of_stream
Definition: ni_device_api.h:2634
_ni_session_context::network_data
ni_network_data_t * network_data
Definition: ni_device_api.h:1680
NI_T35_SEI_HDR10_PLUS
@ NI_T35_SEI_HDR10_PLUS
Definition: ni_device_api_priv.c:90
_ni_frame_config::orientation
uint8_t orientation
Definition: ni_device_api.h:2852
NI_HWDESC_UNIFIED_MEMBIN_SIZE
#define NI_HWDESC_UNIFIED_MEMBIN_SIZE
Definition: ni_device_api_priv.h:821
CONFIG_SESSION_SWVersion_W
#define CONFIG_SESSION_SWVersion_W(sid)
Definition: ni_nvme.h:886
NI_DEC_FRAME_BUF_POOL_SIZE_INIT
#define NI_DEC_FRAME_BUF_POOL_SIZE_INIT
Definition: ni_util.h:117
_ni_hw_capability::video_profile
uint8_t video_profile
Definition: ni_device_api.h:1147
NI_CODEC_HW_DOWNLOAD
@ NI_CODEC_HW_DOWNLOAD
Definition: ni_device_api.h:943
_ni_encoder_config_t::ui8colorSpace
uint8_t ui8colorSpace
Definition: ni_device_api_priv.h:557
_ni_metadata_enc_bstream::frame_size
uint16_t frame_size
Definition: ni_device_api_priv.h:292
_ni_encoder_frame_params::data_format
uint16_t data_format
Definition: ni_device_api_priv.h:200
ni_pthread_mutex_lock
int ni_pthread_mutex_lock(ni_pthread_mutex_t *mutex)
thread mutex lock
Definition: ni_util.c:4057
_niFrameSurface1::encoding_type
int8_t encoding_type
Definition: ni_device_api.h:2832
ni_hwdownload_session_read
int ni_hwdownload_session_read(ni_session_context_t *p_ctx, ni_frame_t *p_frame, niFrameSurface1_t *hwdesc)
Retrieve a YUV p_frame from decoder.
Definition: ni_device_api_priv.c:15163
_ni_encoder_config_t::i32hwframes
int32_t i32hwframes
Definition: ni_device_api_priv.h:538
_ni_frameclone_desc::ui16SrcIdx
uint16_t ui16SrcIdx
Definition: ni_device_api.h:1379
_ni_xcoder_params::hwframes
int hwframes
Definition: ni_device_api.h:2804
_ni_session_context::prev_size
int prev_size
Definition: ni_device_api.h:1509
_ni_encoder_cfg_params::newRcEnable
int newRcEnable
Definition: ni_device_api.h:2405
_ni_session_context::device_handle
ni_device_handle_t device_handle
Definition: ni_device_api.h:1464
_ni_session_context::p_session_config
void * p_session_config
Definition: ni_device_api.h:1475
NI_CODEC_FORMAT_AV1
@ NI_CODEC_FORMAT_AV1
Definition: ni_device_api.h:917
NI_MAX_TX_RETRIES
#define NI_MAX_TX_RETRIES
Definition: ni_device_api.h:64
_ni_encoder_cfg_params::skip_frame_enable
int skip_frame_enable
Definition: ni_device_api.h:2431
ni_rsrc_free_device_context
void ni_rsrc_free_device_context(ni_device_context_t *p_device_context)
Free previously allocated device context.
Definition: ni_rsrc_api.cpp:1322
_ni_frame::sei_hdr_plus_len
unsigned int sei_hdr_plus_len
Definition: ni_device_api.h:2667
_ni_xcoder_params::preset
int preset
Definition: ni_device_api.h:2746
NI_FW_ENC_BITSTREAM_META_DATA_SIZE_UNDER_MAJOR_6_MINOR_rc
#define NI_FW_ENC_BITSTREAM_META_DATA_SIZE_UNDER_MAJOR_6_MINOR_rc
Definition: ni_device_api_priv.h:842
_ni_metadata_dec_frame::sei_header
uint32_t sei_header
Definition: ni_device_api_priv.h:231
_ni_encoder_frame_params::timestamp
uint32_t timestamp
Definition: ni_device_api_priv.h:204
NI_SCALER_OPCODE_SCALE
@ NI_SCALER_OPCODE_SCALE
Definition: ni_defs.h:585
_ni_session_context::dec_fme_buf_pool
ni_buf_pool_t * dec_fme_buf_pool
Definition: ni_device_api.h:1561
NI_NOPTS_VALUE
#define NI_NOPTS_VALUE
Definition: ni_device_api.h:52
_ni_metadata_enc_bstream_rev61::avg_frame_qp
uint32_t avg_frame_qp
Definition: ni_device_api_priv.h:269
_ni_custom_sei_set
Definition: ni_device_api.h:1127
_ni_encoder_cfg_params::entropy_coding_mode
int entropy_coding_mode
Definition: ni_device_api.h:2321
_ni_metadata_common::frame_offset
uint64_t frame_offset
Definition: ni_device_api_priv.h:216
_ni_packet::p_data
void * p_data
Definition: ni_device_api.h:2867
INST_BUF_INFO_RW_WRITE_BY_EP
@ INST_BUF_INFO_RW_WRITE_BY_EP
Definition: ni_device_api_priv.h:54
_ni_hw_capability::min_video_width
uint16_t min_video_width
Definition: ni_device_api.h:1145
_ni_session_data_io
Definition: ni_device_api.h:2896
_ni_t408_config_t::conf_win_left
int32_t conf_win_left
Definition: ni_device_api_priv.h:382
ni_validate_custom_dec_template
ni_retcode_t ni_validate_custom_dec_template(ni_xcoder_params_t *p_src, ni_session_context_t *p_ctx, ni_decoder_config_t *p_cfg, char *p_param_err, uint32_t max_err_len)
Perform validation on custom dec parameters (Rev. B)
Definition: ni_device_api_priv.c:10125
_ni_encoder_config_t::ui8temporalLayersEnable
uint8_t ui8temporalLayersEnable
Definition: ni_device_api_priv.h:610
ni_uploader_session_open
ni_retcode_t ni_uploader_session_open(ni_session_context_t *p_ctx)
Open a xcoder upload instance.
Definition: ni_device_api_priv.c:13362
_ni_metadata_enc_bstream::reconLumaSize
uint32_t reconLumaSize
Definition: ni_device_api_priv.h:298
NI_RETCODE_NVME_SC_VPU_RECOVERY
@ NI_RETCODE_NVME_SC_VPU_RECOVERY
Definition: ni_defs.h:559
QUERY_INSTANCE_RBUFF_SIZE_BUSY_R
#define QUERY_INSTANCE_RBUFF_SIZE_BUSY_R(sid, instance)
Definition: ni_nvme.h:728
_ni_encoder_cfg_params::ltrFirstGap
int ltrFirstGap
Definition: ni_device_api.h:2301
TP_CORE
@ TP_CORE
Definition: ni_defs.h:381
_ni_encoder_frame_params::force_picture_type
uint16_t force_picture_type
Definition: ni_device_api_priv.h:199
_ni_content_light_level_info_bytes::max_pic_average_light_level
uint16_t max_pic_average_light_level
Definition: ni_device_api.h:1079
_ni_encoder_cfg_params::ltrRefQpOffset
int ltrRefQpOffset
Definition: ni_device_api.h:2300
_ni_t408_config_t::chromaCrQpOffset
int32_t chromaCrQpOffset
Definition: ni_device_api_priv.h:419
NI_PIX_FMT_YUV420P10LE
@ NI_PIX_FMT_YUV420P10LE
Definition: ni_device_api.h:264
_ni_frame_config::rgba_color
uint32_t rgba_color
Definition: ni_device_api.h:2848
ni_log
void ni_log(ni_log_level_t level, const char *fmt,...)
print log message using ni_log_callback
Definition: ni_log.c:183
ni_ai_session_close
ni_retcode_t ni_ai_session_close(ni_session_context_t *p_ctx, int eos_recieved)
Definition: ni_device_api_priv.c:17350
_ni_metadata_common::pkt_delay_cnt
uint8_t pkt_delay_cnt
Definition: ni_device_api_priv.h:223
NI_XCODER_REVISION_API_MAJOR_VER_IDX
#define NI_XCODER_REVISION_API_MAJOR_VER_IDX
Definition: ni_defs.h:99
_ni_t408_config_t::slice_arg
int32_t slice_arg
Definition: ni_device_api_priv.h:494
_ni_encoder_cfg_params::enable_transform_8x8
int enable_transform_8x8
Definition: ni_device_api.h:2370
_ni_session_context::sender_handle
ni_device_handle_t sender_handle
Definition: ni_device_api.h:1470
ni_hwdownload_by_frame_idx
int ni_hwdownload_by_frame_idx(niFrameSurface1_t *hwdesc, ni_frame_t *p_frame, int is_auto_dl)
Retrieve a YUV p_frame by frame index.
Definition: ni_device_api_priv.c:15355
_ni_scaler_config::filterblit
uint8_t filterblit
Definition: ni_device_api_priv.h:785
CONFIG_INSTANCE_SetP2P_W
#define CONFIG_INSTANCE_SetP2P_W(sid, instance)
Definition: ni_nvme.h:862
_ni_session_statistic_t::ui32FramesOutput
uint32_t ui32FramesOutput
Definition: ni_defs.h:394
_ni_dec_mastering_display_colour_volume_bytes::min_display_mastering_luminance
uint32_t min_display_mastering_luminance
Definition: ni_device_api.h:1069
NI_MAX_GOP_NUM
#define NI_MAX_GOP_NUM
Definition: ni_device_api.h:57
NI_CUSTOMIZE_ROI_QP_NUM
#define NI_CUSTOMIZE_ROI_QP_NUM
Max number of entries per line supported for the qp number.
Definition: ni_device_api.h:483
_ni_t408_config_t::monochromeEnable
uint32_t monochromeEnable
Definition: ni_device_api_priv.h:443
ni_query_instance_buf_info
ni_retcode_t ni_query_instance_buf_info(ni_session_context_t *p_ctx, ni_instance_buf_info_rw_type_t rw_type, ni_device_type_t device_type, ni_instance_buf_info_t *p_inst_buf_info)
Query a particular xcoder instance to get buffer/data Info data.
Definition: ni_device_api_priv.c:6682
_ni_encoder_cfg_params::enable_smooth_crf
int enable_smooth_crf
Definition: ni_device_api.h:2450
SwapSW32
void SwapSW32(uint32_t *buf, uint32_t bytes)
Definition: ni_device_api_priv.c:201
_ni_encoder_config_t::i8crfDecimal
int8_t i8crfDecimal
Definition: ni_device_api_priv.h:634
MAX_BDF_LEN
#define MAX_BDF_LEN
Definition: ni_device_api_priv.c:997
ni_create_frame
int ni_create_frame(ni_frame_t *p_frame, uint32_t read_length, uint64_t *p_frame_offset, bool is_hw_frame)
Get info from received p_frame.
Definition: ni_device_api_priv.c:7448
_ni_session_context::keep_alive_timeout
uint32_t keep_alive_timeout
Definition: ni_device_api.h:1523
_ni_encoder_config_t::ui8bitstreamFormat
uint8_t ui8bitstreamFormat
Definition: ni_device_api_priv.h:509
_ni_session_statistic_t::ui8AdditionalFramesDelay
uint8_t ui8AdditionalFramesDelay
Definition: ni_defs.h:408
_ni_encoder_cfg_params::totalCuTreeDepth
int totalCuTreeDepth
Definition: ni_device_api.h:2457
NI_HDR10P_SEI_BYTE2
#define NI_HDR10P_SEI_BYTE2
Definition: ni_device_api.h:456
NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_8X8_EN
@ NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_8X8_EN
Definition: ni_defs.h:498
_ni_encoder_cfg_params::customize_roi_qp_level
int customize_roi_qp_level
Definition: ni_device_api.h:2445
_ni_encoder_config_t::ui8enableTimecode
uint8_t ui8enableTimecode
Definition: ni_device_api_priv.h:651
ni_ai_session_read
ni_retcode_t ni_ai_session_read(ni_session_context_t *p_ctx, ni_packet_t *p_packet)
Definition: ni_device_api_priv.c:16750
NI_SCALER_OPCODE_STACK
@ NI_SCALER_OPCODE_STACK
Definition: ni_defs.h:590
_ni_packet::scene_change_detected
uint8_t scene_change_detected
Definition: ni_device_api.h:2893
_ni_encoder_cfg_params
Definition: ni_device_api.h:2067
GC620_BGRX8888
#define GC620_BGRX8888
Definition: ni_device_api.h:202
NI_RETCODE_PARAM_WARN
@ NI_RETCODE_PARAM_WARN
Definition: ni_defs.h:538
_ni_encoder_config_t::ui8repeatHeaders
uint8_t ui8repeatHeaders
Definition: ni_device_api_priv.h:547
_ni_encoder_cfg_params::EnableAUD
int EnableAUD
Definition: ni_device_api.h:2315
_ni_nvme_identity::hw1_codec_type
uint8_t hw1_codec_type
Definition: ni_nvme.h:183
_ni_session_context::pkt_offsets_index
uint64_t pkt_offsets_index[NI_FIFO_SZ]
Definition: ni_device_api.h:1453
_ni_encoder_cfg_params::spatial_layers
int spatial_layers
Definition: ni_device_api.h:2452
ni_fmt_fw_api_ver_str
void ni_fmt_fw_api_ver_str(const char ver_str[], char fmt_str[])
Get formatted FW API version string from unformatted FW API version string.
Definition: ni_util.c:3644
FRAME_CHUNK_INDEX_SIZE
#define FRAME_CHUNK_INDEX_SIZE
Definition: ni_device_api_priv.h:816
ni_usleep
void ni_usleep(int64_t usec)
Definition: ni_util.c:358
_ni_encoder_config_t::ui8useLowDelayPocType
uint8_t ui8useLowDelayPocType
Definition: ni_device_api_priv.h:550
_ni_encoder_cfg_params::scene_change_detect_level
int scene_change_detect_level
Definition: ni_device_api.h:2448
ni_ai_multi_config_frame
ni_retcode_t ni_ai_multi_config_frame(ni_session_context_t *p_ctx, ni_frame_config_t p_cfg_in[], int numInCfgs, ni_frame_config_t *p_cfg_out)
Definition: ni_device_api_priv.c:17468
CONFIG_SESSION_KeepAlive_W
#define CONFIG_SESSION_KeepAlive_W(sid)
Definition: ni_nvme.h:869
_ni_session_context::device_type
uint32_t device_type
Definition: ni_device_api.h:1486
_ni_decoder_config_t::ui32MaxPktSize
uint32_t ui32MaxPktSize
Definition: ni_device_api_priv.h:739
niFrameSurface1_t
struct _niFrameSurface1 niFrameSurface1_t
NI_RETCODE_PARAM_ERROR_MX_DELTA_QP
@ NI_RETCODE_PARAM_ERROR_MX_DELTA_QP
Definition: ni_defs.h:475
_ni_t408_config_t::intra_period
int32_t intra_period
Definition: ni_device_api_priv.h:379
ni_set_default_template
void ni_set_default_template(ni_session_context_t *p_ctx, ni_encoder_config_t *p_config)
Setup and initialize all xcoder configuration to default (Rev. B)
Definition: ni_device_api_priv.c:9733
NI_ENC_MIN_RESOLUTION_WIDTH
#define NI_ENC_MIN_RESOLUTION_WIDTH
Definition: ni_device_api.h:88
QUERY_INSTANCE_CUR_STATUS_INFO_R
#define QUERY_INSTANCE_CUR_STATUS_INFO_R(sid, instance)
Definition: ni_nvme.h:711
NI_AI_HW_ALIGN_SIZE
#define NI_AI_HW_ALIGN_SIZE
Definition: ni_device_api_priv.h:1254
ni_decoder_output_config_t::ui8SemiPlanarEnabled
uint8_t ui8SemiPlanarEnabled
Definition: ni_device_api_priv.h:716
_ni_t408_config_t::minQpB
int32_t minQpB
Definition: ni_device_api_priv.h:485
_ni_metadata_common::frame_width
uint16_t frame_width
Definition: ni_device_api_priv.h:219
QUERY_INSTANCE_NL_SIZE_R
#define QUERY_INSTANCE_NL_SIZE_R(sid, instance)
Definition: ni_nvme.h:752
GC620_RGBA8888
#define GC620_RGBA8888
Definition: ni_device_api.h:200
_ni_frame::force_key_frame
int force_key_frame
Definition: ni_device_api.h:2650
NI_GET_MAX_HWDESC_FRAME_INDEX
#define NI_GET_MAX_HWDESC_FRAME_INDEX(x)
Definition: ni_defs.h:290
_ni_global_session_stats::check_flag
uint8_t check_flag[4]
Definition: ni_device_api_priv.h:152
_ni_scaler_params_t::scaler_param_b
double scaler_param_b
Definition: ni_device_api.h:2591
LRETURN
#define LRETURN
Definition: ni_defs.h:335
NI_PIXEL_PLANAR_FORMAT_SEMIPLANAR
@ NI_PIXEL_PLANAR_FORMAT_SEMIPLANAR
Definition: ni_device_api.h:922
calculate_psnr
void calculate_psnr(ni_session_context_t *p_ctx, ni_packet_t *p_packet)
Definition: ni_device_api_priv.c:451
_ni_t408_config_t::numUnitsInTick
uint32_t numUnitsInTick
Definition: ni_device_api_priv.h:414
ni_instance_mgr_allocation_info_t
struct _ni_instance_mgr_allocation_info ni_instance_mgr_allocation_info_t
_ni_instance_mgr_general_status::fw_model_load_overall
uint32_t fw_model_load_overall
Definition: ni_device_api_priv.h:104
_ni_nvme_identity::hw2_video_profile
uint8_t hw2_video_profile
Definition: ni_nvme.h:201
_ni_encoder_cfg_params::HDR10CLLEnable
int HDR10CLLEnable
Definition: ni_device_api.h:2286
_ni_metadata_enc_frame::force_headers
uint16_t force_headers
Definition: ni_device_api_priv.h:253
_ni_scaler_params_t::enable_scaler_params
bool enable_scaler_params
Definition: ni_device_api.h:2593
_ni_encoder_config_t::ui8EnableRdoQuant
uint8_t ui8EnableRdoQuant
Definition: ni_device_api_priv.h:546
_ni_t408_config_t::pu08IntraPlanarDeltaRate
int32_t pu08IntraPlanarDeltaRate
Definition: ni_device_api_priv.h:462
_ni_frameclone_desc::ui32Offset
uint32_t ui32Offset
Definition: ni_device_api.h:1381
_ni_encoder_config_t::ui16hdr10_dy1
uint16_t ui16hdr10_dy1
Definition: ni_device_api_priv.h:592
NI_HDR10P_SEI_BYTE0
#define NI_HDR10P_SEI_BYTE0
Definition: ni_device_api.h:454
_ni_content_light_level_info_bytes::max_content_light_level
uint16_t max_content_light_level
Definition: ni_device_api.h:1078
_ni_packet::video_height
uint32_t video_height
Definition: ni_device_api.h:2864
ni_fix_VUI
void ni_fix_VUI(uint8_t *vui, int pos, int value)
insert the 32 bits of integer value at bit position pos
Definition: ni_device_api_priv.c:8217
_ni_session_context::buffer_pool
ni_queue_buffer_pool_t * buffer_pool
Definition: ni_device_api.h:1560
_ni_xcoder_params::chroma_linesize
int chroma_linesize
Definition: ni_device_api.h:2815
NI_MINIMUM_CROPPED_LENGTH
#define NI_MINIMUM_CROPPED_LENGTH
Definition: ni_device_api_priv.h:692
_ni_xcoder_params::minFramesDelay
int minFramesDelay
Definition: ni_device_api.h:2818
_ni_xcoder_params::sar_num
int sar_num
Definition: ni_device_api.h:2789
_ni_metadata_enc_frame::metadata_common
ni_metadata_common_t metadata_common
Definition: ni_device_api_priv.h:242
_ni_t408_config_t::pu16IntraAngleDeltaRate
int32_t pu16IntraAngleDeltaRate
Definition: ni_device_api_priv.h:467
_ni_t408_config_t::constIntraPredFlag
int32_t constIntraPredFlag
Definition: ni_device_api_priv.h:375
_ni_metadata_enc_frame::frame_roi_map_size
uint32_t frame_roi_map_size
Definition: ni_device_api_priv.h:244
NI_RETCODE_PARAM_ERROR_HVS_QP_EN
@ NI_RETCODE_PARAM_ERROR_HVS_QP_EN
Definition: ni_defs.h:469
ni_config_instance_eos
ni_retcode_t ni_config_instance_eos(ni_session_context_t *p_ctx, ni_device_type_t device_type)
Send a p_config command for End Of Stream.
Definition: ni_device_api_priv.c:6960
_ni_packet
Definition: ni_device_api.h:2855
NI_DATA_BUFFER_LEN
#define NI_DATA_BUFFER_LEN
Definition: ni_nvme.h:623
_ni_session_statistic_t::ui32RdBufAvailSize
uint32_t ui32RdBufAvailSize
Definition: ni_defs.h:389
_ni_session_config_rw::ui8HWAccess
uint8_t ui8HWAccess
Definition: ni_device_api_priv.h:71
_ni_nvme_identity::ai8Sn
uint8_t ai8Sn[20]
Definition: ni_nvme.h:95
NI_MAX_P2P_SGL_ENTRIES
#define NI_MAX_P2P_SGL_ENTRIES
Definition: ni_device_api_priv.h:830
_ni_session_context::frame_num
uint64_t frame_num
Definition: ni_device_api.h:1533
_ni_frame::sei_cc_len
unsigned int sei_cc_len
Definition: ni_device_api.h:2659
ni_device_session_context_clear
void ni_device_session_context_clear(ni_session_context_t *p_ctx)
Clear already allocated session context.
Definition: ni_device_api.c:249
_ni_encoder_cfg_params::HDR10Enable
int HDR10Enable
Definition: ni_device_api.h:2287
CONFIG_INSTANCE_SetEncFramePara_W
#define CONFIG_INSTANCE_SetEncFramePara_W(sid, instance)
Definition: ni_nvme.h:838
_ni_queue_t::count
uint32_t count
Definition: ni_device_api.h:1318
_ni_encoder_cfg_params::intra_qp_delta
int intra_qp_delta
Definition: ni_device_api.h:2383
_ni_session_context::ori_luma_linesize
int ori_luma_linesize
Definition: ni_device_api.h:1682
ni_scaler_session_query_buffer_avail
ni_retcode_t ni_scaler_session_query_buffer_avail(ni_session_context_t *p_ctx)
Query and acquire buffer from xcoder scaler instance.
Definition: ni_device_api_priv.c:13604
_ni_xcoder_params::ai_enhance_level
int ai_enhance_level
Definition: ni_device_api.h:2816
_ni_network_layer_params_t::sizes
uint32_t sizes[6]
Definition: ni_device_api.h:1331
_ni_t408_config_t::enable_mb_level_rc
int32_t enable_mb_level_rc
Definition: ni_device_api_priv.h:497
ni_decoder_session_send_eos
ni_retcode_t ni_decoder_session_send_eos(ni_session_context_t *p_ctx)
Send end of stream signal to the decoder.
Definition: ni_device_api_priv.c:1524
_ni_network_layer_offset::offset
int32_t offset
Definition: ni_device_api.h:1362
_ni_t408_config_t::cu16IntraDeltaRate
int32_t cu16IntraDeltaRate
Definition: ni_device_api_priv.h:474
_ni_instance_mgr_stream_info::is_flushed
uint16_t is_flushed
Definition: ni_device_api_priv.h:129
_ni_encoder_cfg_params::colorDescPresent
int colorDescPresent
Definition: ni_device_api.h:2325
_ni_t408_config_t::wppEnable
int32_t wppEnable
Definition: ni_device_api_priv.h:394
ni_ai_config_t
struct _ni_ai_config_t ni_ai_config_t
CONFIG_INSTANCE_SetEncPara_W
#define CONFIG_INSTANCE_SetEncPara_W(sid, instance)
Definition: ni_nvme.h:834
_ni_overall_load_query::overall_fw_model_load
uint32_t overall_fw_model_load
Definition: ni_device_api.h:1227
_ni_device_context
Definition: ni_rsrc_api.h:145
_ni_decoder_config_t::fps_number
uint32_t fps_number
Definition: ni_device_api_priv.h:733
_ni_session_context::last_gop_size
int last_gop_size
Definition: ni_device_api.h:1693
nvme_admin_cmd_xcoder_config
@ nvme_admin_cmd_xcoder_config
Definition: ni_nvme.h:324
_ni_frame::vui_offset
unsigned int vui_offset
Definition: ni_device_api.h:2675
_ni_encoder_cfg_params::tolCtbRcInter
float tolCtbRcInter
Definition: ni_device_api.h:2343
ni_recv_from_target
ni_retcode_t ni_recv_from_target(ni_session_context_t *p_ctx, const ni_p2p_sgl_t *dmaAddrs, ni_frame_t *pDstFrame)
Definition: ni_device_api_priv.c:18607
_ni_frame::p_data
uint8_t * p_data[NI_MAX_NUM_DATA_POINTERS]
Definition: ni_device_api.h:2689
_ni_encoder_cfg_params::custom_gop_params
ni_custom_gop_params_t custom_gop_params
Definition: ni_device_api.h:2269
_ni_encoder_cfg_params::HDR10dx0
int HDR10dx0
Definition: ni_device_api.h:2288
NVME_LOG_OFFSET_IN_4K
#define NVME_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:660
_ni_xcoder_params::zerocopy_mode
int zerocopy_mode
Definition: ni_device_api.h:2813
NI_EC_POLICY_DEFAULT
#define NI_EC_POLICY_DEFAULT
Definition: ni_device_api.h:326
_ni_xcoder_params::sar_denom
int sar_denom
Definition: ni_device_api.h:2790
_ni_t408_config_t::cu32IntraDeltaRate
int32_t cu32IntraDeltaRate
Definition: ni_device_api_priv.h:477
_ni_iovec::size
uint32_t size
Definition: ni_device_api.h:2623
ni_decoder_output_config_t::sCroppingRectable
ni_decode_cropping_rectangle sCroppingRectable
Definition: ni_device_api_priv.h:724
_ni_metadata_common::crop_bottom
uint16_t crop_bottom
Definition: ni_device_api_priv.h:213
_ni_decoder_input_params_t::max_extra_hwframe_cnt
int max_extra_hwframe_cnt
Definition: ni_device_api.h:2548
NI_RETCODE_PARAM_ERROR_MAXNUMMERGE
@ NI_RETCODE_PARAM_ERROR_MAXNUMMERGE
Definition: ni_defs.h:489
_ni_device_capability::fw_build_time
uint8_t fw_build_time[26]
Definition: ni_device_api.h:1170
_ni_encoder_config_t::ui32sourceEndian
uint32_t ui32sourceEndian
Definition: ni_device_api_priv.h:532
_ni_decoder_config_t::asOutputConfig
ni_decoder_output_config_t asOutputConfig[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api_priv.h:740
_ni_iovec::ptr
void * ptr
Definition: ni_device_api.h:2624
ni_check_ratecontrol_params
ni_retcode_t ni_check_ratecontrol_params(ni_encoder_config_t *p_cfg, char *p_param_err, uint32_t max_err_len)
Definition: ni_device_api_priv.c:12971
_ni_t408_config_t::nrIntraWeightCr
uint32_t nrIntraWeightCr
Definition: ni_device_api_priv.h:430
_ni_session_context::pkt_index
int pkt_index
Definition: ni_device_api.h:1452
_ni_encoder_cfg_params::disableBframeRdoq
int disableBframeRdoq
Definition: ni_device_api.h:2440
NI_CODEC_FORMAT_VP9
@ NI_CODEC_FORMAT_VP9
Definition: ni_device_api.h:915
GOP_PRESET_IDX_HIERARCHICAL_IPPPP
@ GOP_PRESET_IDX_HIERARCHICAL_IPPPP
Definition: ni_device_api_priv.h:331
_ni_t408_config_t::nrCrEnable
uint32_t nrCrEnable
Definition: ni_device_api_priv.h:425
ni_core_type_t
ni_core_type_t
Definition: ni_defs.h:375
NI_DEVICE_TYPE_SCALER
@ NI_DEVICE_TYPE_SCALER
Definition: ni_defs.h:360
_ni_segment::ui32Size
uint32_t ui32Size
Definition: ni_device_api_priv.h:759
_ni_nvme_identity_xcoder_hw::hw_video_profile
uint8_t hw_video_profile
Definition: ni_nvme.h:83
_ni_instance_mgr_stream_info::frame_rate
uint16_t frame_rate
Definition: ni_device_api_priv.h:128
_ni_encoder_cfg_params::tolCtbRcIntra
float tolCtbRcIntra
Definition: ni_device_api.h:2344
_ni_t408_config_t::cu08MergeDeltaRate
int32_t cu08MergeDeltaRate
Definition: ni_device_api_priv.h:473
_ni_session_statistic_t::ui32LastErrorTransactionId
uint32_t ui32LastErrorTransactionId
Definition: ni_defs.h:402
_ni_instance_mgr_stream_info::picture_height
uint16_t picture_height
Definition: ni_device_api_priv.h:125
XCODER_MAX_ENC_PIC_HEIGHT
#define XCODER_MAX_ENC_PIC_HEIGHT
Definition: ni_util.h:115
_ni_iovec
Definition: ni_device_api.h:2622
NI_DDR_PRIORITY_RESET
@ NI_DDR_PRIORITY_RESET
Definition: ni_device_api.h:1832
_ni_frame::roi_len
unsigned int roi_len
Definition: ni_device_api.h:2679
_ni_encoder_config_t::ui8gopSize
uint8_t ui8gopSize
Definition: ni_device_api_priv.h:549
ni_dec_fme_buffer_pool_initialize
int32_t ni_dec_fme_buffer_pool_initialize(ni_session_context_t *p_ctx, int32_t number_of_buffers, int width, int height, int height_align, int factor)
Definition: ni_util.c:626
_ni_session_context::last_pts_interval
int64_t last_pts_interval
Definition: ni_device_api.h:1642
ni_encoder_session_close
ni_retcode_t ni_encoder_session_close(ni_session_context_t *p_ctx, int eos_recieved)
Close a xcoder encoder instance.
Definition: ni_device_api_priv.c:3796
NI_RETCODE_PARAM_ERROR_CONF_WIN_R
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_R
Definition: ni_defs.h:483
_ni_metadata_common::crop_top
uint16_t crop_top
Definition: ni_device_api_priv.h:212
NI_MAX_SEGMENT_NUM
#define NI_MAX_SEGMENT_NUM
Definition: ni_device_api_priv.h:775
NI_ERRNO
#define NI_ERRNO
Definition: ni_defs.h:229
_ni_frame_config::picture_height
uint16_t picture_height
Definition: ni_device_api.h:2841
NI_RETCODE_PARAM_ERROR_WIDTH_TOO_BIG
@ NI_RETCODE_PARAM_ERROR_WIDTH_TOO_BIG
Definition: ni_defs.h:506
_ni_encoder_config_t::ui8LowDelay
uint8_t ui8LowDelay
Definition: ni_device_api_priv.h:584
_ni_frame::sei_hdr_plus_offset
unsigned int sei_hdr_plus_offset
Definition: ni_device_api.h:2666
_ni_encoder_cfg_params::enable_dynamic_16x16_merge
int enable_dynamic_16x16_merge
Definition: ni_device_api.h:2365
ni_xcoder_resource_recovery
@ ni_xcoder_resource_recovery
Definition: ni_device_api_priv.h:63
_ni_nvme_identity::xcoder_devices
ni_nvme_identity_xcoder_hw_t xcoder_devices[NI_MAX_DEVICES_PER_HW_INSTANCE]
Definition: ni_nvme.h:232
NI_RETCODE_PARAM_ERROR_LOOK_AHEAD_DEPTH
@ NI_RETCODE_PARAM_ERROR_LOOK_AHEAD_DEPTH
Definition: ni_defs.h:529
_ni_encoder_config_t::ui32cropHeight
uint32_t ui32cropHeight
Definition: ni_device_api_priv.h:616
ni_t35_sei_mesg_type_t
enum _ni_t35_sei_mesg_type ni_t35_sei_mesg_type_t
_ni_t408_config_t::pu16IntraPlanarDeltaRate
int32_t pu16IntraPlanarDeltaRate
Definition: ni_device_api_priv.h:465
_ni_t408_config_t::enable_transform_8x8
int32_t enable_transform_8x8
Definition: ni_device_api_priv.h:492
_ni_session_context::hw_action
int hw_action
Definition: ni_device_api.h:1611
_ni_input_frame::video_height
uint32_t video_height
Definition: ni_device_api.h:1403
_ni_encoder_config_t::niParamT408
ni_t408_config_t niParamT408
Definition: ni_device_api_priv.h:529
nvme_admin_cmd_xcoder_query
@ nvme_admin_cmd_xcoder_query
Definition: ni_nvme.h:320
_ni_session_context
Definition: ni_device_api.h:1410
_ni_session_context::itu_t_t35_cc_sei_hdr_hevc
uint8_t itu_t_t35_cc_sei_hdr_hevc[NI_CC_SEI_HDR_HEVC_LEN]
Definition: ni_device_api.h:1419
QUERY_DETAIL_GET_STATUS_V1_R
#define QUERY_DETAIL_GET_STATUS_V1_R(instance)
Definition: ni_nvme.h:786
_ni_encoder_cfg_params::iframe_size_ratio
int iframe_size_ratio
Definition: ni_device_api.h:2436
ni_get_planar_from_pixfmt
int ni_get_planar_from_pixfmt(int pix_fmt)
Grab planar info from NI_PIX_FMT.
Definition: ni_device_api_priv.c:15942
NI_PIX_FMT_P010LE
@ NI_PIX_FMT_P010LE
Definition: ni_device_api.h:266
ni_memfree
#define ni_memfree(p_memptr)
Definition: ni_util.h:408
_ni_session_context::bus
unsigned short bus
Definition: ni_device_api.h:1622
_ni_thread_arg_struct_t::hw_id
int hw_id
Definition: ni_device_api.h:1261
ni_validate_custom_template
ni_retcode_t ni_validate_custom_template(ni_session_context_t *p_ctx, ni_encoder_config_t *p_cfg, ni_xcoder_params_t *p_src, char *p_param_err, uint32_t max_err_len)
Perform validation on custom parameters (Rev. B)
Definition: ni_device_api_priv.c:10435
NI_CODEC_HW_UPLOAD
@ NI_CODEC_HW_UPLOAD
Definition: ni_device_api.h:944
_niFrameSurface1
Definition: ni_device_api.h:2823
NI_EC_ERR_THRESHOLD_DEFAULT
#define NI_EC_ERR_THRESHOLD_DEFAULT
Definition: ni_device_api.h:327
NI_CODEC_HW_ENABLE
@ NI_CODEC_HW_ENABLE
Definition: ni_device_api.h:942
ni_sei_header_t
struct _ni_sei_header ni_sei_header_t
_ni_t408_config_t::coefClearDisable
int32_t coefClearDisable
Definition: ni_device_api_priv.h:480
_ni_metadata_enc_bstream_rev61::frame_type
uint32_t frame_type
Definition: ni_device_api_priv.h:266
_ni_t408_config_t::cu08InterDeltaRate
int32_t cu08InterDeltaRate
Definition: ni_device_api_priv.h:472
NI_PARAM_AV1_MAX_HEIGHT
#define NI_PARAM_AV1_MAX_HEIGHT
Definition: ni_device_api.h:146
ni_decode_cropping_rectangle::ui16H
uint16_t ui16H
Definition: ni_device_api_priv.h:703
_ni_session_context::last_dts_interval
int64_t last_dts_interval
Definition: ni_device_api.h:1641
ni_nvme_send_read_cmd
int32_t ni_nvme_send_read_cmd(ni_device_handle_t handle, ni_event_handle_t event_handle, void *p_data, uint32_t data_len, uint32_t lba)
Compose an io read command.
Definition: ni_nvme.c:554
_ni_metadata_dec_frame::metadata_common
ni_metadata_common_t metadata_common
Definition: ni_device_api_priv.h:229
_ni_ddr_priority_config
Definition: ni_device_api_priv.h:793
_ni_encoder_cfg_params::adaptiveCuTree
int adaptiveCuTree
Definition: ni_device_api.h:2458
_ni_encoder_config_t::ui32QLevel
uint32_t ui32QLevel
Definition: ni_device_api_priv.h:574
_ni_t408_config_t::pu16DeltaRate
int32_t pu16DeltaRate
Definition: ni_device_api_priv.h:457
_ni_session_data_io::data
union _ni_session_data_io::@19 data
_ni_session_context::blk_xcoder_name
char blk_xcoder_name[MAX_CHAR_IN_DEVICE_NAME]
Definition: ni_device_api.h:1492
_ni_frame
Definition: ni_device_api.h:2627
_ni_decoder_config_t::ui8DisablePictureReordering
uint8_t ui8DisablePictureReordering
Definition: ni_device_api_priv.h:736
_ni_metadata_dec_frame::sei_number
uint16_t sei_number
Definition: ni_device_api_priv.h:234
ni_pthread_sigmask
int ni_pthread_sigmask(int how, const ni_sigset_t *set, ni_sigset_t *oldset)
examine and change mask of blocked signals
Definition: ni_util.c:4305
_ni_t408_config_t::intraNxNEnable
int32_t intraNxNEnable
Definition: ni_device_api_priv.h:402
_ni_network_data::input_num
uint32_t input_num
Definition: ni_device_api.h:1367
_ni_packet::sent_size
int sent_size
Definition: ni_device_api.h:2869
_ni_encoder_cfg_params::HDR10dy1
int HDR10dy1
Definition: ni_device_api.h:2291
ni_instance_mgr_stream_info_t
struct _ni_instance_mgr_stream_info ni_instance_mgr_stream_info_t
CONFIG_INSTANCE_SetScalerAlloc_W
#define CONFIG_INSTANCE_SetScalerAlloc_W(sid, instance)
Definition: ni_nvme.h:816
CONFIG_INSTANCE_SetScalerPara_W
#define CONFIG_INSTANCE_SetScalerPara_W(sid, instance)
Definition: ni_nvme.h:811
NI_PIX_FMT_RGBA
@ NI_PIX_FMT_RGBA
Definition: ni_device_api.h:267
_ni_encoder_cfg_params::conf_win_bottom
int conf_win_bottom
Definition: ni_device_api.h:2312
_ni_metadata_enc_bstream_rev61
Definition: ni_device_api_priv.h:263
_ni_decoder_input_params_t::scale_round
int scale_round[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2556
_ni_overall_load_query::overall_instance_count
uint32_t overall_instance_count
Definition: ni_device_api.h:1228
_ni_session_context::roi_len
uint32_t roi_len
Definition: ni_device_api.h:1498
_ni_encoder_cfg_params::linkFrameMaxIntraRatio
int linkFrameMaxIntraRatio
Definition: ni_device_api.h:2462
NI_DDR_PRIORITY_NONE
@ NI_DDR_PRIORITY_NONE
Definition: ni_device_api.h:1831
_ni_encoder_cfg_params::profile
int profile
Definition: ni_device_api.h:2262
ni_encoder_config_t
struct _ni_encoder_config_t ni_encoder_config_t
EP_LOG_OFFSET_IN_4K
#define EP_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:661
ni_decoder_config_t
struct _ni_decoder_config_t ni_decoder_config_t
_ni_encoder_cfg_params::colorSpace
int colorSpace
Definition: ni_device_api.h:2328
_ni_session_context::max_frame_delay
int max_frame_delay
Definition: ni_device_api.h:1696
_ni_gop_params::rps
ni_gop_rps_t rps[NI_MAX_REF_PIC]
Definition: ni_device_api.h:1974
CONFIG_INSTANCE_SetEOS_W
#define CONFIG_INSTANCE_SetEOS_W(sid, instance)
Definition: ni_nvme.h:804
ni_rsrc_priv.h
Private definitions used by ni_rsrc_api.cpp for management of NETINT video processing devices.
SwapSW64
void SwapSW64(uint64_t *buf, uint64_t bytes)
Definition: ni_device_api_priv.c:212
NI_DEC_CROP_MODE_MANUAL
@ NI_DEC_CROP_MODE_MANUAL
Definition: ni_device_api.h:932
_ni_t408_config_t::pu32IntraDcDeltaRate
int32_t pu32IntraDcDeltaRate
Definition: ni_device_api_priv.h:469
NI_RETCODE_PARAM_ERROR_USR_RMD_ENC_PARAM
@ NI_RETCODE_PARAM_ERROR_USR_RMD_ENC_PARAM
Definition: ni_defs.h:485
NI_RETCODE_NVME_SC_INVALID_PARAMETER
@ NI_RETCODE_NVME_SC_INVALID_PARAMETER
Definition: ni_defs.h:556
NI_FW_ENC_BITSTREAM_META_DATA_SIZE_UNDER_MAJOR_6_MINOR_o
#define NI_FW_ENC_BITSTREAM_META_DATA_SIZE_UNDER_MAJOR_6_MINOR_o
Definition: ni_device_api_priv.h:840
_ni_packet::average_psnr
double average_psnr
Definition: ni_device_api.h:2888
_ni_encoder_cfg_params::bitrateWindow
int bitrateWindow
Definition: ni_device_api.h:2345
NI_RETCODE_ERROR_INVALID_SESSION
@ NI_RETCODE_ERROR_INVALID_SESSION
Definition: ni_defs.h:445
_ni_nvme_identity::xcoder_num_h264_decoder_hw
uint8_t xcoder_num_h264_decoder_hw
Definition: ni_nvme.h:160
_ni_session_context::pkt_offsets_index_min
uint64_t pkt_offsets_index_min[NI_FIFO_SZ]
Definition: ni_device_api.h:1454
_ni_t408_config_t::scalingListEnable
int32_t scalingListEnable
Definition: ni_device_api_priv.h:391
ni_hwupload_session_write
int ni_hwupload_session_write(ni_session_context_t *p_ctx, ni_frame_t *p_frame, niFrameSurface1_t *hwdesc)
Send a YUV p_frame to upload session.
Definition: ni_device_api_priv.c:13801
NI_PIX_FMT_ABGR
@ NI_PIX_FMT_ABGR
Definition: ni_device_api.h:270
_ni_t408_config_t::bgDetectEnable
uint32_t bgDetectEnable
Definition: ni_device_api_priv.h:447
hwdl_frame
int hwdl_frame(ni_session_context_t *p_ctx, ni_session_data_io_t *p_session_data, ni_frame_t *p_src_frame, int output_format)
Download hw frames by HwDesc.
Definition: ni_generic_utils.c:708
ni_session_statistic_t
struct _ni_session_statistic_t ni_session_statistic_t
NI_MAX_ENCODER_QUERY_RETRIES
#define NI_MAX_ENCODER_QUERY_RETRIES
Definition: ni_device_api.h:66
_ni_load_query::total_pixel_load
uint32_t total_pixel_load
Definition: ni_device_api.h:1219
QUERY_GENERAL_GET_STATUS_R
#define QUERY_GENERAL_GET_STATUS_R(instance)
Definition: ni_nvme.h:781
NI_PIX_FMT_ARGB
@ NI_PIX_FMT_ARGB
Definition: ni_device_api.h:269
_ni_encoder_config_t::i32pbRatio
int32_t i32pbRatio
Definition: ni_device_api_priv.h:628
_ni_xcoder_params
Definition: ni_device_api.h:2743
TP_LOG_OFFSET_IN_4K
#define TP_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:663
NI_RETRY_INTERVAL_100US
#define NI_RETRY_INTERVAL_100US
Definition: ni_device_api_priv.h:835
ni_nvme_check_error_code
ni_retcode_t ni_nvme_check_error_code(int rc, int opcode, uint32_t xcoder_type, uint32_t hw_id, uint32_t *p_instance_id)
Check f/w error return code, and if it's a fatal one, terminate application's decoding/encoding proce...
Definition: ni_nvme.c:62
ni_session_config_rw_t
struct _ni_session_config_rw ni_session_config_rw_t
_ni_metadata_enc_frame::get_recon_frame_mode
uint8_t get_recon_frame_mode
Definition: ni_device_api_priv.h:259
_ni_frame::sei_hdr_content_light_level_info_len
unsigned int sei_hdr_content_light_level_info_len
Definition: ni_device_api.h:2664
_ni_encoder_config_t::ui8enablePTS
uint8_t ui8enablePTS
Definition: ni_device_api_priv.h:526
_ni_encoder_config_t::i32userMaxDeltaQp
int32_t i32userMaxDeltaQp
Definition: ni_device_api_priv.h:519
EP_CORE
@ EP_CORE
Definition: ni_defs.h:379
NI_RETCODE_PARAM_ERROR_WIDTH_TOO_SMALL
@ NI_RETCODE_PARAM_ERROR_WIDTH_TOO_SMALL
Definition: ni_defs.h:508
_ni_encoder_config_t::i8crf
int8_t i8crf
Definition: ni_device_api_priv.h:542
_ni_decoder_input_params_t::reduce_dpb_delay
int reduce_dpb_delay
Definition: ni_device_api.h:2561
_ni_metadata_enc_bstream::min_mv_x
int16_t min_mv_x[2]
Definition: ni_device_api_priv.h:289
_ni_encoder_cfg_params::max_delta_qp
int max_delta_qp
Definition: ni_device_api.h:2396
_ni_xcoder_params::staticMmapThreshold
int staticMmapThreshold
Definition: ni_device_api.h:2810
_ni_t408_config_t::tcOffsetDiv2
int32_t tcOffsetDiv2
Definition: ni_device_api_priv.h:399
_ni_encoder_cfg_params::ctbRowQpStep
int ctbRowQpStep
Definition: ni_device_api.h:2404
_ni_encoder_config_t::ui32cropWidth
uint32_t ui32cropWidth
Definition: ni_device_api_priv.h:615
_ni_encoder_config_t::ui8HDR10CLLEnable
uint8_t ui8HDR10CLLEnable
Definition: ni_device_api_priv.h:545
_ni_encoder_config_t::ui8enableSmoothCrf
uint8_t ui8enableSmoothCrf
Definition: ni_device_api_priv.h:648
CONFIG_INSTANCE_SetAiPara_W
#define CONFIG_INSTANCE_SetAiPara_W(sid, instance)
Definition: ni_nvme.h:846
_ni_session_context::low_delay_sync_cond
ni_pthread_cond_t low_delay_sync_cond
Definition: ni_device_api.h:1668
_ni_frame::sei_hdr_mastering_display_color_vol_len
unsigned int sei_hdr_mastering_display_color_vol_len
Definition: ni_device_api.h:2662
_ni_encoder_cfg_params::motionConstrainedMode
int motionConstrainedMode
Definition: ni_device_api.h:2446
ni_hwupload_session_query_buffer_avail
ni_retcode_t ni_hwupload_session_query_buffer_avail(ni_session_context_t *p_ctx)
Query and acquire buffer from xcoder upload instance.
Definition: ni_device_api_priv.c:13696
NI_SESSION_CLOSE_RETRY_MAX
#define NI_SESSION_CLOSE_RETRY_MAX
Definition: ni_device_api_priv.h:817
_ni_session_context::ready_to_close
uint32_t ready_to_close
Definition: ni_device_api.h:1546
_ni_session_context::overall_load_query
ni_overall_load_query_t overall_load_query
Definition: ni_device_api.h:1505
OPEN_ADD_CODEC
#define OPEN_ADD_CODEC(instance, codec, param)
Definition: ni_nvme.h:695
_ni_encoder_config_t::ui8vbvBufferReencode
uint8_t ui8vbvBufferReencode
Definition: ni_device_api_priv.h:654
_ni_encoder_config_t::i8ppsInitQp
int8_t i8ppsInitQp
Definition: ni_device_api_priv.h:631
_ni_encoder_cfg_params::decoding_refresh_type
int decoding_refresh_type
Definition: ni_device_api.h:2374
_ni_encoder_config_t::ui8picSkipEnable
uint8_t ui8picSkipEnable
Definition: ni_device_api_priv.h:566
NI_RETCODE_NVME_SC_VPU_RSRC_INSUFFICIENT
@ NI_RETCODE_NVME_SC_VPU_RSRC_INSUFFICIENT
Definition: ni_defs.h:560
_ni_encoder_config_t::ui8blockRCSize
uint8_t ui8blockRCSize
Definition: ni_device_api_priv.h:581
_ni_device_capability::xcoder_devices_cnt
uint8_t xcoder_devices_cnt
Definition: ni_device_api.h:1159
NI_ENC_MIN_RESOLUTION_HEIGHT
#define NI_ENC_MIN_RESOLUTION_HEIGHT
Definition: ni_device_api.h:89
INST_BUF_INFO_RW_READ
@ INST_BUF_INFO_RW_READ
Definition: ni_device_api_priv.h:48
ni_xcoder_session_query
int ni_xcoder_session_query(ni_session_context_t *p_ctx, ni_device_type_t device_type)
Query current xcoder status.
Definition: ni_device_api_priv.c:3112
_ni_encoder_config_t::ui32chromaLinesize
uint32_t ui32chromaLinesize
Definition: ni_device_api_priv.h:614
enqueue_ni_frame
void enqueue_ni_frame(ni_session_context_t *p_ctx, ni_frame_t *ni_frame, int32_t source_width, int32_t source_height)
Definition: ni_device_api_priv.c:3958
_ni_load_query::fw_load
uint32_t fw_load
Definition: ni_device_api.h:1202
ni_encoder_start_buffer_alloc
ni_retcode_t ni_encoder_start_buffer_alloc(ni_frame_t *p_frame)
Allocate memory for the non-4k-aligned part at the start of YUV data for encoder input data.
Definition: ni_device_api_priv.c:18220
_ni_device_capability::fw_rev
uint8_t fw_rev[8]
Definition: ni_device_api.h:1166
_ni_encoder_cfg_params::crop_width
int crop_width
Definition: ni_device_api.h:2409
GOP_PRESET_IDX_IBBBP
@ GOP_PRESET_IDX_IBBBP
Definition: ni_device_api_priv.h:325
_ni_thread_arg_struct_t
Definition: ni_device_api.h:1259
_ni_nvme_identity::fw_build_id
uint8_t fw_build_id[256]
Definition: ni_nvme.h:222
_ni_device_vf_ns_id
Definition: ni_rsrc_api.h:229
_ni_encoder_cfg_params::slice_mode
int slice_mode
Definition: ni_device_api.h:2371
XCODER_MIN_ENC_PIC_HEIGHT
#define XCODER_MIN_ENC_PIC_HEIGHT
Definition: ni_util.h:113
CPU_LOG_BUFFER_SIZE
#define CPU_LOG_BUFFER_SIZE
Definition: ni_defs.h:371
_ni_encoder_cfg_params::HDR10minluma
int HDR10minluma
Definition: ni_device_api.h:2297
ni_encoder_metadata_buffer_alloc
ni_retcode_t ni_encoder_metadata_buffer_alloc(ni_frame_t *p_frame, int extra_len)
Allocate memory for the metadata header and auxillary data for encoder input data.
Definition: ni_device_api_priv.c:18133
ni_uploader_session_close
ni_retcode_t ni_uploader_session_close(ni_session_context_t *p_ctx)
Close an xcoder upload instance.
Definition: ni_device_api_priv.c:15537
_ni_session_context::codec_format
uint32_t codec_format
Definition: ni_device_api.h:1488
_ni_device_capability
device capability type
Definition: ni_device_api.h:1155
NI_RETRY_INTERVAL_200US
#define NI_RETRY_INTERVAL_200US
Definition: ni_device_api_priv.h:834
NI_MAX_NUM_OF_DECODER_OUTPUTS
#define NI_MAX_NUM_OF_DECODER_OUTPUTS
Definition: ni_defs.h:255
_ni_metadata_enc_frame::frame_force_type
uint16_t frame_force_type
Definition: ni_device_api_priv.h:248
_ni_decoder_config_t::ui8DisableAdaptiveBuffers
uint8_t ui8DisableAdaptiveBuffers
Definition: ni_device_api_priv.h:743
_ni_encoder_config_t::ui8intraResetRefresh
uint8_t ui8intraResetRefresh
Definition: ni_device_api_priv.h:607
_ni_t408_config_t::max_num_merge
int32_t max_num_merge
Definition: ni_device_api_priv.h:395
ALL_CORE
@ ALL_CORE
Definition: ni_defs.h:377
_ni_encoder_config_t::i8hvsBaseMbComplexity
int8_t i8hvsBaseMbComplexity
Definition: ni_device_api_priv.h:635
_ni_session_context::scaler_operation
uint32_t scaler_operation
Definition: ni_device_api.h:1612
ni_posix_memalign
int ni_posix_memalign(void **memptr, size_t alignment, size_t size)
Allocate aligned memory.
Definition: ni_util.c:198
_ni_t408_config_t::intraRefreshArg
int32_t intraRefreshArg
Definition: ni_device_api_priv.h:389
_ni_session_context::stream_dir_name
char stream_dir_name[256]
Definition: ni_device_api.h:1610
_ni_encoder_config_t::ui32lumaLinesize
uint32_t ui32lumaLinesize
Definition: ni_device_api_priv.h:613
NI_RETCODE_ERROR_UNSUPPORTED_FEATURE
@ NI_RETCODE_ERROR_UNSUPPORTED_FEATURE
Definition: ni_defs.h:534
_ni_session_context::itu_t_t35_hdr10p_sei_hdr_hevc
uint8_t itu_t_t35_hdr10p_sei_hdr_hevc[NI_HDR10P_SEI_HDR_HEVC_LEN]
Definition: ni_device_api.h:1421
_ni_encoder_config_t::i8maxConsecutiveSkipFrameNum
int8_t i8maxConsecutiveSkipFrameNum
Definition: ni_device_api_priv.h:600
QUERY_INSTANCE_NL_V2_R
#define QUERY_INSTANCE_NL_V2_R(sid, instance)
Definition: ni_nvme.h:769
_ni_nvme_identity::xcoder_num_hw
uint8_t xcoder_num_hw
Definition: ni_nvme.h:159
_ni_session_context::psnr_y
double psnr_y
Definition: ni_device_api.h:1712
NI_RETCODE_PARAM_ERROR_PIC_HEIGHT
@ NI_RETCODE_PARAM_ERROR_PIC_HEIGHT
Definition: ni_defs.h:494
_ni_encoder_cfg_params::colorPrimaries
int colorPrimaries
Definition: ni_device_api.h:2326
_ni_decoder_input_params_t::scale_wh
int scale_wh[NI_MAX_NUM_OF_DECODER_OUTPUTS][2]
Definition: ni_device_api.h:2532
CONFIG_INSTANCE_SetDecPara_W
#define CONFIG_INSTANCE_SetDecPara_W(sid, instance)
Definition: ni_nvme.h:809
_ni_t408_config_t::customLambdaEnable
uint32_t customLambdaEnable
Definition: ni_device_api_priv.h:453
ni_log2
void ni_log2(const void *p_context, ni_log_level_t level, const char *fmt,...)
print log message and additional information using ni_log_callback,
Definition: ni_log.c:337
_ni_instance_mgr_general_status::fw_video_shared_mem_usage
uint8_t fw_video_shared_mem_usage
Definition: ni_device_api_priv.h:98
NI_RETCODE_PARAM_ERROR_BRATE
@ NI_RETCODE_PARAM_ERROR_BRATE
Definition: ni_defs.h:452
NI_AI_FLAG_IOVEC
#define NI_AI_FLAG_IOVEC
Definition: ni_device_api.h:304
_ni_encoder_cfg_params::forceBframeQpfactor
float forceBframeQpfactor
Definition: ni_device_api.h:2441
_ni_metadata_enc_frame::frame_sei_data_size
uint32_t frame_sei_data_size
Definition: ni_device_api_priv.h:245
_ni_network_layer_params_t::quant_format
int32_t quant_format
Definition: ni_device_api.h:1335
_ni_nvme_identity_xcoder_hw::hw_video_level
uint8_t hw_video_level
Definition: ni_nvme.h:84
_ni_load_query
Definition: ni_device_api.h:1198
_ni_session_context::is_auto_dl
uint8_t is_auto_dl
Definition: ni_device_api.h:1472
FP_LOG_OFFSET_IN_4K
#define FP_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:664
_ni_thread_arg_struct_t::session_id
uint32_t session_id
Definition: ni_device_api.h:1262
_ni_encoder_cfg_params::crfFloat
float crfFloat
Definition: ni_device_api.h:2424
_ni_metadata_enc_bstream::recycle_index
uint32_t recycle_index
Definition: ni_device_api_priv.h:281
_ni_t408_config_t::nrIntraWeightY
uint32_t nrIntraWeightY
Definition: ni_device_api_priv.h:428
_ni_encoder_cfg_params::long_term_ref_count
int long_term_ref_count
Definition: ni_device_api.h:2306
_ni_t408_config_t::cu32InterDeltaRate
int32_t cu32InterDeltaRate
Definition: ni_device_api_priv.h:478
_ni_encoder_config_t::ui32vbvMaxRate
uint32_t ui32vbvMaxRate
Definition: ni_device_api_priv.h:630
NI_CC_SEI_HDR_HEVC_LEN
#define NI_CC_SEI_HDR_HEVC_LEN
Definition: ni_device_api.h:463
ni_nvme.h
Private definitions for interfacing with NETINT video processing devices over NVMe.
_ni_t408_config_t::custom_gop_params
ni_custom_gop_params_t custom_gop_params
Definition: ni_device_api_priv.h:411
ni_query_detail_status
int ni_query_detail_status(ni_session_context_t *p_ctx, ni_device_type_t device_type, void *p_detail_status, int ver)
Query a particular xcoder instance to get DetailStatus data.
Definition: ni_device_api_priv.c:6070
_niFrameSurface1::ui16width
uint16_t ui16width
Definition: ni_device_api.h:2827
_ni_session_context::flags_array
int flags_array[NI_FIFO_SZ]
Definition: ni_device_api.h:1646
_ni_instance_mgr_allocation_info::picture_width
uint16_t picture_width
Definition: ni_device_api_priv.h:158
ni_scaler_params_t
struct _ni_scaler_params_t ni_scaler_params_t
QUERY_INSTANCE_NL_R
#define QUERY_INSTANCE_NL_R(sid, instance)
Definition: ni_nvme.h:757
_ni_metadata_enc_bstream::max_mv_y
int16_t max_mv_y[2]
Definition: ni_device_api_priv.h:290
_ni_thread_arg_struct_t::thread_event_handle
ni_event_handle_t thread_event_handle
Definition: ni_device_api.h:1267
_ni_instance_mgr_stream_info::transfer_frame_stride
uint16_t transfer_frame_stride
Definition: ni_device_api_priv.h:126
_ni_encoder_config_t::i32ltrRefQpOffset
int32_t i32ltrRefQpOffset
Definition: ni_device_api_priv.h:568
_ni_session_context::load_query
ni_load_query_t load_query
Definition: ni_device_api.h:1502
_ni_session_statistic_t::ui32FramesDropped
uint32_t ui32FramesDropped
Definition: ni_defs.h:395
_ni_session_data_io::frame
ni_frame_t frame
Definition: ni_device_api.h:2900
_ni_network_buffer_info::ui8MultiIn
uint8_t ui8MultiIn
Definition: ni_device_api_priv.h:771
_ni_encoder_config_t::ui16hdr10_dx0
uint16_t ui16hdr10_dx0
Definition: ni_device_api_priv.h:589
_ni_t408_config_t::initialRcQp
int32_t initialRcQp
Definition: ni_device_api_priv.h:421
_ni_metadata_enc_bstream::frame_tstamp
uint64_t frame_tstamp
Definition: ni_device_api_priv.h:278
_ni_decoder_input_params_t::enable_out2
int enable_out2
Definition: ni_device_api.h:2522
_ni_encoder_config_t::ui8rdoLevel
uint8_t ui8rdoLevel
Definition: ni_device_api_priv.h:541
_ni_encoder_cfg_params::level_idc
int level_idc
Definition: ni_device_api.h:2263
_ni_scaler_params_t::filterblit
int filterblit
Definition: ni_device_api.h:2589
_ni_encoder_cfg_params::HDR10dy2
int HDR10dy2
Definition: ni_device_api.h:2293
INST_BUF_INFO_RW_READ_BUSY
@ INST_BUF_INFO_RW_READ_BUSY
Definition: ni_device_api_priv.h:51
_ni_instance_mgr_allocation_info
Definition: ni_device_api_priv.h:156
_ni_t408_config_t::intra_mb_refresh_mode
int32_t intra_mb_refresh_mode
Definition: ni_device_api_priv.h:495
NI_NUM_OF_PIXELS_1440P
#define NI_NUM_OF_PIXELS_1440P
Definition: ni_device_api.h:78
_ni_uploader_config_t::ui16picHeight
uint16_t ui16picHeight
Definition: ni_device_api_priv.h:667
_niFrameSurface1::output_idx
int8_t output_idx
Definition: ni_device_api.h:2833
NI_H265_USER_DATA_FLAG_CONTENT_LIGHT_LEVEL_INFO
@ NI_H265_USER_DATA_FLAG_CONTENT_LIGHT_LEVEL_INFO
Definition: ni_device_api.h:376
NI_SCALER_FLAG_P2
#define NI_SCALER_FLAG_P2
Definition: ni_device_api.h:295
_ni_segment
Definition: ni_device_api_priv.h:757
_ni_thread_arg_struct_t::session_timestamp
uint64_t session_timestamp
Definition: ni_device_api.h:1263
_ni_nvme_identity_xcoder_hw::hw_codec_format
uint8_t hw_codec_format
Definition: ni_nvme.h:77
_ni_encoder_config_t::ui8hdr10_enable
uint8_t ui8hdr10_enable
Definition: ni_device_api_priv.h:588
_ni_session_context::pts_table
ni_timestamp_table_t * pts_table
Definition: ni_device_api.h:1517
_ni_metadata_enc_frame::frame_force_type_enable
uint16_t frame_force_type_enable
Definition: ni_device_api_priv.h:247
_ni_metadata_enc_bstream::min_mv_y
int16_t min_mv_y[2]
Definition: ni_device_api_priv.h:291
COMPILE_ASSERT
#define COMPILE_ASSERT(condition)
Definition: ni_util.h:102
NI_MAX_NUM_SW_FRAME_DATA_POINTERS
#define NI_MAX_NUM_SW_FRAME_DATA_POINTERS
Definition: ni_defs.h:246
CONFIG_SESSION_FRAME_COPY_W
#define CONFIG_SESSION_FRAME_COPY_W(sid)
Definition: ni_nvme.h:903
NI_RETCODE_PARAM_ERROR_CUSTOM_GOP
@ NI_RETCODE_PARAM_ERROR_CUSTOM_GOP
Definition: ni_defs.h:490
CONFIG_INSTANCE_SetAiHVSPlus_W
#define CONFIG_INSTANCE_SetAiHVSPlus_W(sid, instance)
Definition: ni_nvme.h:851
_ni_encoder_cfg_params::preIntraHandling
int preIntraHandling
Definition: ni_device_api.h:2459
_ni_frameclone_desc::ui32Size
uint32_t ui32Size
Definition: ni_device_api.h:1382
_ni_t408_config_t::timeScale
uint32_t timeScale
Definition: ni_device_api_priv.h:415
_ni_encoder_config_t::ui16hdr10_dy0
uint16_t ui16hdr10_dy0
Definition: ni_device_api_priv.h:590
_ni_encoder_cfg_params::baseLayerOnly
int baseLayerOnly
Definition: ni_device_api.h:2460
_ni_encoder_config_t::ui8linkFrameMaxIntraRatio
uint8_t ui8linkFrameMaxIntraRatio
Definition: ni_device_api_priv.h:660
_ni_encoder_cfg_params::maxFrameSize
int maxFrameSize
Definition: ni_device_api.h:2257
_ni_encoder_cfg_params::hvs_qp_scale
int hvs_qp_scale
Definition: ni_device_api.h:2389
_ni_metadata_enc_bstream::intra_total_count
uint16_t intra_total_count
Definition: ni_device_api_priv.h:294
_ni_encoder_cfg_params::cplxDecay
float cplxDecay
Definition: ni_device_api.h:2420
_ni_packet::av1_show_frame
int av1_show_frame
Definition: ni_device_api.h:2879
_ni_instance_mgr_general_status
Definition: ni_device_api_priv.h:79
atoi
#define atoi(p_str)
Definition: ni_device_api.c:7263
_ni_network_buffer_info::ui16Width
uint16_t ui16Width
Definition: ni_device_api_priv.h:765
_ni_t408_config_t::nrYEnable
uint32_t nrYEnable
Definition: ni_device_api_priv.h:423
_ni_uploader_config_t::ui16picWidth
uint16_t ui16picWidth
Definition: ni_device_api_priv.h:666
ni_get_hw_yuv420p_dim
void ni_get_hw_yuv420p_dim(int width, int height, int factor, int is_semiplanar, int plane_stride[NI_MAX_NUM_DATA_POINTERS], int plane_height[NI_MAX_NUM_DATA_POINTERS])
Get dimension information of Netint HW YUV420p frame to be sent to encoder for encoding....
Definition: ni_util.c:2040
_ni_frame::sei_user_data_unreg_offset
unsigned int sei_user_data_unreg_offset
Definition: ni_device_api.h:2669
NI_PARAM_MAX_WIDTH
#define NI_PARAM_MAX_WIDTH
Definition: ni_device_api.h:135
_ni_t408_config_t::bitAllocMode
int32_t bitAllocMode
Definition: ni_device_api_priv.h:403
END
#define END
Definition: ni_defs.h:336
_ni_encoder_cfg_params::enable_hvs_qp
int enable_hvs_qp
Definition: ni_device_api.h:2388
_ni_t408_config_t::bgDeltaQp
int32_t bgDeltaQp
Definition: ni_device_api_priv.h:451
_ni_frame::video_height
uint32_t video_height
Definition: ni_device_api.h:2637
ni_config_instance_set_decoder_params
ni_retcode_t ni_config_instance_set_decoder_params(ni_session_context_t *p_ctx, uint32_t max_pkt_size)
Send a p_config command to configure decoding parameters.
Definition: ni_device_api_priv.c:15667
_ni_t408_config_t::pu08IntraDcDeltaRate
int32_t pu08IntraDcDeltaRate
Definition: ni_device_api_priv.h:463
_ni_network_data::inset
ni_network_layer_offset_t * inset
Definition: ni_device_api.h:1371
_ni_decoder_input_params_t::force_8_bit
int force_8_bit[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2525
_ni_t408_config_t::cu_size_mode
int32_t cu_size_mode
Definition: ni_device_api_priv.h:392
_ni_encoder_cfg_params::qcomp
float qcomp
Definition: ni_device_api.h:2414
_ni_session_context::low_delay_sync_flag
int low_delay_sync_flag
Definition: ni_device_api.h:1666
NI_CUSTOMIZE_ROI_QPOFFSET_LEVEL
#define NI_CUSTOMIZE_ROI_QPOFFSET_LEVEL
Max number of lines supported for qpoffset level.
Definition: ni_device_api.h:481
_ni_packet::pos
long long pos
Definition: ni_device_api.h:2859
_ni_t408_config_t::lfCrossSliceBoundaryEnable
int32_t lfCrossSliceBoundaryEnable
Definition: ni_device_api_priv.h:397
_ni_frame_config::output_index
uint8_t output_index
Definition: ni_device_api.h:2851
_ni_encoder_cfg_params::avcc_hvcc
int avcc_hvcc
Definition: ni_device_api.h:2454
_ni_encoder_config_t::ui8sliceMode
uint8_t ui8sliceMode
Definition: ni_device_api_priv.h:513
NI_CC_SEI_BYTE4
#define NI_CC_SEI_BYTE4
Definition: ni_device_api.h:449
_ni_input_frame::p_input_buffer
uint8_t * p_input_buffer
Definition: ni_device_api.h:1400
_ni_xcoder_params::ddr_priority_mode
ni_ddr_priority_mode_t ddr_priority_mode
Definition: ni_device_api.h:2817
_ni_encoder_cfg_params::colorTrc
int colorTrc
Definition: ni_device_api.h:2327
ni_encoder_session_send_eos
ni_retcode_t ni_encoder_session_send_eos(ni_session_context_t *p_ctx)
Flush encoder output.
Definition: ni_device_api_priv.c:3751
_ni_session_context::hvsplus_level
int hvsplus_level
encoder:calculate PSNR end
Definition: ni_device_api.h:1717
_ni_session_context::active_video_height
uint32_t active_video_height
Definition: ni_device_api.h:1553
_ni_encoder_config_t::ui8gopLowdelay
uint8_t ui8gopLowdelay
Definition: ni_device_api_priv.h:551
ni_dump_log_all_cores
ni_retcode_t ni_dump_log_all_cores(ni_session_context_t *p_ctx, void *p_data, bool gen_log_file)
Definition: ni_device_api_priv.c:18537
NI_MAX_SEI_DATA
#define NI_MAX_SEI_DATA
Definition: ni_device_api.h:428
_ni_session_context::pkt_delay_cnt
uint32_t pkt_delay_cnt
Definition: ni_device_api.h:1704
ni_queue_free
ni_retcode_t ni_queue_free(ni_queue_t *p_queue, ni_queue_buffer_pool_t *p_buffer_pool)
Free xcoder queue.
Definition: ni_util.c:1811
_ni_t408_config_t::intra_qp
int32_t intra_qp
Definition: ni_device_api_priv.h:378
_ni_encoder_cfg_params::av1_error_resilient_mode
int av1_error_resilient_mode
Definition: ni_device_api.h:2402
NI_CC_SEI_BYTE6
#define NI_CC_SEI_BYTE6
Definition: ni_device_api.h:451
_ni_session_context::enc_pts_r_idx
int64_t enc_pts_r_idx
Definition: ni_device_api.h:1442
_ni_encoder_cfg_params::tune_bframe_visual
int tune_bframe_visual
Definition: ni_device_api.h:2442
ni_session_keep_alive_thread
void * ni_session_keep_alive_thread(void *arguments)
decoder keep alive thread function triggers every 1 second
Definition: ni_device_api_priv.c:13148
ni_instance_mgr_stream_complete_t
struct _ni_instance_mgr_stream_complete ni_instance_mgr_stream_complete_t
_ni_encoder_cfg_params::conf_win_left
int conf_win_left
Definition: ni_device_api.h:2313
NI_MAX_FRAME_SIZE
#define NI_MAX_FRAME_SIZE
Definition: ni_device_api.h:178
_ni_encoder_cfg_params::pass1_qp
int pass1_qp
Definition: ni_device_api.h:2423
_ni_session_context::rc_error_count
int rc_error_count
Definition: ni_device_api.h:1535
_ni_encoder_cfg_params::conf_win_right
int conf_win_right
Definition: ni_device_api.h:2314
_ni_encoder_config_t::ui8rcQpDeltaRange
uint8_t ui8rcQpDeltaRange
Definition: ni_device_api_priv.h:583
CONFIG_INSTANCE_UploadModel_W
#define CONFIG_INSTANCE_UploadModel_W(sid, instance)
Definition: ni_nvme.h:909
_ni_session_context::blk_dev_name
char blk_dev_name[NI_MAX_DEVICE_NAME_LEN]
Definition: ni_device_api.h:1661
_ni_decoder_input_params_t::enable_low_delay_check
int enable_low_delay_check
Definition: ni_device_api.h:2540
NI_POOL_TYPE_NONE
@ NI_POOL_TYPE_NONE
Definition: ni_device_api.h:493
_ni_session_context::biggest_bitstream_buffer_allocated
uint32_t biggest_bitstream_buffer_allocated
Definition: ni_device_api.h:1557
_ni_session_context::session_run_state
ni_session_run_state_t session_run_state
Definition: ni_device_api.h:1549
NI_NVME_IDENTITY_CMD_DATA_SZ
#define NI_NVME_IDENTITY_CMD_DATA_SZ
Definition: ni_nvme.h:38
ni_query_stream_info
ni_retcode_t ni_query_stream_info(ni_session_context_t *p_ctx, ni_device_type_t device_type, ni_instance_mgr_stream_info_t *p_stream_info)
Query a particular xcoder instance to get Stream Info data.
Definition: ni_device_api_priv.c:6152
_ni_session_context::enable_low_delay_check
int enable_low_delay_check
Definition: ni_device_api.h:1597
_ni_t408_config_t::lambdaScalingEnable
int32_t lambdaScalingEnable
Definition: ni_device_api_priv.h:491
_ni_session_context::sent_size
uint32_t sent_size
Definition: ni_device_api.h:1510
_niFrameSurface1::device_handle
int32_t device_handle
Definition: ni_device_api.h:2830
_ni_encoder_cfg_params::crfMax
int crfMax
Definition: ni_device_api.h:2413
NI_CC_SEI_TRAILER_LEN
#define NI_CC_SEI_TRAILER_LEN
Definition: ni_device_api.h:467
_ni_encoder_config_t::ui8bitrateMode
uint8_t ui8bitrateMode
Definition: ni_device_api_priv.h:632
_ni_metadata_common::frame_height
uint16_t frame_height
Definition: ni_device_api_priv.h:220
_niFrameSurface1::bit_depth
int8_t bit_depth
Definition: ni_device_api.h:2831
_ni_t408_config_t::weightPredEnable
uint32_t weightPredEnable
Definition: ni_device_api_priv.h:446
_ni_decoder_config_t::fps_denominator
uint32_t fps_denominator
Definition: ni_device_api_priv.h:734
_ni_metadata_enc_bstream::frameCropLeftOffset
uint16_t frameCropLeftOffset
Definition: ni_device_api_priv.h:303
_ni_metadata_enc_bstream_rev61::av1_show_frame
uint32_t av1_show_frame
Definition: ni_device_api_priv.h:271
_ni_p2p_sgl_t::ui32DMALen
uint32_t ui32DMALen[NI_MAX_P2P_SGL_ENTRY]
Definition: ni_defs.h:418
_ni_t408_config_t::nrInterWeightCb
uint32_t nrInterWeightCb
Definition: ni_device_api_priv.h:432
_ni_encoder_cfg_params::cuTreeFactor
int cuTreeFactor
Definition: ni_device_api.h:2417
_ni_network_layer_info::out_param
ni_network_layer_params_t * out_param
Definition: ni_device_api.h:1357
_ni_encoder_cfg_params::enableipRatio
int enableipRatio
Definition: ni_device_api.h:2434
NI_CODEC_HW_PAYLOAD_OFFSET
@ NI_CODEC_HW_PAYLOAD_OFFSET
Definition: ni_device_api.h:946
_ni_decoder_input_params_t::scale_resolution_ceil
int scale_resolution_ceil[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2555
ni_device_api.h
Public definitions for operating NETINT video processing devices for video processing.
_ni_device_capability::serial_number
uint8_t serial_number[20]
Definition: ni_device_api.h:1163
_ni_t408_config_t::rdoSkip
int32_t rdoSkip
Definition: ni_device_api_priv.h:490
NI_MAX_RESOLUTION_RGBA_HEIGHT
#define NI_MAX_RESOLUTION_RGBA_HEIGHT
Definition: ni_device_api.h:104
_ni_instance_upload_ret_hwdesc::frame_index
int16_t frame_index
Definition: ni_device_api_priv.h:185
_ni_session_context::meta_size
uint32_t meta_size
Params used in VFR mode Done///.
Definition: ni_device_api.h:1640
_ni_encoder_cfg_params::vbv_min_rate
int vbv_min_rate
Definition: ni_device_api.h:2438
NI_DEVICE_TYPE_XCODER_MAX
@ NI_DEVICE_TYPE_XCODER_MAX
Definition: ni_defs.h:362
CONFIG_INSTANCE_SetSOS_W
#define CONFIG_INSTANCE_SetSOS_W(sid, instance)
Definition: ni_nvme.h:802
_ni_t408_config_t::pu04IntraDcDeltaRate
int32_t pu04IntraDcDeltaRate
Definition: ni_device_api_priv.h:460
ni_get_core_name
char * ni_get_core_name(ni_core_type_t eCoreType)
Definition: ni_device_api_priv.c:18361
_ni_encoder_cfg_params::HDR10MaxLight
int HDR10MaxLight
Definition: ni_device_api.h:2284
_ni_encoder_config_t::ui8ctbRcMode
uint8_t ui8ctbRcMode
Definition: ni_device_api_priv.h:548
_ni_xcoder_params::cacheRoi
int cacheRoi
Definition: ni_device_api.h:2777
_ni_nvme_identity_xcoder_hw::hw_id
uint8_t hw_id
Definition: ni_nvme.h:74
_ni_session_context::session_timestamp
uint64_t session_timestamp
Definition: ni_device_api.h:1484
_ni_session_context::psnr_u
double psnr_u
Definition: ni_device_api.h:1713
_ni_dec_mastering_display_colour_volume_bytes::white_point_x
uint16_t white_point_x
Definition: ni_device_api.h:1066
_ni_encoder_config_t::ui8motionConstrainedMode
uint8_t ui8motionConstrainedMode
Definition: ni_device_api_priv.h:644
_ni_encoder_cfg_params::still_image_detect_level
int still_image_detect_level
Definition: ni_device_api.h:2447
_ni_encoder_cfg_params::crop_height
int crop_height
Definition: ni_device_api.h:2410
_ni_hw_capability::codec_format
uint8_t codec_format
Definition: ni_device_api.h:1141
_ni_frame_config::rectangle_width
uint16_t rectangle_width
Definition: ni_device_api.h:2844
_ni_session_context::max_nvme_io_size
uint32_t max_nvme_io_size
Definition: ni_device_api.h:1478
_ni_session_context::enable_user_data_sei_passthru
int enable_user_data_sei_passthru
Definition: ni_device_api.h:1616
_ni_encoder_config_t::ui8videoFullRange
uint8_t ui8videoFullRange
Definition: ni_device_api_priv.h:572
_ni_encoder_cfg_params::aspectRatioHeight
int aspectRatioHeight
Definition: ni_device_api.h:2255
ni_config_instance_set_encoder_frame_params
ni_retcode_t ni_config_instance_set_encoder_frame_params(ni_session_context_t *p_ctx, ni_encoder_frame_params_t *p_params)
Send a p_config command to configure encoding p_frame parameters.
Definition: ni_device_api_priv.c:7379
NI_RETCODE_EAGAIN
@ NI_RETCODE_EAGAIN
Definition: ni_defs.h:541
_ni_timestamp_table_t
Definition: ni_device_api.h:1323
_ni_dec_mastering_display_colour_volume_bytes::max_display_mastering_luminance
uint32_t max_display_mastering_luminance
Definition: ni_device_api.h:1068
NI_MIN_HEIGHT
#define NI_MIN_HEIGHT
Definition: ni_device_api.h:126
_ni_overall_load_query::admin_queried
uint32_t admin_queried
Definition: ni_device_api.h:1229
_ni_t408_config_t::minQpI
int32_t minQpI
Definition: ni_device_api_priv.h:481
NI_RETCODE_FAILURE
@ NI_RETCODE_FAILURE
Definition: ni_defs.h:440
_ni_encoder_cfg_params::HDR10dx1
int HDR10dx1
Definition: ni_device_api.h:2290
_ni_nvme_identity::hw2_codec_format
uint8_t hw2_codec_format
Definition: ni_nvme.h:195
_ni_encoder_config_t::ui32VuiDataSizeBytes
uint32_t ui32VuiDataSizeBytes
Definition: ni_device_api_priv.h:537
_ni_session_context::is_first_frame
int is_first_frame
Definition: ni_device_api.h:1438
_ni_t408_config_t::tmvpEnable
int32_t tmvpEnable
Definition: ni_device_api_priv.h:393
NI_HDR10P_SEI_BYTE4
#define NI_HDR10P_SEI_BYTE4
Definition: ni_device_api.h:458
_ni_encoder_config_t::ui8enableSSIM
uint8_t ui8enableSSIM
Definition: ni_device_api_priv.h:587
ni_ai_alloc_hwframe
ni_retcode_t ni_ai_alloc_hwframe(ni_session_context_t *p_ctx, int width, int height, int options, int pool_size, int frame_index)
Definition: ni_device_api_priv.c:17699
_ni_instance_mgr_general_status::fw_p2p_mem_usage
uint8_t fw_p2p_mem_usage
Definition: ni_device_api_priv.h:95
_ni_hw_capability::max_video_width
uint16_t max_video_width
Definition: ni_device_api.h:1143
_ni_decoder_config_t::ui32SourceWidth
uint32_t ui32SourceWidth
Definition: ni_device_api_priv.h:745
_ni_encoder_config_t::ui16maxFrameSize
uint16_t ui16maxFrameSize
Definition: ni_device_api_priv.h:586
_ni_encoder_config_t::ui32horOffset
uint32_t ui32horOffset
Definition: ni_device_api_priv.h:617
NI_MIN_BITRATE
#define NI_MIN_BITRATE
Definition: ni_device_api.h:114
_ni_encoder_cfg_params::planar
int planar
Definition: ni_device_api.h:2256
_ni_instance_mgr_general_status::admin_nsid
uint8_t admin_nsid
Definition: ni_device_api_priv.h:105
_ni_frame::start_of_stream
uint32_t start_of_stream
Definition: ni_device_api.h:2635
_ni_packet::buffer_size
uint32_t buffer_size
Definition: ni_device_api.h:2872
_ni_t408_config_t::decoding_refresh_type
int32_t decoding_refresh_type
Definition: ni_device_api_priv.h:377
_ni_instance_mgr_stream_info::picture_width
uint16_t picture_width
Definition: ni_device_api_priv.h:124
NI_RETCODE_PARAM_ERROR_GOP_PRESET
@ NI_RETCODE_PARAM_ERROR_GOP_PRESET
Definition: ni_defs.h:460
_ni_instance_mgr_general_status::fw_load
uint8_t fw_load
Definition: ni_device_api_priv.h:97
NI_RETCODE_PARAM_ERROR_HEIGHT_TOO_SMALL
@ NI_RETCODE_PARAM_ERROR_HEIGHT_TOO_SMALL
Definition: ni_defs.h:512
_ni_sei_header::type
uint8_t type
Definition: ni_device_api.h:348
_ni_session_context::dts_queue
ni_timestamp_table_t * dts_queue
Definition: ni_device_api.h:1520
_ni_encoder_frame_params::video_height
uint16_t video_height
Definition: ni_device_api_priv.h:203
_ni_frame::pts
long long pts
Definition: ni_device_api.h:2633
_ni_nvme_identity::hw2_video_level
uint8_t hw2_video_level
Definition: ni_nvme.h:202
_ni_xcoder_params::enable_vfr
int enable_vfr
Definition: ni_device_api.h:2808
_ni_decoder_config_t::ui8EnablelowDelayCheck
uint8_t ui8EnablelowDelayCheck
Definition: ni_device_api_priv.h:737
_ni_frame::inconsecutive_transfer
uint8_t inconsecutive_transfer
Definition: ni_device_api.h:2734
_ni_overall_load_query::overall_current_load
uint32_t overall_current_load
Definition: ni_device_api.h:1226
NI_RETCODE_PARAM_ERROR_FRATE
@ NI_RETCODE_PARAM_ERROR_FRATE
Definition: ni_defs.h:450
_ni_frame::crop_right
uint32_t crop_right
Definition: ni_device_api.h:2642
_ni_nvme_identity::hw1_video_level
uint8_t hw1_video_level
Definition: ni_nvme.h:189
_ni_session_context::last_framerate
ni_framerate_t last_framerate
Definition: ni_device_api.h:1677
NI_SCALER_FLAG_PC
#define NI_SCALER_FLAG_PC
Definition: ni_device_api.h:293
NI_RETCODE_PARAM_ERROR_CU_SIZE_MODE
@ NI_RETCODE_PARAM_ERROR_CU_SIZE_MODE
Definition: ni_defs.h:462
_ni_encoder_config_t::ui16HDR10MaxLight
uint16_t ui16HDR10MaxLight
Definition: ni_device_api_priv.h:543
_ni_encoder_cfg_params::rdoLevel
int rdoLevel
Definition: ni_device_api.h:2282
_ni_t408_config_t::slice_mode
int32_t slice_mode
Definition: ni_device_api_priv.h:493
_ni_encoder_cfg_params::long_term_ref_enable
int long_term_ref_enable
Definition: ni_device_api.h:2273
_ni_frame::pixel_format
int pixel_format
Definition: ni_device_api.h:2698
_ni_encoder_config_t::i32frameRateDenominator
int32_t i32frameRateDenominator
Definition: ni_device_api_priv.h:563
_ni_encoder_config_t::i32picWidth
int32_t i32picWidth
Definition: ni_device_api_priv.h:510
NI_T35_SEI_CLOSED_CAPTION
@ NI_T35_SEI_CLOSED_CAPTION
Definition: ni_device_api_priv.c:89
_ni_nvme_identity::ui16Ssvid
uint16_t ui16Ssvid
Definition: ni_nvme.h:94
NI_RETCODE_PARAM_ERROR_TOO_SMALL
@ NI_RETCODE_PARAM_ERROR_TOO_SMALL
Definition: ni_defs.h:502
_ni_decoder_config_t::ui8SurviveStreamErr
uint8_t ui8SurviveStreamErr
Definition: ni_device_api_priv.h:748
_ni_encoder_cfg_params::get_psnr_mode
int get_psnr_mode
Definition: ni_device_api.h:2444
ni_device_capability_query2
ni_retcode_t ni_device_capability_query2(ni_device_handle_t device_handle, ni_device_capability_t *p_cap, bool device_in_ctxt)
Query device and return device capability structure This function had replaced ni_device_capability_q...
Definition: ni_device_api.c:647
QUERY_INSTANCE_STREAM_INFO_R
#define QUERY_INSTANCE_STREAM_INFO_R(sid, instance)
Definition: ni_nvme.h:718
_ni_encoder_config_t::i32userQpMax
int32_t i32userQpMax
Definition: ni_device_api_priv.h:516
ni_util.h
Utility definitions.
_ni_session_context::av1_pkt_num
uint64_t av1_pkt_num
Definition: ni_device_api.h:1697
ni_decoder_output_config_t::ui8EnablePpuScaleAdapt
uint8_t ui8EnablePpuScaleAdapt
Definition: ni_device_api_priv.h:721
_ni_encoder_cfg_params::ltrNextInterval
int ltrNextInterval
Definition: ni_device_api.h:2302
_ni_decoder_input_params_t::survive_stream_err
int survive_stream_err
Definition: ni_device_api.h:2560
_ni_encoder_cfg_params::videoFullRange
int videoFullRange
Definition: ni_device_api.h:2304
ni_network_perf_metrics_t
struct _ni_network_perf_metrics ni_network_perf_metrics_t
_ni_session_context::last_pts
int64_t last_pts
Definition: ni_device_api.h:1439
ni_device_session_hwdl
int ni_device_session_hwdl(ni_session_context_t *p_ctx, ni_session_data_io_t *p_data, niFrameSurface1_t *hwdesc)
Reads YUV data from hw descriptor stored location on device.
Definition: ni_device_api.c:8295
ni_get_memory_offset
ni_retcode_t ni_get_memory_offset(ni_session_context_t *p_ctx, const niFrameSurface1_t *hwdesc, uint32_t *p_offset)
Get an address offset from a hw descriptor.
Definition: ni_device_api_priv.c:15979
_ni_encoder_cfg_params::intra_reset_refresh
int intra_reset_refresh
Definition: ni_device_api.h:2403
_ni_uploader_config_t::ui8poolSize
uint8_t ui8poolSize
Definition: ni_device_api_priv.h:668
SESSION_WRITE_CONFIG
@ SESSION_WRITE_CONFIG
Definition: ni_device_api_priv.h:43
ni_decode_cropping_rectangle::ui16Y
uint16_t ui16Y
Definition: ni_device_api_priv.h:701
_ni_network_buffer_info::ui32RelOffset
uint32_t ui32RelOffset
Definition: ni_device_api_priv.h:777
INST_BUF_INFO_RW_UPLOAD
@ INST_BUF_INFO_RW_UPLOAD
Definition: ni_device_api_priv.h:50
ni_device_set_ddr_configuration
ni_retcode_t ni_device_set_ddr_configuration(ni_session_context_t *p_ctx, uint8_t ddr_priority_mode)
Set DDR configuration of Quadra device.
Definition: ni_device_api_priv.c:18070
NI_PIX_FMT_10_TILED4X4
@ NI_PIX_FMT_10_TILED4X4
Definition: ni_device_api.h:277
_ni_encoder_cfg_params::slice_arg
int slice_arg
Definition: ni_device_api.h:2372
NI_H265_USERDATA_FLAG_UNREGISTERED_SUF
@ NI_H265_USERDATA_FLAG_UNREGISTERED_SUF
Definition: ni_device_api.h:369
_ni_frame::p_metadata_buffer
uint8_t * p_metadata_buffer
Definition: ni_device_api.h:2721
SESSION_READ_CONFIG
@ SESSION_READ_CONFIG
Definition: ni_device_api_priv.h:42
_ni_encoder_config_t::ui16hdr10_wy
uint16_t ui16hdr10_wy
Definition: ni_device_api_priv.h:596
_ni_encoder_config_t::i16ctbRowQpStep
int16_t i16ctbRowQpStep
Definition: ni_device_api_priv.h:608
WRITE_METADATA_W
#define WRITE_METADATA_W(sid, instance)
Definition: ni_nvme.h:675
lower_pixel_rate
int lower_pixel_rate(const ni_load_query_t *pQuery, uint32_t ui32CurrentLowest)
Definition: ni_device_api_priv.c:18679
NI_GET_MIN_HWDESC_P2P_BUF_ID
#define NI_GET_MIN_HWDESC_P2P_BUF_ID(x)
Definition: ni_defs.h:282
_ni_nvme_identity::xcoder_num_elements
uint8_t xcoder_num_elements
Definition: ni_nvme.h:229
_ni_t408_config_t::nrNoiseSigmaCr
uint32_t nrNoiseSigmaCr
Definition: ni_device_api_priv.h:438
ni_frame_buffer_alloc_dl
ni_retcode_t ni_frame_buffer_alloc_dl(ni_frame_t *p_frame, int video_width, int video_height, int pixel_format)
Allocate preliminary memory for the frame buffer based on provided parameters.
Definition: ni_device_api.c:2393
_ni_encoder_cfg_params::lookAheadDepth
int lookAheadDepth
Definition: ni_device_api.h:2281
NI_MAX_AI_NETWORK_BINARY_BUFFER_QUERY_RETRIES
#define NI_MAX_AI_NETWORK_BINARY_BUFFER_QUERY_RETRIES
Definition: ni_device_api_priv.h:832
NI_RETCODE_ERROR_INVALID_HANDLE
@ NI_RETCODE_ERROR_INVALID_HANDLE
Definition: ni_defs.h:523
_ni_encoder_config_t::ui8baseLayerOnly
uint8_t ui8baseLayerOnly
Definition: ni_device_api_priv.h:658
NI_RETCODE_PARAM_ERROR_INTRA_QP
@ NI_RETCODE_PARAM_ERROR_INTRA_QP
Definition: ni_defs.h:459
_ni_packet::still_image_detected
uint8_t still_image_detected
Definition: ni_device_api.h:2892
_ni_frame_config::rectangle_y
int16_t rectangle_y
Definition: ni_device_api.h:2847
NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL
@ NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL
Definition: ni_defs.h:546
_ni_t408_config_t::independSliceMode
int32_t independSliceMode
Definition: ni_device_api_priv.h:384
ni_dump_log_single_core
ni_retcode_t ni_dump_log_single_core(ni_session_context_t *p_ctx, void *p_data, uint32_t core_id, bool gen_log_file)
Definition: ni_device_api_priv.c:18420
_ni_custom_gop_params::pic_param
ni_gop_params_t pic_param[NI_MAX_GOP_NUM]
Definition: ni_device_api.h:2060
_ni_session_context::pic_reorder_delay
int pic_reorder_delay
Definition: ni_device_api.h:1643
ni_encoder_session_read
int ni_encoder_session_read(ni_session_context_t *p_ctx, ni_packet_t *p_packet)
Definition: ni_device_api_priv.c:4583
_ni_instance_mgr_general_status::active_sub_instances_cnt
uint8_t active_sub_instances_cnt
Definition: ni_device_api_priv.h:81
ni_aligned_free
#define ni_aligned_free(p_memptr)
Definition: ni_util.h:399
_ni_session_context::dev
unsigned short dev
Definition: ni_device_api.h:1623
_ni_xcoder_params::low_delay_mode
int low_delay_mode
Definition: ni_device_api.h:2763
_ni_decoder_input_params_t::decoder_low_delay
int decoder_low_delay
Definition: ni_device_api.h:2538
_ni_metadata_enc_bstream::av1_show_frame
uint32_t av1_show_frame
Definition: ni_device_api_priv.h:282
_ni_metadata_enc_bstream::reconChromaWidth
uint16_t reconChromaWidth
Definition: ni_device_api_priv.h:301
_ni_decoder_input_params_t::nb_save_pkt
int nb_save_pkt
Definition: ni_device_api.h:2524
_ni_device_vf_ns_id::vf_id
uint16_t vf_id
Definition: ni_rsrc_api.h:231
OPT_1
@ OPT_1
Definition: ni_device_api_priv.c:95
_ni_packet::video_width
uint32_t video_width
Definition: ni_device_api.h:2863
NI_MIN_RESOLUTION_HEIGHT
#define NI_MIN_RESOLUTION_HEIGHT
Definition: ni_device_api.h:87
QUERY_INSTANCE_METRICS_R
#define QUERY_INSTANCE_METRICS_R(sid, instance)
Definition: ni_nvme.h:775
_ni_encoder_config_t::ui8colorDescPresent
uint8_t ui8colorDescPresent
Definition: ni_device_api_priv.h:554
_ni_metadata_enc_frame::frame_roi_avg_qp
uint32_t frame_roi_avg_qp
Definition: ni_device_api_priv.h:243
_ni_session_context::pkt_num
uint64_t pkt_num
Definition: ni_device_api.h:1534
_ni_encoder_cfg_params::enable_cu_level_rate_control
int enable_cu_level_rate_control
Definition: ni_device_api.h:2387
_ni_session_context::iocbs
ni_iocb_t ** iocbs
Definition: ni_device_api.h:1729
_ni_decoder_input_params_t::disable_adaptive_buffers
int disable_adaptive_buffers
Definition: ni_device_api.h:2558
NI_SCALER_OPCODE_ROTATE
@ NI_SCALER_OPCODE_ROTATE
Definition: ni_defs.h:591
NI_CODEC_FORMAT_H264
@ NI_CODEC_FORMAT_H264
Definition: ni_device_api.h:913
_ni_frame::use_cur_src_as_long_term_pic
uint8_t use_cur_src_as_long_term_pic
Definition: ni_device_api.h:2647
INST_BUF_INFO_RW_WRITE
@ INST_BUF_INFO_RW_WRITE
Definition: ni_device_api_priv.h:49
ni_ai_session_read_hwdesc
ni_retcode_t ni_ai_session_read_hwdesc(ni_session_context_t *p_ctx, ni_frame_t *p_frame)
read a hardware descriptor from a scaler session
Definition: ni_device_api_priv.c:17893
_ni_session_stats::ui16ErrorCount
uint16_t ui16ErrorCount
Definition: ni_device_api_priv.h:139
_ni_thread_arg_struct_t::plast_access_time
volatile uint64_t * plast_access_time
Definition: ni_device_api.h:1271
_ni_frame::start_buffer_size
uint32_t start_buffer_size
Definition: ni_device_api.h:2729
_ni_decoder_config_t::ui8UduSeiEnabled
uint8_t ui8UduSeiEnabled
Definition: ni_device_api_priv.h:731
_ni_t408_config_t::independSliceModeArg
int32_t independSliceModeArg
Definition: ni_device_api_priv.h:385
NI_MAX_BITRATE
#define NI_MAX_BITRATE
Definition: ni_device_api.h:113
NI_RETCODE_PARAM_ERROR_OOR
@ NI_RETCODE_PARAM_ERROR_OOR
Definition: ni_defs.h:505
ni_log_get_utime
uint64_t ni_log_get_utime()
Get time for logs with microsecond timestamps.
Definition: ni_log.c:308
_ni_frame::video_width
uint32_t video_width
Definition: ni_device_api.h:2636
_ni_t408_config_t::pu32DeltaRate
int32_t pu32DeltaRate
Definition: ni_device_api_priv.h:458
NI_QUADRA_MEMORY_CONFIG_SR
#define NI_QUADRA_MEMORY_CONFIG_SR
Definition: ni_device_api_priv.h:824
_ni_session_context::initial_frame_delay
int initial_frame_delay
Definition: ni_device_api.h:1694
_ni_xcoder_params::bitrate
int bitrate
Definition: ni_device_api.h:2759
_ni_encoder_cfg_params::HDR10wy
int HDR10wy
Definition: ni_device_api.h:2295
NI_MIN_WIDTH
#define NI_MIN_WIDTH
Definition: ni_device_api.h:124
_ni_encoder_config_t::hdrEnableVUI
uint32_t hdrEnableVUI
Definition: ni_device_api_priv.h:535
ni_device_session_context_init
ni_retcode_t ni_device_session_context_init(ni_session_context_t *p_ctx)
Initialize already allocated session context to a known state.
Definition: ni_device_api.c:156
_ni_encoder_config_t::ui16iFrameSizeRatio
uint16_t ui16iFrameSizeRatio
Definition: ni_device_api_priv.h:603
_ni_encoder_cfg_params::preferred_transfer_characteristics
int preferred_transfer_characteristics
Definition: ni_device_api.h:2279
_ni_t408_config_t::intra_mb_refresh_arg
int32_t intra_mb_refresh_arg
Definition: ni_device_api_priv.h:496
_ni_session_context::pixel_format
int pixel_format
Definition: ni_device_api.h:1618
_ni_instance_mgr_general_status::active_hwupload_sub_inst_cnt
uint8_t active_hwupload_sub_inst_cnt
Definition: ni_device_api_priv.h:96
_ni_xcoder_params::use_low_delay_poc_type
int use_low_delay_poc_type
Definition: ni_device_api.h:2766
_ni_frame::flags
int flags
Definition: ni_device_api.h:2718
_ni_encoder_cfg_params::conf_win_top
int conf_win_top
Definition: ni_device_api.h:2311
ni_params_print
void ni_params_print(ni_xcoder_params_t *const p_encoder_params)
Print xcoder user configurations.
Definition: ni_device_api_priv.c:13025
_ni_encoder_config_t::ui8EnableAcqLimit
uint8_t ui8EnableAcqLimit
Definition: ni_device_api_priv.h:642
_ni_t408_config_t::nrInterWeightCr
uint32_t nrInterWeightCr
Definition: ni_device_api_priv.h:433
_ni_metadata_enc_bstream::reconChromaSize
uint32_t reconChromaSize
Definition: ni_device_api_priv.h:299
ni_ai_query_network_ready
ni_retcode_t ni_ai_query_network_ready(ni_session_context_t *p_ctx)
Definition: ni_device_api_priv.c:16371
_ni_session_context::sei_trailer
uint8_t sei_trailer[NI_CC_SEI_TRAILER_LEN]
Definition: ni_device_api.h:1424
_ni_encoder_config_t::ui8noMbtree
uint8_t ui8noMbtree
Definition: ni_device_api_priv.h:623
QUADRA
#define QUADRA
Definition: ni_defs.h:123
ni_ai_network_layer_size
uint32_t ni_ai_network_layer_size(ni_network_layer_params_t *p_param)
Definition: ni_util.c:3363
CHECK_VPU_RECOVERY
#define CHECK_VPU_RECOVERY(ret)
Definition: ni_device_api_priv.c:753
_ni_t408_config_t::chromaCbQpOffset
int32_t chromaCbQpOffset
Definition: ni_device_api_priv.h:418
_ni_metadata_enc_bstream::frame_type
uint32_t frame_type
Definition: ni_device_api_priv.h:277
STD_JPEG
@ STD_JPEG
Definition: ni_device_api_priv.h:364
_ni_nvme_identity::hw2_codec_type
uint8_t hw2_codec_type
Definition: ni_nvme.h:196
_ni_encoder_config_t::ui32VuiDataSizeBits
uint32_t ui32VuiDataSizeBits
Definition: ni_device_api_priv.h:536
_ni_encoder_cfg_params::bitrateMode
int bitrateMode
Definition: ni_device_api.h:2422
_ni_encoder_config_t::ui8inLoopDSRatio
uint8_t ui8inLoopDSRatio
Definition: ni_device_api_priv.h:580
_ni_nvme_identity::ai8Fr
uint8_t ai8Fr[8]
Definition: ni_nvme.h:97
_ni_metadata_dec_frame::sei_size
uint16_t sei_size
Definition: ni_device_api_priv.h:235
_ni_session_context::pkt_custom_sei_set
ni_custom_sei_set_t * pkt_custom_sei_set[NI_FIFO_SZ]
Definition: ni_device_api.h:1458
NI_PIXEL_PLANAR_FORMAT_TILED4X4
@ NI_PIXEL_PLANAR_FORMAT_TILED4X4
Definition: ni_device_api.h:924
NI_LOG_DEBUG
@ NI_LOG_DEBUG
Definition: ni_log.h:62
NI_CC_SEI_BYTE5
#define NI_CC_SEI_BYTE5
Definition: ni_device_api.h:450
_ni_metadata_enc_frame::force_pic_qp_enable
uint16_t force_pic_qp_enable
Definition: ni_device_api_priv.h:249
get_dev_pcie_addr
void get_dev_pcie_addr(char *device_name, char *pcie, char *domain, char *slot, char *dev, char *func)
_ni_nvme_identity::hw0_codec_type
uint8_t hw0_codec_type
Definition: ni_nvme.h:170
_ni_frame::crop_bottom
uint32_t crop_bottom
Definition: ni_device_api.h:2640
NI_ENC_REPEAT_HEADERS_ALL_I_FRAMES
#define NI_ENC_REPEAT_HEADERS_ALL_I_FRAMES
Definition: ni_device_api.h:2064
_ni_t408_config_t::pu04IntraAngleDeltaRate
int32_t pu04IntraAngleDeltaRate
Definition: ni_device_api_priv.h:461
_ni_xcoder_params::dec_input_params
ni_decoder_input_params_t dec_input_params
Definition: ni_device_api.h:2796
_ni_network_perf_metrics::total_cycles
uint32_t total_cycles
Definition: ni_device_api.h:1388
_ni_decoder_config_t::ui8EnableAdvancedEc
uint8_t ui8EnableAdvancedEc
Definition: ni_device_api_priv.h:742
STD_HEVC
@ STD_HEVC
Definition: ni_device_api_priv.h:359
_ni_metadata_common::frame_type
uint16_t frame_type
Definition: ni_device_api_priv.h:221
_ni_metadata_dec_frame::first_sei_header
ni_sei_header_t first_sei_header
Definition: ni_device_api_priv.h:232
STD_AVC
@ STD_AVC
Definition: ni_device_api_priv.h:348
_ni_sei_header
Definition: ni_device_api.h:345
_ni_nvme_identity::fw_branch_name
uint8_t fw_branch_name[256]
Definition: ni_nvme.h:218
_ni_metadata_enc_bstream::ui8SceneChange
uint8_t ui8SceneChange
Definition: ni_device_api_priv.h:305
_ni_nvme_identity::fw_commit_time
uint8_t fw_commit_time[26]
Definition: ni_nvme.h:219
_ni_gop_params::temporal_id
int temporal_id
Definition: ni_device_api.h:1971
_ni_global_session_stats
Definition: ni_device_api_priv.h:149
ni_get_log_lba
uint32_t ni_get_log_lba(ni_core_type_t eCoreType)
Definition: ni_device_api_priv.c:18391
NI_PIXEL_PLANAR_FORMAT_PLANAR
@ NI_PIXEL_PLANAR_FORMAT_PLANAR
Definition: ni_device_api.h:923
_ni_encoder_config_t::ui8noHWMultiPassSupport
uint8_t ui8noHWMultiPassSupport
Definition: ni_device_api_priv.h:624
_ni_frame::p_custom_sei_set
ni_custom_sei_set_t * p_custom_sei_set
Definition: ni_device_api.h:2700
ni_encoder_session_sequence_change
ni_retcode_t ni_encoder_session_sequence_change(ni_session_context_t *p_ctx, ni_resolution_t *p_resolution)
Send sequnce change to a xcoder encoder instance.
Definition: ni_device_api_priv.c:5019
_ni_t408_config_t::pu08IntraAngleDeltaRate
int32_t pu08IntraAngleDeltaRate
Definition: ni_device_api_priv.h:464
_ni_packet::avg_frame_qp
uint32_t avg_frame_qp
Definition: ni_device_api.h:2873
_ni_encoder_config_t::ui8spatialLayersMinusOne
uint8_t ui8spatialLayersMinusOne
Definition: ni_device_api_priv.h:650
_ni_session_context::aio_context
ni_aio_context_t aio_context
Definition: ni_device_api.h:1728
ni_decoder_frame_buffer_free
ni_retcode_t ni_decoder_frame_buffer_free(ni_frame_t *p_frame)
Free decoder frame buffer that was previously allocated with ni_decoder_frame_buffer_alloc,...
Definition: ni_device_api.c:3645
_ni_encoder_frame_params::video_width
uint16_t video_width
Definition: ni_device_api_priv.h:202
_ni_session_context::roi_avg_qp
uint32_t roi_avg_qp
Definition: ni_device_api.h:1499
ni_decoder_session_close
ni_retcode_t ni_decoder_session_close(ni_session_context_t *p_ctx, int eos_recieved)
Close a xcoder decoder instance.
Definition: ni_device_api_priv.c:1614
_ni_encoder_config_t::ui8EnableAUD
uint8_t ui8EnableAUD
Definition: ni_device_api_priv.h:539
_ni_device_capability::device_is_xcoder
uint8_t device_is_xcoder
Definition: ni_device_api.h:1157
ni_frameclone_desc_t
struct _ni_frameclone_desc ni_frameclone_desc_t
_ni_session_context::force_low_delay_cnt
uint32_t force_low_delay_cnt
Definition: ni_device_api.h:1702
NI_MAX_RESOLUTION_AREA
#define NI_MAX_RESOLUTION_AREA
Definition: ni_device_api.h:102
INST_BUF_INFO_RW_READ_BY_AI
@ INST_BUF_INFO_RW_READ_BY_AI
Definition: ni_device_api_priv.h:55
NI_RETCODE_ERROR_RESOURCE_UNAVAILABLE
@ NI_RETCODE_ERROR_RESOURCE_UNAVAILABLE
Definition: ni_defs.h:446
NI_RETCODE_PARAM_ERROR_MN_QP
@ NI_RETCODE_PARAM_ERROR_MN_QP
Definition: ni_defs.h:471
_ni_session_context::last_frame_offset
uint64_t last_frame_offset
Definition: ni_device_api.h:1457
_ni_encoder_cfg_params::forced_header_enable
int forced_header_enable
Definition: ni_device_api.h:2272
_ni_hw_capability::max_number_of_contexts
uint8_t max_number_of_contexts
Definition: ni_device_api.h:1139
ni_get_bitdepth_factor_from_pixfmt
int ni_get_bitdepth_factor_from_pixfmt(int pix_fmt)
Grab bitdepth factor from NI_PIX_FMT.
Definition: ni_device_api_priv.c:15913
_ni_session_context::session_statistic
ni_session_statistic_t session_statistic
Definition: ni_device_api.h:1652
_ni_encoder_cfg_params::spatial_layers_ref_base_layer
int spatial_layers_ref_base_layer
Definition: ni_device_api.h:2455
_ni_session_stats::ui32LastErrorStatus
uint32_t ui32LastErrorStatus
Definition: ni_device_api_priv.h:143
_ni_session_context::async_mode
int async_mode
Definition: ni_device_api.h:1665
IDENTIFY_DEVICE_R
#define IDENTIFY_DEVICE_R
Definition: ni_nvme.h:686
_ni_scaler_params_t::scaler_param_c
double scaler_param_c
Definition: ni_device_api.h:2592
ni_assert
#define ni_assert(expression)
Definition: ni_defs.h:339
_ni_t408_config_t::pu08DeltaRate
int32_t pu08DeltaRate
Definition: ni_device_api_priv.h:456
_ni_encoder_frame_params
Definition: ni_device_api_priv.h:197
NI_CODEC_FORMAT_H265
@ NI_CODEC_FORMAT_H265
Definition: ni_device_api.h:914
_ni_encoder_config_t::i32forceBframeQpFactor
int32_t i32forceBframeQpFactor
Definition: ni_device_api_priv.h:640
_ni_load_query::active_hwuploaders
uint32_t active_hwuploaders
Definition: ni_device_api.h:1215
_ni_frame::use_long_term_ref
uint8_t use_long_term_ref
Definition: ni_device_api.h:2648
NI_RETCODE_PARAM_ERROR_INTRA_PERIOD
@ NI_RETCODE_PARAM_ERROR_INTRA_PERIOD
Definition: ni_defs.h:457
_ni_encoder_config_t::ui8NewRCEnable
uint8_t ui8NewRCEnable
Definition: ni_device_api_priv.h:609
_ni_session_context::last_access_time
volatile uint64_t last_access_time
Definition: ni_device_api.h:1685
QUERY_INSTANCE_NL_SIZE_V2_R
#define QUERY_INSTANCE_NL_SIZE_V2_R(sid, instance)
Definition: ni_nvme.h:763
_ni_packet::data_len
uint32_t data_len
Definition: ni_device_api.h:2868